lua.cmake 11 KB

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