How to correctly default initialize CMAKE_BUILD_TYPE inside CMakeLists.txt

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

How to correctly default initialize CMAKE_BUILD_TYPE inside CMakeLists.txt

Kim Walisch
Hi,

By default I would like to set CMAKE_BUILD_TYPE=Release (inside CMakeLists.txt) if the user has not defined CMAKE_BUILD_TYPE himself (e.g. as command-line option: cmake . -DCMAKE_BUILD_TYPE=Debug). Is there a best practice how to do this in CMake? I have been searching the web to see how other people achieved this and I found that many people use code like this:

if(NOT CMAKE_BUILD_TYPE)
        set(CMAKE_BUILD_TYPE "Release" CACHE STRING
        "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel."
        FORCE)
endif()

I used this code myself in one of my projects but I recently discovered that this code causes issues when compiling with clang-cl on Windows.

Greetings,
Kim

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: How to correctly default initialize CMAKE_BUILD_TYPE inside CMakeLists.txt

frodak17


On Mon, Jan 14, 2019 at 3:48 AM Kim Walisch <[hidden email]> wrote:
Hi,

By default I would like to set CMAKE_BUILD_TYPE=Release (inside CMakeLists.txt) if the user has not defined CMAKE_BUILD_TYPE himself (e.g. as command-line option: cmake . -DCMAKE_BUILD_TYPE=Debug). Is there a best practice how to do this in CMake? I have been searching the web to see how other people achieved this and I found that many people use code like this:

if(NOT CMAKE_BUILD_TYPE)
        set(CMAKE_BUILD_TYPE "Release" CACHE STRING
        "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel."
        FORCE)
endif()

I used this code myself in one of my projects but I recently discovered that this code causes issues when compiling with clang-cl on Windows.

Greetings,
Kim


Which part of that causes the problem?  It sounds like there is an issue with Release mode flags.
Wouldn't you just have the same problem if "cmake -DCMAKE_BUILD_TYPE=Release" was used on the command line?
I guess maybe it could be a problem if it wasn't one of the first things done and it changed mid-processing.


--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: How to correctly default initialize CMAKE_BUILD_TYPE inside CMakeLists.txt

frodak17


On Mon, Jan 14, 2019 at 1:09 PM Kim Walisch <[hidden email]> wrote:
Based on feedback from Craig Scott I now use the following code:

if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.9)
         get_property(isMultiConfig GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
elseif(CMAKE_CONFIGURATION_TYPES)
         set(isMultiConfig TRUE)
endif()

if(NOT isMultiConfig AND NOT CMAKE_BUILD_TYPE)
        set(CMAKE_BUILD_TYPE Release CACHE STRING
        "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel." FORCE)
endif()

This code sets CMAKE_BUILD_TYPE only for single configuration generators (like GNU make or Ninja) because multi configuration generators (like Xcode or Visual Studio) generally ignore CMAKE_BUILD_TYPE. For an in depth explanation please refer to chapter 13 of Scott's excellent "Professional CMake" book (https://crascit.com/professional-cmake).

Regards,
Kim


There is nothing particularly wrong with setting CMAKE_BUILD_TYPE to Release in a CMakeLists.txt except that then you can't use the default settings of when it is not set.

In general a CMakeLists.txt that supports Multi-configuration generators will use the generator expressions to determine the configuration type.  In those cases CMAKE_BUILD_TYPE is not used and doesn't show up in any script.

Other scripts may check if CMAKE_BUILD_TYPE is set to enable additional flags or add in special files and those scripts won't usually work as expected with a multi-configuration generator because Debug / Release can be chosen at build time.

If you have to work around something that is going to break because it expects CMAKE_BUILD_TYPE to never be set then it won't work for single configuration generators when the user sets CMAKE_BUILD_TYPE to Release.

Both types of generators are available for use when working with clang on windows.

You never mentioned what part didn't work or what generator you are using, but if only setting CMAKE_BUILD_TYPE when a single configuration generator is used gets you past the problem then that works too.

Regards,
F


--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake