Search code examples
cachingcmakecmake-gui

Switch between different build settings


Sorry for the vague title, I'm not sure how to phrase this correctly. I would like to write a cmake script that allows to build a target with different settings for bit width (forced 32 bit, forced 64 bit or native bit width) and static linking. I figured out how to set up the build under each condition and so far I'm using cmake options to switch between different setups.

My problem is that changing one of these build options with ccmake or on the command line also requires to look for new library paths. Since these paths are cached, I currently have to delete the cache when changing bit width. This way users also loose all other settings for options that are independent of bit width and static linking.

Is there a common way to handle this?


Solution

  • Use different build directories for different settings:

    • cmake -H. -B_builds/arch64 -DCMAKE_CXX_FLAGS=-m64
    • cmake -H. -B_builds/arch32 -DCMAKE_CXX_FLAGS=-m32
    • cmake -H. -B_builds/shared -DBUILD_SHARED_LIBS=ON
    • cmake -H. -B_builds/static -DBUILD_SHARED_LIBS=OFF
    • cmake -H. -B_builds/debug -DCMAKE_BUILD_TYPE=Debug
    • cmake -H. -B_builds/release -DCMAKE_BUILD_TYPE=Release

    Exceptions

    Note that in each case there may be exceptions, like:

    add_library(foo STATIC ${FOO_SOURCES}) # BUILD_SHARED_LIBS will be ignored
    

    or for Visual Studio and Xcode Debug/Release will be:

    cmake -H. -B_builds/xcode -GXcode
    cmake --build _builds/xcode --config Debug # build Debug
    cmake --build _builds/xcode --config Release # build Release
    

    instead of xcode-debug and xcode-release

    Related