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(uloop_timeout_remaining64 REMAINING64_FUNCTION_EXISTS) 202 check_function_exists(ubus_channel_connect HAVE_CHANNEL_SUPPORT) 203 file(WRITE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/test2.c" " 204 #include <libubus.h> 205 int main() { struct ubus_subscriber sub = { .new_obj_cb = NULL }; return 0; } 206 ") 207 try_compile(HAVE_UBUS_NEW_OBJ_CB 208 ${CMAKE_BINARY_DIR} 209 "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/test2.c") 210 if(REMAINING64_FUNCTION_EXISTS) 211 target_compile_definitions(ubus_lib PUBLIC HAVE_ULOOP_TIMEOUT_REMAINING64) 212 endif() 213 if(HAVE_CHANNEL_SUPPORT) 214 target_compile_definitions(ubus_lib PUBLIC HAVE_UBUS_CHANNEL_SUPPORT) 215 endif() 216 if(HAVE_NEW_UBUS_STATUS_CODES) 217 add_definitions(-DHAVE_NEW_UBUS_STATUS_CODES) 218 endif() 219 if (HAVE_UBUS_NEW_OBJ_CB) 220 target_compile_definitions(ubus_lib PUBLIC HAVE_UBUS_NEW_OBJ_CB) 221 endif() 222 if(FD_SET_CB_EXISTS) 223 target_compile_definitions(ubus_lib PUBLIC HAVE_ULOOP_FD_SET_CB) 224 endif() 225 endif() 226 227 if(UCI_SUPPORT) 228 find_path(uci_include_dir uci.h) 229 include_directories(${uci_include_dir}) 230 set(LIBRARIES ${LIBRARIES} uci_lib) 231 add_library(uci_lib MODULE lib/uci.c) 232 list(APPEND CMAKE_REQUIRED_LIBRARIES ${libubox} ${libuci}) 233 check_function_exists(uci_set_conf2dir HAVE_UCI_CONF2DIR) 234 if (HAVE_UCI_CONF2DIR) 235 target_compile_definitions(uci_lib PUBLIC HAVE_UCI_CONF2DIR) 236 endif() 237 set_target_properties(uci_lib PROPERTIES OUTPUT_NAME uci PREFIX "") 238 target_link_options(uci_lib PRIVATE ${UCODE_MODULE_LINK_OPTIONS}) 239 target_link_libraries(uci_lib ${libuci} ${libubox}) 240 endif() 241 242 if(RTNL_SUPPORT) 243 find_path(nl_include_dir NAMES netlink/msg.h PATH_SUFFIXES libnl-tiny) 244 include_directories(${nl_include_dir}) 245 set(LIBRARIES ${LIBRARIES} rtnl_lib) 246 add_library(rtnl_lib MODULE lib/rtnl.c) 247 set_target_properties(rtnl_lib PROPERTIES OUTPUT_NAME rtnl PREFIX "") 248 target_link_options(rtnl_lib PRIVATE ${UCODE_MODULE_LINK_OPTIONS}) 249 target_link_libraries(rtnl_lib ${libnl_tiny} ${libubox}) 250 endif() 251 252 if(NL80211_SUPPORT) 253 find_path(nl_include_dir NAMES netlink/msg.h PATH_SUFFIXES libnl-tiny) 254 include_directories(${nl_include_dir}) 255 set(LIBRARIES ${LIBRARIES} nl80211_lib) 256 add_library(nl80211_lib MODULE lib/nl80211.c) 257 set_target_properties(nl80211_lib PROPERTIES OUTPUT_NAME nl80211 PREFIX "") 258 target_link_options(nl80211_lib PRIVATE ${UCODE_MODULE_LINK_OPTIONS}) 259 target_link_libraries(nl80211_lib ${libnl_tiny} ${libubox}) 260 endif() 261 262 if(RESOLV_SUPPORT) 263 set(LIBRARIES ${LIBRARIES} resolv_lib) 264 add_library(resolv_lib MODULE lib/resolv.c) 265 set_target_properties(resolv_lib PROPERTIES OUTPUT_NAME resolv PREFIX "") 266 target_link_options(resolv_lib PRIVATE ${UCODE_MODULE_LINK_OPTIONS}) 267 check_function_exists(res_mkquery RES_MKQUERY_FUNCTION_EXISTS) 268 check_function_exists(ns_initparse NS_INITARSE_FUNCTION_EXISTS) 269 check_function_exists(clock_gettime CLOCK_GETTIME_FUNCTION_EXISTS) 270 if(NOT RES_MKQUERY_FUNCTION_EXISTS OR NOT NS_INITARSE_FUNCTION_EXISTS) 271 target_link_libraries(resolv_lib resolv) 272 endif() 273 if(NOT CLOCK_GETTIME_FUNCTION_EXISTS) 274 target_link_libraries(resolv_lib rt) 275 endif() 276 endif() 277 278 if(STRUCT_SUPPORT) 279 set(LIBRARIES ${LIBRARIES} struct_lib) 280 add_library(struct_lib MODULE lib/struct.c) 281 set_target_properties(struct_lib PROPERTIES OUTPUT_NAME struct PREFIX "") 282 target_link_options(struct_lib PRIVATE ${UCODE_MODULE_LINK_OPTIONS}) 283 check_function_exists(frexp FREXP_FUNCTION_EXISTS) 284 if(NOT FREXP_FUNCTION_EXISTS) 285 target_link_libraries(struct_lib m) 286 endif() 287 endif() 288 289 if(ULOOP_SUPPORT) 290 find_path(uloop_include_dir NAMES libubox/uloop.h) 291 include_directories(${uloop_include_dir}) 292 set(LIBRARIES ${LIBRARIES} uloop_lib) 293 add_library(uloop_lib MODULE lib/uloop.c) 294 set_target_properties(uloop_lib PROPERTIES OUTPUT_NAME uloop PREFIX "") 295 target_link_options(uloop_lib PRIVATE ${UCODE_MODULE_LINK_OPTIONS}) 296 list(APPEND CMAKE_REQUIRED_LIBRARIES ${libubox}) 297 check_function_exists(uloop_timeout_remaining64 REMAINING64_FUNCTION_EXISTS) 298 check_function_exists(uloop_interval_set INTERVAL_FUNCTION_EXISTS) 299 check_function_exists(uloop_signal_add SIGNAL_FUNCTION_EXISTS) 300 if(REMAINING64_FUNCTION_EXISTS) 301 target_compile_definitions(uloop_lib PUBLIC HAVE_ULOOP_TIMEOUT_REMAINING64) 302 endif() 303 if(INTERVAL_FUNCTION_EXISTS) 304 target_compile_definitions(uloop_lib PUBLIC HAVE_ULOOP_INTERVAL) 305 endif() 306 if(SIGNAL_FUNCTION_EXISTS) 307 target_compile_definitions(uloop_lib PUBLIC HAVE_ULOOP_SIGNAL) 308 endif() 309 target_link_libraries(uloop_lib ${libubox}) 310 endif() 311 312 if(LOG_SUPPORT) 313 set(LIBRARIES ${LIBRARIES} log_lib) 314 add_library(log_lib MODULE lib/log.c) 315 set_target_properties(log_lib PROPERTIES OUTPUT_NAME log PREFIX "") 316 target_link_options(log_lib PRIVATE ${UCODE_MODULE_LINK_OPTIONS}) 317 if(libubox) 318 find_path(ulog_include_dir NAMES libubox/ulog.h) 319 include_directories(${ulog_include_dir}) 320 target_link_libraries(log_lib ${libubox}) 321 set_target_properties(log_lib PROPERTIES COMPILE_DEFINITIONS HAVE_ULOG) 322 endif() 323 endif() 324 325 if(SOCKET_SUPPORT) 326 set(LIBRARIES ${LIBRARIES} socket_lib) 327 add_library(socket_lib MODULE lib/socket.c) 328 set_target_properties(socket_lib PROPERTIES OUTPUT_NAME socket PREFIX "") 329 target_link_options(socket_lib PRIVATE ${UCODE_MODULE_LINK_OPTIONS}) 330 endif() 331 332 if(ZLIB_SUPPORT) 333 set(LIBRARIES ${LIBRARIES} zlib_lib) 334 add_library(zlib_lib MODULE lib/zlib.c) 335 set_target_properties(zlib_lib PROPERTIES OUTPUT_NAME zlib PREFIX "") 336 target_link_options(zlib_lib PRIVATE ${UCODE_MODULE_LINK_OPTIONS}) 337 target_link_libraries(zlib_lib ZLIB::ZLIB) 338 endif() 339 340 if(DIGEST_SUPPORT) 341 pkg_check_modules(LIBMD REQUIRED libmd) 342 include_directories(${LIBMD_INCLUDE_DIRS}) 343 set(LIBRARIES ${LIBRARIES} digest_lib) 344 add_library(digest_lib MODULE lib/digest.c) 345 set_target_properties(digest_lib PROPERTIES OUTPUT_NAME digest PREFIX "") 346 if(DIGEST_SUPPORT_EXTENDED) 347 target_compile_definitions(digest_lib PUBLIC HAVE_DIGEST_EXTENDED) 348 endif() 349 target_link_options(digest_lib PRIVATE ${UCODE_MODULE_LINK_OPTIONS}) 350 target_link_libraries(digest_lib ${libmd}) 351 endif() 352 353 if(UNIT_TESTING) 354 enable_testing() 355 add_definitions(-DUNIT_TESTING) 356 add_subdirectory(tests) 357 list(APPEND CMAKE_CTEST_ARGUMENTS "--output-on-failure") 358 359 if(CMAKE_C_COMPILER_ID STREQUAL "Clang") 360 add_executable(ucode-san ${CLI_SOURCES} ${UCODE_SOURCES}) 361 set_property(TARGET ucode-san PROPERTY ENABLE_EXPORTS 1) 362 target_link_libraries(ucode-san ${JSONC_LINK_LIBRARIES}) 363 target_compile_options(ucode-san PRIVATE -g -fno-omit-frame-pointer -fsanitize=undefined,address,leak -fno-sanitize-recover=all) 364 target_link_options(ucode-san PRIVATE -fsanitize=undefined,address,leak) 365 endif() 366 endif() 367 368 install(TARGETS ucode RUNTIME DESTINATION bin) 369 install(TARGETS libucode LIBRARY DESTINATION lib) 370 install(TARGETS ${LIBRARIES} LIBRARY DESTINATION lib/ucode) 371 372 add_custom_target(utpl ALL COMMAND ${CMAKE_COMMAND} -E create_symlink ucode utpl) 373 install(FILES ${CMAKE_CURRENT_BINARY_DIR}/utpl DESTINATION bin) 374 375 if(COMPILE_SUPPORT) 376 add_custom_target(ucc ALL COMMAND ${CMAKE_COMMAND} -E create_symlink ucode ucc) 377 install(FILES ${CMAKE_CURRENT_BINARY_DIR}/ucc DESTINATION bin) 378 endif() 379 380 file(GLOB UCODE_HEADERS "include/ucode/*.h") 381 install(FILES ${UCODE_HEADERS} DESTINATION include/ucode) 382 383 add_subdirectory(examples)
This page was automatically generated by LXR 0.3.1. • OpenWrt