blob: 0cba0f051330352d45faffd0ba6c629f028e342c [file] [log] [blame]
Evan Nemerson93ef13f2016-06-20 13:07:35 -07001# 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.
5cmake_minimum_required(VERSION 2.8.6)
6
7project(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).
13get_directory_property(BROTLI_PARENT_DIRECTORY PARENT_DIRECTORY)
14if(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()
22endif()
23mark_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.
29file(STRINGS "tools/version.h" BROTLI_VERSION REGEX "^#define BROTLI_VERSION \"+([0-9]+)\\.([0-9]+)\\.([0-9]+)\"")
30string(REGEX REPLACE "^#define BROTLI_VERSION \"([0-9]+)\\.([0-9]+)\\.([0-9]+)\"$" "\\1" BROTLI_VERSION_MAJOR "${BROTLI_VERSION}")
31string(REGEX REPLACE "^#define BROTLI_VERSION \"([0-9]+)\\.([0-9]+)\\.([0-9]+)\"$" "\\2" BROTLI_VERSION_MINOR "${BROTLI_VERSION}")
32string(REGEX REPLACE "^#define BROTLI_VERSION \"([0-9]+)\\.([0-9]+)\\.([0-9]+)\"$" "\\3" BROTLI_VERSION_REVISION "${BROTLI_VERSION}")
33mark_as_advanced(BROTLI_VERSION_MAJOR BROTLI_VERSION_MINOR BROTLI_VERSION_REVISION)
34
Evan Nemerson37be4e32016-06-28 20:35:16 -070035if (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 Nemerson26a59352016-07-26 08:22:58 -070039
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 Nemerson37be4e32016-06-28 20:35:16 -070045endif ()
46
Evan Nemersonc1ec7ba2016-07-29 11:52:15 -070047include(CheckFunctionExists)
48set(LIBM_LIBRARY)
49CHECK_FUNCTION_EXISTS(log2 LOG2_RES)
50if(NOT LOG2_RES)
Evan Nemersonc1ec7ba2016-07-29 11:52:15 -070051 set(orig_req_libs "${CMAKE_REQUIRED_LIBRARIES}")
52 set(CMAKE_REQUIRED_LIBRARIES "${CMAKE_REQUIRED_LIBRARIES};m")
Evan Nemersonfe0e1532016-08-04 18:51:20 -070053 CHECK_FUNCTION_EXISTS(log2 LOG2_LIBM_RES)
54 if(LOG2_LIBM_RES)
Evan Nemersonc1ec7ba2016-07-29 11:52:15 -070055 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 Nemersonfe0e1532016-08-04 18:51:20 -070061 unset(LOG2_LIBM_RES)
Evan Nemersonc1ec7ba2016-07-29 11:52:15 -070062 unset(orig_req_libs)
63endif()
64unset(LOG2_RES)
65
Evan Nemerson93ef13f2016-06-20 13:07:35 -070066set(BROTLI_INCLUDE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}")
Evan Nemersonc1ec7ba2016-07-29 11:52:15 -070067set(BROTLI_LIBRARIES brotli_enc brotli_dec brotli_common ${LIBM_LIBRARY})
Evan Nemerson93ef13f2016-06-20 13:07:35 -070068mark_as_advanced(BROTLI_INCLUDE_DIRS BROTLI_LIBRARIES)
69
70if(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
71 add_definitions(-DOS_LINUX)
72elseif(${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD")
73 add_definitions(-DOS_FREEBSD)
74elseif(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
75 add_definitions(-DOS_MACOSX)
76endif()
77
78add_library(brotli_common STATIC
79 common/dictionary.c)
80add_library(brotli_dec STATIC
81 dec/bit_reader.c
82 dec/decode.c
83 dec/huffman.c
84 dec/state.c)
85add_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
102foreach(lib brotli_common brotli_dec brotli_enc)
Evan Nemersonc1ec7ba2016-07-29 11:52:15 -0700103 target_link_libraries(${lib} ${LIBM_LIBRARY})
Evan Nemerson93ef13f2016-06-20 13:07:35 -0700104 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}")
110endforeach()
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})
118if(BROTLI_PARENT_DIRECTORY)
119 set(BROTLI_INCLUDE_DIRS "${BROTLI_INCLUDE_DIRS}" PARENT_SCOPE)
120 set(BROTLI_LIBRARIES "${BROTLI_LIBRARIES}" PARENT_SCOPE)
121endif()
122
123# Build the bro executable
124add_executable(bro tools/bro.c)
125target_link_libraries(bro ${BROTLI_LIBRARIES})
126
127# Installation
128if(NOT BROTLI_BUNDLE_MODE)
129 include(GNUInstallDirs)
130
131 install (TARGETS bro RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")
132endif()
133
134# Tests
Evan Nemerson03657e82016-07-28 13:31:09 -0700135
136# If we're targeting Windows but not running on Windows, we need Wine
137# to run the tests...
138if(NOT BROTLI_DISABLE_TESTS)
139 if(WIN32 AND NOT CMAKE_HOST_WIN32)
140 find_program(BROTLI_WINE NAMES wine)
Evan Nemerson03657e82016-07-28 13:31:09 -0700141
Evan Nemersonc1ec7ba2016-07-29 11:52:15 -0700142 if(NOT BROTLI_WINE)
143 message(STATUS "wine not found, disabling tests")
144 set(BROTLI_DISABLE_TESTS TRUE)
145 endif()
Evan Nemerson03657e82016-07-28 13:31:09 -0700146 endif()
147endif()
148
Evan Nemerson93ef13f2016-06-20 13:07:35 -0700149if(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 Nemerson03657e82016-07-28 13:31:09 -0700163 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 Nemerson93ef13f2016-06-20 13:07:35 -0700168 foreach(quality 1 6 9 11)
169 add_test(NAME "${BROTLI_TEST_PREFIX}roundtrip/${INPUT}/${quality}"
170 COMMAND "${CMAKE_COMMAND}"
Evan Nemerson03657e82016-07-28 13:31:09 -0700171 -DBROTLI_WRAPPER=${BROTLI_WINE}
Evan Nemerson93ef13f2016-06-20 13:07:35 -0700172 -DBROTLI_CLI=$<TARGET_FILE:bro>
173 -DQUALITY=${quality}
Evan Nemerson03657e82016-07-28 13:31:09 -0700174 -DINPUT=${INPUT_FILE}
175 -DOUTPUT=${OUTPUT_FILE}.${quality}
Evan Nemerson93ef13f2016-06-20 13:07:35 -0700176 -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 Nemerson03657e82016-07-28 13:31:09 -0700188 -DBROTLI_WRAPPER=${BROTLI_WINE}
Evan Nemerson93ef13f2016-06-20 13:07:35 -0700189 -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()
193endif()