1 cmake_minimum_required(VERSION 3.13) 2 3 project(ucode C) 4 5 add_library(uc_defines INTERFACE) 6 7 include(CheckFunctionExists) 8 include(CheckSymbolExists) 9 include(CheckCSourceCompiles) 10 include(GNUInstallDirs) 11 12 option(BUILD_OPTIMIZE_SIZE "Optimize for size" ON) 13 if(BUILD_OPTIMIZE_SIZE) 14 add_compile_options( 15 -Os 16 ) 17 endif() 18 19 set(CMAKE_C_STANDARD 99) 20 set(CMAKE_C_STANDARD_REQUIRED ON) 21 set(CMAKE_C_EXTENSIONS ON) 22 23 add_compile_definitions( _GNU_SOURCE ) 24 add_compile_options( 25 -Wall 26 -Werror 27 -Wmissing-declarations 28 ) 29 30 if(CMAKE_C_COMPILER_VERSION VERSION_GREATER 6) 31 add_compile_options( 32 -Wextra 33 -Wformat 34 -Werror=implicit-function-declaration 35 -Werror=format-security 36 -Werror=format-nonliteral 37 ) 38 endif() 39 40 add_compile_options( 41 -Wno-unused-parameter 42 -Wno-error=unused-variable 43 ) 44 45 # signed overflow semantics are defined AND expected 46 add_compile_options(-fwrapv) 47 48 option(BUILD_FUNCTION_SECTIONS "Compile with -ffunction-sections" ON) 49 if(BUILD_FUNCTION_SECTIONS) 50 add_compile_options(-ffunction-sections) 51 endif() 52 53 check_c_source_compiles(" 54 int main() { 55 #ifndef _FORTIFY_SOURCE 56 #error No fortification 57 #endif 58 return 0; 59 } 60 " DEFAULT_FORTIFY_SOURCE) 61 62 if(NOT DEFAULT_FORTIFY_SOURCE) 63 add_compile_definitions( FORTIFY_SOURCE=2 ) 64 endif() 65 66 include_directories(include) 67 68 option(COMPILE_SUPPORT "Support compilation from source" ON) 69 if(NOT COMPILE_SUPPORT) 70 target_compile_definitions(uc_defines INTERFACE NO_COMPILE) 71 endif() 72 73 find_library(libuci NAMES uci) 74 find_library(libubox NAMES ubox) 75 find_library(libubus NAMES ubus) 76 find_library(libblobmsg_json NAMES blobmsg_json) 77 find_package(ZLIB) 78 find_library(libmd NAMES libmd.a md) 79 80 if(LINUX) 81 find_library(libnl_tiny NAMES nl-tiny) 82 83 if(libnl_tiny AND libubox) 84 set(DEFAULT_NL_SUPPORT ON) 85 endif() 86 endif() 87 88 if(libuci AND libubox) 89 set(DEFAULT_UCI_SUPPORT ON) 90 endif() 91 92 if(libubus AND libblobmsg_json) 93 set(DEFAULT_UBUS_SUPPORT ON) 94 endif() 95 96 if(libubox) 97 set(DEFAULT_ULOOP_SUPPORT ON) 98 endif() 99 100 if(ZLIB_FOUND) 101 set(DEFAULT_ZLIB_SUPPORT ON) 102 endif() 103 104 if(libmd) 105 set(DEFAULT_DIGEST_SUPPORT ON) 106 endif() 107 108 option(DEBUG_SUPPORT "Debug plugin support" ON) 109 option(FS_SUPPORT "Filesystem plugin support" ON) 110 option(IO_SUPPORT "IO plugin support" ON) 111 option(MATH_SUPPORT "Math plugin support" ON) 112 option(UBUS_SUPPORT "Ubus plugin support" ${DEFAULT_UBUS_SUPPORT}) 113 option(UCI_SUPPORT "UCI plugin support" ${DEFAULT_UCI_SUPPORT}) 114 option(RTNL_SUPPORT "Route Netlink plugin support" ${DEFAULT_NL_SUPPORT}) 115 option(NL80211_SUPPORT "Wireless Netlink plugin support" ${DEFAULT_NL_SUPPORT}) 116 option(RESOLV_SUPPORT "NS resolve plugin support" ON) 117 option(STRUCT_SUPPORT "Struct plugin support" ON) 118 option(ULOOP_SUPPORT "Uloop plugin support" ${DEFAULT_ULOOP_SUPPORT}) 119 option(LOG_SUPPORT "Log plugin support" ON) 120 option(SOCKET_SUPPORT "Socket plugin support" ON) 121 option(ZLIB_SUPPORT "Zlib plugin support" ${DEFAULT_ZLIB_SUPPORT}) 122 option(DIGEST_SUPPORT "Digest plugin support" ${DEFAULT_DIGEST_SUPPORT}) 123 option(DIGEST_SUPPORT_EXTENDED "Enable additional hash algorithms" ${DEFAULT_DIGEST_SUPPORT}) 124 125 set(LIB_SEARCH_PATH "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/ucode/*.so:${CMAKE_INSTALL_PREFIX}/share/ucode/*.uc:./*.so:./*.uc" CACHE STRING "Default library search path") 126 string(REPLACE ":" "\", \"" LIB_SEARCH_DEFINE "${LIB_SEARCH_PATH}") 127 target_compile_definitions(uc_defines INTERFACE LIB_SEARCH_PATH="${LIB_SEARCH_DEFINE}") 128 129 if(APPLE) 130 set(UCODE_MODULE_LINK_OPTIONS "LINKER:-undefined,dynamic_lookup") 131 # TODO: localize target(s) and switch to target_compile_definitions() 132 add_compile_definitions( BIND_8_COMPAT ) 133 endif() 134 135 if(APPLE) 136 set(DEFAULT_LINK_GC_SECTIONS OFF) 137 else() 138 set(DEFAULT_LINK_GC_SECTIONS ON) 139 endif() 140 option(LINK_GC_SECTIONS "Link with --gc-sections" ${DEFAULT_LINK_GC_SECTIONS}) 141 if(LINK_GC_SECTIONS) 142 if(UNIT_TESTING) 143 message(WARNING "Unit testing **MAY** conflict with --gc-sections") 144 else() 145 add_link_options("LINKER:--gc-sections") 146 endif() 147 endif() 148 149 if(DEBUG) 150 add_compile_definitions( DEBUG ) 151 add_compile_options( 152 -Og 153 -g3 154 ) 155 else() 156 add_compile_definitions( NDEBUG ) 157 endif() 158 159 include(FindPkgConfig) 160 pkg_check_modules(JSONC REQUIRED json-c) 161 include_directories(${JSONC_INCLUDE_DIRS}) 162 163 set(UCODE_SOURCES lexer.c lib.c vm.c chunk.c vallist.c compiler.c source.c types.c program.c platform.c) 164 add_library(libucode SHARED ${UCODE_SOURCES}) 165 set(SOVERSION 0 CACHE STRING "Override ucode library version") 166 set_target_properties(libucode PROPERTIES OUTPUT_NAME ucode SOVERSION ${SOVERSION}) 167 target_link_libraries(libucode uc_defines ${JSONC_LINK_LIBRARIES}) 168 169 set(CLI_SOURCES main.c) 170 add_executable(ucode ${CLI_SOURCES}) 171 target_link_libraries(ucode uc_defines libucode) 172 173 check_function_exists(dlopen DLOPEN_FUNCTION_EXISTS) 174 if(NOT DLOPEN_FUNCTION_EXISTS) 175 target_link_libraries(libucode dl) 176 endif() 177 178 check_function_exists(fmod FMOD_FUNCTION_EXISTS) 179 if(NOT FMOD_FUNCTION_EXISTS) 180 target_link_libraries(libucode m) 181 endif() 182 183 list(APPEND CMAKE_REQUIRED_INCLUDES ${JSONC_INCLUDE_DIRS}) 184 list(APPEND CMAKE_REQUIRED_LIBRARIES ${JSONC_LINK_LIBRARIES}) 185 check_symbol_exists(json_tokener_get_parse_end "json-c/json.h" HAVE_PARSE_END) 186 if(HAVE_PARSE_END) 187 target_compile_definitions(uc_defines INTERFACE HAVE_PARSE_END) 188 endif() 189 check_symbol_exists(json_object_new_array_ext "json-c/json.h" HAVE_ARRAY_EXT) 190 if(HAVE_ARRAY_EXT) 191 target_compile_definitions(uc_defines INTERFACE HAVE_ARRAY_EXT) 192 endif() 193 check_symbol_exists(json_object_new_uint64 "json-c/json.h" HAVE_JSON_UINT64) 194 if(HAVE_JSON_UINT64) 195 target_compile_definitions(uc_defines INTERFACE HAVE_JSON_UINT64) 196 endif() 197 198 set(LIBRARIES "") 199 200 if(DEBUG_SUPPORT) 201 set(LIBRARIES ${LIBRARIES} debug_lib) 202 add_library(debug_lib MODULE lib/debug.c) 203 set_target_properties(debug_lib PROPERTIES OUTPUT_NAME debug PREFIX "") 204 target_link_options(debug_lib PRIVATE ${UCODE_MODULE_LINK_OPTIONS}) 205 if(libubox) 206 find_path(uloop_include_dir NAMES libubox/uloop.h) 207 include_directories(${uloop_include_dir}) 208 target_link_libraries(debug_lib ${libubox} ${libucode}) 209 target_compile_definitions(debug_lib PRIVATE HAVE_ULOOP) 210 endif() 211 endif() 212 213 if(FS_SUPPORT) 214 set(LIBRARIES ${LIBRARIES} fs_lib) 215 add_library(fs_lib MODULE lib/fs.c) 216 set_target_properties(fs_lib PROPERTIES OUTPUT_NAME fs PREFIX "") 217 target_link_options(fs_lib PRIVATE ${UCODE_MODULE_LINK_OPTIONS}) 218 endif() 219 220 if(IO_SUPPORT) 221 set(LIBRARIES ${LIBRARIES} io_lib) 222 add_library(io_lib MODULE lib/io.c) 223 set_target_properties(io_lib PROPERTIES OUTPUT_NAME io PREFIX "") 224 target_link_options(io_lib PRIVATE ${UCODE_MODULE_LINK_OPTIONS}) 225 endif() 226 227 if(MATH_SUPPORT) 228 set(LIBRARIES ${LIBRARIES} math_lib) 229 add_library(math_lib MODULE lib/math.c) 230 set_target_properties(math_lib PROPERTIES OUTPUT_NAME math PREFIX "") 231 target_link_options(math_lib PRIVATE ${UCODE_MODULE_LINK_OPTIONS}) 232 check_function_exists(ceil CEIL_FUNCTION_EXISTS) 233 if(NOT CEIL_FUNCTION_EXISTS) 234 target_link_libraries(math_lib m) 235 endif() 236 endif() 237 238 if(UBUS_SUPPORT) 239 find_path(ubus_include_dir NAMES libubus.h) 240 include_directories(${ubus_include_dir}) 241 set(LIBRARIES ${LIBRARIES} ubus_lib) 242 add_library(ubus_lib MODULE lib/ubus.c) 243 set_target_properties(ubus_lib PROPERTIES OUTPUT_NAME ubus PREFIX "") 244 target_link_options(ubus_lib PRIVATE ${UCODE_MODULE_LINK_OPTIONS}) 245 target_link_libraries(ubus_lib ${libubus} ${libblobmsg_json}) 246 list(APPEND CMAKE_REQUIRED_LIBRARIES ${libubox} ${libubus}) 247 file(WRITE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/test.c" " 248 #include <libubus.h> 249 int main() { return UBUS_STATUS_NO_MEMORY; } 250 ") 251 try_compile(HAVE_NEW_UBUS_STATUS_CODES 252 ${CMAKE_BINARY_DIR} 253 "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/test.c") 254 check_function_exists(ubus_flush_requests HAVE_UBUS_FLUSH_REQUESTS) 255 check_function_exists(uloop_timeout_remaining64 REMAINING64_FUNCTION_EXISTS) 256 check_function_exists(ubus_channel_connect HAVE_CHANNEL_SUPPORT) 257 file(WRITE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/test2.c" " 258 #include <libubus.h> 259 int main() { struct ubus_subscriber sub = { .new_obj_cb = NULL }; return 0; } 260 ") 261 try_compile(HAVE_UBUS_NEW_OBJ_CB 262 ${CMAKE_BINARY_DIR} 263 "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/test2.c") 264 if(REMAINING64_FUNCTION_EXISTS) 265 target_compile_definitions(ubus_lib PRIVATE HAVE_ULOOP_TIMEOUT_REMAINING64) 266 endif() 267 if(HAVE_CHANNEL_SUPPORT) 268 target_compile_definitions(ubus_lib PRIVATE HAVE_UBUS_CHANNEL_SUPPORT) 269 endif() 270 if(HAVE_NEW_UBUS_STATUS_CODES) 271 target_compile_definitions(ubus_lib PRIVATE HAVE_NEW_UBUS_STATUS_CODES) 272 endif() 273 if(HAVE_UBUS_FLUSH_REQUESTS) 274 target_compile_definitions(ubus_lib PRIVATE HAVE_UBUS_FLUSH_REQUESTS) 275 endif() 276 if (HAVE_UBUS_NEW_OBJ_CB) 277 target_compile_definitions(ubus_lib PRIVATE HAVE_UBUS_NEW_OBJ_CB) 278 endif() 279 endif() 280 281 if(UCI_SUPPORT) 282 find_path(uci_include_dir uci.h) 283 include_directories(${uci_include_dir}) 284 set(LIBRARIES ${LIBRARIES} uci_lib) 285 add_library(uci_lib MODULE lib/uci.c) 286 list(APPEND CMAKE_REQUIRED_LIBRARIES ${libubox} ${libuci}) 287 check_function_exists(uci_set_conf2dir HAVE_UCI_CONF2DIR) 288 if (HAVE_UCI_CONF2DIR) 289 target_compile_definitions(uci_lib PRIVATE HAVE_UCI_CONF2DIR) 290 endif() 291 set_target_properties(uci_lib PROPERTIES OUTPUT_NAME uci PREFIX "") 292 target_link_options(uci_lib PRIVATE ${UCODE_MODULE_LINK_OPTIONS}) 293 target_link_libraries(uci_lib ${libuci} ${libubox}) 294 endif() 295 296 if(RTNL_SUPPORT) 297 find_path(nl_include_dir NAMES netlink/msg.h PATH_SUFFIXES libnl-tiny) 298 include_directories(${nl_include_dir}) 299 set(LIBRARIES ${LIBRARIES} rtnl_lib) 300 add_library(rtnl_lib MODULE lib/rtnl.c) 301 set_target_properties(rtnl_lib PROPERTIES OUTPUT_NAME rtnl PREFIX "") 302 target_link_options(rtnl_lib PRIVATE ${UCODE_MODULE_LINK_OPTIONS}) 303 target_link_libraries(rtnl_lib ${libnl_tiny} ${libubox}) 304 endif() 305 306 if(NL80211_SUPPORT) 307 find_path(nl_include_dir NAMES netlink/msg.h PATH_SUFFIXES libnl-tiny) 308 include_directories(${nl_include_dir}) 309 set(LIBRARIES ${LIBRARIES} nl80211_lib) 310 add_library(nl80211_lib MODULE lib/nl80211.c) 311 set_target_properties(nl80211_lib PROPERTIES OUTPUT_NAME nl80211 PREFIX "") 312 target_link_options(nl80211_lib PRIVATE ${UCODE_MODULE_LINK_OPTIONS}) 313 target_link_libraries(nl80211_lib ${libnl_tiny} ${libubox}) 314 endif() 315 316 if(RESOLV_SUPPORT) 317 set(LIBRARIES ${LIBRARIES} resolv_lib) 318 add_library(resolv_lib MODULE lib/resolv.c) 319 set_target_properties(resolv_lib PROPERTIES OUTPUT_NAME resolv PREFIX "") 320 target_link_options(resolv_lib PRIVATE ${UCODE_MODULE_LINK_OPTIONS}) 321 check_function_exists(res_mkquery RES_MKQUERY_FUNCTION_EXISTS) 322 check_function_exists(ns_initparse NS_INITARSE_FUNCTION_EXISTS) 323 check_function_exists(clock_gettime CLOCK_GETTIME_FUNCTION_EXISTS) 324 if(NOT RES_MKQUERY_FUNCTION_EXISTS OR NOT NS_INITARSE_FUNCTION_EXISTS) 325 target_link_libraries(resolv_lib resolv) 326 endif() 327 if(NOT CLOCK_GETTIME_FUNCTION_EXISTS) 328 target_link_libraries(resolv_lib rt) 329 endif() 330 endif() 331 332 if(STRUCT_SUPPORT) 333 set(LIBRARIES ${LIBRARIES} struct_lib) 334 add_library(struct_lib MODULE lib/struct.c) 335 set_target_properties(struct_lib PROPERTIES OUTPUT_NAME struct PREFIX "") 336 target_link_options(struct_lib PRIVATE ${UCODE_MODULE_LINK_OPTIONS}) 337 check_function_exists(frexp FREXP_FUNCTION_EXISTS) 338 if(NOT FREXP_FUNCTION_EXISTS) 339 target_link_libraries(struct_lib m) 340 endif() 341 endif() 342 343 if(ULOOP_SUPPORT) 344 find_path(uloop_include_dir NAMES libubox/uloop.h) 345 include_directories(${uloop_include_dir}) 346 set(LIBRARIES ${LIBRARIES} uloop_lib) 347 add_library(uloop_lib MODULE lib/uloop.c) 348 set_target_properties(uloop_lib PROPERTIES OUTPUT_NAME uloop PREFIX "") 349 target_link_options(uloop_lib PRIVATE ${UCODE_MODULE_LINK_OPTIONS}) 350 list(APPEND CMAKE_REQUIRED_LIBRARIES ${libubox}) 351 check_function_exists(uloop_timeout_remaining64 REMAINING64_FUNCTION_EXISTS) 352 check_function_exists(uloop_interval_set INTERVAL_FUNCTION_EXISTS) 353 check_function_exists(uloop_signal_add SIGNAL_FUNCTION_EXISTS) 354 if(REMAINING64_FUNCTION_EXISTS) 355 target_compile_definitions(uloop_lib PRIVATE HAVE_ULOOP_TIMEOUT_REMAINING64) 356 endif() 357 if(INTERVAL_FUNCTION_EXISTS) 358 target_compile_definitions(uloop_lib PRIVATE HAVE_ULOOP_INTERVAL) 359 endif() 360 if(SIGNAL_FUNCTION_EXISTS) 361 target_compile_definitions(uloop_lib PRIVATE HAVE_ULOOP_SIGNAL) 362 endif() 363 target_link_libraries(uloop_lib ${libubox}) 364 endif() 365 366 if(LOG_SUPPORT) 367 set(LIBRARIES ${LIBRARIES} log_lib) 368 add_library(log_lib MODULE lib/log.c) 369 set_target_properties(log_lib PROPERTIES OUTPUT_NAME log PREFIX "") 370 target_link_options(log_lib PRIVATE ${UCODE_MODULE_LINK_OPTIONS}) 371 if(libubox) 372 find_path(ulog_include_dir NAMES libubox/ulog.h) 373 include_directories(${ulog_include_dir}) 374 target_link_libraries(log_lib ${libubox}) 375 target_compile_definitions(log_lib PRIVATE HAVE_ULOG) 376 endif() 377 endif() 378 379 if(SOCKET_SUPPORT) 380 set(LIBRARIES ${LIBRARIES} socket_lib) 381 add_library(socket_lib MODULE lib/socket.c) 382 set_target_properties(socket_lib PROPERTIES OUTPUT_NAME socket PREFIX "") 383 target_link_options(socket_lib PRIVATE ${UCODE_MODULE_LINK_OPTIONS}) 384 endif() 385 386 if(ZLIB_SUPPORT) 387 set(LIBRARIES ${LIBRARIES} zlib_lib) 388 add_library(zlib_lib MODULE lib/zlib.c) 389 set_target_properties(zlib_lib PROPERTIES OUTPUT_NAME zlib PREFIX "") 390 target_link_options(zlib_lib PRIVATE ${UCODE_MODULE_LINK_OPTIONS}) 391 target_link_libraries(zlib_lib ZLIB::ZLIB) 392 if(ZLIB_CHUNK_SIZE) 393 target_compile_definitions(zlib_lib PRIVATE UC_ZLIB_CHUNK=${ZLIB_CHUNK_SIZE}) 394 endif() 395 endif() 396 397 if(DIGEST_SUPPORT) 398 pkg_check_modules(LIBMD REQUIRED libmd) 399 include_directories(${LIBMD_INCLUDE_DIRS}) 400 set(LIBRARIES ${LIBRARIES} digest_lib) 401 add_library(digest_lib MODULE lib/digest.c) 402 set_target_properties(digest_lib PROPERTIES OUTPUT_NAME digest PREFIX "") 403 if(DIGEST_SUPPORT_EXTENDED) 404 target_compile_definitions(digest_lib PRIVATE HAVE_DIGEST_EXTENDED) 405 endif() 406 target_link_options(digest_lib PRIVATE ${UCODE_MODULE_LINK_OPTIONS}) 407 target_link_libraries(digest_lib ${libmd}) 408 endif() 409 410 if(UNIT_TESTING) 411 enable_testing() 412 add_compile_definitions( UNIT_TESTING ) 413 add_subdirectory(tests) 414 list(APPEND CMAKE_CTEST_ARGUMENTS "--output-on-failure") 415 416 if(CMAKE_C_COMPILER_ID STREQUAL "Clang") 417 add_executable(ucode-san ${CLI_SOURCES} ${UCODE_SOURCES}) 418 set_property(TARGET ucode-san PROPERTY ENABLE_EXPORTS 1) 419 target_link_libraries(ucode-san uc_defines ${JSONC_LINK_LIBRARIES}) 420 target_compile_options(ucode-san PRIVATE -g -fno-omit-frame-pointer -fsanitize=undefined,address,leak -fno-sanitize-recover=all) 421 target_link_options(ucode-san PRIVATE -fsanitize=undefined,address,leak) 422 endif() 423 endif() 424 425 install(TARGETS ucode RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) 426 install(TARGETS libucode LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}) 427 install(TARGETS ${LIBRARIES} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}/ucode) 428 429 add_custom_target(utpl ALL COMMAND ${CMAKE_COMMAND} -E create_symlink ucode utpl) 430 install(FILES ${CMAKE_CURRENT_BINARY_DIR}/utpl DESTINATION ${CMAKE_INSTALL_BINDIR}) 431 432 if(COMPILE_SUPPORT) 433 add_custom_target(ucc ALL COMMAND ${CMAKE_COMMAND} -E create_symlink ucode ucc) 434 install(FILES ${CMAKE_CURRENT_BINARY_DIR}/ucc DESTINATION ${CMAKE_INSTALL_BINDIR}) 435 endif() 436 437 file(GLOB UCODE_HEADERS "include/ucode/*.h") 438 install(FILES ${UCODE_HEADERS} DESTINATION include/ucode) 439 440 add_subdirectory(examples)
This page was automatically generated by LXR 0.3.1. • OpenWrt