define cppformat cmake targets with proper interface definitions

This commit is contained in:
Mario Werner 2016-01-29 13:18:02 +01:00
parent 0fb474be3a
commit b52d0bd9d4
2 changed files with 63 additions and 56 deletions

View File

@ -19,16 +19,30 @@ option(FMT_TEST "Generate the test target." ON)
project(FORMAT) project(FORMAT)
# starting with cmake 3.0 VERSION is part of the project command
set(CPPFORMAT_VERSION 2.1.0)
if (NOT CPPFORMAT_VERSION MATCHES "^([0-9]+).([0-9]+).([0-9]+)$")
message(FATAL_ERROR "Invalid version format ${CPPFORMAT_VERSION}.")
endif ()
set(CPACK_PACKAGE_VERSION_MAJOR ${CMAKE_MATCH_1})
set(CPACK_PACKAGE_VERSION_MINOR ${CMAKE_MATCH_2})
set(CPACK_PACKAGE_VERSION_PATCH ${CMAKE_MATCH_3})
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}") message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH}
"${CMAKE_CURRENT_SOURCE_DIR}/support/cmake") "${CMAKE_CURRENT_SOURCE_DIR}/support/cmake")
include(testCxx11) include(testCxx11)
if (CMAKE_COMPILER_IS_GNUCXX OR (CMAKE_CXX_COMPILER_ID MATCHES "Clang"))
set(PEDANTIC_COMPILE_FLAGS -Wall -Wextra -Wshadow -pedantic)
elseif (MSVC)
set(PEDANTIC_COMPILE_FLAGS /W4)
endif ()
if (CMAKE_GENERATOR MATCHES "Visual Studio") if (CMAKE_GENERATOR MATCHES "Visual Studio")
# If Microsoft SDK is installed create script run-msbuild.bat that # If Microsoft SDK is installed create script run-msbuild.bat that
# calls SetEnv.cmd to to set up build environment and runs msbuild. # calls SetEnv.cmd to to set up build environment and runs msbuild.
@ -45,63 +59,62 @@ if (CMAKE_GENERATOR MATCHES "Visual Studio")
${CMAKE_MAKE_PROGRAM} -p:FrameworkPathOverride=\"${netfxpath}\" %*") ${CMAKE_MAKE_PROGRAM} -p:FrameworkPathOverride=\"${netfxpath}\" %*")
endif () endif ()
set(FMT_SOURCES cppformat/format.cc cppformat/format.h)
include(CheckSymbolExists) include(CheckSymbolExists)
if (WIN32) if (WIN32)
check_symbol_exists(open io.h HAVE_OPEN) check_symbol_exists(open io.h HAVE_OPEN)
else () else ()
check_symbol_exists(open fcntl.h HAVE_OPEN) check_symbol_exists(open fcntl.h HAVE_OPEN)
endif () endif ()
if (HAVE_OPEN)
add_definitions(-DFMT_USE_FILE_DESCRIPTORS=1)
set(FMT_SOURCES ${FMT_SOURCES} cppformat/posix.cc cppformat/posix.h)
endif ()
if (CPP11_FLAG)
set(CMAKE_REQUIRED_FLAGS ${CPP11_FLAG})
endif ()
if (BIICODE) if (BIICODE)
include(support/cmake/biicode.cmake) include(support/cmake/biicode.cmake)
return() return()
endif () endif ()
#------------------------------------------------------------------------------
# define the cppformat library, its includes and the needed defines
set(FMT_SOURCES cppformat/format.cc cppformat/format.h)
if (HAVE_OPEN)
set(FMT_SOURCES ${FMT_SOURCES} cppformat/posix.cc cppformat/posix.h)
endif ()
add_library(cppformat ${FMT_SOURCES}) add_library(cppformat ${FMT_SOURCES})
target_compile_options(cppformat PUBLIC ${CPP11_FLAG}) # starting with cmake 3.1 the CXX_STANDARD property can be used
if (FMT_PEDANTIC)
target_compile_options(cppformat PRIVATE ${PEDANTIC_COMPILE_FLAGS})
endif ()
target_compile_definitions(cppformat INTERFACE
FMT_USE_FILE_DESCRIPTORS=$<BOOL:${HAVE_OPEN}>)
target_include_directories(cppformat INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<INSTALL_INTERFACE:include>)
set_target_properties(cppformat PROPERTIES
VERSION ${CPPFORMAT_VERSION} SOVERSION ${CPACK_PACKAGE_VERSION_MAJOR})
if (BUILD_SHARED_LIBS) if (BUILD_SHARED_LIBS)
if (UNIX AND NOT APPLE) if (UNIX AND NOT APPLE)
# Fix rpmlint warning: # Fix rpmlint warning:
# unused-direct-shlib-dependency /usr/lib/libformat.so.1.1.0 /lib/libm.so.6. # unused-direct-shlib-dependency /usr/lib/libformat.so.1.1.0 /lib/libm.so.6.
target_link_libraries(cppformat -Wl,--as-needed) target_link_libraries(cppformat -Wl,--as-needed)
endif () endif ()
set(FMT_EXTRA_COMPILE_FLAGS -DFMT_EXPORT) target_compile_definitions(cppformat PRIVATE FMT_EXPORT INTERFACE FMT_SHARED)
endif () endif ()
if (FMT_PEDANTIC AND #------------------------------------------------------------------------------
(CMAKE_COMPILER_IS_GNUCXX OR (CMAKE_CXX_COMPILER_ID MATCHES "Clang"))) # additionally define a header only library when cmake is new enough
set(FMT_EXTRA_COMPILE_FLAGS if (CMAKE_VERSION VERSION_GREATER 3.1.0 OR CMAKE_VERSION VERSION_EQUAL 3.1.0)
"${FMT_EXTRA_COMPILE_FLAGS} -Wall -Wextra -Wshadow -pedantic") add_library(cppformat-header-only INTERFACE)
endif ()
# If FMT_PEDANTIC is TRUE, then test compilation with both -std=c++11 target_compile_definitions(cppformat-header-only INTERFACE FMT_HEADER_ONLY=1)
# and the default flags. Otherwise use only the default flags.
# The library is distributed in the source form and users have full control
# over compile options, so the options used here only matter for testing.
if (CPP11_FLAG AND FMT_PEDANTIC)
set(FMT_EXTRA_COMPILE_FLAGS "${FMT_EXTRA_COMPILE_FLAGS} ${CPP11_FLAG}")
set(FMT_TEST_DEFAULT_FLAGS TRUE)
endif ()
set_target_properties(cppformat target_include_directories(cppformat-header-only INTERFACE
PROPERTIES COMPILE_FLAGS "${FMT_EXTRA_COMPILE_FLAGS}") $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>
$<INSTALL_INTERFACE:include>)
set(CPPFORMAT_VERSION 2.1.0)
if (NOT CPPFORMAT_VERSION MATCHES "^([0-9]+).([0-9]+).([0-9]+)$")
message(FATAL_ERROR "Invalid version format ${CPPFORMAT_VERSION}.")
endif () endif ()
set(CPACK_PACKAGE_VERSION_MAJOR ${CMAKE_MATCH_1})
set(CPACK_PACKAGE_VERSION_MINOR ${CMAKE_MATCH_2})
set(CPACK_PACKAGE_VERSION_PATCH ${CMAKE_MATCH_3})
if (FMT_DOC) if (FMT_DOC)
add_subdirectory(doc) add_subdirectory(doc)
@ -112,10 +125,9 @@ if (FMT_TEST)
add_subdirectory(test) add_subdirectory(test)
endif () endif ()
set_target_properties(cppformat PROPERTIES
VERSION ${CPPFORMAT_VERSION} SOVERSION ${CPACK_PACKAGE_VERSION_MAJOR})
set(gitignore ${CMAKE_CURRENT_SOURCE_DIR}/.gitignore)
set(gitignore ${PROJECT_SOURCE_DIR}/.gitignore)
if (EXISTS ${gitignore}) if (EXISTS ${gitignore})
# Get the list of ignored files from .gitignore. # Get the list of ignored files from .gitignore.
file (STRINGS ${gitignore} lines) file (STRINGS ${gitignore} lines)
@ -132,7 +144,7 @@ if (EXISTS ${gitignore})
set(CPACK_SOURCE_IGNORE_FILES ${ignored_files}) set(CPACK_SOURCE_IGNORE_FILES ${ignored_files})
set(CPACK_SOURCE_PACKAGE_FILE_NAME cppformat-${CPPFORMAT_VERSION}) set(CPACK_SOURCE_PACKAGE_FILE_NAME cppformat-${CPPFORMAT_VERSION})
set(CPACK_PACKAGE_NAME cppformat) set(CPACK_PACKAGE_NAME cppformat)
set(CPACK_RESOURCE_FILE_README ${FORMAT_SOURCE_DIR}/README.rst) set(CPACK_RESOURCE_FILE_README ${PROJECT_SOURCE_DIR}/README.rst)
include(CPack) include(CPack)
endif () endif ()
@ -147,12 +159,6 @@ if (FMT_INSTALL)
set(FMT_LIB_DIR lib CACHE STRING set(FMT_LIB_DIR lib CACHE STRING
"Installation directory for libraries, relative to ${CMAKE_INSTALL_PREFIX}.") "Installation directory for libraries, relative to ${CMAKE_INSTALL_PREFIX}.")
# Add the include directories for both build and install tree.
target_include_directories(
cppformat PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<INSTALL_INTERFACE:include>)
# Generate the version, config and target files into the build directory. # Generate the version, config and target files into the build directory.
write_basic_package_version_file( write_basic_package_version_file(
${version_config} ${version_config}
@ -162,7 +168,11 @@ if (FMT_INSTALL)
support/cmake/cppformat-config.cmake.in support/cmake/cppformat-config.cmake.in
${project_config} ${project_config}
INSTALL_DESTINATION ${config_install_dir}) INSTALL_DESTINATION ${config_install_dir})
export(TARGETS cppformat FILE ${targets_export_name}.cmake) set(CPPFORMAT_LIBRARY_TARGETS cppformat)
if (TARGET cppformat-header-only)
set(CPPFORMAT_LIBRARY_TARGETS ${CPPFORMAT_LIBRARY_TARGETS} cppformat-header-only)
endif ()
export(TARGETS ${CPPFORMAT_LIBRARY_TARGETS} FILE ${targets_export_name}.cmake)
# Install version, config and target files. # Install version, config and target files.
install( install(

View File

@ -102,6 +102,8 @@ target_link_libraries(macro-test gmock)
if (HAVE_OPEN) if (HAVE_OPEN)
add_executable(posix-mock-test posix-mock-test.cc ../cppformat/format.cc ${TEST_MAIN_SRC}) add_executable(posix-mock-test posix-mock-test.cc ../cppformat/format.cc ${TEST_MAIN_SRC})
target_include_directories(posix-mock-test PRIVATE ${PROJECT_SOURCE_DIR})
target_compile_definitions(posix-mock-test PRIVATE FMT_USE_FILE_DESCRIPTORS=1)
target_link_libraries(posix-mock-test gmock) target_link_libraries(posix-mock-test gmock)
add_test(NAME posix-mock-test COMMAND posix-mock-test) add_test(NAME posix-mock-test COMMAND posix-mock-test)
add_fmt_test(posix-test) add_fmt_test(posix-test)
@ -109,9 +111,13 @@ endif ()
add_executable(header-only-test add_executable(header-only-test
header-only-test.cc header-only-test2.cc test-main.cc) header-only-test.cc header-only-test2.cc test-main.cc)
set_target_properties(header-only-test
PROPERTIES COMPILE_DEFINITIONS "FMT_HEADER_ONLY=1")
target_link_libraries(header-only-test gmock) target_link_libraries(header-only-test gmock)
if (TARGET cppformat-header-only)
target_link_libraries(header-only-test cppformat-header-only)
else ()
target_include_directories(header-only-test PRIVATE ${PROJECT_SOURCE_DIR})
target_compile_definitions(header-only-test PRIVATE FMT_HEADER_ONLY=1)
endif ()
# Test that the library can be compiled with exceptions disabled. # Test that the library can be compiled with exceptions disabled.
check_cxx_compiler_flag(-fno-exceptions HAVE_FNO_EXCEPTIONS_FLAG) check_cxx_compiler_flag(-fno-exceptions HAVE_FNO_EXCEPTIONS_FLAG)
@ -121,15 +127,6 @@ if (HAVE_FNO_EXCEPTIONS_FLAG)
PROPERTIES COMPILE_FLAGS -fno-exceptions) PROPERTIES COMPILE_FLAGS -fno-exceptions)
endif () endif ()
# Test compilation with default flags.
if (FMT_TEST_DEFAULT_FLAGS)
file(GLOB src RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cc *.h)
foreach (s ${FMT_SOURCES})
set(src ${src} ../${s})
endforeach ()
add_library(testformat STATIC ${src})
endif ()
if (FMT_PEDANTIC) if (FMT_PEDANTIC)
add_test(compile-test ${CMAKE_CTEST_COMMAND} add_test(compile-test ${CMAKE_CTEST_COMMAND}
--build-and-test --build-and-test