1 cmake_minimum_required(VERSION 3.13) 2 3 project(ucode C) 4 5 add_library(uc_defines INTERFACE) 6 7 list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake") 8 9 include(CheckFunctionExists) 10 include(CheckSymbolExists) 11 include(CheckCSourceCompiles) 12 include(CheckIPOSupported) 13 include(GNUInstallDirs) 14 15 option(BUILD_OPTIMIZE_SIZE "Optimize for size" ON) 16 if(BUILD_OPTIMIZE_SIZE) 17 add_compile_options( 18 -Os 19 ) 20 endif() 21 22 set(CMAKE_C_STANDARD 99) 23 set(CMAKE_C_STANDARD_REQUIRED ON) 24 set(CMAKE_C_EXTENSIONS ON) 25 26 add_compile_definitions( _GNU_SOURCE ) 27 add_compile_options( 28 -Wall 29 -Werror 30 -Wmissing-declarations 31 ) 32 33 if(CMAKE_C_COMPILER_VERSION VERSION_GREATER 6) 34 add_compile_options( 35 -Wextra 36 -Wformat 37 -Werror=implicit-function-declaration 38 -Werror=format-security 39 -Werror=format-nonliteral 40 ) 41 endif() 42 43 add_compile_options( 44 -Wno-unused-parameter 45 -Wno-error=unused-variable 46 ) 47 48 # signed overflow semantics are defined AND expected 49 add_compile_options(-fwrapv) 50 51 option(BUILD_FUNCTION_SECTIONS "Compile with -ffunction-sections" ON) 52 if(BUILD_FUNCTION_SECTIONS) 53 add_compile_options(-ffunction-sections) 54 endif() 55 56 # Omit the async stack-unwind tables (.eh_frame) in release builds. ucode is 57 # pure C with no exceptions and provides its own bytecode-level stack traces, 58 # so these tables only serve external C-stack unwinding (backtrace(), C-level 59 # crash reporters, profilers) and cost roughly 23 KB of the shared library. 60 # Debug and RelWithDebInfo keep them to preserve native stack traces. 61 option(BUILD_NO_UNWIND_TABLES "Omit async stack-unwind tables in release builds" ON) 62 if(BUILD_NO_UNWIND_TABLES) 63 # listing several configurations in a single $<CONFIG:> expression requires 64 # CMake >= 3.19, so combine single-configuration expressions to stay 65 # compatible with the declared minimum CMake version 66 add_compile_options( 67 $<$<OR:$<CONFIG:Release>,$<CONFIG:MinSizeRel>>:-fno-asynchronous-unwind-tables> 68 ) 69 endif() 70 71 # Link-time optimization in release builds. Cross translation-unit dead code 72 # elimination and folding of identical functions trims a further few percent 73 # from libucode. Off by default: it lengthens builds and assumes a matching LTO 74 # toolchain, so OpenWrt and default builds are unaffected unless requested. 75 # Only Release and MinSizeRel are enabled, Debug and RelWithDebInfo are not. 76 option(BUILD_LTO "Enable link-time optimization in release builds" OFF) 77 if(BUILD_LTO) 78 check_ipo_supported(RESULT lto_supported OUTPUT lto_error) 79 80 if(lto_supported) 81 set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_RELEASE ON) 82 set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_MINSIZEREL ON) 83 else() 84 message(WARNING "BUILD_LTO requested but IPO is unsupported: ${lto_error}") 85 endif() 86 endif() 87 88 check_c_source_compiles(" 89 int main() { 90 #ifndef _FORTIFY_SOURCE 91 #error No fortification 92 #endif 93 return 0; 94 } 95 " DEFAULT_FORTIFY_SOURCE) 96 97 if(NOT DEFAULT_FORTIFY_SOURCE) 98 add_compile_definitions( FORTIFY_SOURCE=2 ) 99 endif() 100 101 # Emscripten / WebAssembly build: 102 # cmake -B build-wasm -DCMAKE_TOOLCHAIN_FILE=cmake/emscripten.cmake 103 # 104 # Produces libucode.a and ucode-modules.a (modules with renamed 105 # uc_module_init symbols) for static linking into an embedder's 106 # executable. The CLI, udbg, dlopen plugins and examples are not built. 107 if(CMAKE_C_COMPILER_ID STREQUAL "Emscripten") 108 set(UCODE_WASM_DEFAULT ON) 109 else() 110 set(UCODE_WASM_DEFAULT OFF) 111 endif() 112 option(UCODE_WASM "Build static libraries for wasm32 (no CLI, no dlopen plugins)" ${UCODE_WASM_DEFAULT}) 113 114 include_directories(include) 115 116 option(COMPILE_SUPPORT "Support compilation from source" ON) 117 if(NOT COMPILE_SUPPORT) 118 target_compile_definitions(uc_defines INTERFACE NO_COMPILE) 119 endif() 120 121 if(NOT UCODE_WASM) 122 find_library(libuci NAMES uci) 123 find_library(libubox NAMES ubox) 124 find_library(libubus NAMES ubus) 125 find_library(libblobmsg_json NAMES blobmsg_json) 126 find_package(ZLIB) 127 find_library(libmd NAMES libmd.a md) 128 find_library(libffi NAMES ffi) 129 130 if(LINUX) 131 find_library(libnl_tiny NAMES nl-tiny) 132 133 if(libnl_tiny AND libubox) 134 set(DEFAULT_NL_SUPPORT ON) 135 endif() 136 endif() 137 138 if(libuci AND libubox) 139 set(DEFAULT_UCI_SUPPORT ON) 140 endif() 141 142 if(libubus AND libblobmsg_json) 143 set(DEFAULT_UBUS_SUPPORT ON) 144 endif() 145 146 if(libubox) 147 set(DEFAULT_ULOOP_SUPPORT ON) 148 endif() 149 150 if(ZLIB_FOUND) 151 set(DEFAULT_ZLIB_SUPPORT ON) 152 endif() 153 154 if(libmd) 155 set(DEFAULT_DIGEST_SUPPORT ON) 156 endif() 157 158 if(libffi) 159 set(DEFAULT_FFI_SUPPORT ON) 160 endif() 161 endif() 162 163 if(NOT UCODE_WASM) 164 option(DEBUG_SUPPORT "Debug plugin support" ON) 165 option(FS_SUPPORT "Filesystem plugin support" ON) 166 option(IO_SUPPORT "IO plugin support" ON) 167 option(MATH_SUPPORT "Math plugin support" ON) 168 option(NETADDR_SUPPORT "Network address plugin support" ON) 169 option(FFI_SUPPORT "FFI module support" ${DEFAULT_FFI_SUPPORT}) 170 option(UBUS_SUPPORT "Ubus plugin support" ${DEFAULT_UBUS_SUPPORT}) 171 option(UCI_SUPPORT "UCI plugin support" ${DEFAULT_UCI_SUPPORT}) 172 option(RTNL_SUPPORT "Route Netlink plugin support" ${DEFAULT_NL_SUPPORT}) 173 option(NL80211_SUPPORT "Wireless Netlink plugin support" ${DEFAULT_NL_SUPPORT}) 174 option(RESOLV_SUPPORT "NS resolve plugin support" ON) 175 option(STRUCT_SUPPORT "Struct plugin support" ON) 176 option(ULOOP_SUPPORT "Uloop plugin support" ${DEFAULT_ULOOP_SUPPORT}) 177 option(LOG_SUPPORT "Log plugin support" ON) 178 option(SOCKET_SUPPORT "Socket plugin support" ON) 179 option(SERIAL_SUPPORT "Serial port plugin support" ON) 180 option(ZLIB_SUPPORT "Zlib plugin support" ${DEFAULT_ZLIB_SUPPORT}) 181 option(DIGEST_SUPPORT "Digest plugin support" ${DEFAULT_DIGEST_SUPPORT}) 182 option(DIGEST_SUPPORT_EXTENDED "Enable additional hash algorithms" ${DEFAULT_DIGEST_SUPPORT}) 183 else() 184 # Modules built into ucode-modules.a; each is compiled with 185 # uc_module_init renamed to uc_module_init_<name> so several can be 186 # linked into one executable. Server-side socket APIs (bind/listen/ 187 # accept) are compiled in but not supported by emscripten. 188 set(UCODE_WASM_MODULES "io;math;struct;fs;zlib;resolv;socket;digest;netaddr" CACHE STRING "Modules built into ucode-modules.a") 189 endif() 190 191 if(UCODE_WASM) 192 # No dlopen under wasm: .so entries would only cause failed lookups. 193 # Embedders (e.g. the pen service) extend REQUIRE_SEARCH_PATH at 194 # runtime. 195 set(LIB_SEARCH_PATH "./*.uc" CACHE STRING "Default library search path") 196 else() 197 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") 198 endif() 199 string(REPLACE ":" "\", \"" LIB_SEARCH_DEFINE "${LIB_SEARCH_PATH}") 200 target_compile_definitions(uc_defines INTERFACE LIB_SEARCH_PATH="${LIB_SEARCH_DEFINE}") 201 202 if(APPLE) 203 set(UCODE_MODULE_LINK_OPTIONS "LINKER:-undefined,dynamic_lookup") 204 # TODO: localize target(s) and switch to target_compile_definitions() 205 add_compile_definitions( BIND_8_COMPAT ) 206 endif() 207 208 if(APPLE) 209 set(DEFAULT_LINK_GC_SECTIONS OFF) 210 else() 211 set(DEFAULT_LINK_GC_SECTIONS ON) 212 endif() 213 option(LINK_GC_SECTIONS "Link with --gc-sections" ${DEFAULT_LINK_GC_SECTIONS}) 214 if(LINK_GC_SECTIONS) 215 if(UNIT_TESTING) 216 message(WARNING "Unit testing **MAY** conflict with --gc-sections") 217 else() 218 add_link_options("LINKER:--gc-sections") 219 endif() 220 endif() 221 222 if(DEBUG) 223 add_compile_definitions( DEBUG ) 224 add_compile_options( 225 -Og 226 -g3 227 ) 228 else() 229 add_compile_definitions( NDEBUG ) 230 endif() 231 232 if(UCODE_WASM) 233 include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/emscripten-deps.cmake) 234 set(JSONC_INCLUDE_DIRS ${UCODE_DEPS_PREFIX}/json-c/dist/include) 235 set(JSONC_LINK_LIBRARIES libjson-c) 236 include_directories(${JSONC_INCLUDE_DIRS}) 237 # json-c is pinned by emscripten-deps.cmake; the features checked for 238 # below are all present in it 239 target_compile_definitions(uc_defines INTERFACE HAVE_PARSE_END HAVE_ARRAY_EXT HAVE_JSON_UINT64) 240 else() 241 include(FindPkgConfig) 242 pkg_check_modules(JSONC REQUIRED json-c) 243 include_directories(${JSONC_INCLUDE_DIRS}) 244 endif() 245 246 set(UCODE_SOURCES lexer.c lib.c vm.c chunk.c vallist.c compiler.c source.c types.c program.c platform.c) 247 if(UCODE_WASM) 248 add_library(libucode STATIC ${UCODE_SOURCES}) 249 set_target_properties(libucode PROPERTIES OUTPUT_NAME ucode) 250 else() 251 add_library(libucode SHARED ${UCODE_SOURCES}) 252 set(SOVERSION 0 CACHE STRING "Override ucode library version") 253 set_target_properties(libucode PROPERTIES OUTPUT_NAME ucode SOVERSION ${SOVERSION}) 254 endif() 255 target_link_libraries(libucode uc_defines ${JSONC_LINK_LIBRARIES}) 256 257 if(NOT UCODE_WASM) 258 set(CLI_SOURCES main.c) 259 add_executable(ucode ${CLI_SOURCES}) 260 target_link_libraries(ucode uc_defines libucode) 261 endif() 262 263 # Ensure rpath is used on macOS 264 set(CMAKE_MACOSX_RPATH ON) 265 266 # Automatically include linked library paths in rpath 267 set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) 268 269 if(APPLE) 270 set_target_properties(libucode PROPERTIES 271 INSTALL_NAME_DIR "@rpath" 272 ) 273 set_target_properties(ucode PROPERTIES 274 INSTALL_RPATH "@loader_path/../${CMAKE_INSTALL_LIBDIR}" 275 ) 276 endif() 277 278 if(APPLE AND CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) 279 execute_process( 280 COMMAND brew --prefix 281 OUTPUT_VARIABLE HOMEBREW_PREFIX 282 OUTPUT_STRIP_TRAILING_WHITESPACE 283 ERROR_QUIET 284 ) 285 286 if(HOMEBREW_PREFIX AND EXISTS "${HOMEBREW_PREFIX}") 287 message(STATUS "Using Homebrew prefix: ${HOMEBREW_PREFIX}") 288 set(CMAKE_INSTALL_PREFIX "${HOMEBREW_PREFIX}" CACHE PATH "Install path" FORCE) 289 endif() 290 endif() 291 292 if(NOT UCODE_WASM) 293 check_function_exists(dlopen DLOPEN_FUNCTION_EXISTS) 294 if(NOT DLOPEN_FUNCTION_EXISTS) 295 target_link_libraries(libucode dl) 296 endif() 297 298 check_function_exists(fmod FMOD_FUNCTION_EXISTS) 299 if(NOT FMOD_FUNCTION_EXISTS) 300 target_link_libraries(libucode m) 301 endif() 302 endif() 303 304 if(NOT UCODE_WASM) 305 list(APPEND CMAKE_REQUIRED_INCLUDES ${JSONC_INCLUDE_DIRS}) 306 list(APPEND CMAKE_REQUIRED_LIBRARIES ${JSONC_LINK_LIBRARIES}) 307 check_symbol_exists(json_tokener_get_parse_end "json-c/json.h" HAVE_PARSE_END) 308 if(HAVE_PARSE_END) 309 target_compile_definitions(uc_defines INTERFACE HAVE_PARSE_END) 310 endif() 311 check_symbol_exists(json_object_new_array_ext "json-c/json.h" HAVE_ARRAY_EXT) 312 if(HAVE_ARRAY_EXT) 313 target_compile_definitions(uc_defines INTERFACE HAVE_ARRAY_EXT) 314 endif() 315 check_symbol_exists(json_object_new_uint64 "json-c/json.h" HAVE_JSON_UINT64) 316 if(HAVE_JSON_UINT64) 317 target_compile_definitions(uc_defines INTERFACE HAVE_JSON_UINT64) 318 endif() 319 endif() 320 321 if(NOT UCODE_WASM) 322 set(LIBRARIES "") 323 324 if(DEBUG_SUPPORT) 325 set(LIBRARIES ${LIBRARIES} debug_lib) 326 add_library(debug_lib MODULE lib/debug.c lib/debug_remote.c lib/debug_proto.c) 327 set_target_properties(debug_lib PROPERTIES OUTPUT_NAME debug PREFIX "") 328 target_link_options(debug_lib PRIVATE ${UCODE_MODULE_LINK_OPTIONS}) 329 target_link_libraries(debug_lib PRIVATE libucode) 330 if(libubox) 331 find_path(uloop_include_dir NAMES libubox/uloop.h) 332 include_directories(${uloop_include_dir}) 333 target_link_libraries(debug_lib PRIVATE ${libubox} ${libucode}) 334 target_compile_definitions(debug_lib PRIVATE HAVE_ULOOP) 335 endif() 336 endif() 337 338 if(FS_SUPPORT) 339 set(LIBRARIES ${LIBRARIES} fs_lib) 340 add_library(fs_lib MODULE lib/fs.c) 341 set_target_properties(fs_lib PROPERTIES OUTPUT_NAME fs PREFIX "") 342 target_link_options(fs_lib PRIVATE ${UCODE_MODULE_LINK_OPTIONS}) 343 endif() 344 345 if(IO_SUPPORT) 346 set(LIBRARIES ${LIBRARIES} io_lib) 347 add_library(io_lib MODULE lib/io.c) 348 set_target_properties(io_lib PROPERTIES OUTPUT_NAME io PREFIX "") 349 target_link_options(io_lib PRIVATE ${UCODE_MODULE_LINK_OPTIONS}) 350 endif() 351 352 if(MATH_SUPPORT) 353 set(LIBRARIES ${LIBRARIES} math_lib) 354 add_library(math_lib MODULE lib/math.c) 355 set_target_properties(math_lib PROPERTIES OUTPUT_NAME math PREFIX "") 356 target_link_options(math_lib PRIVATE ${UCODE_MODULE_LINK_OPTIONS}) 357 check_function_exists(ceil CEIL_FUNCTION_EXISTS) 358 if(NOT CEIL_FUNCTION_EXISTS) 359 target_link_libraries(math_lib m) 360 endif() 361 endif() 362 363 if(UBUS_SUPPORT) 364 find_path(ubus_include_dir NAMES libubus.h) 365 include_directories(${ubus_include_dir}) 366 set(LIBRARIES ${LIBRARIES} ubus_lib) 367 add_library(ubus_lib MODULE lib/ubus.c) 368 set_target_properties(ubus_lib PROPERTIES OUTPUT_NAME ubus PREFIX "") 369 target_link_options(ubus_lib PRIVATE ${UCODE_MODULE_LINK_OPTIONS}) 370 target_link_libraries(ubus_lib ${libubus} ${libblobmsg_json}) 371 list(APPEND CMAKE_REQUIRED_LIBRARIES ${libubox} ${libubus}) 372 file(WRITE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/test.c" " 373 #include <libubus.h> 374 int main() { return UBUS_STATUS_NO_MEMORY; } 375 ") 376 try_compile(HAVE_NEW_UBUS_STATUS_CODES 377 ${CMAKE_BINARY_DIR} 378 "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/test.c") 379 check_function_exists(ubus_flush_requests HAVE_UBUS_FLUSH_REQUESTS) 380 check_function_exists(uloop_timeout_remaining64 REMAINING64_FUNCTION_EXISTS) 381 check_function_exists(ubus_channel_connect HAVE_CHANNEL_SUPPORT) 382 file(WRITE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/test2.c" " 383 #include <libubus.h> 384 int main() { struct ubus_subscriber sub = { .new_obj_cb = NULL }; return 0; } 385 ") 386 try_compile(HAVE_UBUS_NEW_OBJ_CB 387 ${CMAKE_BINARY_DIR} 388 "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/test2.c") 389 if(REMAINING64_FUNCTION_EXISTS) 390 target_compile_definitions(ubus_lib PRIVATE HAVE_ULOOP_TIMEOUT_REMAINING64) 391 endif() 392 if(HAVE_CHANNEL_SUPPORT) 393 target_compile_definitions(ubus_lib PRIVATE HAVE_UBUS_CHANNEL_SUPPORT) 394 endif() 395 if(HAVE_NEW_UBUS_STATUS_CODES) 396 target_compile_definitions(ubus_lib PRIVATE HAVE_NEW_UBUS_STATUS_CODES) 397 endif() 398 if(HAVE_UBUS_FLUSH_REQUESTS) 399 target_compile_definitions(ubus_lib PRIVATE HAVE_UBUS_FLUSH_REQUESTS) 400 endif() 401 if (HAVE_UBUS_NEW_OBJ_CB) 402 target_compile_definitions(ubus_lib PRIVATE HAVE_UBUS_NEW_OBJ_CB) 403 endif() 404 endif() 405 406 if(UCI_SUPPORT) 407 find_path(uci_include_dir uci.h) 408 include_directories(${uci_include_dir}) 409 set(LIBRARIES ${LIBRARIES} uci_lib) 410 add_library(uci_lib MODULE lib/uci.c) 411 list(APPEND CMAKE_REQUIRED_LIBRARIES ${libubox} ${libuci}) 412 check_function_exists(uci_set_conf2dir HAVE_UCI_CONF2DIR) 413 if (HAVE_UCI_CONF2DIR) 414 target_compile_definitions(uci_lib PRIVATE HAVE_UCI_CONF2DIR) 415 endif() 416 set_target_properties(uci_lib PROPERTIES OUTPUT_NAME uci PREFIX "") 417 target_link_options(uci_lib PRIVATE ${UCODE_MODULE_LINK_OPTIONS}) 418 target_link_libraries(uci_lib ${libuci} ${libubox}) 419 endif() 420 421 if(RTNL_SUPPORT) 422 find_path(nl_include_dir NAMES netlink/msg.h PATH_SUFFIXES libnl-tiny) 423 include_directories(${nl_include_dir}) 424 set(LIBRARIES ${LIBRARIES} rtnl_lib) 425 add_library(rtnl_lib MODULE lib/rtnl.c) 426 set_target_properties(rtnl_lib PROPERTIES OUTPUT_NAME rtnl PREFIX "") 427 target_link_options(rtnl_lib PRIVATE ${UCODE_MODULE_LINK_OPTIONS}) 428 target_link_libraries(rtnl_lib ${libnl_tiny} ${libubox}) 429 endif() 430 431 if(NL80211_SUPPORT) 432 find_path(nl_include_dir NAMES netlink/msg.h PATH_SUFFIXES libnl-tiny) 433 include_directories(${nl_include_dir}) 434 set(LIBRARIES ${LIBRARIES} nl80211_lib) 435 add_library(nl80211_lib MODULE lib/nl80211.c) 436 set_target_properties(nl80211_lib PROPERTIES OUTPUT_NAME nl80211 PREFIX "") 437 target_link_options(nl80211_lib PRIVATE ${UCODE_MODULE_LINK_OPTIONS}) 438 target_link_libraries(nl80211_lib ${libnl_tiny} ${libubox}) 439 endif() 440 441 if(RESOLV_SUPPORT) 442 set(LIBRARIES ${LIBRARIES} resolv_lib) 443 add_library(resolv_lib MODULE lib/resolv.c) 444 set_target_properties(resolv_lib PROPERTIES OUTPUT_NAME resolv PREFIX "") 445 target_link_options(resolv_lib PRIVATE ${UCODE_MODULE_LINK_OPTIONS}) 446 check_function_exists(res_mkquery RES_MKQUERY_FUNCTION_EXISTS) 447 check_function_exists(ns_initparse NS_INITARSE_FUNCTION_EXISTS) 448 check_function_exists(clock_gettime CLOCK_GETTIME_FUNCTION_EXISTS) 449 if(NOT RES_MKQUERY_FUNCTION_EXISTS OR NOT NS_INITARSE_FUNCTION_EXISTS) 450 target_link_libraries(resolv_lib resolv) 451 endif() 452 if(NOT CLOCK_GETTIME_FUNCTION_EXISTS) 453 target_link_libraries(resolv_lib rt) 454 endif() 455 endif() 456 457 if(STRUCT_SUPPORT) 458 set(LIBRARIES ${LIBRARIES} struct_lib) 459 add_library(struct_lib MODULE lib/struct.c) 460 set_target_properties(struct_lib PROPERTIES OUTPUT_NAME struct PREFIX "") 461 target_link_options(struct_lib PRIVATE ${UCODE_MODULE_LINK_OPTIONS}) 462 check_function_exists(frexp FREXP_FUNCTION_EXISTS) 463 if(NOT FREXP_FUNCTION_EXISTS) 464 target_link_libraries(struct_lib m) 465 endif() 466 endif() 467 468 if(ULOOP_SUPPORT) 469 find_path(uloop_include_dir NAMES libubox/uloop.h) 470 include_directories(${uloop_include_dir}) 471 set(LIBRARIES ${LIBRARIES} uloop_lib) 472 add_library(uloop_lib MODULE lib/uloop.c) 473 set_target_properties(uloop_lib PROPERTIES OUTPUT_NAME uloop PREFIX "") 474 target_link_options(uloop_lib PRIVATE ${UCODE_MODULE_LINK_OPTIONS}) 475 list(APPEND CMAKE_REQUIRED_LIBRARIES ${libubox}) 476 check_function_exists(uloop_timeout_remaining64 REMAINING64_FUNCTION_EXISTS) 477 check_function_exists(uloop_interval_set INTERVAL_FUNCTION_EXISTS) 478 check_function_exists(uloop_signal_add SIGNAL_FUNCTION_EXISTS) 479 if(REMAINING64_FUNCTION_EXISTS) 480 target_compile_definitions(uloop_lib PRIVATE HAVE_ULOOP_TIMEOUT_REMAINING64) 481 endif() 482 if(INTERVAL_FUNCTION_EXISTS) 483 target_compile_definitions(uloop_lib PRIVATE HAVE_ULOOP_INTERVAL) 484 endif() 485 if(SIGNAL_FUNCTION_EXISTS) 486 target_compile_definitions(uloop_lib PRIVATE HAVE_ULOOP_SIGNAL) 487 endif() 488 target_link_libraries(uloop_lib ${libubox}) 489 endif() 490 491 if(LOG_SUPPORT) 492 set(LIBRARIES ${LIBRARIES} log_lib) 493 add_library(log_lib MODULE lib/log.c) 494 set_target_properties(log_lib PROPERTIES OUTPUT_NAME log PREFIX "") 495 target_link_options(log_lib PRIVATE ${UCODE_MODULE_LINK_OPTIONS}) 496 if(libubox) 497 find_path(ulog_include_dir NAMES libubox/ulog.h) 498 include_directories(${ulog_include_dir}) 499 target_link_libraries(log_lib ${libubox}) 500 target_compile_definitions(log_lib PRIVATE HAVE_ULOG) 501 endif() 502 endif() 503 504 if(SOCKET_SUPPORT) 505 set(LIBRARIES ${LIBRARIES} socket_lib) 506 add_library(socket_lib MODULE lib/socket.c) 507 set_target_properties(socket_lib PROPERTIES OUTPUT_NAME socket PREFIX "") 508 target_link_options(socket_lib PRIVATE ${UCODE_MODULE_LINK_OPTIONS}) 509 endif() 510 511 if(SERIAL_SUPPORT) 512 set(LIBRARIES ${LIBRARIES} serial_lib) 513 add_library(serial_lib MODULE lib/serial.c) 514 set_target_properties(serial_lib PROPERTIES OUTPUT_NAME serial PREFIX "") 515 target_link_options(serial_lib PRIVATE ${UCODE_MODULE_LINK_OPTIONS}) 516 endif() 517 518 if(ZLIB_SUPPORT) 519 set(LIBRARIES ${LIBRARIES} zlib_lib) 520 add_library(zlib_lib MODULE lib/zlib.c) 521 set_target_properties(zlib_lib PROPERTIES OUTPUT_NAME zlib PREFIX "") 522 target_link_options(zlib_lib PRIVATE ${UCODE_MODULE_LINK_OPTIONS}) 523 target_link_libraries(zlib_lib ZLIB::ZLIB) 524 if(ZLIB_CHUNK_SIZE) 525 target_compile_definitions(zlib_lib PRIVATE UC_ZLIB_CHUNK=${ZLIB_CHUNK_SIZE}) 526 endif() 527 endif() 528 529 if(DIGEST_SUPPORT) 530 pkg_check_modules(LIBMD REQUIRED libmd) 531 include_directories(${LIBMD_INCLUDE_DIRS}) 532 set(LIBRARIES ${LIBRARIES} digest_lib) 533 add_library(digest_lib MODULE lib/digest.c) 534 set_target_properties(digest_lib PROPERTIES OUTPUT_NAME digest PREFIX "") 535 if(DIGEST_SUPPORT_EXTENDED) 536 target_compile_definitions(digest_lib PRIVATE HAVE_DIGEST_EXTENDED) 537 endif() 538 target_link_options(digest_lib PRIVATE ${UCODE_MODULE_LINK_OPTIONS}) 539 target_link_libraries(digest_lib ${libmd}) 540 endif() 541 542 if(FFI_SUPPORT) 543 pkg_check_modules(LIBFFI REQUIRED libffi) 544 include_directories(${LIBFFI_INCLUDE_DIRS}) 545 set(LIBRARIES ${LIBRARIES} ffi_lib) 546 add_library(ffi_lib MODULE lib/ffi/uc_cparse.c lib/ffi/uc_ctype.c lib/ffi/uc_cconv.c lib/ffi/uc_cdata.c lib/ffi/ffi.c) 547 set_target_properties(ffi_lib PROPERTIES OUTPUT_NAME ffi PREFIX "") 548 549 set(CMAKE_REQUIRED_INCLUDES ${LIBFFI_INCLUDE_DIRS}) 550 set(CMAKE_REQUIRED_LIBRARIES ${LIBFFI_LIBRARIES}) 551 552 check_c_source_compiles(" 553 #include <ffi.h> 554 int main() { return FFI_BAD_ARGTYPE; } 555 " HAVE_FFI_BAD_ARGTYPE) 556 557 if(HAVE_FFI_BAD_ARGTYPE) 558 target_compile_definitions(ffi_lib PRIVATE HAVE_FFI_BAD_ARGTYPE) 559 endif() 560 561 # A backend provides the predefined complex number types only when it 562 # defines FFI_TARGET_HAS_COMPLEX_TYPE, which powerpc does not. Only declare 563 # the symbols, as the check may compile to a static library when cross 564 # compiling, where referencing them would not be caught by the linker. 565 check_c_source_compiles(" 566 #include <ffi.h> 567 ffi_type *types[] = { &ffi_type_complex_float, &ffi_type_complex_double, 568 &ffi_type_complex_longdouble }; 569 int main() { return types != 0; } 570 " HAVE_FFI_COMPLEX_TYPES) 571 572 if(HAVE_FFI_COMPLEX_TYPES) 573 target_compile_definitions(ffi_lib PRIVATE HAVE_FFI_COMPLEX_TYPES) 574 endif() 575 576 target_link_options(ffi_lib PRIVATE ${UCODE_MODULE_LINK_OPTIONS}) 577 target_link_libraries(ffi_lib ${LIBFFI_LINK_LIBRARIES}) 578 endif() 579 580 if(NETADDR_SUPPORT) 581 set(LIBRARIES ${LIBRARIES} netaddr_lib) 582 add_library(netaddr_lib MODULE lib/netaddr.c) 583 set_target_properties(netaddr_lib PROPERTIES OUTPUT_NAME netaddr PREFIX "") 584 target_link_options(netaddr_lib PRIVATE ${UCODE_MODULE_LINK_OPTIONS}) 585 endif() 586 endif() # UCODE_WASM 587 588 if(UCODE_WASM) 589 # ucode-modules.a: the selected modules with renamed uc_module_init 590 # symbols, for static linking into an embedder's executable together 591 # with libucode.a. The embedder calls uc_module_init_<name>() itself 592 # (e.g. to preload the modules into the global `modules` dictionary) 593 # and lists the names in config->force_dynlink_list. 594 set(UCODE_MODULE_OBJECTS "") 595 foreach(mod IN LISTS UCODE_WASM_MODULES) 596 add_library(ucode_mod_${mod} OBJECT lib/${mod}.c) 597 target_compile_definitions(ucode_mod_${mod} PRIVATE uc_module_init=uc_module_init_${mod}) 598 target_include_directories(ucode_mod_${mod} PRIVATE include ${JSONC_INCLUDE_DIRS}) 599 # the json-c headers only exist once the external project has built 600 add_dependencies(ucode_mod_${mod} json-c-build) 601 if(mod STREQUAL "zlib") 602 target_compile_options(ucode_mod_${mod} PRIVATE -sUSE_ZLIB=1) 603 endif() 604 if(mod STREQUAL "socket") 605 # emscripten's CMSG_NXTHDR() macro expands to a size_t/ptrdiff_t 606 # comparison; not fixable from the call site 607 target_compile_options(ucode_mod_${mod} PRIVATE -Wno-sign-compare) 608 endif() 609 if(mod STREQUAL "digest") 610 target_include_directories(ucode_mod_${mod} PRIVATE ${UCODE_DEPS_PREFIX}/libmd/dist/include) 611 add_dependencies(ucode_mod_${mod} libmd-build) 612 endif() 613 list(APPEND UCODE_MODULE_OBJECTS $<TARGET_OBJECTS:ucode_mod_${mod}>) 614 endforeach() 615 616 add_library(ucode-modules STATIC ${UCODE_MODULE_OBJECTS}) 617 set_target_properties(ucode-modules PROPERTIES PREFIX "") 618 target_link_libraries(ucode-modules PUBLIC libucode libjson-c) 619 if("digest" IN_LIST UCODE_WASM_MODULES) 620 target_link_libraries(ucode-modules PUBLIC libmd) 621 endif() 622 if("zlib" IN_LIST UCODE_WASM_MODULES) 623 target_link_options(ucode-modules INTERFACE -sUSE_ZLIB=1) 624 endif() 625 endif() 626 627 if(NOT UCODE_WASM) 628 if(UNIT_TESTING) 629 enable_testing() 630 add_compile_definitions( UNIT_TESTING ) 631 add_subdirectory(tests) 632 list(APPEND CMAKE_CTEST_ARGUMENTS "--output-on-failure") 633 634 if(CMAKE_C_COMPILER_ID STREQUAL "Clang") 635 add_executable(ucode-san ${CLI_SOURCES} ${UCODE_SOURCES}) 636 set_property(TARGET ucode-san PROPERTY ENABLE_EXPORTS 1) 637 target_link_libraries(ucode-san uc_defines ${JSONC_LINK_LIBRARIES}) 638 target_compile_options(ucode-san PRIVATE -g -fno-omit-frame-pointer -fsanitize=undefined,address,leak -fno-sanitize-recover=all) 639 target_link_options(ucode-san PRIVATE -fsanitize=undefined,address,leak) 640 endif() 641 endif() 642 endif() # UCODE_WASM 643 644 if(NOT UCODE_WASM) 645 add_executable(udbg udbg.c debug_highlight.c debug_lineedit.c) 646 target_link_libraries(udbg PRIVATE libucode ${JSONC_LINK_LIBRARIES}) 647 endif() 648 649 if(UCODE_WASM) 650 install(TARGETS libucode ucode-modules ARCHIVE DESTINATION lib) 651 else() 652 install(TARGETS ucode udbg RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) 653 install(TARGETS libucode LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}) 654 install(TARGETS ${LIBRARIES} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}/ucode) 655 endif() 656 657 if(NOT UCODE_WASM) 658 add_custom_target(utpl ALL COMMAND ${CMAKE_COMMAND} -E create_symlink ucode utpl) 659 install(FILES ${CMAKE_CURRENT_BINARY_DIR}/utpl DESTINATION ${CMAKE_INSTALL_BINDIR}) 660 661 if(COMPILE_SUPPORT) 662 add_custom_target(ucc ALL COMMAND ${CMAKE_COMMAND} -E create_symlink ucode ucc) 663 install(FILES ${CMAKE_CURRENT_BINARY_DIR}/ucc DESTINATION ${CMAKE_INSTALL_BINDIR}) 664 endif() 665 endif() 666 667 file(GLOB UCODE_HEADERS "include/ucode/*.h") 668 install(FILES ${UCODE_HEADERS} DESTINATION include/ucode) 669 670 if(NOT UCODE_WASM) 671 add_subdirectory(examples) 672 endif()
This page was automatically generated by LXR 0.3.1. • OpenWrt