-
Notifications
You must be signed in to change notification settings - Fork 185
/
CMakeLists.txt
335 lines (305 loc) · 12.1 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
cmake_minimum_required(VERSION 3.5)
project (luv C ASM)
set(LUV_VERSION_MAJOR 1)
set(LUV_VERSION_MINOR 49)
set(LUV_VERSION_PATCH 2)
set(LUV_VERSION ${LUV_VERSION_MAJOR}.${LUV_VERSION_MINOR}.${LUV_VERSION_PATCH})
if(NOT ${CMAKE_VERSION} VERSION_LESS "3.5.0")
# generate compilation database used by C code intelligence tools
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
endif()
option(BUILD_MODULE "Build as module" ON)
option(BUILD_STATIC_LIBS "Build static library" OFF)
option(BUILD_SHARED_LIBS "Build shared library" OFF)
option(WITH_SHARED_LIBUV "Link to a shared libuv library instead of static linking" OFF)
if (MINGW)
add_definitions(-D_WIN32_WINNT=0x0600)
endif (MINGW)
if (WIN32)
# replace /MD to /MT to avoid link msvcr*.dll
# this needs to be before add_subdirectory calls so that they inherit the modified flags
set(CompilerFlags
CMAKE_C_FLAGS
CMAKE_C_FLAGS_DEBUG
CMAKE_C_FLAGS_MINSIZEREL
CMAKE_C_FLAGS_RELWITHDEBINFO
CMAKE_C_FLAGS_RELEASE)
foreach(CompilerFlag ${CompilerFlags})
string(REPLACE "/MD" "/MT" ${CompilerFlag} "${${CompilerFlag}}")
endforeach()
endif ()
if (NOT WITH_LUA_ENGINE)
set(WITH_LUA_ENGINE "LuaJIT"
CACHE STRING "Link to LuaJIT or PUC Lua" FORCE)
set_property(CACHE WITH_LUA_ENGINE
PROPERTY STRINGS "Lua;LuaJIT")
endif (NOT WITH_LUA_ENGINE)
if (NOT LUA_BUILD_TYPE)
set(LUA_BUILD_TYPE "Static"
CACHE STRING "Build Lua/LuaJIT as static, dynamic libary, or use system one" FORCE)
set_property(CACHE LUA_BUILD_TYPE
PROPERTY STRINGS "Static;Dynamic;System")
endif (NOT LUA_BUILD_TYPE)
if (WITH_LUA_ENGINE STREQUAL Lua)
if (NOT WIN32)
add_definitions(-DLUA_USE_DLOPEN)
endif (NOT WIN32)
set(USE_LUAJIT OFF)
else ()
set(USE_LUAJIT ON)
endif ()
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")
if (WITH_SHARED_LIBUV)
find_package(Libuv REQUIRED)
include_directories(${LIBUV_INCLUDE_DIR})
else (WITH_SHARED_LIBUV)
include_directories(deps/libuv/include)
add_subdirectory(deps/libuv EXCLUDE_FROM_ALL)
if (BUILD_MODULE)
add_definitions( -DBUILDING_UV_SHARED )
endif (BUILD_MODULE)
set(LIBUV_LIBRARIES uv_a)
set_target_properties(uv_a PROPERTIES COMPILE_FLAGS "-fPIC")
endif (WITH_SHARED_LIBUV)
if (LUA)
MESSAGE(STATUS "Lua: using information from luarocks")
MESSAGE(STATUS "LUA_LIBDIR: " ${LUA_LIBDIR})
MESSAGE(STATUS "LUA_LIBFILE: " ${LUA_LIBFILE})
MESSAGE(STATUS "LUA_INCDIR: " ${LUA_INCDIR})
MESSAGE(STATUS "LUA: " ${LUA})
SET(LUA_EXECUTABLE "${LUA}")
SET(LUA_INCLUDE_DIR "${LUA_INCDIR}")
SET(LUA_PACKAGE_PATH "${LUADIR}")
SET(LUA_PACKAGE_CPATH "${LIBDIR}")
SET(INSTALL_LIB_DIR ${LIBDIR})
if (LUA_LIBDIR)
if (NOT LUA_LIBFILE)
# If LIBDIR is set but LIBFILE is not, then that means
# Luarocks 2.x is being used, and we'll need to find the lib
# ourselves
GET_FILENAME_COMPONENT(LUA_EXEC_NAME ${LUA_EXECUTABLE} NAME_WE)
IF(LUA_EXEC_NAME STREQUAL "luajit")
FIND_LIBRARY(LUA_LIBRARIES
NAMES luajit libluajit
PATHS ${LUA_LIBDIR}
NO_DEFAULT_PATH)
ELSEIF(LUA_EXEC_NAME MATCHES "lua.*")
FIND_LIBRARY(LUA_LIBRARIES
NAMES lua lua54 lua53 lua52 lua51 liblua liblua54 liblua53 liblua52 liblua51
PATHS ${LUA_LIBDIR}
NO_DEFAULT_PATH)
ENDIF()
else()
# Otherwise, we can just use the LIBFILE that Luarocks provides
get_filename_component(LUA_LIBRARIES "${LUA_LIBDIR}/${LUA_LIBFILE}" ABSOLUTE)
endif()
MESSAGE(STATUS "Lua library: ${LUA_LIBRARIES}")
else()
MESSAGE(STATUS "Lua library not set, presuming LuaRocks config has link_lua_explicitly set to false")
endif()
include_directories(${LUA_INCLUDE_DIR})
else (LUA)
if (LUA_BUILD_TYPE STREQUAL System)
if (USE_LUAJIT)
# We only link the libs on Windows, so find_package fully succeeding
# is only required on Windows
if (WIN32 OR CYGWIN)
find_package(LuaJIT REQUIRED)
link_directories(${LUAJIT_LIBRARIES})
else()
find_package(LuaJIT)
endif()
if(NOT LUAJIT_INCLUDE_DIR)
message( FATAL_ERROR "Failed to find LuaJIT headers. Variable `LUAJIT_INCLUDE_DIR' expected to be defined.")
endif()
include_directories(${LUAJIT_INCLUDE_DIR})
else (USE_LUAJIT)
# We only link the libs on Windows, so find_package fully succeeding
# is only required on Windows
if (WIN32 OR CYGWIN)
find_package(Lua REQUIRED)
else()
find_package(Lua)
endif()
if(NOT LUA_INCLUDE_DIR)
message( FATAL_ERROR "Failed to find Lua headers. Variable `LUA_INCLUDE_DIR' expected to be defined.")
endif()
include_directories(${LUA_INCLUDE_DIR})
endif (USE_LUAJIT)
else (LUA_BUILD_TYPE STREQUAL System)
if (LUA_BUILD_TYPE STREQUAL Static)
SET(WITH_SHARED_LUA OFF)
else (LUA_BUILD_TYPE STREQUAL Static)
SET(WITH_SHARED_LUA ON)
endif (LUA_BUILD_TYPE STREQUAL Static)
if (USE_LUAJIT)
include(deps/luajit.cmake)
set(LUA_INCLUDE_DIR deps/luajit/src)
set(LUAJIT_LIBRARIES luajit-5.1)
else(USE_LUAJIT)
include(deps/lua.cmake)
set(LUA_INCLUDE_DIR deps/lua)
endif (USE_LUAJIT)
include_directories(${LUA_INCLUDE_DIR})
set(LUA_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/${LUA_INCLUDE_DIR})
endif (LUA_BUILD_TYPE STREQUAL System)
endif (LUA)
if (EXISTS "${LUA_INCLUDE_DIR}/lua.h")
# At least 5.[012] have different ways to express the version
# so all of them need to be tested. Lua 5.2 defines LUA_VERSION
# and LUA_RELEASE as joined by the C preprocessor, so avoid those.
# lua 5.2 and above
file(STRINGS "${LUA_INCLUDE_DIR}/lua.h" LUA_VERSION_MAJOR
REGEX "^#define[ \t]+LUA_VERSION_MAJOR[ \t]+\"Lua [0-9]+")
file(STRINGS "${LUA_INCLUDE_DIR}/lua.h" LUA_VERSION_MINOR
REGEX "^#define[ \t]+LUA_VERSION_MINOR[ \t]+\"Lua [0-9]+")
if(NOT LUA_VERSION_MAJOR AND NOT LUA_VERSION_MINOR)
file(STRINGS "${LUA_INCLUDE_DIR}/lua.h" lua_version_strings
REGEX "^#define[ \t]+LUA_VERSION([ \t]+\"Lua [0-9]|_[MR]).*")
string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION_MAJOR[ \t]+\"([0-9])\"[ \t]*;.*" "\\1" LUA_VERSION_MAJOR ";${lua_version_strings};")
if (LUA_VERSION_MAJOR MATCHES "^[0-9]+$")
string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION_MINOR[ \t]+\"([0-9])\"[ \t]*;.*" "\\1" LUA_VERSION_MINOR ";${lua_version_strings};")
string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION_RELEASE[ \t]+\"([0-9])\"[ \t]*;.*" "\\1" LUA_VERSION_PATCH ";${lua_version_strings};")
set(LUA_VERSION_STRING "${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}.${LUA_VERSION_PATCH}")
else ()
string(REGEX REPLACE ".*;#define[ \t]+LUA_RELEASE[ \t]+\"Lua ([0-9.]+)\"[ \t]*;.*" "\\1" LUA_VERSION_STRING ";${lua_version_strings};")
if (NOT LUA_VERSION_STRING MATCHES "^[0-9.]+$")
string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION[ \t]+\"Lua ([0-9.]+)\"[ \t]*;.*" "\\1" LUA_VERSION_STRING ";${lua_version_strings};")
endif ()
string(REGEX REPLACE "^([0-9]+)\\.[0-9.]*$" "\\1" LUA_VERSION_MAJOR "${LUA_VERSION_STRING}")
string(REGEX REPLACE "^[0-9]+\\.([0-9]+)[0-9.]*$" "\\1" LUA_VERSION_MINOR "${LUA_VERSION_STRING}")
string(REGEX REPLACE "^[0-9]+\\.[0-9]+\\.([0-9]).*" "\\1" LUA_VERSION_PATCH "${LUA_VERSION_STRING}")
endif ()
unset(lua_version_strings)
endif()
endif()
if (BUILD_MODULE)
add_library(luv MODULE src/luv.c)
set_target_properties(luv PROPERTIES PREFIX "")
list(APPEND ACTIVE_TARGETS "luv")
endif (BUILD_MODULE)
if (BUILD_STATIC_LIBS)
add_library(libluv_a STATIC src/luv.c)
set_target_properties(libluv_a PROPERTIES OUTPUT_NAME luv)
list(APPEND ACTIVE_TARGETS "libluv_a")
endif (BUILD_STATIC_LIBS)
if (BUILD_SHARED_LIBS)
add_library(libluv SHARED src/luv.c)
set_target_properties(libluv
PROPERTIES VERSION ${LUV_VERSION} SOVERSION ${LUV_VERSION_MAJOR} OUTPUT_NAME luv)
if(APPLE)
set_target_properties(libluv
PROPERTIES LINK_FLAGS "-undefined dynamic_lookup")
endif(APPLE)
list(APPEND ACTIVE_TARGETS "libluv")
endif (BUILD_SHARED_LIBS)
if(APPLE)
set(CMAKE_SHARED_MODULE_CREATE_C_FLAGS
"${CMAKE_SHARED_MODULE_CREATE_C_FLAGS} -flat_namespace -undefined suppress"
)
# execute_process(COMMAND which luajit OUTPUT_VARIABLE LUAJIT)
# set(CMAKE_SHARED_MODULE_CREATE_C_FLAGS
# "${CMAKE_SHARED_MODULE_CREATE_C_FLAGS} -bundle_loader ${LUAJIT}"
# )
endif()
if(NOT LUA_COMPAT53_DIR)
set(LUA_COMPAT53_DIR deps/lua-compat-5.3)
endif()
if(DEFINED ENV{LUA_COMPAT53_DIR})
set(LUA_COMPAT53_DIR $ENV{LUA_COMPAT53_DIR})
endif()
include_directories(${LUA_COMPAT53_DIR}/c-api)
if(WIN32)
add_definitions(-DLUA_BUILD_AS_DLL -DLUA_LIB)
endif()
foreach(TARGET_NAME ${ACTIVE_TARGETS})
if(WIN32 OR CYGWIN)
if (LUA)
target_link_libraries(${TARGET_NAME} ${LIBUV_LIBRARIES} ${LUA_LIBRARIES})
else (LUA)
if (USE_LUAJIT)
target_link_libraries(${TARGET_NAME} ${LIBUV_LIBRARIES} ${LUAJIT_LIBRARIES})
else (USE_LUAJIT)
if (LUA_BUILD_TYPE STREQUAL System)
target_link_libraries(${TARGET_NAME} ${LIBUV_LIBRARIES} ${LUA_LIBRARIES})
else (LUA_BUILD_TYPE STREQUAL System)
target_link_libraries(${TARGET_NAME} ${LIBUV_LIBRARIES} lualib)
endif (LUA_BUILD_TYPE STREQUAL System)
endif (USE_LUAJIT)
endif (LUA)
elseif("${CMAKE_SYSTEM_NAME}" MATCHES "Linux")
target_link_libraries(${TARGET_NAME} ${LIBUV_LIBRARIES} rt)
else()
target_link_libraries(${TARGET_NAME} ${LIBUV_LIBRARIES})
endif()
endforeach()
if (NOT LUA)
if (BUILD_MODULE)
if (WIN32)
set(MODULE_INSTALL_LIB_DIR "${CMAKE_INSTALL_PREFIX}/lib")
else (WIN32)
set(MODULE_INSTALL_LIB_DIR "${CMAKE_INSTALL_PREFIX}/lib/lua/${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}")
endif (WIN32)
endif (BUILD_MODULE)
if (BUILD_STATIC_LIBS)
set(STATICLIBS_INSTALL_LIB_DIR "${CMAKE_INSTALL_PREFIX}/lib"
CACHE PATH "Installation directory for static libraries")
endif (BUILD_STATIC_LIBS)
if (BUILD_SHARED_LIBS)
set(SHAREDLIBS_INSTALL_BIN_DIR "${CMAKE_INSTALL_PREFIX}/bin"
CACHE PATH "Installation directory for runtime components")
set(SHAREDLIBS_INSTALL_LIB_DIR "${CMAKE_INSTALL_PREFIX}/lib"
CACHE PATH "Installation directory for shared libraries")
endif (BUILD_SHARED_LIBS)
else ()
# use paths from luaRocks
set(MODULE_INSTALL_LIB_DIR "${INSTALL_LIB_DIR}")
set(STATICLIBS_INSTALL_LIB_DIR "${INSTALL_LIB_DIR}")
set(SHAREDLIBS_INSTALL_LIB_DIR "${INSTALL_LIB_DIR}")
endif ()
# header install paths are LuaRocks-agnostic, so just use CMAKE_INSTALL_PREFIX regardless
if (BUILD_STATIC_LIBS)
set(STATICLIBS_INSTALL_INC_DIR "${CMAKE_INSTALL_PREFIX}/include/luv"
CACHE PATH "Installation directory for headers")
endif (BUILD_STATIC_LIBS)
if (BUILD_SHARED_LIBS)
set(SHAREDLIBS_INSTALL_INC_DIR "${CMAKE_INSTALL_PREFIX}/include/luv"
CACHE PATH "Installation directory for headers")
endif (BUILD_SHARED_LIBS)
if (CMAKE_INSTALL_PREFIX)
if (BUILD_MODULE)
install(TARGETS luv
ARCHIVE DESTINATION "${MODULE_INSTALL_LIB_DIR}"
LIBRARY DESTINATION "${MODULE_INSTALL_LIB_DIR}"
)
endif (BUILD_MODULE)
if (BUILD_STATIC_LIBS)
install(TARGETS libluv_a
ARCHIVE DESTINATION "${STATICLIBS_INSTALL_LIB_DIR}"
LIBRARY DESTINATION "${STATICLIBS_INSTALL_LIB_DIR}"
)
install(
FILES src/luv.h src/util.h src/lhandle.h src/lreq.h
DESTINATION "${STATICLIBS_INSTALL_INC_DIR}"
)
endif (BUILD_STATIC_LIBS)
if (BUILD_SHARED_LIBS)
install(TARGETS libluv
RUNTIME DESTINATION "${SHAREDLIBS_INSTALL_BIN_DIR}"
ARCHIVE DESTINATION "${SHAREDLIBS_INSTALL_LIB_DIR}"
LIBRARY DESTINATION "${SHAREDLIBS_INSTALL_LIB_DIR}"
)
if(UNIX OR MINGW)
set(prefix ${CMAKE_INSTALL_PREFIX})
set(includedir ${SHAREDLIBS_INSTALL_INC_DIR})
set(libdir ${SHAREDLIBS_INSTALL_LIB_DIR})
configure_file(libluv.pc.in ${CMAKE_CURRENT_BINARY_DIR}/libluv.pc @ONLY)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libluv.pc
DESTINATION ${SHAREDLIBS_INSTALL_LIB_DIR}/pkgconfig)
endif()
install(
FILES src/luv.h src/util.h src/lhandle.h src/lreq.h
DESTINATION "${SHAREDLIBS_INSTALL_INC_DIR}"
)
endif (BUILD_SHARED_LIBS)
endif (CMAKE_INSTALL_PREFIX)