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