Skip to content

Commit 934abbb

Browse files
committed
Feat: Add pixi build and refactor thirdparties
1 parent c93257e commit 934abbb

File tree

15 files changed

+238
-137
lines changed

15 files changed

+238
-137
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@
3535
.vscode/
3636
.vscode_backup/
3737
.devcontainer.json
38+
pixi.lock
3839

3940
.cache/
4041
build*/
4142
tmp/
43+
Testing/
4244

4345
# Python
4446
__pycache__

CMakeLists.txt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,23 @@ project(tinyopt VERSION ${TINYOPT_VERSION}
1010

1111
# Options
1212
include(Options)
13+
include(Configuration)
1314

1415
# Compiler flags
1516
include(CompilerFlags)
1617

18+
# Header-only library
19+
add_library(tinyopt INTERFACE)
20+
1721
# 3rd parties
1822
include(ThirdParties)
1923

20-
# Header-only library
21-
add_library(tinyopt INTERFACE)
22-
target_include_directories(tinyopt INTERFACE include ${THIRDPARTY_INCLUDE_DIRS})
23-
set_property(TARGET tinyopt PROPERTY INTERFACE_LINK_LIBRARIES ${THIRDPARTY_LIBS})
24+
# Configure
25+
configure_tinyopt_features(tinyopt)
26+
27+
target_include_directories(tinyopt INTERFACE
28+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
29+
$<INSTALL_INTERFACE:include>)
2430

2531
# Tests
2632
if (TINYOPT_BUILD_TESTS)

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ sudo make install
5353

5454
Header files will be copied to `/usr/local/include`.
5555

56+
Or use [pixi](https://pixi.prefix.dev/latest/):
57+
```shell
58+
pixi run build
59+
```
60+
61+
See 'pixi.toml' for a list of available tasks.
62+
5663
# Usage 👨🏻‍💻
5764

5865
## Tinyopt: The Easy Way 😎

benchmarks/CMakeLists.txt

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,14 @@
22
include(Benchmarking)
33

44
add_executable(tinyopt_bench_tinyopt dense.cpp densef.cpp sparse.cpp)
5-
target_link_libraries(tinyopt_bench_tinyopt PRIVATE ${THIRDPARTY_TEST_LIBS} tinyopt)
65
add_benchmark_target(tinyopt_bench_tinyopt)
76

87
# Sparse only
98
add_executable(tinyopt_bench_sparse sparse.cpp)
10-
target_link_libraries(tinyopt_bench_sparse PRIVATE ${THIRDPARTY_TEST_LIBS} tinyopt)
119
add_benchmark_target(tinyopt_bench_sparse)
1210

1311
# Dense only
1412
add_executable(tinyopt_bench_dense dense.cpp)
15-
target_link_libraries(tinyopt_bench_dense PRIVATE ${THIRDPARTY_TEST_LIBS} tinyopt)
1613
add_benchmark_target(tinyopt_bench_dense)
1714

1815

@@ -21,12 +18,19 @@ set(BENCHMARK_EXECUTABLES tinyopt_bench_tinyopt)
2118

2219
if (TINYOPT_BUILD_CERES)
2320
add_executable(tinyopt_bench_ceres ceres.cpp)
24-
target_link_libraries(tinyopt_bench_ceres PRIVATE ${THIRDPARTY_TEST_LIBS} tinyopt)
2521
add_benchmark_target(tinyopt_bench_ceres)
26-
set(BENCHMARK_EXECUTABLES ${BENCHMARK_EXECUTABLES} tinyopt_bench_ceres)
22+
list(APPEND BENCHMARK_EXECUTABLES tinyopt_bench_ceres)
2723
endif()
2824

29-
30-
# Make bench
31-
#TODO add_benchmark_all_targets(bench "${BENCHMARK_EXECUTABLES}")
32-
#TODO export to a benchmarks.json file
25+
# Create a custom target to run all benchmarks
26+
set(RUN_COMMANDS "")
27+
foreach(tgt IN LISTS BENCHMARK_EXECUTABLES)
28+
# Append the word 'COMMAND' followed by the executable path
29+
list(APPEND RUN_COMMANDS COMMAND "$<TARGET_FILE:${tgt}>")
30+
endforeach()
31+
32+
add_custom_target(run_all_benchmarks
33+
${RUN_COMMANDS}
34+
COMMENT "Executing all benchmarks..."
35+
VERBATIM
36+
)

cmake/Benchmarking.cmake

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,12 @@
22

33
# Define a custom target to run benchmarks for a specific executable
44
function(add_benchmark_target executable_name)
5+
target_link_libraries(${executable_name} PRIVATE tinyopt Catch2::Catch2WithMain)
6+
set_target_properties(${executable_name} PROPERTIES LABELS "Benchmark Tinyopt")
57
add_custom_target(run_${executable_name}
68
COMMAND $<TARGET_FILE:${executable_name}> "[benchmark]" --benchmark-no-analysis
79
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
810
COMMENT "Running benchmarks in ${executable_name}..."
911
VERBATIM
1012
)
1113
endfunction()
12-
13-
# Create a custom target for each benchmark executables
14-
function(add_benchmark_all_targets target_name executable_list)
15-
add_custom_target(${target_name}
16-
COMMAND
17-
${CMAKE_COMMAND} -E echo "Running benchmarks..."
18-
COMMAND
19-
foreach (exe ${executable_list})
20-
${CMAKE_COMMAND} -E echo "Running: $<TARGET_FILE:${exe}>"
21-
COMMAND
22-
$<TARGET_FILE:${exe}>
23-
endforeach ()
24-
DEPENDS ${executable_list}
25-
COMMENT "Runs all benchmark executables"
26-
)
27-
endfunction()

cmake/Bindings.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ if (TINYOPT_BUILD_BINDING)
77
# Create binding
88
add_definitions(-DPYBIND11_DETAILED_ERROR_MESSAGES)
99
Python_add_library(tinyopt "")
10-
target_link_libraries(tinyopt PUBLIC pybind11::headers ${THIRDPARTY_LIBS})
10+
target_link_libraries(tinyopt PUBLIC pybind11::headers tinyopt)
1111
set_target_properties(tinyopt PROPERTIES
1212
INTERPROCEDURAL_OPTIMIZATION OFF # OFF to speed-up compilation for now
1313
CXX_VISIBILITY_PRESET default

cmake/Configuration.cmake

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
function(configure_tinyopt_features TARGET_NAME)
2+
3+
# 0. Checks
4+
5+
# 1. Definitions
6+
target_compile_definitions(${TARGET_NAME} INTERFACE
7+
HAS_EIGEN # mandatory
8+
$<$<NOT:$<BOOL:${TINYOPT_ENABLE_FORMATTERS}>>:TINYOPT_NO_FORMATTERS=1>
9+
$<$<BOOL:${TINYOPT_DISABLE_AUTODIFF}>:TINYOPT_NO_FORMATTERS=1>
10+
$<$<BOOL:${TINYOPT_DISABLE_NUMDIFF}>:TINYOPT_DISABLE_NUMDIFF=1>
11+
$<$<TARGET_EXISTS:fmt::fmt>:HAS_FMT>
12+
$<$<TARGET_EXISTS:Ceres::ceres>:HAS_CERES>
13+
$<$<TARGET_EXISTS:Sophus::Sophus>:HAS_SOPHUS>
14+
$<$<BOOL:${Sophus_FOUND}>:HAS_SOPHUS>
15+
$<$<BOOL:${LiePlusPlus_INCLUDE_DIR}>:HAS_LIEPLUSPLUS>
16+
$<$<TARGET_EXISTS:Catch2::Catch2WithMain>:CATCH2_MAJOR_VERSION=${CATCH2_MAJOR_VERSION}>
17+
)
18+
19+
# 2. Include Directories
20+
target_include_directories(${TARGET_NAME} INTERFACE
21+
$<$<BOOL:${Sophus_FOUND}>:${Sophus_INCLUDE_DIR}>
22+
$<$<BOOL:${LiePlusPlus_INCLUDE_DIR}>:${LiePlusPlus_INCLUDE_DIR}>
23+
)
24+
25+
# # 3. Linking
26+
target_link_libraries(${TARGET_NAME} INTERFACE
27+
Eigen3::Eigen # mandatory
28+
$<$<TARGET_EXISTS:fmt::fmt>:fmt::fmt>
29+
$<$<TARGET_EXISTS:Ceres::ceres>:Ceres::ceres>
30+
$<$<TARGET_EXISTS:Sophus::Sophus>:Sophus::Sophus>
31+
$<$<TARGET_EXISTS:Catch2::Catch2WithMain>:Catch2::Catch2WithMain>
32+
)
33+
endfunction()

cmake/Options.cmake

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,3 @@ option(TINYOPT_BUILD_PACKAGES "Build packages" OFF)
2626

2727
# Documentation
2828
option(TINYOPT_BUILD_DOCS "Build documentation" OFF)
29-
30-
31-
# Adding Definitions
32-
if (NOT TINYOPT_ENABLE_FORMATTERS)
33-
add_definitions(-DTINYOPT_NO_FORMATTERS=1)
34-
endif ()
35-
if (TINYOPT_DISABLE_AUTODIFF)
36-
add_definitions(-DTINYOPT_DISABLE_AUTODIFF=1)
37-
endif ()
38-
if (TINYOPT_DISABLE_NUMDIFF)
39-
add_definitions(-DTINYOPT_DISABLE_NUMDIFF=1)
40-
endif ()

cmake/Testing.cmake

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@ enable_testing()
44

55
# Define a custom target to run tests
66
function(add_test_target test_name)
7-
add_test(NAME $<TARGET_FILE:${test_name}> COMMAND ${test_name})
8-
set_property(TEST $<TARGET_FILE:${test_name}> PROPERTY LABELS TESTLABEL Tinyopt)
9-
add_custom_target(run_${test_name}
10-
COMMAND $<TARGET_FILE:${test_name}> "" --benchmark-no-analysis
11-
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
12-
COMMENT "Running tests in ${test_name}..."
13-
VERBATIM
14-
)
7+
target_link_libraries(${test_name} PRIVATE tinyopt Catch2::Catch2WithMain)
8+
9+
add_test(NAME ${test_name} COMMAND ${test_name})
10+
set_property(TEST ${test_name} PROPERTY LABELS Tinyopt Tests)
11+
1512
endfunction()

cmake/ThirdParties.cmake

Lines changed: 50 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
set(THIRDPARTY_LIBS "")
2-
set(THIRDPARTY_INCLUDE_DIRS "")
31

42
include(FetchContent)
53

6-
set(BUILD_TESTING_OLD ${BUILD_TESTING}) # Save your setting
7-
set(BUILD_TESTING OFF CACHE BOOL "" FORCE) # Disbale third party tests
4+
set(BUILD_TESTING_OLD ${BUILD_TESTING}) # Save setting
5+
set(BUILD_TESTING OFF CACHE BOOL "" FORCE) # Disable third party tests
86

97
# For now, Eigen is mandatory
108
find_package(Eigen3 QUIET)
11-
9+
set(EIGEN_BUILD_TESTING OFF)
10+
set(EIGEN_BUILD_DOC OFF)
11+
set(EIGEN_BUILD_PKGCONFIG OFF)
1212
if (EIGEN3_FOUND)
1313
message("Eigen3 found at ${EIGEN3_INCLUDE_DIR}")
1414
else()
@@ -22,48 +22,47 @@ else()
2222
)
2323
block (SCOPE_FOR VARIABLES) # requires cmake 3.25+
2424
set(BUILD_TESTING OFF)
25-
set(EIGEN_BUILD_TESTS OFF)
26-
set(EIGEN_BUILD_DOC OFF)
27-
set(EIGEN_BUILD_DEMOS OFF)
28-
set(EIGEN_BUILD_PKGCONFIG OFF)
2925
set(EIGEN_TEST_CXX11 OFF)
26+
set(EIGEN_HAS_CXX11_MATH ON)
3027
FetchContent_MakeAvailable(Eigen)
3128
endblock ()
3229
set(EIGEN3_INCLUDE_DIR "${eigen3_SOURCE_DIR}" CACHE PATH "Eigen3 include directory" FORCE)
30+
set(EIGEN3_FOUND TRUE CACHE BOOL "Eigen3 found" FORCE)
31+
endif ()
32+
33+
# Eigen is mandatory
34+
if (NOT TARGET Eigen3::Eigen)
35+
message(FATAL_ERROR "Eigen3 not found")
3336
endif ()
34-
add_definitions(-DHAS_EIGEN)
35-
set(THIRDPARTY_LIBS ${THIRDPARTY_LIBS} Eigen3::Eigen)
3637

38+
39+
# FMT
3740
if (TINYOPT_USE_FMT)
3841
find_package(fmt REQUIRED)
3942
message("fmt found at ${FMT_INCLUDE_DIR}")
40-
set(THIRDPARTY_LIBS ${THIRDPARTY_LIBS} fmt::fmt)
41-
add_definitions(-DHAS_FMT)
4243
endif ()
4344

4445

4546
if (TINYOPT_BUILD_CERES)
4647
find_package(Ceres QUIET)
48+
set(MINIGLOG ON)
4749
if (NOT Ceres_FOUND)
4850
message("Ceres not found, fetching...")
4951
FetchContent_Declare(Ceres
5052
GIT_REPOSITORY https://github.com/ceres-solver/ceres-solver
5153
GIT_TAG 2.2.0
5254
GIT_SHALLOW TRUE
5355
GIT_PROGRESS TRUE)
54-
block (SCOPE_FOR VARIABLES) # requires cmake 3.25+
55-
set(BUILD_TESTING OFF)
56-
set(BUILD_EXAMPLES OFF)
57-
set(BUILD_BENCHMARKS OFF)
58-
set(MINIGLOG ON)
59-
FetchContent_MakeAvailable(Ceres)
60-
endblock ()
61-
set(CERES_LIBRARIES Ceres::ceres)
56+
set(BUILD_TESTING OFF)
57+
set(BUILD_EXAMPLES OFF)
58+
set(BUILD_BENCHMARKS OFF)
59+
set(MINIGLOG ON)
60+
FetchContent_MakeAvailable(Ceres)
6261
target_compile_options(ceres PUBLIC "-Wno-reorder" "-Wno-maybe-uninitialized")
6362
endif ()
64-
set(THIRDPARTY_INCLUDE_DIRS ${THIRDPARTY_INCLUDE_DIRS} ${CERES_INCLUDE_DIRS})
65-
set(THIRDPARTY_LIBS ${THIRDPARTY_LIBS} ${CERES_LIBRARIES})
66-
add_definitions(-DHAS_CERES)
63+
if(NOT TARGET Ceres::ceres)
64+
message(FATAL_ERROR "Ceres target not found")
65+
endif()
6766
endif()
6867

6968

@@ -85,16 +84,19 @@ if (TINYOPT_BUILD_SOPHUS_TEST)
8584
FetchContent_MakeAvailable(Sophus)
8685
endblock ()
8786
endif ()
88-
add_definitions(-DHAS_SOPHUS)
89-
#include_directories(${Sophus_SOURCE_DIR}/sophus)
90-
#add_definitions(-DSOPHUS_USE_BASIC_LOGGING=1)
91-
set(THIRDPARTY_LIBS ${THIRDPARTY_LIBS} Sophus::Sophus)
92-
set(THIRDPARTY_INCLUDE_DIRS ${THIRDPARTY_INCLUDE_DIRS} ${SOPHUS_INCLUDE_DIR})
87+
message("Sophus ${Sophus_FOUND} : found at ${Sophus_INCLUDE_DIR}")
88+
89+
if(TARGET Sophus::Sophus)
90+
elseif(Sophus_FOUND)
91+
else()
92+
message(FATAL_ERROR "Sophus target not found")
93+
endif ()
9394
endif ()
9495

9596

9697
if (TINYOPT_BUILD_LIEPLUSPLUS_TEST)
9798
find_package(LiePlusPlus QUIET)
99+
set(LIEPLUSPLUS_TESTS OFF)
98100
if (NOT LiePlusPlus_FOUND)
99101
message("Lie++ not found, fetching...")
100102
FetchContent_Declare(
@@ -108,40 +110,29 @@ if (TINYOPT_BUILD_LIEPLUSPLUS_TEST)
108110
set(LIEPLUSPLUS_TESTS OFF)
109111
FetchContent_MakeAvailable(LiePlusPlus)
110112
endblock ()
113+
FetchContent_MakeAvailable(LiePlusPlus)
111114
endif ()
112-
add_definitions(-DHAS_LIEPLUSPLUS)
113-
#include_directories(${LiePlusPlus_SOURCE_DIR}/include)
114-
set(THIRDPARTY_LIBS ${THIRDPARTY_LIBS} LiePlusPlus)
115-
set(THIRDPARTY_INCLUDE_DIRS ${THIRDPARTY_INCLUDE_DIRS} ${SOPHUS_INCLUDE_DIR})
115+
message("LiePlusPlus ${LiePlusPlus_FOUND} : found at ${LiePlusPlus_INCLUDE_DIR} target ${LiePlusPlus_TARGET}")
116+
117+
# Conditionally link to LiePlusPlus
118+
if(NOT LiePlusPlus_INCLUDE_DIR)
119+
message(FATAL_ERROR "LiePlusPlus not found")
120+
endif()
116121
endif ()
117122

118123

119124
if (TINYOPT_BUILD_TESTS OR TINYOPT_BUILD_BENCHMARKS)
120-
find_package(Catch2 QUIET)
121-
if (NOT Catch2_FOUND)
122-
include(FetchContent)
123-
message("Catch2 is missing, fetching...")
124-
FetchContent_Declare(
125-
Catch2
126-
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
127-
GIT_TAG devel
128-
GIT_SHALLOW TRUE
129-
GIT_PROGRESS TRUE
130-
OVERRIDE_FIND_PACKAGE
131-
)
132-
FetchContent_MakeAvailable(Catch2)
133-
if(NOT Catch2_VERSION)
134-
get_directory_property(Catch2_VERSION DIRECTORY "${catch2_SOURCE_DIR}" DEFINITION PROJECT_VERSION)
135-
endif()
136-
set(Catch2_FOUND TRUE)
137-
endif ()
138-
message(STATUS "Found Catch2 version: ${Catch2_VERSION}")
139-
set(THIRDPARTY_TEST_LIBS ${THIRDPARTY_LIBS} Catch2::Catch2WithMain)
140-
if (${Catch2_VERSION} GREATER_EQUAL 3.0.0)
141-
add_definitions(-DCATCH2_VERSION=3)
142-
else ()
143-
add_definitions(-DCATCH2_VERSION=2)
144-
endif ()
145-
endif()
125+
find_package(Catch2 3 REQUIRED)
126+
# Conditionally link to Catch2
127+
if(TARGET Catch2::Catch2WithMain)
128+
if (${Catch2_VERSION} GREATER_EQUAL 3.0.0)
129+
set(CATCH2_MAJOR_VERSION 3)
130+
else ()
131+
set(CATCH2_MAJOR_VERSION 2)
132+
endif ()
133+
else()
134+
message(FATAL_ERROR "Catch2 target not found")
135+
endif()
146136

137+
endif()
147138
set(BUILD_TESTING ${BUILD_TESTING_OLD} CACHE BOOL "" FORCE) # Restore testing config

0 commit comments

Comments
 (0)