Use version from common/version.h

This commit is contained in:
Eugene Kliuchnikov 2016-08-22 15:44:12 +02:00
parent 313066a037
commit 2c2d5578a6
4 changed files with 52 additions and 14 deletions

View File

@ -22,14 +22,44 @@ if(BROTLI_BUNDLED_MODE STREQUAL "")
endif()
mark_as_advanced(BROTLI_BUNDLED_MODE)
# Parse version information from tools/version.h. Normally we would
# 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
# tools/version.h to be less intrusive.
file(STRINGS "tools/version.h" BROTLI_VERSION REGEX "^#define BROTLI_VERSION \"+([0-9]+)\\.([0-9]+)\\.([0-9]+)\"")
string(REGEX REPLACE "^#define BROTLI_VERSION \"([0-9]+)\\.([0-9]+)\\.([0-9]+)\"$" "\\1" BROTLI_VERSION_MAJOR "${BROTLI_VERSION}")
string(REGEX REPLACE "^#define BROTLI_VERSION \"([0-9]+)\\.([0-9]+)\\.([0-9]+)\"$" "\\2" BROTLI_VERSION_MINOR "${BROTLI_VERSION}")
string(REGEX REPLACE "^#define BROTLI_VERSION \"([0-9]+)\\.([0-9]+)\\.([0-9]+)\"$" "\\3" BROTLI_VERSION_REVISION "${BROTLI_VERSION}")
# 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)
file(STRINGS "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_REVISION "${_brotli_version} & 4095")
mark_as_advanced(BROTLI_VERSION_MAJOR BROTLI_VERSION_MINOR BROTLI_VERSION_REVISION)
if (ENABLE_SANITIZER)

View File

@ -5,6 +5,7 @@ include dec/*.c
include dec/*.h
include enc/*.c
include enc/*.h
include public/*.h
include LICENSE
include MANIFEST.in
include python/bro.py
@ -13,4 +14,3 @@ include python/README.md
include README.md
include setup.py
include tools/bro.c
include tools/version.h

View File

@ -1,10 +1,11 @@
#define PY_SSIZE_T_CLEAN 1
#include <Python.h>
#include <bytesobject.h>
#include <cstdio>
#include <vector>
#include "../common/version.h"
#include "../public/encode.h"
#include "../public/decode.h"
#include "../tools/version.h"
#if PY_MAJOR_VERSION >= 3
#define PyInt_Check PyLong_Check
@ -297,7 +298,10 @@ PyMODINIT_FUNC INIT_BROTLI(void) {
PyModule_AddIntConstant(m, "MODE_TEXT", (int) BROTLI_MODE_TEXT);
PyModule_AddIntConstant(m, "MODE_FONT", (int) BROTLI_MODE_FONT);
PyModule_AddStringConstant(m, "__version__", BROTLI_VERSION);
char version[16];
snprintf(version, sizeof(version), "%d.%d.%d",
BROTLI_VERSION >> 24, (BROTLI_VERSION >> 12) & 0xFFF, BROTLI_VERSION & 0xFFF);
PyModule_AddStringConstant(m, "__version__", version);
RETURN_BROTLI;
}

View File

@ -15,14 +15,17 @@ CURR_DIR = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
def get_version():
""" Return BROTLI_VERSION string as defined in 'tools/version.h' file. """
brotlimodule = os.path.join(CURR_DIR, 'tools', 'version.h')
""" Return BROTLI_VERSION string as defined in 'common/version.h' file. """
brotlimodule = os.path.join(CURR_DIR, 'common', 'version.h')
version = 0
with open(brotlimodule, 'r') as f:
for line in f:
m = re.match(r'#define\sBROTLI_VERSION\s"(.*)"', line)
m = re.match(r'#define\sBROTLI_VERSION\s+0x([0-9a-fA-F])', line)
if m:
return m.group(1)
return ""
version = int(m.group(1), 16)
if version == 0:
return ""
return "{0}.{1}.{2}".format(version >> 24, (version >> 12) & 0xFFF, version & 0xFFF)
class TestCommand(Command):
@ -146,6 +149,7 @@ brotli = Extension("brotli",
"common/constants.h",
"common/dictionary.h",
"common/port.h",
"common/version.h",
"dec/bit_reader.h",
"dec/context.h",
"dec/huffman.h",