blob: 9d55711f092ad66bb2339b978489243ea1b4f3bd [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 Nemerson93ef13f2016-06-20 13:07:35 -070047set(BROTLI_INCLUDE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}")
48set(BROTLI_LIBRARIES brotli_enc brotli_dec brotli_common m)
49mark_as_advanced(BROTLI_INCLUDE_DIRS BROTLI_LIBRARIES)
50
51if(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
52 add_definitions(-DOS_LINUX)
53elseif(${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD")
54 add_definitions(-DOS_FREEBSD)
55elseif(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
56 add_definitions(-DOS_MACOSX)
57endif()
58
59add_library(brotli_common STATIC
60 common/dictionary.c)
61add_library(brotli_dec STATIC
62 dec/bit_reader.c
63 dec/decode.c
64 dec/huffman.c
65 dec/state.c)
66add_library(brotli_enc STATIC
67 enc/backward_references.c
68 enc/bit_cost.c
69 enc/block_splitter.c
70 enc/brotli_bit_stream.c
71 enc/cluster.c
72 enc/compress_fragment.c
73 enc/compress_fragment_two_pass.c
74 enc/encode.c
75 enc/entropy_encode.c
76 enc/histogram.c
77 enc/literal_cost.c
78 enc/memory.c
79 enc/metablock.c
80 enc/static_dict.c
81 enc/utf8_util.c)
82
83foreach(lib brotli_common brotli_dec brotli_enc)
84 target_link_libraries(${lib} m)
85 set_property(TARGET ${lib} APPEND PROPERTY INCLUDE_DIRECTORIES ${BROTLI_INCLUDE_DIRS})
86 set_target_properties(${lib} PROPERTIES
87 VERSION ${BROTLI_VERSION_MAJOR}.${BROTLI_VERSION_MINOR}.${BROTLI_VERSION_REVISION}
88 POSITION_INDEPENDENT_CODE TRUE)
89
90 set_property(TARGET ${lib} APPEND PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${BROTLI_INCLUDE_DIRS}")
91endforeach()
92
93# For projects stuck on older versions of CMake, this will set the
94# BROTLI_INCLUDE_DIRS and BROTLI_LIBRARIES variables so they still
95# have a relatively easy way to use Brotli:
96#
97# include_directories(${BROTLI_INCLUDE_DIRS})
98# target_link_libraries(foo ${BROTLI_LIBRARIES})
99if(BROTLI_PARENT_DIRECTORY)
100 set(BROTLI_INCLUDE_DIRS "${BROTLI_INCLUDE_DIRS}" PARENT_SCOPE)
101 set(BROTLI_LIBRARIES "${BROTLI_LIBRARIES}" PARENT_SCOPE)
102endif()
103
104# Build the bro executable
105add_executable(bro tools/bro.c)
106target_link_libraries(bro ${BROTLI_LIBRARIES})
107
108# Installation
109if(NOT BROTLI_BUNDLE_MODE)
110 include(GNUInstallDirs)
111
112 install (TARGETS bro RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")
113endif()
114
115# Tests
116if(NOT BROTLI_DISABLE_TESTS)
117 include(CTest)
118 enable_testing()
119
120 set(ROUNDTRIP_INPUTS
121 tests/testdata/alice29.txt
122 tests/testdata/asyoulik.txt
123 tests/testdata/lcet10.txt
124 tests/testdata/plrabn12.txt
125 enc/encode.c
126 common/dictionary.h
127 dec/decode.c)
128
129 foreach(INPUT ${ROUNDTRIP_INPUTS})
130 foreach(quality 1 6 9 11)
131 add_test(NAME "${BROTLI_TEST_PREFIX}roundtrip/${INPUT}/${quality}"
132 COMMAND "${CMAKE_COMMAND}"
133 -DBROTLI_CLI=$<TARGET_FILE:bro>
134 -DQUALITY=${quality}
135 -DINPUT=${CMAKE_CURRENT_SOURCE_DIR}/${INPUT}
136 -P ${CMAKE_CURRENT_SOURCE_DIR}/tests/run-roundtrip-test.cmake)
137 endforeach()
138 endforeach()
139
140 file(GLOB_RECURSE
141 COMPATIBILITY_INPUTS
142 RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
143 tests/testdata/*.compressed*)
144
145 foreach(INPUT ${COMPATIBILITY_INPUTS})
146 add_test(NAME "${BROTLI_TEST_PREFIX}compatibility/${INPUT}"
147 COMMAND "${CMAKE_COMMAND}"
148 -DBROTLI_CLI=$<TARGET_FILE:bro>
149 -DINPUT=${CMAKE_CURRENT_SOURCE_DIR}/${INPUT}
150 -P ${CMAKE_CURRENT_SOURCE_DIR}/tests/run-compatibility-test.cmake)
151 endforeach()
152endif()