Skip to content
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

Merged
merged 15 commits into from
Nov 27, 2022
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/buildsCI.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:
cd build
pwd
ls
cmake .. -G "Visual Studio 16 2019" -DPYTHON_EXECUTABLE=${{env.PYTHON_PATH}} -DBUILD_PYTHON_BINDINGS=true -DPYBIND11_PYTHON_VERSION=3.8 -DBUILD_UNIT_TESTS=false -DBUILD_LEGACY_LIVE_TEST=true -DBUILD_EXAMPLES=false -DBUILD_TOOLS=false -DBUILD_WITH_TM2=false -DFORCE_RSUSB_BACKEND=true
cmake .. -G "Visual Studio 16 2019" -DPYTHON_EXECUTABLE=${{env.PYTHON_PATH}} -DBUILD_PYTHON_BINDINGS=true -DBUILD_UNIT_TESTS=false -DBUILD_LEGACY_LIVE_TEST=true -DBUILD_EXAMPLES=false -DBUILD_TOOLS=false -DBUILD_WITH_TM2=false -DFORCE_RSUSB_BACKEND=true

- name: Build
# Build your program with the given configuration
Expand Down Expand Up @@ -273,7 +273,7 @@ jobs:
cd ./scripts
./pr_check.sh
cd ../build
cmake .. -DBUILD_EXAMPLES=true -DBUILD_WITH_TM2=true -DBUILD_SHARED_LIBS=false -DCHECK_FOR_UPDATES=true -DBUILD_PYTHON_BINDINGS=true -DPYBIND11_PYTHON_VERSION=3.5
cmake .. -DBUILD_EXAMPLES=true -DBUILD_WITH_TM2=true -DBUILD_SHARED_LIBS=false -DCHECK_FOR_UPDATES=true -DBUILD_PYTHON_BINDINGS=true -DPYTHON_EXECUTABLE=$(which python3)
cmake --build . --config ${{env.LRS_BUILD_CONFIG}} -- -j4


Expand Down Expand Up @@ -328,7 +328,7 @@ jobs:
run: |
python ./wrappers/nodejs/tools/enums.py -i ./include/librealsense2 -a ./wrappers/nodejs/src -v
cd build
cmake .. -DBUILD_PYTHON_BINDINGS=true -DBUILD_NODEJS_BINDINGS=true -DPYBIND11_PYTHON_VERSION=2.7 -DCHECK_FOR_UPDATES=true
cmake .. -DBUILD_PYTHON_BINDINGS=true -DBUILD_NODEJS_BINDINGS=true -DPYTHON_EXECUTABLE=$(which python3) -DCHECK_FOR_UPDATES=true
cmake --build . --config $LRS_BUILD_CONFIG -- -j4
cd ../wrappers/nodejs/ && npm install && cd -

Expand Down Expand Up @@ -400,4 +400,4 @@ jobs:
cmake --build . --config ${{env.LRS_BUILD_CONFIG}} -- -j4
ls



86 changes: 86 additions & 0 deletions CMake/external_pybind11.cmake
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
Copy link
Collaborator

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?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as discussed

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()
1 change: 1 addition & 0 deletions CMake/global_config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ macro(global_set_flags)

if(BUILD_PYTHON_BINDINGS)
include(libusb_config)
include(CMake/external_pybind11.cmake)
endif()

if(BUILD_NETWORK_DEVICE)
Expand Down
19 changes: 19 additions & 0 deletions CMake/pybind11-download.cmake.in
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 ""
)


8 changes: 8 additions & 0 deletions CMake/unix_config.cmake
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a prefix 'We cannot uncomment because this.."

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A prefix?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  # Note note: this ruins our CI procedures... otherwise it mirrors Windows, simplifies

to

  # Note note: We cannot uncomment because it ruins our CI procedures... otherwise it mirrors Windows, simplifies

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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")
Expand Down
3 changes: 2 additions & 1 deletion CMake/windows_config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ config_crt()
macro(os_set_flags)
set_property(GLOBAL PROPERTY USE_FOLDERS ON)

# Makes VS15 find the DLL when trying to run examples/tests
# Put all the collaterals together, so we can find when trying to run examples/tests
# Note: this puts the outputs under <binary>/<build-type>
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})

Expand Down
1 change: 0 additions & 1 deletion src/source.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#pragma once

#include "core/processing.h"
//#include <utilities/concurrency/concurrency.h>
#include "archive.h"
#include "metadata-parser.h"
#include "frame-archive.h"
Expand Down
19 changes: 0 additions & 19 deletions third-party/pybind11/CMakeLists.txt

This file was deleted.

10 changes: 9 additions & 1 deletion third-party/utilities/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,13 @@ target_sources( ${PROJECT_NAME} PRIVATE ${UTILITIES_SOURCE_FILES} )
#
install( TARGETS ${PROJECT_NAME}
EXPORT realsense2Targets
ARCHIVE DESTINATION lib
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
)


# Python ------------------------------------------------------------------------------------
#
if( BUILD_PYTHON_BINDINGS )
add_subdirectory( py )
endif()

53 changes: 53 additions & 0 deletions third-party/utilities/include/utilities/py/pybind11.h
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 ); } ); \
}
32 changes: 32 additions & 0 deletions third-party/utilities/py/CMakeLists.txt
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}
)
49 changes: 49 additions & 0 deletions third-party/utilities/py/pyrsutils.cpp
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 );
}
4 changes: 1 addition & 3 deletions tools/depth-quality/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ find_package(Threads REQUIRED)

include_directories(${CMAKE_BINARY_DIR})

if(${BUILD_EASYLOGGINGPP})
include("${REPO_ROOT}/third-party/utilities/include/utilities/easylogging/shared-init.cmake")
endif()
include("${REPO_ROOT}/third-party/utilities/include/utilities/easylogging/shared-init.cmake")

include(../../common/CMakeLists.txt)

Expand Down
4 changes: 1 addition & 3 deletions tools/realsense-viewer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ find_package(Threads REQUIRED)

include_directories(${CMAKE_BINARY_DIR})

if(${BUILD_EASYLOGGINGPP})
include("${REPO_ROOT}/third-party/utilities/include/utilities/easylogging/shared-init.cmake")
endif()
include("${REPO_ROOT}/third-party/utilities/include/utilities/easylogging/shared-init.cmake")

include(../../common/CMakeLists.txt)

Expand Down
Loading