[CMake] Generator expressions: Identifying when they're used in a custom command?

Sam Edwards cfsworks at gmail.com
Sun Feb 18 21:47:09 EST 2018


Alan,

Thanks for your help! I tried to implement that paradigm myself in a small
example CMakeLists.txt which I've attached, but the addition of a depending
custom target doesn't seem to change the context of the 'this' target. In
other words, the IS_INTERROGATE property is still read from fake_target and
not depending_target.

Could you take a look and clarify what I'm not doing quite right?

Best,
Sam

On Sun, Feb 18, 2018 at 3:09 PM, Alan W. Irwin <irwin at beluga.phys.uvic.ca>
wrote:

> On 2018-02-18 03:49-0700 Sam Edwards wrote:
>
> However, when it came time to actually set this IS_INTERROGATE property, I
>> could find no way to apply it to a custom command. I guess this makes
>> sense
>> - custom commands aren't targets, after all. But I couldn't convert my
>> custom command to a custom target, because it produces a C++ source file
>> and I need to use OUTPUT to let CMake know where that source file comes
>> from, and you can't use OUTPUT and TARGET together in add_custom_command.
>>
>
> Hi Sam:
>
> It's quite common for each custom command to have a corresponding
> custom target that DEPENDS on the OUTPUT of the custom command.  So
> building the target part of that pair means the custom command is
> executed *only if* the OUTPUT from it is out of date.
>
> If you used that paradigm for the custom commands you refer to
> above (where each such custom command is paired with a unique target
> via the DEPENDS of the latter) could you not set the IS_INTERROGATE
> property for the custom target part of of each pair whenever that
> property is relevant?
>
> Alan
> __________________________
> Alan W. Irwin
>
> Astronomical research affiliation with Department of Physics and Astronomy,
> University of Victoria (astrowww.phys.uvic.ca).
>
> Programming affiliations with the FreeEOS equation-of-state
> implementation for stellar interiors (freeeos.sf.net); the Time
> Ephemerides project (timeephem.sf.net); PLplot scientific plotting
> software package (plplot.sf.net); the libLASi project
> (unifont.org/lasi); the Loads of Linux Links project (loll.sf.net);
> and the Linux Brochure Project (lbproject.sf.net).
> __________________________
>
> Linux-powered Science
> __________________________
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20180218/95a94058/attachment.html>
-------------- next part --------------
cmake_minimum_required(VERSION 2.8.12)

# Create a fake target, with a property with a generator expression
add_custom_target(base_target)
set_target_properties(base_target PROPERTIES
  IS_INTERROGATE 0
  INTERFACE_INCLUDE_DIRECTORIES "IS_INTERROGATE=$<BOOL:$<TARGET_PROPERTY:IS_INTERROGATE>>")

add_custom_target(fake_target)
set_target_properties(fake_target PROPERTIES
  IS_INTERROGATE 0
  INTERFACE_INCLUDE_DIRECTORIES "$<TARGET_PROPERTY:base_target,INTERFACE_INCLUDE_DIRECTORIES>")

# This is the script that we run with the custom command which generates our
# C source code.
set(script_path "${CMAKE_CURRENT_BINARY_DIR}/make_printer_program.py")
FILE(WRITE "${script_path}" "
import sys

arg = sys.argv[1]

print('#include <stdio.h>')
print('int main() { puts(\"%s\"); return 0; }' % arg)
")

# Run the generation script
find_package(PythonInterp QUIET REQUIRED)
add_custom_command(OUTPUT source.c
  COMMAND "${PYTHON_EXECUTABLE}" "${script_path}" "$<TARGET_PROPERTY:fake_target,INTERFACE_INCLUDE_DIRECTORIES>" > source.c)

# Add a custom target to depend on source.c, as suggested
add_custom_target(depending_target)
set_target_properties(depending_target PROPERTIES
  IS_INTERROGATE 1)

# Compile the output
add_executable(output source.c)
# Make sure we depend on the custom target
add_dependencies(output depending_target)


More information about the CMake mailing list