From ec1c96baf44b18b9262ffdb4fb3581ddc96e2adb Mon Sep 17 00:00:00 2001 From: Juan Manzanero Date: Tue, 23 Jul 2024 16:22:02 +0200 Subject: [PATCH] [JMT] Add cmake files for nlohmann json --- CMakeLists.txt | 4 ++-- cmake/Findnlohmann_json.cmake | 16 +++++++++++++ cmake/get-third-party.cmake | 9 ++++++++ cmake/third-party/CMakeLists.txt | 2 ++ cmake/third-party/nlohmann_json.cmake | 23 +++++++++++++++++++ .../small_dense_inverse_matrices_test.cpp | 14 +++++------ 6 files changed, 59 insertions(+), 9 deletions(-) create mode 100644 cmake/Findnlohmann_json.cmake create mode 100644 cmake/third-party/nlohmann_json.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index f07f1dc..3223885 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -68,7 +68,7 @@ if(WITH_HSL) target_compile_definitions(lion INTERFACE WITH_HSL) endif() -target_link_libraries(lion INTERFACE ipopt blaslapack tinyxml2 fortran_libraries cppad hdf5 ZLIB::ZLIB SZIP::SZIP) +target_link_libraries(lion INTERFACE ipopt blaslapack tinyxml2 nlohmann_json fortran_libraries cppad hdf5 ZLIB::ZLIB SZIP::SZIP) # Create a lion header only add_library(header_only INTERFACE) @@ -113,7 +113,7 @@ install(FILES ${version_file} ${config_file} DESTINATION ${cmake_files_install_dir}) # Export the targets -install(TARGETS lion ipopt blaslapack tinyxml2 header_only fortran_libraries cppad hdf5 +install(TARGETS lion ipopt blaslapack tinyxml2 nlohmann_json header_only fortran_libraries cppad hdf5 EXPORT ${targets_export_name} RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" diff --git a/cmake/Findnlohmann_json.cmake b/cmake/Findnlohmann_json.cmake new file mode 100644 index 0000000..7481eab --- /dev/null +++ b/cmake/Findnlohmann_json.cmake @@ -0,0 +1,16 @@ +if (NOT nlohmann_json_FOUND) + find_path(NLOHMANNJSON_INCLUDE_DIR nlohmann/json.hpp PATHS ${CMAKE_BINARY_DIR}/lion/thirdparty/include NO_DEFAULT_PATH) + + if (NLOHMANNJSON_INCLUDE_DIR) + set(nlohmann_json_FOUND YES) + + add_library(nlohmann_json INTERFACE) + + target_include_directories(nlohmann_json SYSTEM INTERFACE + "$" + "$/${CMAKE_INSTALL_INCLUDEDIR}>") + + else() + set(nlohmann_json_FOUND NO) + endif() +endif() diff --git a/cmake/get-third-party.cmake b/cmake/get-third-party.cmake index aaf7943..ecdd18b 100644 --- a/cmake/get-third-party.cmake +++ b/cmake/get-third-party.cmake @@ -24,6 +24,14 @@ if (NOT ${tinyxml2_FOUND}) set(BUILD_TINYXML YES) endif() +# nlohmann-json +find_package(nlohmann_json) + +if (NOT ${nlohmann_json_FOUND}) + set(BUILD_NLOHMANNJSON YES) +endif() + + # Ipopt + dependencies find_package(ipopt) @@ -78,6 +86,7 @@ endif() find_package(blaslapack REQUIRED) find_package(tinyxml2 REQUIRED) +find_package(nlohmann_json REQUIRED) find_package(ipopt REQUIRED) find_package(loggercpp REQUIRED) find_package(cppad REQUIRED) diff --git a/cmake/third-party/CMakeLists.txt b/cmake/third-party/CMakeLists.txt index 3d84a22..36ad1f1 100644 --- a/cmake/third-party/CMakeLists.txt +++ b/cmake/third-party/CMakeLists.txt @@ -9,6 +9,7 @@ set(PATCH_DIR ${CMAKE_SOURCE_DIR}/cmake/patch) set(BUILD_LAPACK ${BUILD_LAPACK}) set(BUILD_GTEST ${BUILD_GTEST}) set(BUILD_TINYXML ${BUILD_TINYXML}) +set(BUILD_NLOHMANNJSON ${BUILD_NLOHMANNJSON}) set(BUILD_IPOPT ${BUILD_IPOPT}) set(BUILD_LOGGERCPP ${BUILD_LOGGERCPP}) set(BUILD_CPPAD ${BUILD_CPPAD}) @@ -24,6 +25,7 @@ set(CMAKE_FORTRAN_COMPILER ${CMAKE_FORTRAN_COMPILER}) include(third-party/lapack) include(third-party/gtest) include(third-party/tinyxml) +include(third-party/nlohmann_json) include(third-party/ipopt) include(third-party/loggercpp) include(third-party/cppad) diff --git a/cmake/third-party/nlohmann_json.cmake b/cmake/third-party/nlohmann_json.cmake new file mode 100644 index 0000000..e61db53 --- /dev/null +++ b/cmake/third-party/nlohmann_json.cmake @@ -0,0 +1,23 @@ +if (${BUILD_NLOHMANNJSON}) + +include (ExternalProject) +ExternalProject_Add(nlohmann_json + GIT_REPOSITORY https://github.com/nlohmann/json.git + GIT_TAG master + PREFIX "${THIRD_PARTY_DIR}/nlohmann_json" + SOURCE_DIR "${THIRD_PARTY_DIR}/nlohmann_json/source" + BINARY_DIR "${THIRD_PARTY_DIR}/nlohmann_json/build" + INSTALL_DIR "${THIRD_PARTY_DIR}/lion/thirdparty" + + CONFIGURE_COMMAND ${CMAKE_COMMAND} + -DJSON_BuildTests=OFF + -DBUILD_TESTING=Off + -DCMAKE_CXX_COMPILER:FILEPATH=${CMAKE_CXX_COMPILER} + -DCMAKE_INSTALL_PREFIX:PATH=${THIRD_PARTY_DIR}/lion/thirdparty + -DCMAKE_BUILD_TYPE:STRING=Release + -DCMAKE_CXX_STANDARD=11 + -DCMAKE_CXX_FLAGS=${windows_flag} + ${THIRD_PARTY_DIR}/nlohmann_json/source +) + +endif() diff --git a/test/math/small_dense_inverse_matrices_test.cpp b/test/math/small_dense_inverse_matrices_test.cpp index 7f1ffdd..78d0d0c 100644 --- a/test/math/small_dense_inverse_matrices_test.cpp +++ b/test/math/small_dense_inverse_matrices_test.cpp @@ -15,12 +15,12 @@ static Xml_document small_dense_inverse_matrices_test_reference_file("data/small template -void test_matrices(Document_element &reference_xml_element, DetAndInverseMatrixFun &&det_and_inverse_matrix_fun) +void test_matrices(Document_element_ptr reference_xml_element, DetAndInverseMatrixFun &&det_and_inverse_matrix_fun) { constexpr auto reltolnear = 1e-12; constexpr auto abstolnear = 1e-10; - for (auto &test : reference_xml_element.get_children()) { + for (auto &test : reference_xml_element->get_children()) { const auto reference_matrix = test->get_child("matrix_colmaj")->get_value(std::vector{}); const auto reference_det = test->get_child("det")->get_value(double{}); const auto reference_inverse_matrix = test->get_child("inverse_matrix_colmaj")->get_value(std::vector{}); @@ -41,7 +41,7 @@ void test_matrices(Document_element &reference_xml_element, DetAndInverseMatrixF TEST(small_dense_inverse_matrices, inv2x2) { - test_matrices(*small_dense_inverse_matrices_test_reference_file.get_root_element().get_child("inv2x2"), + test_matrices(small_dense_inverse_matrices_test_reference_file.get_root_element().get_child("inv2x2"), [](const auto &mat) { EXPECT_EQ(mat.size(), 4u); @@ -52,7 +52,7 @@ TEST(small_dense_inverse_matrices, inv2x2) TEST(small_dense_inverse_matrices, inv3x3) { - test_matrices(*small_dense_inverse_matrices_test_reference_file.get_root_element().get_child("inv3x3"), + test_matrices(small_dense_inverse_matrices_test_reference_file.get_root_element().get_child("inv3x3"), [](const auto &mat) { EXPECT_EQ(mat.size(), 9u); @@ -63,7 +63,7 @@ TEST(small_dense_inverse_matrices, inv3x3) TEST(small_dense_inverse_matrices, inv4x4) { - test_matrices(*small_dense_inverse_matrices_test_reference_file.get_root_element().get_child("inv4x4"), + test_matrices(small_dense_inverse_matrices_test_reference_file.get_root_element().get_child("inv4x4"), [](const auto &mat) { EXPECT_EQ(mat.size(), 16u); @@ -74,7 +74,7 @@ TEST(small_dense_inverse_matrices, inv4x4) TEST(small_dense_inverse_matrices, inv5x5) { - test_matrices(*small_dense_inverse_matrices_test_reference_file.get_root_element().get_child("inv5x5"), + test_matrices(small_dense_inverse_matrices_test_reference_file.get_root_element().get_child("inv5x5"), [](const auto &mat) { EXPECT_EQ(mat.size(), 25u); @@ -85,7 +85,7 @@ TEST(small_dense_inverse_matrices, inv5x5) TEST(small_dense_inverse_matrices, inv6x6) { - test_matrices(*small_dense_inverse_matrices_test_reference_file.get_root_element().get_child("inv6x6"), + test_matrices(small_dense_inverse_matrices_test_reference_file.get_root_element().get_child("inv6x6"), [](const auto &mat) { EXPECT_EQ(mat.size(), 36u);