How to support separate debug and release build directories?

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

How to support separate debug and release build directories?

David Aldrich-2
Thanks for the help I have received in the past few days. I am making incremental improvements to my CMake project and have a new challenge.  I am running CMake 3.13 on Centos 7.6, targeting make.  My CMake file successfully builds debug or release targets and puts the executable in an out-of-source build directory.  I have added debug and release make targets so I can execute 'make debug' etc.

I now want to support separate target directories: build/debug and build/release.  I've shown my CMakeLists.txt below. So far I've just added an attempt to support build/debug:

if (CMAKE_BUILD_TYPE EQUAL "DEBUG")
    message("debug mode")
    set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/debug)
endif (CMAKE_BUILD_TYPE EQUAL "DEBUG")

but:

$ cmake3 -DCMAKE_BUILD_TYPE=DEBUG ..

puts the target into build, not build/debug.

I would also like this to work if I use the make targets e.g. make debug.

Here's my full CMakeLists.txt.  Any advice would be appreciated.

cmake_minimum_required(VERSION 3.5 FATAL_ERROR)

if(NOT CMAKE_BUILD_TYPE)
  set(CMAKE_BUILD_TYPE "Release")
  message(STATUS "Build type not specified: Use Release by default")
endif(NOT CMAKE_BUILD_TYPE)

project(hello_world LANGUAGES CXX)    # Among other things, this sets PROJECT_NAME

add_executable(${PROJECT_NAME} "")

target_include_directories(${PROJECT_NAME}
    PRIVATE
        ${CMAKE_CURRENT_SOURCE_DIR})

target_sources(${PROJECT_NAME}
  PRIVATE
    main.cpp
    Message.cpp)

ADD_CUSTOM_TARGET(debug
  COMMAND ${CMAKE_COMMAND} -DCMAKE_BUILD_TYPE=Debug ${CMAKE_SOURCE_DIR}
  COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target all
  COMMENT "Creating the executable in the debug mode.")

ADD_CUSTOM_TARGET(release
  COMMAND ${CMAKE_COMMAND} -DCMAKE_BUILD_TYPE=Release ${CMAKE_SOURCE_DIR}
  COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target all
  COMMENT "Creating the executable in the release mode.")

if (CMAKE_BUILD_TYPE EQUAL "DEBUG")
    message("debug mode")
    set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/debug)
endif (CMAKE_BUILD_TYPE EQUAL "DEBUG")


--

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 support separate debug and release build directories?

David Demelier-2
Le 21/06/2019 à 15:19, David Aldrich a écrit :
> I now want to support separate target directories: build/debug and
> build/release.  I've shown my CMakeLists.txt below. So far I've just
> added an attempt to support build/debug:
>
> if (CMAKE_BUILD_TYPE EQUAL "DEBUG")
>      message("debug mode")
>      set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/debug)
> endif (CMAKE_BUILD_TYPE EQUAL "DEBUG")
>

Do never test CMAKE_BUILD_TYPE in CMakeLists.txt files, it is ignored in
multiple generators (e.g. Visual Studio).

Just use the appropriate variables that contain suffixes regarding the
configuration.

e.g

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}/debug)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR}/release)

See [0] for a list with _<CONFIG> variables.

[0]: https://cmake.org/cmake/help/v3.15/manual/cmake-variables.7.html

HTH

--
David
--

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 support separate debug and release build directories?

braden
In reply to this post by David Aldrich-2
On Fri, 2019-06-21 at 14:19 +0100, David Aldrich wrote:

> Thanks for the help I have received in the past few days. I am making
> incremental improvements to my CMake project and have a new
> challenge.  I am running CMake 3.13 on Centos 7.6, targeting make.
> My CMake file successfully builds debug or release targets and puts
> the executable in an out-of-source build directory.  I have added
> debug and release make targets so I can execute 'make debug' etc.
>
> I now want to support separate target directories: build/debug and
> build/release.  I've shown my CMakeLists.txt below. So far I've just
> added an attempt to support build/debug:
>
> if (CMAKE_BUILD_TYPE EQUAL "DEBUG")
>     message("debug mode")
>     set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/debug)
> endif (CMAKE_BUILD_TYPE EQUAL "DEBUG")
>
> but:
>
> $ cmake3 -DCMAKE_BUILD_TYPE=DEBUG ..
>
> puts the target into build, not build/debug.

The conventional solution to this is to run cmake in the "build/debug"
directory.

> I would also like this to work if I use the make targets e.g. make
> debug.

I think that's outside the scope of the Makefile generator.  For that
generator, CMAKE_BUILD_TYPE is a configuration-wide setting.  If you
want a different configuration, you need a different build directory
(where "build directory" is wherever you run cmake).

--
Braden McDaniel <[hidden email]>

--

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 support separate debug and release build directories?

David Aldrich-2
In reply to this post by David Demelier-2
Do never test CMAKE_BUILD_TYPE in CMakeLists.txt files, it is ignored in
multiple generators (e.g. Visual Studio).
 
Does that mean I shouldn't have this in CMakeLists.txt? :

# Specify a Release build by default
if(NOT CMAKE_BUILD_TYPE)
  set(CMAKE_BUILD_TYPE "Release")
  message(STATUS "Build type not specified: Use Release by default")
endif(NOT CMAKE_BUILD_TYPE)
 

Just use the appropriate variables that contain suffixes regarding the
configuration.

e.g

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}/debug)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR}/release)

See [0] for a list with _<CONFIG> variables.

[0]: https://cmake.org/cmake/help/v3.15/manual/cmake-variables.7.html

HTH
 
Thank you. That is working for me. 

--

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 support separate debug and release build directories?

David Demelier-2
Le 21/06/2019 à 15:42, David Aldrich a écrit :

>     Do never test CMAKE_BUILD_TYPE in CMakeLists.txt files, it is
>     ignored in
>     multiple generators (e.g. Visual Studio).
>
> Does that mean I shouldn't have this in CMakeLists.txt? :
>
> # Specify a Release build by default
> if(NOT CMAKE_BUILD_TYPE)
>    set(CMAKE_BUILD_TYPE "Release")
>    message(STATUS "Build type not specified: Use Release by default")
> endif(NOT CMAKE_BUILD_TYPE)

It may be handy if you're sure that the project will only use single
generators tools (e.g. make, ninja) otherwise yes it's strongly advised
to not touch/inspect CMAKE_BUILD_TYPE if your project can be build with
any kind of generators.

Regards

--
David

--

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 support separate debug and release build directories?

David Aldrich-2
In reply to this post by braden
> I would also like this to work if I use the make targets e.g. make
> debug.

I think that's outside the scope of the Makefile generator.  For that
generator, CMAKE_BUILD_TYPE is a configuration-wide setting.  If you
want a different configuration, you need a different build directory
(where "build directory" is wherever you run cmake).

If I don't use make targets (so that user can type 'make debug' etc) the build command would be more cumbersome:

cmake3 --build -D CMAKE_BUILD_TYPE=Debug .

What would best practice be to provide convenient commands for our developers to easily build the target ?

--

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 support separate debug and release build directories?

braden
On Fri, 2019-06-21 at 15:28 +0100, David Aldrich wrote:

> > > I would also like this to work if I use the make targets e.g.
> > make
> > > debug.
> >
> > I think that's outside the scope of the Makefile generator.  For
> > that
> > generator, CMAKE_BUILD_TYPE is a configuration-wide setting.  If
> > you
> > want a different configuration, you need a different build
> > directory
> > (where "build directory" is wherever you run cmake).
>
> If I don't use make targets (so that user can type 'make debug' etc)
> the build command would be more cumbersome:
>
> cmake3 --build -D CMAKE_BUILD_TYPE=Debug .

Assuming that command actually triggers a reconfigure (when previously
configured with a different CMAKE_BUILD_TYPE), you'll most likely hose
incremental builds of the other build type(s).  (And if it doesn't
trigger a reconfigure in that case, it's pretty certainly not doing
what you want.)

> What would best practice be to provide convenient commands for our
> developers to easily build the target ?

For the Makefile generator, best practice is to use separate build
directories (i.e., places where you run cmake) for different
configurations (i.e., different settings recorded during the
configuration step).

If you want to provide developers with some known set(s) of
configuration settings, I suggest wrapper scripts that invoke cmake
with those settings.

--
Braden McDaniel <[hidden email]>

--

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 support separate debug and release build directories?

David Aldrich-2
> What would best practice be to provide convenient commands for our
> developers to easily build the target ?

For the Makefile generator, best practice is to use separate build
directories (i.e., places where you run cmake) for different
configurations (i.e., different settings recorded during the
configuration step).

If you want to provide developers with some known set(s) of
configuration settings, I suggest wrapper scripts that invoke cmake
with those settings.

Thanks for your advice. I am not finding it easy to find 'patterns' for these sort of issues. I would have thought that configuring a project with separate debug and release directories would be quite typical. But it's hard to find the recommended way of doing such things. Anyway, I think I am on the right track now.

--

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 support separate debug and release build directories?

Michael Jackson

David,

    I think a bit more explanation of the philosophy (at least how I interpret it) is needed. I see in your emails that you are “targeting makefiles”. With CMake you need to really stop thinking this way. Rarely do you need to target any specific build system (although those times do come up…). A lot of folks I see coming from autoconf to CMake still try to treat CMake in the same way. Don’t. There are a few golden rules with CMake that if you adhere to those will allow you to use CMake much easier.

 

1: NEVER have the Source directory and the Build directory be the same place.

2: PREFER out-of-source build directories (Not required but helpful)

3: Try not to target specific generators (makefiles, visual studio, xcode)

 

What this looks like is the following. Say I have a project from github called Foo and I clone it onto my local filesystem. From a command line perspective you get the following:

 

Git clone https://github.com/somebody/Foo

 

The *Preferred* way is this

Clone Foo

*At the same directory level as Foo*

Mkdir Debug && cd Debug

Cmake -DCMAKE_BUILD_TYPE=Release ../Foo

 

 

# We can break rule 2 a bit by making subdirectories inside of Foo

  cd Foo

   mkdir Debug && cd Debug

   cmake -DCMAKE_BUILD_TYPE=Debug ../

   make -j

 

We have just created a Debug build in the Debug Directory. Now, the same is analogous for the Release build.

 

Cd Foo

Mkdir Release && cd Release

Cmake -DCMAKE_BUILD_TYPE=Release ../

Make -j

 

*IF* you make these subdirectories in a source code top level be sure to add those directory names to the .gitignore file.

 

If your cmake is written properly then the build creates NOTHING inside of the source directory.

 

I hope this helps summarize all of the other emails. Glad to have you in the CMake community.

 

--

Mike Jackson

 

From: CMake <[hidden email]> on behalf of David Aldrich <[hidden email]>
Date: Friday, June 21, 2019 at 12:10 PM
To: Braden McDaniel <[hidden email]>
Cc: CMake <[hidden email]>
Subject: Re: [CMake] How to support separate debug and release build directories?

 

> What would best practice be to provide convenient commands for our
> developers to easily build the target ?

For the Makefile generator, best practice is to use separate build
directories (i.e., places where you run cmake) for different
configurations (i.e., different settings recorded during the
configuration step).

If you want to provide developers with some known set(s) of
configuration settings, I suggest wrapper scripts that invoke cmake
with those settings.

Thanks for your advice. I am not finding it easy to find 'patterns' for these sort of issues. I would have thought that configuring a project with separate debug and release directories would be quite typical. But it's hard to find the recommended way of doing such things. Anyway, I think I am on the right track now.

-- 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


--

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 support separate debug and release build directories?

David Aldrich-2

David,

    I think a bit more explanation of the philosophy (at least how I interpret it) is needed. I see in your emails that you are “targeting makefiles”. With CMake you need to really stop thinking this way. Rarely do you need to target any specific build system (although those times do come up…). A lot of folks I see coming from autoconf to CMake still try to treat CMake in the same way. Don’t. There are a few golden rules with CMake that if you adhere to those will allow you to use CMake much easier.

 

1: NEVER have the Source directory and the Build directory be the same place.

2: PREFER out-of-source build directories (Not required but helpful)

3: Try not to target specific generators (makefiles, visual studio, xcode)

<snip>


Hi Michael 

Thanks very much for your reply and explanation. As a result, I now have the separate Debug/Release build subdirectories working correctly.

Best regards

David

--

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 support separate debug and release build directories?

Marc Herbert
In reply to this post by Michael Jackson
Le ven. 21 juin 2019 à 11:40, Michael Jackson <[hidden email]> a écrit :


  cd Foo

   mkdir Debug && cd Debug

   cmake -DCMAKE_BUILD_TYPE=Debug ../

   make -j

 

We have just created a Debug build in the Debug Directory. Now, the same is analogous for the Release build.

 

Cd Foo

Mkdir Release && cd Release

Cmake -DCMAKE_BUILD_TYPE=Release ../

Make -j

 


Or, from CMake 3.13, simpler:

cd foo
cmake -B release/ -DCMAKE_BUILD_TYPE=Release 
cmake -B debug/ -DCMAKE_BUILD_TYPE=Debug 
make -j -C build & make -j -C debug

 

 I am not finding it easy to find 'patterns' for these sort of issues. I would have thought that configuring a project with separate debug and release directories would be quite typical. But it's hard to find the recommended way of doing such things. 



In this case CMake doesn't recommend anything because it assumes this is implemented outside/on top of CMake

Marc

--

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 support separate debug and release build directories?

Marc Herbert


 I am not finding it easy to find 'patterns' for these sort of issues. I would have thought that configuring a project with separate debug and release directories would be quite typical. But it's hard to find the recommended way of doing such things. 



In this case CMake doesn't recommend anything because it assumes this is implemented outside/on top of CMake


Finally found it while looking for something else: 

--

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