Install CMake for MSYS

Post date: Oct 08, 2010 10:37:49 PM

CMake is a cross-platform build system. A software project would be tied to a specific environment without a build system. CMake detects the local environment, such as compiler, libraries, and system commands. I believe automake/autoconf is a similar system. It is not a replacement for the "make" utility, it is used to generate the makefile used by make.

Installation

Installing CMake is a requirement for compiling some cross-platform software. Follow these steps:

    • Go to the CMake download page.

    • Under "Binary distributions", download the win32-x86 ZIP (current version is 3.7.1)

    • Extract the .zip file to C:\dev

      • or wget https://cmake.org/files/v3.7/cmake-3.7.1-win32-x86.zip

    • Add the cmake bin directory to the PATH environment in your MSYS .profile:

      • PATH=$PATH:/c/dev/cmake-3.7.1-win32-x86/bin (don't delete your other PATHs!)

Check the installation by starting MinGW Shell and running "cmake --version".

Generic build with CMake and make

    • Start the MinGW shell

  • cd $HOME/project_name/build/

  • cmake .. -G"MSYS Makefiles"

  • make

Building DFHack with CMake and make

Download the DFHack source code to the dfhack directory, as described in the Git tutorial.

The source code includes instructions for building in Linux and MinGW, but not MSYS specifically. Follow these steps to compile dfhack with MSYS:

    • Start the MinGW Shell

  • cd $HOME/dfhack/build

  • cmake .. -G"MSYS Makefiles" -DCMAKE_BUILD_TYPE:STRING=Release

      • This creates MSYS compatible "Makefile", using the "Release" build options, for parent directory ".."

  • make

If all went well, you should be able to run the compiled DFHack utilities. Start a game of Dwarf Fortress, then try running "dfattachtest.exe" in the dfhack/output directory. If there is an error about shared libraries, you will need to copy libgcc_s_dw2-1.dll and libstdc++-6.dll from C:\dev\MinGW\bin\.

Adding extra compiler options via CMake

You can statically link libgcc by adding options "-static-libgcc -static-libstdc++" to the linking step of the build. Here's a way to have these generated in the Makefile by CMake (The \ is used to ignore the newline):

  • cmake .. -G"MSYS Makefiles" -DCMAKE_BUILD_TYPE:STRING=Release \

  • -DCMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS:STRING="-static-libgcc -static-libstdc++"

It might be easier to create a build script when you want to add a lot of flags:

  • vi build-msys.sh

#Build DFHack in MSYS

LINK_FLAGS="-static-libgcc -static-libstdc++ --enable-auto-import"

cd $HOME/dfhack/build

cmake .. -G "MSYS Makefiles" -DCMAKE_BUILD_TYPE:STRING=Release \

-DCMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS:STRING=$LINK_FLAGS \

-DCMAKE_SHARED_LINKER_FLAGS:STRING=$LINK_FLAGS

make

  • build-msys.sh

I had a couple cases where my build failed due to permission error. I ran "make" again and it completed.

Notice that statically linking the standard libraries increased the file size by about 1000KB per executable! If you're distributing a lot of binaries that have a common shared library, it may be better to keep it dynamically linked.

If you are distributing GNU GCC shared libraries, you need to include the source code of those libraries. Decide if the size of the source code is more than the extra space from statically linking :)

What Now?

CMake can be used when packaging your source code to be compiled on other platforms. I don't know how to do that yet! The CMake Wiki has more information. I would put off learning CMake until you are ready to distribute your project code to Windows and Unix.