Skip to content

Commit 22c256b

Browse files
authored
Merge pull request #92 from rsheeter/master
Add support for the CMake build system.
2 parents 3060d8b + ed27db0 commit 22c256b

File tree

4 files changed

+372
-0
lines changed

4 files changed

+372
-0
lines changed

CMakeLists.txt

Lines changed: 279 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,279 @@
1+
# Copyright 2017 Igalia S.L. All Rights Reserved.
2+
#
3+
# Distributed under MIT license.
4+
# See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
5+
6+
# Ubuntu 12.04 LTS has CMake 2.8.7, and is an important target since
7+
# several CI services, such as Travis and Drone, use it. Solaris 11
8+
# has 2.8.6, and it's not difficult to support if you already have to
9+
# support 2.8.7.
10+
cmake_minimum_required(VERSION 2.8.6)
11+
12+
project(woff2)
13+
14+
include(GNUInstallDirs)
15+
16+
# Build options
17+
option(BUILD_SHARED_LIBS "Build shared libraries" ON)
18+
option(CANONICAL_PREFIXES "Canonical prefixes" OFF)
19+
option(NOISY_LOGGING "Noisy logging" ON)
20+
21+
# Version information
22+
set(WOFF2_VERSION 0.0.1)
23+
24+
# When building shared libraries it is important to set the correct rpath
25+
# See https://cmake.org/Wiki/CMake_RPATH_handling#Always_full_RPATH
26+
set(CMAKE_SKIP_BUILD_RPATH FALSE)
27+
set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
28+
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
29+
list(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${CMAKE_INSTALL_LIBDIR}" isSystemDir)
30+
if ("${isSystemDir}" STREQUAL "-1")
31+
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_LIBDIR}")
32+
endif()
33+
34+
# Find Brotli dependencies
35+
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
36+
find_package(BrotliDec)
37+
if (NOT BROTLIDEC_FOUND)
38+
message(FATAL_ERROR "librotlidec is needed to build woff2.")
39+
endif ()
40+
find_package(BrotliEnc)
41+
if (NOT BROTLIENC_FOUND)
42+
message(FATAL_ERROR "librotlienc is needed to build woff2.")
43+
endif ()
44+
45+
# Set compiler flags
46+
if (NOT CANONICAL_PREFIXES)
47+
add_definitions(-no-canonical-prefixes)
48+
endif ()
49+
if (NOISY_LOGGING)
50+
add_definitions(-DFONT_COMPRESSION_BIN)
51+
endif ()
52+
add_definitions(-D__STDC_FORMAT_MACROS)
53+
set(COMMON_FLAGS -fno-omit-frame-pointer)
54+
55+
if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
56+
add_definitions(-DOS_MACOSX)
57+
else ()
58+
set(COMMON_FLAGS "${COMMON_FLAG} -fno-omit-frame-pointer")
59+
endif()
60+
61+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${COMMON_FLAG}")
62+
63+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COMMON_FLAG}")
64+
set(CMAKE_CXX_STANDARD 11)
65+
66+
# Set search path for our private/public headers as well as Brotli headers
67+
include_directories("src" "include"
68+
"${BROTLIDEC_INCLUDE_DIRS}" "${BROTLIENC_INCLUDE_DIRS}")
69+
70+
# Common part used by decoder and encoder
71+
add_library(woff2common
72+
src/table_tags.cc
73+
src/variable_length.cc
74+
src/woff2_common.cc)
75+
76+
# WOFF2 Decoder
77+
add_library(woff2dec
78+
src/woff2_dec.cc
79+
src/woff2_out.cc)
80+
target_link_libraries(woff2dec woff2common "${BROTLIDEC_LIBRARIES}")
81+
add_executable(woff2_decompress src/woff2_decompress.cc)
82+
target_link_libraries(woff2_decompress woff2dec)
83+
84+
# WOFF2 Encoder
85+
add_library(woff2enc
86+
src/font.cc
87+
src/glyph.cc
88+
src/normalize.cc
89+
src/transform.cc
90+
src/woff2_enc.cc)
91+
target_link_libraries(woff2enc woff2common "${BROTLIENC_LIBRARIES}")
92+
add_executable(woff2_compress src/woff2_compress.cc)
93+
target_link_libraries(woff2_compress woff2enc)
94+
95+
# WOFF2 info
96+
add_executable(woff2_info src/woff2_info.cc)
97+
target_link_libraries(woff2_info woff2common)
98+
99+
foreach(lib woff2common woff2dec woff2enc)
100+
set_target_properties(${lib} PROPERTIES
101+
SOVERSION ${WOFF2_VERSION}
102+
VERSION ${WOFF2_VERSION}
103+
POSITION_INDEPENDENT_CODE TRUE)
104+
endforeach()
105+
106+
# Fuzzer libraries
107+
add_library(convert_woff2ttf_fuzzer STATIC src/convert_woff2ttf_fuzzer.cc)
108+
target_link_libraries(convert_woff2ttf_fuzzer woff2dec)
109+
add_library(convert_woff2ttf_fuzzer_new_entry STATIC src/convert_woff2ttf_fuzzer_new_entry.cc)
110+
target_link_libraries(convert_woff2ttf_fuzzer_new_entry woff2dec)
111+
112+
# PC files
113+
include(CMakeParseArguments)
114+
115+
function(generate_pkg_config_path outvar path)
116+
string(LENGTH "${path}" path_length)
117+
118+
set(path_args ${ARGV})
119+
list(REMOVE_AT path_args 0 1)
120+
list(LENGTH path_args path_args_remaining)
121+
122+
set("${outvar}" "${path}")
123+
124+
while(path_args_remaining GREATER 1)
125+
list(GET path_args 0 name)
126+
list(GET path_args 1 value)
127+
128+
get_filename_component(value_full "${value}" ABSOLUTE)
129+
string(LENGTH "${value}" value_length)
130+
131+
if(path_length EQUAL value_length AND path STREQUAL value)
132+
set("${outvar}" "\${${name}}")
133+
break()
134+
elseif(path_length GREATER value_length)
135+
# We might be in a subdirectory of the value, but we have to be
136+
# careful about a prefix matching but not being a subdirectory
137+
# (for example, /usr/lib64 is not a subdirectory of /usr/lib).
138+
# We'll do this by making sure the next character is a directory
139+
# separator.
140+
string(SUBSTRING "${path}" ${value_length} 1 sep)
141+
if(sep STREQUAL "/")
142+
string(SUBSTRING "${path}" 0 ${value_length} s)
143+
if(s STREQUAL value)
144+
string(SUBSTRING "${path}" "${value_length}" -1 suffix)
145+
set("${outvar}" "\${${name}}${suffix}")
146+
break()
147+
endif()
148+
endif()
149+
endif()
150+
151+
list(REMOVE_AT path_args 0 1)
152+
list(LENGTH path_args path_args_remaining)
153+
endwhile()
154+
155+
set("${outvar}" "${${outvar}}" PARENT_SCOPE)
156+
endfunction(generate_pkg_config_path)
157+
158+
function(generate_pkg_config output_file)
159+
set (options)
160+
set (oneValueArgs NAME DESCRIPTION URL VERSION PREFIX LIBDIR INCLUDEDIR)
161+
set (multiValueArgs DEPENDS_PRIVATE CFLAGS LIBRARIES)
162+
cmake_parse_arguments(GEN_PKG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
163+
unset (options)
164+
unset (oneValueArgs)
165+
unset (multiValueArgs)
166+
167+
if(NOT GEN_PKG_PREFIX)
168+
set(GEN_PKG_PREFIX "${CMAKE_INSTALL_PREFIX}")
169+
endif()
170+
171+
if(NOT GEN_PKG_LIBDIR)
172+
set(GEN_PKG_LIBDIR "${CMAKE_INSTALL_FULL_LIBDIR}")
173+
endif()
174+
generate_pkg_config_path(GEN_PKG_LIBDIR "${GEN_PKG_LIBDIR}"
175+
prefix "${GEN_PKG_PREFIX}")
176+
177+
if(NOT GEN_PKG_INCLUDEDIR)
178+
set(GEN_PKG_INCLUDEDIR "${CMAKE_INSTALL_FULL_INCLUDEDIR}")
179+
endif()
180+
generate_pkg_config_path(GEN_PKG_INCLUDEDIR "${GEN_PKG_INCLUDEDIR}"
181+
prefix "${GEN_PKG_PREFIX}")
182+
183+
file(WRITE "${output_file}" "prefix=${GEN_PKG_PREFIX}\n")
184+
file(APPEND "${output_file}" "libdir=${GEN_PKG_LIBDIR}\n")
185+
file(APPEND "${output_file}" "includedir=${GEN_PKG_INCLUDEDIR}\n")
186+
file(APPEND "${output_file}" "\n")
187+
188+
if(GEN_PKG_NAME)
189+
file(APPEND "${output_file}" "Name: ${GEN_PKG_NAME}\n")
190+
else()
191+
file(APPEND "${output_file}" "Name: ${CMAKE_PROJECT_NAME}\n")
192+
endif()
193+
194+
if(GEN_PKG_DESCRIPTION)
195+
file(APPEND "${output_file}" "Description: ${GEN_PKG_DESCRIPTION}\n")
196+
endif()
197+
198+
if(GEN_PKG_URL)
199+
file(APPEND "${output_file}" "URL: ${GEN_PKG_URL}\n")
200+
endif()
201+
202+
if(GEN_PKG_VERSION)
203+
file(APPEND "${output_file}" "Version: ${GEN_PKG_VERSION}\n")
204+
endif()
205+
206+
if(GEN_PKG_DEPENDS_PRIVATE)
207+
file(APPEND "${output_file}" "Requires.private:")
208+
foreach(lib ${GEN_PKG_DEPENDS_PRIVATE})
209+
file(APPEND "${output_file}" " ${lib}")
210+
endforeach()
211+
file(APPEND "${output_file}" "\n")
212+
endif()
213+
214+
if(GEN_PKG_LIBRARIES)
215+
set(libs)
216+
217+
file(APPEND "${output_file}" "Libs: -L\${libdir}")
218+
foreach(lib ${GEN_PKG_LIBRARIES})
219+
file(APPEND "${output_file}" " -l${lib}")
220+
endforeach()
221+
file(APPEND "${output_file}" "\n")
222+
endif()
223+
224+
file(APPEND "${output_file}" "Cflags: -I\${includedir}")
225+
if(GEN_PKG_CFLAGS)
226+
foreach(cflag ${GEN_PKG_CFLAGS})
227+
file(APPEND "${output_file}" " ${cflag}")
228+
endforeach()
229+
endif()
230+
file(APPEND "${output_file}" "\n")
231+
endfunction(generate_pkg_config)
232+
233+
generate_pkg_config ("${CMAKE_CURRENT_BINARY_DIR}/libwoff2common.pc"
234+
NAME libwoff2common
235+
DESCRIPTION "Shared data used by libwoff2 and libwoff2dec libraries"
236+
URL "https://github.com/google/woff2"
237+
VERSION "${WOFF2_VERSION}"
238+
LIBRARIES woff2common)
239+
240+
generate_pkg_config ("${CMAKE_CURRENT_BINARY_DIR}/libwoff2dec.pc"
241+
NAME libwoff2dec
242+
DESCRIPTION "WOFF2 decoder library"
243+
URL "https://github.com/google/woff2"
244+
VERSION "${WOFF2_VERSION}"
245+
DEPENDS_PRIVATE libwoff2common
246+
LIBRARIES woff2dec)
247+
248+
generate_pkg_config ("${CMAKE_CURRENT_BINARY_DIR}/libwoff2enc.pc"
249+
NAME libwoff2enc
250+
DESCRIPTION "WOFF2 encoder library"
251+
URL "https://github.com/google/woff2"
252+
VERSION "${WOFF2_VERSION}"
253+
DEPENDS_PRIVATE libwoff2common
254+
LIBRARIES woff2enc)
255+
256+
# Installation
257+
if (NOT BUILD_SHARED_LIBS)
258+
install(
259+
TARGETS woff2_decompress woff2_compress woff2_info
260+
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
261+
)
262+
endif()
263+
264+
install(
265+
TARGETS woff2common woff2dec woff2enc
266+
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
267+
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
268+
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
269+
)
270+
install(
271+
DIRECTORY include/woff2
272+
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
273+
)
274+
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/libwoff2common.pc"
275+
DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
276+
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/libwoff2dec.pc"
277+
DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
278+
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/libwoff2enc.pc"
279+
DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,29 @@ cd woff2
2525
make clean all
2626
```
2727

28+
Alternatively, if Brotli is already installed on your system you can use CMake
29+
to build executables and libraries:
30+
31+
```
32+
git clone https://github.com/google/woff2.git
33+
cd woff2
34+
mkdir out
35+
cd out
36+
cmake ..
37+
make
38+
make install
39+
```
40+
41+
By default, shared libraries are built. To use static linkage, do:
42+
43+
```
44+
cd woff2
45+
mkdir out-static
46+
cmake -DBUILD_SHARED_LIBS=OFF ..
47+
make
48+
make install
49+
```
50+
2851
## Run
2952

3053
Ensure the binaries from the build process are in your $PATH, then:

cmake/FindBrotliDec.cmake

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Copyright 2017 Igalia S.L. All Rights Reserved.
2+
#
3+
# Distributed under MIT license.
4+
# See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
5+
6+
# Try to find BrotliDec. Once done, this will define
7+
#
8+
# BROTLIDEC_FOUND - system has BrotliDec.
9+
# BROTLIDEC_INCLUDE_DIRS - the BrotliDec include directories
10+
# BROTLIDEC_LIBRARIES - link these to use BrotliDec.
11+
12+
find_package(PkgConfig)
13+
14+
pkg_check_modules(PC_BROTLIDEC libbrotlidec)
15+
16+
find_path(BROTLIDEC_INCLUDE_DIRS
17+
NAMES brotli/decode.h
18+
HINTS ${PC_BROTLIDEC_INCLUDEDIR}
19+
)
20+
21+
find_library(BROTLIDEC_LIBRARIES
22+
NAMES brotlidec
23+
HINTS ${PC_BROTLIDEC_LIBDIR}
24+
)
25+
26+
include(FindPackageHandleStandardArgs)
27+
find_package_handle_standard_args(BrotliDec
28+
REQUIRED_VARS BROTLIDEC_INCLUDE_DIRS BROTLIDEC_LIBRARIES
29+
FOUND_VAR BROTLIDEC_FOUND
30+
VERSION_VAR PC_BROTLIDEC_VERSION)
31+
32+
mark_as_advanced(
33+
BROTLIDEC_INCLUDE_DIRS
34+
BROTLIDEC_LIBRARIES
35+
)

cmake/FindBrotliEnc.cmake

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Copyright 2017 Igalia S.L. All Rights Reserved.
2+
#
3+
# Distributed under MIT license.
4+
# See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
5+
6+
# Try to find BrotliEnc. Once done, this will define
7+
#
8+
# BROTLIENC_FOUND - system has BrotliEnc.
9+
# BROTLIENC_INCLUDE_DIRS - the BrotliEnc include directories
10+
# BROTLIENC_LIBRARIES - link these to use BrotliEnc.
11+
12+
find_package(PkgConfig)
13+
14+
pkg_check_modules(PC_BROTLIENC libbrotlienc)
15+
16+
find_path(BROTLIENC_INCLUDE_DIRS
17+
NAMES brotli/encode.h
18+
HINTS ${PC_BROTLIENC_INCLUDEDIR}
19+
)
20+
21+
find_library(BROTLIENC_LIBRARIES
22+
NAMES brotlienc
23+
HINTS ${PC_BROTLIENC_LIBDIR}
24+
)
25+
26+
include(FindPackageHandleStandardArgs)
27+
find_package_handle_standard_args(BrotliEnc
28+
REQUIRED_VARS BROTLIENC_INCLUDE_DIRS BROTLIENC_LIBRARIES
29+
FOUND_VAR BROTLIENC_FOUND
30+
VERSION_VAR PC_BROTLIENC_VERSION)
31+
32+
mark_as_advanced(
33+
BROTLIENC_INCLUDE_DIRS
34+
BROTLIENC_LIBRARIES
35+
)

0 commit comments

Comments
 (0)