Skip to content

Commit

Permalink
CMake: Generate a real CMake config file when installing instead of o…
Browse files Browse the repository at this point in the history
…ur hacked mess

Should resolve all problems of consumers of our library.

In case of autotools a very simple config file that only uses PkgConfig is used.
Everything else is not maintainable as the config files generated by CMake look horrible.

Fix EasyRPG#446
  • Loading branch information
Ghabry committed Jan 4, 2023
1 parent 0ed38af commit 96a6715
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 152 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ Makefile
*.o
*.pc
config.h
builds/liblcf-config.cmake

# tests
test_runner*
Expand Down
39 changes: 30 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -299,14 +299,15 @@ endforeach()
# includes
target_include_directories(
lcf PUBLIC
${CMAKE_CURRENT_BINARY_DIR}/src
${CMAKE_CURRENT_SOURCE_DIR}/src
${CMAKE_CURRENT_SOURCE_DIR}/src/generated
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/src>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/generated>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_LIBDIR}>
)

# Optimize floating point math functions into intrinsics and don't check or set errno (i.e. sqrt(-1))
if (NOT CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
target_compile_options(lcf PUBLIC "-fno-math-errno")
target_compile_options(lcf PRIVATE "-fno-math-errno")
endif()

# endianess checking
Expand All @@ -324,6 +325,9 @@ set_property(TARGET lcf PROPERTY DEBUG_POSTFIX "${CMAKE_DEBUG_POSTFIX}")
# Export symbols for DLL
set_property(TARGET lcf PROPERTY WINDOWS_EXPORT_ALL_SYMBOLS ON)

# Name of the exported library
set_property(TARGET lcf PROPERTY EXPORT_NAME liblcf)

# icu or fallback to iconv
set(LCF_SUPPORT_ICU 0)
if(LIBLCF_WITH_ICU)
Expand Down Expand Up @@ -384,10 +388,9 @@ string(REPLACE ";" " " AX_PACKAGE_REQUIRES_PRIVATE "${LIBLCF_DEPS}")
configure_file(builds/${PROJECT_NAME}.pc.in builds/${PROJECT_NAME}.pc @ONLY)

# Cmake-config file generation
configure_file(builds/${PROJECT_NAME}-config.cmake.in builds/${PROJECT_NAME}-config.cmake @ONLY)

install(
TARGETS lcf
EXPORT lcf-export
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
Expand All @@ -407,9 +410,27 @@ install(
)

install(
FILES
${CMAKE_CURRENT_BINARY_DIR}/builds/liblcf-config.cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/liblcf)
EXPORT lcf-export FILE liblcf-targets.cmake
NAMESPACE liblcf:: DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/liblcf
)

include(CMakePackageConfigHelpers)
configure_package_config_file(
${CMAKE_CURRENT_SOURCE_DIR}/builds/cmake/liblcf-config.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/liblcf-config.cmake
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/liblcf
)

write_basic_package_version_file(
${CMAKE_CURRENT_BINARY_DIR}/liblcf-config-version.cmake
COMPATIBILITY ExactVersion
)

install(FILES
${CMAKE_CURRENT_BINARY_DIR}/liblcf-config.cmake
${CMAKE_CURRENT_BINARY_DIR}/liblcf-config-version.cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/liblcf
)

# mime types
install(
Expand Down
4 changes: 3 additions & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ mimedbdir = $(datadir)/mime/packages
mimedb_DATA = mime/liblcf.xml

cmakeconfigdir = $(libdir)/cmake/liblcf
cmakeconfig_DATA = builds/liblcf-config.cmake
cmakeconfig_DATA = \
builds/autoconf/liblcf-config.cmake \
builds/autoconf/liblcf-config-version.cmake

lcfincludedir = $(includedir)/lcf
lcfldbincludedir = $(includedir)/lcf/ldb
Expand Down
70 changes: 70 additions & 0 deletions builds/autoconf/liblcf-config-version.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# This is a basic version file for the Config-mode of find_package().
# It is used by write_basic_package_version_file() as input file for configure_file()
# to create a version-file which can be installed along a config.cmake file.
#
# The created file sets PACKAGE_VERSION_EXACT if the current version string and
# the requested version string are exactly the same and it sets
# PACKAGE_VERSION_COMPATIBLE if the current version is equal to the requested version.
# The tweak version component is ignored.
# The variable CVF_VERSION must be set before calling configure_file().


if (PACKAGE_FIND_VERSION_RANGE)
message(AUTHOR_WARNING
"`find_package()` specify a version range but the version strategy "
"(ExactVersion) of the module `${PACKAGE_FIND_NAME}` is incompatible "
"with this request. Only the lower endpoint of the range will be used.")
endif()

set(PACKAGE_VERSION "@PACKAGE_VERSION@")

if("@PACKAGE_VERSION@" MATCHES "^([0-9]+)\\.([0-9]+)\\.([0-9]+)") # strip the tweak version
set(CVF_VERSION_MAJOR "${CMAKE_MATCH_1}")
set(CVF_VERSION_MINOR "${CMAKE_MATCH_2}")
set(CVF_VERSION_PATCH "${CMAKE_MATCH_3}")

if(NOT CVF_VERSION_MAJOR VERSION_EQUAL 0)
string(REGEX REPLACE "^0+" "" CVF_VERSION_MAJOR "${CVF_VERSION_MAJOR}")
endif()
if(NOT CVF_VERSION_MINOR VERSION_EQUAL 0)
string(REGEX REPLACE "^0+" "" CVF_VERSION_MINOR "${CVF_VERSION_MINOR}")
endif()
if(NOT CVF_VERSION_PATCH VERSION_EQUAL 0)
string(REGEX REPLACE "^0+" "" CVF_VERSION_PATCH "${CVF_VERSION_PATCH}")
endif()

set(CVF_VERSION_NO_TWEAK "${CVF_VERSION_MAJOR}.${CVF_VERSION_MINOR}.${CVF_VERSION_PATCH}")
else()
set(CVF_VERSION_NO_TWEAK "@PACKAGE_VERSION@")
endif()

if(PACKAGE_FIND_VERSION MATCHES "^([0-9]+)\\.([0-9]+)\\.([0-9]+)") # strip the tweak version
set(REQUESTED_VERSION_MAJOR "${CMAKE_MATCH_1}")
set(REQUESTED_VERSION_MINOR "${CMAKE_MATCH_2}")
set(REQUESTED_VERSION_PATCH "${CMAKE_MATCH_3}")

if(NOT REQUESTED_VERSION_MAJOR VERSION_EQUAL 0)
string(REGEX REPLACE "^0+" "" REQUESTED_VERSION_MAJOR "${REQUESTED_VERSION_MAJOR}")
endif()
if(NOT REQUESTED_VERSION_MINOR VERSION_EQUAL 0)
string(REGEX REPLACE "^0+" "" REQUESTED_VERSION_MINOR "${REQUESTED_VERSION_MINOR}")
endif()
if(NOT REQUESTED_VERSION_PATCH VERSION_EQUAL 0)
string(REGEX REPLACE "^0+" "" REQUESTED_VERSION_PATCH "${REQUESTED_VERSION_PATCH}")
endif()

set(REQUESTED_VERSION_NO_TWEAK
"${REQUESTED_VERSION_MAJOR}.${REQUESTED_VERSION_MINOR}.${REQUESTED_VERSION_PATCH}")
else()
set(REQUESTED_VERSION_NO_TWEAK "${PACKAGE_FIND_VERSION}")
endif()

if(REQUESTED_VERSION_NO_TWEAK STREQUAL CVF_VERSION_NO_TWEAK)
set(PACKAGE_VERSION_COMPATIBLE TRUE)
else()
set(PACKAGE_VERSION_COMPATIBLE FALSE)
endif()

if(PACKAGE_FIND_VERSION STREQUAL PACKAGE_VERSION)
set(PACKAGE_VERSION_EXACT TRUE)
endif()
9 changes: 9 additions & 0 deletions builds/autoconf/liblcf-config.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# For simplicity reasons depend on pkg-config when liblcf was compiled with autotools

include(CMakeFindDependencyMacro)

find_dependency(PkgConfig REQUIRED QUIET)

pkg_check_modules(liblcf REQUIRED QUIET IMPORTED_TARGET liblcf=@PACKAGE_VERSION@)

add_library(liblcf::liblcf ALIAS PkgConfig::liblcf)
15 changes: 15 additions & 0 deletions builds/cmake/liblcf-config.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
@PACKAGE_INIT@

include(CMakeFindDependencyMacro)

if(@LCF_SUPPORT_ICU@)
find_dependency(ICU COMPONENTS i18n uc data)
else()
find_dependency(Iconv REQUIRED)
endif()

if(@LCF_SUPPORT_XML@)
find_dependency(expat CONFIG)
endif()

include(${CMAKE_CURRENT_LIST_DIR}/liblcf-targets.cmake)
138 changes: 0 additions & 138 deletions builds/liblcf-config.cmake.in

This file was deleted.

7 changes: 4 additions & 3 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,9 @@ AC_SUBST([CMAKE_DEBUG_POSTFIX])

# Files to generate
AC_CONFIG_FILES([Makefile
builds/liblcf.pc
builds/liblcf-config.cmake
src/lcf/config.h:builds/config.h.in])
builds/liblcf.pc
builds/autoconf/liblcf-config.cmake
builds/autoconf/liblcf-config-version.cmake
src/lcf/config.h:builds/config.h.in])

AC_OUTPUT

0 comments on commit 96a6715

Please sign in to comment.