| 1 |
cmake_minimum_required(VERSION 3.12) |
| 2 |
|
| 3 |
include(FetchContent) |
| 4 |
|
| 5 |
set(CMAKE_CXX_STANDARD 11) |
| 6 |
set(CMAKE_CXX_STANDARD_REQUIRED ON) |
| 7 |
set(CMAKE_CXX_EXTENSIONS ON) |
| 8 |
enable_language(CXX) |
| 9 |
|
| 10 |
# Search for Google benchmark package |
| 11 |
find_package(benchmark QUIET) |
| 12 |
if(NOT benchmark_FOUND) |
| 13 |
# Fetch google benchmark source code from official repository |
| 14 |
set(BENCHMARK_ENABLE_TESTING OFF) |
| 15 |
|
| 16 |
# Allow specifying alternative Google benchmark repository |
| 17 |
if(NOT DEFINED GBENCHMARK_REPOSITORY) |
| 18 |
set(GBENCHMARK_REPOSITORY https://github.com/google/benchmark.git) |
| 19 |
endif() |
| 20 |
if(NOT DEFINED GBENCHMARK_TAG) |
| 21 |
set(GBENCHMARK_TAG v1.7.0) |
| 22 |
endif() |
| 23 |
|
| 24 |
FetchContent_Declare(benchmark |
| 25 |
GIT_REPOSITORY ${GBENCHMARK_REPOSITORY} |
| 26 |
GIT_TAG ${GBENCHMARK_TAG}) |
| 27 |
|
| 28 |
FetchContent_GetProperties(benchmark) |
| 29 |
if(NOT benchmark_POPULATED) |
| 30 |
FetchContent_Populate(benchmark) |
| 31 |
add_subdirectory(${benchmark_SOURCE_DIR} ${benchmark_BINARY_DIR} EXCLUDE_FROM_ALL) |
| 32 |
endif() |
| 33 |
endif() |
| 34 |
|
| 35 |
add_executable(benchmark_zlib |
| 36 |
benchmark_adler32.cc |
| 37 |
benchmark_adler32_copy.cc |
| 38 |
benchmark_compare256.cc |
| 39 |
benchmark_crc32.cc |
| 40 |
benchmark_main.cc |
| 41 |
benchmark_slidehash.cc |
| 42 |
) |
| 43 |
|
| 44 |
target_compile_definitions(benchmark_zlib PRIVATE -DBENCHMARK_STATIC_DEFINE) |
| 45 |
target_include_directories(benchmark_zlib PRIVATE |
| 46 |
${PROJECT_SOURCE_DIR} |
| 47 |
${PROJECT_BINARY_DIR} |
| 48 |
${benchmark_SOURCE_DIR}/benchmark/include) |
| 49 |
|
| 50 |
target_link_libraries(benchmark_zlib zlibstatic benchmark::benchmark) |
| 51 |
if(WIN32) |
| 52 |
target_link_libraries(benchmark_zlib shlwapi) |
| 53 |
endif() |
| 54 |
|
| 55 |
if(ZLIB_ENABLE_TESTS) |
| 56 |
add_test(NAME benchmark_zlib |
| 57 |
COMMAND ${CMAKE_CROSSCOMPILING_EMULATOR} $<TARGET_FILE:benchmark_zlib>) |
| 58 |
endif() |
| 59 |
|
| 60 |
if(WITH_BENCHMARK_APPS) |
| 61 |
option(BUILD_ALT_BENCH "Link against alternative zlib implementation" OFF) |
| 62 |
|
| 63 |
# Search for libpng package |
| 64 |
find_package(PNG QUIET) |
| 65 |
|
| 66 |
if(NOT PNG_FOUND) |
| 67 |
FetchContent_Declare(PNG |
| 68 |
GIT_REPOSITORY https://github.com/glennrp/libpng.git) |
| 69 |
|
| 70 |
FetchContent_GetProperties(PNG) |
| 71 |
if(NOT PNG_POPULATED) |
| 72 |
FetchContent_Populate(PNG) |
| 73 |
add_subdirectory(${PNG_SOURCE_DIR} ${PNG_BINARY_DIR}) |
| 74 |
endif() |
| 75 |
endif() |
| 76 |
|
| 77 |
set(BENCH_APP_SRCS |
| 78 |
benchmark_png_encode.cc |
| 79 |
benchmark_png_decode.cc |
| 80 |
benchmark_main.cc |
| 81 |
) |
| 82 |
|
| 83 |
add_executable(benchmark_zlib_apps ${BENCH_APP_SRCS}) |
| 84 |
|
| 85 |
if(DEFINED BUILD_ALT_BENCH) |
| 86 |
set(ZLIB_ALT_LIB "libz.a" CACHE FILEPATH "Optional alternative zlib implementation (defaults to stock zlib)") |
| 87 |
add_executable(benchmark_zlib_apps_alt ${BENCH_APP_SRCS}) |
| 88 |
target_link_libraries(benchmark_zlib_apps_alt libpng.a ${ZLIB_ALT_LIB} benchmark::benchmark) |
| 89 |
target_compile_definitions(benchmark_zlib_apps_alt PRIVATE BUILD_ALT=1) |
| 90 |
target_include_directories(benchmark_zlib_apps_alt PRIVATE |
| 91 |
${PROJECT_SOURCE_DIR} |
| 92 |
${PROJECT_BINARY_DIR} |
| 93 |
${PNG_INCLUDE_DIR} |
| 94 |
${benchmark_SOURCE_DIR}/benchmark/include) |
| 95 |
endif() |
| 96 |
|
| 97 |
target_include_directories(benchmark_zlib_apps PRIVATE |
| 98 |
${PROJECT_SOURCE_DIR} |
| 99 |
${PROJECT_BINARY_DIR} |
| 100 |
${PNG_INCLUDE_DIR} |
| 101 |
${benchmark_SOURCE_DIR}/benchmark/include) |
| 102 |
|
| 103 |
# We need the static png library if we're statically linking to zlib, |
| 104 |
# otherwise it will resolve these things in the system provided dynamic |
| 105 |
# libraries (likely linked to stock zlib) |
| 106 |
target_link_libraries(benchmark_zlib_apps libpng.a zlibstatic benchmark::benchmark) |
| 107 |
endif() |