CMake: Improve debug library handling for MSVC, fix pkg-config var substitution
Add FindEXPAT because the target export functionality was not shipped before CMake 3.10
MODIFY
CMakeLists.txt
CMakeLists.txt
@@ -5,6 +5,15 @@ project(liblcf VERSION 0.5.3 LANGUAGES CXX)
option(DISABLE_ICU "Disable ICU encoding detection (fallback to iconv)" OFF)
option(DISABLE_XML "Disable XML reading support (expat)" OFF)
+if(MSVC)
+ set(DEBUG_POSTFIX "d")
+else()
+ set(DEBUG_POSTFIX "")
+endif()
+mark_as_advanced(DEBUG_POSTFIX)
+
+set(CMAKE_DEBUG_POSTFIX ${DEBUG_POSTFIX} CACHE STRING "Override CMAKE_DEBUG_POSTFIX.")
+
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/builds/cmake/Modules)
# Doxygen
@@ -126,23 +135,28 @@ if(NOT DEFINED CMAKE_TOOLCHAIN_FILE)
endif()
endif()
-# icu
+# Add optional library name postfix
+set_property(TARGET lcf PROPERTY DEBUG_POSTFIX "${CMAKE_DEBUG_POSTFIX}")
+
+if(MSVC)
+ # Exception handling
+ set_property(TARGET lcf PROPERTY COMPILE_FLAGS "/EHsc")
+endif()
+
+# icu or fallback to iconv
if(DISABLE_ICU)
find_package(Iconv REQUIRED)
target_link_libraries(lcf Iconv::Iconv)
- if(NOT Iconv_IS_BUILT_IN)
- list(APPEND LIBLCF_DEPS "iconv")
- endif()
else()
- find_package(ICU COMPONENTS uc data REQUIRED)
- target_link_libraries(lcf ICU::uc ICU::data)
+ find_package(ICU COMPONENTS i18n uc data REQUIRED)
+ target_link_libraries(lcf ICU::i18n ICU::uc ICU::data)
target_compile_definitions(lcf PRIVATE LCF_SUPPORT_ICU=1)
list(APPEND LIBLCF_DEPS "icu-i18n")
endif()
# expat
if(NOT DISABLE_XML)
- find_package(EXPAT REQUIRED)
+ find_package(EXPAT)
target_link_libraries(lcf EXPAT::EXPAT)
target_compile_definitions(lcf PRIVATE LCF_SUPPORT_XML=1)
list(APPEND LIBLCF_DEPS "expat")
@@ -150,6 +164,7 @@ endif()
# pkg-config file generation
set(PACKAGE_TARNAME ${PROJECT_NAME})
+set(PACKAGE_VERSION ${PROJECT_VERSION})
set(prefix "${CMAKE_INSTALL_PREFIX}")
set(exec_prefix "\${prefix}")
set(libdir "\${exec_prefix}/${CMAKE_INSTALL_LIBDIR}")
@@ -169,7 +184,6 @@ install(
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
-# installation
file(GLOB includefiles "${CMAKE_CURRENT_SOURCE_DIR}/src/*.h"
"${CMAKE_CURRENT_SOURCE_DIR}/src/generated/*.h")
@@ -177,7 +191,6 @@ install(
FILES ${includefiles}
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/liblcf)
-# pkg-config file generation
install(
FILES builds/${PROJECT_NAME}.pc
DESTINATION lib/pkgconfig

ADD
builds/cmake/Modules/FindEXPAT.cmake
+# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
+# file Copyright.txt or https://cmake.org/licensing for details.
+
+#.rst:
+# FindEXPAT
+# ---------
+#
+# Find the native Expat headers and library.
+#
+# Imported Targets
+# ^^^^^^^^^^^^^^^^
+#
+# This module defines the following :prop_tgt:`IMPORTED` targets:
+#
+# ``EXPAT::EXPAT``
+# The Expat ``expat`` library, if found.
+#
+# Result Variables
+# ^^^^^^^^^^^^^^^^
+#
+# This module will set the following variables in your project:
+#
+# ``EXPAT_INCLUDE_DIRS``
+# where to find expat.h, etc.
+# ``EXPAT_LIBRARIES``
+# the libraries to link against to use Expat.
+# ``EXPAT_FOUND``
+# true if the Expat headers and libraries were found.
+#
+
+find_package(PkgConfig QUIET)
+
+pkg_check_modules(PC_EXPAT QUIET expat)
+
+# Look for the header file.
+find_path(EXPAT_INCLUDE_DIR NAMES expat.h HINTS ${PC_EXPAT_INCLUDE_DIRS})
+
+# Look for the library.
+find_library(EXPAT_LIBRARY NAMES expat libexpat HINTS ${PC_EXPAT_LIBRARY_DIRS})
+
+if (EXPAT_INCLUDE_DIR AND EXISTS "${EXPAT_INCLUDE_DIR}/expat.h")
+ file(STRINGS "${EXPAT_INCLUDE_DIR}/expat.h" expat_version_str
+ REGEX "^#[\t ]*define[\t ]+XML_(MAJOR|MINOR|MICRO)_VERSION[\t ]+[0-9]+$")
+
+ unset(EXPAT_VERSION_STRING)
+ foreach(VPART MAJOR MINOR MICRO)
+ foreach(VLINE ${expat_version_str})
+ if(VLINE MATCHES "^#[\t ]*define[\t ]+XML_${VPART}_VERSION[\t ]+([0-9]+)$")
+ set(EXPAT_VERSION_PART "${CMAKE_MATCH_1}")
+ if(EXPAT_VERSION_STRING)
+ string(APPEND EXPAT_VERSION_STRING ".${EXPAT_VERSION_PART}")
+ else()
+ set(EXPAT_VERSION_STRING "${EXPAT_VERSION_PART}")
+ endif()
+ endif()
+ endforeach()
+ endforeach()
+endif ()
+
+include(FindPackageHandleStandardArgs)
+FIND_PACKAGE_HANDLE_STANDARD_ARGS(EXPAT
+ REQUIRED_VARS EXPAT_LIBRARY EXPAT_INCLUDE_DIR
+ VERSION_VAR EXPAT_VERSION_STRING)
+
+# Copy the results to the output variables and target.
+if(EXPAT_FOUND)
+ set(EXPAT_LIBRARIES ${EXPAT_LIBRARY})
+ set(EXPAT_INCLUDE_DIRS ${EXPAT_INCLUDE_DIR})
+
+ if(NOT TARGET EXPAT::EXPAT)
+ add_library(EXPAT::EXPAT UNKNOWN IMPORTED)
+ set_target_properties(EXPAT::EXPAT PROPERTIES
+ IMPORTED_LINK_INTERFACE_LANGUAGES "C"
+ IMPORTED_LOCATION "${EXPAT_LIBRARY}"
+ INTERFACE_INCLUDE_DIRECTORIES "${EXPAT_INCLUDE_DIRS}")
+ endif()
+endif()
+
+mark_as_advanced(EXPAT_INCLUDE_DIR EXPAT_LIBRARY)

MODIFY
builds/liblcf-config.cmake.in
builds/liblcf-config.cmake.in
@@ -37,10 +37,20 @@ find_path(LIBLCF_INCLUDE_DIR
HINTS ${PC_LIBLCF_INCLUDE_DIRS})
# Look for the library.
-find_library(LIBLCF_LIBRARY
+# Allow LIBLCF_LIBRARY to be set manually, as the location of the liblcf library
+if(NOT LIBLCF_LIBRARY)
+ find_library(LIBLCF_LIBRARY_RELEASE
NAMES lcf liblcf
HINTS ${PC_LIBLCF_LIBRARY_DIRS})
+ find_library(LIBLCF_LIBRARY_DEBUG
+ NAMES lcfd liblcfd
+ HINTS ${PC_LIBLCF_LIBRARY_DIRS})
+
+ include(SelectLibraryConfigurations)
+ select_library_configurations(LIBLCF)
+endif()
+
set(LIBLCF_VERSION_STRING @PACKAGE_VERSION@)
include(FindPackageHandleStandardArgs)
@@ -56,7 +66,7 @@ if(LIBLCF_FOUND)
if(NOT TARGET liblcf::liblcf)
add_library(liblcf::liblcf UNKNOWN IMPORTED)
- set(LIBLCF_LIBRARY_DEPS "icu-i18n expat")
+ set(LIBLCF_LIBRARY_DEPS "@AX_PACKAGE_REQUIRES_PRIVATE@")
string(REPLACE " " ";" LIBLCF_LIBRARY_DEPS "${LIBLCF_LIBRARY_DEPS}")
foreach(dep ${LIBLCF_LIBRARY_DEPS})
@@ -73,10 +83,28 @@ if(LIBLCF_FOUND)
set_target_properties(liblcf::liblcf PROPERTIES
IMPORTED_LINK_INTERFACE_LANGUAGES "CXX"
- IMPORTED_LOCATION "${LIBLCF_LIBRARY}"
INTERFACE_INCLUDE_DIRECTORIES "${LIBLCF_INCLUDE_DIRS}"
INTERFACE_LINK_LIBRARIES "${LIBLCF_LINK_LIBRARIES}")
+ if(LIBLCF_LIBRARY_RELEASE)
+ set_property(TARGET liblcf::liblcf APPEND PROPERTY
+ IMPORTED_CONFIGURATIONS RELEASE)
+ set_target_properties(liblcf::liblcf PROPERTIES
+ IMPORTED_LOCATION_RELEASE "${LIBLCF_LIBRARY_RELEASE}")
+ endif()
+
+ if(LIBLCF_LIBRARY_DEBUG)
+ set_property(TARGET liblcf::liblcf APPEND PROPERTY
+ IMPORTED_CONFIGURATIONS DEBUG)
+ set_target_properties(liblcf::liblcf PROPERTIES
+ IMPORTED_LOCATION_DEBUG "${LIBLCF_LIBRARY_DEBUG}")
+ endif()
+
+ if(NOT LIBLCF_LIBRARY_RELEASE AND NOT LIBLCF_LIBRARY_DEBUG)
+ set_property(TARGET liblcf::liblcf APPEND PROPERTY
+ IMPORTED_LOCATION "${LIBLCF_LIBRARY}")
+ endif()
+
mark_as_advanced(LIBLCF_LIBRARY_DEPS LIBLCF_LINK_LIBRARIES)
endif()
endif()

CMake: Improve debug library handling for MSVC, fix pkg-config var substitution
Add FindEXPAT because the target export functionality was not shipped before CMake 3.10