Skip to content

Commit 58d4e94

Browse files
committed
Fix CMake --find-package mode
There is a CMake mode which can be used to extract basic compile and linker flags as would be produced by a call to find_package(). Existing ament_cmake code always assumes that we're being invoked under a "PROJECT" role. This change skips certain project-specific parts of ament_cmake_core and ament_cmake_test which shouldn't be run unless we're actually configuring a CMakeLists.txt project. The smoke tests added by this change validate that the "extra" CMake functionality provided by these packages continues to be loadable outside of a project context. It does not validate that the defined functions can actually be used, but will serve as a validation that features like CMake --find-package should work. Signed-off-by: Scott K Logan <[email protected]>
1 parent 17d4da4 commit 58d4e94

File tree

11 files changed

+89
-57
lines changed

11 files changed

+89
-57
lines changed

ament_cmake_core/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ ament_package(
2929
"ament_cmake_symlink_install-extras.cmake" # must be after uninstall_target
3030
)
3131

32+
include(CTest)
33+
if(BUILD_TESTING)
34+
add_subdirectory(test)
35+
endif()
36+
3237
install(
3338
DIRECTORY cmake
3439
DESTINATION share/${PROJECT_NAME}

ament_cmake_core/ament_cmake_core-extras.cmake

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616

1717
include("${ament_cmake_core_DIR}/core/all.cmake" NO_POLICY_SCOPE)
1818

19-
# Add AMENT_IGNORE to CMAKE_BINARY_DIR to avoid picking up cmake specific folders created by
20-
# CLion in `colcon build` and `colcon test` commands
21-
file(WRITE ${CMAKE_BINARY_DIR}/AMENT_IGNORE "")
22-
19+
get_property(_cmake_role GLOBAL PROPERTY CMAKE_ROLE)
20+
if("${_cmake_role}" STREQUAL "PROJECT")
21+
# Add AMENT_IGNORE to CMAKE_BINARY_DIR to avoid picking up cmake specific folders created by
22+
# CLion in `colcon build` and `colcon test` commands
23+
file(WRITE ${CMAKE_BINARY_DIR}/AMENT_IGNORE "")
24+
endif()

ament_cmake_core/ament_cmake_package_templates-extras.cmake

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,39 +12,42 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
# extract information from ament_package.templates
16-
if(NOT TARGET Python3::Interpreter)
17-
message(FATAL_ERROR
18-
"ament_cmake_package_templates: target 'Python3::Interpreter' must exist")
19-
endif()
15+
get_property(_cmake_role GLOBAL PROPERTY CMAKE_ROLE)
16+
if("${_cmake_role}" STREQUAL "PROJECT")
17+
# extract information from ament_package.templates
18+
if(NOT TARGET Python3::Interpreter)
19+
message(FATAL_ERROR
20+
"ament_cmake_package_templates: target 'Python3::Interpreter' must exist")
21+
endif()
2022

21-
# stamp script to generate CMake code
22-
set(_generator
23-
"${ament_cmake_core_DIR}/package_templates/templates_2_cmake.py")
24-
stamp("${_generator}")
23+
# stamp script to generate CMake code
24+
set(_generator
25+
"${ament_cmake_core_DIR}/package_templates/templates_2_cmake.py")
26+
stamp("${_generator}")
2527

26-
# invoke generator script
27-
set(_generated_file
28-
"${CMAKE_CURRENT_BINARY_DIR}/ament_cmake_package_templates/templates.cmake")
29-
get_executable_path(_python_interpreter Python3::Interpreter CONFIGURE)
30-
set(_cmd
31-
"${_python_interpreter}"
32-
"${_generator}"
33-
"${_generated_file}"
34-
)
35-
execute_process(
36-
COMMAND ${_cmd}
37-
RESULT_VARIABLE _res
38-
)
39-
if(NOT _res EQUAL 0)
40-
string(REPLACE ";" " " _cmd_str "${_cmd}")
41-
message(FATAL_ERROR
42-
"execute_process(${_cmd_str}) returned error code ${_res}")
43-
endif()
28+
# invoke generator script
29+
set(_generated_file
30+
"${CMAKE_CURRENT_BINARY_DIR}/ament_cmake_package_templates/templates.cmake")
31+
get_executable_path(_python_interpreter Python3::Interpreter CONFIGURE)
32+
set(_cmd
33+
"${_python_interpreter}"
34+
"${_generator}"
35+
"${_generated_file}"
36+
)
37+
execute_process(
38+
COMMAND ${_cmd}
39+
RESULT_VARIABLE _res
40+
)
41+
if(NOT _res EQUAL 0)
42+
string(REPLACE ";" " " _cmd_str "${_cmd}")
43+
message(FATAL_ERROR
44+
"execute_process(${_cmd_str}) returned error code ${_res}")
45+
endif()
4446

45-
# load extracted variables into cmake
46-
# for each environment hook defined in `ament_package`
47-
# (e.g. `library_path.bat|sh`) a CMake variable is defined starting with
48-
# `ament_cmake_package_templates_ENVIRONMENT_HOOK_`
49-
# (e.g. `ament_cmake_package_templates_ENVIRONMENT_HOOK_LIBRARY_PATH`)
50-
include("${_generated_file}")
47+
# load extracted variables into cmake
48+
# for each environment hook defined in `ament_package`
49+
# (e.g. `library_path.bat|sh`) a CMake variable is defined starting with
50+
# `ament_cmake_package_templates_ENVIRONMENT_HOOK_`
51+
# (e.g. `ament_cmake_package_templates_ENVIRONMENT_HOOK_LIBRARY_PATH`)
52+
include("${_generated_file}")
53+
endif()

ament_cmake_core/ament_cmake_uninstall_target-extras.cmake

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,27 @@ if(AMENT_CMAKE_UNINSTALL_TARGET)
2121
include(
2222
"${ament_cmake_core_DIR}/uninstall_target/ament_cmake_uninstall_target_append_uninstall_code.cmake")
2323

24-
# create the install script from the template
25-
# ament_cmake_core/uninstall_target/ament_cmake_uninstall_target.cmake.in
26-
set(AMENT_CMAKE_UNINSTALL_TARGET_UNINSTALL_SCRIPT
27-
"${CMAKE_CURRENT_BINARY_DIR}/ament_cmake_uninstall_target/ament_cmake_uninstall_target.cmake")
28-
configure_file(
29-
"${ament_cmake_core_DIR}/uninstall_target/ament_cmake_uninstall_target.cmake.in"
30-
"${AMENT_CMAKE_UNINSTALL_TARGET_UNINSTALL_SCRIPT}"
31-
@ONLY
32-
)
24+
get_property(_cmake_role GLOBAL PROPERTY CMAKE_ROLE)
25+
if("${_cmake_role}" STREQUAL "PROJECT")
26+
# create the install script from the template
27+
# ament_cmake_core/uninstall_target/ament_cmake_uninstall_target.cmake.in
28+
set(AMENT_CMAKE_UNINSTALL_TARGET_UNINSTALL_SCRIPT
29+
"${CMAKE_CURRENT_BINARY_DIR}/ament_cmake_uninstall_target/ament_cmake_uninstall_target.cmake")
30+
configure_file(
31+
"${ament_cmake_core_DIR}/uninstall_target/ament_cmake_uninstall_target.cmake.in"
32+
"${AMENT_CMAKE_UNINSTALL_TARGET_UNINSTALL_SCRIPT}"
33+
@ONLY
34+
)
3335

34-
if(NOT TARGET uninstall)
35-
add_custom_target(uninstall)
36-
endif()
36+
if(NOT TARGET uninstall)
37+
add_custom_target(uninstall)
38+
endif()
3739

38-
if(NOT TARGET ${PROJECT_NAME}_uninstall)
39-
# register uninstall target to run generated CMake script
40-
add_custom_target(${PROJECT_NAME}_uninstall
41-
COMMAND ${CMAKE_COMMAND} -P "${AMENT_CMAKE_UNINSTALL_TARGET_UNINSTALL_SCRIPT}")
42-
add_dependencies(uninstall ${PROJECT_NAME}_uninstall)
40+
if(NOT TARGET ${PROJECT_NAME}_uninstall)
41+
# register uninstall target to run generated CMake script
42+
add_custom_target(${PROJECT_NAME}_uninstall
43+
COMMAND ${CMAKE_COMMAND} -P "${AMENT_CMAKE_UNINSTALL_TARGET_UNINSTALL_SCRIPT}")
44+
add_dependencies(uninstall ${PROJECT_NAME}_uninstall)
45+
endif()
4346
endif()
4447
endif()

ament_cmake_core/cmake/core/all.cmake

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ endif()
3030

3131
# the following operations must be performed inside a project context
3232
if(NOT PROJECT_NAME)
33-
project(ament_cmake_internal NONE)
33+
get_property(_cmake_role GLOBAL PROPERTY CMAKE_ROLE)
34+
if("${_cmake_role}" STREQUAL "PROJECT")
35+
project(ament_cmake_internal NONE)
36+
endif()
3437
endif()
3538

3639
# use BUILD_TESTING to avoid warnings about not using it
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
add_test(NAME smoke
2+
COMMAND "${CMAKE_COMMAND}" -P "${CMAKE_CURRENT_SOURCE_DIR}/smoke.cmake")

ament_cmake_core/test/smoke.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
set(ament_cmake_core_DIR "${CMAKE_CURRENT_LIST_DIR}/../cmake")
2+
include("${CMAKE_CURRENT_LIST_DIR}/../ament_cmake_core-extras.cmake")

ament_cmake_test/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ ament_package(
1111
CONFIG_EXTRAS "ament_cmake_test-extras.cmake"
1212
)
1313

14+
include(CTest)
15+
if(BUILD_TESTING)
16+
add_subdirectory(test)
17+
endif()
18+
1419
install(
1520
DIRECTORY cmake
1621
DESTINATION share/${PROJECT_NAME}

ament_cmake_test/ament_cmake_test-extras.cmake

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414

1515
# copied from ament_cmake_test/ament_cmake_test-extras.cmake
1616

17-
enable_testing()
17+
get_property(_cmake_role GLOBAL PROPERTY CMAKE_ROLE)
18+
if("${_cmake_role}" STREQUAL "PROJECT")
19+
enable_testing()
20+
endif()
1821
# same option as in the CTest module
1922
option(BUILD_TESTING "Build the testing tree." ON)
2023

@@ -24,7 +27,7 @@ set(
2427
CACHE PATH "The path where test results are generated"
2528
)
2629

27-
if(BUILD_TESTING)
30+
if(BUILD_TESTING AND "${_cmake_role}" STREQUAL "PROJECT")
2831
# configure ctest not to truncate the dashboard summary
2932
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/CTestCustom.cmake"
3033
"set(CTEST_CUSTOM_MAXIMUM_PASSED_TEST_OUTPUT_SIZE 0)\n"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
add_test(NAME smoke
2+
COMMAND "${CMAKE_COMMAND}" -P "${CMAKE_CURRENT_SOURCE_DIR}/smoke.cmake")

0 commit comments

Comments
 (0)