Skip to content

Commit

Permalink
simplify CMake build
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 552238545
  • Loading branch information
eustas authored and copybara-github committed Jul 30, 2023
1 parent 0300be3 commit 27a9a80
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 275 deletions.
80 changes: 21 additions & 59 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,55 +50,23 @@ mark_as_advanced(BROTLI_BUNDLED_MODE)

include(GNUInstallDirs)

# Parse version information from common/version.h. Normally we would
# define these values here and write them out to configuration file(s)
# (i.e., config.h), but in this case we parse them from
# common/version.h to be less intrusive.
function(hex_to_dec HEXADECIMAL DECIMAL)
string(TOUPPER "${HEXADECIMAL}" _tail)
set(_decimal 0)
string(LENGTH "${_tail}" _tail_length)
while (_tail_length GREATER 0)
math(EXPR _decimal "${_decimal} * 16")
string(SUBSTRING "${_tail}" 0 1 _digit)
string(SUBSTRING "${_tail}" 1 -1 _tail)
if (_digit STREQUAL "A")
math(EXPR _decimal "${_decimal} + 10")
elseif (_digit STREQUAL "B")
math(EXPR _decimal "${_decimal} + 11")
elseif (_digit STREQUAL "C")
math(EXPR _decimal "${_decimal} + 12")
elseif (_digit STREQUAL "D")
math(EXPR _decimal "${_decimal} + 13")
elseif (_digit STREQUAL "E")
math(EXPR _decimal "${_decimal} + 14")
elseif (_digit STREQUAL "F")
math(EXPR _decimal "${_decimal} + 15")
else()
math(EXPR _decimal "${_decimal} + ${_digit}")
endif()
string(LENGTH "${_tail}" _tail_length)
endwhile()
set(${DECIMAL} ${_decimal} PARENT_SCOPE)
endfunction(hex_to_dec)
# Reads macro from .h file; it is expected to be a single-line define.
function(read_macro PATH MACRO OUTPUT)
file(STRINGS ${PATH} _line REGEX "^#define +${MACRO} +(.+)$")
string(REGEX REPLACE "^#define +${MACRO} +(.+)$" "\\1" _val "${_line}")
set(${OUTPUT} ${_val} PARENT_SCOPE)
endfunction(read_macro)

# Version information
file(STRINGS "c/common/version.h" _brotli_version_line REGEX "^#define BROTLI_VERSION (0x[0-9a-fA-F]+)$")
string(REGEX REPLACE "^#define BROTLI_VERSION 0x([0-9a-fA-F]+)$" "\\1" _brotli_version_hex "${_brotli_version_line}")
hex_to_dec("${_brotli_version_hex}" _brotli_version)
math(EXPR BROTLI_VERSION_MAJOR "${_brotli_version} >> 24")
math(EXPR BROTLI_VERSION_MINOR "(${_brotli_version} >> 12) & 4095")
math(EXPR BROTLI_VERSION_PATCH "${_brotli_version} & 4095")
set(BROTLI_VERSION "${BROTLI_VERSION_MAJOR}.${BROTLI_VERSION_MINOR}.${BROTLI_VERSION_PATCH}")
read_macro("c/common/version.h" "BROTLI_VERSION_MAJOR" BROTLI_VERSION_MAJOR)
read_macro("c/common/version.h" "BROTLI_VERSION_MINOR" BROTLI_VERSION_MINOR)
read_macro("c/common/version.h" "BROTLI_VERSION_PATCH" BROTLI_VERSION_PATCH)
mark_as_advanced(BROTLI_VERSION BROTLI_VERSION_MAJOR BROTLI_VERSION_MINOR BROTLI_VERSION_PATCH)

# ABI Version information
file(STRINGS "c/common/version.h" _brotli_abi_info_line REGEX "^#define BROTLI_ABI_VERSION (0x[0-9a-fA-F]+)$")
string(REGEX REPLACE "^#define BROTLI_ABI_VERSION 0x([0-9a-fA-F]+)$" "\\1" _brotli_abi_info_hex "${_brotli_abi_info_line}")
hex_to_dec("${_brotli_abi_info_hex}" _brotli_abi_info)
math(EXPR BROTLI_ABI_CURRENT "${_brotli_abi_info} >> 24")
math(EXPR BROTLI_ABI_REVISION "(${_brotli_abi_info} >> 12) & 4095")
math(EXPR BROTLI_ABI_AGE "${_brotli_abi_info} & 4095")
read_macro("c/common/version.h" "BROTLI_ABI_CURRENT" BROTLI_ABI_CURRENT)
read_macro("c/common/version.h" "BROTLI_ABI_REVISION" BROTLI_ABI_REVISION)
read_macro("c/common/version.h" "BROTLI_ABI_AGE" BROTLI_ABI_AGE)
math(EXPR BROTLI_ABI_COMPATIBILITY "${BROTLI_ABI_CURRENT} - ${BROTLI_ABI_AGE}")
mark_as_advanced(BROTLI_ABI_CURRENT BROTLI_ABI_REVISION BROTLI_ABI_AGE BROTLI_ABI_COMPATIBILITY)

Expand Down Expand Up @@ -147,24 +115,18 @@ elseif(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
set(CMAKE_INSTALL_NAME_DIR "${CMAKE_INSTALL_PREFIX}/lib")
endif()

function(transform_sources_list INPUT_FILE OUTPUT_FILE)
file(READ ${INPUT_FILE} TEXT)
string(REGEX REPLACE "\\\\\n" "~continuation~" TEXT ${TEXT})
string(REGEX REPLACE "([a-zA-Z_][a-zA-Z0-9_]*)[\t ]*=[\t ]*([^\n]*)" "SET(\\1 \\2)" TEXT ${TEXT})
string(REPLACE "~continuation~" "\n" TEXT ${TEXT})
file(WRITE ${OUTPUT_FILE} ${TEXT})
endfunction()

transform_sources_list("scripts/sources.lst" "${CMAKE_CURRENT_BINARY_DIR}/sources.lst.cmake")
include("${CMAKE_CURRENT_BINARY_DIR}/sources.lst.cmake")

if(BROTLI_EMSCRIPTEN)
set(BUILD_SHARED_LIBS OFF)
endif()

add_library(brotlicommon ${BROTLI_COMMON_C})
add_library(brotlidec ${BROTLI_DEC_C})
add_library(brotlienc ${BROTLI_ENC_C})
file(GLOB_RECURSE BROTLI_COMMON_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} c/common/*.c)
add_library(brotlicommon ${BROTLI_COMMON_SOURCES})

file(GLOB_RECURSE BROTLI_DEC_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} c/dec/*.c)
add_library(brotlidec ${BROTLI_DEC_SOURCES})

file(GLOB_RECURSE BROTLI_ENC_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} c/enc/*.c)
add_library(brotlienc ${BROTLI_ENC_SOURCES})

# Older CMake versions does not understand INCLUDE_DIRECTORIES property.
include_directories(${BROTLI_INCLUDE_DIRS})
Expand Down Expand Up @@ -206,7 +168,7 @@ if(BROTLI_PARENT_DIRECTORY)
endif()

# Build the brotli executable
add_executable(brotli ${BROTLI_CLI_C})
add_executable(brotli c/tools/brotli.c)
target_link_libraries(brotli ${BROTLI_LIBRARIES})

# Installation
Expand Down
25 changes: 19 additions & 6 deletions c/common/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,31 @@
#ifndef BROTLI_COMMON_VERSION_H_
#define BROTLI_COMMON_VERSION_H_

/* This macro should only be used when library is compiled together with client.
If library is dynamically linked, use BrotliDecoderVersion and
/* Compose 3 components into a single number. In a hexadecimal representation
B and C components occupy exactly 3 digits. */
#define BROTLI_MAKE_HEX_VERSION(A, B, C) ((A << 24) | (B << 12) | C)

/* Those macros should only be used when library is compiled together with
the client. If library is dynamically linked, use BrotliDecoderVersion and
BrotliEncoderVersion methods. */

/* Semantic version, calculated as (MAJOR << 24) | (MINOR << 12) | PATCH */
#define BROTLI_VERSION 0x1000009
#define BROTLI_VERSION_MAJOR 1
#define BROTLI_VERSION_MINOR 0
#define BROTLI_VERSION_PATCH 9

#define BROTLI_VERSION BROTLI_MAKE_HEX_VERSION( \
BROTLI_VERSION_MAJOR, BROTLI_VERSION_MINOR, BROTLI_VERSION_PATCH)

/* This macro is used by build system to produce Libtool-friendly soname. See
https://www.gnu.org/software/libtool/manual/html_node/Libtool-versioning.html
Version evolution rules:
- interfaces added (or change is compatible) -> current+1:0:age+1
- interfaces removed (or changed is incompatible) -> current+1:0:0
- interfaces not changed -> current:revision+1:age
*/

/* ABI version, calculated as (CURRENT << 24) | (REVISION << 12) | AGE */
#define BROTLI_ABI_VERSION 0x1009000
#define BROTLI_ABI_CURRENT 1
#define BROTLI_ABI_REVISION 9
#define BROTLI_ABI_AGE 0

#endif /* BROTLI_COMMON_VERSION_H_ */
2 changes: 1 addition & 1 deletion c/include/brotli/decode.h
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ BROTLI_DEC_API const char* BrotliDecoderErrorString(BrotliDecoderErrorCode c);
/**
* Gets a decoder library version.
*
* Look at BROTLI_VERSION for more information.
* Look at BROTLI_MAKE_HEX_VERSION for more information.
*/
BROTLI_DEC_API uint32_t BrotliDecoderVersion(void);

Expand Down
2 changes: 1 addition & 1 deletion c/include/brotli/encode.h
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ BROTLI_ENC_EXTRA_API size_t BrotliEncoderGetPreparedDictionarySize(
/**
* Gets an encoder library version.
*
* Look at BROTLI_VERSION for more information.
* Look at BROTLI_MAKE_HEX_VERSION for more information.
*/
BROTLI_ENC_API uint32_t BrotliEncoderVersion(void);

Expand Down
6 changes: 3 additions & 3 deletions c/tools/brotli.c
Original file line number Diff line number Diff line change
Expand Up @@ -561,9 +561,9 @@ static Command ParseParams(Context* params) {
}

static void PrintVersion(void) {
int major = BROTLI_VERSION >> 24;
int minor = (BROTLI_VERSION >> 12) & 0xFFF;
int patch = BROTLI_VERSION & 0xFFF;
int major = BROTLI_VERSION_MAJOR;
int minor = BROTLI_VERSION_MINOR;
int patch = BROTLI_VERSION_PATCH;
fprintf(stdout, "brotli %d.%d.%d\n", major, minor, patch);
}

Expand Down
2 changes: 1 addition & 1 deletion docs/decode.h.3
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ pointer to output data
.SS "uint32_t BrotliDecoderVersion (void)"

.PP
Gets a decoder library version\&. Look at BROTLI_VERSION for more information\&.
Gets a decoder library version\&. Look at BROTLI_MAKE_HEX_VERSION for more information\&.
.SH "Author"
.PP
Generated automatically by Doxygen for Brotli from the source code\&.
2 changes: 1 addition & 1 deletion docs/encode.h.3
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ pointer to output data
.SS "uint32_t BrotliEncoderVersion (void)"

.PP
Gets an encoder library version\&. Look at BROTLI_VERSION for more information\&.
Gets an encoder library version\&. Look at BROTLI_MAKE_HEX_VERSION for more information\&.
.SH "Author"
.PP
Generated automatically by Doxygen for Brotli from the source code\&.
111 changes: 0 additions & 111 deletions scripts/sources.lst

This file was deleted.

Loading

0 comments on commit 27a9a80

Please sign in to comment.