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