Skip to content

Commit 22b9a53

Browse files
committed
Add SIMDJSON_FORCE_IMPLEMENTATION
1 parent 3be81e3 commit 22b9a53

File tree

7 files changed

+83
-9
lines changed

7 files changed

+83
-9
lines changed

.circleci/config.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ commands:
8585
steps:
8686
- checkout
8787
- run: make
88+
- run: ./json2json -h # Print out the implementation we're using on this hardware
8889
- run: make amalgamate
8990
- run: make test
9091
- run: make checkperf
@@ -96,6 +97,7 @@ commands:
9697
- checkout
9798
- run: cmake $CMAKE_FLAGS $CMAKE_IMPLEMENTATION_FLAGS
9899
- run: make all
100+
- run: tools/json2json -h # Print out the implementation we're using on this hardware
99101
- run: ctest --output-on-failure
100102

101103
cmake_test: # this version builds, install, test and then verify from the installation
@@ -105,6 +107,7 @@ commands:
105107
- checkout
106108
- run: cmake $CMAKE_FLAGS $CMAKE_IMPLEMENTATION_FLAGS -DCMAKE_INSTALL_PREFIX:PATH=destination
107109
- run: make all install
110+
- run: tools/json2json -h # Print out the implementation we're using on this hardware
108111
- run: ctest --output-on-failure
109112
- run: echo -e '#include <simdjson.h>\nint main(int argc,char**argv) {simdjson::dom::parser parser;simdjson::dom::element tweets = parser.load(argv[1]); }' > tmp.cpp && c++ -Idestination/include -Ldestination/lib -std=c++17 -Wl,-rpath,destination/lib -o linkandrun tmp.cpp -lsimdjson && ./linkandrun jsonexamples/twitter.json # we not only want cmake to build and run tests, but we want also a succesful installation from which we can build, link and run programs
110113

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ objs
130130
/singleheader/demo
131131
/tests/allparserscheckfile
132132
/tests/basictests
133+
/tests/checkimplementation
133134
/tests/errortests
134135
/tests/extracting_values_example
135136
/tests/integer_tests

CMakeLists.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,12 @@ export_private_library(simdjson-flags)
122122

123123
#
124124
# Create the top level simdjson library (must be done at this level to use both src/ and include/
125-
# directories)
125+
# directories) and tools
126126
#
127127
add_subdirectory(include)
128128
add_subdirectory(src)
129+
add_subdirectory(windows)
130+
add_subdirectory(tools)
129131

130132
#
131133
# Compile tools / tests / benchmarks
@@ -135,12 +137,11 @@ enable_testing()
135137
add_library(test-data INTERFACE)
136138
target_compile_definitions(test-data INTERFACE SIMDJSON_TEST_DATA_DIR="${CMAKE_CURRENT_SOURCE_DIR}/jsonchecker/")
137139
target_compile_definitions(test-data INTERFACE SIMDJSON_BENCHMARK_DATA_DIR="${CMAKE_CURRENT_SOURCE_DIR}/jsonexamples/")
140+
set(EXAMPLE_JSON ${CMAKE_CURRENT_SOURCE_DIR}/jsonexamples/twitter.json)
138141

139-
add_subdirectory(windows)
140142
add_subdirectory(dependencies)
141143
add_subdirectory(tests)
142144
add_subdirectory(examples)
143-
add_subdirectory(tools)
144145
add_subdirectory(benchmark)
145146

146147
# for fuzzing, read the comments in the fuzz/CMakeLists.txt file

src/implementation.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,15 @@ const implementation *available_implementation_list::detect_best_supported() con
122122
}
123123

124124
const implementation *detect_best_supported_implementation_on_first_use::set_best() const noexcept {
125+
char *force_implementation_name = getenv("SIMDJSON_FORCE_IMPLEMENTATION");
126+
if (force_implementation_name) {
127+
auto force_implementation = available_implementations[force_implementation_name];
128+
if (!force_implementation) {
129+
fprintf(stderr, "SIMDJSON_FORCE_IMPLEMENTATION environment variable set to '%s', which is not a supported implementation name!\n", force_implementation_name);
130+
abort();
131+
}
132+
return active_implementation = force_implementation;
133+
}
125134
return active_implementation = available_implementations.detect_best_supported();
126135
}
127136

tests/CMakeLists.txt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ endif()
4343
# All remaining tests link with simdjson proper
4444
link_libraries(simdjson)
4545
add_cpp_test(basictests basictests.cpp quicktests)
46+
add_cpp_test(checkimplementation checkimplementation.cpp quicktests)
4647
add_cpp_test(errortests errortests.cpp quicktests)
4748
add_cpp_test(integer_tests integer_tests.cpp quicktests)
4849
add_cpp_test(jsoncheck jsoncheck.cpp quicktests)
@@ -76,6 +77,35 @@ if (NOT MSVC) # Can't run .sh on windows
7677
endif()
7778
endif()
7879

80+
if (NOT MSVC)
81+
#
82+
# json2json tool tests
83+
#
84+
85+
add_test(NAME json2json COMMAND $<TARGET_FILE:json2json> ${EXAMPLE_JSON})
86+
87+
#
88+
# SIMDJSON_FORCE_IMPLEMENTATION tests
89+
#
90+
if (SIMDJSON_IMPLEMENTATION_FALLBACK)
91+
add_test(
92+
NAME simdjson_force_implementation_fallback
93+
COMMAND
94+
${CMAKE_COMMAND} -E env
95+
SIMDJSON_FORCE_IMPLEMENTATION=fallback
96+
$<TARGET_FILE:json2json> ${EXAMPLE_JSON}
97+
)
98+
endif()
99+
add_test(
100+
NAME simdjson_force_implementation_error
101+
COMMAND
102+
${CMAKE_COMMAND} -E env
103+
SIMDJSON_FORCE_IMPLEMENTATION=doesnotexist
104+
$<TARGET_FILE:json2json> ${EXAMPLE_JSON}
105+
)
106+
set_tests_properties(simdjson_force_implementation_error PROPERTIES WILL_FAIL TRUE)
107+
endif()
108+
79109
#
80110
# Compile-only tests with simdjson flags on
81111
#

tests/checkimplementation.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include "simdjson.h"
2+
#include <iostream>
3+
#include <cstring>
4+
5+
int main(int argc, const char *argv[]) {
6+
std::cout << "simdjson v" << STRINGIFY(SIMDJSON_VERSION) << " is running the " << simdjson::active_implementation->name() << " implementation." << std::endl;
7+
const char *expected_implementation = nullptr;
8+
if (argc > 1) {
9+
expected_implementation = argv[1];
10+
} else {
11+
expected_implementation = getenv("SIMDJSON_FORCE_IMPLEMENTATION");
12+
if (!expected_implementation) {
13+
std::cout << "No expected implementation argument and SIMDJSON_FORCE_IMPLEMENTATION is not set, success by default!" << std::endl;
14+
return EXIT_SUCCESS;
15+
}
16+
std::cout << "No expected implementation argument, but SIMDJSON_FORCE_IMPLEMENTATION is set to " << expected_implementation << ", so we'll check for that." << std::endl;
17+
}
18+
if (strcmp(expected_implementation, simdjson::active_implementation->name().c_str())) {
19+
std::cerr << "Wrong implementation! Expected " << expected_implementation << "." << std::endl;
20+
return EXIT_FAILURE;
21+
}
22+
return EXIT_SUCCESS;
23+
}

tools/json2json.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,27 @@
44
#endif
55
#include "simdjson.h"
66

7+
void usage(const char *exe) {
8+
std::cerr << exe << " v" << STRINGIFY(SIMDJSON_VERSION) << " (" << simdjson::active_implementation->name() << ")" << std::endl;
9+
std::cerr << std::endl;
10+
std::cerr << "Reads json in, out the result of the parsing. " << std::endl;
11+
std::cerr << "Usage: " << exe << " <jsonfile>" << std::endl;
12+
std::cerr << "The -d flag dumps the raw content of the tape." << std::endl;
13+
}
714
int main(int argc, char *argv[]) {
815
bool rawdump = false;
916

1017
#ifndef _MSC_VER
1118
int c;
1219

13-
while ((c = getopt(argc, argv, "d")) != -1) {
20+
while ((c = getopt(argc, argv, "dh")) != -1) {
1421
switch (c) {
1522
case 'd':
1623
rawdump = true;
1724
break;
25+
case 'h':
26+
usage(argv[0]);
27+
return EXIT_SUCCESS;
1828
default:
1929
abort();
2030
}
@@ -23,11 +33,8 @@ int main(int argc, char *argv[]) {
2333
int optind = 1;
2434
#endif
2535
if (optind >= argc) {
26-
std::cerr << "Reads json in, out the result of the parsing. " << std::endl;
27-
std::cerr << "Usage: " << argv[0] << " <jsonfile>" << std::endl;
28-
std::cerr << "The -d flag dumps the raw content of the tape." << std::endl;
29-
30-
exit(1);
36+
usage(argv[0]);
37+
return EXIT_FAILURE;
3138
}
3239
const char *filename = argv[optind];
3340
if (optind + 1 < argc) {

0 commit comments

Comments
 (0)