Skip to content

Commit a608556

Browse files
authored
Use modern cmake targets to avoid absolute paths to appear in binary archives (#160)
* Use FindPython3 Signed-off-by: Ivan Santiago Paunovic <[email protected]> * Make link libraries private when possible to avoid having to export dependencies Signed-off-by: Ivan Santiago Paunovic <[email protected]> * Use Python3::Numpy Signed-off-by: Ivan Santiago Paunovic <[email protected]>
1 parent 4d0d65d commit a608556

File tree

1 file changed

+11
-38
lines changed

1 file changed

+11
-38
lines changed

rosidl_generator_py/cmake/rosidl_generator_py_generate_interfaces.cmake

Lines changed: 11 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ find_package(PythonInterp 3.6 REQUIRED)
2121

2222
find_package(python_cmake_module REQUIRED)
2323
find_package(PythonExtra MODULE REQUIRED)
24+
find_package(Python3 REQUIRED COMPONENTS Development NumPy)
2425

2526
# Get a list of typesupport implementations from valid rmw implementations.
2627
rosidl_generator_py_get_typesupports(_typesupport_impls)
@@ -165,52 +166,23 @@ set(rosidl_generator_py_suffix "__rosidl_generator_py")
165166
set(_target_name_lib "${rosidl_generate_interfaces_TARGET}${rosidl_generator_py_suffix}")
166167
add_library(${_target_name_lib} SHARED ${_generated_c_files})
167168
target_link_libraries(${_target_name_lib}
169+
PRIVATE
168170
${rosidl_generate_interfaces_TARGET}__rosidl_generator_c)
169171
add_dependencies(
170172
${_target_name_lib}
171173
${rosidl_generate_interfaces_TARGET}${_target_suffix}
172174
${rosidl_generate_interfaces_TARGET}__rosidl_typesupport_c
173175
)
174-
175-
target_link_libraries(
176-
${_target_name_lib}
177-
${PythonExtra_LIBRARIES}
178-
)
179176
target_include_directories(${_target_name_lib}
180177
PRIVATE
181178
${CMAKE_CURRENT_BINARY_DIR}/rosidl_generator_c
182179
${CMAKE_CURRENT_BINARY_DIR}/rosidl_generator_py
183-
${PythonExtra_INCLUDE_DIRS}
184-
)
185-
186-
# Check if numpy is in the include path
187-
find_file(_numpy_h numpy/numpyconfig.h
188-
PATHS ${PythonExtra_INCLUDE_DIRS}
189180
)
190181

191-
if(APPLE OR WIN32 OR NOT _numpy_h)
192-
# add include directory for numpy headers
193-
set(_python_code
194-
"import numpy"
195-
"print(numpy.get_include())"
196-
)
197-
execute_process(
198-
COMMAND "${PYTHON_EXECUTABLE}" "-c" "${_python_code}"
199-
OUTPUT_VARIABLE _output
200-
RESULT_VARIABLE _result
201-
OUTPUT_STRIP_TRAILING_WHITESPACE
202-
)
203-
if(NOT _result EQUAL 0)
204-
message(FATAL_ERROR
205-
"execute_process(${PYTHON_EXECUTABLE} -c '${_python_code}') returned "
206-
"error code ${_result}")
207-
endif()
208-
message(STATUS "Using numpy include directory: ${_output}")
209-
target_include_directories(${_target_name_lib} PUBLIC "${_output}")
210-
endif()
182+
target_link_libraries(${_target_name_lib} PRIVATE Python3::NumPy Python3::Module)
211183

212184
rosidl_get_typesupport_target(c_typesupport_target "${rosidl_generate_interfaces_TARGET}" "rosidl_typesupport_c")
213-
target_link_libraries(${_target_name_lib} ${c_typesupport_target})
185+
target_link_libraries(${_target_name_lib} PRIVATE ${c_typesupport_target})
214186

215187
foreach(_typesupport_impl ${_typesupport_impls})
216188
find_package(${_typesupport_impl} REQUIRED)
@@ -245,35 +217,36 @@ foreach(_typesupport_impl ${_typesupport_impls})
245217
endif()
246218
target_link_libraries(
247219
${_target_name}
220+
PRIVATE
248221
${_target_name_lib}
249-
${PythonExtra_LIBRARIES}
250222
${rosidl_generate_interfaces_TARGET}__${_typesupport_impl}
223+
Python3::Module
251224
)
252225

253226
target_include_directories(${_target_name}
254227
PUBLIC
255228
${CMAKE_CURRENT_BINARY_DIR}/rosidl_generator_c
256229
${CMAKE_CURRENT_BINARY_DIR}/rosidl_generator_py
257-
${PythonExtra_INCLUDE_DIRS}
258230
)
259231

260-
target_link_libraries(${_target_name} ${c_typesupport_target})
232+
target_link_libraries(${_target_name} PRIVATE ${c_typesupport_target})
261233

262234
ament_target_dependencies(${_target_name}
235+
PUBLIC
263236
"rosidl_runtime_c"
264237
"rosidl_typesupport_c"
265238
"rosidl_typesupport_interface"
266239
)
267240
foreach(_pkg_name ${rosidl_generate_interfaces_DEPENDENCY_PACKAGE_NAMES})
268-
ament_target_dependencies(${_target_name}
241+
ament_target_dependencies(${_target_name} PUBLIC
269242
${_pkg_name}
270243
)
271244
endforeach()
272245

273246
add_dependencies(${_target_name}
274247
${rosidl_generate_interfaces_TARGET}__${_typesupport_impl}
275248
)
276-
ament_target_dependencies(${_target_name}
249+
ament_target_dependencies(${_target_name} PUBLIC
277250
"rosidl_runtime_c"
278251
"rosidl_generator_py"
279252
)
@@ -288,7 +261,7 @@ set(PYTHON_EXECUTABLE ${_PYTHON_EXECUTABLE})
288261

289262
# Depend on rosidl_generator_py generated targets from our dependencies
290263
foreach(_pkg_name ${rosidl_generate_interfaces_DEPENDENCY_PACKAGE_NAMES})
291-
target_link_libraries(${_target_name_lib} ${${_pkg_name}_TARGETS${rosidl_generator_py_suffix}})
264+
target_link_libraries(${_target_name_lib} PRIVATE ${${_pkg_name}_TARGETS${rosidl_generator_py_suffix}})
292265
endforeach()
293266

294267
set_lib_properties("")

0 commit comments

Comments
 (0)