From d2794cf912243590cdbdcbe12923f905450e5af7 Mon Sep 17 00:00:00 2001 From: Linus Henke Date: Thu, 5 Dec 2024 11:32:05 +0100 Subject: [PATCH 1/2] Added CMake as a build tool The libary can now be built and installed using CMake. --- CMakeLists.txt | 76 +++++++++++++++++++++++++++++++++++++++ cmake/argsConfig.cmake.in | 3 ++ cmake/examples.cmake | 33 +++++++++++++++++ cmake/tests.cmake | 12 +++++++ 4 files changed, 124 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 cmake/argsConfig.cmake.in create mode 100644 cmake/examples.cmake create mode 100644 cmake/tests.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..408d7a5 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,76 @@ +cmake_minimum_required(VERSION 3.16) +project(args + VERSION 3.3.1 + LANGUAGES C + DESCRIPTION "An argument-parsing library for C." +) +include(GNUInstallDirs) +include(CMakePackageConfigHelpers) + +# ------------------------------------------------ # +# Setting a variable for the name of the library # +# ------------------------------------------------ # +set(LIB_NAME args) + +# ---------------------------------- # +# Set C standard and compile flags # +# ---------------------------------- # + +set(CMAKE_C_STANDARD 99) +set(CMAKE_C_FLAGS_COMMANDS + "${CMAKE_C_FLAGS} -Wall -Wextra --pedantic -Wno-unused-parameter") + +add_library(${LIB_NAME} + src/args.c +) + +target_include_directories(${LIB_NAME} + PUBLIC + $ + $ +) + +# ------------------------- # +# Set installation config # +# ------------------------- # + +install(TARGETS ${LIB_NAME} + EXPORT ${LIB_NAME}Targets + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} +) +install(FILES src/args.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) + +# --------------------------------------------------------------- # +# Export targets so that they can be fetched by another project # +# --------------------------------------------------------------- # + +install(EXPORT ${LIB_NAME}Targets + FILE ${LIB_NAME}Targets.cmake + NAMESPACE dmullholl:: + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${LIB_NAME} +) + +configure_package_config_file( + cmake/argsConfig.cmake.in + ${CMAKE_CURRENT_BINARY_DIR}/${LIB_NAME}Config.cmake + INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${LIB_NAME} +) +write_basic_package_version_file( + ${CMAKE_CURRENT_BINARY_DIR}/${LIB_NAME}ConfigVersion.cmake + COMPATIBILITY SameMajorVersion +) + +install(FILES + ${CMAKE_CURRENT_BINARY_DIR}/${LIB_NAME}Config.cmake + ${CMAKE_CURRENT_BINARY_DIR}/${LIB_NAME}ConfigVersion.cmake + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${LIB_NAME} +) + +# ----------------------------------- # +# Special config for build and test # +# ----------------------------------- # + +include(cmake/tests.cmake) +include(cmake/examples.cmake) diff --git a/cmake/argsConfig.cmake.in b/cmake/argsConfig.cmake.in new file mode 100644 index 0000000..e60cb95 --- /dev/null +++ b/cmake/argsConfig.cmake.in @@ -0,0 +1,3 @@ +@PACKAGE_INIT@ + +include("${CMAKE_CURRENT_LIST_DIR}/argsTargets.cmake") diff --git a/cmake/examples.cmake b/cmake/examples.cmake new file mode 100644 index 0000000..09fa0ef --- /dev/null +++ b/cmake/examples.cmake @@ -0,0 +1,33 @@ +# ------------------------------- # +# Optionally build all examples # +# ------------------------------- # + +if(BUILD_EXAMPLES) + add_executable(example1 ${CMAKE_SOURCE_DIR}/src/example1.c) + add_executable(example2 ${CMAKE_SOURCE_DIR}/src/example2.c) + + target_link_libraries(example1 PRIVATE args) + target_link_libraries(example2 PRIVATE args) + + target_include_directories(example1 PRIVATE ${CMAKE_SOURCE_DIR}/src) +endif() + +# --------------------------- # +# Optionally build example1 # +# --------------------------- # + +if(BUILD_EXAMPLE1 AND NOT BUILD_EXAMPLES) + add_executable(example1 ${CMAKE_SOURCE_DIR}/src/example1.c) + target_link_libraries(example1 PRIVATE args) + target_include_directories(example1 PRIVATE ${CMAKE_SOURCE_DIR}/src) +endif() + +# --------------------------- # +# Optionally build example2 # +# --------------------------- # + +if(BUILD_EXAMPLE2 AND NOT BUILD_EXAMPLES) + add_executable(example2 ${CMAKE_SOURCE_DIR}/src/example2.c) + target_link_libraries(example2 PRIVATE args) + target_include_directories(example2 PRIVATE ${CMAKE_SOURCE_DIR}/src) +endif() diff --git a/cmake/tests.cmake b/cmake/tests.cmake new file mode 100644 index 0000000..424cdfa --- /dev/null +++ b/cmake/tests.cmake @@ -0,0 +1,12 @@ +# ---------------------------- # +# Optionally build the tests # +# ---------------------------- # + +if(BUILD_TESTS) + add_executable(tests ${CMAKE_SOURCE_DIR}/src/tests.c) + target_link_libraries(tests PRIVATE args) + target_include_directories(tests PRIVATE ${CMAKE_SOURCE_DIR}/src) + + enable_testing() ## Enables the possibility to run ctest + add_test(NAME tests COMMAND tests) +endif() From 34e34727f1225550c5b01894e63cb8ae9b914afb Mon Sep 17 00:00:00 2001 From: Linus Henke Date: Thu, 5 Dec 2024 11:40:48 +0100 Subject: [PATCH 2/2] Fixed typo. --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 408d7a5..ea4597c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -48,7 +48,7 @@ install(FILES src/args.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) install(EXPORT ${LIB_NAME}Targets FILE ${LIB_NAME}Targets.cmake - NAMESPACE dmullholl:: + NAMESPACE dmulholl:: DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${LIB_NAME} )