Issue
Suppose I have an existing project and it's cmake-configured build directory. How can I retreive some target's properties using this build, provided that I know the target name? I tried creating a separate script like this
get_target_property(VAR target property)
but it fails with error
Command get_target_property() is not scriptable
Are there any other methods?
Solution
Apparently get_target_property() can be called only when configuring a build directory with cmake. I am not aware of any method of getting targets properties on already configured build directory. But if modifying existing CMakeFiles.txt is an option, there is a workaround.
You can try locating target definition, get the target's properties there and dump them into a text file. Then, this file can be then used in any other scripts called after build directory configuration is done.
This example illustrates this workaround:
add_executable(app ${app_sources})
set_target_properties(app PROPERTIES COMPILE_DEFINITIONS SOME_DEF=1)
get_target_property(compile_defs app COMPILE_DEFINITIONS)
file(WRITE app_compile_defs.txt ${compile_defs})
Be sure to use get_target_property
after every property changes for given target in CMakeFiles.txt are done. Otherwise you can miss something as in example below.
add_executable(app ${app_sources})
set_target_properties(app PROPERTIES COMPILE_DEFINITIONS SOME_DEF=1)
get_target_property(compile_defs app COMPILE_DEFINITIONS)
file(WRITE app_compile_defs.txt ${compile_defs})
set_target_properties(app PROPERTIES COMPILE_DEFINITIONS ANOTHER_DEF=0)
In the example above, the ANOTHER_DEF=0
definition will not be listed in app_compile_defs.txt
.
Answered By - jacek.chmiel Answer Checked By - Marilyn (WPSolving Volunteer)