-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
pybind as a dependency in own project; utilities-py; test-split #11147
Changes from 13 commits
db14e5b
849fad7
56c43c0
9857e22
3ec18e9
edf2130
5ea6bbb
809a418
0321109
0223be8
70da794
92c4b3e
257f974
e22a46e
15db22f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
cmake_minimum_required(VERSION 3.6) | ||
include(ExternalProject) | ||
|
||
# We use a function to enforce a scoped variables creation only for the build | ||
# (i.e turn off BUILD_SHARED_LIBS which is used on LRS build as well) | ||
function(get_pybind11) | ||
|
||
message( STATUS #CHECK_START | ||
"Fetching pybind11..." ) | ||
#list( APPEND CMAKE_MESSAGE_INDENT " " ) # Indent outputs | ||
|
||
# We want to clone the pybind repo and build it here, during configuration, so we can use it. | ||
# But ExternalProject_add is limited in that it only does its magic during build. | ||
# This is possible in CMake 3.12+ with FetchContent and FetchContent_MakeAvailable in 3.14+ (meaning Ubuntu 20) | ||
# but we need to adhere to CMake 3.10 (Ubuntu 18). | ||
# So instead, we invoke a new CMake project just to download pybind: | ||
configure_file( CMake/pybind11-download.cmake.in | ||
${CMAKE_BINARY_DIR}/external-projects/pybind11-download/CMakeLists.txt ) | ||
execute_process( COMMAND "${CMAKE_COMMAND}" -G "${CMAKE_GENERATOR}" . | ||
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/external-projects/pybind11-download" | ||
OUTPUT_QUIET ) | ||
execute_process( COMMAND "${CMAKE_COMMAND}" --build . | ||
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/external-projects/pybind11-download" | ||
OUTPUT_QUIET ) | ||
|
||
# Now that it's available, we can refer to it with an actual ExternalProject_add (but notice we're not | ||
# downloading anything) | ||
ExternalProject_Add( pybind11 | ||
PREFIX ${CMAKE_BINARY_DIR}/external-projects/pybind11 # just so it doesn't use build/pybind11-prefix | ||
SOURCE_DIR ${CMAKE_BINARY_DIR}/third-party/pybind11 | ||
BINARY_DIR ${CMAKE_BINARY_DIR}/third-party/pybind11/build | ||
CMAKE_ARGS | ||
-DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD} | ||
-DPYTHON_EXECUTABLE=${PYTHON_EXECUTABLE} | ||
-DBUILD_SHARED_LIBS=OFF | ||
# Suppress warnings that are meant for the author of the CMakeLists.txt files | ||
-Wno-dev | ||
# Just in case! | ||
-DPYBIND11_INSTALL=OFF | ||
-DPYBIND11_TEST=OFF | ||
|
||
INSTALL_COMMAND "" | ||
UPDATE_COMMAND "" | ||
PATCH_COMMAND "" | ||
TEST_COMMAND "" | ||
) | ||
|
||
# Force Pybind11 not to share pyrealsense2 resources with other pybind modules. | ||
# With this definition we force the ABI version to be unique and not risk crashes on different modules. | ||
# (workaround for RS5-10582; see also https://github.com/pybind/pybind11/issues/2898) | ||
add_definitions( -DPYBIND11_COMPILER_TYPE="_librs_abi" ) | ||
|
||
add_subdirectory( "${CMAKE_BINARY_DIR}/third-party/pybind11" | ||
"${CMAKE_BINARY_DIR}/third-party/pybind11/build" ) | ||
|
||
set_target_properties( pybind11 PROPERTIES FOLDER "ExternalProjectTargets" ) | ||
|
||
# Besides pybind11, any python module will also need to be installed using: | ||
# install( | ||
# TARGETS ${PROJECT_NAME} | ||
# EXPORT pyrealsense2Targets | ||
# LIBRARY DESTINATION ${PYTHON_INSTALL_DIR} | ||
# ARCHIVE DESTINATION ${PYTHON_INSTALL_DIR} | ||
# ) | ||
# But, to do this, we need to define PYTHON_INSTALL_DIR! | ||
if( CMAKE_VERSION VERSION_LESS 3.12 ) | ||
#find_package(PythonInterp REQUIRED) | ||
#find_package(PythonLibs REQUIRED) | ||
option( PYTHON_INSTALL_DIR "Installation directory for Python libraries" | ||
"${CMAKE_INSTALL_LIBDIR}/python${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}/pyrealsense2" | ||
) | ||
else() | ||
#find_package(Python REQUIRED COMPONENTS Interpreter Development) | ||
option( PYTHON_INSTALL_DIR "Installation directory for Python libraries" | ||
"${Python_SITEARCH}/pyrealsense2" | ||
) | ||
endif() | ||
|
||
message( STATUS #CHECK_PASS | ||
"Fetching pybind11 - Done" ) | ||
#list( POP_BACK CMAKE_MESSAGE_INDENT ) # Unindent outputs (requires cmake 3.15) | ||
|
||
endfunction() | ||
|
||
# Trigger the build | ||
get_pybind11() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
cmake_minimum_required(VERSION 3.6) | ||
project(pybind11-download NONE) | ||
|
||
include(ExternalProject) | ||
ExternalProject_Add( | ||
pybind11 | ||
PREFIX . | ||
GIT_REPOSITORY "https://github.com/pybind/pybind11.git" | ||
GIT_TAG v2.9.2 # 2.10 requires VS 2017+ and Python 3.6+ | ||
GIT_CONFIG advice.detachedHead=false # otherwise we'll get "You are in 'detached HEAD' state..." | ||
SOURCE_DIR "${CMAKE_BINARY_DIR}/third-party/pybind11" | ||
GIT_SHALLOW ON # No history needed (requires cmake 3.6) | ||
# Override default steps with no action, we just want the clone step. | ||
CONFIGURE_COMMAND "" | ||
BUILD_COMMAND "" | ||
INSTALL_COMMAND "" | ||
) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,14 @@ | ||
message(STATUS "Setting Unix configurations") | ||
|
||
macro(os_set_flags) | ||
|
||
# Put all the collaterals together, so we can find when trying to run examples/tests | ||
# Note: this puts the outputs under <binary>/<build-type> | ||
# Note note: this ruins our CI procedures... otherwise it mirrors Windows, simplifies | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a prefix 'We cannot uncomment because this.." There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A prefix? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
to
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed |
||
# scripts, makes things easier to find... c'est la vie! | ||
#set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_BUILD_TYPE}) | ||
#set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_BUILD_TYPE}) | ||
|
||
set(CMAKE_POSITION_INDEPENDENT_CODE ON) | ||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pedantic -g -D_DEFAULT_SOURCE") | ||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -g -Wno-missing-field-initializers") | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// License: Apache 2.0. See LICENSE file in root directory. | ||
// Copyright(c) 2022 Intel Corporation. All Rights Reserved. | ||
|
||
#pragma once | ||
|
||
#pragma warning(push) | ||
// third-party\pybind11\include\pybind11\pybind11.h(232): warning C4702: unreachable code | ||
#if defined(_MSC_VER) && _MSC_VER < 1910 // VS 2015's MSVC | ||
# pragma warning(disable: 4702) | ||
#endif | ||
#include <pybind11/pybind11.h> | ||
#pragma warning(pop) | ||
|
||
// convenience functions | ||
#include <pybind11/operators.h> | ||
|
||
// STL conversions | ||
#include <pybind11/stl.h> | ||
|
||
// std::chrono::* | ||
#include <pybind11/chrono.h> | ||
|
||
// makes certain STL containers opaque to prevent expensive copies | ||
#include <pybind11/stl_bind.h> | ||
|
||
// makes std::function conversions work | ||
#include <pybind11/functional.h> | ||
|
||
namespace py = pybind11; | ||
using namespace pybind11::literals; | ||
|
||
|
||
// "When calling a C++ function from Python, the GIL is always held" | ||
// -- since we're not being called from Python but instead are calling it, | ||
// we need to acquire it to not have issues with other threads... | ||
#define FN_FWD_CALL( CLS, FN_NAME, CODE ) \ | ||
try \ | ||
{ \ | ||
py::gil_scoped_acquire gil; \ | ||
CODE \ | ||
} \ | ||
catch( std::exception const & e ) \ | ||
{ \ | ||
LOG_ERROR( "EXCEPTION in python " #CLS "." #FN_NAME ": " << e.what() ); \ | ||
} \ | ||
catch( ... ) \ | ||
{ \ | ||
LOG_ERROR( "UNKNOWN EXCEPTION in python " #CLS "." #FN_NAME ); \ | ||
} | ||
#define FN_FWD( CLS, FN_NAME, PY_ARGS, FN_ARGS, CODE ) \ | ||
#FN_NAME, []( CLS & self, std::function < void PY_ARGS > callback ) { \ | ||
self.FN_NAME( [&self,callback] FN_ARGS { FN_FWD_CALL( CLS, FN_NAME, CODE ); } ); \ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# License: Apache 2.0. See LICENSE file in root directory. | ||
# Copyright(c) 2022 Intel Corporation. All Rights Reserved. | ||
cmake_minimum_required(VERSION 3.1.0) | ||
project( pyrsutils ) | ||
|
||
set( DEPENDENCIES utilities ) | ||
|
||
set( PYRSUTILS_FILES | ||
"pyrsutils.cpp" | ||
) | ||
|
||
pybind11_add_module( ${PROJECT_NAME} SHARED ${PYRSUTILS_FILES} ) | ||
Nir-Az marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
target_link_libraries( ${PROJECT_NAME} PRIVATE ${DEPENDENCIES} ) | ||
set_target_properties( ${PROJECT_NAME} | ||
PROPERTIES | ||
VERSION ${REALSENSE_VERSION_STRING} | ||
SOVERSION "${REALSENSE_VERSION_MAJOR}.${REALSENSE_VERSION_MINOR}" | ||
) | ||
set_target_properties( ${PROJECT_NAME} | ||
PROPERTIES | ||
FOLDER Library | ||
PROJECT_LABEL utilities-py | ||
) | ||
|
||
include("../include/utilities/easylogging/shared-init.cmake") | ||
|
||
install( | ||
TARGETS ${PROJECT_NAME} | ||
EXPORT pyrealsense2Targets | ||
LIBRARY DESTINATION ${PYTHON_INSTALL_DIR} | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// License: Apache 2.0. See LICENSE file in root directory. | ||
// Copyright(c) 2022 Intel Corporation. All Rights Reserved. | ||
|
||
#include <utilities/py/pybind11.h> | ||
#include <utilities/easylogging/easyloggingpp.h> | ||
#include <utilities/string/split.h> | ||
|
||
#define NAME pyrsutils | ||
#define SNAME "pyrsutils" | ||
|
||
|
||
#ifndef BUILD_SHARED_LIBS // shared-init takes care of the else | ||
INITIALIZE_EASYLOGGINGPP | ||
#endif | ||
|
||
|
||
PYBIND11_MODULE(NAME, m) { | ||
m.doc() = R"pbdoc( | ||
RealSense Utilities Python Bindings | ||
)pbdoc"; | ||
m.attr( "__version__" ) = "0.1"; // RS2_API_VERSION_STR; | ||
|
||
// Configure the same logger as librealsense, and default to only errors by default... | ||
el::Configurations defaultConf; | ||
defaultConf.setToDefault(); | ||
defaultConf.setGlobally( el::ConfigurationType::ToStandardOutput, "false" ); | ||
defaultConf.set( el::Level::Error, el::ConfigurationType::ToStandardOutput, "true" ); | ||
defaultConf.setGlobally( el::ConfigurationType::Format, "-%levshort- %datetime{%H:%m:%s.%g} %msg (%fbase:%line [%thread])" ); | ||
el::Loggers::reconfigureLogger( "librealsense", defaultConf ); | ||
|
||
m.def( | ||
"debug", | ||
[]( bool enable, std::string const & nested ) { | ||
el::Logger * logger = el::Loggers::getLogger( "librealsense" ); | ||
auto configs = logger->configurations(); | ||
configs->set( el::Level::Warning, el::ConfigurationType::ToStandardOutput, enable ? "true" : "false" ); | ||
configs->set( el::Level::Info, el::ConfigurationType::ToStandardOutput, enable ? "true" : "false" ); | ||
configs->set( el::Level::Debug, el::ConfigurationType::ToStandardOutput, enable ? "true" : "false" ); | ||
std::string format = "-%levshort- %datetime{%H:%m:%s.%g} %msg (%fbase:%line [%thread])"; | ||
if( ! nested.empty() ) | ||
format = '[' + nested + "] " + format; | ||
configs->setGlobally( el::ConfigurationType::Format, format ); | ||
logger->reconfigure(); | ||
}, | ||
py::arg( "enable" ), | ||
py::arg( "nested-string" ) = "" ); | ||
|
||
m.def( "split", &utilities::string::split ); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What will happen here?
I think it will build it again during the build process
Why do we need to build it again?
why not just
pybind11_add_module
as before?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as discussed