From be6dc3db83c4701e3e79694dcbfd1c3da03b91dd Mon Sep 17 00:00:00 2001 From: costan Date: Fri, 28 Jul 2017 09:55:21 -0700 Subject: [PATCH] Redo CMake configuration. The style was changed to match the official manual [1], the install configuration was simplified and now matches the official packaging guide [2], and the config files use the CMake-specific variable syntax ${VAR} instead of the autoconf-compatible syntax @VAR@, as documented in [3]. The public header files are declared as such (for CMake 3.3+), and the generated headers are included in the library target definition. The tests are only built if SNAPPY_BUILD_TESTS (default ON) is true, so zippy can be easily used in projects that add_subdirectory() its source code directly, instead of using find_package(). [1] https://cmake.org/cmake/help/git-master/manual/cmake-language.7.html [2] https://cmake.org/cmake/help/git-master/manual/cmake-packages.7.html [3] https://cmake.org/cmake/help/git-master/command/configure_file.html --- .appveyor.yml | 4 +- .travis.yml | 4 +- CMakeLists.txt | 262 +++++++++++++++++++----------------- cmake/SnappyConfig.cmake | 1 + cmake/SnappyConfig.cmake.in | 9 -- snappy-stubs-public.h.in | 26 ++-- 6 files changed, 158 insertions(+), 148 deletions(-) create mode 100644 cmake/SnappyConfig.cmake delete mode 100644 cmake/SnappyConfig.cmake.in diff --git a/.appveyor.yml b/.appveyor.yml index f54dbfb..2ed49f9 100644 --- a/.appveyor.yml +++ b/.appveyor.yml @@ -31,11 +31,11 @@ build_script: - mkdir out - cd out - if "%platform%"=="x64" set CMAKE_GENERATOR=%CMAKE_GENERATOR% Win64 + - cmake --version - cmake .. -G "%CMAKE_GENERATOR%" -DCMAKE_CONFIGURATION_TYPES="%CONFIGURATION%" - cmake --build . --config %CONFIGURATION% - cd .. - - cmake --version test_script: - - out\%CONFIGURATION%\snappy-unittest \ No newline at end of file + - out\%CONFIGURATION%\snappy_unittest \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index 65ee97e..9841e9a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -47,13 +47,13 @@ install: - echo ${CC} - echo ${CXX} - ${CXX} --version +- cmake --version before_script: - mkdir -p build && cd build - cmake .. -DCMAKE_BUILD_TYPE=$BUILD_TYPE - cmake --build . - cd .. -- cmake --version script: -- build/snappy-unittest \ No newline at end of file +- build/snappy_unittest \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index e5a3680..11c7b00 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,147 +1,165 @@ -CMAKE_MINIMUM_REQUIRED(VERSION 3.1) -PROJECT(Snappy VERSION 1.1.6 LANGUAGES C CXX) +cmake_minimum_required(VERSION 3.1) +project(Snappy VERSION 1.1.6 LANGUAGES C CXX) # BUILD_SHARED_LIBS is a standard CMake variable, but we declare it here to make # it prominent in the GUI. -OPTION (BUILD_SHARED_LIBS "Build shared libraries (DLLs)." OFF) +option(BUILD_SHARED_LIBS "Build shared libraries(DLLs)." OFF) -SET(CMAKE_INCLUDE_CURRENT_DIR ON) +option(SNAPPY_BUILD_TESTS "Build Snappy's own tests." ON) -INCLUDE(CheckIncludeFiles) -INCLUDE(CheckLibraryExists) -INCLUDE(CheckCXXSourceCompiles) -INCLUDE(CheckSymbolExists) -INCLUDE(TestBigEndian) -INCLUDE(CMakePackageConfigHelpers) +include(TestBigEndian) +test_big_endian(WORDS_BIG_ENDIAN) +if(WORDS_BIG_ENDIAN) + add_definitions(-DWORDS_BIGENDIAN=1) +endif(WORDS_BIG_ENDIAN) -TEST_BIG_ENDIAN(WORDS_BIG_ENDIAN) -IF (WORDS_BIG_ENDIAN) - MESSAGE(STATUS "Building on big endian system") - ADD_DEFINITIONS(-DWORDS_BIGENDIAN=1) -ENDIF (WORDS_BIG_ENDIAN) +include(CheckIncludeFile) +check_include_file("byteswap.h" HAVE_BYTESWAP_H) +check_include_file("stddef.h" HAVE_STDDEF_H) +check_include_file("stdint.h" HAVE_STDINT_H) +check_include_file("sys/endian.h" HAVE_SYS_ENDIAN_H) +check_include_file("sys/mman.h" HAVE_SYS_MMAN_H) +check_include_file("sys/resource.h" HAVE_SYS_RESOURCE_H) +check_include_file("sys/time.h" HAVE_SYS_TIME_H) +check_include_file("sys/uio.h" HAVE_SYS_UIO_H) +check_include_file("windows.h" HAVE_WINDOWS_H) -CHECK_INCLUDE_FILES("byteswap.h" HAVE_BYTESWAP_H) -CHECK_INCLUDE_FILES("stddef.h" HAVE_STDDEF_H) -CHECK_INCLUDE_FILES("stdint.h" HAVE_STDINT_H) -CHECK_INCLUDE_FILES("sys/endian.h" HAVE_SYS_ENDIAN_H) -CHECK_INCLUDE_FILES("sys/mman.h" HAVE_SYS_MMAN_H) -CHECK_INCLUDE_FILES("sys/resource.h" HAVE_SYS_RESOURCE_H) -CHECK_INCLUDE_FILES("sys/time.h" HAVE_SYS_TIME_H) -CHECK_INCLUDE_FILES("sys/uio.h" HAVE_SYS_UIO_H) -CHECK_INCLUDE_FILES("windows.h" HAVE_WINDOWS_H) +include(CheckLibraryExists) +check_library_exists(z zlibVersion "" HAVE_LIBZ) +check_library_exists(lzo2 lzo1x_1_15_compress "" HAVE_LIBLZO2) -IF (NOT HAVE_SYS_UIO_H) - SET(HAVE_SYS_UIO_H 0) -ENDIF (NOT HAVE_SYS_UIO_H) +include(CheckCXXSourceCompiles) +check_cxx_source_compiles( + "int main(void) { return __builtin_expect(0, 1); }" HAVE_BUILTIN_EXPECT) -IF (NOT HAVE_STDINT_H) - SET(HAVE_STDINT_H 0) -ENDIF (NOT HAVE_STDINT_H) +check_cxx_source_compiles( + "int main(void) { return __builtin_ctzll(0); }" HAVE_BUILTIN_CTZ) -IF (NOT HAVE_STDDEF_H) - SET(HAVE_STDDEF_H 0) -ENDIF (NOT HAVE_STDDEF_H) +include(CheckSymbolExists) +check_symbol_exists("mmap" "sys/mman.h" HAVE_FUNC_MMAP) -CHECK_LIBRARY_EXISTS(z zlibVersion "" HAVE_LIBZ) -CHECK_LIBRARY_EXISTS(lzo2 lzo1x_1_15_compress "" HAVE_LIBLZO2) +find_package(GTest QUIET) +if(GTEST_FOUND) + set(HAVE_GTEST 1) +endif(GTEST_FOUND) -CHECK_SYMBOL_EXISTS(mmap sys/mman.h HAVE_FUNC_MMAP) +find_package(Gflags QUIET) +if(GFLAGS_FOUND) + set(HAVE_GFLAGS 1) +endif(GFLAGS_FOUND) -CHECK_CXX_SOURCE_COMPILES("int main(void) { return __builtin_expect(0, 1); }" - HAVE_BUILTIN_EXPECT) +configure_file( + "${PROJECT_SOURCE_DIR}/cmake/config.h.in" + "${PROJECT_BINARY_DIR}/config.h" +) -CHECK_CXX_SOURCE_COMPILES("int main(void) { return __builtin_ctzll(0); }" - HAVE_BUILTIN_CTZ) +# We don't want to define HAVE_ macros in public headers. Instead, we use +# CMake's variable substitution with 0/1 variables, which will be seen by the +# preprocessor as constants. +set(HAVE_STDINT_H_01 ${HAVE_STDINT_H}) +set(HAVE_STDDEF_H_01 ${HAVE_STDDEF_H}) +set(HAVE_SYS_UIO_H_01 ${HAVE_SYS_UIO_H}) +if(NOT HAVE_STDINT_H_01) + set(HAVE_STDINT_H_01 0) +endif(NOT HAVE_STDINT_H_01) +if(NOT HAVE_STDDEF_H_01) + set(HAVE_STDDEF_H_01 0) +endif(NOT HAVE_STDDEF_H_01) +if(NOT HAVE_SYS_UIO_H_01) + set(HAVE_SYS_UIO_H_01 0) +endif(NOT HAVE_SYS_UIO_H_01) -FIND_PACKAGE(GTest QUIET) -IF(GTEST_FOUND) - SET(HAVE_GTEST 1) -ENDIF() +configure_file( + "${PROJECT_SOURCE_DIR}/snappy-stubs-public.h.in" + "${PROJECT_BINARY_DIR}/snappy-stubs-public.h") -FIND_PACKAGE(Gflags QUIET) -IF(GFLAGS_FOUND) - SET(HAVE_GFLAGS 1) -ENDIF() +add_library(snappy "") +target_sources(snappy + PRIVATE + "${PROJECT_SOURCE_DIR}/snappy-internal.h" + "${PROJECT_SOURCE_DIR}/snappy-stubs-internal.h" + "${PROJECT_SOURCE_DIR}/snappy-c.cc" + "${PROJECT_SOURCE_DIR}/snappy-sinksource.cc" + "${PROJECT_SOURCE_DIR}/snappy-stubs-internal.cc" + "${PROJECT_SOURCE_DIR}/snappy.cc" + "${PROJECT_BINARY_DIR}/config.h" -CONFIGURE_FILE(${Snappy_SOURCE_DIR}/cmake/config.h.in config.h) + # Only CMake 3.3+ supports PUBLIC sources in targets exported by "install". + $<$:PUBLIC> + $ + $ + $ + $ + $ + $ + $ + $ +) +target_include_directories(snappy + PUBLIC + $ + $ + $ +) +set_target_properties(snappy + PROPERTIES VERSION ${PROJECT_VERSION} SOVERSION ${PROJECT_VERSION_MAJOR}) -# Configure snappy-stubs-public.h.in -SET(ac_cv_have_stdint_h ${HAVE_STDINT_H}) -SET(ac_cv_have_stddef_h ${HAVE_STDDEF_H}) -SET(ac_cv_have_sys_uio_h ${HAVE_SYS_UIO_H}) -CONFIGURE_FILE(${Snappy_SOURCE_DIR}/snappy-stubs-public.h.in - snappy-stubs-public.h) +target_compile_definitions(snappy PRIVATE -DHAVE_CONFIG_H) +if(BUILD_SHARED_LIBS) + set_target_properties(snappy PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON) +endif(BUILD_SHARED_LIBS) -IF (WIN32) - ADD_DEFINITIONS(-D_CRT_SECURE_NO_WARNINGS) -ENDIF (WIN32) +if(SNAPPY_BUILD_TESTS) + enable_testing() -# Define the main library. -ADD_LIBRARY(snappy - snappy-c.cc - snappy-c.h - snappy-sinksource.cc - snappy-sinksource.h - snappy-stubs-internal.cc - snappy-stubs-public.h - snappy.cc - snappy.h) + add_executable(snappy_unittest "") + target_sources(snappy_unittest + PRIVATE + "${PROJECT_SOURCE_DIR}/snappy_unittest.cc" + "${PROJECT_SOURCE_DIR}/snappy-test.cc" + ) + target_compile_definitions(snappy_unittest PRIVATE -DHAVE_CONFIG_H) + target_link_libraries(snappy_unittest snappy ${GFLAGS_LIBRARIES}) -TARGET_COMPILE_DEFINITIONS(snappy PRIVATE -DHAVE_CONFIG_H) + if(HAVE_LIBZ) + target_link_libraries(snappy_unittest z) + endif(HAVE_LIBZ) + if(HAVE_LIBLZO2) + target_link_libraries(snappy_unittest lzo2) + endif(HAVE_LIBLZO2) -IF (BUILD_SHARED_LIBS) - SET_TARGET_PROPERTIES(snappy PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON) -ENDIF (BUILD_SHARED_LIBS) + target_include_directories(snappy_unittest + BEFORE PRIVATE + "${PROJECT_SOURCE_DIR}" + "${GTEST_INCLUDE_DIRS}" + "${GFLAGS_INCLUDE_DIRS}" + ) -INSTALL(FILES snappy.h - snappy-c.h - snappy-sinksource.h - ${Snappy_BINARY_DIR}/snappy-stubs-public.h - DESTINATION include) + add_test( + NAME snappy_unittest + WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}" + COMMAND "${PROJECT_BINARY_DIR}/snappy_unittest") +endif(SNAPPY_BUILD_TESTS) -INSTALL(TARGETS snappy - EXPORT SnappyTargets - RUNTIME DESTINATION bin - LIBRARY DESTINATION lib - ARCHIVE DESTINATION lib) -INSTALL(EXPORT SnappyTargets NAMESPACE Snappy:: DESTINATION lib/cmake/Snappy) +install(TARGETS snappy EXPORT SnappyTargets DESTINATION lib) +install( + FILES + "${PROJECT_SOURCE_DIR}/snappy-c.h" + "${PROJECT_SOURCE_DIR}/snappy-sinksource.h" + "${PROJECT_SOURCE_DIR}/snappy.h" + "${PROJECT_BINARY_DIR}/snappy-stubs-public.h" + DESTINATION include +) -SET_TARGET_PROPERTIES(snappy PROPERTIES VERSION ${PROJECT_VERSION} - SOVERSION ${PROJECT_VERSION_MAJOR}) - -SET(INCLUDE_INSTALL_DIR include) -SET(LIBRARY_INSTALL_DIR lib) -SET(BINARY_INSTALL_DIR bin) - -CONFIGURE_PACKAGE_CONFIG_FILE(cmake/SnappyConfig.cmake.in - ${Snappy_BINARY_DIR}/SnappyConfig.cmake - INSTALL_DESTINATION lib/Snappy/cmake - PATH_VARS INCLUDE_INSTALL_DIR LIBRARY_INSTALL_DIR BINARY_INSTALL_DIR - ) - -WRITE_BASIC_PACKAGE_VERSION_FILE(${Snappy_BINARY_DIR}/SnappyConfigVersion.cmake - COMPATIBILITY SameMajorVersion) -INSTALL(FILES ${Snappy_BINARY_DIR}/SnappyConfig.cmake - ${Snappy_BINARY_DIR}/SnappyConfigVersion.cmake - DESTINATION lib/cmake) - -ENABLE_TESTING() - -IF (HAVE_LIBZ) - LIST(APPEND COMPRESSION_LIBS z) -ENDIF (HAVE_LIBZ) - -IF (HAVE_LIBLZO2) - LIST(APPEND COMPRESSION_LIBS lzo2) -ENDIF (HAVE_LIBLZO2) - -ADD_EXECUTABLE(snappy-unittest snappy_unittest.cc snappy-test.cc) -TARGET_COMPILE_DEFINITIONS(snappy-unittest PRIVATE -DHAVE_CONFIG_H) -TARGET_LINK_LIBRARIES(snappy-unittest snappy ${COMPRESSION_LIBS} - ${GFLAGS_LIBRARIES}) -TARGET_INCLUDE_DIRECTORIES(snappy-unittest BEFORE PRIVATE ${Snappy_SOURCE_DIR} - ${GTEST_INCLUDE_DIRS} ${GFLAGS_INCLUDE_DIRS}) - -ADD_TEST(NAME snappy-unittest - WORKING_DIRECTORY ${Snappy_SOURCE_DIR} - COMMAND ${Snappy_BINARY_DIR}/snappy-unittest) +include(CMakePackageConfigHelpers) +write_basic_package_version_file( + "${PROJECT_BINARY_DIR}/SnappyConfigVersion.cmake" + COMPATIBILITY SameMajorVersion +) +install(EXPORT SnappyTargets NAMESPACE Snappy:: DESTINATION lib/cmake/Snappy) +install( + FILES + "${PROJECT_SOURCE_DIR}/cmake/SnappyConfig.cmake" + "${PROJECT_BINARY_DIR}/SnappyConfigVersion.cmake" + DESTINATION lib/cmake/Snappy +) diff --git a/cmake/SnappyConfig.cmake b/cmake/SnappyConfig.cmake new file mode 100644 index 0000000..dc308ae --- /dev/null +++ b/cmake/SnappyConfig.cmake @@ -0,0 +1 @@ +include("${CMAKE_CURRENT_LIST_DIR}/SnappyTargets.cmake") \ No newline at end of file diff --git a/cmake/SnappyConfig.cmake.in b/cmake/SnappyConfig.cmake.in deleted file mode 100644 index 5e604fe..0000000 --- a/cmake/SnappyConfig.cmake.in +++ /dev/null @@ -1,9 +0,0 @@ -set(SNAPPY_VERSION @SNAPPY_MAJOR@.@SNAPPY_MINOR@.@SNAPPY_PATCHLEVEL@) - -@PACKAGE_INIT@ - -set_and_check(SNAPPY_INCLUDE_DIR "@PACKAGE_INCLUDE_INSTALL_DIR@") -set_and_check(SNAPPY_LIBRARY_DIR "@PACKAGE_LIBRARY_INSTALL_DIR@") -set_and_check(SNAPPY_BINARY_DIR "@PACKAGE_BINARY_INSTALL_DIR@") - -check_required_components(SNAPPY) \ No newline at end of file diff --git a/snappy-stubs-public.h.in b/snappy-stubs-public.h.in index 15f5c9e..3fd79bb 100644 --- a/snappy-stubs-public.h.in +++ b/snappy-stubs-public.h.in @@ -36,21 +36,21 @@ #ifndef THIRD_PARTY_SNAPPY_OPENSOURCE_SNAPPY_STUBS_PUBLIC_H_ #define THIRD_PARTY_SNAPPY_OPENSOURCE_SNAPPY_STUBS_PUBLIC_H_ -#if @ac_cv_have_stdint_h@ +#if ${HAVE_STDINT_H_01} // HAVE_STDINT_H #include -#endif +#endif // HAVE_STDDEF_H -#if @ac_cv_have_stddef_h@ +#if ${HAVE_STDDEF_H_01} // HAVE_STDDEF_H #include -#endif +#endif // HAVE_STDDEF_H -#if @ac_cv_have_sys_uio_h@ +#if ${HAVE_SYS_UIO_H_01} // HAVE_SYS_UIO_H #include -#endif +#endif // HAVE_SYS_UIO_H -#define SNAPPY_MAJOR @SNAPPY_MAJOR@ -#define SNAPPY_MINOR @SNAPPY_MINOR@ -#define SNAPPY_PATCHLEVEL @SNAPPY_PATCHLEVEL@ +#define SNAPPY_MAJOR ${SNAPPY_MAJOR} +#define SNAPPY_MINOR ${SNAPPY_MINOR} +#define SNAPPY_PATCHLEVEL ${SNAPPY_PATCHLEVEL} #define SNAPPY_VERSION \ ((SNAPPY_MAJOR << 16) | (SNAPPY_MINOR << 8) | SNAPPY_PATCHLEVEL) @@ -58,7 +58,7 @@ namespace snappy { -#if @ac_cv_have_stdint_h@ +#if ${HAVE_STDINT_H_01} // HAVE_STDINT_H typedef int8_t int8; typedef uint8_t uint8; typedef int16_t int16; @@ -76,18 +76,18 @@ typedef int int32; typedef unsigned int uint32; typedef long long int64; typedef unsigned long long uint64; -#endif +#endif // HAVE_STDINT_H typedef std::string string; -#if !@ac_cv_have_sys_uio_h@ +#if !${HAVE_SYS_UIO_H_01} // !HAVE_SYS_UIO_H // Windows does not have an iovec type, yet the concept is universally useful. // It is simple to define it ourselves, so we put it inside our own namespace. struct iovec { void* iov_base; size_t iov_len; }; -#endif +#endif // !HAVE_SYS_UIO_H } // namespace snappy