-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #130 from GiulioRomualdi/icub-models-library
Implement a machinery to easily access model locations on C++ and Python
- Loading branch information
Showing
12 changed files
with
1,206 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# Copyright (C) 2022 Istituto Italiano di Tecnologia (IIT). All rights reserved. | ||
# This software may be modified and distributed under the terms of the | ||
# GNU Lesser General Public License v2.1 or any later version. | ||
|
||
if(ICUB_MODELS_COMPILE_PYTHON_BINDINGS) | ||
|
||
find_package(pybind11 REQUIRED) | ||
find_package(Python3 COMPONENTS Interpreter REQUIRED) | ||
|
||
|
||
# define new line accordingly to the operating system | ||
if (WIN32) | ||
set(NEW_LINE "\n\r") | ||
else() | ||
set(NEW_LINE "\n") | ||
endif() | ||
|
||
option(ICUB_MODELS_DETECT_ACTIVE_PYTHON_SITEPACKAGES | ||
"Do you want icub-models to detect and use the active site-package directory? (it could be a system dir)" | ||
FALSE) | ||
|
||
# Install the resulting Python package for the active interpreter | ||
if(ICUB_MODELS_DETECT_ACTIVE_PYTHON_SITEPACKAGES) | ||
set(PYTHON_INSTDIR ${Python3_SITELIB}/icub_models) | ||
else() | ||
execute_process(COMMAND ${Python3_EXECUTABLE} -c "from distutils import sysconfig; print(sysconfig.get_python_lib(1,0,prefix=''))" | ||
OUTPUT_VARIABLE _PYTHON_INSTDIR) | ||
|
||
string(STRIP ${_PYTHON_INSTDIR} _PYTHON_INSTDIR_CLEAN) | ||
set(PYTHON_INSTDIR ${_PYTHON_INSTDIR_CLEAN}/icub_models) | ||
endif() | ||
|
||
# Folder of the Python package within the build tree. | ||
# It is used for the Python tests. | ||
set(ICUB_MODELS_PYTHON_PACKAGE "${CMAKE_BINARY_DIR}/icub_models") | ||
|
||
# Add the bindings directory | ||
add_subdirectory(python) | ||
|
||
# Create the __init__.py file | ||
file(GENERATE | ||
OUTPUT "${ICUB_MODELS_PYTHON_PACKAGE}/__init__.py" | ||
CONTENT "from icub_models.bindings import *") | ||
|
||
# Install the __init__.py file | ||
install(FILES "${ICUB_MODELS_PYTHON_PACKAGE}/__init__.py" | ||
DESTINATION ${PYTHON_INSTDIR}) | ||
|
||
# Install pip metadata files to ensure that icub_models installed via CMake is listed by pip list | ||
# See https://packaging.python.org/specifications/recording-installed-packages/ | ||
# and https://packaging.python.org/en/latest/specifications/core-metadata/#core-metadata | ||
option(ICUB_MODELS_PYTHON_PIP_METADATA_INSTALL "Use CMake to install Python pip metadata. Set to off if some other tool already installs it." ON) | ||
mark_as_advanced(ICUB_MODELS_PYTHON_PIP_METADATA_INSTALL) | ||
set(ICUB_MODELS_PYTHON_PIP_METADATA_INSTALLER "cmake" CACHE STRING "Specify the string to identify the pip Installer. Default: cmake, change this if you are using another tool.") | ||
mark_as_advanced(ICUB_MODELS_PYTHON_PIP_METADATA_INSTALLER) | ||
if(ICUB_MODELS_PYTHON_PIP_METADATA_INSTALL) | ||
get_filename_component(PYTHON_METADATA_PARENT_DIR ${PYTHON_INSTDIR} DIRECTORY) | ||
if(WIN32) | ||
set(NEW_LINE "\n\r") | ||
else() | ||
set(NEW_LINE "\n") | ||
endif() | ||
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/METADATA "") | ||
file(APPEND ${CMAKE_CURRENT_BINARY_DIR}/METADATA "Metadata-Version: 2.1${NEW_LINE}") | ||
file(APPEND ${CMAKE_CURRENT_BINARY_DIR}/METADATA "Name: icub-models${NEW_LINE}") | ||
file(APPEND ${CMAKE_CURRENT_BINARY_DIR}/METADATA "Version: ${PROJECT_VERSION}${NEW_LINE}") | ||
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/INSTALLER "${ICUB_MODELS_PYTHON_PIP_METADATA_INSTALLER}${NEW_LINE}") | ||
install( | ||
FILES "${CMAKE_CURRENT_BINARY_DIR}/METADATA" "${CMAKE_CURRENT_BINARY_DIR}/INSTALLER" | ||
DESTINATION ${PYTHON_METADATA_PARENT_DIR}/icub_models-${PROJECT_VERSION}.dist-info) | ||
endif() | ||
|
||
endif() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Copyright (C) 2022 Istituto Italiano di Tecnologia (IIT). All rights reserved. | ||
# This software may be modified and distributed under the terms of the | ||
# GNU Lesser General Public License v2.1 or any later version. | ||
|
||
pybind11_add_module(pybind11_icub_models MODULE | ||
${CMAKE_CURRENT_SOURCE_DIR}/icub_models.cpp | ||
) | ||
|
||
target_include_directories(pybind11_icub_models PUBLIC "$<BUILD_INTERFACE:${pybind_include_dirs}>") | ||
|
||
target_link_libraries(pybind11_icub_models PRIVATE | ||
icub-models::icub-models) | ||
|
||
# # The generated Python dynamic module must have the same name as the pybind11 | ||
# # module, i.e. `bindings`. | ||
set_target_properties(pybind11_icub_models PROPERTIES | ||
LIBRARY_OUTPUT_DIRECTORY "${ICUB_MODELS_PYTHON_PACKAGE}" | ||
OUTPUT_NAME "bindings") | ||
|
||
|
||
# Output package is: | ||
# | ||
# icub_models | ||
# |-- __init__.py (generated from main bindings CMake file) | ||
# `-- bindings.<cpython_extension> | ||
|
||
install(TARGETS pybind11_icub_models DESTINATION ${PYTHON_INSTDIR}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* @file icub_models.cpp | ||
* @authors Giulio Romualdi | ||
* @copyright 2022 Istituto Italiano di Tecnologia Released under the terms of the Creative Commons Attribution Share Alike 4.0 International | ||
*/ | ||
|
||
#include <iCubModels/iCubModels.h> | ||
|
||
#include <pybind11/pybind11.h> | ||
#include <pybind11/stl.h> | ||
|
||
namespace iCubModels | ||
{ | ||
namespace | ||
{ | ||
|
||
namespace py = ::pybind11; | ||
PYBIND11_MODULE(bindings, m) | ||
{ | ||
m.doc() = "Python bindings for the icub-models."; | ||
|
||
m.def("get_models_path", &iCubModels::getModelsPath, | ||
"Get the folder where the models are installed.") | ||
.def("get_robot_names", &iCubModels::getRobotNames, | ||
"Return a set containing the names of the robots installed.") | ||
.def("get_model_file", &iCubModels::getModelFile, py::arg("model_name"), | ||
"Return the path of the model given its name. If the 'model_name' is not in the list " | ||
"of the installed robot an empty path is returned."); | ||
} | ||
|
||
} // namespace | ||
} // namespace iCubModels |
Oops, something went wrong.