Skip to content
This repository was archived by the owner on Nov 20, 2020. It is now read-only.

Commit e654d54

Browse files
committed
Updated build, tested on OSX.
1 parent 923d1e3 commit e654d54

8 files changed

+1657
-176
lines changed

Diff for: .gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
_*
2+
.DS_Store
3+

Diff for: CMakeLists.txt

+155-64
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,159 @@
1-
# Copyright (C) 2007-2009 LuaDist.
2-
# Created by Peter Drahoš and Peter Kapec
1+
# Copyright (C) 2007-2011 LuaDist.
2+
# Created by Peter Drahoš, Peter Kapec
33
# Redistribution and use of this file is allowed according to the terms of the MIT license.
44
# For details see the COPYRIGHT file distributed with LuaDist.
55
# Please note that the package source code is licensed under its own license.
66

7-
PROJECT ( lua C )
8-
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
9-
INCLUDE(dist.cmake )
10-
11-
# Determine install host
12-
IF ( WIN32 AND NOT CYGWIN)
13-
ADD_DEFINITIONS ( -DLUA_BUILD_AS_DLL )
14-
IF(MSVC)
15-
ADD_DEFINITIONS(-D_CRT_SECURE_NO_DEPRECATE )
16-
SET(DEF_FILE src/lua.def)
17-
SET(DLL_RC_FILE src/lua_dll.rc)
18-
SET(LUA_RC_FILE src/lua.rc)
19-
SET(LUAC_RC_FILE src/lua_simple.rc)
20-
ENDIF()
21-
ELSE ( )
22-
ADD_DEFINITIONS ( -DLUA_USE_POSIX -DLUA_USE_DLOPEN )
23-
SET ( LIBS m dl )
24-
ENDIF ( )
25-
26-
# Add Readline support when available
27-
FIND_PATH(READLINE_INCLUDE_DIR readline/readline.h)
28-
FIND_LIBRARY(READLINE_LIBRARY NAMES readline)
29-
IF ( READLINE_LIBRARY )
30-
INCLUDE_DIRECTORIES ( ${READLINE_INCLUDE_DIR} )
31-
ADD_DEFINITIONS ( -DLUA_USE_READLINE )
32-
SET ( LIBS ${LIBS} ${READLINE_LIBRARY} )
33-
ENDIF ( )
34-
35-
# Add Curses support when available
36-
INCLUDE(FindCurses)
37-
IF ( CURSES_LIBRARY )
38-
INCLUDE_DIRECTORIES ( ${CURSES_INCLUDE_DIR} )
39-
SET ( LIBS ${LIBS} ${CURSES_LIBRARY} )
40-
ENDIF ( )
41-
42-
# Build Libraries
43-
SET ( SRC_LIBLUA 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/loadlib.c src/linit.c )
44-
45-
ADD_LIBRARY ( liblua SHARED ${SRC_LIBLUA} ${DEF_FILE} ${DLL_RC_FILE})
46-
TARGET_LINK_LIBRARIES ( liblua ${LIBS})
47-
SET_TARGET_PROPERTIES ( liblua PROPERTIES OUTPUT_NAME lua51 CLEAN_DIRECT_OUTPUT 1 )
48-
49-
ADD_LIBRARY ( liblua_static ${SRC_LIBLUA} )
50-
TARGET_LINK_LIBRARIES ( liblua_static ${LIBS})
51-
52-
INCLUDE_DIRECTORIES ( src )
53-
54-
# Build Executables
55-
SET ( SRC_LUA src/lua.c )
56-
SET ( SRC_LUAC src/luac.c src/print.c )
57-
58-
ADD_EXECUTABLE ( lua ${SRC_LUA} ${LUA_RC_FILE})
59-
ADD_EXECUTABLE ( luac ${SRC_LUAC} ${LUAC_RC_FILE})
60-
TARGET_LINK_LIBRARIES ( lua liblua )
61-
TARGET_LINK_LIBRARIES ( luac liblua_static )
62-
63-
# Install
64-
INSTALL ( TARGETS lua luac liblua RUNTIME DESTINATION ${INSTALL_BIN} LIBRARY DESTINATION ${INSTALL_LIB} ARCHIVE DESTINATION ${INSTALL_LIB})
65-
INSTALL ( FILES src/lua.h src/luaconf.h src/lualib.h src/lauxlib.h etc/lua.hpp DESTINATION ${INSTALL_INC} )
66-
INSTALL ( FILES etc/strict.lua DESTINATION ${INSTALL_LMOD} )
67-
INSTALL ( DIRECTORY doc etc test DESTINATION ${INSTALL_DATA} PATTERN ".git" EXCLUDE )
68-
INSTALL ( FILES README COPYRIGHT HISTORY DESTINATION ${INSTALL_DATA} )
7+
project ( lua C )
8+
cmake_minimum_required ( VERSION 2.6 )
9+
include ( dist.cmake )
10+
11+
## CONFIGURATION
12+
# Default configuration (we assume POSIX by default)
13+
set ( LUA_PATH "LUA_PATH" CACHE STRING "Environment variable to use as package.path." )
14+
set ( LUA_CPATH "LUA_CPATH" CACHE STRING "Environment variable to use as package.cpath." )
15+
set ( LUA_INIT "LUA_INIT" CACHE STRING "Environment variable for initial script." )
16+
17+
option ( LUA_ANSI "Use only ansi features." OFF )
18+
option ( LUA_USE_RELATIVE_LOADLIB "Use modified loadlib.c with support for relative paths on posix systems." ON)
19+
set ( LUA_IDSIZE 60 CACHE NUMBER "gives the maximum size for the description of the source." )
20+
set ( LUA_PROMPT "> " CACHE STRING "Is the default prompt used by stand-alone Lua." )
21+
set ( LUA_PROMPT2 ">> " CACHE STRING "Is the default continuation prompt used by stand-alone Lua." )
22+
set ( LUA_MAXINPUT 512 CACHE NUMBER "Is the maximum length for an input line in the stand-alone interpreter.")
23+
24+
#2DO: LUAI_* and LUAL_* settings, for now defaults are used.
25+
26+
if ( WIN32 AND NOT CYGWIN )
27+
# Windows systems
28+
option ( LUA_WIN "Windows specific build." ON )
29+
option ( LUA_BUILD_AS_DLL "Build Lua library as Dll." ON )
30+
# Paths
31+
set ( LUA_DIRSEP "\\" )
32+
set ( LUA_LDIR "!\\lua\\" CACHE STRING "Pure Lua module location." )
33+
set ( LUA_CDIR "!\\" CACHE STRING "Binary Lua module location." )
34+
set ( LUA_PATH_DEFAULT ".\\?.lua;${LUA_LDIR}?.lua;${LUA_LDIR}?\\init.lua;${LUA_CDIR}?.lua;${LUA_CDIR}?\\init.lua" )
35+
set ( LUA_CPATH_DEFAULT ".\\?${CMAKE_SHARED_MODULE_SUFFIX};${LUA_CDIR}?${CMAKE_SHARED_MODULE_SUFFIX};${LUA_CDIR}loadall${CMAKE_SHARED_MODULE_SUFFIX}" )
36+
else ()
37+
# Posix systems
38+
option ( LUA_USE_POSIX "Use POSIX functionality." ON )
39+
option ( LUA_USE_DLOPEN "Use dynamic linker to load modules." ON )
40+
option ( LUA_USE_MKSTEMP "Use mkstep." ON )
41+
option ( LUA_USE_ISATTY "Use tty." ON )
42+
option ( LUA_USE_POPEN "Use popen." ON )
43+
option ( LUA_USE_ULONGJMP "Use ulongjmp" ON)
44+
# Paths
45+
set ( LUA_DIRSEP "/" )
46+
set ( LUA_LDIR "/usr/local/share/lua/5.1/" CACHE STRING "Pure Lua module location." )
47+
set ( LUA_CDIR "/usr/local/lib/lua/5.1/" CACHE STRING "Binary Lua module location." )
48+
set ( LUA_PATH_DEFAULT "./?.lua;${LUA_LDIR}?.lua;${LUA_LDIR}?/init.lua;${LUA_CDIR}?.lua;${LUA_CDIR}?/init.lua" )
49+
set ( LUA_CPATH_DEFAULT "./?${CMAKE_SHARED_MODULE_SUFFIX};${LUA_CDIR}?${CMAKE_SHARED_MODULE_SUFFIX};${LUA_CDIR}loadall${CMAKE_SHARED_MODULE_SUFFIX}" )
50+
endif ()
51+
52+
## SETUP
53+
# Optional libraries
54+
find_package ( Readline )
55+
if ( READLINE_FOUND )
56+
option ( LUA_USE_READLINE "Use readline in the Lua CLI." ON )
57+
endif ()
58+
59+
find_package ( Curses )
60+
if ( CURSES_FOUND )
61+
option ( LUA_USE_CURSES "Use curses in the Lua CLI." ON )
62+
endif ()
63+
64+
# Setup needed variables and libraries
65+
if ( LUA_USE_POSIX )
66+
# On POSIX Lua links to standard math library "m"
67+
list ( APPEND LIBS m )
68+
endif ()
69+
70+
if ( LUA_USE_DLOPEN )
71+
# Link to dynamic linker library "dl"
72+
list ( APPEND LIBS dl )
73+
endif ()
74+
75+
if ( LUA_WIN )
76+
ADD_DEFINITIONS(-D_CRT_SECURE_NO_DEPRECATE )
77+
# Add extra rc files to the windows build
78+
if ( MSVC OR MINGW )
79+
set ( LUA_DEF src/lua.def )
80+
set ( LUA_DLL_RC src/lua_dll.rc )
81+
set ( LUA_RC src/lua.rc )
82+
set ( LUAC_RC src/lua_simple.rc )
83+
endif ()
84+
endif ()
85+
86+
if ( LUA_USE_READLINE )
87+
# Add readline
88+
include_directories ( ${READLINE_INCLUDE_DIR} )
89+
list ( APPEND LIBS ${READLINE_LIBRARY} )
90+
endif ()
91+
92+
if ( LUA_USE_CURSES )
93+
# Add curses
94+
include_directories ( ${CURSES_INCLUDE_DIR} )
95+
list ( APPEND LIBS ${CURSES_LIBRARY} )
96+
endif ()
97+
98+
## SOURCES
99+
# Generate luaconf.h
100+
configure_file ( src/luaconf.h.in ${CMAKE_CURRENT_BINARY_DIR}/luaconf.h )
101+
102+
# Sources and headers
103+
include_directories ( src ${CMAKE_CURRENT_BINARY_DIR} )
104+
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 )
105+
set ( SRC_LUA src/lua.c )
106+
set ( SRC_LUAC src/luac.c src/print.c )
107+
108+
if ( LUA_USE_RELATIVE_LOADLIB )
109+
# Use modified loadlib
110+
list ( APPEND SRC_LIB src/loadlib_rel.c )
111+
else ()
112+
list ( APPEND SRC_LIB src/loadlib.c )
113+
endif ()
114+
115+
## BUILD
116+
# Create dynamic library
117+
add_library ( liblua SHARED ${SRC_LIB} ${LUA_DLL_RC} ${LUA_DEF} )
118+
target_link_libraries ( liblua ${LIBS} )
119+
set_target_properties ( liblua PROPERTIES OUTPUT_NAME lua51 CLEAN_DIRECT_OUTPUT 1 )
120+
121+
# Create static library, this is needed to compile luac in the 5.1.x Lua series
122+
add_library ( liblua_static STATIC ${SRC_LIB} )
123+
target_link_libraries ( liblua_static ${LIBS} )
124+
125+
add_executable ( lua ${SRC_LUA} ${LUA_RC} )
126+
target_link_libraries ( lua liblua )
127+
128+
add_executable ( luac ${SRC_LUAC} ${LUAC_RC} )
129+
target_link_libraries ( luac liblua_static )
130+
131+
## INSTALL
132+
install ( TARGETS lua luac liblua RUNTIME DESTINATION ${INSTALL_BIN} LIBRARY DESTINATION ${INSTALL_LIB} ARCHIVE DESTINATION ${INSTALL_LIB} )
133+
install ( FILES src/lua.h src/lualib.h src/lauxlib.h etc/lua.hpp ${CMAKE_CURRENT_BINARY_DIR}/luaconf.h DESTINATION ${INSTALL_INC} )
134+
install ( FILES etc/strict.lua DESTINATION ${INSTALL_LMOD} )
135+
install ( DIRECTORY doc etc test DESTINATION ${INSTALL_DATA} )
136+
install ( FILES README COPYRIGHT HISTORY DESTINATION ${INSTALL_DATA} )
137+
138+
## TESTS
139+
add_lua_test ( test/bisect.lua )
140+
add_lua_test ( test/cf.lua )
141+
add_lua_test ( test/echo.lua )
142+
add_lua_test ( test/env.lua )
143+
add_lua_test ( test/factorial.lua )
144+
add_lua_test ( test/fib.lua )
145+
add_lua_test ( test/fibfor.lua )
146+
#add_lua_test ( test/globals.lua ) # Requires input
147+
add_lua_test ( test/hello.lua )
148+
add_lua_test ( test/life.lua )
149+
#add_lua_test ( test/luac.lua ) # Requires input
150+
add_lua_test ( test/printf.lua )
151+
#add_lua_test ( test/readonly.lua ) # this is aimed to fail, modification is required
152+
add_lua_test ( test/sieve.lua )
153+
add_lua_test ( test/sort.lua )
154+
#add_lua_test ( test/table.lua ) # Requires input
155+
add_lua_test ( test/trace-calls.lua )
156+
add_lua_test ( test/trace-globals.lua )
157+
#add_lua_test ( test/xd.lua ) # Requires input
158+
159+

Diff for: FindReadline.cmake

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# - Try to find Readline
2+
# Once done this will define
3+
# READLINE_FOUND - System has readline
4+
# READLINE_INCLUDE_DIRS - The readline include directories
5+
# READLINE_LIBRARIES - The libraries needed to use readline
6+
# READLINE_DEFINITIONS - Compiler switches required for using readline
7+
8+
find_package ( PkgConfig )
9+
pkg_check_modules ( PC_READLINE QUIET readline )
10+
set ( READLINE_DEFINITIONS ${PC_READLINE_CFLAGS_OTHER} )
11+
12+
find_path ( READLINE_INCLUDE_DIR readline/readline.h
13+
HINTS ${PC_READLINE_INCLUDEDIR} ${PC_READLINE_INCLUDE_DIRS}
14+
PATH_SUFFIXES readline )
15+
16+
find_library ( READLINE_LIBRARY NAMES readline
17+
HINTS ${PC_READLINE_LIBDIR} ${PC_READLINE_LIBRARY_DIRS} )
18+
19+
set ( READLINE_LIBRARIES ${READLINE_LIBRARY} )
20+
set ( READLINE_INCLUDE_DIRS ${READLINE_INCLUDE_DIR} )
21+
22+
include ( FindPackageHandleStandardArgs )
23+
# handle the QUIETLY and REQUIRED arguments and set READLINE_FOUND to TRUE
24+
# if all listed variables are TRUE
25+
find_package_handle_standard_args ( readline DEFAULT_MSG READLINE_LIBRARY READLINE_INCLUDE_DIR )

Diff for: README

+12-3
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,18 @@ See HISTORY for a summary of changes since the last released version.
2323

2424
* Installation
2525
------------
26-
Lua is implemented in pure ANSI C, and compiles unmodified in all known
27-
platforms that have an ANSI C compiler. In most Unix-like platforms, simply
28-
do "make" with a suitable target. See INSTALL for detailed instructions.
26+
In the LuaDist distribution the default makefiles have been replaced with
27+
CMake. To build Lua use the following approach.
28+
> mkdir _build && cd_build
29+
> ccmake .. # Set up configuration
30+
> cmake --build . --target install
31+
32+
The default makefile should work too, you just need to edit src/luaconf.h.orig
33+
and save it as src/luaconf.h.
34+
35+
> make
36+
37+
See INSTALL for detailed instructions with make.
2938

3039
* Origin
3140
------

Diff for: src/loadlib.c

+22-60
Original file line numberDiff line numberDiff line change
@@ -39,70 +39,14 @@
3939
#define ERRLIB 1
4040
#define ERRFUNC 2
4141

42+
#define setprogdir(L) ((void)0)
43+
44+
4245
static void ll_unloadlib (void *lib);
4346
static void *ll_load (lua_State *L, const char *path);
4447
static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym);
45-
static void setprogdir (lua_State *L);
4648

47-
/*
48-
** {=========================================================================
49-
** This determines the location of the executable for relative module loading
50-
** ==========================================================================
51-
*/
52-
#if defined(_WIN32) || defined(__CYGWIN__)
53-
#include <windows.h>
54-
#define PATH_MAX MAX_PATH
55-
#endif
5649

57-
static void setprogdir (lua_State *L) {
58-
char progdir[PATH_MAX + 1];
59-
char *lb;
60-
int nsize = sizeof(progdir)/sizeof(char);
61-
int n;
62-
#if defined(__CYGWIN__)
63-
char win_buff[PATH_MAX + 1];
64-
GetModuleFileNameA(NULL, win_buff, nsize);
65-
cygwin_conv_to_posix_path(win_buff, progdir);
66-
n = strlen(progdir);
67-
#elif defined(_WIN32)
68-
n = GetModuleFileNameA(NULL, progdir, nsize);
69-
#elif defined(__linux__)
70-
n = readlink("/proc/self/exe", progdir, nsize);
71-
if (n > 0) progdir[n] = 0;
72-
#elif defined(__FreeBSD__)
73-
n = readlink("/proc/curproc/file", progdir, nsize);
74-
if (n > 0) progdir[n] = 0;
75-
#else
76-
// FALLBACK
77-
// Use 'lsof' ... should work on most UNIX systems (incl. OSX)
78-
// lsof will list open files, this captures the 1st file listed (usually the executable)
79-
int pid;
80-
FILE* fd;
81-
char cmd[80];
82-
pid = getpid();
83-
84-
sprintf(cmd, "lsof -p %d | awk '{if ($5==\"REG\") { print $9 ; exit}}' 2> /dev/null", pid);
85-
fd = popen(cmd, "r");
86-
n = fread(progdir, 1, nsize, fd);
87-
88-
// remove newline
89-
if (n > 1) progdir[--n] = '\0';
90-
#endif
91-
if (n == 0 || n == nsize || (lb = strrchr(progdir, (int)LUA_DIRSEP[0])) == NULL)
92-
luaL_error(L, "unable to get process executable path");
93-
else {
94-
*lb = '\0';
95-
// Set progdir global
96-
lua_pushstring(L, progdir);
97-
lua_setglobal(L, "_PROGDIR");
98-
99-
// Replace the relative path placeholder
100-
luaL_gsub(L, lua_tostring(L, -1), LUA_EXECDIR, progdir);
101-
lua_remove(L, -2);
102-
}
103-
}
104-
105-
/* }====================================================== */
10650

10751
#if defined(LUA_DL_DLOPEN)
10852
/*
@@ -115,12 +59,12 @@ static void setprogdir (lua_State *L) {
11559
*/
11660

11761
#include <dlfcn.h>
118-
#include <sys/stat.h>
11962

12063
static void ll_unloadlib (void *lib) {
12164
dlclose(lib);
12265
}
12366

67+
12468
static void *ll_load (lua_State *L, const char *path) {
12569
void *lib = dlopen(path, RTLD_NOW);
12670
if (lib == NULL) lua_pushstring(L, dlerror());
@@ -147,6 +91,24 @@ static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) {
14791

14892
#include <windows.h>
14993

94+
95+
#undef setprogdir
96+
97+
static void setprogdir (lua_State *L) {
98+
char buff[MAX_PATH + 1];
99+
char *lb;
100+
DWORD nsize = sizeof(buff)/sizeof(char);
101+
DWORD n = GetModuleFileNameA(NULL, buff, nsize);
102+
if (n == 0 || n == nsize || (lb = strrchr(buff, '\\')) == NULL)
103+
luaL_error(L, "unable to get ModuleFileName");
104+
else {
105+
*lb = '\0';
106+
luaL_gsub(L, lua_tostring(L, -1), LUA_EXECDIR, buff);
107+
lua_remove(L, -2); /* remove original string */
108+
}
109+
}
110+
111+
150112
static void pusherror (lua_State *L) {
151113
int error = GetLastError();
152114
char buffer[128];

0 commit comments

Comments
 (0)