aboutsummaryrefslogtreecommitdiff
path: root/cmake/lua.cmake
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--cmake/lua.cmake309
1 files changed, 309 insertions, 0 deletions
diff --git a/cmake/lua.cmake b/cmake/lua.cmake
new file mode 100644
index 0000000..ee2e35d
--- /dev/null
+++ b/cmake/lua.cmake
@@ -0,0 +1,309 @@
1# LuaDist CMake utility library for Lua.
2#
3# Copyright (C) 2007-2012 LuaDist.
4# by David Manura, Peter Drahos
5# Redistribution and use of this file is allowed according to the terms of the MIT license.
6# For details see the COPYRIGHT file distributed with LuaDist.
7# Please note that the package source code is licensed under its own license.
8
9set ( INSTALL_LMOD ${INSTALL_LIB}/lua
10 CACHE PATH "Directory to install Lua modules." )
11set ( INSTALL_CMOD ${INSTALL_LIB}/lua
12 CACHE PATH "Directory to install Lua binary modules." )
13
14option ( LUA_SKIP_WRAPPER
15 "Do not build and install Lua executable wrappers." OFF )
16option ( LUA_STATIC_MODULE "Build modules for static linking" OFF )
17
18# List of (Lua module name, file path) pairs.
19# Used internally by add_lua_test. Built by add_lua_module.
20set ( _lua_modules )
21
22# utility function: appends path `path` to path `basepath`, properly
23# handling cases when `path` may be relative or absolute.
24macro ( _append_path basepath path result )
25 if ( IS_ABSOLUTE "${path}" )
26 set ( ${result} "${path}" )
27 else ()
28 set ( ${result} "${basepath}/${path}" )
29 endif ()
30endmacro ()
31
32# install_lua_executable ( target source )
33# Automatically generate a binary if srlua package is available
34# The application or its source will be placed into /bin
35# If the application source did not have .lua suffix then it will be added
36# USE: lua_executable ( sputnik src/sputnik.lua )
37macro ( install_lua_executable _name _source )
38 get_filename_component ( _source_name ${_source} NAME_WE )
39 # Find srlua and glue
40 find_program( SRLUA_EXECUTABLE NAMES srlua )
41 find_program( GLUE_EXECUTABLE NAMES glue )
42 # Executable output
43 set ( _exe ${CMAKE_CURRENT_BINARY_DIR}/${_name}${CMAKE_EXECUTABLE_SUFFIX} )
44 if ( NOT SKIP_LUA_WRAPPER AND SRLUA_EXECUTABLE AND GLUE_EXECUTABLE )
45 # Generate binary gluing the lua code to srlua, this is a robuust approach for most systems
46 add_custom_command(
47 OUTPUT ${_exe}
48 COMMAND ${GLUE_EXECUTABLE}
49 ARGS ${SRLUA_EXECUTABLE} ${_source} ${_exe}
50 DEPENDS ${_source}
51 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
52 VERBATIM
53 )
54 # Make sure we have a target associated with the binary
55 add_custom_target(${_name} ALL
56 DEPENDS ${_exe}
57 )
58 # Install with run permissions
59 install ( PROGRAMS ${_exe} DESTINATION ${INSTALL_BIN} COMPONENT Runtime)
60 # Also install source as optional resurce
61 install ( FILES ${_source} DESTINATION ${INSTALL_FOO} COMPONENT Other )
62 else()
63 # Install into bin as is but without the lua suffix, we assume the executable uses UNIX shebang/hash-bang magic
64 install ( PROGRAMS ${_source} DESTINATION ${INSTALL_BIN}
65 RENAME ${_source_name}
66 COMPONENT Runtime
67 )
68 endif()
69endmacro ()
70
71macro ( _lua_module_helper is_install _name )
72 parse_arguments ( _MODULE "LINK;ALL_IN_ONE" "" ${ARGN} )
73 # _target is CMake-compatible target name for module (e.g. socket_core).
74 # _module is relative path of target (e.g. socket/core),
75 # without extension (e.g. .lua/.so/.dll).
76 # _MODULE_SRC is list of module source files (e.g. .lua and .c files).
77 # _MODULE_NAMES is list of module names (e.g. socket.core).
78 if ( _MODULE_ALL_IN_ONE )
79 string ( REGEX REPLACE "\\..*" "" _target "${_name}" )
80 string ( REGEX REPLACE "\\..*" "" _module "${_name}" )
81 set ( _target "${_target}_all_in_one")
82 set ( _MODULE_SRC ${_MODULE_ALL_IN_ONE} )
83 set ( _MODULE_NAMES ${_name} ${_MODULE_DEFAULT_ARGS} )
84 else ()
85 string ( REPLACE "." "_" _target "${_name}" )
86 string ( REPLACE "." "/" _module "${_name}" )
87 set ( _MODULE_SRC ${_MODULE_DEFAULT_ARGS} )
88 set ( _MODULE_NAMES ${_name} )
89 endif ()
90 if ( NOT _MODULE_SRC )
91 message ( FATAL_ERROR "no module sources specified" )
92 endif ()
93 list ( GET _MODULE_SRC 0 _first_source )
94
95 get_filename_component ( _ext ${_first_source} EXT )
96 if ( _ext STREQUAL ".lua" ) # Lua source module
97 list ( LENGTH _MODULE_SRC _len )
98 if ( _len GREATER 1 )
99 message ( FATAL_ERROR "more than one source file specified" )
100 endif ()
101
102 set ( _module "${_module}.lua" )
103
104 get_filename_component ( _module_dir ${_module} PATH )
105 get_filename_component ( _module_filename ${_module} NAME )
106 _append_path ( "${CMAKE_CURRENT_SOURCE_DIR}" "${_first_source}" _module_path )
107 list ( APPEND _lua_modules "${_name}" "${_module_path}" )
108
109 if ( ${is_install} )
110 install ( FILES ${_first_source} DESTINATION ${INSTALL_LMOD}/${_module_dir}
111 RENAME ${_module_filename}
112 COMPONENT Runtime
113 )
114 endif ()
115 else () # Lua C binary module
116 enable_language ( C )
117 find_package ( Lua REQUIRED )
118 include_directories ( ${LUA_INCLUDE_DIR} )
119
120 set ( _module "${_module}${CMAKE_SHARED_MODULE_SUFFIX}" )
121
122 get_filename_component ( _module_dir ${_module} PATH )
123 get_filename_component ( _module_filenamebase ${_module} NAME_WE )
124 foreach ( _thisname ${_MODULE_NAMES} )
125 list ( APPEND _lua_modules "${_thisname}"
126 "${CMAKE_CURRENT_BINARY_DIR}/\${CMAKE_CFG_INTDIR}/${_module}" )
127 endforeach ()
128
129 # Static module (not linking to lua)
130 if ( LUA_STATIC_MODULE )
131 add_library( ${_target} STATIC ${_MODULE_SRC})
132 target_link_libraries ( ${_target} ${_MODULE_LINK} )
133 else ()
134 # Dynamic module
135 add_library( ${_target} MODULE ${_MODULE_SRC})
136 target_link_libraries ( ${_target} ${LUA_LIBRARY} ${_MODULE_LINK} )
137 endif ()
138
139 set_target_properties ( ${_target} PROPERTIES
140 ARCHIVE_OUTPUT_DIRECTORY "${_module_dir}"
141 LIBRARY_OUTPUT_DIRECTORY "${_module_dir}"
142 PREFIX ""
143 OUTPUT_NAME "${_module_filenamebase}" )
144 if ( ${is_install} )
145 install ( TARGETS ${_target}
146 LIBRARY DESTINATION ${INSTALL_CMOD}/${_module_dir}
147 COMPONENT Runtime
148 ARCHIVE DESTINATION ${INSTALL_CMOD}/${_module_dir}
149 COMPONENT Library )
150 endif ()
151 endif ()
152endmacro ()
153
154# add_lua_module
155# Builds a Lua source module into a destination locatable by Lua
156# require syntax.
157# Binary modules are also supported where this function takes sources and
158# libraries to compile separated by LINK keyword.
159# USE: add_lua_module ( socket.http src/http.lua )
160# USE2: add_lua_module ( mime.core src/mime.c )
161# USE3: add_lua_module ( socket.core ${SRC_SOCKET} LINK ${LIB_SOCKET} )
162# USE4: add_lua_module ( ssl.context ssl.core ALL_IN_ONE src/context.c src/ssl.c )
163# This form builds an "all-in-one" module (e.g. ssl.so or ssl.dll containing
164# both modules ssl.context and ssl.core). The CMake target name will be
165# ssl_all_in_one.
166# Also sets variable _module_path (relative path where module typically
167# would be installed).
168macro ( add_lua_module )
169 _lua_module_helper ( 0 ${ARGN} )
170endmacro ()
171
172
173# install_lua_module
174# This is the same as `add_lua_module` but also installs the module.
175# USE: install_lua_module ( socket.http src/http.lua )
176# USE2: install_lua_module ( mime.core src/mime.c )
177# USE3: install_lua_module ( socket.core ${SRC_SOCKET} LINK ${LIB_SOCKET} )
178macro ( install_lua_module )
179 _lua_module_helper ( 1 ${ARGN} )
180endmacro ()
181
182# Builds string representing Lua table mapping Lua modules names to file
183# paths. Used internally.
184macro ( _make_module_table _outvar )
185 set ( ${_outvar} )
186 list ( LENGTH _lua_modules _n )
187 if ( ${_n} GREATER 0 ) # avoids cmake complaint
188 foreach ( _i RANGE 1 ${_n} 2 )
189 list ( GET _lua_modules ${_i} _path )
190 math ( EXPR _ii ${_i}-1 )
191 list ( GET _lua_modules ${_ii} _name )
192 set ( ${_outvar} "${_table} ['${_name}'] = '${_path}'\;\n")
193 endforeach ()
194 endif ()
195 set ( ${_outvar}
196"local modules = {
197${_table}}" )
198endmacro ()
199
200# add_lua_test ( _testfile [ WORKING_DIRECTORY _working_dir ] )
201# Runs Lua script `_testfile` under CTest tester.
202# Optional named argument `WORKING_DIRECTORY` is current working directory to
203# run test under (defaults to ${CMAKE_CURRENT_BINARY_DIR}).
204# Both paths, if relative, are relative to ${CMAKE_CURRENT_SOURCE_DIR}.
205# Any modules previously defined with install_lua_module are automatically
206# preloaded (via package.preload) prior to running the test script.
207# Under LuaDist, set test=true in config.lua to enable testing.
208# USE: add_lua_test ( test/test1.lua [args...] [WORKING_DIRECTORY dir])
209macro ( add_lua_test _testfile )
210 if ( NOT SKIP_TESTING )
211 parse_arguments ( _ARG "WORKING_DIRECTORY" "" ${ARGN} )
212 include ( CTest )
213 find_program ( LUA NAMES lua lua.bat )
214 get_filename_component ( TESTFILEABS ${_testfile} ABSOLUTE )
215 get_filename_component ( TESTFILENAME ${_testfile} NAME )
216 get_filename_component ( TESTFILEBASE ${_testfile} NAME_WE )
217
218 # Write wrapper script.
219 # Note: One simple way to allow the script to find modules is
220 # to just put them in package.preload.
221 set ( TESTWRAPPER ${CMAKE_CURRENT_BINARY_DIR}/${TESTFILENAME} )
222 _make_module_table ( _table )
223 set ( TESTWRAPPERSOURCE
224"local CMAKE_CFG_INTDIR = ... or '.'
225${_table}
226local function preload_modules(modules)
227 for name, path in pairs(modules) do
228 if path:match'%.lua' then
229 package.preload[name] = assert(loadfile(path))
230 else
231 local name = name:gsub('.*%-', '') -- remove any hyphen prefix
232 local symbol = 'luaopen_' .. name:gsub('%.', '_')
233 --improve: generalize to support all-in-one loader?
234 local path = path:gsub('%$%{CMAKE_CFG_INTDIR%}', CMAKE_CFG_INTDIR)
235 package.preload[name] = assert(package.loadlib(path, symbol))
236 end
237 end
238end
239preload_modules(modules)
240arg[0] = '${TESTFILEABS}'
241table.remove(arg, 1)
242return assert(loadfile '${TESTFILEABS}')(unpack(arg))
243" )
244 if ( _ARG_WORKING_DIRECTORY )
245 get_filename_component (
246 TESTCURRENTDIRABS ${_ARG_WORKING_DIRECTORY} ABSOLUTE )
247 # note: CMake 2.6 (unlike 2.8) lacks WORKING_DIRECTORY parameter.
248 set ( _pre ${CMAKE_COMMAND} -E chdir "${TESTCURRENTDIRABS}" )
249 endif ()
250 file ( WRITE ${TESTWRAPPER} ${TESTWRAPPERSOURCE})
251 add_test ( NAME ${TESTFILEBASE} COMMAND ${_pre} ${LUA}
252 ${TESTWRAPPER} "${CMAKE_CFG_INTDIR}"
253 ${_ARG_DEFAULT_ARGS} )
254 endif ()
255 # see also http://gdcm.svn.sourceforge.net/viewvc/gdcm/Sandbox/CMakeModules/UsePythonTest.cmake
256 # Note: ${CMAKE_CFG_INTDIR} is a command-line argument to allow proper
257 # expansion by the native build tool.
258endmacro ()
259
260
261# Converts Lua source file `_source` to binary string embedded in C source
262# file `_target`. Optionally compiles Lua source to byte code (not available
263# under LuaJIT2, which doesn't have a bytecode loader). Additionally, Lua
264# versions of bin2c [1] and luac [2] may be passed respectively as additional
265# arguments.
266#
267# [1] http://lua-users.org/wiki/BinToCee
268# [2] http://lua-users.org/wiki/LuaCompilerInLua
269function ( add_lua_bin2c _target _source )
270 find_program ( LUA NAMES lua lua.bat )
271 execute_process ( COMMAND ${LUA} -e "string.dump(function()end)"
272 RESULT_VARIABLE _LUA_DUMP_RESULT ERROR_QUIET )
273 if ( NOT ${_LUA_DUMP_RESULT} )
274 SET ( HAVE_LUA_DUMP true )
275 endif ()
276 message ( "-- string.dump=${HAVE_LUA_DUMP}" )
277
278 if ( ARGV2 )
279 get_filename_component ( BIN2C ${ARGV2} ABSOLUTE )
280 set ( BIN2C ${LUA} ${BIN2C} )
281 else ()
282 find_program ( BIN2C NAMES bin2c bin2c.bat )
283 endif ()
284 if ( HAVE_LUA_DUMP )
285 if ( ARGV3 )
286 get_filename_component ( LUAC ${ARGV3} ABSOLUTE )
287 set ( LUAC ${LUA} ${LUAC} )
288 else ()
289 find_program ( LUAC NAMES luac luac.bat )
290 endif ()
291 endif ( HAVE_LUA_DUMP )
292 message ( "-- bin2c=${BIN2C}" )
293 message ( "-- luac=${LUAC}" )
294
295 get_filename_component ( SOURCEABS ${_source} ABSOLUTE )
296 if ( HAVE_LUA_DUMP )
297 get_filename_component ( SOURCEBASE ${_source} NAME_WE )
298 add_custom_command (
299 OUTPUT ${_target} DEPENDS ${_source}
300 COMMAND ${LUAC} -o ${CMAKE_CURRENT_BINARY_DIR}/${SOURCEBASE}.lo
301 ${SOURCEABS}
302 COMMAND ${BIN2C} ${CMAKE_CURRENT_BINARY_DIR}/${SOURCEBASE}.lo
303 ">${_target}" )
304 else ()
305 add_custom_command (
306 OUTPUT ${_target} DEPENDS ${SOURCEABS}
307 COMMAND ${BIN2C} ${_source} ">${_target}" )
308 endif ()
309endfunction()