[CMake] Setting a cmake option via make targets?

Michael Hertling mhertling at online.de
Wed Nov 2 18:47:40 EDT 2011


On 11/02/2011 07:32 PM, Yngve Inntjore Levinsen wrote:
> Onsdag 02 november 2011 skrev szalai endre:
>> Hi guys,
>>
>> I am using Rational Purify for runtime error checks in C++. To use it, you
>> have to call purify instead of the C++ linker at link phase. To do so, I
>> have the following cmake file:
>>
>> //------------
>> project(myproject )
>> option (USE_PURIFY "Use Rational Purify for runtime error checks" OFF)
>> IF (USE_PURIFY)
>>     set(CMAKE_CXX_LINK_EXECUTABLE "purify ${CMAKE_CXX_LINK_EXECUTABLE}")
>> ENDIF (USE_PURIFY)
>> add_executable (myproject HelloWorld.cc)
>> //------------
>>
>> This adds the option and changes the linker (prepending with "purify"). This
>> works fine.
>>
>> Now I would like to trigger the IF() line from a make target. Say, when I
>> type "make myproject_purify", I would like to set the option (or prepend
>> the linker, whichever is best).
>>
>> I am quite uncertain how to do it with cmake. I did not find any hint if I
>> can set options from cmake itself or not. Also, whenever I define a new
>> make target, I have to supply a shell command and it seems I cannot execute
>> cmake commands there.
>>
>> So my question: how would you set an option via a make target? Any hints?
>>
>> Thanks,
>> Mc
>> --
>>
>> 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
> 
> Hi,
> 
> Just an idea, would it be possible to just add a second target and use 
> set_target_properties() somehow? I'm guessing it would be something like:
> 
> set(sources HelloWorld.cc)
> add_executable (myproject ${sources})
> add_executable (myproject_purify ${sources} EXCLUDE_FROM_ALL)
> set_target_properties(myproject_purify CMAKE_CXX_LINK_EXECUTABLE "purify 
> ${CMAKE_CXX_LINK_EXECUTABLE}")
> 
> I have not tested this, so I might be wrong.
> 
> Cheers,
> Yngve

CMAKE_CXX_LINK_EXECUTABLE isn't a target property, so you cannot do it
in this way. However, you might use that approach in conjunction with
purify as the targets' RULE_LAUNCH_LINK property and custom targets:

CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
PROJECT(PURIFY CXX)
SET(CMAKE_VERBOSE_MAKEFILE ON)
FILE(WRITE ${CMAKE_BINARY_DIR}/main.cxx "int main(void){return 0;}\n")
ADD_EXECUTABLE(main main.cxx)
OPTION(PURIFY "Purify" OFF)
IF(PURIFY)
    SET_TARGET_PROPERTIES(main PROPERTIES RULE_LAUNCH_LINK purify)
ENDIF()
ADD_CUSTOM_TARGET(purify
    COMMAND ${CMAKE_COMMAND} ${CMAKE_BINARY_DIR} -DPURIFY=ON)
ADD_CUSTOM_TARGET(unpurify
    COMMAND ${CMAKE_COMMAND} ${CMAKE_BINARY_DIR} -DPURIFY=OFF)

The custom target "purify" reconfigures the project with -DPURIFY=ON
whereas "unpurify" does the reverse. So, after "make purify", Make
rebuilds the project with purify, and after "make unpurify", the
project is rebuilt as usual.

BTW, manipulating the CMAKE_CXX_LINK_EXECUTABLE rule variable in this
manner works, too, but the RULE_LAUNCH_* properties are a bit more to
my personal liking. Anyway, both do work for Makefile generators only.

'hope that helps.

Regards,

Michael


More information about the CMake mailing list