Skip to content

Commit 3b24ba9

Browse files
committed
Adding cmake
1 parent 41b916e commit 3b24ba9

File tree

11 files changed

+210
-1
lines changed

11 files changed

+210
-1
lines changed

CMakeLists.txt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
cmake_minimum_required(VERSION 2.8)
2+
3+
4+
set(CMAKE_MACOSX_RPATH OFF)
5+
if (NOT CMAKE_BUILD_TYPE)
6+
message(STATUS "No build type selected, default to Release")
7+
set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build." FORCE)
8+
endif()
9+
10+
project(simdjson)
11+
set(SIMDJSON_LIB_NAME simdjson)
12+
13+
14+
option(SIMDJSON_BUILD_STATIC "Build a static library" OFF) # turning it on disables the production of a dynamic library
15+
option(SIMDJSON_BUILD_LTO "Build library with Link Time Optimization" OFF)
16+
option(SIMDJSON_SANITIZE "Sanitize addresses" OFF)
17+
18+
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/tools/cmake")
19+
20+
find_package(CTargets)
21+
find_package(Options)
22+
find_package(LTO)
23+
24+
install(DIRECTORY include/${SIMDJSON_LIB_NAME} DESTINATION include)
25+
set (TEST_DATA_DIR "${CMAKE_CURRENT_SOURCE_DIR}/jsonchecker/")
26+
set (BENCHMARK_DATA_DIR "${CMAKE_CURRENT_SOURCE_DIR}/jsonexamples/")
27+
add_definitions(-DSIMDJSON_TEST_DATA_DIR="${TEST_DATA_DIR}")
28+
add_definitions(-DSIMDJSON_BENCHMARK_DATA_DIR="${TEST_DATA_DIR}")
29+
enable_testing()
30+
add_subdirectory(src)
31+
add_subdirectory(tools)
32+
add_subdirectory(tests)
33+
add_subdirectory(benchmark)

benchmark/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
target_include_directories(${SIMDJSON_LIB_NAME}
2+
PUBLIC ${PROJECT_SOURCE_DIR}/benchmark
3+
)
4+
5+
target_include_directories(${SIMDJSON_LIB_NAME}
6+
PUBLIC ${PROJECT_SOURCE_DIR}/benchmark/linux
7+
)
8+
9+
add_cpp_benchmark(parse)
10+
add_cpp_benchmark(statisticalmodel)

src/CMakeLists.txt

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
if(${CMAKE_C_COMPILER_ID} MATCHES "Intel") # icc / icpc
2+
# prevent shared libraries from depending on Intel provided libraries
3+
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -static-intel")
4+
endif()
5+
6+
7+
# we default on a shared library.
8+
if(SIMDJSON_BUILD_STATIC)
9+
set(SIMDJSON_LIB_TYPE STATIC)
10+
MESSAGE( STATUS "Building a static library." )
11+
else()
12+
MESSAGE( STATUS "Building a dynamic library (default)." )
13+
set(SIMDJSON_LIB_TYPE SHARED)
14+
endif()
15+
16+
MESSAGE( STATUS "SIMDJSON_LIB_TYPE: " ${SIMDJSON_LIB_TYPE})
17+
set(SIMDJSON_SRC
18+
jsonioutil.cpp
19+
jsonminifier.cpp
20+
jsonparser.cpp
21+
stage1_find_marks.cpp
22+
stage2_flatten.cpp
23+
stage34_unified.cpp)
24+
25+
add_library(${SIMDJSON_LIB_NAME} ${SIMDJSON_LIB_TYPE} ${SIMDJSON_SRC})
26+
target_include_directories(${SIMDJSON_LIB_NAME}
27+
PUBLIC ${PROJECT_SOURCE_DIR}/include
28+
)
29+
30+
install(TARGETS ${SIMDJSON_LIB_NAME} DESTINATION lib)
31+
32+
if(NOT MSVC)
33+
## We output the library at the root of the current directory where cmake is invoked
34+
## This is handy but Visual Studio will happily ignore us
35+
set_target_properties(${SIMDJSON_LIB_NAME} PROPERTIES
36+
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
37+
MESSAGE( STATUS "Library output directory (does not apply to Visual Studio): " ${CMAKE_BINARY_DIR})
38+
endif()
39+
40+
if(MSVC AND (SIMDJSON_LIB_TYPE STREQUAL "SHARED"))
41+
if (CMAKE_VERSION VERSION_LESS 3.4)
42+
MESSAGE( STATUS "To build a Windows DLL using Visual Studio, you may need cmake 3.4 or better." )
43+
endif()
44+
MESSAGE( STATUS "Building a Windows DLL using Visual Studio, exporting all symbols automatically." )
45+
set_target_properties(${SIMDJSON_LIB_NAME}
46+
PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS 1)
47+
endif()

tests/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
add_cpp_test(jsoncheck)
3+
add_test(jsoncheck jsoncheck)

tests/jsoncheck.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ bool contains(const char *pre, const char *str) {
2929

3030
bool validate(const char *dirname) {
3131
bool everythingfine = true;
32-
// init_state_machine(); // no longer necessary
3332
const char *extension = ".json";
3433
size_t dirlen = strlen(dirname);
3534
struct dirent **entry_list;
@@ -117,10 +116,17 @@ int main(int argc, char *argv[]) {
117116
if (argc != 2) {
118117
std::cerr << "Usage: " << argv[0] << " <directorywithjsonfiles>"
119118
<< std::endl;
119+
#ifndef SIMDJSON_TEST_DATA_DIR
120120
std::cout
121121
<< "We are going to assume you mean to use the 'jsonchecker' directory."
122122
<< std::endl;
123123
return validate("jsonchecker/") ? EXIT_SUCCESS : EXIT_FAILURE;
124+
#else
125+
std::cout
126+
<< "We are going to assume you mean to use the '"<< SIMDJSON_TEST_DATA_DIR <<"' directory."
127+
<< std::endl;
128+
return validate(SIMDJSON_TEST_DATA_DIR) ? EXIT_SUCCESS : EXIT_FAILURE;
129+
#endif
124130
}
125131
return validate(argv[1]) ? EXIT_SUCCESS : EXIT_FAILURE;
126132
}

tests/numberparsingcheck.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,19 @@ int main(int argc, char *argv[]) {
173173
if (argc != 2) {
174174
std::cerr << "Usage: " << argv[0] << " <directorywithjsonfiles>"
175175
<< std::endl;
176+
#if defined(SIMDJSON_TEST_DATA_DIR) && defined(SIMDJSON_BENCHMARK_DATA_DIR)
177+
std::cout
178+
<< "We are going to assume you mean to use the '"<< SIMDJSON_TEST_DATA_DIR <<"' and '"<< SIMDJSON_BENCHMARK_DATA_DIR <<"'directories."
179+
<< std::endl;
180+
return validate(SIMDJSON_TEST_DATA_DIR) && validate(SIMDJSON_BENCHMARK_DATA_DIR) ? EXIT_SUCCESS
181+
: EXIT_FAILURE;
182+
#else
176183
std::cout << "We are going to assume you mean to use the 'jsonchecker' and "
177184
"'jsonexamples' directories."
178185
<< std::endl;
179186
return validate("jsonchecker/") && validate("jsonexamples/") ? EXIT_SUCCESS
180187
: EXIT_FAILURE;
188+
#endif
181189
}
182190
return validate(argv[1]) ? EXIT_SUCCESS : EXIT_FAILURE;
183191
}

tests/stringparsingcheck.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <stdbool.h>
77
#include <stdio.h>
88
#include <stdlib.h>
9+
#include <iostream>
910

1011
#ifndef JSON_TEST_STRINGS
1112
#define JSON_TEST_STRINGS
@@ -381,11 +382,19 @@ int main(int argc, char *argv[]) {
381382
if (argc != 2) {
382383
std::cerr << "Usage: " << argv[0] << " <directorywithjsonfiles>"
383384
<< std::endl;
385+
#if defined(SIMDJSON_TEST_DATA_DIR) && defined(SIMDJSON_BENCHMARK_DATA_DIR)
386+
std::cout
387+
<< "We are going to assume you mean to use the '"<< SIMDJSON_TEST_DATA_DIR <<"' and '"<< SIMDJSON_BENCHMARK_DATA_DIR <<"'directories."
388+
<< std::endl;
389+
return validate(SIMDJSON_TEST_DATA_DIR) && validate(SIMDJSON_BENCHMARK_DATA_DIR) ? EXIT_SUCCESS
390+
: EXIT_FAILURE;
391+
#else
384392
std::cout << "We are going to assume you mean to use the 'jsonchecker' and "
385393
"'jsonexamples' directories."
386394
<< std::endl;
387395
return validate("jsonchecker/") && validate("jsonexamples/") ? EXIT_SUCCESS
388396
: EXIT_FAILURE;
397+
#endif
389398
}
390399
return validate(argv[1]) ? EXIT_SUCCESS : EXIT_FAILURE;
391400
}

tools/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
add_cpp_tool(json2json)
2+
add_cpp_tool(jsonstats)
3+
add_cpp_tool(minify)

tools/cmake/FindCTargets.cmake

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
if (CMAKE_VERSION VERSION_GREATER 3.0.0)
2+
cmake_policy(VERSION 3.0.0)
3+
endif ()
4+
5+
6+
function(add_cpp_test TEST_NAME)
7+
add_executable(${TEST_NAME} ${TEST_NAME}.cpp)
8+
target_link_libraries(${TEST_NAME} ${SIMDJSON_LIB_NAME})
9+
add_test(${TEST_NAME} ${TEST_NAME})
10+
endfunction(add_cpp_test)
11+
12+
function(add_cpp_benchmark BENCH_NAME)
13+
add_executable(${BENCH_NAME} ${BENCH_NAME}.cpp)
14+
target_link_libraries(${BENCH_NAME} ${SIMDJSON_LIB_NAME})
15+
endfunction(add_cpp_benchmark)
16+
17+
function(add_cpp_tool TOOL_NAME)
18+
add_executable(${TOOL_NAME} ${TOOL_NAME}.cpp)
19+
target_link_libraries(${TOOL_NAME} ${SIMDJSON_LIB_NAME})
20+
endfunction(add_cpp_tool)

tools/cmake/FindLTO.cmake

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# gcc and clang require a special 'ar' and 'ranlib' to create a
2+
# static libX.a that allows for further inlining, but icc does not.
3+
4+
macro(append var string)
5+
set(${var} "${${var}} ${string}")
6+
endmacro(append)
7+
8+
if(BUILD_LTO)
9+
if ("${CMAKE_C_COMPILER_ID}" MATCHES "Intel") # icc
10+
append(CMAKE_CXX_FLAGS "-ipo")
11+
elseif("${CMAKE_C_COMPILER_ID}" MATCHES "MSVC") # Microsoft
12+
# TODO
13+
elseif ("${CMAKE_C_COMPILER_ID}" MATCHES "Clang") # Clang or AppleClang
14+
append(CMAKE_CXX_FLAGS "-flto")
15+
append(CMAKE_EXE_LINKER_FLAGS "-fuse-ld=gold -flto")
16+
append(CMAKE_SHARED_LINKER_FLAGS "-fuse-ld=gold -flto")
17+
set(_LLVM_VERSION_STRING "3.8") # FIXME: determine version automatically
18+
set(CMAKE_AR "llvm-ar-${_LLVM_VERSION_STRING}")
19+
set(CMAKE_RANLIB "llvm-ranlib-${_LLVM_VERSION_STRING}")
20+
else() # assume GCC compatible syntax if not matched
21+
append(CMAKE_CXX_FLAGS "-flto")
22+
set(CMAKE_AR "gcc-ar")
23+
set(CMAKE_RANLIB "gcc-ranlib")
24+
endif()
25+
endif(BUILD_LTO)

0 commit comments

Comments
 (0)