Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
$<INSTALL_INTERFACE:include>
)

# ------------------------- #
# 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 dmulholl::
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)
3 changes: 3 additions & 0 deletions cmake/argsConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@PACKAGE_INIT@

include("${CMAKE_CURRENT_LIST_DIR}/argsTargets.cmake")
33 changes: 33 additions & 0 deletions cmake/examples.cmake
Original file line number Diff line number Diff line change
@@ -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()
12 changes: 12 additions & 0 deletions cmake/tests.cmake
Original file line number Diff line number Diff line change
@@ -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()