aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Makefile257
-rw-r--r--src/compat.c17
-rw-r--r--src/compat.h11
-rw-r--r--src/core.c19
-rw-r--r--src/time.c89
5 files changed, 393 insertions, 0 deletions
diff --git a/src/Makefile b/src/Makefile
new file mode 100644
index 0000000..25f468d
--- /dev/null
+++ b/src/Makefile
@@ -0,0 +1,257 @@
1# luasystem
2#
3# Definitions in this section can be overriden on the command line or in the
4# environment.
5#
6
7# PLAT: linux macosx win32 mingw
8# platform to build for
9PLAT?=linux
10
11# LUA_VERSION: 5.1 5.2 5.3
12# lua version to build against
13LUA_VERSION?=5.1
14
15# MYCFLAGS: to be set by user if needed
16MYCFLAGS=
17
18# MYLDFLAGS: to be set by user if needed
19MYLDFLAGS=
20
21# where lua headers are found for macosx builds
22# LUAINC_macosx:
23# /opt/local/include
24LUAINC_macosx_base?=/opt/local/include
25LUAINC_macosx?=$(LUAINC_macosx_base)/lua/$(LUA_VERSION)
26# FIXME default should this default to fink or to macports?
27# What happens when more than one Lua version is installed?
28LUAPREFIX_macosx?=/opt/local
29CDIR_macosx?=lib/lua/$(LUA_VERSION)
30LDIR_macosx?=share/lua/$(LUA_VERSION)
31
32# LUAINC_linux:
33# /usr/include/lua$(LUA_VERSION)
34# /usr/local/include
35# /usr/local/include/lua$(LUA_VERSION)
36# where lua headers are found for linux builds
37LUAINC_linux_base?=/usr/include
38LUAINC_linux?=$(LUAINC_linux_base)/lua/$(LUA_VERSION)
39LUAPREFIX_linux?=/usr/local
40CDIR_linux?=lib/lua/$(LUA_VERSION)
41LDIR_linux?=share/lua/$(LUA_VERSION)
42
43# LUAINC_freebsd:
44# /usr/local/include/lua$(LUA_VERSION)
45# where lua headers are found for linux builds
46LUAINC_freebsd_base?=/usr/local/include/
47LUAINC_freebsd?=$(LUAINC_freebsd_base)/lua$(LUA_VERSION)
48LUAPREFIX_freebsd?=/usr/local/
49CDIR_freebsd?=lib/lua/$(LUA_VERSION)
50LDIR_freebsd?=share/lua/$(LUA_VERSION)
51
52# where lua headers are found for mingw builds
53# LUAINC_mingw:
54# /opt/local/include
55LUAINC_mingw_base?=/usr/include
56LUAINC_mingw?=$(LUAINC_mingw_base)/lua/$(LUA_VERSION)
57LUALIB_mingw_base?=/usr/bin
58LUALIB_mingw?=$(LUALIB_mingw_base)/lua/$(LUA_VERSION)/lua$(subst .,,$(LUA_VERSION)).dll
59LUAPREFIX_mingw?=/usr
60CDIR_mingw?=lua/$(LUA_VERSION)
61LDIR_mingw?=lua/$(LUA_VERSION)/lua
62
63
64# LUAINC_win32:
65# LUALIB_win32:
66# where lua headers and libraries are found for win32 builds
67LUAPREFIX_win32?=
68LUAINC_win32?=$(LUAPREFIX_win32)/include/lua/$(LUA_VERSION)
69PLATFORM_win32?=Release
70CDIR_win32?=bin/lua/$(LUA_VERSION)/$(PLATFORM_win32)
71LDIR_win32?=bin/lua/$(LUA_VERSION)/$(PLATFORM_win32)/lua
72LUALIB_win32?=$(LUAPREFIX_win32)/lib/lua/$(LUA_VERSION)/$(PLATFORM_win32)
73LUALIBNAME_win32?=lua$(subst .,,$(LUA_VERSION)).lib
74
75
76# prefix: /usr/local /usr /opt/local /sw
77# the top of the default install tree
78prefix?=$(LUAPREFIX_$(PLAT))
79
80CDIR?=$(CDIR_$(PLAT))
81LDIR?=$(LDIR_$(PLAT))
82
83# DESTDIR: (no default)
84# used by package managers to install into a temporary destination
85DESTDIR=
86
87#------
88# Definitions below can be overridden on the make command line, but
89# shouldn't have to be.
90
91
92#------
93# Install directories
94#
95
96INSTALL_DIR=install -d
97INSTALL_DATA=install -m644
98INSTALL_EXEC=install
99INSTALL_TOP=$(DESTDIR)$(prefix)
100
101INSTALL_TOP_LDIR=$(INSTALL_TOP)/$(LDIR)
102INSTALL_TOP_CDIR=$(INSTALL_TOP)/$(CDIR)
103
104INSTALL_LDIR=$(INSTALL_TOP_LDIR)/system
105INSTALL_CDIR=$(INSTALL_TOP_CDIR)/system
106
107#------
108# Supported platforms
109#
110PLATS= macosx linux win32 mingw
111
112#------
113# Compiler and linker settings
114# for Mac OS X
115SO_macosx=so
116O_macosx=o
117CC_macosx=gcc
118DEF_macosx=
119CFLAGS_macosx= -I$(LUAINC) $(DEF) -Wall -O2 -fno-common \
120 -fvisibility=hidden
121LDFLAGS_macosx= -bundle -undefined dynamic_lookup -o
122LD_macosx= export MACOSX_DEPLOYMENT_TARGET="10.3"; gcc
123
124#------
125# Compiler and linker settings
126# for Linux
127SO_linux=so
128O_linux=o
129CC_linux=gcc
130DEF_linux=
131CFLAGS_linux= -I$(LUAINC) $(DEF) -Wall -Wshadow -Wextra \
132 -Wimplicit -O2 -ggdb3 -fpic -fvisibility=hidden
133LDFLAGS_linux=-O -shared -fpic -o
134LD_linux=gcc
135
136#------
137# Compiler and linker settings
138# for FreeBSD
139SO_freebsd=so
140O_freebsd=o
141CC_freebsd=gcc
142DEF_freebsd=
143CFLAGS_freebsd= -I$(LUAINC) $(DEF) -Wall -Wshadow -Wextra \
144 -Wimplicit -O2 -ggdb3 -fpic -fvisibility=hidden
145LDFLAGS_freebsd=-O -shared -fpic -o
146LD_freebsd=gcc
147
148#------
149# Compiler and linker settings
150# for MingW
151SO_mingw=dll
152O_mingw=o
153CC_mingw=gcc
154DEF_mingw=-DWINVER=0x0501
155CFLAGS_mingw= -I$(LUAINC) $(DEF) -Wall -O2 -fno-common \
156 -fvisibility=hidden
157LDFLAGS_mingw= $(LUALIB) -shared -Wl,-s -o
158LD_mingw=gcc
159
160
161#------
162# Compiler and linker settings
163# for Win32
164SO_win32=dll
165O_win32=obj
166CC_win32=cl
167DEF_win32= //D "WIN32" //D "NDEBUG" //D "_WINDOWS" //D "_USRDLL" \
168 //D "_CRT_SECURE_NO_WARNINGS" //D "_WINDLL"
169CFLAGS_win32=//I "$(LUAINC)" $(DEF) //O2 //Ot //MD //W3 //nologo
170LDFLAGS_win32= //nologo //link //NOLOGO //DLL //INCREMENTAL:NO \
171 //MANIFEST //MANIFESTFILE:"intermediate.manifest" \
172 //MANIFESTUAC:"level='asInvoker' uiAccess='false'" \
173 //SUBSYSTEM:WINDOWS //OPT:REF //OPT:ICF //DYNAMICBASE:NO \
174 //MACHINE:X86 /LIBPATH:"$(shell cmd //c echo $(LUALIB))" \
175 $(LUALIBNAME_win32) //OUT:
176LD_win32=cl
177
178.SUFFIXES: .obj
179
180.c.obj:
181 $(CC) $(CFLAGS) //Fo"$@" //c $<
182
183#------
184# Output file names
185#
186SO=$(SO_$(PLAT))
187O=$(O_$(PLAT))
188SOLIB=core.$(SO)
189
190#------
191# Settings selected for platform
192#
193CC=$(CC_$(PLAT))
194DEF=$(DEF_$(PLAT))
195CFLAGS=$(MYCFLAGS) $(CFLAGS_$(PLAT))
196LDFLAGS=$(MYLDFLAGS) $(LDFLAGS_$(PLAT))
197LD=$(LD_$(PLAT))
198LUAINC= $(LUAINC_$(PLAT))
199LUALIB= $(LUALIB_$(PLAT))
200
201#------
202# Objects
203#
204OBJS=core.$(O) compat.$(O) time.$(O)
205
206#------
207# Targets
208#
209default: $(PLAT)
210
211freebsd:
212 $(MAKE) all PLAT=freebsd
213
214macosx:
215 $(MAKE) all PLAT=macosx
216
217win32:
218 $(MAKE) all PLAT=win32
219
220linux:
221 $(MAKE) all PLAT=linux
222
223mingw:
224 $(MAKE) all PLAT=mingw
225
226none:
227 @echo "Please run"
228 @echo " make PLATFORM"
229 @echo "where PLATFORM is one of these:"
230 @echo " $(PLATS)"
231
232all: $(SOLIB)
233
234$(SOLIB): $(OBJS)
235 $(LD) $(OBJS) $(LDFLAGS)$@
236
237install: all
238 $(INSTALL_DIR) $(INSTALL_TOP_LDIR)
239 $(INSTALL_DIR) $(INSTALL_CDIR)
240 $(INSTALL_EXEC) $(SOLIB) $(INSTALL_CDIR)/$(SOLIB)
241
242local:
243 $(MAKE) install INSTALL_TOP_CDIR=.. INSTALL_TOP_LDIR=..
244
245clean:
246 rm -f $(SOLIB) $(OBJS) ../system/core.so
247
248print:
249 @echo PLAT=$(PLAT)
250 @echo LUA_VERSION=$(LUA_VERSION)
251 @echo prefix=$(prefix)
252 @echo LUAINC_$(PLAT)=$(LUAINC_$(PLAT))
253 @echo LUALIB_$(PLAT)=$(LUALIB_$(PLAT))
254 @echo INSTALL_TOP_CDIR=$(INSTALL_TOP_CDIR)
255 @echo INSTALL_TOP_LDIR=$(INSTALL_TOP_LDIR)
256
257.PHONY: all $(PLATS) default clean none
diff --git a/src/compat.c b/src/compat.c
new file mode 100644
index 0000000..d763c56
--- /dev/null
+++ b/src/compat.c
@@ -0,0 +1,17 @@
1#include <lua.h>
2#include <lauxlib.h>
3
4#if LUA_VERSION_NUM == 501
5void luaL_setfuncs(lua_State *L, const luaL_Reg *l, int nup) {
6 luaL_checkstack(L, nup+1, "too many upvalues");
7 for (; l->name != NULL; l++) { /* fill the table with given functions */
8 int i;
9 lua_pushstring(L, l->name);
10 for (i = 0; i < nup; i++) /* copy upvalues to the top */
11 lua_pushvalue(L, -(nup+1));
12 lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */
13 lua_settable(L, -(nup + 3));
14 }
15 lua_pop(L, nup); /* remove upvalues */
16}
17#endif
diff --git a/src/compat.h b/src/compat.h
new file mode 100644
index 0000000..f523fd9
--- /dev/null
+++ b/src/compat.h
@@ -0,0 +1,11 @@
1#ifndef COMPAT_H
2#define COMPAT_H
3
4#include <lua.h>
5#include <lauxlib.h>
6
7#if LUA_VERSION_NUM == 501
8void luaL_setfuncs(lua_State *L, const luaL_Reg *l, int nup);
9#endif
10
11#endif
diff --git a/src/core.c b/src/core.c
new file mode 100644
index 0000000..d2acb95
--- /dev/null
+++ b/src/core.c
@@ -0,0 +1,19 @@
1#include <lua.h>
2#include <lauxlib.h>
3
4#ifdef _WIN32
5#define LUAEXPORT __declspec(dllexport)
6#else
7#define LUAEXPORT __attribute__((visibility("default")))
8#endif
9
10void time_open(lua_State *L);
11
12/*-------------------------------------------------------------------------
13 * Initializes all library modules.
14 *-------------------------------------------------------------------------*/
15LUAEXPORT int luaopen_system_core(lua_State *L) {
16 lua_newtable(L);
17 time_open(L);
18 return 1;
19}
diff --git a/src/time.c b/src/time.c
new file mode 100644
index 0000000..3f61001
--- /dev/null
+++ b/src/time.c
@@ -0,0 +1,89 @@
1#include <lua.h>
2#include <lauxlib.h>
3
4#ifdef _WIN32
5#include <windows.h>
6#else
7#include <time.h>
8#include <sys/time.h>
9#endif
10
11#include "compat.h"
12
13/*-------------------------------------------------------------------------
14 * Gets time in s, relative to January 1, 1970 (UTC)
15 * Returns
16 * time in s.
17 *-------------------------------------------------------------------------*/
18#ifdef _WIN32
19static double time_gettime(void) {
20 FILETIME ft;
21 double t;
22 GetSystemTimeAsFileTime(&ft);
23 /* Windows file time (time since January 1, 1601 (UTC)) */
24 t = ft.dwLowDateTime/1.0e7 + ft.dwHighDateTime*(4294967296.0/1.0e7);
25 /* convert to Unix Epoch time (time since January 1, 1970 (UTC)) */
26 return (t - 11644473600.0);
27}
28#else
29static double time_gettime(void) {
30 struct timeval v;
31 gettimeofday(&v, (struct timezone *) NULL);
32 /* Unix Epoch time (time since January 1, 1970 (UTC)) */
33 return v.tv_sec + v.tv_usec/1.0e6;
34}
35#endif
36
37/*-------------------------------------------------------------------------
38 * Returns the time the system has been up, in secconds.
39 *-------------------------------------------------------------------------*/
40static int time_lua_gettime(lua_State *L)
41{
42 lua_pushnumber(L, time_gettime());
43 return 1;
44}
45
46/*-------------------------------------------------------------------------
47 * Sleep for n seconds.
48 *-------------------------------------------------------------------------*/
49#ifdef _WIN32
50static int time_lua_sleep(lua_State *L)
51{
52 double n = luaL_checknumber(L, 1);
53 if (n < 0.0) n = 0.0;
54 if (n < DBL_MAX/1000.0) n *= 1000.0;
55 if (n > INT_MAX) n = INT_MAX;
56 Sleep((int)n);
57 return 0;
58}
59#else
60static int time_lua_sleep(lua_State *L)
61{
62 double n = luaL_checknumber(L, 1);
63 struct timespec t, r;
64 if (n < 0.0) n = 0.0;
65 if (n > INT_MAX) n = INT_MAX;
66 t.tv_sec = (int) n;
67 n -= t.tv_sec;
68 t.tv_nsec = (int) (n * 1000000000);
69 if (t.tv_nsec >= 1000000000) t.tv_nsec = 999999999;
70 while (nanosleep(&t, &r) != 0) {
71 t.tv_sec = r.tv_sec;
72 t.tv_nsec = r.tv_nsec;
73 }
74 return 0;
75}
76#endif
77
78static luaL_Reg func[] = {
79 { "gettime", time_lua_gettime },
80 { "sleep", time_lua_sleep },
81 { NULL, NULL }
82};
83
84/*-------------------------------------------------------------------------
85 * Initializes module
86 *-------------------------------------------------------------------------*/
87void time_open(lua_State *L) {
88 luaL_setfuncs(L, func, 0);
89}