Evan Nemerson | 93ef13f | 2016-06-20 13:07:35 -0700 | [diff] [blame] | 1 | # Ubuntu 12.04 LTS has CMake 2.8.7, and is an important target since |
| 2 | # several CI services, such as Travis and Drone, use it. Solaris 11 |
| 3 | # has 2.8.6, and it's not difficult to support if you already have to |
| 4 | # support 2.8.7. |
| 5 | cmake_minimum_required(VERSION 2.8.6) |
| 6 | |
| 7 | project(brotli) |
| 8 | |
| 9 | # If Brotli is being bundled in another project, we don't want to |
| 10 | # install anything. However, we want to let people override this, so |
| 11 | # we'll use the BROTLI_BUNDLED_MODE variable to let them do that; just |
| 12 | # set it to OFF in your project before you add_subdirectory(brotli). |
| 13 | get_directory_property(BROTLI_PARENT_DIRECTORY PARENT_DIRECTORY) |
| 14 | if(BROTLI_BUNDLED_MODE STREQUAL "") |
| 15 | # Bundled mode hasn't been set one way or the other, set the default |
| 16 | # depending on whether or not we are the top-level project. |
| 17 | if(BROTLI_PARENT_DIRECTORY) |
| 18 | set(BROTLI_BUNDLED_MODE OFF) |
| 19 | else() |
| 20 | set(BROTLI_BUNDLED_MODE ON) |
| 21 | endif() |
| 22 | endif() |
| 23 | mark_as_advanced(BROTLI_BUNDLED_MODE) |
| 24 | |
| 25 | # Parse version information from tools/version.h. Normally we would |
| 26 | # define these values here and write them out to configuration file(s) |
| 27 | # (i.e., config.h), but in this case we parse them from |
| 28 | # tools/version.h to be less intrusive. |
| 29 | file(STRINGS "tools/version.h" BROTLI_VERSION REGEX "^#define BROTLI_VERSION \"+([0-9]+)\\.([0-9]+)\\.([0-9]+)\"") |
| 30 | string(REGEX REPLACE "^#define BROTLI_VERSION \"([0-9]+)\\.([0-9]+)\\.([0-9]+)\"$" "\\1" BROTLI_VERSION_MAJOR "${BROTLI_VERSION}") |
| 31 | string(REGEX REPLACE "^#define BROTLI_VERSION \"([0-9]+)\\.([0-9]+)\\.([0-9]+)\"$" "\\2" BROTLI_VERSION_MINOR "${BROTLI_VERSION}") |
| 32 | string(REGEX REPLACE "^#define BROTLI_VERSION \"([0-9]+)\\.([0-9]+)\\.([0-9]+)\"$" "\\3" BROTLI_VERSION_REVISION "${BROTLI_VERSION}") |
| 33 | mark_as_advanced(BROTLI_VERSION_MAJOR BROTLI_VERSION_MINOR BROTLI_VERSION_REVISION) |
| 34 | |
Evan Nemerson | 37be4e3 | 2016-06-28 20:35:16 -0700 | [diff] [blame] | 35 | if (ENABLE_SANITIZER) |
| 36 | set(CMAKE_C_FLAGS " ${CMAKE_C_FLAGS} -fsanitize=${ENABLE_SANITIZER}") |
| 37 | set(CMAKE_CXX_FLAGS " ${CMAKE_CXX_FLAGS} -fsanitize=${ENABLE_SANITIZER}") |
| 38 | set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=${ENABLE_SANITIZER}") |
Evan Nemerson | 26a5935 | 2016-07-26 08:22:58 -0700 | [diff] [blame] | 39 | |
| 40 | # By default, brotli depends on undefined behavior, but setting |
| 41 | # BROTLI_BUILD_PORTABLE should result in a build which does not. |
| 42 | if(ENABLE_SANITIZER STREQUAL "undefined") |
| 43 | add_definitions(-DBROTLI_BUILD_PORTABLE) |
| 44 | endif() |
Evan Nemerson | 37be4e3 | 2016-06-28 20:35:16 -0700 | [diff] [blame] | 45 | endif () |
| 46 | |
Evan Nemerson | c1ec7ba | 2016-07-29 11:52:15 -0700 | [diff] [blame] | 47 | include(CheckFunctionExists) |
| 48 | set(LIBM_LIBRARY) |
| 49 | CHECK_FUNCTION_EXISTS(log2 LOG2_RES) |
| 50 | if(NOT LOG2_RES) |
Evan Nemerson | c1ec7ba | 2016-07-29 11:52:15 -0700 | [diff] [blame] | 51 | set(orig_req_libs "${CMAKE_REQUIRED_LIBRARIES}") |
| 52 | set(CMAKE_REQUIRED_LIBRARIES "${CMAKE_REQUIRED_LIBRARIES};m") |
Evan Nemerson | fe0e153 | 2016-08-04 18:51:20 -0700 | [diff] [blame^] | 53 | CHECK_FUNCTION_EXISTS(log2 LOG2_LIBM_RES) |
| 54 | if(LOG2_LIBM_RES) |
Evan Nemerson | c1ec7ba | 2016-07-29 11:52:15 -0700 | [diff] [blame] | 55 | set(LIBM_LIBRARY "m") |
| 56 | else() |
| 57 | message(FATAL_ERROR "log2() not found") |
| 58 | endif() |
| 59 | |
| 60 | set(CMAKE_REQUIRED_LIBRARIES "${orig_req_libs}") |
Evan Nemerson | fe0e153 | 2016-08-04 18:51:20 -0700 | [diff] [blame^] | 61 | unset(LOG2_LIBM_RES) |
Evan Nemerson | c1ec7ba | 2016-07-29 11:52:15 -0700 | [diff] [blame] | 62 | unset(orig_req_libs) |
| 63 | endif() |
| 64 | unset(LOG2_RES) |
| 65 | |
Evan Nemerson | 93ef13f | 2016-06-20 13:07:35 -0700 | [diff] [blame] | 66 | set(BROTLI_INCLUDE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}") |
Evan Nemerson | c1ec7ba | 2016-07-29 11:52:15 -0700 | [diff] [blame] | 67 | set(BROTLI_LIBRARIES brotli_enc brotli_dec brotli_common ${LIBM_LIBRARY}) |
Evan Nemerson | 93ef13f | 2016-06-20 13:07:35 -0700 | [diff] [blame] | 68 | mark_as_advanced(BROTLI_INCLUDE_DIRS BROTLI_LIBRARIES) |
| 69 | |
| 70 | if(${CMAKE_SYSTEM_NAME} MATCHES "Linux") |
| 71 | add_definitions(-DOS_LINUX) |
| 72 | elseif(${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD") |
| 73 | add_definitions(-DOS_FREEBSD) |
| 74 | elseif(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") |
| 75 | add_definitions(-DOS_MACOSX) |
| 76 | endif() |
| 77 | |
| 78 | add_library(brotli_common STATIC |
| 79 | common/dictionary.c) |
| 80 | add_library(brotli_dec STATIC |
| 81 | dec/bit_reader.c |
| 82 | dec/decode.c |
| 83 | dec/huffman.c |
| 84 | dec/state.c) |
| 85 | add_library(brotli_enc STATIC |
| 86 | enc/backward_references.c |
| 87 | enc/bit_cost.c |
| 88 | enc/block_splitter.c |
| 89 | enc/brotli_bit_stream.c |
| 90 | enc/cluster.c |
| 91 | enc/compress_fragment.c |
| 92 | enc/compress_fragment_two_pass.c |
| 93 | enc/encode.c |
| 94 | enc/entropy_encode.c |
| 95 | enc/histogram.c |
| 96 | enc/literal_cost.c |
| 97 | enc/memory.c |
| 98 | enc/metablock.c |
| 99 | enc/static_dict.c |
| 100 | enc/utf8_util.c) |
| 101 | |
| 102 | foreach(lib brotli_common brotli_dec brotli_enc) |
Evan Nemerson | c1ec7ba | 2016-07-29 11:52:15 -0700 | [diff] [blame] | 103 | target_link_libraries(${lib} ${LIBM_LIBRARY}) |
Evan Nemerson | 93ef13f | 2016-06-20 13:07:35 -0700 | [diff] [blame] | 104 | set_property(TARGET ${lib} APPEND PROPERTY INCLUDE_DIRECTORIES ${BROTLI_INCLUDE_DIRS}) |
| 105 | set_target_properties(${lib} PROPERTIES |
| 106 | VERSION ${BROTLI_VERSION_MAJOR}.${BROTLI_VERSION_MINOR}.${BROTLI_VERSION_REVISION} |
| 107 | POSITION_INDEPENDENT_CODE TRUE) |
| 108 | |
| 109 | set_property(TARGET ${lib} APPEND PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${BROTLI_INCLUDE_DIRS}") |
| 110 | endforeach() |
| 111 | |
| 112 | # For projects stuck on older versions of CMake, this will set the |
| 113 | # BROTLI_INCLUDE_DIRS and BROTLI_LIBRARIES variables so they still |
| 114 | # have a relatively easy way to use Brotli: |
| 115 | # |
| 116 | # include_directories(${BROTLI_INCLUDE_DIRS}) |
| 117 | # target_link_libraries(foo ${BROTLI_LIBRARIES}) |
| 118 | if(BROTLI_PARENT_DIRECTORY) |
| 119 | set(BROTLI_INCLUDE_DIRS "${BROTLI_INCLUDE_DIRS}" PARENT_SCOPE) |
| 120 | set(BROTLI_LIBRARIES "${BROTLI_LIBRARIES}" PARENT_SCOPE) |
| 121 | endif() |
| 122 | |
| 123 | # Build the bro executable |
| 124 | add_executable(bro tools/bro.c) |
| 125 | target_link_libraries(bro ${BROTLI_LIBRARIES}) |
| 126 | |
| 127 | # Installation |
| 128 | if(NOT BROTLI_BUNDLE_MODE) |
| 129 | include(GNUInstallDirs) |
| 130 | |
| 131 | install (TARGETS bro RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}") |
| 132 | endif() |
| 133 | |
| 134 | # Tests |
Evan Nemerson | 03657e8 | 2016-07-28 13:31:09 -0700 | [diff] [blame] | 135 | |
| 136 | # If we're targeting Windows but not running on Windows, we need Wine |
| 137 | # to run the tests... |
| 138 | if(NOT BROTLI_DISABLE_TESTS) |
| 139 | if(WIN32 AND NOT CMAKE_HOST_WIN32) |
| 140 | find_program(BROTLI_WINE NAMES wine) |
Evan Nemerson | 03657e8 | 2016-07-28 13:31:09 -0700 | [diff] [blame] | 141 | |
Evan Nemerson | c1ec7ba | 2016-07-29 11:52:15 -0700 | [diff] [blame] | 142 | if(NOT BROTLI_WINE) |
| 143 | message(STATUS "wine not found, disabling tests") |
| 144 | set(BROTLI_DISABLE_TESTS TRUE) |
| 145 | endif() |
Evan Nemerson | 03657e8 | 2016-07-28 13:31:09 -0700 | [diff] [blame] | 146 | endif() |
| 147 | endif() |
| 148 | |
Evan Nemerson | 93ef13f | 2016-06-20 13:07:35 -0700 | [diff] [blame] | 149 | if(NOT BROTLI_DISABLE_TESTS) |
| 150 | include(CTest) |
| 151 | enable_testing() |
| 152 | |
| 153 | set(ROUNDTRIP_INPUTS |
| 154 | tests/testdata/alice29.txt |
| 155 | tests/testdata/asyoulik.txt |
| 156 | tests/testdata/lcet10.txt |
| 157 | tests/testdata/plrabn12.txt |
| 158 | enc/encode.c |
| 159 | common/dictionary.h |
| 160 | dec/decode.c) |
| 161 | |
| 162 | foreach(INPUT ${ROUNDTRIP_INPUTS}) |
Evan Nemerson | 03657e8 | 2016-07-28 13:31:09 -0700 | [diff] [blame] | 163 | get_filename_component(OUTPUT_NAME "${INPUT}" NAME) |
| 164 | |
| 165 | set(OUTPUT_FILE "${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT_NAME}") |
| 166 | set(INPUT_FILE "${CMAKE_CURRENT_SOURCE_DIR}/${INPUT}") |
| 167 | |
Evan Nemerson | 93ef13f | 2016-06-20 13:07:35 -0700 | [diff] [blame] | 168 | foreach(quality 1 6 9 11) |
| 169 | add_test(NAME "${BROTLI_TEST_PREFIX}roundtrip/${INPUT}/${quality}" |
| 170 | COMMAND "${CMAKE_COMMAND}" |
Evan Nemerson | 03657e8 | 2016-07-28 13:31:09 -0700 | [diff] [blame] | 171 | -DBROTLI_WRAPPER=${BROTLI_WINE} |
Evan Nemerson | 93ef13f | 2016-06-20 13:07:35 -0700 | [diff] [blame] | 172 | -DBROTLI_CLI=$<TARGET_FILE:bro> |
| 173 | -DQUALITY=${quality} |
Evan Nemerson | 03657e8 | 2016-07-28 13:31:09 -0700 | [diff] [blame] | 174 | -DINPUT=${INPUT_FILE} |
| 175 | -DOUTPUT=${OUTPUT_FILE}.${quality} |
Evan Nemerson | 93ef13f | 2016-06-20 13:07:35 -0700 | [diff] [blame] | 176 | -P ${CMAKE_CURRENT_SOURCE_DIR}/tests/run-roundtrip-test.cmake) |
| 177 | endforeach() |
| 178 | endforeach() |
| 179 | |
| 180 | file(GLOB_RECURSE |
| 181 | COMPATIBILITY_INPUTS |
| 182 | RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} |
| 183 | tests/testdata/*.compressed*) |
| 184 | |
| 185 | foreach(INPUT ${COMPATIBILITY_INPUTS}) |
| 186 | add_test(NAME "${BROTLI_TEST_PREFIX}compatibility/${INPUT}" |
| 187 | COMMAND "${CMAKE_COMMAND}" |
Evan Nemerson | 03657e8 | 2016-07-28 13:31:09 -0700 | [diff] [blame] | 188 | -DBROTLI_WRAPPER=${BROTLI_WINE} |
Evan Nemerson | 93ef13f | 2016-06-20 13:07:35 -0700 | [diff] [blame] | 189 | -DBROTLI_CLI=$<TARGET_FILE:bro> |
| 190 | -DINPUT=${CMAKE_CURRENT_SOURCE_DIR}/${INPUT} |
| 191 | -P ${CMAKE_CURRENT_SOURCE_DIR}/tests/run-compatibility-test.cmake) |
| 192 | endforeach() |
| 193 | endif() |