Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unset a CMake property?

Tags:

cmake

Summary

I would like to make a (boolean) property unset in CMake such that get_property(... SET) returns 0 although the property was set in the first place. Settings the property to 0, FALSE, or OFF is not sufficient. How to achieve that?

Background

I have some custom commands which generate some source files. CMake automatically assigns the GENERATED source file property to those. This seems to make the GNU Makefile generator to add a rule to remove these files with make clean.

However, I do not want those files to be cleaned up, because once they exist the user may edit them. The custom command should only make sure they become generated if they do not exist. Generating them at configure-time with execute_process instead of the custom command is not feasible.

Setting GENERATED to 0, FALSE, or OFF did not exclude the sources from clean.

Versions

  • OS: Ubuntu 16.04
  • CMake: 3.5.1 (from Ubuntu 16.04 repository)

I'm currently bound to Ubuntu 16.04 and getting all users to use a more recent CMake version should be avoided if possible.

like image 980
bjhend Avatar asked Oct 19 '25 22:10

bjhend


1 Answers

You need to set it to nothing:

# test unsetting a property
set_property(TARGET Properties PROPERTY TARGETTEST)
get_property(TARGETRESULT TARGET Properties PROPERTY TARGETTEST SET)
if (TARGETRESULT)
    message(SEND_ERROR "Error: target prop not unset, "
            "result is TARGETRESULT=${TARGETRESULT}")
endif ()

(From https://github.com/Kitware/CMake/blob/master/Tests/Properties/CMakeLists.txt)

like image 79
MSalters Avatar answered Oct 25 '25 16:10

MSalters



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!