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