• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/ucode/CMakeLists.txt

  1 cmake_minimum_required(VERSION 3.13)
  2 include(CheckFunctionExists)
  3 include(CheckSymbolExists)
  4 
  5 PROJECT(ucode C)
  6 ADD_DEFINITIONS(-Os -Wall -Werror --std=gnu99 -ffunction-sections -fwrapv -D_GNU_SOURCE)
  7 
  8 IF(CMAKE_C_COMPILER_VERSION VERSION_GREATER 6)
  9         ADD_DEFINITIONS(-Wextra -Werror=implicit-function-declaration)
 10         ADD_DEFINITIONS(-Wformat -Werror=format-security -Werror=format-nonliteral)
 11 ENDIF()
 12 ADD_DEFINITIONS(-Wmissing-declarations -Wno-error=unused-variable -Wno-unused-parameter)
 13 
 14 INCLUDE_DIRECTORIES(include)
 15 
 16 OPTION(COMPILE_SUPPORT "Support compilation from source" ON)
 17 
 18 IF(NOT COMPILE_SUPPORT)
 19   ADD_DEFINITIONS(-DNO_COMPILE)
 20 ENDIF()
 21 
 22 IF(APPLE)
 23   SET(NOT_APPLE OFF)
 24 ELSE()
 25   SET(NOT_APPLE ON)
 26 ENDIF()
 27 
 28 OPTION(FS_SUPPORT "Filesystem plugin support" ON)
 29 OPTION(MATH_SUPPORT "Math plugin support" ON)
 30 OPTION(UBUS_SUPPORT "Ubus plugin support" ON)
 31 OPTION(UCI_SUPPORT "UCI plugin support" ON)
 32 OPTION(RTNL_SUPPORT "Route Netlink plugin support" ${NOT_APPLE})
 33 OPTION(NL80211_SUPPORT "Wireless Netlink plugin support" ${NOT_APPLE})
 34 OPTION(RESOLV_SUPPORT "NS resolve plugin support" ON)
 35 OPTION(STRUCT_SUPPORT "Struct plugin support" ON)
 36 OPTION(ULOOP_SUPPORT "Uloop plugin support" ON)
 37 
 38 SET(LIB_SEARCH_PATH "${CMAKE_INSTALL_PREFIX}/lib/ucode/*.so:${CMAKE_INSTALL_PREFIX}/share/ucode/*.uc:./*.so:./*.uc" CACHE STRING "Default library search path")
 39 STRING(REPLACE ":" "\", \"" LIB_SEARCH_DEFINE "${LIB_SEARCH_PATH}")
 40 ADD_DEFINITIONS(-DLIB_SEARCH_PATH="${LIB_SEARCH_DEFINE}")
 41 
 42 IF(APPLE)
 43   SET(UCODE_MODULE_LINK_OPTIONS "LINKER:-undefined,dynamic_lookup")
 44   ADD_DEFINITIONS(-DBIND_8_COMPAT)
 45 ELSE()
 46   SET(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "-Wl,--gc-sections")
 47 ENDIF()
 48 
 49 IF(DEBUG)
 50   ADD_DEFINITIONS(-DDEBUG -g3 -O0)
 51 ELSE()
 52   ADD_DEFINITIONS(-DNDEBUG)
 53 ENDIF()
 54 
 55 INCLUDE(FindPkgConfig)
 56 PKG_CHECK_MODULES(JSONC REQUIRED json-c)
 57 INCLUDE_DIRECTORIES(${JSONC_INCLUDE_DIRS})
 58 
 59 SET(UCODE_SOURCES lexer.c lib.c vm.c chunk.c vallist.c compiler.c source.c types.c program.c)
 60 ADD_LIBRARY(libucode SHARED ${UCODE_SOURCES})
 61 SET(SOVERSION 0 CACHE STRING "Override ucode library version")
 62 SET_TARGET_PROPERTIES(libucode PROPERTIES OUTPUT_NAME ucode SOVERSION ${SOVERSION})
 63 TARGET_LINK_LIBRARIES(libucode ${JSONC_LINK_LIBRARIES})
 64 
 65 SET(CLI_SOURCES main.c)
 66 ADD_EXECUTABLE(ucode ${CLI_SOURCES})
 67 TARGET_LINK_LIBRARIES(ucode libucode ${JSONC_LINK_LIBRARIES})
 68 
 69 CHECK_FUNCTION_EXISTS(dlopen DLOPEN_FUNCTION_EXISTS)
 70 IF (NOT DLOPEN_FUNCTION_EXISTS)
 71   TARGET_LINK_LIBRARIES(libucode dl)
 72 ENDIF()
 73 
 74 CHECK_FUNCTION_EXISTS(fmod FMOD_FUNCTION_EXISTS)
 75 IF (NOT FMOD_FUNCTION_EXISTS)
 76   TARGET_LINK_LIBRARIES(libucode m)
 77 ENDIF()
 78 
 79 SET(CMAKE_REQUIRED_INCLUDES ${JSONC_INCLUDE_DIRS})
 80 SET(CMAKE_REQUIRED_LIBRARIES ${JSONC_LINK_LIBRARIES})
 81 CHECK_SYMBOL_EXISTS(json_tokener_get_parse_end "json-c/json.h" HAVE_PARSE_END)
 82 IF(HAVE_PARSE_END)
 83   ADD_DEFINITIONS(-DHAVE_PARSE_END)
 84 ENDIF()
 85 CHECK_SYMBOL_EXISTS(json_object_new_array_ext "json-c/json.h" HAVE_ARRAY_EXT)
 86 IF(HAVE_ARRAY_EXT)
 87   ADD_DEFINITIONS(-DHAVE_ARRAY_EXT)
 88 ENDIF()
 89 CHECK_SYMBOL_EXISTS(json_object_new_uint64 "json-c/json.h" HAVE_JSON_UINT64)
 90 IF(HAVE_JSON_UINT64)
 91   ADD_DEFINITIONS(-DHAVE_JSON_UINT64)
 92 ENDIF()
 93 UNSET(CMAKE_REQUIRED_INCLUDES)
 94 UNSET(CMAKE_REQUIRED_LIBRARIES)
 95 
 96 SET(LIBRARIES "")
 97 
 98 IF(FS_SUPPORT)
 99   SET(LIBRARIES ${LIBRARIES} fs_lib)
100   ADD_LIBRARY(fs_lib MODULE lib/fs.c)
101   SET_TARGET_PROPERTIES(fs_lib PROPERTIES OUTPUT_NAME fs PREFIX "")
102   TARGET_LINK_OPTIONS(fs_lib PRIVATE ${UCODE_MODULE_LINK_OPTIONS})
103 ENDIF()
104 
105 IF(MATH_SUPPORT)
106   SET(LIBRARIES ${LIBRARIES} math_lib)
107   ADD_LIBRARY(math_lib MODULE lib/math.c)
108   SET_TARGET_PROPERTIES(math_lib PROPERTIES OUTPUT_NAME math PREFIX "")
109   TARGET_LINK_OPTIONS(math_lib PRIVATE ${UCODE_MODULE_LINK_OPTIONS})
110   CHECK_FUNCTION_EXISTS(ceil CEIL_FUNCTION_EXISTS)
111   IF (NOT CEIL_FUNCTION_EXISTS)
112     TARGET_LINK_LIBRARIES(math_lib m)
113   ENDIF()
114 ENDIF()
115 
116 IF(UBUS_SUPPORT)
117   FIND_LIBRARY(ubus NAMES ubus)
118   FIND_LIBRARY(blobmsg_json NAMES blobmsg_json)
119   FIND_PATH(ubus_include_dir NAMES libubus.h)
120   INCLUDE_DIRECTORIES(${ubus_include_dir})
121   SET(LIBRARIES ${LIBRARIES} ubus_lib)
122   ADD_LIBRARY(ubus_lib MODULE lib/ubus.c)
123   SET_TARGET_PROPERTIES(ubus_lib PROPERTIES OUTPUT_NAME ubus PREFIX "")
124   TARGET_LINK_OPTIONS(ubus_lib PRIVATE ${UCODE_MODULE_LINK_OPTIONS})
125   TARGET_LINK_LIBRARIES(ubus_lib ${ubus} ${blobmsg_json})
126   FILE(WRITE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/test.c" "
127     #include <libubus.h>
128     int main() { return UBUS_STATUS_NO_MEMORY; }
129   ")
130   TRY_COMPILE(HAVE_NEW_UBUS_STATUS_CODES
131     ${CMAKE_BINARY_DIR}
132     "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/test.c")
133   IF(HAVE_NEW_UBUS_STATUS_CODES)
134     ADD_DEFINITIONS(-DHAVE_NEW_UBUS_STATUS_CODES)
135   ENDIF()
136 ENDIF()
137 
138 IF(UCI_SUPPORT)
139   FIND_LIBRARY(uci NAMES uci)
140   FIND_LIBRARY(ubox NAMES ubox)
141   FIND_PATH(uci_include_dir uci.h)
142   INCLUDE_DIRECTORIES(${uci_include_dir})
143   SET(LIBRARIES ${LIBRARIES} uci_lib)
144   ADD_LIBRARY(uci_lib MODULE lib/uci.c)
145   SET_TARGET_PROPERTIES(uci_lib PROPERTIES OUTPUT_NAME uci PREFIX "")
146   TARGET_LINK_OPTIONS(uci_lib PRIVATE ${UCODE_MODULE_LINK_OPTIONS})
147   TARGET_LINK_LIBRARIES(uci_lib ${uci} ${ubox})
148 ENDIF()
149 
150 IF(RTNL_SUPPORT)
151   FIND_LIBRARY(nl NAMES nl-tiny)
152   FIND_PATH(nl_include_dir NAMES netlink/msg.h PATH_SUFFIXES libnl-tiny)
153   INCLUDE_DIRECTORIES(${nl_include_dir})
154   SET(LIBRARIES ${LIBRARIES} rtnl_lib)
155   ADD_LIBRARY(rtnl_lib MODULE lib/rtnl.c)
156   SET_TARGET_PROPERTIES(rtnl_lib PROPERTIES OUTPUT_NAME rtnl PREFIX "")
157   TARGET_LINK_OPTIONS(rtnl_lib PRIVATE ${UCODE_MODULE_LINK_OPTIONS})
158   TARGET_LINK_LIBRARIES(rtnl_lib ${nl})
159 ENDIF()
160 
161 IF(NL80211_SUPPORT)
162   FIND_LIBRARY(nl NAMES nl-tiny)
163   FIND_PATH(nl_include_dir NAMES netlink/msg.h PATH_SUFFIXES libnl-tiny)
164   INCLUDE_DIRECTORIES(${nl_include_dir})
165   SET(LIBRARIES ${LIBRARIES} nl80211_lib)
166   ADD_LIBRARY(nl80211_lib MODULE lib/nl80211.c)
167   SET_TARGET_PROPERTIES(nl80211_lib PROPERTIES OUTPUT_NAME nl80211 PREFIX "")
168   TARGET_LINK_OPTIONS(nl80211_lib PRIVATE ${UCODE_MODULE_LINK_OPTIONS})
169   TARGET_LINK_LIBRARIES(nl80211_lib ${nl})
170 ENDIF()
171 
172 IF(RESOLV_SUPPORT)
173   SET(LIBRARIES ${LIBRARIES} resolv_lib)
174   ADD_LIBRARY(resolv_lib MODULE lib/resolv.c)
175   SET_TARGET_PROPERTIES(resolv_lib PROPERTIES OUTPUT_NAME resolv PREFIX "")
176   TARGET_LINK_OPTIONS(resolv_lib PRIVATE ${UCODE_MODULE_LINK_OPTIONS})
177   CHECK_FUNCTION_EXISTS(res_mkquery RES_MKQUERY_FUNCTION_EXISTS)
178   CHECK_FUNCTION_EXISTS(clock_gettime CLOCK_GETTIME_FUNCTION_EXISTS)
179   IF (NOT RES_MKQUERY_FUNCTION_EXISTS)
180     TARGET_LINK_LIBRARIES(resolv_lib resolv)
181   ENDIF()
182   IF (NOT CLOCK_GETTIME_FUNCTION_EXISTS)
183     TARGET_LINK_LIBRARIES(resolv_lib rt)
184   ENDIF()
185 ENDIF()
186 
187 IF(STRUCT_SUPPORT)
188   SET(LIBRARIES ${LIBRARIES} struct_lib)
189   ADD_LIBRARY(struct_lib MODULE lib/struct.c)
190   SET_TARGET_PROPERTIES(struct_lib PROPERTIES OUTPUT_NAME struct PREFIX "")
191   TARGET_LINK_OPTIONS(struct_lib PRIVATE ${UCODE_MODULE_LINK_OPTIONS})
192   CHECK_FUNCTION_EXISTS(frexp FREXP_FUNCTION_EXISTS)
193   IF (NOT FREXP_FUNCTION_EXISTS)
194     TARGET_LINK_LIBRARIES(struct_lib m)
195   ENDIF()
196 ENDIF()
197 
198 IF(ULOOP_SUPPORT)
199   FIND_LIBRARY(ubox NAMES ubox)
200   FIND_PATH(uloop_include_dir NAMES libubox/uloop.h)
201   INCLUDE_DIRECTORIES(${uloop_include_dir})
202   SET(LIBRARIES ${LIBRARIES} uloop_lib)
203   ADD_LIBRARY(uloop_lib MODULE lib/uloop.c)
204   SET_TARGET_PROPERTIES(uloop_lib PROPERTIES OUTPUT_NAME uloop PREFIX "")
205   TARGET_LINK_OPTIONS(uloop_lib PRIVATE ${UCODE_MODULE_LINK_OPTIONS})
206   SET(CMAKE_REQUIRED_LIBRARIES ${ubox})
207   CHECK_FUNCTION_EXISTS(uloop_timeout_remaining64 REMAINING64_FUNCTION_EXISTS)
208   UNSET(CMAKE_REQUIRED_LIBRARIES)
209   IF (REMAINING64_FUNCTION_EXISTS)
210     TARGET_COMPILE_DEFINITIONS(uloop_lib PUBLIC HAVE_ULOOP_TIMEOUT_REMAINING64)
211   ENDIF()
212   TARGET_LINK_LIBRARIES(uloop_lib ${ubox})
213 ENDIF()
214 
215 IF(UNIT_TESTING)
216   ENABLE_TESTING()
217   ADD_DEFINITIONS(-DUNIT_TESTING)
218   ADD_SUBDIRECTORY(tests)
219   LIST(APPEND CMAKE_CTEST_ARGUMENTS "--output-on-failure")
220 
221   IF(CMAKE_C_COMPILER_ID STREQUAL "Clang")
222     ADD_EXECUTABLE(ucode-san ${CLI_SOURCES} ${UCODE_SOURCES})
223     SET_PROPERTY(TARGET ucode-san PROPERTY ENABLE_EXPORTS 1)
224     TARGET_LINK_LIBRARIES(ucode-san ${JSONC_LINK_LIBRARIES})
225     TARGET_COMPILE_OPTIONS(ucode-san PRIVATE -g -fno-omit-frame-pointer -fsanitize=undefined,address,leak -fno-sanitize-recover=all)
226     TARGET_LINK_OPTIONS(ucode-san PRIVATE -fsanitize=undefined,address,leak)
227   ENDIF()
228 ENDIF()
229 
230 INSTALL(TARGETS ucode RUNTIME DESTINATION bin)
231 INSTALL(TARGETS libucode LIBRARY DESTINATION lib)
232 INSTALL(TARGETS ${LIBRARIES} LIBRARY DESTINATION lib/ucode)
233 
234 ADD_CUSTOM_TARGET(utpl ALL COMMAND ${CMAKE_COMMAND} -E create_symlink ucode utpl)
235 INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/utpl DESTINATION bin)
236 
237 IF(COMPILE_SUPPORT)
238   ADD_CUSTOM_TARGET(ucc ALL COMMAND ${CMAKE_COMMAND} -E create_symlink ucode ucc)
239   INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/ucc DESTINATION bin)
240 ENDIF()
241 
242 FILE(GLOB UCODE_HEADERS "include/ucode/*.h")
243 INSTALL(FILES ${UCODE_HEADERS} DESTINATION include/ucode)
244 
245 ADD_SUBDIRECTORY(examples)

This page was automatically generated by LXR 0.3.1.  •  OpenWrt