cmake_minimum_required(VERSION 3.13) include(CheckFunctionExists) include(CheckSymbolExists) PROJECT(ucode C) ADD_DEFINITIONS(-Os -Wall -Werror --std=gnu99 -ffunction-sections -D_GNU_SOURCE) IF(CMAKE_C_COMPILER_VERSION VERSION_GREATER 6) ADD_DEFINITIONS(-Wextra -Werror=implicit-function-declaration) ADD_DEFINITIONS(-Wformat -Werror=format-security -Werror=format-nonliteral) ENDIF() ADD_DEFINITIONS(-Wmissing-declarations -Wno-error=unused-variable -Wno-unused-parameter) INCLUDE_DIRECTORIES(include) OPTION(FS_SUPPORT "Filesystem plugin support" ON) OPTION(MATH_SUPPORT "Math plugin support" ON) OPTION(UBUS_SUPPORT "Ubus plugin support" ON) OPTION(UCI_SUPPORT "UCI plugin support" ON) OPTION(LEGACY_SUPPORT "Support deprecated syntax features" ON) SET(LIB_SEARCH_PATH "${CMAKE_INSTALL_PREFIX}/lib/ucode/*.so:${CMAKE_INSTALL_PREFIX}/share/ucode/*.uc:./*.so:./*.uc" CACHE STRING "Default library search path") STRING(REPLACE ":" "\", \"" LIB_SEARCH_DEFINE "${LIB_SEARCH_PATH}") ADD_DEFINITIONS(-DLIB_SEARCH_PATH="${LIB_SEARCH_DEFINE}") IF(NOT APPLE) SET(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "-Wl,--gc-sections") ENDIF() FIND_LIBRARY(json NAMES json-c json) IF(DEBUG) ADD_DEFINITIONS(-DDEBUG -g3 -O0) ELSE() ADD_DEFINITIONS(-DNDEBUG) ENDIF() IF(NOT LEGACY_SUPPORT) ADD_DEFINITIONS(-DNO_LEGACY) ENDIF() INCLUDE(FindPkgConfig) PKG_CHECK_MODULES(JSONC json-c json) IF(JSONC_FOUND) ADD_DEFINITIONS(-DJSONC) INCLUDE_DIRECTORIES(${JSONC_INCLUDE_DIRS}) ENDIF() SET(UCODE_SOURCES lexer.c lib.c vm.c chunk.c value.c compiler.c source.c types.c) ADD_LIBRARY(libucode SHARED ${UCODE_SOURCES}) SET(SOVERSION 0 CACHE STRING "Override ucode library version") SET_TARGET_PROPERTIES(libucode PROPERTIES OUTPUT_NAME ucode SOVERSION ${SOVERSION}) TARGET_LINK_LIBRARIES(libucode ${json}) SET(CLI_SOURCES main.c) ADD_EXECUTABLE(ucode ${CLI_SOURCES}) TARGET_LINK_LIBRARIES(ucode libucode ${json}) CHECK_FUNCTION_EXISTS(dlopen DLOPEN_FUNCTION_EXISTS) IF (NOT DLOPEN_FUNCTION_EXISTS) TARGET_LINK_LIBRARIES(libucode dl) ENDIF() SET(CMAKE_REQUIRED_LIBRARIES json-c) CHECK_SYMBOL_EXISTS(json_object_array_shrink "json.h" HAVE_ARRAY_SHRINK) IF(HAVE_ARRAY_SHRINK) ADD_DEFINITIONS(-DHAVE_ARRAY_SHRINK) ELSE() CHECK_SYMBOL_EXISTS(json_object_array_shrink "json-c/json.h" HAVE_ARRAY_SHRINK2) IF(HAVE_ARRAY_SHRINK2) ADD_DEFINITIONS(-DHAVE_ARRAY_SHRINK) ENDIF() ENDIF() UNSET(CMAKE_REQUIRED_LIBRARIES) SET(LIBRARIES "") IF(FS_SUPPORT) SET(LIBRARIES ${LIBRARIES} fs_lib) ADD_LIBRARY(fs_lib MODULE lib/fs.c) SET_TARGET_PROPERTIES(fs_lib PROPERTIES OUTPUT_NAME fs PREFIX "") ENDIF() IF(MATH_SUPPORT) SET(LIBRARIES ${LIBRARIES} math_lib) ADD_LIBRARY(math_lib MODULE lib/math.c) SET_TARGET_PROPERTIES(math_lib PROPERTIES OUTPUT_NAME math PREFIX "") CHECK_FUNCTION_EXISTS(ceil CEIL_FUNCTION_EXISTS) IF (NOT CEIL_FUNCTION_EXISTS) TARGET_LINK_LIBRARIES(math_lib m) ENDIF() ENDIF() IF(UBUS_SUPPORT) FIND_LIBRARY(ubus NAMES ubus) FIND_LIBRARY(blobmsg_json NAMES blobmsg_json) FIND_PATH(ubus_include_dir NAMES libubus.h) INCLUDE_DIRECTORIES(${ubus_include_dir}) SET(LIBRARIES ${LIBRARIES} ubus_lib) ADD_LIBRARY(ubus_lib MODULE lib/ubus.c) SET_TARGET_PROPERTIES(ubus_lib PROPERTIES OUTPUT_NAME ubus PREFIX "") TARGET_LINK_LIBRARIES(ubus_lib ${ubus} ${blobmsg_json}) ENDIF() IF(UCI_SUPPORT) FIND_LIBRARY(uci NAMES uci) FIND_LIBRARY(ubox NAMES ubox) FIND_PATH(uci_include_dir uci.h) INCLUDE_DIRECTORIES(${uci_include_dir}) SET(LIBRARIES ${LIBRARIES} uci_lib) ADD_LIBRARY(uci_lib MODULE lib/uci.c) SET_TARGET_PROPERTIES(uci_lib PROPERTIES OUTPUT_NAME uci PREFIX "") TARGET_LINK_LIBRARIES(uci_lib ${uci} ${ubox}) ENDIF() IF(UNIT_TESTING) ENABLE_TESTING() ADD_DEFINITIONS(-DUNIT_TESTING) ADD_SUBDIRECTORY(tests) LIST(APPEND CMAKE_CTEST_ARGUMENTS "--output-on-failure") IF(CMAKE_C_COMPILER_ID STREQUAL "Clang") ADD_EXECUTABLE(ucode-san ${CLI_SOURCES} ${UCODE_SOURCES}) SET_PROPERTY(TARGET ucode-san PROPERTY ENABLE_EXPORTS 1) TARGET_LINK_LIBRARIES(ucode-san ${json}) TARGET_COMPILE_OPTIONS(ucode-san PRIVATE -g -fno-omit-frame-pointer -fsanitize=undefined,address,leak -fno-sanitize-recover=all) TARGET_LINK_OPTIONS(ucode-san PRIVATE -fsanitize=undefined,address,leak) ENDIF() ENDIF() INSTALL(TARGETS ucode RUNTIME DESTINATION bin) INSTALL(TARGETS libucode LIBRARY DESTINATION lib) INSTALL(TARGETS ${LIBRARIES} LIBRARY DESTINATION lib/ucode) FILE(GLOB UCODE_HEADERS "include/ucode/*.h") INSTALL(FILES ${UCODE_HEADERS} DESTINATION include/ucode)