Skip to content

Commit

Permalink
feat: support BUILD_ONLY_BASE
Browse files Browse the repository at this point in the history
  • Loading branch information
GACLove committed Jan 26, 2024
1 parent e38c3b3 commit 947f3ee
Show file tree
Hide file tree
Showing 13 changed files with 114 additions and 82 deletions.
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ include(cmake/version.cmake)

project(${META_PROJECT_NAME} VERSION ${META_VERSION} LANGUAGES C CXX)

option(BUILD_ONLY_BASE "Only build base library" OFF)

if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
set(_DEFAULT_INSTALL_PREFIX ${CMAKE_SOURCE_DIR}/install)
set(CMAKE_INSTALL_PREFIX ${_DEFAULT_INSTALL_PREFIX}
Expand Down Expand Up @@ -97,7 +99,7 @@ cmake_policy(SET CMP0077 NEW) # ENABLE CMP0077: option() honors normal variables
#
# c++11
#
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

Expand Down
56 changes: 34 additions & 22 deletions cmake/dependencies.cmake
Original file line number Diff line number Diff line change
@@ -1,32 +1,43 @@
#
# find external project and embbed to main project
#
# Public Target for target_link_libraries
set(XRPRIMER_PUBLIC_TARGET)

if(NOT IOS)
find_package(Eigen3 REQUIRED CONFIG)
find_package(Ceres REQUIRED CONFIG)
list(APPEND XRPRIMER_PUBLIC_TARGET Eigen3::Eigen)

if(NOT TARGET Ceres::ceres)
add_library(Ceres::ceres INTERFACE IMPORTED)
set_target_properties(
Ceres::ceres PROPERTIES INTERFACE_LINK_LIBRARIES ceres
)
endif()

find_package(OpenCV REQUIRED CONFIG)
find_package(jsoncpp REQUIRED CONFIG)
list(APPEND XRPRIMER_PUBLIC_TARGET jsoncpp_static)

if(OpenCV_FOUND)
add_library(OpenCV::OpenCV INTERFACE IMPORTED GLOBAL)
target_include_directories(
OpenCV::OpenCV INTERFACE ${OpenCV_INCLUDE_DIRS}
)
target_link_libraries(OpenCV::OpenCV INTERFACE ${OpenCV_LIBS})
endif()
find_package(spdlog REQUIRED CONFIG)
list(APPEND XRPRIMER_PUBLIC_TARGET spdlog::spdlog_header_only)

find_package(pybind11 REQUIRED CONFIG)
find_package(jsoncpp REQUIRED CONFIG)
find_package(spdlog REQUIRED CONFIG)
find_package(PnpSolver REQUIRED CONFIG)

if(NOT BUILD_ONLY_BASE)
find_package(Ceres REQUIRED CONFIG)

if(NOT TARGET Ceres::ceres)
add_library(Ceres::ceres INTERFACE IMPORTED)
set_target_properties(
Ceres::ceres PROPERTIES INTERFACE_LINK_LIBRARIES ceres
)
endif()

list(APPEND XRPRIMER_PUBLIC_TARGET Ceres::ceres)

find_package(OpenCV REQUIRED CONFIG)

if(OpenCV_FOUND)
add_library(OpenCV::OpenCV INTERFACE IMPORTED GLOBAL)
target_include_directories(
OpenCV::OpenCV INTERFACE ${OpenCV_INCLUDE_DIRS}
)
target_link_libraries(OpenCV::OpenCV INTERFACE ${OpenCV_LIBS})
endif()

find_package(PnpSolver REQUIRED CONFIG)
list(APPEND XRPRIMER_PUBLIC_TARGET PnpSolver::PnpSolver)
endif()
else()
# 指定opencv_framework_path
if(EXISTS ${xrprimer_framework_path})
Expand All @@ -35,6 +46,7 @@ else()
OpenCV::OpenCV INTERFACE -F${xrprimer_framework_path}
)
target_link_options(OpenCV::OpenCV INTERFACE -framework opencv2)
list(APPEND XRPRIMER_PUBLIC_TARGET OpenCV::OpenCV)
else()
message(
FATAL_ERROR
Expand Down
8 changes: 3 additions & 5 deletions cmake/external/pybind11.cmake
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
if(NOT IOS)
include(ExternalProject)

externalproject_add(
ext_pybind11
PREFIX pybind11
URL https://github.com/pybind/pybind11/archive/refs/tags/v2.6.2.tar.gz
URL_HASH
SHA256=8ff2fff22df038f5cd02cea8af56622bc67f5b64534f1b83b9f133b8366acff2
URL https://github.com/pybind/pybind11/archive/refs/tags/v2.10.4.tar.gz
DOWNLOAD_EXTRACT_TIMESTAMP TRUE
CMAKE_ARGS ${ExternalProject_CMAKE_ARGS_hidden}
-DCMAKE_INSTALL_PREFIX=${STAGED_INSTALL_PREFIX}/pybind11
-DBUILD_TESTING=OFF
-DBUILD_TESTING=OFF -DPYBIND11_USE_STATIC_PYTHON=ON
)
else()
message(STATUS "[XRPrimer] Disable pybind11 on IOS")
Expand Down
13 changes: 10 additions & 3 deletions cmake/setup_deps.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@
#

if(3RT_FROM_LOCAL)
if(POLICY CMP0135)
cmake_policy(SET CMP0135 NEW)
endif()

include(${CMAKE_SOURCE_DIR}/cmake/external/common.cmake)
include(${CMAKE_SOURCE_DIR}/cmake/external/eigen.cmake)
include(${CMAKE_SOURCE_DIR}/cmake/external/ceres.cmake)
include(${CMAKE_SOURCE_DIR}/cmake/external/opencv.cmake)
include(${CMAKE_SOURCE_DIR}/cmake/external/pybind11.cmake)
include(${CMAKE_SOURCE_DIR}/cmake/external/spdlog.cmake)
include(${CMAKE_SOURCE_DIR}/cmake/external/jsoncpp.cmake)
include(${CMAKE_SOURCE_DIR}/cmake/external/pnpsolver.cmake)

if(NOT BUILD_ONLY_BASE)
include(${CMAKE_SOURCE_DIR}/cmake/external/ceres.cmake)
include(${CMAKE_SOURCE_DIR}/cmake/external/opencv.cmake)
include(${CMAKE_SOURCE_DIR}/cmake/external/pnpsolver.cmake)
endif()
endif()

if(3RT_FROM_CONAN)
Expand Down
28 changes: 20 additions & 8 deletions cpp/pybind/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
find_package(Python3 COMPONENTS Interpreter Development)

find_package(pybind11 REQUIRED CONFIG)

if(Python3_FOUND)
set(PYTHON_EXECUTABLE
${Python3_EXECUTABLE}
Expand All @@ -15,16 +17,26 @@ endif()

set(PYTHON_VERSION "${Python3_VERSION_MAJOR}.${Python3_VERSION_MINOR}")

pybind11_add_module(
xrprimer_cpp xrprimer_pybind.cpp data_structure/camera.cpp
data_structure/pose.cpp calibration/calibrator_api.cpp ops/pnpsolver.cpp
common/version.cpp
pybind11_add_module(xrprimer_cpp)
target_sources(
xrprimer_cpp PRIVATE xrprimer_pybind.cpp data_structure/camera.cpp
data_structure/pose.cpp common/version.cpp
)

target_link_libraries(
xrprimer_cpp PUBLIC XRPrimer::xrprimer opencv_imgcodecs opencv_calib3d
opencv_core
)
set(EXTERNAL_MODULES XRPrimer::xrprimer)

if(NOT BUILD_ONLY_BASE)
set(EXTERNAL_MODULES ${EXTERNAL_MODULES} opencv_imgcodecs opencv_calib3d
opencv_core
)
else()
target_compile_definitions(xrprimer_cpp PRIVATE XRPRIMER_BUILD_BASE)
endif()

add_subdirectory(calibration)
add_subdirectory(ops)

target_link_libraries(xrprimer_cpp PUBLIC ${EXTERNAL_MODULES})
target_include_directories(
xrprimer_cpp PUBLIC $<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/cpp/>
$<INSTALL_INTERFACE:include/>
Expand Down
1 change: 1 addition & 0 deletions cpp/pybind/calibration/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target_sources(xrprimer_cpp PRIVATE calibrator_api.cpp)
8 changes: 7 additions & 1 deletion cpp/pybind/calibration/calibrator_api.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@

#include <calibration/calibrator_api.h>
#include <pybind/calibration/calibrator_api.h>

#ifdef XRPRIMER_BUILD_BASE
void xrprimer_pybind_calibrator(py::module &m) {}
#else

#include <calibration/calibrator_api.h>

void pybind_camera_calibrator(py::module &m) {
m.def("CalibrateMultiPinholeCamera", &CalibrateMultiPinholeCamera);
}
Expand All @@ -10,3 +15,4 @@ void xrprimer_pybind_calibrator(py::module &m) {
py::module m_submodule = m.def_submodule("calibrator");
pybind_camera_calibrator(m_submodule);
}
#endif
1 change: 1 addition & 0 deletions cpp/pybind/ops/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target_sources(xrprimer_cpp PRIVATE pnpsolver.cpp)
9 changes: 8 additions & 1 deletion cpp/pybind/ops/pnpsolver.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
#include <pnp_solver.h>

#include <pybind/ops/pnpsolver.h>

#ifdef XRPRIMER_BUILD_BASE
void xrprimer_pybind_pnpsolver(py::module &m) {}
#else

#include <pnp_solver.h>

py::dict prior_guided_pnp(
const Eigen::Ref<Eigen::Matrix<float, 2, Eigen::Dynamic, Eigen::RowMajor>>
points2D,
Expand Down Expand Up @@ -71,3 +77,4 @@ void xrprimer_pybind_pnpsolver(py::module &m) {
py::module m_submodule = m.def_submodule("ops");
pybind_pnpsolver(m_submodule);
}
#endif
2 changes: 1 addition & 1 deletion cpp/pybind/xrprimer_pybind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ PYBIND11_MODULE(xrprimer_cpp, m) {

pybind_eigen_classes(m);
xrprimer_pybind_camera(m);
xrprimer_pybind_calibrator(m);
xrprimer_pybind_version(m);
xrprimer_pybind_pose(m);
xrprimer_pybind_calibrator(m);
xrprimer_pybind_pnpsolver(m);
}
62 changes: 22 additions & 40 deletions cpp/xrprimer/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,60 +1,42 @@
set(Headers
common/version.h
calibration/calibrator.h
calibration/calibrator_api.h
data_structure/math_util.h
data_structure/camera/camera.h
data_structure/camera/pinhole_camera.h
data_structure/camera/omni_camera.h
data_structure/camera/fisheye_camera.h
data_structure/image.h
data_structure/pose.h
utils/logging.h
)
source_group("Headers" FILES ${Headers})

set(Sources
common/version.cpp
calibration/calibrator.cpp
calibration/calibrator_api.cpp
data_structure/camera/camera.cpp
data_structure/camera/pinhole_camera.cpp
data_structure/camera/omni_camera.cpp
data_structure/camera/fisheye_camera.cpp
data_structure/image.cpp
data_structure/pose.cpp
utils/logging.cpp
)
if(NOT BUILD_ONLY_BASE)
add_subdirectory(calibration)
endif()

source_group("Sources" FILES ${Sources})
add_library(xrprimer)
add_library(XRPrimer::xrprimer ALIAS xrprimer)

set(target xrprimer)

add_library(${target} ${Headers} ${Sources})
add_library(XRPrimer::${target} ALIAS ${target})
target_sources(
xrprimer
PRIVATE common/version.cpp
data_structure/image.cpp
data_structure/pose.cpp
data_structure/camera/camera.cpp
data_structure/camera/fisheye_camera.cpp
data_structure/camera/omni_camera.cpp
data_structure/camera/pinhole_camera.cpp
)

target_compile_options(${target} PRIVATE -fPIC)
target_compile_options(xrprimer PRIVATE -fPIC)

include(GenerateExportHeader)
generate_export_header(
${target} EXPORT_FILE_NAME ${CMAKE_CURRENT_BINARY_DIR}/xrprimer_export.h
xrprimer EXPORT_FILE_NAME ${CMAKE_CURRENT_BINARY_DIR}/xrprimer_export.h
)

target_include_directories(
${target}
xrprimer
PUBLIC $<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/cpp/xrprimer>
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/>
$<INSTALL_INTERFACE:include/xrprimer>
)

target_link_libraries(
${target}
PUBLIC $<$<NOT:$<PLATFORM_ID:IOS>>:PnpSolver::pnpsolver> OpenCV::OpenCV
Eigen3::Eigen Ceres::ceres jsoncpp_static spdlog::spdlog_header_only
xrprimer PUBLIC $<$<TARGET_EXISTS:calibrator_obj>:calibrator_obj>
${XRPRIMER_PUBLIC_TARGET}
)

install(TARGETS ${target} DESTINATION lib EXPORT ${target}-targets)
install(EXPORT ${target}-targets DESTINATION lib/cmake/xrprimer
install(TARGETS xrprimer DESTINATION lib EXPORT xrprimer-targets)
install(EXPORT xrprimer-targets DESTINATION lib/cmake/xrprimer
NAMESPACE XRPrimer::
)
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} DESTINATION include FILES_MATCHING
Expand Down
3 changes: 3 additions & 0 deletions cpp/xrprimer/calibration/CMakelists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
add_library(calibrator_obj OBJECT)
target_sources(calibrator_obj PUBLIC calibrator.cpp calibrator_api.cpp)
target_include_directories(calibrator_obj PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../)
1 change: 1 addition & 0 deletions docs/en/installation/cpp.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ It is currently tested on Linux and iOS. Ideally it can be also compiled on macO
```bash
#1. First run, get external dependencies, will install external deps to 3rdparty
cmake -S. -Bbuild_deps <-D3RT_FROM_LOCAL=ON/-D3RT_FROM_CONAN=ON>
cmake -S. -Bbuild_deps -D3RT_FROM_LOCAL=ON
cmake --build build_deps
#2. build xrprimer
Expand Down

0 comments on commit 947f3ee

Please sign in to comment.