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 | |
Eugene Kliuchnikov | 2c2d557 | 2016-08-22 15:44:12 +0200 | [diff] [blame^] | 25 | # Parse version information from common/version.h. Normally we would |
Evan Nemerson | 93ef13f | 2016-06-20 13:07:35 -0700 | [diff] [blame] | 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 |
Eugene Kliuchnikov | 2c2d557 | 2016-08-22 15:44:12 +0200 | [diff] [blame^] | 28 | # common/version.h to be less intrusive. |
| 29 | function(hex_to_dec HEXADECIMAL DECIMAL) |
| 30 | string(TOUPPER "${HEXADECIMAL}" _tail) |
| 31 | set(_decimal 0) |
| 32 | string(LENGTH "${_tail}" _tail_length) |
| 33 | while (_tail_length GREATER 0) |
| 34 | math(EXPR _decimal "${_decimal} * 16") |
| 35 | string(SUBSTRING "${_tail}" 0 1 _digit) |
| 36 | string(SUBSTRING "${_tail}" 1 -1 _tail) |
| 37 | if (_digit STREQUAL "A") |
| 38 | math(EXPR _decimal "${_decimal} + 10") |
| 39 | elseif (_digit STREQUAL "B") |
| 40 | math(EXPR _decimal "${_decimal} + 11") |
| 41 | elseif (_digit STREQUAL "C") |
| 42 | math(EXPR _decimal "${_decimal} + 12") |
| 43 | elseif (_digit STREQUAL "D") |
| 44 | math(EXPR _decimal "${_decimal} + 13") |
| 45 | elseif (_digit STREQUAL "E") |
| 46 | math(EXPR _decimal "${_decimal} + 14") |
| 47 | elseif (_digit STREQUAL "F") |
| 48 | math(EXPR _decimal "${_decimal} + 15") |
| 49 | else() |
| 50 | math(EXPR _decimal "${_decimal} + ${_digit}") |
| 51 | endif() |
| 52 | string(LENGTH "${_tail}" _tail_length) |
| 53 | endwhile() |
| 54 | set(${DECIMAL} ${_decimal} PARENT_SCOPE) |
| 55 | endfunction(hex_to_dec) |
| 56 | |
| 57 | file(STRINGS "common/version.h" _brotli_version_line REGEX "^#define BROTLI_VERSION (0x[0-9a-fA-F]+)$") |
| 58 | string(REGEX REPLACE "^#define BROTLI_VERSION 0x([0-9a-fA-F]+)$" "\\1" _brotli_version_hex "${_brotli_version_line}") |
| 59 | hex_to_dec("${_brotli_version_hex}" _brotli_version) |
| 60 | math(EXPR BROTLI_VERSION_MAJOR "${_brotli_version} >> 24") |
| 61 | math(EXPR BROTLI_VERSION_MINOR "(${_brotli_version} >> 12) & 4095") |
| 62 | math(EXPR BROTLI_VERSION_REVISION "${_brotli_version} & 4095") |
Evan Nemerson | 93ef13f | 2016-06-20 13:07:35 -0700 | [diff] [blame] | 63 | mark_as_advanced(BROTLI_VERSION_MAJOR BROTLI_VERSION_MINOR BROTLI_VERSION_REVISION) |
| 64 | |
Evan Nemerson | 37be4e3 | 2016-06-28 20:35:16 -0700 | [diff] [blame] | 65 | if (ENABLE_SANITIZER) |
| 66 | set(CMAKE_C_FLAGS " ${CMAKE_C_FLAGS} -fsanitize=${ENABLE_SANITIZER}") |
| 67 | set(CMAKE_CXX_FLAGS " ${CMAKE_CXX_FLAGS} -fsanitize=${ENABLE_SANITIZER}") |
| 68 | set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=${ENABLE_SANITIZER}") |
Evan Nemerson | 26a5935 | 2016-07-26 08:22:58 -0700 | [diff] [blame] | 69 | |
| 70 | # By default, brotli depends on undefined behavior, but setting |
| 71 | # BROTLI_BUILD_PORTABLE should result in a build which does not. |
| 72 | if(ENABLE_SANITIZER STREQUAL "undefined") |
| 73 | add_definitions(-DBROTLI_BUILD_PORTABLE) |
| 74 | endif() |
Evan Nemerson | 37be4e3 | 2016-06-28 20:35:16 -0700 | [diff] [blame] | 75 | endif () |
| 76 | |
Evan Nemerson | c1ec7ba | 2016-07-29 11:52:15 -0700 | [diff] [blame] | 77 | include(CheckFunctionExists) |
| 78 | set(LIBM_LIBRARY) |
| 79 | CHECK_FUNCTION_EXISTS(log2 LOG2_RES) |
| 80 | if(NOT LOG2_RES) |
Evan Nemerson | c1ec7ba | 2016-07-29 11:52:15 -0700 | [diff] [blame] | 81 | set(orig_req_libs "${CMAKE_REQUIRED_LIBRARIES}") |
| 82 | set(CMAKE_REQUIRED_LIBRARIES "${CMAKE_REQUIRED_LIBRARIES};m") |
Evan Nemerson | fe0e153 | 2016-08-04 18:51:20 -0700 | [diff] [blame] | 83 | CHECK_FUNCTION_EXISTS(log2 LOG2_LIBM_RES) |
| 84 | if(LOG2_LIBM_RES) |
Evan Nemerson | c1ec7ba | 2016-07-29 11:52:15 -0700 | [diff] [blame] | 85 | set(LIBM_LIBRARY "m") |
| 86 | else() |
| 87 | message(FATAL_ERROR "log2() not found") |
| 88 | endif() |
| 89 | |
| 90 | set(CMAKE_REQUIRED_LIBRARIES "${orig_req_libs}") |
Evan Nemerson | fe0e153 | 2016-08-04 18:51:20 -0700 | [diff] [blame] | 91 | unset(LOG2_LIBM_RES) |
Evan Nemerson | c1ec7ba | 2016-07-29 11:52:15 -0700 | [diff] [blame] | 92 | unset(orig_req_libs) |
| 93 | endif() |
| 94 | unset(LOG2_RES) |
| 95 | |
Evan Nemerson | 93ef13f | 2016-06-20 13:07:35 -0700 | [diff] [blame] | 96 | set(BROTLI_INCLUDE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}") |
Evan Nemerson | c1ec7ba | 2016-07-29 11:52:15 -0700 | [diff] [blame] | 97 | set(BROTLI_LIBRARIES brotli_enc brotli_dec brotli_common ${LIBM_LIBRARY}) |
Evan Nemerson | 93ef13f | 2016-06-20 13:07:35 -0700 | [diff] [blame] | 98 | mark_as_advanced(BROTLI_INCLUDE_DIRS BROTLI_LIBRARIES) |
| 99 | |
| 100 | if(${CMAKE_SYSTEM_NAME} MATCHES "Linux") |
| 101 | add_definitions(-DOS_LINUX) |
| 102 | elseif(${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD") |
| 103 | add_definitions(-DOS_FREEBSD) |
| 104 | elseif(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") |
| 105 | add_definitions(-DOS_MACOSX) |
| 106 | endif() |
| 107 | |
| 108 | add_library(brotli_common STATIC |
| 109 | common/dictionary.c) |
| 110 | add_library(brotli_dec STATIC |
| 111 | dec/bit_reader.c |
| 112 | dec/decode.c |
| 113 | dec/huffman.c |
| 114 | dec/state.c) |
| 115 | add_library(brotli_enc STATIC |
| 116 | enc/backward_references.c |
| 117 | enc/bit_cost.c |
| 118 | enc/block_splitter.c |
| 119 | enc/brotli_bit_stream.c |
| 120 | enc/cluster.c |
| 121 | enc/compress_fragment.c |
| 122 | enc/compress_fragment_two_pass.c |
| 123 | enc/encode.c |
| 124 | enc/entropy_encode.c |
| 125 | enc/histogram.c |
| 126 | enc/literal_cost.c |
| 127 | enc/memory.c |
| 128 | enc/metablock.c |
| 129 | enc/static_dict.c |
| 130 | enc/utf8_util.c) |
| 131 | |
| 132 | foreach(lib brotli_common brotli_dec brotli_enc) |
Evan Nemerson | c1ec7ba | 2016-07-29 11:52:15 -0700 | [diff] [blame] | 133 | target_link_libraries(${lib} ${LIBM_LIBRARY}) |
Evan Nemerson | 93ef13f | 2016-06-20 13:07:35 -0700 | [diff] [blame] | 134 | set_property(TARGET ${lib} APPEND PROPERTY INCLUDE_DIRECTORIES ${BROTLI_INCLUDE_DIRS}) |
| 135 | set_target_properties(${lib} PROPERTIES |
| 136 | VERSION ${BROTLI_VERSION_MAJOR}.${BROTLI_VERSION_MINOR}.${BROTLI_VERSION_REVISION} |
| 137 | POSITION_INDEPENDENT_CODE TRUE) |
| 138 | |
| 139 | set_property(TARGET ${lib} APPEND PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${BROTLI_INCLUDE_DIRS}") |
| 140 | endforeach() |
| 141 | |
| 142 | # For projects stuck on older versions of CMake, this will set the |
| 143 | # BROTLI_INCLUDE_DIRS and BROTLI_LIBRARIES variables so they still |
| 144 | # have a relatively easy way to use Brotli: |
| 145 | # |
| 146 | # include_directories(${BROTLI_INCLUDE_DIRS}) |
| 147 | # target_link_libraries(foo ${BROTLI_LIBRARIES}) |
| 148 | if(BROTLI_PARENT_DIRECTORY) |
| 149 | set(BROTLI_INCLUDE_DIRS "${BROTLI_INCLUDE_DIRS}" PARENT_SCOPE) |
| 150 | set(BROTLI_LIBRARIES "${BROTLI_LIBRARIES}" PARENT_SCOPE) |
| 151 | endif() |
| 152 | |
| 153 | # Build the bro executable |
| 154 | add_executable(bro tools/bro.c) |
| 155 | target_link_libraries(bro ${BROTLI_LIBRARIES}) |
| 156 | |
| 157 | # Installation |
| 158 | if(NOT BROTLI_BUNDLE_MODE) |
| 159 | include(GNUInstallDirs) |
| 160 | |
| 161 | install (TARGETS bro RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}") |
| 162 | endif() |
| 163 | |
| 164 | # Tests |
Evan Nemerson | 03657e8 | 2016-07-28 13:31:09 -0700 | [diff] [blame] | 165 | |
| 166 | # If we're targeting Windows but not running on Windows, we need Wine |
| 167 | # to run the tests... |
| 168 | if(NOT BROTLI_DISABLE_TESTS) |
| 169 | if(WIN32 AND NOT CMAKE_HOST_WIN32) |
| 170 | find_program(BROTLI_WINE NAMES wine) |
Evan Nemerson | 03657e8 | 2016-07-28 13:31:09 -0700 | [diff] [blame] | 171 | |
Evan Nemerson | c1ec7ba | 2016-07-29 11:52:15 -0700 | [diff] [blame] | 172 | if(NOT BROTLI_WINE) |
| 173 | message(STATUS "wine not found, disabling tests") |
| 174 | set(BROTLI_DISABLE_TESTS TRUE) |
| 175 | endif() |
Evan Nemerson | 03657e8 | 2016-07-28 13:31:09 -0700 | [diff] [blame] | 176 | endif() |
| 177 | endif() |
| 178 | |
Evan Nemerson | 93ef13f | 2016-06-20 13:07:35 -0700 | [diff] [blame] | 179 | if(NOT BROTLI_DISABLE_TESTS) |
| 180 | include(CTest) |
| 181 | enable_testing() |
| 182 | |
| 183 | set(ROUNDTRIP_INPUTS |
| 184 | tests/testdata/alice29.txt |
| 185 | tests/testdata/asyoulik.txt |
| 186 | tests/testdata/lcet10.txt |
| 187 | tests/testdata/plrabn12.txt |
| 188 | enc/encode.c |
| 189 | common/dictionary.h |
| 190 | dec/decode.c) |
| 191 | |
| 192 | foreach(INPUT ${ROUNDTRIP_INPUTS}) |
Evan Nemerson | 03657e8 | 2016-07-28 13:31:09 -0700 | [diff] [blame] | 193 | get_filename_component(OUTPUT_NAME "${INPUT}" NAME) |
| 194 | |
| 195 | set(OUTPUT_FILE "${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT_NAME}") |
| 196 | set(INPUT_FILE "${CMAKE_CURRENT_SOURCE_DIR}/${INPUT}") |
| 197 | |
Evan Nemerson | 93ef13f | 2016-06-20 13:07:35 -0700 | [diff] [blame] | 198 | foreach(quality 1 6 9 11) |
| 199 | add_test(NAME "${BROTLI_TEST_PREFIX}roundtrip/${INPUT}/${quality}" |
| 200 | COMMAND "${CMAKE_COMMAND}" |
Evan Nemerson | 03657e8 | 2016-07-28 13:31:09 -0700 | [diff] [blame] | 201 | -DBROTLI_WRAPPER=${BROTLI_WINE} |
Evan Nemerson | 93ef13f | 2016-06-20 13:07:35 -0700 | [diff] [blame] | 202 | -DBROTLI_CLI=$<TARGET_FILE:bro> |
| 203 | -DQUALITY=${quality} |
Evan Nemerson | 03657e8 | 2016-07-28 13:31:09 -0700 | [diff] [blame] | 204 | -DINPUT=${INPUT_FILE} |
| 205 | -DOUTPUT=${OUTPUT_FILE}.${quality} |
Evan Nemerson | 93ef13f | 2016-06-20 13:07:35 -0700 | [diff] [blame] | 206 | -P ${CMAKE_CURRENT_SOURCE_DIR}/tests/run-roundtrip-test.cmake) |
| 207 | endforeach() |
| 208 | endforeach() |
| 209 | |
| 210 | file(GLOB_RECURSE |
| 211 | COMPATIBILITY_INPUTS |
| 212 | RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} |
| 213 | tests/testdata/*.compressed*) |
| 214 | |
| 215 | foreach(INPUT ${COMPATIBILITY_INPUTS}) |
| 216 | add_test(NAME "${BROTLI_TEST_PREFIX}compatibility/${INPUT}" |
| 217 | COMMAND "${CMAKE_COMMAND}" |
Evan Nemerson | 03657e8 | 2016-07-28 13:31:09 -0700 | [diff] [blame] | 218 | -DBROTLI_WRAPPER=${BROTLI_WINE} |
Evan Nemerson | 93ef13f | 2016-06-20 13:07:35 -0700 | [diff] [blame] | 219 | -DBROTLI_CLI=$<TARGET_FILE:bro> |
| 220 | -DINPUT=${CMAKE_CURRENT_SOURCE_DIR}/${INPUT} |
| 221 | -P ${CMAKE_CURRENT_SOURCE_DIR}/tests/run-compatibility-test.cmake) |
| 222 | endforeach() |
| 223 | endif() |