Skip to content

Commit

Permalink
Prototype code for seeing if FindPython3 is useable for rosidl_python
Browse files Browse the repository at this point in the history
  • Loading branch information
sloretz committed Aug 4, 2021
1 parent 17de859 commit f74448c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 84 deletions.
2 changes: 1 addition & 1 deletion rosidl_generator_py/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.5)
cmake_minimum_required(VERSION 3.14)

project(rosidl_generator_py)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ find_package(rosidl_runtime_c REQUIRED)
find_package(rosidl_typesupport_c REQUIRED)
find_package(rosidl_typesupport_interface REQUIRED)

find_package(PythonInterp 3.5 REQUIRED)
# find_package(PythonInterp 3.5 REQUIRED)

find_package(python_cmake_module REQUIRED)
find_package(PythonExtra MODULE REQUIRED)
# find_package(python_cmake_module REQUIRED)
# find_package(PythonExtra MODULE REQUIRED)

find_package(Python3 REQUIRED Development Interpreter NumPy)

# Get a list of typesupport implementations from valid rmw implementations.
rosidl_generator_py_get_typesupports(_typesupport_impls)
Expand Down Expand Up @@ -126,11 +128,6 @@ endif()

set(_target_suffix "__py")

set(_PYTHON_EXECUTABLE ${PYTHON_EXECUTABLE})
if(WIN32 AND "${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
set(PYTHON_EXECUTABLE ${PYTHON_EXECUTABLE_DEBUG})
endif()

# move custom command into a subdirectory to avoid multiple invocations on Windows
set(_subdir "${CMAKE_CURRENT_BINARY_DIR}/${rosidl_generate_interfaces_TARGET}${_target_suffix}")
file(MAKE_DIRECTORY "${_subdir}")
Expand All @@ -142,23 +139,6 @@ set_property(
${_generated_extension_files} ${_generated_py_files} ${_generated_c_files}
PROPERTY GENERATED 1)

macro(set_properties _build_type)
set_target_properties(${_target_name} PROPERTIES
COMPILE_OPTIONS "${_extension_compile_flags}"
PREFIX ""
LIBRARY_OUTPUT_DIRECTORY${_build_type} ${_output_path}
RUNTIME_OUTPUT_DIRECTORY${_build_type} ${_output_path}
OUTPUT_NAME "${PROJECT_NAME}_s__${_typesupport_impl}${PythonExtra_EXTENSION_SUFFIX}"
SUFFIX "${PythonExtra_EXTENSION_EXTENSION}")
endmacro()

macro(set_lib_properties _build_type)
set_target_properties(${_target_name_lib} PROPERTIES
COMPILE_OPTIONS "${_extension_compile_flags}"
LIBRARY_OUTPUT_DIRECTORY${_build_type} ${_output_path}
RUNTIME_OUTPUT_DIRECTORY${_build_type} ${_output_path})
endmacro()

set(_target_name_lib "${rosidl_generate_interfaces_TARGET}__python")
add_library(${_target_name_lib} SHARED ${_generated_c_files})
target_link_libraries(${_target_name_lib}
Expand All @@ -171,44 +151,25 @@ add_dependencies(

target_link_libraries(
${_target_name_lib}
${PythonExtra_LIBRARIES}
Python3::NumPy
Python3::Python
)
target_include_directories(${_target_name_lib}
PUBLIC
${CMAKE_CURRENT_BINARY_DIR}/rosidl_generator_c
${CMAKE_CURRENT_BINARY_DIR}/rosidl_generator_py
${PythonExtra_INCLUDE_DIRS}
)

# Check if numpy is in the include path
find_file(_numpy_h numpy/numpyconfig.h
PATHS ${PythonExtra_INCLUDE_DIRS}
)
# rosidl_get_typesupport_target(c_typesupport_target "${rosidl_generate_interfaces_TARGET}" "rosidl_typesupport_c")
# TODO(sloretz) - replace line below with ros2/rosidl#606 line above
set(c_typesupport_target"${rosidl_generate_interfaces_TARGET}__rosidl_typesupport_c") # XXX
target_link_libraries(${_target_name_lib} ${c_typesupport_target})

if(APPLE OR WIN32 OR NOT _numpy_h)
# add include directory for numpy headers
set(_python_code
"import numpy"
"print(numpy.get_include())"
)
execute_process(
COMMAND "${PYTHON_EXECUTABLE}" "-c" "${_python_code}"
OUTPUT_VARIABLE _output
RESULT_VARIABLE _result
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if(NOT _result EQUAL 0)
message(FATAL_ERROR
"execute_process(${PYTHON_EXECUTABLE} -c '${_python_code}') returned "
"error code ${_result}")
endif()
message(STATUS "Using numpy include directory: ${_output}")
target_include_directories(${_target_name_lib} PUBLIC "${_output}")
set(_extension_compile_flags "")
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set(_extension_compile_flags -Wall -Wextra)
endif()

rosidl_target_interfaces(${_target_name_lib}
${rosidl_generate_interfaces_TARGET} rosidl_typesupport_c)

foreach(_typesupport_impl ${_typesupport_impls})
find_package(${_typesupport_impl} REQUIRED)
# a typesupport package might not be able to generated a target anymore
Expand All @@ -217,10 +178,9 @@ foreach(_typesupport_impl ${_typesupport_impls})
continue()
endif()

set(_pyext_suffix "__pyext")
set(_target_name "${PROJECT_NAME}__${_typesupport_impl}${_pyext_suffix}")
set(_target_name "${PROJECT_NAME}_s__${_typesupport_impl}")

add_library(${_target_name} SHARED
Python3_add_library(${_target_name} MODULE
${_generated_extension_${_typesupport_impl}_files}
)
add_dependencies(
Expand All @@ -229,60 +189,54 @@ foreach(_typesupport_impl ${_typesupport_impls})
${rosidl_generate_interfaces_TARGET}__rosidl_typesupport_c
)

set(_extension_compile_flags "")
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set(_extension_compile_flags -Wall -Wextra)
endif()
set_properties("")
if(WIN32)
set_properties("_DEBUG")
set_properties("_MINSIZEREL")
set_properties("_RELEASE")
set_properties("_RELWITHDEBINFO")
endif()
# target_compile_options(${_target_name} PRIVATE ${_extension_compile_flags})
# TODO(sloretz) use target_compile_options when python extension passes -Wpedantic
set_target_properties(${_target_name} PROPERTIES COMPILE_OPTIONS "${_extension_compile_flags}") # XXX

set_target_properties(${_target_name} PROPERTIES
LIBRARY_OUTPUT_DIRECTORY ${_output_path}
RUNTIME_OUTPUT_DIRECTORY ${_output_path})

target_link_libraries(
${_target_name}
${_target_name} PUBLIC
${_target_name_lib}
${PythonExtra_LIBRARIES}
${rosidl_generate_interfaces_TARGET}__${_typesupport_impl}
)

target_include_directories(${_target_name}
PUBLIC
${CMAKE_CURRENT_BINARY_DIR}/rosidl_generator_c
${CMAKE_CURRENT_BINARY_DIR}/rosidl_generator_py
${PythonExtra_INCLUDE_DIRS}
)

rosidl_target_interfaces(${_target_name}
${rosidl_generate_interfaces_TARGET} rosidl_typesupport_c)
target_link_libraries(${_target_name} PUBLIC ${c_typesupport_target})

ament_target_dependencies(${_target_name}
ament_target_dependencies(${_target_name} PUBLIC
"rosidl_runtime_c"
"rosidl_typesupport_c"
"rosidl_typesupport_interface"
)
foreach(_pkg_name ${rosidl_generate_interfaces_DEPENDENCY_PACKAGE_NAMES})
ament_target_dependencies(${_target_name}
ament_target_dependencies(${_target_name} PUBLIC
${_pkg_name}
)
endforeach()

add_dependencies(${_target_name}
${rosidl_generate_interfaces_TARGET}__${_typesupport_impl}
)
ament_target_dependencies(${_target_name}
ament_target_dependencies(${_target_name} PUBLIC
"rosidl_runtime_c"
"rosidl_generator_py"
)

if(NOT rosidl_generate_interfaces_SKIP_INSTALL)
# PYTHON_INSTALL_DIR is defined by ament_cmake_python
install(TARGETS ${_target_name}
DESTINATION "${PYTHON_INSTALL_DIR}/${PROJECT_NAME}")
endif()
endforeach()

set(PYTHON_EXECUTABLE ${_PYTHON_EXECUTABLE})

foreach(_pkg_name ${rosidl_generate_interfaces_DEPENDENCY_PACKAGE_NAMES})
set(_pkg_install_base "${${_pkg_name}_DIR}/../../..")
Expand All @@ -297,13 +251,15 @@ foreach(_pkg_name ${rosidl_generate_interfaces_DEPENDENCY_PACKAGE_NAMES})
endif()
endforeach()

set_lib_properties("")
if(WIN32)
set_lib_properties("_DEBUG")
set_lib_properties("_MINSIZEREL")
set_lib_properties("_RELEASE")
set_lib_properties("_RELWITHDEBINFO")
endif()

# target_compile_options(${_target_name_lib} PRIVATE ${_extension_compile_flags})
# TODO(sloretz) use target_compile_options when python extension passes -Wpedantic
set_target_properties(${_target_name_lib} PROPERTIES COMPILE_OPTIONS "${_extension_compile_flags}") # XXX

set_target_properties(${_target_name_lib} PROPERTIES
LIBRARY_OUTPUT_DIRECTORY ${_output_path}
RUNTIME_OUTPUT_DIRECTORY ${_output_path})

if(NOT rosidl_generate_interfaces_SKIP_INSTALL)
install(TARGETS ${_target_name_lib}
ARCHIVE DESTINATION lib
Expand Down

0 comments on commit f74448c

Please sign in to comment.