Skip to content

Commit

Permalink
[JMT] Add cmake files for nlohmann json
Browse files Browse the repository at this point in the history
  • Loading branch information
Juan Manzanero committed Jul 23, 2024
1 parent d968749 commit ec1c96b
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 9 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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}"
Expand Down
16 changes: 16 additions & 0 deletions cmake/Findnlohmann_json.cmake
Original file line number Diff line number Diff line change
@@ -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
"$<BUILD_INTERFACE:${CMAKE_BINARY_DIR}>"
"$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR}>")

else()
set(nlohmann_json_FOUND NO)
endif()
endif()
9 changes: 9 additions & 0 deletions cmake/get-third-party.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions cmake/third-party/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand All @@ -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)
Expand Down
23 changes: 23 additions & 0 deletions cmake/third-party/nlohmann_json.cmake
Original file line number Diff line number Diff line change
@@ -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()
14 changes: 7 additions & 7 deletions test/math/small_dense_inverse_matrices_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ static Xml_document small_dense_inverse_matrices_test_reference_file("data/small


template<typename DetAndInverseMatrixFun>
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<double>{});
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<double>{});
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit ec1c96b

Please sign in to comment.