summaryrefslogtreecommitdiff
path: root/libs/cargs/CMakeLists.txt
blob: 4cd618ffbb96136066f64295d7132b7232976543 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
cmake_minimum_required(VERSION 3.14.7)

# set project name
project(cargs
  VERSION 1.0.3
  DESCRIPTION "A simple argument parser library"
  LANGUAGES C)

# include utilities
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
include(EnableWarnings)
include(CTest)
include(CreateTestList)
include(CMakePackageConfigHelpers)
include(GNUInstallDirs)

# configure requirements
set(CMAKE_C_STANDARD 11)

# setup target and directory names
set(LIBRARY_TARGET "cargs")
set(TEST_TARGET "cargstest")
set(INCLUDE_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/include")
set(SOURCE_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/src")
set(TEST_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/test")

# enable coverage if requested
if(ENABLE_COVERAGE)
  message("-- Coverage enabled")
  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
  set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --coverage")
endif()

# enable sanitizer
if(ENABLE_SANITIZER)
  message("-- Sanitizer enabled")
  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-omit-frame-pointer -fsanitize=${ENABLE_SANITIZER}")
  set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fno-omit-frame-pointer -fsanitize=${ENABLE_SANITIZER}")
endif()

# add the main library
add_library(${LIBRARY_TARGET}
  "${INCLUDE_DIRECTORY}/cargs.h"
  "${SOURCE_DIRECTORY}/cargs.c")
enable_warnings(${LIBRARY_TARGET})
target_include_directories(${LIBRARY_TARGET} PUBLIC 
  $<BUILD_INTERFACE:${INCLUDE_DIRECTORY}>
  $<INSTALL_INTERFACE:include>
)
set_target_properties(cargs PROPERTIES PUBLIC_HEADER "${INCLUDE_DIRECTORY}/cargs.h")
set_target_properties(cargs PROPERTIES DEFINE_SYMBOL CAG_EXPORTS)

# add shared library macro
if(BUILD_SHARED_LIBS)
  target_compile_definitions(cargs PUBLIC CAG_SHARED)
endif()

# add tests
if(ENABLE_TESTS)
  message("-- Tests enabled")
  enable_testing()
  create_test(DEFAULT option complex)
  create_test(DEFAULT option mixed)
  create_test(DEFAULT option ending)
  create_test(DEFAULT option long_missing_value)
  create_test(DEFAULT option short_missing_value)
  create_test(DEFAULT option long_space_value)
  create_test(DEFAULT option short_space_value)
  create_test(DEFAULT option long_equal_value)
  create_test(DEFAULT option short_equal_value)
  create_test(DEFAULT option combined)
  create_test(DEFAULT option unknown_long)
  create_test(DEFAULT option unknown_short)
  create_test(DEFAULT option alias)
  create_test(DEFAULT option simple_long)
  create_test(DEFAULT option simple)
  create_test(DEFAULT option print)
  create_test_list(DEFAULT "${TEST_DIRECTORY}/tests.h")

  add_executable(${TEST_TARGET}
    "${TEST_DIRECTORY}/main.c"
    "${TEST_DIRECTORY}/option_test.c")
  target_link_libraries(${TEST_TARGET} PUBLIC ${LIBRARY_TARGET})
  target_include_directories(${TEST_TARGET} PUBLIC "${INCLUDE_DIRECTORY}")
  enable_warnings(${TEST_TARGET})
endif()

# version file
configure_package_config_file("cmake/CargsConfig.cmake.in"
  ${CMAKE_CURRENT_BINARY_DIR}/CargsConfig.cmake
  INSTALL_DESTINATION ${LIB_INSTALL_DIR}/cargs/cmake)
write_basic_package_version_file(
  ${CMAKE_CURRENT_BINARY_DIR}/CargsConfigVersion.cmake
  COMPATIBILITY SameMajorVersion)

# installing
install(TARGETS cargs
  EXPORT CargsTargets)

install(FILES
  ${CMAKE_CURRENT_BINARY_DIR}/CargsConfig.cmake
  ${CMAKE_CURRENT_BINARY_DIR}/CargsConfigVersion.cmake
  DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/cargs)
install(EXPORT CargsTargets
  DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/cargs)