Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support commands with executable targets #352

Merged
merged 1 commit into from
Aug 3, 2021
Merged

Conversation

sloretz
Copy link
Contributor

@sloretz sloretz commented Jul 29, 2021

This PR adds a new CMake function get_executable_path() and makes some ament_cmake functions that accept a command to execute use it.

The function checks if its argument is an executable target, and if so returns the appropriate property as a value or generator expression. This allows commands to be given executable targets in addition to paths.

Locally I tested this with

find_package(Python3 REQUIRED COMPONENTS Interpreter)
get_executable_path(py_imported_target_build Python3::Interpreter BUILD)
message(STATUS "py_imported_target_build ${py_imported_target_build}")
add_custom_target(genexdebug1 ALL COMMAND ${CMAKE_COMMAND} -E echo "py_imported_target_build: ${py_imported_target_build}")

set(PYTHON_EXECUTABLE "/usr/bin/python3")
get_executable_path(py_variable_build "${PYTHON_EXECUTABLE}" BUILD)
message(STATUS "py_variable_build ${py_variable_build}")
add_custom_target(genexdebug2 ALL COMMAND ${CMAKE_COMMAND} -E echo "py_variable_build: ${py_variable_build}")

get_executable_path(py_imported_target_config Python3::Interpreter CONFIGURE)
message(STATUS "py_imported_target_config ${py_imported_target_config}")

get_executable_path(py_variable_config "${PYTHON_EXECUTABLE}" CONFIGURE)
message(STATUS "py_variable_config ${py_variable_config}")

add_executable(foo foobar.cpp)
get_executable_path(py_exec_build "foo" BUILD)
message(STATUS "py_exec_build ${py_exec_build}")
add_custom_target(genexdebug3 ALL COMMAND ${CMAKE_COMMAND} -E echo "py_exec_build: ${py_exec_build}")

get_executable_path(py_exec_config "foo" CONFIGURE)
message(STATUS "py_exec_config ${py_exec_config}")

And the results were

$ cmake ..
-- py_imported_target_build $<TARGET_PROPERTY:Python3::Interpreter,LOCATION>
-- py_variable_build /usr/bin/python3
-- py_imported_target_config /home/osrf/projects/siws/env3/bin/python3
-- py_variable_config /usr/bin/python3
-- py_exec_build $<TARGET_FILE:foo>
CMake Warning at CMakeLists.txt:62 (message):
  There exists a non-imported executable target named 'foo', but those cannot
  be used at configure time.  Assuming it's a path instead.
Call Stack (most recent call first):
  CMakeLists.txt:96 (get_executable_path)


-- py_exec_config foo
-- Configuring done
-- Generating done
-- Build files have been written to: /home/osrf/tmp/cet/build
$ make
[100%] Built target foo
py_exec_build: /home/osrf/tmp/cet/build/foo
[100%] Built target genexdebug3
py_variable_build: /usr/bin/python3
[100%] Built target genexdebug2
py_imported_target_build: /home/osrf/projects/siws/env3/bin/python3
[100%] Built target genexdebug1

This is in support of ros2/python_cmake_module#6

@sloretz sloretz self-assigned this Jul 29, 2021
Adds get_executable_path()
This CMake function checks if its argument is an executable target, and
if so returns the appropriate property as a value or generator
expression
This allows commands to be given executable targets in addtion to paths.

Signed-off-by: Shane Loretz <sloretz@osrfoundation.org>
@sloretz
Copy link
Contributor Author

sloretz commented Jul 29, 2021

Full CI

  • Linux Build Status
  • Linux-aarch64 Build Status
  • macOS Build Status
  • Windows Build Status

Copy link
Contributor

@nuclearsandwich nuclearsandwich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the kind of thing where it feels like there ought to be a CMake built in for it but there's enough logic here that I wouldn't want it copied around everywhere it is needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants