Skip to content

Commit

Permalink
Updated CMake macros. Clean up of CMakeLists.txt
Browse files Browse the repository at this point in the history
  • Loading branch information
drahosp committed Jul 31, 2012
1 parent 3011a2c commit b9a70a6
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 164 deletions.
70 changes: 37 additions & 33 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# Please note that the package source code is licensed under its own license.

project ( lua C )
cmake_minimum_required ( VERSION 2.6 )
cmake_minimum_required ( VERSION 2.8 )
include ( cmake/dist.cmake )
include ( lua )

Expand All @@ -16,14 +16,14 @@ set ( LUA_CPATH "LUA_CPATH" CACHE STRING "Environment variable to use as package
set ( LUA_INIT "LUA_INIT" CACHE STRING "Environment variable for initial script." )

option ( LUA_ANSI "Use only ansi features." OFF )
option ( LUA_USE_RELATIVE_LOADLIB "Use modified loadlib.c with support for relative paths on posix systems." ON)
option ( LUA_USE_RELATIVE_LOADLIB "Use modified loadlib.c with support for relative paths on posix systems."
ON )
set ( LUA_IDSIZE 60 CACHE NUMBER "gives the maximum size for the description of the source." )
set ( LUA_PROMPT "> " CACHE STRING "Is the default prompt used by stand-alone Lua." )
set ( LUA_PROMPT2 ">> " CACHE STRING "Is the default continuation prompt used by stand-alone Lua." )
set ( LUA_MAXINPUT 512 CACHE NUMBER "Is the maximum length for an input line in the stand-alone interpreter.")
set ( LUA_MAXINPUT 512 CACHE NUMBER "Is the maximum length for an input line in the stand-alone interpreter." )

#2DO: LUAI_* and LUAL_* settings, for now defaults are used.

set ( LUA_DIRSEP "/" )
set ( LUA_MODULE_SUFFIX ${CMAKE_SHARED_MODULE_SUFFIX} )
set ( LUA_LDIR ${INSTALL_LMOD} )
Expand All @@ -32,58 +32,57 @@ set ( LUA_CDIR ${INSTALL_CMOD} )
if ( LUA_USE_RELATIVE_LOADLIB )
# This will set up relative paths to lib
string ( REGEX REPLACE "[^!/]+" ".." LUA_DIR "!/${INSTALL_BIN}/" )
else ()
else ( )
# Direct path to installation
set ( LUA_DIR ${CMAKE_INSTALL_PREFIX} CACHE STRING "Destination from which modules will be resolved. See INSTALL_LMOD and INSTALL_CMOD.")
endif ()
set ( LUA_DIR ${CMAKE_INSTALL_PREFIX} CACHE STRING "Destination from which modules will be resolved. See INSTALL_LMOD and INSTALL_CMOD." )
endif ( )

set ( LUA_PATH_DEFAULT "./?.lua;${LUA_DIR}${LUA_LDIR}/?.lua;${LUA_DIR}${LUA_LDIR}/?/init.lua;./?/init.lua" )
set ( LUA_CPATH_DEFAULT "./?${LUA_MODULE_SUFFIX};${LUA_DIR}${LUA_CDIR}/?${LUA_MODULE_SUFFIX};${LUA_DIR}${LUA_CDIR}/loadall${LUA_MODULE_SUFFIX}" )

if ( WIN32 AND NOT CYGWIN )
# Windows systems
option ( LUA_WIN "Windows specific build." ON )
option ( LUA_BUILD_WLUA "Build wLua interpretter without console output." ON)
option ( LUA_BUILD_WLUA "Build wLua interpretter without console output." ON )
option ( LUA_BUILD_AS_DLL "Build Lua library as Dll." ON )
# Paths (Double escapes needed)
set ( LUA_DIRSEP "\\\\" )
string ( REPLACE "/" ${LUA_DIRSEP} LUA_DIR "${LUA_DIR}" )
set ( LUA_DIRSEP "\\\\" ) string ( REPLACE " /" ${LUA_DIRSEP} LUA_DIR "${LUA_DIR}" )
string ( REPLACE "/" ${LUA_DIRSEP} LUA_LDIR "${LUA_LDIR}" )
string ( REPLACE "/" ${LUA_DIRSEP} LUA_CDIR "${LUA_CDIR}" )
string ( REPLACE "/" ${LUA_DIRSEP} LUA_PATH_DEFAULT "${LUA_PATH_DEFAULT}" )
string ( REPLACE "/" ${LUA_DIRSEP} LUA_CPATH_DEFAULT "${LUA_CPATH_DEFAULT}" )
else ()
else ( )
# Posix systems (incl. Cygwin)
option ( LUA_USE_POSIX "Use POSIX functionality." ON )
option ( LUA_USE_DLOPEN "Use dynamic linker to load modules." ON )
option ( LUA_USE_MKSTEMP "Use mkstep." ON )
option ( LUA_USE_ISATTY "Use tty." ON )
option ( LUA_USE_POPEN "Use popen." ON )
option ( LUA_USE_ULONGJMP "Use ulongjmp" ON)
endif ()
option ( LUA_USE_ULONGJMP "Use ulongjmp" ON )
endif ( )

## SETUP
# Optional libraries
find_package ( Readline )
if ( READLINE_FOUND )
option ( LUA_USE_READLINE "Use readline in the Lua CLI." ON )
endif ()
endif ( )

find_package ( Curses )
if ( CURSES_FOUND )
option ( LUA_USE_CURSES "Use curses in the Lua CLI." ON )
endif ()
endif ( )

# Setup needed variables and libraries
if ( LUA_USE_POSIX )
# On POSIX Lua links to standard math library "m"
list ( APPEND LIBS m )
endif ()
endif ( )

if ( LUA_USE_DLOPEN )
# Link to dynamic linker library "dl"
list ( APPEND LIBS dl )
endif ()
endif ( )

if ( LUA_WIN )
# Add extra rc files to the windows build
Expand All @@ -92,37 +91,41 @@ if ( LUA_WIN )
set ( LUA_DLL_RC src/lua_dll.rc )
set ( LUA_RC src/lua.rc )
set ( LUAC_RC src/luac.rc )
endif ()
endif ()
endif ( )
endif ( )

if ( LUA_USE_READLINE )
# Add readline
include_directories ( ${READLINE_INCLUDE_DIR} )
list ( APPEND LIBS ${READLINE_LIBRARY} )
endif ()
endif ( )

if ( LUA_USE_CURSES )
# Add curses
include_directories ( ${CURSES_INCLUDE_DIR} )
list ( APPEND LIBS ${CURSES_LIBRARY} )
endif ()
endif ( )

## SOURCES
# Generate luaconf.h
configure_file ( src/luaconf.h.in ${CMAKE_CURRENT_BINARY_DIR}/luaconf.h )

# Sources and headers
include_directories ( src ${CMAKE_CURRENT_BINARY_DIR} )
set ( SRC_LIB src/lapi.c src/lcode.c src/ldebug.c src/ldo.c src/ldump.c src/lfunc.c src/lgc.c src/llex.c src/lmem.c src/lobject.c src/lopcodes.c src/lparser.c src/lstate.c src/lstring.c src/ltable.c src/ltm.c src/lundump.c src/lvm.c src/lzio.c src/lauxlib.c src/lbaselib.c src/ldblib.c src/liolib.c src/lmathlib.c src/loslib.c src/ltablib.c src/lstrlib.c src/linit.c )
set ( SRC_LIB src/lapi.c src/lcode.c src/ldebug.c src/ldo.c src/ldump.c src/lfunc.c
src/lgc.c src/llex.c src/lmem.c src/lobject.c src/lopcodes.c src/lparser.c src/lstate.c
src/lstring.c src/ltable.c src/ltm.c src/lundump.c src/lvm.c src/lzio.c src/lauxlib.c
src/lbaselib.c src/ldblib.c src/liolib.c src/lmathlib.c src/loslib.c src/ltablib.c
src/lstrlib.c src/linit.c )
set ( SRC_LUA src/lua.c )
set ( SRC_LUAC src/luac.c src/print.c )

if ( LUA_USE_RELATIVE_LOADLIB )
# Use modified loadlib
list ( APPEND SRC_LIB src/loadlib_rel.c )
else ()
else ( )
list ( APPEND SRC_LIB src/loadlib.c )
endif ()
endif ( )

## BUILD
# Create dynamic library
Expand All @@ -138,11 +141,11 @@ add_executable ( lua ${SRC_LUA} ${LUA_RC} )
target_link_libraries ( lua liblua )

# On windows a variant of the lua interpreter without console output needs to be built
if(LUA_BUILD_WLUA)
if ( LUA_BUILD_WLUA )
add_executable ( wlua WIN32 ${SRC_LUA} ${LUA_RC} )
target_link_libraries ( wlua liblua )
target_link_libraries ( wlua liblua )
install_executable ( wlua )
endif()
endif ( )

add_executable ( luac ${SRC_LUAC} ${LUAC_RC} )
target_link_libraries ( luac liblua_static )
Expand All @@ -151,13 +154,13 @@ install_executable ( lua luac )
install_library ( liblua )
install_data ( README.md COPYRIGHT HISTORY )
install_lua_module ( strict etc/strict.lua )
install_header (src/lua.h src/lualib.h src/lauxlib.h etc/lua.hpp ${CMAKE_CURRENT_BINARY_DIR}/luaconf.h )
install_header ( src/lua.h src/lualib.h src/lauxlib.h etc/lua.hpp ${CMAKE_CURRENT_BINARY_DIR}/luaconf.h )
install_doc ( doc/ )
install_foo ( etc/ )
install_test ( test/ )

## TESTS
set(LUA lua)
set ( LUA lua )

add_lua_test ( test/bisect.lua )
add_lua_test ( test/cf.lua )
Expand All @@ -168,10 +171,11 @@ add_lua_test ( test/fib.lua 20 )
add_lua_test ( test/fibfor.lua )
#add_lua_test ( test/globals.lua ) # Requires input
add_lua_test ( test/hello.lua )
file(READ test/life.lua _data) # life.lua test, with reduced run-time.
string(REPLACE "40,20" "20,15" _data "${_data}")
string(REPLACE 2000 20 _data "${_data}")
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/test/life-quick.lua "${_data}")
file ( READ test/life.lua _data )
# life.lua test, with reduced run-time.
string ( REPLACE "40,20" "20,15" _data "${_data}" )
string ( REPLACE 2000 20 _data "${_data}" )
file ( WRITE ${CMAKE_CURRENT_BINARY_DIR}/test/life-quick.lua "${_data}" )
add_lua_test ( ${CMAKE_CURRENT_BINARY_DIR}/test/life-quick.lua )
#add_lua_test ( test/luac.lua ) # Requires input
add_lua_test ( test/printf.lua )
Expand Down
162 changes: 31 additions & 131 deletions cmake/lua.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -29,141 +29,39 @@ macro ( _append_path basepath path result )
endmacro ()

# install_lua_executable ( target source )
# Automatically generate a binary wrapper for lua application and install it
# The wrapper and the source of the application will be placed into /bin
# Automatically generate a binary if srlua package is available
# The application or its source will be placed into /bin
# If the application source did not have .lua suffix then it will be added
# USE: lua_executable ( sputnik src/sputnik.lua )
macro ( install_lua_executable _name _source )
get_filename_component ( _source_name ${_source} NAME_WE )
if ( NOT SKIP_LUA_WRAPPER )
enable_language ( C )
# Find srlua and glue
find_program( SRLUA_EXECUTABLE NAMES srlua )
find_program( GLUE_EXECUTABLE NAMES glue )

find_package ( Lua REQUIRED )
include_directories ( ${LUA_INCLUDE_DIR} )

set ( _wrapper ${CMAKE_CURRENT_BINARY_DIR}/${_name}.c )
set ( _code
"// Not so simple executable wrapper for Lua apps
#include <stdio.h>
#include <signal.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
lua_State *L\;
static int getargs (lua_State *L, char **argv, int n) {
int narg\;
int i\;
int argc = 0\;
while (argv[argc]) argc++\;
narg = argc - (n + 1)\;
luaL_checkstack(L, narg + 3, \"too many arguments to script\")\;
for (i=n+1\; i < argc\; i++)
lua_pushstring(L, argv[i])\;
lua_createtable(L, narg, n + 1)\;
for (i=0\; i < argc\; i++) {
lua_pushstring(L, argv[i])\;
lua_rawseti(L, -2, i - n)\;
}
return narg\;
}
static void lstop (lua_State *L, lua_Debug *ar) {
(void)ar\;
lua_sethook(L, NULL, 0, 0)\;
luaL_error(L, \"interrupted!\")\;
}
static void laction (int i) {
signal(i, SIG_DFL)\;
lua_sethook(L, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1)\;
}
static void l_message (const char *pname, const char *msg) {
if (pname) fprintf(stderr, \"%s: \", pname)\;
fprintf(stderr, \"%s\\n\", msg)\;
fflush(stderr)\;
}
static int report (lua_State *L, int status) {
if (status && !lua_isnil(L, -1)) {
const char *msg = lua_tostring(L, -1)\;
if (msg == NULL) msg = \"(error object is not a string)\"\;
l_message(\"${_source_name}\", msg)\;
lua_pop(L, 1)\;
}
return status\;
}
static int traceback (lua_State *L) {
if (!lua_isstring(L, 1))
return 1\;
lua_getfield(L, LUA_GLOBALSINDEX, \"debug\")\;
if (!lua_istable(L, -1)) {
lua_pop(L, 1)\;
return 1\;
}
lua_getfield(L, -1, \"traceback\")\;
if (!lua_isfunction(L, -1)) {
lua_pop(L, 2)\;
return 1\;
}
lua_pushvalue(L, 1)\;
lua_pushinteger(L, 2)\;
lua_call(L, 2, 1)\;
return 1\;
}
static int docall (lua_State *L, int narg, int clear) {
int status\;
int base = lua_gettop(L) - narg\;
lua_pushcfunction(L, traceback)\;
lua_insert(L, base)\;
signal(SIGINT, laction)\;
status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base)\;
signal(SIGINT, SIG_DFL)\;
lua_remove(L, base)\;
if (status != 0) lua_gc(L, LUA_GCCOLLECT, 0)\;
return status\;
}
int main (int argc, char **argv) {
L=lua_open()\;
lua_gc(L, LUA_GCSTOP, 0)\;
luaL_openlibs(L)\;
lua_gc(L, LUA_GCRESTART, 0)\;
int narg = getargs(L, argv, 0)\;
lua_setglobal(L, \"arg\")\;
// Script
char script[500] = \"./${_source_name}.lua\"\;
lua_getglobal(L, \"_PROGDIR\")\;
if (lua_isstring(L, -1)) {
sprintf( script, \"%s/${_source_name}.lua\", lua_tostring(L, -1))\;
}
lua_pop(L, 1)\;
// Run
int status = luaL_loadfile(L, script)\;
lua_insert(L, -(narg+1))\;
if (status == 0)
status = docall(L, narg, 0)\;
else
lua_pop(L, narg)\;
report(L, status)\;
lua_close(L)\;
return status\;
};
")
file ( WRITE ${_wrapper} ${_code} )
add_executable ( ${_name} ${_wrapper} )
target_link_libraries ( ${_name} ${LUA_LIBRARY} )
install ( TARGETS ${_name} DESTINATION ${INSTALL_BIN} )
if ( NOT SKIP_LUA_WRAPPER AND SRLUA_EXECUTABLE AND GLUE_EXECUTABLE )
# Generate binary gluing the lua code to srlua
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_name}
COMMAND ${GLUE_EXECUTABLE}
ARGS ${SRLUA_EXECUTABLE} ${_source} ${CMAKE_CURRENT_BINARY_DIR}/${_name}
DEPENDS ${_source}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
VERBATIM
)
# Make sure we have a target associated with the binary
add_custom_target(${_name} ALL
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${_name}
)
# Install with run permissions
install ( PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/${_name} DESTINATION ${INSTALL_BIN} COMPONENT Runtime)
else()
# Add .lua suffix and install as is
install ( PROGRAMS ${_source} DESTINATION ${INSTALL_BIN}
RENAME ${_source_name}.lua
COMPONENT Runtime
)
endif()
install ( PROGRAMS ${_source} DESTINATION ${INSTALL_BIN}
RENAME ${_source_name}.lua )
endmacro ()

macro ( _lua_module_helper is_install _name )
Expand Down Expand Up @@ -206,7 +104,9 @@ macro ( _lua_module_helper is_install _name )

if ( ${is_install} )
install ( FILES ${_first_source} DESTINATION ${INSTALL_LMOD}/${_module_dir}
RENAME ${_module_filename} )
RENAME ${_module_filename}
COMPONENT Runtime
)
endif ()
else () # Lua C binary module
enable_language ( C )
Expand All @@ -227,7 +127,7 @@ macro ( _lua_module_helper is_install _name )
set_target_properties ( ${_target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY
"${_module_dir}" PREFIX "" OUTPUT_NAME "${_module_filenamebase}" )
if ( ${is_install} )
install ( TARGETS ${_target} DESTINATION ${INSTALL_CMOD}/${_module_dir})
install ( TARGETS ${_target} DESTINATION ${INSTALL_CMOD}/${_module_dir} COMPONENT Runtime)
endif ()
endif ()
endmacro ()
Expand Down

0 comments on commit b9a70a6

Please sign in to comment.