diff --git a/CMakeLists.txt b/CMakeLists.txt index 6049c75..14c9a60 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/cmake/CompilerFlags.cmake b/cmake/CompilerFlags.cmake index 5e4bd54..739dcea 100644 --- a/cmake/CompilerFlags.cmake +++ b/cmake/CompilerFlags.cmake @@ -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($<$:-fPIC>) + add_compile_options($<$:-Wall>) + add_compile_options($<$:-Wextra>) + add_compile_options($<$:-Werror>) + add_compile_options($<$:-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($<$:-fPIC>) + add_compile_options($<$:-Wall>) + add_compile_options($<$:-Wextra>) + add_compile_options($<$:-Werror>) + add_compile_options($<$:-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($<$:/W3>) + add_compile_options($<$:/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($<$:-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>) link_libraries("-fsanitize=address") elseif(CMAKE_BUILD_TYPE MATCHES "RelWithDebInfo") - add_compile_options("-O2") + add_compile_options($<$:-O2>) elseif(CMAKE_BUILD_TYPE MATCHES "Release") - add_compile_options("-O3") + add_compile_options($<$:-O3>) endif() diff --git a/cmake/Options.cmake b/cmake/Options.cmake index 7216ef1..6a92827 100644 --- a/cmake/Options.cmake +++ b/cmake/Options.cmake @@ -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) diff --git a/cmake/ThirdParties.cmake b/cmake/ThirdParties.cmake index 7f747b9..b0f0b97 100644 --- a/cmake/ThirdParties.cmake +++ b/cmake/ThirdParties.cmake @@ -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) diff --git a/include/tinyopt/formatters.h b/include/tinyopt/formatters.h index 60649c0..c08e9f9 100644 --- a/include/tinyopt/formatters.h +++ b/include/tinyopt/formatters.h @@ -23,7 +23,7 @@ #include -#if __cplusplus >= 202002L +#if (__cplusplus >= 202002L) && __has_include() template struct TINYOPT_FORMAT_NS::formatter< diff --git a/include/tinyopt/log.h b/include/tinyopt/log.h index 45e93b5..c0805fb 100644 --- a/include/tinyopt/log.h +++ b/include/tinyopt/log.h @@ -27,7 +27,7 @@ #define TINYOPT_LOG(...) fmt::print(__VA_ARGS__); #define TINYOPT_FORMAT_NS fmt -#elif __cplusplus >= 202002L +#elif (__cplusplus >= 202002L) && __has_include() #include