CMakeLists.txt (11340B)
1 cmake_minimum_required(VERSION 3.9.2) 2 3 # set project name 4 project(cwalk 5 VERSION 1.2.9 6 DESCRIPTION "A simple path library" 7 HOMEPAGE_URL "https://likle.github.io/cwalk/" 8 LANGUAGES C) 9 10 # include utilities 11 list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake") 12 include(EnableWarnings) 13 include(CTest) 14 include(CreateTestList) 15 include(CMakePackageConfigHelpers) 16 include(GNUInstallDirs) 17 18 # configure requirements 19 set(CMAKE_C_STANDARD 11) 20 21 # setup target and directory names 22 set(INCLUDE_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/include") 23 set(SOURCE_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/src") 24 set(TEST_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/test") 25 26 # enable coverage if requested 27 if(ENABLE_COVERAGE) 28 message("-- Coverage enabled") 29 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage") 30 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --coverage") 31 endif() 32 33 # enable sanitizer 34 if(ENABLE_SANITIZER) 35 message("-- Sanitizer enabled") 36 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-omit-frame-pointer -fsanitize=${ENABLE_SANITIZER}") 37 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fno-omit-frame-pointer -fsanitize=${ENABLE_SANITIZER}") 38 endif() 39 40 # add the main executable 41 add_library(cwalk 42 "${INCLUDE_DIRECTORY}/cwalk.h" 43 "${SOURCE_DIRECTORY}/cwalk.c") 44 enable_warnings(cwalk) 45 target_include_directories(cwalk PUBLIC 46 $<BUILD_INTERFACE:${INCLUDE_DIRECTORY}> 47 $<INSTALL_INTERFACE:include> 48 ) 49 set_target_properties(cwalk PROPERTIES PUBLIC_HEADER "${INCLUDE_DIRECTORY}/cwalk.h") 50 set_target_properties(cwalk PROPERTIES DEFINE_SYMBOL CWK_EXPORTS) 51 52 # add shared library macro 53 if(BUILD_SHARED_LIBS) 54 target_compile_definitions(cwalk PUBLIC CWK_SHARED) 55 endif() 56 57 # enable tests 58 if(ENABLE_TESTS) 59 message("-- Tests enabled") 60 enable_testing() 61 62 create_test_list(DEFAULT cwalktest) 63 create_test(DEFAULT absolute simple) 64 create_test(DEFAULT absolute absolute_path) 65 create_test(DEFAULT absolute unix_relative_base) 66 create_test(DEFAULT absolute windows_relative_base) 67 create_test(DEFAULT absolute mixed) 68 create_test(DEFAULT absolute normalization) 69 create_test(DEFAULT absolute too_far) 70 create_test(DEFAULT absolute check) 71 create_test(DEFAULT absolute buffer_reuse) 72 create_test(DEFAULT basename simple) 73 create_test(DEFAULT basename empty) 74 create_test(DEFAULT basename trailing_separator) 75 create_test(DEFAULT basename trailing_separators) 76 create_test(DEFAULT basename no_separators) 77 create_test(DEFAULT basename special_directories) 78 create_test(DEFAULT basename root) 79 create_test(DEFAULT basename windows) 80 create_test(DEFAULT basename change_simple) 81 create_test(DEFAULT basename change_empty_path) 82 create_test(DEFAULT basename change_only_root) 83 create_test(DEFAULT basename change_empty_basename) 84 create_test(DEFAULT basename change_relative) 85 create_test(DEFAULT basename change_trim) 86 create_test(DEFAULT basename change_trim_only_root) 87 create_test(DEFAULT dirname simple) 88 create_test(DEFAULT dirname empty) 89 create_test(DEFAULT dirname trailing_separator) 90 create_test(DEFAULT dirname trailing_separators) 91 create_test(DEFAULT dirname no_separators) 92 create_test(DEFAULT dirname special_directories) 93 create_test(DEFAULT dirname root) 94 create_test(DEFAULT dirname three_segments) 95 create_test(DEFAULT dirname relative) 96 create_test(DEFAULT extension get_simple) 97 create_test(DEFAULT extension get_without) 98 create_test(DEFAULT extension get_first) 99 create_test(DEFAULT extension get_last) 100 create_test(DEFAULT extension get_multiple) 101 create_test(DEFAULT extension check_simple) 102 create_test(DEFAULT extension check_empty) 103 create_test(DEFAULT extension check_without) 104 create_test(DEFAULT extension change_simple) 105 create_test(DEFAULT extension change_no_basename) 106 create_test(DEFAULT extension change_no_extension) 107 create_test(DEFAULT extension change_with_dot) 108 create_test(DEFAULT extension change_overlap) 109 create_test(DEFAULT extension change_overlap_long) 110 create_test(DEFAULT extension change_hidden_file) 111 create_test(DEFAULT extension change_with_trailing_slash) 112 create_test(DEFAULT guess empty_string) 113 create_test(DEFAULT guess windows_root) 114 create_test(DEFAULT guess unix_root) 115 create_test(DEFAULT guess windows_separator) 116 create_test(DEFAULT guess unix_separator) 117 create_test(DEFAULT guess hidden_file) 118 create_test(DEFAULT guess extension) 119 create_test(DEFAULT guess unguessable) 120 create_test(DEFAULT intersection simple) 121 create_test(DEFAULT intersection trailing_separator) 122 create_test(DEFAULT intersection double_separator) 123 create_test(DEFAULT intersection empty) 124 create_test(DEFAULT intersection unequal_roots) 125 create_test(DEFAULT intersection relative_absolute_mix) 126 create_test(DEFAULT intersection same_roots) 127 create_test(DEFAULT intersection one_root_only) 128 create_test(DEFAULT intersection relative_base) 129 create_test(DEFAULT intersection relative_other) 130 create_test(DEFAULT intersection skipped_end) 131 create_test(DEFAULT is_absolute absolute) 132 create_test(DEFAULT is_absolute unc) 133 create_test(DEFAULT is_absolute device_unc) 134 create_test(DEFAULT is_absolute device_dot) 135 create_test(DEFAULT is_absolute device_question_mark) 136 create_test(DEFAULT is_absolute relative) 137 create_test(DEFAULT is_absolute windows_backslash) 138 create_test(DEFAULT is_absolute windows_slash) 139 create_test(DEFAULT is_absolute unix_backslash) 140 create_test(DEFAULT is_absolute unix_drive) 141 create_test(DEFAULT is_absolute absolute_drive) 142 create_test(DEFAULT is_absolute relative_drive) 143 create_test(DEFAULT is_absolute relative_windows) 144 create_test(DEFAULT is_absolute root) 145 create_test(DEFAULT is_absolute dir) 146 create_test(DEFAULT is_relative absolute) 147 create_test(DEFAULT is_relative unc) 148 create_test(DEFAULT is_relative device_unc) 149 create_test(DEFAULT is_relative device_dot) 150 create_test(DEFAULT is_relative device_question_mark) 151 create_test(DEFAULT is_relative relative) 152 create_test(DEFAULT is_relative windows_backslash) 153 create_test(DEFAULT is_relative windows_slash) 154 create_test(DEFAULT is_relative unix_backslash) 155 create_test(DEFAULT is_relative unix_drive) 156 create_test(DEFAULT is_relative absolute_drive) 157 create_test(DEFAULT is_relative relative_drive) 158 create_test(DEFAULT is_relative relative_windows) 159 create_test(DEFAULT join simple) 160 create_test(DEFAULT join navigate_back) 161 create_test(DEFAULT join empty) 162 create_test(DEFAULT join two_absolute) 163 create_test(DEFAULT join two_unc) 164 create_test(DEFAULT join with_two_roots) 165 create_test(DEFAULT join back_after_root) 166 create_test(DEFAULT join relative_back_after_root) 167 create_test(DEFAULT join multiple) 168 create_test(DEFAULT normalize do_nothing) 169 create_test(DEFAULT normalize navigate_back) 170 create_test(DEFAULT normalize relative_too_far) 171 create_test(DEFAULT normalize absolute_too_far) 172 create_test(DEFAULT normalize terminated) 173 create_test(DEFAULT normalize double_separator) 174 create_test(DEFAULT normalize remove_current) 175 create_test(DEFAULT normalize mixed) 176 create_test(DEFAULT normalize overlap) 177 create_test(DEFAULT normalize empty) 178 create_test(DEFAULT normalize only_separators) 179 create_test(DEFAULT normalize back_after_root) 180 create_test(DEFAULT normalize forward_slashes) 181 create_test(DEFAULT relative simple) 182 create_test(DEFAULT relative relative) 183 create_test(DEFAULT relative long_base) 184 create_test(DEFAULT relative long_target) 185 create_test(DEFAULT relative equal) 186 create_test(DEFAULT relative same_base) 187 create_test(DEFAULT relative base_skipped_end) 188 create_test(DEFAULT relative target_skipped_end) 189 create_test(DEFAULT relative base_div_skipped_end) 190 create_test(DEFAULT relative target_div_skipped_end) 191 create_test(DEFAULT relative skip_all) 192 create_test(DEFAULT relative different_roots) 193 create_test(DEFAULT relative relative_and_absolute) 194 create_test(DEFAULT relative check) 195 create_test(DEFAULT relative root_path_unix) 196 create_test(DEFAULT relative root_path_windows) 197 create_test(DEFAULT relative root_forward_slashes) 198 create_test(DEFAULT root absolute) 199 create_test(DEFAULT root unc) 200 create_test(DEFAULT root device_unc) 201 create_test(DEFAULT root device_dot) 202 create_test(DEFAULT root device_question_mark) 203 create_test(DEFAULT root relative) 204 create_test(DEFAULT root windows_backslash) 205 create_test(DEFAULT root windows_slash) 206 create_test(DEFAULT root unix_backslash) 207 create_test(DEFAULT root unix_drive) 208 create_test(DEFAULT root absolute_drive) 209 create_test(DEFAULT root relative_drive) 210 create_test(DEFAULT root relative_windows) 211 create_test(DEFAULT root change_simple) 212 create_test(DEFAULT root change_empty) 213 create_test(DEFAULT root change_separators) 214 create_test(DEFAULT root change_overlapping) 215 create_test(DEFAULT root change_without_root) 216 create_test(DEFAULT segment first) 217 create_test(DEFAULT segment last) 218 create_test(DEFAULT segment next) 219 create_test(DEFAULT segment next_too_far) 220 create_test(DEFAULT segment previous_absolute) 221 create_test(DEFAULT segment previous_relative) 222 create_test(DEFAULT segment previous_absolute_one_char_first) 223 create_test(DEFAULT segment previous_relative_one_char_first) 224 create_test(DEFAULT segment previous_too_far) 225 create_test(DEFAULT segment previous_too_far_root) 226 create_test(DEFAULT segment type) 227 create_test(DEFAULT segment back_with_root) 228 create_test(DEFAULT segment change_simple) 229 create_test(DEFAULT segment change_first) 230 create_test(DEFAULT segment change_last) 231 create_test(DEFAULT segment change_trim) 232 create_test(DEFAULT segment change_empty) 233 create_test(DEFAULT segment change_with_separator) 234 create_test(DEFAULT segment change_overlap) 235 create_test(DEFAULT windows change_style) 236 create_test(DEFAULT windows get_root) 237 create_test(DEFAULT windows get_unc_root) 238 create_test(DEFAULT windows get_root_separator) 239 create_test(DEFAULT windows get_root_relative) 240 create_test(DEFAULT windows intersection_case) 241 create_test(DEFAULT windows root_backslash) 242 create_test(DEFAULT windows root_empty) 243 write_test_file(DEFAULT "${TEST_DIRECTORY}/tests.h") 244 245 add_executable(cwalktest 246 "${TEST_DIRECTORY}/main.c" 247 "${TEST_DIRECTORY}/absolute_test.c" 248 "${TEST_DIRECTORY}/basename_test.c" 249 "${TEST_DIRECTORY}/dirname_test.c" 250 "${TEST_DIRECTORY}/extension_test.c" 251 "${TEST_DIRECTORY}/guess_test.c" 252 "${TEST_DIRECTORY}/intersection_test.c" 253 "${TEST_DIRECTORY}/is_absolute_test.c" 254 "${TEST_DIRECTORY}/is_relative_test.c" 255 "${TEST_DIRECTORY}/join_test.c" 256 "${TEST_DIRECTORY}/normalize_test.c" 257 "${TEST_DIRECTORY}/relative_test.c" 258 "${TEST_DIRECTORY}/root_test.c" 259 "${TEST_DIRECTORY}/segment_test.c" 260 "${TEST_DIRECTORY}/windows_test.c") 261 enable_warnings(cwalktest) 262 263 target_link_libraries(cwalktest PRIVATE cwalk) 264 endif() 265 266 write_basic_package_version_file("CwalkConfigVersion.cmake" 267 VERSION ${cwalk_VERSION} 268 COMPATIBILITY SameMajorVersion) 269 270 configure_file(cwalk.pc.in ${PROJECT_BINARY_DIR}/cwalk.pc @ONLY) 271 272 install(TARGETS cwalk 273 EXPORT CwalkTargets) 274 275 install(FILES 276 "${CMAKE_CURRENT_SOURCE_DIR}/cmake/CwalkConfig.cmake" 277 DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/cwalk) 278 279 install(FILES 280 ${PROJECT_BINARY_DIR}/cwalk.pc 281 DESTINATION lib/pkgconfig/) 282 283 install(EXPORT CwalkTargets 284 FILE CwalkTargets.cmake 285 DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/cwalk)