|
| 1 | +#[[ |
| 2 | + CMakeLists.txt - contains CMake build code for libnex |
| 3 | + Copyright 2022 The NexNix Project |
| 4 | +
|
| 5 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + you may not use this file except in compliance with the License. |
| 7 | + There should be a copy of the License distributed in a file named |
| 8 | + LICENSE, if not, you may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | + Unless required by applicable law or agreed to in writing, software |
| 13 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + See the License for the specific language governing permissions and |
| 16 | + limitations under the License |
| 17 | +]] |
| 18 | + |
| 19 | +cmake_minimum_required(VERSION 3.00) |
| 20 | +project(libnex VERSION 0.0.1) |
| 21 | + |
| 22 | +# Various options |
| 23 | +option(BUILD_SHARED_LIBS "Specifies if shared libraries should be built" OFF) |
| 24 | +option(LIBNEX_ENABLE_TESTS "Specifies if the test suite should be built" OFF) |
| 25 | +option(LIBNEX_BAREMETAL "Specifies if the build should be tailored to baremetal platforms" OFF) |
| 26 | + |
| 27 | +if(${LIBNEX_ENABLE_TESTS} AND NOT ${CMAKE_CROSSCOMPILING}) |
| 28 | + enable_testing() |
| 29 | +endif() |
| 30 | + |
| 31 | +# Find the NexNix SDK |
| 32 | +find_package(NexNixSdk REQUIRED) |
| 33 | + |
| 34 | +# Include modules |
| 35 | +include(GNUInstallDirs) |
| 36 | +include(NexTest) |
| 37 | +include(CheckSymbolExists) |
| 38 | +include(CheckCSourceCompiles) |
| 39 | +include(CMakePackageConfigHelpers) |
| 40 | + |
| 41 | +# See if tests should be enabled |
| 42 | +if(${LIBNEX_ENABLE_TESTS}) |
| 43 | + nextest_enable_tests() |
| 44 | +endif() |
| 45 | + |
| 46 | +# Check for different functions |
| 47 | +check_symbol_exists(strlcpy "string.h" HAVE_BSD_STRING) |
| 48 | +check_symbol_exists(setprogname "stdlib.h" HAVE_PROGNAME) |
| 49 | +check_symbol_exists(pthread_self "pthread.h" HAVE_PTHREAD) |
| 50 | + |
| 51 | +# Configure the header |
| 52 | +configure_file(${CMAKE_SOURCE_DIR}/src/libnex_config.in.h ${CMAKE_BINARY_DIR}/libnex_config.h) |
| 53 | +configure_file(${CMAKE_SOURCE_DIR}/data/nex.pc.in ${CMAKE_BINARY_DIR}/nex.pc @ONLY) |
| 54 | + |
| 55 | +set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS |
| 56 | + ${CMAKE_SOURCE_DIR}/data/nex.pc.in |
| 57 | + ${CMAKE_SOURCE_DIR}/src/libnex_config.in.h |
| 58 | + ${CMAKE_SOURCE_DIR}/data/LibNexConfig.cmake.in) |
| 59 | + |
| 60 | +# Setup different source code types |
| 61 | +list(APPEND LIBNEX_SOURCES_ALWAYS |
| 62 | + src/list.c |
| 63 | + src/lock.c |
| 64 | + src/endian.c |
| 65 | + src/object.c) |
| 66 | + |
| 67 | +list(APPEND LIBNEX_HOSTED_SOURCES |
| 68 | + src/textstream.c |
| 69 | + src/getopt.c |
| 70 | + src/error.c |
| 71 | + src/safemalloc.c) |
| 72 | + |
| 73 | +# If the host lacks some functions, add them |
| 74 | +if(NOT HAVE_PROGNAME) |
| 75 | + list(APPEND LIBNEX_HOSTED_SOURCES |
| 76 | + src/progname.c) |
| 77 | +endif() |
| 78 | + |
| 79 | +if(NOT HAVE_BSD_STRING) |
| 80 | + list(APPEND LIBNEX_SOURCES_ALWAYS |
| 81 | + src/strlcpy.c |
| 82 | + src/strlcat.c) |
| 83 | +endif() |
| 84 | + |
| 85 | +# Create the main source code list |
| 86 | +list(APPEND LIBNEX_SOURCES ${LIBNEX_SOURCES_ALWAYS}) |
| 87 | +if(NOT LIBNEX_BAREMETAL) |
| 88 | + list(APPEND LIBNEX_SOURCES ${LIBNEX_HOSTED_SOURCES}) |
| 89 | +endif() |
| 90 | + |
| 91 | +# Create the include files list |
| 92 | +list(APPEND LIBNEX_HEADERS_ALWAYS |
| 93 | + ${CMAKE_SOURCE_DIR}/include/libnex/container.h |
| 94 | + ${CMAKE_SOURCE_DIR}/include/libnex/decls.h |
| 95 | + ${CMAKE_SOURCE_DIR}/include/libnex/endian.h |
| 96 | + ${CMAKE_SOURCE_DIR}/include/libnex/list.h |
| 97 | + ${CMAKE_SOURCE_DIR}/include/libnex/safestring.h |
| 98 | + ${CMAKE_SOURCE_DIR}/include/libnex/bits.h |
| 99 | + ${CMAKE_SOURCE_DIR}/include/object.h) |
| 100 | + |
| 101 | +# Figure out which libnex.h to use |
| 102 | +if(LIBNEX_BAREMETAL) |
| 103 | + configure_file(${CMAKE_SOURCE_DIR}/src/libnex_baremetal.h ${CMAKE_BINARY_DIR}/libnex.h) |
| 104 | + set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS |
| 105 | + ${CMAKE_SOURCE_DIR}/src/libnex_baremetal.h) |
| 106 | +else() |
| 107 | + configure_file(${CMAKE_SOURCE_DIR}/src/libnex_hosted.h ${CMAKE_BINARY_DIR}/libnex.h) |
| 108 | + set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS |
| 109 | + ${CMAKE_SOURCE_DIR}/src/libnex_hosted.h) |
| 110 | +endif() |
| 111 | +set(LIBNEX_HEADER ${CMAKE_BINARY_DIR}/libnex.h) |
| 112 | + |
| 113 | +# Add hosted headers |
| 114 | +list(APPEND LIBNEX_HEADERS_HOSTED |
| 115 | + ${CMAKE_SOURCE_DIR}/include/libnex/getopt.h |
| 116 | + ${CMAKE_SOURCE_DIR}/include/libnex/textstream.h |
| 117 | + ${CMAKE_SOURCE_DIR}/include/libnex/progname.h |
| 118 | + ${CMAKE_SOURCE_DIR}/include/libnex/error.h |
| 119 | + ${CMAKE_SOURCE_DIR}/include/libnex/safemalloc.h) |
| 120 | + |
| 121 | +# Set the headers |
| 122 | +list(APPEND LIBNEX_HEADERS ${LIBNEX_HEADERS_ALWAYS}) |
| 123 | +if(NOT LIBNEX_BAREMETAL) |
| 124 | + list(APPEND LIBNEX_HEADERS ${LIBNEX_HEADERS_HOSTED}) |
| 125 | +endif() |
| 126 | + |
| 127 | +# Setup include directories |
| 128 | +list(APPEND LIBNEX_INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/include ${CMAKE_BINARY_DIR} |
| 129 | + ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR} |
| 130 | + ${NEXNIXSDK_INCLUDE_DIRS}) |
| 131 | + |
| 132 | +# Create the library |
| 133 | +add_library(nex ${LIBNEX_SOURCES}) |
| 134 | + |
| 135 | +# Set compiler flags |
| 136 | + |
| 137 | +# Set SOName |
| 138 | +set_target_properties(nex PROPERTIES VERSION ${PROJECT_VERSION} SOVERSION ${PROJECT_VERSION}) |
| 139 | +# Include the directories |
| 140 | +target_include_directories(nex PRIVATE ${LIBNEX_INCLUDE_DIRS}) |
| 141 | + |
| 142 | +# libnex_config.h is in the binary directory for the libnex build, |
| 143 | +# but in ${CMAKE_INSTALL_INCLUDEDIR}/include/libnex for consumers. Ensure the headers differentiate |
| 144 | +# between them |
| 145 | +target_compile_definitions(nex PRIVATE "IN_LIBNEX") |
| 146 | + |
| 147 | +# Ensure symbols are hidden by default |
| 148 | +if(BUILD_SHARED_LIBS) |
| 149 | + set_target_properties(nex PROPERTIES C_VISIBILITY_PRESET hidden) |
| 150 | +endif() |
| 151 | + |
| 152 | +# Check for __declspec(dllexport) |
| 153 | +check_c_source_compiles([=[ |
| 154 | + __declspec(dllexport) int f() {} |
| 155 | + int main() {} |
| 156 | +]=] HAVE_DECLSPEC_EXPORT) |
| 157 | + |
| 158 | +if(NOT HAVE_DECLSPEC_EXPORT) |
| 159 | + # Check for __attribute__((visibility("default"))) |
| 160 | + check_c_source_compiles([=[ |
| 161 | + __attribute__((visibility("default"))) int f() {} |
| 162 | + int main(){} |
| 163 | + ]=] HAVE_VISIBILITY) |
| 164 | +endif() |
| 165 | + |
| 166 | +# Install pkgconfig script |
| 167 | +install(FILES ${CMAKE_BINARY_DIR}/nex.pc DESTINATION ${CMAKE_INSTALL_DATADIR}/pkgconfig) |
| 168 | + |
| 169 | +# Install the library |
| 170 | +install(TARGETS nex) |
| 171 | + |
| 172 | +# Figure out library type |
| 173 | +if(BUILD_SHARED_LIBS) |
| 174 | + set(LIBNEX_LIBTYPE "SHARED") |
| 175 | + set(LIBNEX_LIBRARY_FILE "${CMAKE_INSTALL_FULL_LIBDIR}/libnex.so") |
| 176 | +else() |
| 177 | + set(LIBNEX_LIBTYPE "STATIC") |
| 178 | + set(LIBNEX_LIBRARY_FILE "${CMAKE_INSTALL_FULL_LIBDIR}/libnex.a") |
| 179 | +endif() |
| 180 | + |
| 181 | +# Install header files |
| 182 | +install(FILES ${LIBNEX_HEADERS} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/libnex) |
| 183 | +install(FILES ${LIBNEX_HEADER} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) |
| 184 | +install(FILES ${CMAKE_BINARY_DIR}/libnex_config.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/libnex) |
| 185 | + |
| 186 | +# Configure package configuration file |
| 187 | +configure_package_config_file(${CMAKE_SOURCE_DIR}/data/LibNexConfig.cmake.in |
| 188 | + ${CMAKE_BINARY_DIR}/LibNexConfig.cmake |
| 189 | + INSTALL_DESTINATION ${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME}/cmake |
| 190 | + PATH_VARS CMAKE_INSTALL_INCLUDEDIR) |
| 191 | + |
| 192 | +write_basic_package_version_file(${CMAKE_BINARY_DIR}/LibNexConfigVersion.cmake |
| 193 | + COMPATIBILITY AnyNewerVersion) |
| 194 | + |
| 195 | +# Install cmake package files |
| 196 | +install(FILES ${CMAKE_BINARY_DIR}/LibNexConfig.cmake |
| 197 | + DESTINATION ${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME}/cmake) |
| 198 | +install(FILES ${CMAKE_BINARY_DIR}/LibNexConfigVersion.cmake |
| 199 | + DESTINATION ${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME}/cmake) |
| 200 | + |
| 201 | +# Figure out which tests to run |
| 202 | +list(APPEND LIBNEX_TESTS |
| 203 | + endian |
| 204 | + list |
| 205 | + bits) |
| 206 | + |
| 207 | +if(NOT HAVE_BSD_STRING) |
| 208 | + list(APPEND LIBNEX_TESTS strlcat strlcpy) |
| 209 | +endif() |
| 210 | + |
| 211 | +foreach(test ${LIBNEX_TESTS}) |
| 212 | + nextest_add_library_test(NAME ${test} |
| 213 | + SOURCE tests/${test}.c |
| 214 | + LIBS nex |
| 215 | + INCLUDES ${LIBNEX_INCLUDE_DIRS} |
| 216 | + DEFINES IN_LIBNEX) |
| 217 | +endforeach() |
0 commit comments