Home | History | Annotate | Download | only in fmt
      1 # Define the fmt library, its includes and the needed defines.
      2 # *.cc are added to FMT_HEADERS for the header-only configuration.
      3 set(FMT_HEADERS format.h format.cc ostream.h ostream.cc printf.h printf.cc
      4                 string.h time.h)
      5 if (HAVE_OPEN)
      6   set(FMT_HEADERS ${FMT_HEADERS} posix.h)
      7   set(FMT_SOURCES ${FMT_SOURCES} posix.cc)
      8 endif ()
      9 
     10 add_library(fmt ${FMT_SOURCES} ${FMT_HEADERS} ../README.rst ../ChangeLog.rst)
     11 
     12 option(FMT_CPPFORMAT "Build cppformat library for backward compatibility." OFF)
     13 if (FMT_CPPFORMAT)
     14   message(WARNING "The cppformat library is deprecated, use fmt instead.")
     15   add_library(cppformat ${FMT_SOURCES} ${FMT_HEADERS})
     16 endif ()
     17 
     18 # Starting with cmake 3.1 the CXX_STANDARD property can be used instead.
     19 target_compile_options(fmt PUBLIC ${CPP11_FLAG})
     20 if (FMT_PEDANTIC)
     21   target_compile_options(fmt PRIVATE ${PEDANTIC_COMPILE_FLAGS})
     22 endif ()
     23 
     24 target_include_directories(fmt PUBLIC
     25   $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>
     26   $<INSTALL_INTERFACE:include>)
     27 
     28 set_target_properties(fmt PROPERTIES
     29   VERSION ${FMT_VERSION} SOVERSION ${CPACK_PACKAGE_VERSION_MAJOR})
     30 
     31 if (BUILD_SHARED_LIBS)
     32   if (UNIX AND NOT APPLE)
     33     # Fix rpmlint warning:
     34     # unused-direct-shlib-dependency /usr/lib/libformat.so.1.1.0 /lib/libm.so.6.
     35     target_link_libraries(fmt -Wl,--as-needed)
     36   endif ()
     37   target_compile_definitions(fmt PRIVATE FMT_EXPORT INTERFACE FMT_SHARED)
     38 endif ()
     39 
     40 #------------------------------------------------------------------------------
     41 # additionally define a header only library when cmake is new enough
     42 if (CMAKE_VERSION VERSION_GREATER 3.1.0 OR CMAKE_VERSION VERSION_EQUAL 3.1.0)
     43   add_library(fmt-header-only INTERFACE)
     44 
     45   target_compile_definitions(fmt-header-only INTERFACE FMT_HEADER_ONLY=1)
     46 
     47   target_include_directories(fmt-header-only INTERFACE
     48     $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>
     49     $<INSTALL_INTERFACE:include>)
     50 endif ()
     51 
     52 # Install targets.
     53 if (FMT_INSTALL)
     54   include(CMakePackageConfigHelpers)
     55   set(FMT_CMAKE_DIR lib/cmake/fmt CACHE STRING
     56     "Installation directory for cmake files, relative to ${CMAKE_INSTALL_PREFIX}.")
     57   set(version_config ${PROJECT_BINARY_DIR}/fmt-config-version.cmake)
     58   set(project_config ${PROJECT_BINARY_DIR}/fmt-config.cmake)
     59   set(targets_export_name fmt-targets)
     60 
     61   set (INSTALL_TARGETS fmt)
     62   if (TARGET fmt-header-only)
     63     set(INSTALL_TARGETS ${INSTALL_TARGETS} fmt-header-only)
     64   endif ()
     65 
     66   set(FMT_LIB_DIR lib CACHE STRING
     67     "Installation directory for libraries, relative to ${CMAKE_INSTALL_PREFIX}.")
     68 
     69   # Generate the version, config and target files into the build directory.
     70   write_basic_package_version_file(
     71     ${version_config}
     72     VERSION ${FMT_VERSION}
     73     COMPATIBILITY AnyNewerVersion)
     74   configure_package_config_file(
     75     ${PROJECT_SOURCE_DIR}/support/cmake/fmt-config.cmake.in
     76     ${project_config}
     77     INSTALL_DESTINATION ${FMT_CMAKE_DIR})
     78   export(TARGETS ${INSTALL_TARGETS}
     79          FILE ${PROJECT_BINARY_DIR}/${targets_export_name}.cmake)
     80 
     81   # Install version, config and target files.
     82   install(
     83     FILES ${project_config} ${version_config}
     84     DESTINATION ${FMT_CMAKE_DIR})
     85   install(EXPORT ${targets_export_name} DESTINATION ${FMT_CMAKE_DIR})
     86 
     87   # Install the library and headers.
     88   install(TARGETS ${INSTALL_TARGETS} EXPORT ${targets_export_name}
     89           DESTINATION ${FMT_LIB_DIR})
     90   install(FILES ${FMT_HEADERS} DESTINATION include/fmt)
     91   if (FMT_CPPFORMAT)
     92     install(TARGETS cppformat DESTINATION ${FMT_LIB_DIR})
     93   endif ()
     94 endif ()
     95