Skip to content

Commit

Permalink
[misc] Bundle cmake FindJiminy helper in wheel.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexis Duburcq committed Apr 17, 2021
1 parent fb44fa2 commit 91e4075
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 5 deletions.
75 changes: 75 additions & 0 deletions build_tools/cmake/JiminyConfig.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Enable link rpath to find shared library dependencies at runtime
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)

# Get Python executable
if(DEFINED PYTHON_EXECUTABLE)
find_program(Python_EXECUTABLE python PATHS "${PYTHON_EXECUTABLE}" NO_DEFAULT_PATH)
else()
if(CMAKE_VERSION VERSION_LESS "3.12.4")
find_program(Python_EXECUTABLE "python${PYTHON_REQUIRED_VERSION}")
else()
if(PYTHON_REQUIRED_VERSION)
find_package(Python ${PYTHON_REQUIRED_VERSION} EXACT COMPONENTS Interpreter)
else()
find_package(Python COMPONENTS Interpreter)
endif()
endif()
endif()
if(NOT Python_EXECUTABLE)
if (Python_FIND_REQUIRED)
message(FATAL_ERROR "Python executable not found, CMake will exit.")
else()
return()
endif()
endif()

# Make sure Jiminy Python module is available
execute_process(COMMAND "${Python_EXECUTABLE}" -c
"import importlib; print(int(importlib.util.find_spec('jiminy_py') is not None), end='')"
OUTPUT_VARIABLE Jiminy_FOUND)
if (NOT Jiminy_FOUND)
if (Python_FIND_REQUIRED)
message(FATAL_ERROR "`jiminy_py` Python module not found, CMake will exit.")
else()
return()
endif()
endif()

# Find Jiminy library and headers.
# Note that Jiminy is compiled under C++17 using either old or new CXX11 ABI.
# Make sure very project dependencies are compiled for the same CXX11 ABI
# otherwise segfaults may occur. It should be fine for the standard library,
# but not for precompiled boost libraries such as boost::filesystem.
# https://gcc.gnu.org/onlinedocs/libstdc++/manual/abi.html
execute_process(COMMAND "${Python_EXECUTABLE}" -c
"import jiminy_py; print(jiminy_py.__version__, end='')"
OUTPUT_VARIABLE Jiminy_VERSION)
execute_process(COMMAND "${Python_EXECUTABLE}" -c
"import jiminy_py; print(jiminy_py.get_include(), end='')"
OUTPUT_VARIABLE Jiminy_INCLUDE_DIRS)
execute_process(COMMAND "${Python_EXECUTABLE}" -c
"import jiminy_py; print(jiminy_py.get_libraries(), end='')"
OUTPUT_VARIABLE Jiminy_LIBRARIES)
if(NOT WIN32)
execute_process(COMMAND readelf --version-info "${Jiminy_LIBRARIES}"
COMMAND grep -c "Name: CXXABI_1.3.9\\\|Name: GLIBCXX_3.4.21"
OUTPUT_STRIP_TRAILING_WHITESPACE
OUTPUT_VARIABLE CHECK_NEW_CXX11_ABI)
if(CHECK_NEW_CXX11_ABI EQUAL 2)
set(Jiminy_DEFINITIONS _GLIBCXX_USE_CXX11_ABI=1)
else()
set(Jiminy_DEFINITIONS _GLIBCXX_USE_CXX11_ABI=0)
endif()
endif()

# Define imported target
add_library(Jiminy SHARED IMPORTED GLOBAL)
set_target_properties(Jiminy PROPERTIES
IMPORTED_LOCATION "${Jiminy_LIBRARIES}"
INTERFACE_INCLUDE_DIRECTORIES "${Jiminy_INCLUDE_DIRS}"
INTERFACE_COMPILE_DEFINITIONS ${Jiminy_DEFINITIONS}
INTERFACE_COMPILE_FEATURES cxx_std_17
CXX_STANDARD 17
CXX_STANDARD_REQUIRED YES
CXX_EXTENSIONS NO
)
6 changes: 4 additions & 2 deletions python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ install(CODE "file(GLOB_RECURSE src_file_list FOLLOW_SYMLINKS
endforeach()"
)

# Bundle headers and core library into Python package `core` submodule.
# Bundle headers, core library, and cmake config into Python package `core` submodule.
# Disable forbidden access to target LOCATION property is necessary, since expression
# generator in `install(CODE ...)` commands is only support from Cmake 3.14 and upward,
# which is newer than default version available on Ubuntu 18.04. For reference, see:
Expand Down Expand Up @@ -53,7 +53,9 @@ install(CODE "foreach(target_source \"${CORE_SOURCE_DIR}\" \"${PYWRAP_SOURCE_DIR
get_filename_component(CORE_LIB_REAL \"${CORE_LIB}\" REALPATH)
get_filename_component(CORE_LIB_NAME \"${CORE_LIB}\" NAME)
file(COPY \"\${CORE_LIB_REAL}/\"
DESTINATION \"${CMAKE_BINARY_DIR}/pypi/${LIBRARY_NAME}_py/src/${LIBRARY_NAME}_py/core/lib/\${CORE_LIB_NAME}\")"
DESTINATION \"${CMAKE_BINARY_DIR}/pypi/${LIBRARY_NAME}_py/src/${LIBRARY_NAME}_py/core/lib/\${CORE_LIB_NAME}\")
file(COPY \"${CMAKE_SOURCE_DIR}/build_tools/cmake/JiminyConfig.cmake\"
DESTINATION \"${CMAKE_BINARY_DIR}/pypi/${LIBRARY_NAME}_py/src/${LIBRARY_NAME}_py/core/cmake\")"
)

################ jiminy_py ################
Expand Down
5 changes: 3 additions & 2 deletions python/jiminy_py/MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
recursive-include src/jiminy_py *.so *.dll *.pyd *.html *.js
recursive-include src/jiminy_py/core/include * *.h *.tpp
recursive-include src/jiminy_py/core/lib *.a *.so[0-9\.]* *.dll
recursive-include src/jiminy_py/core/include *.h *.tpp
recursive-include src/jiminy_py/core/lib *.a *.so[0-9\.]* *.dll *.lib
recursive-include src/jiminy_py/core/cmake *.cmake
9 changes: 9 additions & 0 deletions python/jiminy_py/setup.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import os
import sys
from setuptools import setup, dist, find_packages
from setuptools.command.install import install


# Define CMake configuration file path
CMAKE_CONFIG_FILE = os.path.relpath(
"@SOURCE_DIR@/build_tools/cmake/JiminyConfig.cmake",
os.path.dirname(__file__))

# Force setuptools to recognize that this is actually a binary distribution
class BinaryDistribution(dist.Distribution):
def is_pure(self) -> bool:
Expand Down Expand Up @@ -66,6 +72,9 @@ def finalize_options(self) -> None:
},
packages=find_packages("src"),
package_dir={"": "src"},
data_files=[
("cmake", [CMAKE_CONFIG_FILE])
],
include_package_data=True,
entry_points={"console_scripts": [
"jiminy_plot=jiminy_py.log:plot_log",
Expand Down
1 change: 1 addition & 0 deletions python/jiminy_py/src/jiminy_py/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


__all__ = [
'get_cmake_module_path',
'get_include',
'get_libraries',
'__version__',
Expand Down
7 changes: 6 additions & 1 deletion python/jiminy_py/src/jiminy_py/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@
from . import _pinocchio_init # noqa


# Define include and lib path
# Define helpers to build extension modules
def get_cmake_module_path():
return _os.path.join(_os.path.dirname(__file__), "cmake")


def get_include():
return _os.path.join(_os.path.dirname(__file__), "include")

Expand All @@ -94,6 +98,7 @@ def get_libraries():


__all__ += [
'get_cmake_module_path',
'get_include',
'get_libraries',
'__version__',
Expand Down

0 comments on commit 91e4075

Please sign in to comment.