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