diff --git a/.evergreen/compile-windows.sh b/.evergreen/compile-windows.sh index ec66008b..ac77f64d 100644 --- a/.evergreen/compile-windows.sh +++ b/.evergreen/compile-windows.sh @@ -13,7 +13,7 @@ CC=${CC:-"Visual Studio 14 2015 Win64"} echo "CC: $CC" echo "RELEASE: $RELEASE" -CONFIGURE_FLAGS="-DCMAKE_INSTALL_PREFIX=C:/libbson" +CONFIGURE_FLAGS="-DCMAKE_INSTALL_PREFIX=C:/libbson -DENABLE_EXAMPLES:BOOL=ON" BUILD_FLAGS="/m" # Number of concurrent processes. No value=# of cpus if [ "$RELEASE" ]; then diff --git a/CMakeLists.txt b/CMakeLists.txt index faa57593..8a761d9b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 2.8) project (libbson) option(ENABLE_TESTS "Build libbson tests." ON) +option(ENABLE_EXAMPLES "Build libbson examples." OFF) include(CheckFunctionExists) include(CheckIncludeFile) @@ -271,9 +272,30 @@ if (ENABLE_TESTS) DESTINATION ${PROJECT_BINARY_DIR}/tests) endif () # ENABLE_TESTS +function (add_example bin src) + set (BSON_EXAMPLE_SOURCES ${SOURCE_DIR}/${src}) + add_executable (${bin} ${BSON_EXAMPLE_SOURCES}) + + # Link against the shared lib like normal apps + target_link_libraries(${bin} bson_shared) + + set (EXAMPLES ${EXAMPLES} ${bin}) +endfunction () + +if (ENABLE_EXAMPLES) + add_example (bcon-col-view examples/bcon-col-view.c) + add_example (bcon-speed examples/bcon-speed.c) + add_example (bson-metrics examples/bson-metrics.c) + # Uses getopt () + #add_example (bson-streaming-reader examples/bson-streaming-reader.c) + add_example (bson-to-json examples/bson-to-json.c) + add_example (bson-validate examples/bson-validate.c) + add_example (json-to-bson examples/json-to-bson.c) +endif () # ENABLE_EXAMPLES + set (BSON_HEADER_INSTALL_DIR "include/libbson-${BSON_API_VERSION}") install( - TARGETS bson_shared bson_static + TARGETS bson_shared bson_static ${EXAMPLES} LIBRARY DESTINATION lib ARCHIVE DESTINATION lib RUNTIME DESTINATION bin diff --git a/examples/bson-metrics.c b/examples/bson-metrics.c index 59981974..8772313d 100644 --- a/examples/bson-metrics.c +++ b/examples/bson-metrics.c @@ -271,29 +271,29 @@ main (int argc, char *argv[]) printf (" \"file\": \"%s\",\n", filename); printf (" \"secs\": %.2f,\n", dtime_delta); printf (" \"docs_per_sec\": %" PRIu64 ",\n", - (uint64_t) round (state.doc_count / dtime_delta)); + (uint64_t) floor (state.doc_count / dtime_delta)); printf (" \"docs\": %" PRIu64 ",\n", state.doc_count); printf (" \"elements\": %" PRIu64 ",\n", state.element_count); printf (" \"elements_per_doc\": %" PRIu64 ",\n", - (uint64_t) round ((double) state.element_count / + (uint64_t) floor ((double) state.element_count / (double) BSON_MAX (state.doc_count, 1))); printf (" \"aggregates\": %" PRIu64 ",\n", aggregate_count); printf (" \"aggregates_per_doc\": %" PRIu64 ",\n", - (uint64_t) round ((double) aggregate_count / + (uint64_t) floor ((double) aggregate_count / (double) BSON_MAX (state.doc_count, 1))); printf (" \"degree\": %" PRIu64 ",\n", - (uint64_t) round ( + (uint64_t) floor ( (double) state.element_count / ((double) BSON_MAX (state.doc_count + aggregate_count, 1)))); printf (" \"doc_size_max\": %" PRIu64 ",\n", state.doc_size_max); printf (" \"doc_size_average\": %" PRIu64 ",\n", - (uint64_t) round ((double) bson_reader_tell (reader) / + (uint64_t) floor ((double) bson_reader_tell (reader) / (double) BSON_MAX (state.doc_count, 1))); printf (" \"key_size_average\": %" PRIu64 ",\n", - (uint64_t) round ((double) state.key_size_tally / + (uint64_t) floor ((double) state.key_size_tally / (double) BSON_MAX (state.element_count, 1))); printf (" \"string_size_average\": %" PRIu64 ",\n", - (uint64_t) round ( + (uint64_t) floor ( (double) state.utf8_size_tally / (double) BSON_MAX ( state.bson_type_metrics[BSON_TYPE_UTF8].count, 1))); @@ -302,7 +302,7 @@ main (int argc, char *argv[]) bson_type_metrics_t bson_type_metrics = state.bson_type_metrics[j]; printf (" \"%s\": %" PRIu64 ",\n", bson_type_metrics.description, - (uint64_t) round ((double) bson_type_metrics.count * 100.0 / + (uint64_t) floor ((double) bson_type_metrics.count * 100.0 / (double) BSON_MAX (state.element_count, 1))); } printf (" }\n"); diff --git a/examples/bson-streaming-reader.c b/examples/bson-streaming-reader.c index ea0e9020..50b73c65 100644 --- a/examples/bson-streaming-reader.c +++ b/examples/bson-streaming-reader.c @@ -18,11 +18,11 @@ #include #include #include -#include #ifdef _WIN32 #include #include #else +#include #include #include #include diff --git a/examples/bson-to-json.c b/examples/bson-to-json.c index 0cc4534d..f3b545fd 100644 --- a/examples/bson-to-json.c +++ b/examples/bson-to-json.c @@ -24,6 +24,9 @@ #include #include +#ifndef STDIN_FILENO +#define STDIN_FILENO 0 +#endif int main (int argc, char *argv[]) diff --git a/examples/json-to-bson.c b/examples/json-to-bson.c index 60616f14..c01e64bd 100644 --- a/examples/json-to-bson.c +++ b/examples/json-to-bson.c @@ -26,6 +26,10 @@ #include +#ifndef STDIN_FILENO +#define STDIN_FILENO 0 +#endif + int main (int argc, char *argv[]) {