diff options
author | Evan Nemerson <evan@nemerson.com> | 2016-11-01 02:03:29 -0700 |
---|---|---|
committer | Eugene Kliuchnikov <eustas@google.com> | 2016-11-01 10:03:29 +0100 |
commit | 6c470098928f6dbd76bca47ac61eaa6b9802ba94 (patch) | |
tree | 4cfc3f282ce632ff7cffd683729aacbfa43a3cf5 | |
parent | e9b278ac6e097beb763e8a46eeafcaa3f6f63f18 (diff) | |
download | brotli-6c470098928f6dbd76bca47ac61eaa6b9802ba94.zip brotli-6c470098928f6dbd76bca47ac61eaa6b9802ba94.tar.gz brotli-6c470098928f6dbd76bca47ac61eaa6b9802ba94.tar.bz2 |
FInishing touches for installing libbrotli with CMake (#464)
* build: fix bundled mode + BUILD_SHARED_LIBS
* cmake: add soversion information
* cmake: generate pkg-config file
-rw-r--r-- | CMakeLists.txt | 148 |
1 files changed, 137 insertions, 11 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index fe9d993..856d0be 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,8 +6,6 @@ cmake_minimum_required(VERSION 2.8.6) project(brotli) -option(BUILD_SHARED_LIBS "Build shared libraries" ON) - # If Brotli is being bundled in another project, we don't want to # install anything. However, we want to let people override this, so # we'll use the BROTLI_BUNDLED_MODE variable to let them do that; just @@ -24,6 +22,11 @@ if(BROTLI_BUNDLED_MODE STREQUAL "") endif() mark_as_advanced(BROTLI_BUNDLED_MODE) +include(CMakeDependentOption) +CMAKE_DEPENDENT_OPTION(BUILD_SHARED_LIBS "Build shared libraries" ON "NOT BROTLI_BUNDLED_MODE" OFF) + +include(GNUInstallDirs) + # When building shared libraries it is important to set the correct rpath. # See https://cmake.org/Wiki/CMake_RPATH_handling#Always_full_RPATH if (BUILD_SHARED_LIBS) @@ -31,9 +34,9 @@ if (BUILD_SHARED_LIBS) set(CMAKE_SKIP_BUILD_RPATH FALSE) set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE) set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) - list(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${CMAKE_INSTALL_PREFIX}/lib" isSystemDir) + list(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${CMAKE_INSTALL_LIBDIR}" isSystemDir) if ("${isSystemDir}" STREQUAL "-1") - set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib") + set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_LIBDIR}") endif() endif() @@ -69,6 +72,7 @@ function(hex_to_dec HEXADECIMAL DECIMAL) set(${DECIMAL} ${_decimal} PARENT_SCOPE) endfunction(hex_to_dec) +# Version information 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) @@ -152,7 +156,8 @@ foreach(lib brotlicommon brotlidec brotlienc) target_link_libraries(${lib} ${LIBM_LIBRARY}) set_property(TARGET ${lib} APPEND PROPERTY INCLUDE_DIRECTORIES ${BROTLI_INCLUDE_DIRS}) set_target_properties(${lib} PROPERTIES - VERSION ${BROTLI_VERSION_MAJOR}.${BROTLI_VERSION_MINOR}.${BROTLI_VERSION_REVISION} + SOVERSION "${BROTLI_VERSION_MAJOR}.${BROTLI_VERSION_MINOR}.${BROTLI_VERSION_REVISION}" + VERSION "${BROTLI_VERSION_MAJOR}.${BROTLI_VERSION_MINOR}.${BROTLI_VERSION_REVISION}" POSITION_INDEPENDENT_CODE TRUE) string(TOUPPER "${lib}" LIB) set_target_properties (${lib} PROPERTIES DEFINE_SYMBOL "${LIB}_SHARED_COMPILATION" ) @@ -160,10 +165,8 @@ foreach(lib brotlicommon brotlidec brotlienc) set_property(TARGET ${lib} APPEND PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${BROTLI_INCLUDE_DIRS}") endforeach() -if (BUILD_SHARED_LIBS) - target_link_libraries(brotlidec brotlicommon) - target_link_libraries(brotlienc brotlicommon) -endif() +target_link_libraries(brotlidec brotlicommon) +target_link_libraries(brotlienc brotlicommon) # For projects stuck on older versions of CMake, this will set the # BROTLI_INCLUDE_DIRS and BROTLI_LIBRARIES variables so they still @@ -182,8 +185,6 @@ target_link_libraries(bro ${BROTLI_LIBRARIES}) # Installation if(NOT BROTLI_BUNDLED_MODE) - include(GNUInstallDirs) - install (TARGETS bro RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}") if(WIN32) @@ -262,3 +263,128 @@ if(NOT BROTLI_DISABLE_TESTS) -P ${CMAKE_CURRENT_SOURCE_DIR}/tests/run-compatibility-test.cmake) endforeach() endif() + +# Generate a pkg-config file + +include(CMakeParseArguments) + +function(generate_pkg_config_path outvar path) + string(LENGTH "${path}" path_length) + + set(path_args ${ARGV}) + list(REMOVE_AT path_args 0 1) + list(LENGTH path_args path_args_remaining) + + set("${outvar}" "${path}") + + while(path_args_remaining GREATER 1) + list(GET path_args 0 name) + list(GET path_args 1 value) + + get_filename_component(value_full "${value}" ABSOLUTE) + string(LENGTH "${value}" value_length) + + if(path_length EQUAL value_length AND path STREQUAL value) + set("${outvar}" "\${${name}}") + break() + elseif(path_length GREATER value_length) + # We might be in a subdirectory of the value, but we have to be + # careful about a prefix matching but not being a subdirectory + # (for example, /usr/lib64 is not a subdirectory of /usr/lib). + # We'll do this by making sure the next character is a directory + # separator. + string(SUBSTRING "${path}" ${value_length} 1 sep) + if(sep STREQUAL "/") + string(SUBSTRING "${path}" 0 ${value_length} s) + if(s STREQUAL value) + string(SUBSTRING "${path}" "${value_length}" -1 suffix) + set("${outvar}" "\${${name}}${suffix}") + break() + endif() + endif() + endif() + + list(REMOVE_AT path_args 0 1) + list(LENGTH path_args path_args_remaining) + endwhile() + + set("${outvar}" "${${outvar}}" PARENT_SCOPE) +endfunction(generate_pkg_config_path) + +function(generate_pkg_config output_file) + set (options) + set (oneValueArgs NAME DESCRIPTION PREFIX LIBDIR INCLUDEDIR VERSION) + set (multiValueArgs DEPENDS CFLAGS LIBRARIES) + cmake_parse_arguments(GEN_PKG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) + unset (options) + unset (oneValueArgs) + unset (multiValueArgs) + + if(NOT GEN_PKG_PREFIX) + set(GEN_PKG_PREFIX "${CMAKE_INSTALL_PREFIX}") + endif() + + if(NOT GEN_PKG_LIBDIR) + set(GEN_PKG_LIBDIR "${CMAKE_INSTALL_FULL_LIBDIR}") + endif() + generate_pkg_config_path(GEN_PKG_LIBDIR "${GEN_PKG_LIBDIR}" + prefix "${GEN_PKG_PREFIX}") + + if(NOT GEN_PKG_INCLUDEDIR) + set(GEN_PKG_INCLUDEDIR "${CMAKE_INSTALL_FULL_INCLUDEDIR}") + endif() + generate_pkg_config_path(GEN_PKG_INCLUDEDIR "${GEN_PKG_INCLUDEDIR}" + prefix "${GEN_PKG_PREFIX}") + + file(WRITE "${output_file}" "prefix=${GEN_PKG_PREFIX}\n") + file(APPEND "${output_file}" "libdir=${GEN_PKG_LIBDIR}\n") + file(APPEND "${output_file}" "includedir=${GEN_PKG_INCLUDEDIR}\n") + file(APPEND "${output_file}" "\n") + + if(GEN_PKG_NAME) + file(APPEND "${output_file}" "Name: ${GEN_PKG_NAME}\n") + else() + file(APPEND "${output_file}" "Name: ${CMAKE_PROJECT_NAME}\n") + endif() + + if(GEN_PKG_DESCRIPTION) + file(APPEND "${output_file}" "Description: ${GEN_PKG_DESCRIPTION}\n") + endif() + + if(GEN_PKG_VERSION) + file(APPEND "${output_file}" "Version: ${GEN_PKG_VERSION}\n") + endif() + + if(GEN_PKG_LIBRARIES) + set(libs) + + file(APPEND "${output_file}" "Libs: -L\${libdir}") + foreach(lib ${GEN_PKG_LIBRARIES}) + file(APPEND "${output_file}" " -l${lib}") + endforeach() + file(APPEND "${output_file}" "\n") + endif() + + file(APPEND "${output_file}" "Cflags: -I\${includedir}") + if(GEN_PKG_CFLAGS) + foreach(cflag ${GEN_PKG_CFLAGS}) + file(APPEND "${output_file}" " ${cflag}") + endforeach() + endif() + file(APPEND "${output_file}" "\n") +endfunction(generate_pkg_config) + +generate_pkg_config ("${CMAKE_CURRENT_BINARY_DIR}/brotli.pc" + NAME Brotli + DESCRIPTION "Brotli compression library" + VERSION "${BROTLI_VERSION_MAJOR}.${BROTLI_VERSION_MINOR}.${BROTLI_VERSION_REVISION}" + LIBRARIES brotlicommon brotlidec brotlienc) + +if(NOT BROTLI_BUNDLED_MODE) + install(FILES "${CMAKE_CURRENT_BINARY_DIR}/brotli.pc" + DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig") +endif() + +if (ENABLE_COVERAGE STREQUAL "yes") + SETUP_TARGET_FOR_COVERAGE(coverage test coverage) +endif () |