Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Detect current CMake configuration type. How?
Forum Updated to NodeBB v4.3 + New Features

Detect current CMake configuration type. How?

Scheduled Pinned Locked Moved Solved General and Desktop
19 Posts 3 Posters 223 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bogong
    wrote last edited by
    #8

    It's not the type of the build. Need to know type of the build. The example with file is one of use cases.

    Need to know how to detect build type and why in Qt Creator NOT working ${CMAKE_BUILD_TYPE} variable. It's just empty.

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote last edited by
      #9

      As I said - multi-configuration generators like msbuild or even Ninja nowadays (afair) don't have a build type during configure time.
      If you want to add sources depeding on the build type use what the documentation and I wrote.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      0
      • B Offline
        B Offline
        bogong
        wrote last edited by
        #10

        In my project only XCode used. Why the simplicity of just "if-else" exchanged on complexity of the tricks of f-wording generators???

        I 1 Reply Last reply
        0
        • B Offline
          B Offline
          bogong
          wrote last edited by bogong
          #11

          Based on Debug or Release even Qt Creator were showing different files. It was making developing process simple and clear. For now because of this generators need to add all of the versions of the files and only one will be used when building but all of them in sources in Qt Creator.

          1 Reply Last reply
          0
          • B bogong

            In my project only XCode used. Why the simplicity of just "if-else" exchanged on complexity of the tricks of f-wording generators???

            I Offline
            I Offline
            IgKh
            wrote last edited by
            #12

            @bogong said in Detect current CMake configuration type. How?:

            Why the simplicity of just "if-else" exchanged on complexity of the tricks of f-wording generators

            There is no need for the kind of language here.

            CMake is complex, because cross-platform build engineering is inherently complex. When you use a tool you need to understand it, and the fact that CMake has distinct configuration, generation and build phases (each with its' own way of specifying what should run at that phase) is often uncomfortable but important.

            @bogong said in Detect current CMake configuration type. How?:

            ${CMAKE_BUILD_TYPE} variable. It's just empty.

            @bogong said in Detect current CMake configuration type. How?:

            In my project only XCode used

            Xcode is exactly one of the generators that are multi-configuration (alongside MSBuild); where the same set of final build system files (i.e post configuration/generation) is used to make all build configuration. The variable is empty because variables are set during the configuration phase, and it wasn't set. As you shown, the actual build configuration is only passed to the cmake --build invocation - hence only available during the build phase.

            This is pretty unavoidable, even if you try to manually pass -DCMAKE_BUILD_TYPE=Debug or something to CMake directly alongside -G Xcode to do a a manual CMake configuration, it will just be ignored. That's how Xcode project files work... If that's what Qt Creator's wrapper is doing, what Qt Creator shows is of no relevance.

            Also see this note in CMake documentation: https://cmake.org/cmake/help/v4.1/generator/Xcode.html#limitations

            @bogong said in Detect current CMake configuration type. How?:

            For example for iOS for Debug - one Info.plist file for Release another one set of the files.

            Even on generators that do support that (and again, Xcode doesn't) CMake doesn't make this pattern very easy - mostly because it is a bit of an abuse of the concept of build configurations. Configurations are meant to affect the flags that are passed to the build tools when making targets, not so much the shape of the dependency graph of the targets among themselves.

            Luckily that's not what you are actually trying to do (although you appear to think so) - you don't want different files between build configurations, you want different content for the same file. That can certainly be done, with a custom build target. This is completely unrelated to Qt, but there are many examples around and if you want to delve into this the CMake Discourse site will be more appropriate place. You'll probably love the syntax even less than the one for generator expressions though... (not that you have much of a choice, really).

            B 1 Reply Last reply
            1
            • Christian EhrlicherC Christian Ehrlicher

              Now we finally have a usecase where we also can answer...
              See https://cmake.org/cmake/help/latest/command/target_sources.html

              target_sources(MyTarget PRIVATE "$<$<CONFIG:Debug>:${CMAKE_CURRENT_SOURCE_DIR}/dbgsrc.cpp>")

              B Offline
              B Offline
              bogong
              wrote last edited by bogong
              #13

              @Christian-Ehrlicher said in Detect current CMake configuration type. How?:

              "$<$CONFIG:Debug:${CMAKE_CURRENT_SOURCE_DIR}/dbgsrc.cpp>"

              Screenshot 2025-09-28 at 08.53.06.png

              This is what in result when using your way. The CMake settings is:

              qt_add_executable(${A_NAME_APPLICATION} "$<$<CONFIG:Debug>:Platforms/Debug/IOS/Info.plist>")
              

              Is it next one Qt Creator bug? Or it's normal behavior?

              1 Reply Last reply
              0
              • I IgKh

                @bogong said in Detect current CMake configuration type. How?:

                Why the simplicity of just "if-else" exchanged on complexity of the tricks of f-wording generators

                There is no need for the kind of language here.

                CMake is complex, because cross-platform build engineering is inherently complex. When you use a tool you need to understand it, and the fact that CMake has distinct configuration, generation and build phases (each with its' own way of specifying what should run at that phase) is often uncomfortable but important.

                @bogong said in Detect current CMake configuration type. How?:

                ${CMAKE_BUILD_TYPE} variable. It's just empty.

                @bogong said in Detect current CMake configuration type. How?:

                In my project only XCode used

                Xcode is exactly one of the generators that are multi-configuration (alongside MSBuild); where the same set of final build system files (i.e post configuration/generation) is used to make all build configuration. The variable is empty because variables are set during the configuration phase, and it wasn't set. As you shown, the actual build configuration is only passed to the cmake --build invocation - hence only available during the build phase.

                This is pretty unavoidable, even if you try to manually pass -DCMAKE_BUILD_TYPE=Debug or something to CMake directly alongside -G Xcode to do a a manual CMake configuration, it will just be ignored. That's how Xcode project files work... If that's what Qt Creator's wrapper is doing, what Qt Creator shows is of no relevance.

                Also see this note in CMake documentation: https://cmake.org/cmake/help/v4.1/generator/Xcode.html#limitations

                @bogong said in Detect current CMake configuration type. How?:

                For example for iOS for Debug - one Info.plist file for Release another one set of the files.

                Even on generators that do support that (and again, Xcode doesn't) CMake doesn't make this pattern very easy - mostly because it is a bit of an abuse of the concept of build configurations. Configurations are meant to affect the flags that are passed to the build tools when making targets, not so much the shape of the dependency graph of the targets among themselves.

                Luckily that's not what you are actually trying to do (although you appear to think so) - you don't want different files between build configurations, you want different content for the same file. That can certainly be done, with a custom build target. This is completely unrelated to Qt, but there are many examples around and if you want to delve into this the CMake Discourse site will be more appropriate place. You'll probably love the syntax even less than the one for generator expressions though... (not that you have much of a choice, really).

                B Offline
                B Offline
                bogong
                wrote last edited by bogong
                #14

                @IgKh

                1. Global goal not use manual settings in Qt Creator or command line. It's required by updating procedures when Qt Creator could be broken just because of update.

                2. After 27 years of developing and about +15 year cross-platform in it haven't found complexity in this process when using simplicity of "if-else". For now need (or must) to redesign entire approach for projects development only because someone decided something about "modern and progress".

                3. When you using setting up manual settings in Qt Creator for CMAKE_BUILD_TYPE variable you can't have automated build. You need to rewrite this every time when you switching release and debug version. Only because of Qt bugs, when Debug version not exactly Release version it's important to have ability to rebuild with different types of config rapidly without additional "manual" activities. For now only way - to use global system variable for each of config that will be attached to the Qt Creator environment. It means every update will require me to resetup it because Qt Creator clearing everything when updating or could be rising error for previous versions configs. There are a lot of bugs posted by me related to the Qt Creator.


                Beside everything mentioned above - when asking on this forum means manuals, official, AI (less useful by the way, 8 from 10 question supplied by wrong reply), Google or other sources researched by me for the solution. Here asking only because of pro from Qt Company writing here.

                1 Reply Last reply
                0
                • Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote last edited by
                  #15

                  You mix QtCreator and CMake here for unknown reasons. Also you still mix configure and build time stuff but I would guess this is by intention...
                  XCode sadly does not support the stuff I suggested so you have to find another way: https://discourse.cmake.org/t/xcode-problem-with-files-which-has-vary-configuration/11754

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  B 4 Replies Last reply
                  0
                  • Christian EhrlicherC Christian Ehrlicher

                    You mix QtCreator and CMake here for unknown reasons. Also you still mix configure and build time stuff but I would guess this is by intention...
                    XCode sadly does not support the stuff I suggested so you have to find another way: https://discourse.cmake.org/t/xcode-problem-with-files-which-has-vary-configuration/11754

                    B Offline
                    B Offline
                    bogong
                    wrote last edited by bogong
                    #16

                    @Christian-Ehrlicher Mixing Qt Creator with CMake only because Qt Company in reality allow no other options when using Qt itself for iOS. Trying to use CMake under covering by Qt Creator within minimal efforts.

                    Thx for link on similar troubles discussed on CMake Official.

                    1 Reply Last reply
                    0
                    • Christian EhrlicherC Christian Ehrlicher

                      You mix QtCreator and CMake here for unknown reasons. Also you still mix configure and build time stuff but I would guess this is by intention...
                      XCode sadly does not support the stuff I suggested so you have to find another way: https://discourse.cmake.org/t/xcode-problem-with-files-which-has-vary-configuration/11754

                      B Offline
                      B Offline
                      bogong
                      wrote last edited by bogong
                      #17

                      @Christian-Ehrlicher For now solution is using environment variables like this:
                      Screenshot 2025-09-28 at 12.09.29.png

                      In CMakeLists.txt using something like this:

                      if(DEFINED ENV{A_DATA_MODIFIER_BUILD_TYPE})
                      	set(${A_PREFIX}_BUILD_TYPE $ENV{A_DATA_MODIFIER_BUILD_TYPE})
                      	message(STATUS "Using build type config: ${${A_PREFIX}_BUILD_TYPE}")
                      else()
                      	message(FATAL_ERROR "The system variable A_DATA_MODIFIER_BUILD_TYPE not set")
                      endif()
                      

                      The troubles in this case need to rescan project when switching Debug-Release. Is there any option to do it automatically when switching between configs? Or is there option to configure CMake variable in "Current Configuration" only for exact config in Qt Creator?

                      1 Reply Last reply
                      0
                      • Christian EhrlicherC Christian Ehrlicher

                        You mix QtCreator and CMake here for unknown reasons. Also you still mix configure and build time stuff but I would guess this is by intention...
                        XCode sadly does not support the stuff I suggested so you have to find another way: https://discourse.cmake.org/t/xcode-problem-with-files-which-has-vary-configuration/11754

                        B Offline
                        B Offline
                        bogong
                        wrote last edited by
                        #18

                        @Christian-Ehrlicher Beside all of it why CMAKE_BUILD_TYPE return in CMakeLists.txt empty value? CMakeLists.txt.user contain defined for Android:

                        Screenshot 2025-09-28 at 12.40.51.png

                        But has nothing similar for iOS:

                        Screenshot 2025-09-28 at 12.44.30.png

                        Is it bug or it on purpose?

                        1 Reply Last reply
                        0
                        • Christian EhrlicherC Christian Ehrlicher

                          You mix QtCreator and CMake here for unknown reasons. Also you still mix configure and build time stuff but I would guess this is by intention...
                          XCode sadly does not support the stuff I suggested so you have to find another way: https://discourse.cmake.org/t/xcode-problem-with-files-which-has-vary-configuration/11754

                          B Offline
                          B Offline
                          bogong
                          wrote last edited by bogong
                          #19

                          @Christian-Ehrlicher The solution for all of this troubles is in fixing CMakeLists.txt.user file generated automatically by Qt Creator. This file contain

                          -DCMAKE_BUILD_TYPE=Debug
                          

                          only for Android configurations. For MacOS and iOS it's not defined. When manually added for each of MacOS and iOS configuration all troubles disappeared. It's happening on Qt Creator 17.0.1.

                          Screenshot 2025-09-28 at 13.00.08.png

                          There are no need to use system or environment variables when fixed this file.

                          This file generated automatically by Qt Creator. By my opinion it's definitely next one bug that is blocking normal iOS/MacOS development.

                          Just created project from template. It's by default generated by Qt Creator. The same issue when using template. No defined CMAKE_BUILD_TYPE variable in CMake.Initial.Parameters for any iOS or MacOS platforms with any of configs.

                          1 Reply Last reply
                          0
                          • B bogong has marked this topic as solved

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved