Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
cmake_minimum_required (VERSION 3.25)
cmake_minimum_required (VERSION 3.27)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

include(Version)

# Setup the project
project(tinyopt VERSION ${TINYOPT_VERSION}
DESCRIPTION "Tinyopt, a lightweight, header only optimization library"
LANGUAGES CXX)
Expand Down
33 changes: 21 additions & 12 deletions cmake/CompilerFlags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,40 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # generate compile_commands.json for clang
# Check the C++ compiler, define flags
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
message(STATUS "Using GCC C++ compiler")
set(COMPILER_FLAGS -fPIC -Wall -Wextra -Werror -fdiagnostics-color=always ) # -Wconversion
add_compile_options($<$<COMPILE_LANGUAGE:C,CXX>:-fPIC>)
add_compile_options($<$<COMPILE_LANGUAGE:C,CXX>:-Wall>)
add_compile_options($<$<COMPILE_LANGUAGE:C,CXX>:-Wextra>)
add_compile_options($<$<COMPILE_LANGUAGE:C,CXX>:-Werror>)
add_compile_options($<$<COMPILE_LANGUAGE:C,CXX>:-fdiagnostics-color=always>)
# TODO -Wconversion
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
message(STATUS "Using Clang C++ compiler")
set(COMPILER_FLAGS -fPIC -Wall -Wextra -Werror -fcolor-diagnostics)
add_compile_options($<$<COMPILE_LANGUAGE:C,CXX>:-fPIC>)
add_compile_options($<$<COMPILE_LANGUAGE:C,CXX>:-Wall>)
add_compile_options($<$<COMPILE_LANGUAGE:C,CXX>:-Wextra>)
add_compile_options($<$<COMPILE_LANGUAGE:C,CXX>:-Werror>)
add_compile_options($<$<COMPILE_LANGUAGE:C,CXX>:-fcolor-diagnostics>)
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
message(STATUS "Using MSVC C++ compiler")
set(COMPILER_FLAGS /W3 /wd5054) # 5054 is for Eigen, TODO:/WX ?
add_compile_options($<$<COMPILE_LANGUAGE:C,CXX>:/W3>)
add_compile_options($<$<COMPILE_LANGUAGE:C,CXX>:/wd5054>) # 5054 is for Eigen
# TODO /WX
else()
message(STATUS "Unknown C++ compiler: ${CMAKE_CXX_COMPILER_ID}")
endif()
# Add the compiler flags
add_compile_options(${COMPILER_FLAGS})

# ASAN with ConfigType Address Sanitizer (use -DCMAKE_BUILD_TYPE=ASAN)
list(APPEND CMAKE_CONFIGURATION_TYPES ASAN)

if(CMAKE_BUILD_TYPE MATCHES "ASAN")
add_compile_options("-fsanitize=address")
add_compile_options("-fsanitize-address-use-after-scope")
add_compile_options("-fno-optimize-sibling-calls")
add_compile_options("-fno-omit-frame-pointer")
add_compile_options("-O1")
add_compile_options($<$<COMPILE_LANGUAGE:C,CXX>:-fsanitize=address>)
add_compile_options($<$<COMPILE_LANGUAGE:C,CXX>:-fsanitize-address-use-after-scope>)
add_compile_options($<$<COMPILE_LANGUAGE:C,CXX>:-fno-optimize-sibling-calls>)
add_compile_options($<$<COMPILE_LANGUAGE:C,CXX>:-fno-omit-frame-pointer>)
add_compile_options($<$<COMPILE_LANGUAGE:C,CXX>:-O1>)
link_libraries("-fsanitize=address")
elseif(CMAKE_BUILD_TYPE MATCHES "RelWithDebInfo")
add_compile_options("-O2")
add_compile_options($<$<COMPILE_LANGUAGE:C,CXX>:-O2>)
elseif(CMAKE_BUILD_TYPE MATCHES "Release")
add_compile_options("-O3")
add_compile_options($<$<COMPILE_LANGUAGE:C,CXX>:-O3>)
endif()
3 changes: 3 additions & 0 deletions cmake/Options.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ option(TINYOPT_USE_FMT "Use fmt formatting" OFF)

option(TINYOPT_ENABLE_FORMATTERS "Enable definion of std::formatter for streamable types, linked to TINYOPT_NO_FORMATTERS" ON)

# 3rd parties
option(TINYOPT_USE_CUDA "Use Cuda solvers" OFF)

# Build Options.
## Disable these to speed-up compilation if not needed
option(TINYOPT_DISABLE_AUTODIFF "Disable Automatic Differentiation in Optimizers" OFF)
Expand Down
14 changes: 14 additions & 0 deletions cmake/ThirdParties.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,21 @@ if (TINYOPT_USE_FMT)
add_definitions(-DHAS_FMT)
endif ()

# CUDA
if (TINYOPT_USE_CUDA)
# Tell CMake to enable the CUDA language on an existing project.
# This is a robust way to add a language conditionally.
enable_language(CUDA)

# Check if the CUDA language was successfully enabled.
#if(NOT CMAKE_CUDA_COMPILER_WORKS)
# message(FATAL_ERROR "Could not find a working CUDA compiler! Set TINYOPT_USE_CUDA=OFF to build without CUDA.")
#endif()

#set(THIRDPARTY_LIBS ${THIRDPARTY_LIBS} CUDA::cudart)
endif ()

# Ceres (for testing)
if (TINYOPT_BUILD_CERES)
find_package(Ceres)
if (NOT Ceres_FOUND)
Expand Down
2 changes: 1 addition & 1 deletion include/tinyopt/formatters.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

#include <tinyopt/traits.h>

#if __cplusplus >= 202002L
#if (__cplusplus >= 202002L) && __has_include(<format>)

template <typename T>
struct TINYOPT_FORMAT_NS::formatter<
Expand Down
2 changes: 1 addition & 1 deletion include/tinyopt/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#define TINYOPT_LOG(...) fmt::print(__VA_ARGS__);
#define TINYOPT_FORMAT_NS fmt

#elif __cplusplus >= 202002L
#elif (__cplusplus >= 202002L) && __has_include(<format>)

#include <format>

Expand Down
Loading