From 6287b4c7e9253ab35995760edeadedc249b88dca Mon Sep 17 00:00:00 2001 From: ClausKlein Date: Wed, 4 Sep 2024 23:57:50 +0200 Subject: [PATCH] Add use of compile time option --- tests/myproj/CMakeLists.txt | 16 +++++++++++++--- tests/myproj/include/mylib/lib.hpp | 18 ++++++++++++++---- tests/myproj/src/main/main.cpp | 23 +++++++++++++++-------- 3 files changed, 42 insertions(+), 15 deletions(-) diff --git a/tests/myproj/CMakeLists.txt b/tests/myproj/CMakeLists.txt index 1bfa2849..aad46dfb 100644 --- a/tests/myproj/CMakeLists.txt +++ b/tests/myproj/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.16...3.21) +cmake_minimum_required(VERSION 3.16...3.30) # set a default CXX standard used by the external tools like clang-tidy, cppcheck, etc. # You can later set fine-grained standards for each target using `target_compile_features` @@ -55,7 +55,7 @@ if(APPLE) if(apple_version VERSION_GREATER_EQUAL 13) # workaround for linkage error as described in https://github.com/Homebrew/homebrew-core/issues/145991 # although fixed, this problem still exists in github actions - add_link_options(-Wl,-ld_classic) + #XXX add_link_options(-Wl,-ld_classic) set(ENABLE_INTERPROCEDURAL_OPTIMIZATION "") endif() endif() @@ -71,7 +71,7 @@ project_options( ENABLE_CLANG_TIDY # ENABLE_INCLUDE_WHAT_YOU_USE # ENABLE_GCC_ANALYZER - ENABLE_COVERAGE + #XXX ENABLE_COVERAGE # ENABLE_PCH # PCH_HEADERS # ${PCH_HEADERS} @@ -124,6 +124,13 @@ target_include_system_directories( # Test the fix of semicolon in genex issue target_link_libraries(lib INTERFACE myproj_project_options myproj_project_warnings) target_link_system_libraries(lib INTERFACE fmt::fmt Eigen3::Eigen) +option(LIB_WITH_EIGEN "Enable Eigen" OFF) +if(LIB_WITH_EIGEN) + target_compile_definitions(lib INTERFACE HAS_EIGEN) +endif() + +target_link_libraries(main PRIVATE lib) + # Library add_library(lib2 "./src/mylib2/lib.cpp") set(lib2_INCLUDE_DIR2 "include") @@ -178,3 +185,6 @@ package_project( ) package_project(NAME myproj_main TARGETS main) + +set(CPACK_GENERATOR TGZ) +include(CPack) diff --git a/tests/myproj/include/mylib/lib.hpp b/tests/myproj/include/mylib/lib.hpp index 8d2ed226..d2829adc 100644 --- a/tests/myproj/include/mylib/lib.hpp +++ b/tests/myproj/include/mylib/lib.hpp @@ -1,7 +1,12 @@ #pragma once // test external pac -#include +#ifdef HAS_EIGEN +# include +#else +# include +#endif + #include #include @@ -17,14 +22,19 @@ #include #include -int some_fun() { - fmt::print("Hello from fmt{}", "!"); +int some_fun() +{ + fmt::print("Hello from lib{}\n", "!"); +#ifdef HAS_EIGEN // populate an Eigen vector with the values auto eigen_vec = Eigen::VectorXd::LinSpaced(10, 0, 1); +#else + auto eigen_vec = std::vector() = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; +#endif // print the vector - fmt::print("[{}]", fmt::join(eigen_vec, ", ")); + fmt::print("[{}]\n", fmt::join(eigen_vec, ", ")); return 0; } diff --git a/tests/myproj/src/main/main.cpp b/tests/myproj/src/main/main.cpp index 65c8ad70..ee2ff7f5 100644 --- a/tests/myproj/src/main/main.cpp +++ b/tests/myproj/src/main/main.cpp @@ -1,12 +1,18 @@ +#include "mylib/lib.hpp" + // test external pac -#include #include #include +#ifdef HAS_EIGEN +# include +#endif + // test std libraries #include #include #include +#include // test c libraries #include @@ -15,15 +21,16 @@ #include #include -int main() { - fmt::print("Hello from fmt{}", "!"); +int main() +{ + fmt::print("Hello from main{}\n", "!"); - Eigen::VectorXd eigen_vec = Eigen::Vector3d(1, 2, 3); - fmt::print("[{}]", fmt::join(eigen_vec, ", ")); + auto eigen_vec = std::vector() = {1, 2, 3}; + fmt::print("[{}]\n", fmt::join(eigen_vec, ", ")); -#if !defined(__MINGW32__) && !defined(__MSYS__)// TODO fails +#if defined(HAS_EIGEN) && !defined(__MINGW32__) && !defined(__MSYS__) // TODO fails Eigen::VectorXd eigen_vec2 = Eigen::VectorXd::LinSpaced(10, 0, 1); - fmt::print("[{}]", fmt::join(eigen_vec2, ", ")); + fmt::print("[{}]\n", fmt::join(eigen_vec2, ", ")); #endif // trigger address sanitizer @@ -31,5 +38,5 @@ int main() { // *p = 1; // trigger compiler warnings, clang-tidy, and cppcheck - int a; + int a = some_fun(); }