Re: [CMake] How to build 2 targets from the same source, differing in -D_SWITCHES_ only
Am Freitag 04 September 2009 21:51:59 schrieb Alexander Neundorf: On Thursday 27 August 2009, Christian Ehrlicher wrote: Von: Marcel Loose lo...@astron.nl CC: cmake@cmake.org Betreff: Re: [CMake] How to build 2 targets from the same source, differing in -D_SWITCHES_ only Hi Eike, I think the only safe and reliable way to do this is create several build directories, e.g. build/type_1 and build/type_2. When running cmake in build/type_1, you should add -D_TYPE1_ to the preprocessor flags; same for build/type_2. Why? Does SET_TARGET_PROPERTIES() with COMPILE_FLAGS not work? So the full code is: add_executable(foo main.c) set_target_properties(foo PROPERTIES COMPILE_FLAGS -D_TYPE1_) add_executable(bar main.c) set_target_properties(bar PROPERTIES COMPILE_FLAGS -D_TYPE2_) Actually better: set (foo_DEFS _TYPE1_) set (bar_DEFS _TYPE2_) foreach(i foo bar) add_executable(${i} main.c) set_target_properties(foo PROPERTIES COMPILE_DEFINITIONS ${${i}_DEFS}) foreach(flag RELEASE DEBUG) set_target_properties(foo PROPERTIES COMPILE_DEFINITIONS_{flag} ${${i}_DEFS}) endforeach(flag) endforeach(i) ;-) HS ___ Powered by www.kitware.com Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ Follow this link to subscribe/unsubscribe: http://www.cmake.org/mailman/listinfo/cmake
Re: [CMake] How to build 2 targets from the same source, differing in -D_SWITCHES_ only
On Thursday 27 August 2009, Christian Ehrlicher wrote: Von: Marcel Loose lo...@astron.nl CC: cmake@cmake.org Betreff: Re: [CMake] How to build 2 targets from the same source, differing in -D_SWITCHES_ only Hi Eike, I think the only safe and reliable way to do this is create several build directories, e.g. build/type_1 and build/type_2. When running cmake in build/type_1, you should add -D_TYPE1_ to the preprocessor flags; same for build/type_2. Why? Does SET_TARGET_PROPERTIES() with COMPILE_FLAGS not work? So the full code is: add_executable(foo main.c) set_target_properties(foo PROPERTIES COMPILE_FLAGS -D_TYPE1_) add_executable(bar main.c) set_target_properties(bar PROPERTIES COMPILE_FLAGS -D_TYPE2_) Alex ___ Powered by www.kitware.com Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ Follow this link to subscribe/unsubscribe: http://www.cmake.org/mailman/listinfo/cmake
Re: [CMake] How to build 2 targets from the same source, differing in -D_SWITCHES_ only
Hi Marcel, [...] See tiny example below (hope this helps). Yes, it does, thank you very much for the detailed walkthrough. Based on your suggestion I've experimented a bit and found that my key issues were: - introducing a separated build-directory structure - should have done that long before as my makefiles needed a 'make partialclean' and a flag-file .LAST_TARGET for switching between targets, ähh, historically grown - giving up on a single main Makefile/CMakeLists.txt for all targets, handling those as separate projects makes more sense (perhaps I'll keep my main Makefile just to call configure for all CMake-projects). - stop calling cmake from a directory that is _common_ to all builds; changing into a build directory and CMaking from there works fine. Thanks again, Eike ___ Powered by www.kitware.com Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ Follow this link to subscribe/unsubscribe: http://www.cmake.org/mailman/listinfo/cmake
[CMake] How to build 2 targets from the same source, differing in -D_SWITCHES_ only
Hi there, in the hope of achieving platform independence of my plain-C project I have started looking into CMake, trying to duplicate what my manually created makefiles do. A minimalistic description of my problem is the following: * I have 2 executable targets * both having the same main program source MAIN/main.c * both linking to a library libvehicle.a that is built from the same set of sources, e.g. VEHICLE/read_hydrodynamic_coeffs.c The difference between both targets is a compiler definition -D_TYPE1_ or -D_TYPE2_ that is used in the sourcecode, e.g. hydro_par_t read_hydrodynamic_coeffs(sys_par_t sys_par) { #if defined(_TYPE1_) return(read_hydrodynamic_coeffs_type1(sys_par)); #elif defined(_TYPE2_) return(read_hydrodynamic_coeffs_type2(sys_par)); #endif } So the in both cases the library libvehicle.a will contain a function read_hydrodynamic_coeffs but the actual content of this function will be different. [ In realiter it's more complicated because there are more executables and combinations of several switches, so I really don't want to change the sources. ] What I've done so far is * in the main CMakeLists.txt: add_subdirectory(CMakeTargets/sim_type1) * in CMakeTargets/sim_type1/CMakeLists.txt: add_definitions(-D_TYPE1_) include_directories(../../VEHICLE/INCLUDE) add_subdirectory(../../VEHICLE VEHICLE) add_executable(sim_type1 ../../MAIN/main) target_link_libraries(sim_type1 vehicle) * in CMakeTargets/VEHICLE/CMakeLists.txt: set(SRC read_hydrodynamic_coeffs) set(SRC ${SRC} read_hydrodynamic_coeffs_type1) set(SRC ${SRC} read_hydrodynamic_coeffs_type2) add_library(vehicle ${SRC}) For one target only this gives a executable CMakeTargets/sim_type1/sim_type1 that is working as expected. As specified by add_subdirectory(../../VEHICLE VEHICLE), object file and library are created locally: ./CMakeTargets/sim_type1/VEHICLE/CMakeFiles/vehicle.dir/read_hydrodynamic_coeffs.c.o ./CMakeTargets/sim_type1/VEHICLE/libvehicle.a But as soon as I try to add_subdirectory(CMakeTargets/sim_type2) [rest as above, only 1-2] I get an error message add_library cannot create target vehicle because another target with the same name already exists. The existing target is a static library created in source directory ./VEHICLE. See documentation for policy CMP0002 for more details. Trying to cheat with cmake_policy(SET CMP0002 OLD) I can compile both executables but they are messed up, obviously trying to execute read_hydrodynamic_coeffs for the wrong type. So, how do I define a set of executable make targets, differing only in the -D_defines_ that are used to build all of the source needed? Thanks in advance, Eike ___ Powered by www.kitware.com Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ Follow this link to subscribe/unsubscribe: http://www.cmake.org/mailman/listinfo/cmake
Re: [CMake] How to build 2 targets from the same source, differing in -D_SWITCHES_ only
Hi Eike, I think the only safe and reliable way to do this is create several build directories, e.g. build/type_1 and build/type_2. When running cmake in build/type_1, you should add -D_TYPE1_ to the preprocessor flags; same for build/type_2. Best regards, Marcel Loose. On Thu, 2009-08-27 at 12:28 +0200, Eike Kroemer wrote: Hi there, in the hope of achieving platform independence of my plain-C project I have started looking into CMake, trying to duplicate what my manually created makefiles do. A minimalistic description of my problem is the following: * I have 2 executable targets * both having the same main program source MAIN/main.c * both linking to a library libvehicle.a that is built from the same set of sources, e.g. VEHICLE/read_hydrodynamic_coeffs.c The difference between both targets is a compiler definition -D_TYPE1_ or -D_TYPE2_ that is used in the sourcecode, e.g. hydro_par_t read_hydrodynamic_coeffs(sys_par_t sys_par) { #if defined(_TYPE1_) return(read_hydrodynamic_coeffs_type1(sys_par)); #elif defined(_TYPE2_) return(read_hydrodynamic_coeffs_type2(sys_par)); #endif } So the in both cases the library libvehicle.a will contain a function read_hydrodynamic_coeffs but the actual content of this function will be different. [ In realiter it's more complicated because there are more executables and combinations of several switches, so I really don't want to change the sources. ] What I've done so far is * in the main CMakeLists.txt: add_subdirectory(CMakeTargets/sim_type1) * in CMakeTargets/sim_type1/CMakeLists.txt: add_definitions(-D_TYPE1_) include_directories(../../VEHICLE/INCLUDE) add_subdirectory(../../VEHICLE VEHICLE) add_executable(sim_type1 ../../MAIN/main) target_link_libraries(sim_type1 vehicle) * in CMakeTargets/VEHICLE/CMakeLists.txt: set(SRC read_hydrodynamic_coeffs) set(SRC ${SRC} read_hydrodynamic_coeffs_type1) set(SRC ${SRC} read_hydrodynamic_coeffs_type2) add_library(vehicle ${SRC}) For one target only this gives a executable CMakeTargets/sim_type1/sim_type1 that is working as expected. As specified by add_subdirectory(../../VEHICLE VEHICLE), object file and library are created locally: ./CMakeTargets/sim_type1/VEHICLE/CMakeFiles/vehicle.dir/read_hydrodynamic_coeffs.c.o ./CMakeTargets/sim_type1/VEHICLE/libvehicle.a But as soon as I try to add_subdirectory(CMakeTargets/sim_type2) [rest as above, only 1-2] I get an error message add_library cannot create target vehicle because another target with the same name already exists. The existing target is a static library created in source directory ./VEHICLE. See documentation for policy CMP0002 for more details. Trying to cheat with cmake_policy(SET CMP0002 OLD) I can compile both executables but they are messed up, obviously trying to execute read_hydrodynamic_coeffs for the wrong type. So, how do I define a set of executable make targets, differing only in the -D_defines_ that are used to build all of the source needed? Thanks in advance, Eike ___ Powered by www.kitware.com Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ Follow this link to subscribe/unsubscribe: http://www.cmake.org/mailman/listinfo/cmake ___ Powered by www.kitware.com Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ Follow this link to subscribe/unsubscribe: http://www.cmake.org/mailman/listinfo/cmake
Re: [CMake] How to build 2 targets from the same source, differing in -D_SWITCHES_ only
Von: Marcel Loose lo...@astron.nl CC: cmake@cmake.org Betreff: Re: [CMake] How to build 2 targets from the same source, differing in -D_SWITCHES_ only Hi Eike, I think the only safe and reliable way to do this is create several build directories, e.g. build/type_1 and build/type_2. When running cmake in build/type_1, you should add -D_TYPE1_ to the preprocessor flags; same for build/type_2. Why? Does SET_TARGET_PROPERTIES() with COMPILE_FLAGS not work? Christian -- Neu: GMX Doppel-FLAT mit Internet-Flatrate + Telefon-Flatrate für nur 19,99 Euro/mtl.!* http://portal.gmx.net/de/go/dsl02 ___ Powered by www.kitware.com Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ Follow this link to subscribe/unsubscribe: http://www.cmake.org/mailman/listinfo/cmake
Re: [CMake] How to build 2 targets from the same source, differing in -D_SWITCHES_ only
Hi Marcel, Christian, Am 08/27/2009 01:30 PM Marcel Loose wrote: I think the only safe and reliable way to do this is create several build directories, e.g. build/type_1 and build/type_2. When running cmake in build/type_1, you should add -D_TYPE1_ to the preprocessor flags; same for build/type_2. By 'build directories' you mean to duplicate the library sourcecodes? Using the approach sketched in the original post, I have tried to create different directories for holding objects and libs - so far without success. - Am 08/27/2009 01:37 PM Christian Ehrlicher wrote: Why? Does SET_TARGET_PROPERTIES() with COMPILE_FLAGS not work? I tried something with SET_TARGET_PROPERTIES but when used on the target sim_type1 the define-flag won't be inherited by the library the target depends on, thus I used add_definitions instead I think the problem comes with using add_subdirectory(../../VEHICLE VEHICLE) and the subsequent add_library(vehicle ${SRC}) While this sets up a binary directory local to the make-target, it also creates CMake*-files 'globally', for all targets. A brute-force-approach would be to remove those sources from the ${SRC} in the add_library commands that need the -D_TYPE*_ defines and add them to the source-list of add_executable(sim_type1 ...). This would be awkward, would it work? Thanks again, Eike ___ Powered by www.kitware.com Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ Follow this link to subscribe/unsubscribe: http://www.cmake.org/mailman/listinfo/cmake
Re: [CMake] How to build 2 targets from the same source, differing in -D_SWITCHES_ only
On Thu, Aug 27, 2009 at 8:55 AM, Eike Kroemereike-michael.kroe...@atlas-elektronik.com wrote: Hi Marcel, Christian, Am 08/27/2009 01:30 PM Marcel Loose wrote: I think the only safe and reliable way to do this is create several build directories, e.g. build/type_1 and build/type_2. When running cmake in build/type_1, you should add -D_TYPE1_ to the preprocessor flags; same for build/type_2. By 'build directories' you mean to duplicate the library sourcecodes? Not the source just the output. You put your source in a single folder and then create 1 build tree for each different type of build you want. On windows I use this to build my projects with different compilers and on top of that I use this to create separate 32 and 64 bit builds. John ___ Powered by www.kitware.com Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ Follow this link to subscribe/unsubscribe: http://www.cmake.org/mailman/listinfo/cmake