/ CMakeLists.txt
CMakeLists.txt
  1  # SPDX-FileCopyrightText: 2023-2024 Le’
  2  #
  3  # SPDX-License-Identifier: LGPL-3.0-or-later
  4  
  5  cmake_minimum_required(VERSION 3.23)
  6  
  7  # Get the actual version from 'git describe', and reformat it into a project
  8  # version number by ensuring that it's a three number version, and then add
  9  # the number of commits since the latest reachable tag as a fourth (tweak)
 10  # number.
 11  # The idea came from https://www.hezmatt.org/~mpalmer/blog/2024/11/23/your-release-process-sucks.html
 12  execute_process(
 13    COMMAND git describe --tags --match "v[0-9]*.[0-9]*"
 14    OUTPUT_VARIABLE current_description
 15    OUTPUT_STRIP_TRAILING_WHITESPACE
 16    ERROR_QUIET
 17  )
 18  if(current_description MATCHES [[^v([0-9.]+)-([0-9]+)-g[0-9a-f]+$]])
 19    set(current_version "${CMAKE_MATCH_1}.${CMAKE_MATCH_2}")
 20    # In case the tag was vx.y, reformat the current version as if the tag was vx.y.0
 21    # This ensures that the number of commits after the tag becomes a fourth number,
 22    # representing the current local development (akin to build number, say?)
 23    string(REGEX REPLACE [[^([0-9]+\.[0-9]+)(\.[0-9]+)$]] [[\1.0\2]] current_version ${current_version})
 24  elseif(current_description)
 25    string(REGEX REPLACE [[^v]] [[]] current_version ${current_description})
 26  else()
 27    set(current_version 0.0.1)
 28  endif()
 29  if (current_description)
 30    message(STATUS "Project version is determined to be ${current_version} (from ${current_description})")
 31  else()
 32    message(STATUS "Project version is the default, ${current_version}")
 33  endif()
 34  
 35  project(
 36    leutils
 37    VERSION ${current_version}
 38    DESCRIPTION "Le'Utils is a collection of routines to perform mundane things uniformly on multiple platforms.  Most of the time, each platform has its own way to do certain things, and sometimes, some functionality exists on some of the platforms but not others."
 39    LANGUAGES C)
 40  set(CMAKE_C_STANDARD 99)
 41  
 42  # Global CMake stuff we use
 43  
 44  include(GNUInstallDirs)
 45  include(FetchContent)
 46  
 47  # Declarations of external dependencies
 48  
 49  FetchContent_Declare(
 50    hashmap
 51    GIT_REPOSITORY https://github.com/tidwall/hashmap.c.git
 52    GIT_TAG        v0.8.0
 53  )
 54  
 55  # Get all the things this project depends on
 56  
 57  FetchContent_MakeAvailable(hashmap)
 58  
 59  # Build all libraries in one place, for convenience
 60  set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
 61  set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
 62  # Build all runtimes in one place, for convenience
 63  set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
 64  
 65  # hashmap isn't a CMake project.  As a matter of fact, it only contains
 66  # source files, directly usable here.
 67  add_library(hashmap OBJECT ${hashmap_SOURCE_DIR}/hashmap.c)
 68  set_target_properties(hashmap
 69    PROPERTIES
 70      VISIBILITY_INLINES_HIDDEN 1
 71      C_VISIBILITY_PRESET hidden
 72      LINKER_LANGUAGE C)
 73  target_include_directories(hashmap PUBLIC ${hashmap_SOURCE_DIR})
 74  
 75  # The library itself
 76  
 77  include(CheckSymbolExists)
 78  foreach(string_symbol mempcpy strtcpy stpecpy)
 79    check_symbol_exists(${string_symbol} "string.h" HAVE_${string_symbol})
 80    if (HAVE_${string_symbol})
 81      add_compile_definitions("HAVE_${string_symbol}=1")
 82    endif()
 83  endforeach()
 84  
 85  set(leutils_public_headers
 86    leutils/status.h)
 87  list(TRANSFORM leutils_public_headers PREPEND include/)
 88  
 89  set(leutils_source
 90    status.c)
 91  
 92  add_library(leutils STATIC ${leutils_source} $<TARGET_OBJECTS:hashmap>)
 93  target_include_directories(leutils
 94    PRIVATE
 95      $<TARGET_PROPERTY:hashmap,INTERFACE_INCLUDE_DIRECTORIES>
 96    PUBLIC
 97      $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
 98      $<INSTALL_INTERFACE:include>)
 99  set_target_properties(leutils
100    PROPERTIES
101      VISIBILITY_INLINES_HIDDEN 1
102      C_VISIBILITY_PRESET hidden
103      LINKER_LANGUAGE C
104      VERSION ${PROJECT_VERSION})
105  target_sources(leutils
106    PUBLIC
107      FILE_SET public_headers
108      TYPE HEADERS
109      BASE_DIRS include
110      FILES ${leutils_public_headers})
111  
112  install(TARGETS leutils
113    EXPORT LeUtilsConfig
114    FILE_SET public_headers
115      DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
116      COMPONENT Development)
117  
118  install(EXPORT LeUtilsConfig
119          DESTINATION share/cmake/LeUtils
120          EXPORT_LINK_INTERFACE_LIBRARIES)
121  
122  export(EXPORT LeUtilsConfig)