Mercurial > libbuf
changeset 29:5cbb723ad2a9
cmake: initial support
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sun, 06 Feb 2022 17:26:18 +0100 |
parents | dbdc17e11648 |
children | 7b96c0ff89d5 |
files | CMakeLists.txt INSTALL.md buf.def |
diffstat | 3 files changed, 144 insertions(+), 9 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/CMakeLists.txt Sun Feb 06 17:26:18 2022 +0100 @@ -0,0 +1,90 @@ +# +# CMakeLists.txt -- basic CMake build for libbuf +# +# Copyright (c) 2013-2022 David Demelier <markand@malikania.fr> +# +# Permission to use, copy, modify, and/or distribute this software for any +# purpose with or without fee is hereby granted, provided that the above +# copyright notice and this permission notice appear in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +# + +cmake_minimum_required(VERSION 3.20) +project( + libbuf + VERSION "1.0.0" + DESCRIPTION "Minimalist dynamic string library for C99" + HOMEPAGE_URL "http://projects.malikania.fr/libbuf" + LANGUAGES C +) + +include(CMakePackageConfigHelpers) +include(GNUInstallDirs) + +add_library(libbuf-static STATIC buf.c buf.h) +target_include_directories(libbuf-static PUBLIC $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>) +install( + TARGETS libbuf-static + EXPORT buf-targets + ARCHIVE DESTINATION lib +) + +if (NOT CMAKE_C_COMPILER_ID MATCHES "MSVC" OR NOT BUILD_SHARED_LIBS) + set_target_properties(libbuf-static PROPERTIES OUTPUT_NAME buf) +else () + set_target_properties(libbuf-static PROPERTIES OUTPUT_NAME buf-static) +endif () + +if (BUILD_SHARED_LIBS) + add_library(libbuf-shared SHARED buf.c buf.h buf.def) + target_include_directories(libbuf-shared PUBLIC $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>) + set_target_properties( + libbuf-shared + PROPERTIES + OUTPUT_NAME buf + VERSION ${PROJECT_VERSION} + SOVERSION ${PROJECT_VERSION_MAJOR} + ) + install( + TARGETS libbuf-shared + EXPORT buf-targets + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + ) +endif () + +configure_file( + ${PROJECT_SOURCE_DIR}/buf.pc.in + ${PROJECT_BINARY_DIR}/buf.pc + @ONLY +) + +write_basic_package_version_file( + ${PROJECT_BINARY_DIR}/buf-config-version.cmake + VERSION ${PROJECT_VERSION} + COMPATIBILITY SameMajorVersion +) + +install(FILES ${PROJECT_SOURCE_DIR}/buf.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) +install(FILES ${PROJECT_BINARY_DIR}/buf.pc DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig) +install(FILES ${PROJECT_SOURCE_DIR}/libbuf.3 DESTINATION ${CMAKE_INSTALL_MANDIR}/man3) +install( + EXPORT buf-targets + FILE buf-targets.cmake + NAMESPACE buf:: + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/buf +) +install( + FILES + ${PROJECT_BINARY_DIR}/buf-config-version.cmake + ${PROJECT_SOURCE_DIR}/buf-config.cmake + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/buf +)
--- a/INSTALL.md Sun Jan 02 10:15:18 2022 +0100 +++ b/INSTALL.md Sun Feb 06 17:26:18 2022 +0100 @@ -10,20 +10,51 @@ - POSIX make, only required to build, - POSIX user land (ar, cc, cp, mkdir, rm), to install and test. -Basic installation ------------------- +Embed +----- + +Copy buf.h and buf.c to your project. + +Installation +------------ + +The module is small enough to be incorporated verbatim into your project, but it +still possible to install it system wide. + +### Using CMake (recommended) + +Using [CMake][cmake] you get proper CMake package configuration files, shared +libraries and `pkg-config` files. + + $ cmake -S . -B build -DBUILD_SHARED_LIBS=On + $ cmake --build build + # cmake --build build --target install -### Embedded +Turn `BUILD_SHARED_LIBS` to *Off* if you don't want shared libraries. + +Then, you can import `buf` and use on of the imported targets: + +- `buf::libbuf`: shared if available, static otherwise, +- `buf::libbuf-shared`: shared version, +- `buf::libbuf-static`: static version. + +Example: -Copy buf.c and buf.h files into your projects. + cmake_minimum_required(VERSION 3.20) + project(example) + find_package(buf REQUIRED) + add_executable(example example.c) + target_link_libraries(example buf::libbuf) -### System wide (not recommended) +### Using POSIX make (not recommended) + +POSIX make (not recommended, only static library): $ make - # sudo make install + # make install -### Other targets +The test suite is available using: -Alternatively, you can use the following targets as well. + $ make tests -- `make test`: run test suite. +[cmake]: http://cmake.org