|
| 1 | +# LuaDist CMake utility library. |
| 2 | +# Provides variables and utility functions common to LuaDist CMake builds. |
| 3 | +# |
| 4 | +# Copyright (C) 2007-2010 LuaDist. |
| 5 | +# by David Manura, Peter Drahos |
| 6 | +# Redistribution and use of this file is allowed according to the terms of the MIT license. |
| 7 | +# For details see the COPYRIGHT file distributed with LuaDist. |
| 8 | +# Please note that the package source code is licensed under its own license. |
| 9 | + |
| 10 | +# Few convinence settings |
| 11 | +SET (CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS true) |
| 12 | +SET (CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_MODULE_PATH}) |
| 13 | + |
| 14 | +# Where to install module parts: |
| 15 | +set(INSTALL_BIN bin CACHE PATH "Where to install binaries to.") |
| 16 | +set(INSTALL_LIB lib CACHE PATH "Where to install libraries to.") |
| 17 | +set(INSTALL_INC include CACHE PATH "Where to install headers to.") |
| 18 | +set(INSTALL_ETC etc CACHE PATH "Where to store configuration files") |
| 19 | +set(INSTALL_LMOD share/lua/lmod CACHE PATH "Directory to install Lua modules.") |
| 20 | +set(INSTALL_CMOD share/lua/cmod CACHE PATH "Directory to install Lua binary modules.") |
| 21 | +set(INSTALL_DATA share/${PROJECT_NAME} CACHE PATH "Directory the package can store documentation, tests or other data in.") |
| 22 | +set(INSTALL_DOC ${INSTALL_DATA}/doc CACHE PATH "Recommended directory to install documentation into.") |
| 23 | +set(INSTALL_EXAMPLE ${INSTALL_DATA}/example CACHE PATH "Recommended directory to install examples into.") |
| 24 | +set(INSTALL_TEST ${INSTALL_DATA}/test CACHE PATH "Recommended directory to install tests into.") |
| 25 | +set(INSTALL_FOO ${INSTALL_DATA}/etc CACHE PATH "Where to install additional files") |
| 26 | + |
| 27 | + |
| 28 | +# In MSVC, prevent warnings that can occur when using standard libraries. |
| 29 | +if(MSVC) |
| 30 | + add_definitions(-D_CRT_SECURE_NO_WARNINGS) |
| 31 | +endif(MSVC) |
| 32 | + |
| 33 | +# Adds Lua shared library module target `_target`. |
| 34 | +# Additional sources to build the module are listed after `_target`. |
| 35 | +macro(add_lua_module _target) |
| 36 | + find_package(Lua51 REQUIRED) |
| 37 | + include_directories(${LUA_INCLUDE_DIR}) #2DO: somehow apply only to _target? |
| 38 | + |
| 39 | + add_library(${_target} MODULE ${ARGN}) |
| 40 | + set_target_properties(${_target} PROPERTIES PREFIX "") |
| 41 | + target_link_libraries(${_target} ${LUA_LIBRARY}) |
| 42 | + |
| 43 | + IF(WIN32) |
| 44 | + set_target_properties(${_target} PROPERTIES LINK_FLAGS "-Wl,--enable-auto-import") |
| 45 | + ENDIF() |
| 46 | + |
| 47 | +endmacro(add_lua_module) |
| 48 | + |
| 49 | +# Runs Lua script `_testfile` under CTest tester. |
| 50 | +# Optional argument `_testcurrentdir` is current working directory to run test under |
| 51 | +# (defaults to ${CMAKE_CURRENT_BINARY_DIR}). |
| 52 | +# Both paths, if relative, are relative to ${CMAKE_CURRENT_SOURCE_DIR}. |
| 53 | +# Under LuaDist, set test=true in config.lua to enable testing. |
| 54 | +macro(add_lua_test _testfile) |
| 55 | + include(CTest) |
| 56 | + if(BUILD_TESTING) |
| 57 | + find_program(LUA NAMES lua lua.bat) |
| 58 | + get_filename_component(TESTFILEABS ${_testfile} ABSOLUTE) |
| 59 | + get_filename_component(TESTFILENAME ${_testfile} NAME) |
| 60 | + get_filename_component(TESTFILEBASE ${_testfile} NAME_WE) |
| 61 | + |
| 62 | + # Write wrapper script. |
| 63 | + set(TESTWRAPPER ${CMAKE_CURRENT_BINARY_DIR}/${TESTFILENAME}) |
| 64 | + set(TESTWRAPPERSOURCE |
| 65 | +"package.path = '${CMAKE_CURRENT_BINARY_DIR}/?.lua\;${CMAKE_CURRENT_SOURCE_DIR}/?.lua\;' .. package.path |
| 66 | +package.cpath = '${CMAKE_CURRENT_BINARY_DIR}/?.so\;${CMAKE_CURRENT_BINARY_DIR}/?.dll\;' .. package.cpath |
| 67 | +return dofile '${TESTFILEABS}' |
| 68 | +" ) |
| 69 | + if(${ARGC} GREATER 1) |
| 70 | + set(_testcurrentdir ${ARGV1}) |
| 71 | + get_filename_component(TESTCURRENTDIRABS ${_testcurrentdir} ABSOLUTE) |
| 72 | + set(TESTWRAPPERSOURCE |
| 73 | +"require 'lfs' |
| 74 | +lfs.chdir('${TESTCURRENTDIRABS}') |
| 75 | +${TESTWRAPPERSOURCE}") |
| 76 | + endif() |
| 77 | + FILE(WRITE ${TESTWRAPPER} ${TESTWRAPPERSOURCE}) |
| 78 | + |
| 79 | + add_test(${TESTFILEBASE} ${LUA} ${TESTWRAPPER}) |
| 80 | + endif(BUILD_TESTING) |
| 81 | + |
| 82 | + # see also http://gdcm.svn.sourceforge.net/viewvc/gdcm/Sandbox/CMakeModules/UsePythonTest.cmake |
| 83 | +endmacro(add_lua_test) |
| 84 | + |
| 85 | +# Converts Lua source file `_source` to binary string embedded in C source |
| 86 | +# file `_target`. Optionally compiles Lua source to byte code (not available |
| 87 | +# under LuaJIT2, which doesn't have a bytecode loader). Additionally, Lua |
| 88 | +# versions of bin2c [1] and luac [2] may be passed respectively as additional |
| 89 | +# arguments. |
| 90 | +# |
| 91 | +# [1] http://lua-users.org/wiki/BinToCee |
| 92 | +# [2] http://lua-users.org/wiki/LuaCompilerInLua |
| 93 | +function(add_lua_bin2c _target _source) |
| 94 | + find_program(LUA NAMES lua lua.bat) |
| 95 | + execute_process(COMMAND ${LUA} -e "string.dump(function()end)" RESULT_VARIABLE _LUA_DUMP_RESULT ERROR_QUIET) |
| 96 | + if (NOT ${_LUA_DUMP_RESULT}) |
| 97 | + SET(HAVE_LUA_DUMP true) |
| 98 | + endif() |
| 99 | + message("-- string.dump=${HAVE_LUA_DUMP}") |
| 100 | + |
| 101 | + if (ARGV2) |
| 102 | + get_filename_component(BIN2C ${ARGV2} ABSOLUTE) |
| 103 | + set(BIN2C ${LUA} ${BIN2C}) |
| 104 | + else() |
| 105 | + find_program(BIN2C NAMES bin2c bin2c.bat) |
| 106 | + endif() |
| 107 | + if (HAVE_LUA_DUMP) |
| 108 | + if (ARGV3) |
| 109 | + get_filename_component(LUAC ${ARGV3} ABSOLUTE) |
| 110 | + set(LUAC ${LUA} ${LUAC}) |
| 111 | + else() |
| 112 | + find_program(LUAC NAMES luac luac.bat) |
| 113 | + endif() |
| 114 | + endif (HAVE_LUA_DUMP) |
| 115 | + message("-- bin2c=${BIN2C}") |
| 116 | + message("-- luac=${LUAC}") |
| 117 | + |
| 118 | + get_filename_component(SOURCEABS ${_source} ABSOLUTE) |
| 119 | + if (HAVE_LUA_DUMP) |
| 120 | + get_filename_component(SOURCEBASE ${_source} NAME_WE) |
| 121 | + add_custom_command( |
| 122 | + OUTPUT ${_target} DEPENDS ${_source} |
| 123 | + COMMAND ${LUAC} -o ${CMAKE_CURRENT_BINARY_DIR}/${SOURCEBASE}.lo ${SOURCEABS} |
| 124 | + COMMAND ${BIN2C} ${CMAKE_CURRENT_BINARY_DIR}/${SOURCEBASE}.lo ">${_target}" ) |
| 125 | + else() |
| 126 | + add_custom_command( |
| 127 | + OUTPUT ${_target} DEPENDS ${SOURCEABS} |
| 128 | + COMMAND ${BIN2C} ${_source} ">${_target}" ) |
| 129 | + endif() |
| 130 | +endfunction(add_lua_bin2c) |
0 commit comments