Skip to content

Commit 6ec4e44

Browse files
authored
Improve CMake build for nixpkgs (#18)
Improvements that make it easier to integrate with nixpkgs while still following CMake conventions. * Allow library to be either static or shared dependent on BUILD_SHARED_LIBS which is the default option * Added option to control whether to use vendored double-conversion * Used GNUInstallDirs for common variables for install
1 parent 3a69398 commit 6ec4e44

1 file changed

Lines changed: 26 additions & 18 deletions

File tree

‎CMakeLists.txt‎

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,32 @@ cmake_minimum_required(VERSION 3.15)
22
project(jtjson)
33

44
option(JSON_CPP_BUILD_TESTS "Enable building tests" ON)
5+
option(DOUBLE_CONVERSION_VENDORED "Use vendored double-conversion library" ON)
56

67
set(CMAKE_CXX_STANDARD 11)
78
set(CMAKE_CXX_STANDARD_REQUIRED ON)
89

9-
# Add double-conversion library
10-
add_library(double-conversion STATIC
11-
double-conversion/bignum.cc
12-
double-conversion/bignum-dtoa.cc
13-
double-conversion/cached-powers.cc
14-
double-conversion/double-to-string.cc
15-
double-conversion/fast-dtoa.cc
16-
double-conversion/fixed-dtoa.cc
17-
double-conversion/string-to-double.cc
18-
double-conversion/strtod.cc
19-
)
20-
target_include_directories(double-conversion PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
10+
if (DOUBLE_CONVERSION_VENDORED)
11+
# Add double-conversion library
12+
add_library(double-conversion STATIC
13+
double-conversion/bignum.cc
14+
double-conversion/bignum-dtoa.cc
15+
double-conversion/cached-powers.cc
16+
double-conversion/double-to-string.cc
17+
double-conversion/fast-dtoa.cc
18+
double-conversion/fixed-dtoa.cc
19+
double-conversion/string-to-double.cc
20+
double-conversion/strtod.cc
21+
)
22+
target_include_directories(double-conversion PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
23+
else()
24+
find_package(double-conversion REQUIRED)
25+
endif()
2126

2227
# Main JSON library
23-
add_library(json STATIC
28+
# BUILD_SHARED_LIBS is a standard CMake variable that
29+
# controls whether libraries are built as shared or static
30+
add_library(json
2431
json.cpp
2532
)
2633
target_link_libraries(json PRIVATE double-conversion)
@@ -52,9 +59,10 @@ enable_testing()
5259
)
5360
endif()
5461

55-
install(TARGETS json double-conversion
56-
ARCHIVE DESTINATION lib
57-
LIBRARY DESTINATION lib
58-
RUNTIME DESTINATION bin
59-
PUBLIC_HEADER DESTINATION include
62+
include(GNUInstallDirs)
63+
install(TARGETS json
64+
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
65+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
66+
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
67+
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
6068
)

0 commit comments

Comments
 (0)