diff --git a/CMakeLists.txt b/CMakeLists.txt index 249f7ee..703666c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,17 @@ cmake_minimum_required(VERSION 3.14) -project(nameof VERSION "0.10.2" LANGUAGES CXX) +include(GNUInstallDirs) + +set(ADDITIONAL_MODULES_DIR "${CMAKE_CURRENT_LIST_DIR}/cmake") +list(APPEND CMAKE_MODULE_PATH "${ADDITIONAL_MODULES_DIR}") + +project(nameof + VERSION "0.10.2" + HOMEPAGE_URL "https://github.com/Neargye/nameof" + DESCRIPTION "A library that provides nameof macros and functions to simply obtain the name of a variable, type, function, macro, and enum." + LANGUAGES CXX +) +set(CPACK_PACKAGE_VENDOR "Daniil Goncharov") if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME) set(IS_TOPLEVEL_PROJECT TRUE) @@ -23,33 +34,95 @@ endif() include(CMakePackageConfigHelpers) -add_library(${PROJECT_NAME} INTERFACE) -add_library(${PROJECT_NAME}::${PROJECT_NAME} ALIAS ${PROJECT_NAME}) +set(EXPORT_NAMESPACE "${PROJECT_NAME}::") + +add_library("${PROJECT_NAME}" INTERFACE) +add_library("${EXPORT_NAMESPACE}${PROJECT_NAME}" ALIAS "${PROJECT_NAME}") +set(INCLUDES "${CMAKE_CURRENT_SOURCE_DIR}/include") + target_include_directories(${PROJECT_NAME} INTERFACE - $ - $) - -write_basic_package_version_file(${PROJECT_NAME}ConfigVersion.cmake - VERSION ${PROJECT_VERSION} - COMPATIBILITY AnyNewerVersion - ARCH_INDEPENDENT) + $ + $) if(NAMEOF_OPT_INSTALL) - install(TARGETS ${PROJECT_NAME} - EXPORT ${PROJECT_NAME}Config) + list(APPEND CMAKE_MODULE_PATH "${ADDITIONAL_MODULES_DIR}/GenPkgConfig") + include(GenPkgConfig) + include(CPackComponent) + include(CMakePackageConfigHelpers) + + string(REPLACE "/${CMAKE_LIBRARY_ARCHITECTURE}" "" CMAKE_INSTALL_LIBDIR_ARCHIND "${CMAKE_INSTALL_LIBDIR}") + install(TARGETS "${PROJECT_NAME}" + EXPORT ${PROJECT_NAME} + INCLUDES + DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" + # COMPONENT "${SDK_COMPONENT_NAME}" # component is not allowed for includes! Headers are installed separately! Includes only marks the headers for export + ) - install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake - DESTINATION lib/cmake/${PROJECT_NAME}) + file(GLOB_RECURSE HEADERS "${INCLUDES}/*.h" "${INCLUDES}/*.hxx" "${INCLUDES}/*.hpp") + foreach(headerFile ${HEADERS}) + get_filename_component(headerFileParentDir "${headerFile}" DIRECTORY) + file(RELATIVE_PATH headerFileRelParentDir "${INCLUDES}" "${headerFileParentDir}") - install(EXPORT ${PROJECT_NAME}Config - NAMESPACE ${PROJECT_NAME}:: - DESTINATION lib/cmake/${PROJECT_NAME}) + install(FILES "${headerFile}" + DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/${headerFileRelParentDir}" + ) + endforeach() - install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include - DESTINATION .) + set(CPACK_PACKAGE_NAME "${PROJECT_NAME}") + set(CPACK_PACKAGE_DESCRIPTION "${PROJECT_DESCRIPTION}") + set(CPACK_DEBIAN_PACKAGE_ARCHITECTURE "all") + set(CPACK_DEBIAN_PACKAGE_NAME "lib${CPACK_PACKAGE_NAME}-dev") + set(CPACK_RPM_PACKAGE_NAME "lib${CPACK_PACKAGE_NAME}-devel") + set(CPACK_PACKAGE_HOMEPAGE_URL "${PROJECT_HOMEPAGE_URL}") + set(CPACK_PACKAGE_MAINTAINER "${CPACK_PACKAGE_VENDOR}") + set(CPACK_DEBIAN_PACKAGE_DEPENDS "") + set(CPACK_DEBIAN_PACKAGE_MAINTAINER "${CPACK_PACKAGE_MAINTAINER}") + set(CPACK_PACKAGE_MAINTAINER "${CPACK_PACKAGE_VENDOR}") + set(CPACK_DEB_COMPONENT_INSTALL ON) + set(CPACK_RPM_COMPONENT_INSTALL ON) + set(CPACK_NSIS_COMPONENT_INSTALL ON) + set(CPACK_DEBIAN_COMPRESSION_TYPE "xz") - export(EXPORT ${PROJECT_NAME}Config - NAMESPACE ${PROJECT_NAME}::) + set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE") + set(CPACK_RESOURCE_FILE_README "${CMAKE_CURRENT_SOURCE_DIR}/README.md") + + set(CMAKE_CONFIG_FILE_BASENAME "${PROJECT_NAME}Config.cmake") + set(CMAKE_EXPORT_FILE_BASENAME "${PROJECT_NAME}Export.cmake") + set(CMAKE_CONFIG_VERSION_FILE_BASENAME "${PROJECT_NAME}ConfigVersion.cmake") + set(CMAKE_CONFIG_VERSION_FILE_NAME "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CONFIG_VERSION_FILE_BASENAME}") + + export(TARGETS "${PROJECT_NAME}" + NAMESPACE "${EXPORT_NAMESPACE}" + FILE "${CMAKE_EXPORT_FILE_BASENAME}" + EXPORT_LINK_INTERFACE_LIBRARIES + ) + + install(EXPORT "${PROJECT_NAME}" + FILE "${CMAKE_CONFIG_FILE_BASENAME}" + NAMESPACE "${EXPORT_NAMESPACE}" + DESTINATION "${CMAKE_INSTALL_LIBDIR_ARCHIND}/cmake/${PROJECT_NAME}" + ) + + write_basic_package_version_file( + "${CMAKE_CONFIG_VERSION_FILE_NAME}" + #VERSION "100500.100500.100500" # any version of same bitness suits. CMake cannot compare to infinity, so use a large number we expect to be greater than any future version + VERSION ${_VERSION} + COMPATIBILITY AnyNewerVersion + ARCH_INDEPENDENT + ) + install(FILES "${CMAKE_CONFIG_VERSION_FILE_NAME}" + DESTINATION "${CMAKE_INSTALL_LIBDIR_ARCHIND}/cmake/${CMAKE_EXPORT_NAME}" + ) + + configure_pkg_config_file("${PROJECT_NAME}" + NAME "${PROJECT_NAME}" + VERSION "${PROJECT_VERSION}" + DESCRIPTION "${CPACK_PACKAGE_DESCRIPTION}" + URL "${CPACK_PACKAGE_HOMEPAGE_URL}" + INSTALL_LIB_DIR "${CMAKE_INSTALL_LIBDIR_ARCHIND}" + INSTALL_INCLUDE_DIR "${CMAKE_INSTALL_INCLUDEDIR}" + ) + + include(CPack) endif() - diff --git a/cmake/GenPkgConfig/GenPkgConfig.cmake b/cmake/GenPkgConfig/GenPkgConfig.cmake new file mode 100644 index 0000000..28ef4a0 --- /dev/null +++ b/cmake/GenPkgConfig/GenPkgConfig.cmake @@ -0,0 +1,239 @@ +#[=======================================================================[.rst: + +GenPkgConfig +------------ + +This is the library helping you to generate and install pkg-config files. + +Unlicense +^^^^^^^^^ + +This is free and unencumbered software released into the public domain. +Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means. +In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +For more information, please refer to + +Warning +^^^^^^^ + +CMake is currently merging a built-in impl of pkg-config file generator! https://gitlab.kitware.com/cmake/cmake/-/merge_requests/6363 + +Functions +^^^^^^^^^ +.. command:: configure_pkg_config_file + + .. versionadded:: 3.22 + + Generates a pkg-config file for + + :: + + configure_pkg_config_file( + NAME + VERSION + DESCRIPTION + URL + COMPONENT + INSTALL_LIB_DIR + INSTALL_INCLUDE_DIR + REQUIRES ... ... + REQUIRES ... ... + ) + + The arguments are optional and usually are not needed to be set if global (not component-specific) CPACK vars have been set before. + + Generation is done in build time using packaging expressions. + +#]=======================================================================] + +set("GEN_PKG_CONFIG_WORKAROUNDS_BUILD_TIME_SCRIPTS" "${CMAKE_CURRENT_LIST_DIR}/buildTimeScripts") + +cmake_policy(SET CMP0070 NEW) + +function(configure_pkg_config_file TARGET) + cmake_parse_arguments("" + "" # options + "NAME;VERSION;DESCRIPTION;URL;COMPONENT;INSTALL_LIB_DIR;INSTALL_INCLUDE_DIR" # one_value_keywords + "REQUIRES;CONFLICTS" # multi_value_keywords + ${ARGN} + ) + + configure_pkg_config_file_vars("${TARGET}" "${_NAME}" "${_INSTALL_LIB_DIR}" "${_INSTALL_INCLUDE_DIR}" "${_COMPONENT}" "${_DESCRIPTION}" "${_URL}" "${_VERSION}" "${_REQUIRES}" "${_CONFLICTS}") +endfunction() + +function(ge_expr_basename inputExpr outVar) + set("${outVar}" "$" PARENT_SCOPE) +endfunction() + +function(configure_pkg_config_file_vars TARGET _NAME _INSTALL_LIB_DIR _INSTALL_INCLUDE_DIR _COMPONENT _DESCRIPTION _URL _VERSION _REQUIRES _CONFLICTS) + #$ + #INTERFACE_LINK_DIRECTORIES + #INTERFACE_LINK_LIBRARIES + #INTERFACE_LINK_OPTIONS + + if(_NAME) + else() + set(_NAME "$") + endif() + + if(_DESCRIPTION) + else() + set(_DESCRIPTION "${CPACK_PACKAGE_DESCRIPTION}") + endif() + + if(_VERSION) + else() + set(_VERSION "${CPACK_PACKAGE_VERSION}") + endif() + + if(_URL) + else() + set(_URL "${CPACK_PACKAGE_HOMEPAGE_URL}") + endif() + + if(INSTALL_INCLUDE_DIR) + else() + set(INSTALL_INCLUDE_DIR "${CMAKE_INSTALL_INCLUDEDIR}") + endif() + + if(INSTALL_LIB_DIR) + else() + set(INSTALL_LIB_DIR "${CMAKE_INSTALL_LIBDIR}") + endif() + + set(PKG_CONFIG_FILE_NAME "${CMAKE_CURRENT_BINARY_DIR}/${_NAME}.pc") + + set(PUBLIC_INCLUDES "$") + set(PUBLIC_LIBS "$") + set(PUBLIC_COMPILE_FLAGS "$") + + set("IS_INTERFACE" "$,INTERFACE_LIBRARY>") + set("IS_OBJECT" "$,OBJECT_LIBRARY>") + get_target_property(CONFIGURE_TIME_TARGET_TYPE "${TARGET}" TYPE) + if(CONFIGURE_TIME_TARGET_TYPE STREQUAL OBJECT_LIBRARY) + set(CONFIGURE_TIME_IS_OBJECT ON) # Special measures have to be taken!!! + endif() + + set("NEEDS_LIBS" "$,$>") + set("NEEDS_LIB_DIR" "$") + string(REPLACE "," "$" NEEDS_LIBS_ESCAPED "${NEEDS_LIBS}") + string(REPLACE ">" "$" NEEDS_LIBS_ESCAPED "${NEEDS_LIBS_ESCAPED}") + + list(APPEND header "prefix=${CMAKE_INSTALL_PREFIX}") + list(APPEND header "$,${NEEDS_LIB_DIR}>,libdir=\${prefix}/${INSTALL_LIB_DIR},>") + list(APPEND header "$,includedir=\${prefix}/${INSTALL_INCLUDE_DIR},>") + + + list(APPEND libSpecific "Name: ${_NAME}") + if(_DESCRIPTION) + list(APPEND libSpecific "Description: ${_DESCRIPTION}") + endif() + if(_URL) + list(APPEND libSpecific "URL: ${_URL}") + endif() + if(_VERSION) + list(APPEND libSpecific "Version: ${_VERSION}") + endif() + if(_REQUIRES) + list(APPEND libSpecific "Requires: ${_REQUIRES}") + endif() + if(_CONFLICTS) + list(APPEND libSpecific "Conflicts: ${_CONFLICTS}") + endif() + + set(OTHER_INCLUDE_FLAGS "-I$, -I>") # Not needed, we can only get build interface flags here. Insert them after -I\${includedir} if you find a way to fix/workaround it + + # Here is a workaround to inability to use TARGET_LINKER_FILE_NAME for targets not involving library generation. + # Strangely $<") # A hack because there is no escape for `$` or `<` or `$<`. So we just disrupt $< into pieces + set(CURRENT_LIB_ESCAPED_BINARY_NAME "${ESCAPED_GENEXPR_BEGINNING}TARGET_LINKER_FILE_NAME:${TARGET}$") + set(LINK_CURRENT_LIB_FLAG "$>") + + if(CONFIGURE_TIME_IS_OBJECT) + set(IS_TARGET_OBJECTS_CONFIGURE_TIME_UNAVAILABLE ON) + if(IS_TARGET_OBJECTS_CONFIGURE_TIME_UNAVAILABLE) + message(WARNING "CMake is shit: There is (at least was at the time of writing of this code) no generator expression to get only basenames of object files. They are also unavailable at configure stage. And there were no CMake generator expressions for making replacements in strings. So we workaround this.") + set(TARGET_OBJECTS_FILE "${TARGET}.obj_list") + set(OBJECTS_FILE_RETRIEVAL_TARGET_NAME "${TARGET}_get_objects_list") + + set(PKGCONFIG_DUMMY_UNFINISHED_GEN_TARGET_NAME "${TARGET}_pkgconfig_unfinished") + set(PKGCONFIG_PATCH_TARGET_NAME "${TARGET}_patch_pkgconfig") + + set(PKG_CONFIG_FILE_NAME_FINISHED "${PKG_CONFIG_FILE_NAME}") + set(PKG_CONFIG_FILE_NAME_UNFINISHED "${PKG_CONFIG_FILE_NAME_FINISHED}.unfinished") + + file(GENERATE OUTPUT "${TARGET_OBJECTS_FILE}" CONTENT "$") + + add_custom_command( + OUTPUT "${TARGET_OBJECTS_FILE}" + COMMENT "A dummy command to workaround cmake limitations" + ) + add_custom_target("${OBJECTS_FILE_RETRIEVAL_TARGET_NAME}" + DEPENDS "${TARGET_OBJECTS_FILE}" + ) + + add_custom_command( + OUTPUT "${PKG_CONFIG_FILE_NAME_FINISHED}" + PRE_BUILD COMMAND ${CMAKE_COMMAND} "-DobjectsFile=\"${TARGET_OBJECTS_FILE}\"" "-DpkgConfigFileUnlinished=\"${PKG_CONFIG_FILE_NAME_UNFINISHED}\"" "-DpkgConfigFileFinal=\"${PKG_CONFIG_FILE_NAME_FINISHED}\"" "-P" "${GEN_PKG_CONFIG_WORKAROUNDS_BUILD_TIME_SCRIPTS}/getObjectFilesBaseNames.cmake" + MAIN_DEPENDENCY "${TARGET_OBJECTS_FILE}" + DEPENDS "${PKG_CONFIG_FILE_NAME_UNFINISHED}" + COMMENT "Working around CMake limitations about getting list of basenames of object files and about lack of generator expressions to modify strings: ${PKG_CONFIG_FILE_NAME_UNFINISHED} + ${TARGET_OBJECTS_FILE} -> ${PKG_CONFIG_FILE_NAME_FINISHED}" + ) + add_custom_target("${PKGCONFIG_PATCH_TARGET_NAME}" ALL + DEPENDS "${PKG_CONFIG_FILE_NAME_FINISHED}" + ) + add_dependencies("${PKGCONFIG_PATCH_TARGET_NAME}" "${OBJECTS_FILE_RETRIEVAL_TARGET_NAME}" "${PKGCONFIG_DUMMY_UNFINISHED_GEN_TARGET_NAME}") + + set(PROPERLY_JOINED_TARGET_OBJECTS "@PROPERLY_JOINED_TARGET_OBJECTS@") + else() + set("RAW_TARGET_OBJECTS" "$") + message(FATAL_ERROR "This branch is unimplemented because CMake lacked the needed functionality at the time") + set(PROPERLY_JOINED_TARGET_OBJECTS "${RAW_TARGET_OBJECTS}") + endif() + endif() + + set(LINK_CURRENT_OBJECT_FLAG "$") + + list(APPEND libSpecific "$,${NEEDS_LIBS},${IS_OBJECT}>,Libs: -L\${libdir} ${LINK_CURRENT_LIB_FLAG} ${LINK_CURRENT_OBJECT_FLAG} $,-l$, -l>,>,>\n$,$>,Cflags: -I\${includedir} $,>,>") + + + list(JOIN header "\n" header) + list(JOIN libSpecific "\n" libSpecific) + set(libSpecific "${header}\n\n${libSpecific}") + + if(CONFIGURE_TIME_IS_OBJECT) + file(GENERATE OUTPUT "${PKG_CONFIG_FILE_NAME_UNFINISHED}" + CONTENT "${libSpecific}" + ) + + # Dummy target for generated files + add_custom_command( + OUTPUT "${PKG_CONFIG_FILE_NAME_UNFINISHED}" + COMMENT "A dummy command to workaround cmake limitations" + ) + add_custom_target("${PKGCONFIG_DUMMY_UNFINISHED_GEN_TARGET_NAME}" + DEPENDS "${PKG_CONFIG_FILE_NAME_UNFINISHED}" + ) + + + install(FILES "${PKG_CONFIG_FILE_NAME_FINISHED}" + DESTINATION "${_INSTALL_LIB_DIR}/pkgconfig" + COMPONENT "${_COMPONENT}" + ) + else() + file(GENERATE OUTPUT "${PKG_CONFIG_FILE_NAME}" + CONTENT "${libSpecific}" + ) + + + install(FILES "${PKG_CONFIG_FILE_NAME}" + DESTINATION "${_INSTALL_LIB_DIR}/pkgconfig" + COMPONENT "${_COMPONENT}" + ) + endif() +endfunction() + diff --git a/cmake/GenPkgConfig/ReadMe.md b/cmake/GenPkgConfig/ReadMe.md new file mode 100644 index 0000000..9f7b936 --- /dev/null +++ b/cmake/GenPkgConfig/ReadMe.md @@ -0,0 +1,45 @@ +GenPkgConfig.cmake +=================== + +A script generating pkg-config files. + +WARNING: CMake [is currently merging own built-in pkgconfig generation implementation](https://gitlab.kitware.com/cmake/cmake/-/merge_requests/6363)! + +If you require such a new version of CMake, you probably should use the built-in impl instead. + +Syntax +------ + +```cmake +configure_pkg_config_file( + NAME + VERSION + DESCRIPTION + URL + COMPONENT + INSTALL_LIB_DIR + INSTALL_INCLUDE_DIR + REQUIRES ... ... + REQUIRES ... ... +) +``` + +Issuees +------- + +1. For `OBJECT` targets we have run into big issues. CMake + 1. Doesn't allow to get the list of object files at configure time + 2. Allows to get a list of object files as a generator exression ... + 3. BUT ... the path to them is full, but we need only file name! + 4. CMake doesn't allow to strip directory path via generator expression + 5. ... neither it allows string editing within generator expressions ... + + so ... we have to create a custom target using a custom CMake script executed separately, but ... + + 6. `file(GENERATE` doesn't properly register dependencies + ... so we have to use `add_custom_command` to say CMake that these files are generated + + 7. And CMake `install(FILES` doesn't mean that the targets generating these files are automatically executed, + + So we have to use `ALL` in `add_custom_target`. + diff --git a/cmake/GenPkgConfig/UNLICENSE b/cmake/GenPkgConfig/UNLICENSE new file mode 100644 index 0000000..efb9808 --- /dev/null +++ b/cmake/GenPkgConfig/UNLICENSE @@ -0,0 +1,24 @@ +This is free and unencumbered software released into the public domain. + +Anyone is free to copy, modify, publish, use, compile, sell, or +distribute this software, either in source code form or as a compiled +binary, for any purpose, commercial or non-commercial, and by any +means. + +In jurisdictions that recognize copyright laws, the author or authors +of this software dedicate any and all copyright interest in the +software to the public domain. We make this dedication for the benefit +of the public at large and to the detriment of our heirs and +successors. We intend this dedication to be an overt act of +relinquishment in perpetuity of all present and future rights to this +software under copyright law. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. + +For more information, please refer to diff --git a/cmake/GenPkgConfig/buildTimeScripts/getObjectFilesBaseNames.cmake b/cmake/GenPkgConfig/buildTimeScripts/getObjectFilesBaseNames.cmake new file mode 100644 index 0000000..fdf205d --- /dev/null +++ b/cmake/GenPkgConfig/buildTimeScripts/getObjectFilesBaseNames.cmake @@ -0,0 +1,18 @@ + +message(STATUS "objectsFile ${objectsFile}") +message(STATUS "pkgConfigFileFinal ${pkgConfigFileFinal}") +message(STATUS "pkgConfigFileUnlinished ${pkgConfigFileUnlinished}") + +file(READ "${objectsFile}" TARGET_OBJECTS) + +set(PROPERLY_JOINED_TARGET_OBJECTS "") + +foreach(objFullPath ${TARGET_OBJECTS}) + get_filename_component(objFullPath "${objFullPath}" NAME) + list(APPEND PROPERLY_JOINED_TARGET_OBJECTS "${objFullPath}") +endforeach() +list(JOIN PROPERLY_JOINED_TARGET_OBJECTS " " PROPERLY_JOINED_TARGET_OBJECTS) + +message(STATUS "PROPERLY_JOINED_TARGET_OBJECTS AFTER ${PROPERLY_JOINED_TARGET_OBJECTS}") + +configure_file("${pkgConfigFileUnlinished}" "${pkgConfigFileFinal}" @ONLY)