summaryrefslogtreecommitdiff
path: root/Makefile
blob: d4c10559effc1d30ca56a29c0a9d7aa77aec9890 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
all: lua-5.1-try lua-5.2-try

install: lua-5.1-try-install lua-5.2-try-install

ifeq ($(strip $(shell which pkg-config > /dev/null || echo NONE)),NONE)
  $(error This makefile requires pkg-config)
endif

ifeq ($(strip $(shell which $(CC) > /dev/null || echo NONE)),NONE)
  $(info Luxio: Hmm, CC make variable contains nonsense.  Overriding to gcc.)
  CC := gcc
endif

# This isn't nice.  Find a nice way.
HOST_OS := $(shell uname -s)

ifneq ($(findstring $(HOST_OS),Linux NetBSD GNU/kFreeBSD),)
  # These operating systems require a seperate Real Time library
  EXTRA_LIBS := -lrt
else
  EXTRA_LIBS :=
endif

NAKED_LUA_VER := $(shell (pkg-config --exists lua && \
                          pkg-config --modversion lua || \
			  pkg-config --variable V lua) | cut -d. -f1-2)

# fine appropriate Lua interpreter
LUA_REPL := $(shell (which lua5.1 > /dev/null 2>&1 && echo lua5.1) || \
                    (which lua-5.1 > /dev/null 2>&1 && echo lua-5.1) || \
                    (which lua51  > /dev/null 2>&1 && echo lua51) || \
                    (lua -e"print(_VERSION)" | grep -q 5.1 && echo lua) || \
                    (which lua-5.2 > /dev/null 2>&1 && echo lua-5.2) || \
                    (which lua52 > /dev/null 2>&1 && echo lua52))

# find appropriate Lua libraries
LUA51_PKG := $(shell (pkg-config --exists lua5.1 && echo lua5.1) || \
                     (pkg-config --exists lua-5.1 && echo lua-5.1) || \
		     (pkg-config --exists lua51 && echo lua51) || \
                     (echo "NONE"))


LUA51_PKG := $(strip $(LUA51_PKG))

ifeq ($(LUA51_PKG),NONE)
  ifeq ($(NAKED_LUA_VER),5.1)
    LUA51_PKG := lua
  endif
endif

ifneq ($(LUA51_PKG),NONE)
  $(info Luxio: Lua 5.1 package name on this system is $(LUA51_PKG))
  LUA51_HAS_MODINST := $(shell test x`pkg-config --variable INSTALL_LMOD $(LUA51_PKG)` != x && echo YES)
  LUA51_INC := $(shell pkg-config --cflags $(LUA51_PKG))
  LUA51_LIB := $(shell pkg-config --libs $(LUA51_PKG))
  ifeq ($(LUA51_HAS_MODINST),)
    LOCAL := YES
  endif
  ifeq ($(LOCAL),)
    LUA51_LMOD_INST := $(shell pkg-config --variable=INSTALL_LMOD $(LUA51_PKG))
    LUA51_CMOD_INST := $(shell pkg-config --variable=INSTALL_CMOD $(LUA51_PKG))
  else
    LUA51_LMOD_INST := /usr/local/share/lua/5.1
    LUA51_CMOD_INST := /usr/local/lib/lua/5.1
  endif
  lua-5.1-try: lua-5.1
  lua-5.1-try-install: lua-5.1-install
else
lua-5.1-try:
	@echo Luxio: Lua 5.1 could not be found, so luxio was not built for it.
lua-5.1-try-install:
	@echo Luxio: Lua 5.1 could not be found, so luxio was not installed for it.
endif

LUA52_PKG := $(shell (pkg-config --exists lua5.2 && echo lua5.2) || \
                     (pkg-config --exists lua-5.2 && echo lua-5.2) || \
                     (pkg-config --exists lua52 && echo lua52) || \
                     (echo "NONE")) 

LUA52_PKG := $(strip $(LUA52_PKG))

ifeq ($(LUA52_PKG),NONE)
  ifeq ($(NAKED_LUA_VER),5.2)
    LUA52_PKG := lua
  endif
endif

ifneq ($(LUA52_PKG),NONE)
  $(info Luxio: Lua 5.2 package name on this system is $(LUA52_PKG))
  LUA52_HAS_MODINST := $(shell test x`pkg-config --variable INSTALL_LMOD $(LUA52_PKG)` != x && echo YES)
  LUA52_INC := $(shell pkg-config --cflags $(LUA52_PKG))
  LUA52_LIB := $(shell pkg-config --libs $(LUA52_PKG))
  ifeq ($(LUA52_HAS_MODINST),)
    LOCAL := YES
  endif
  ifeq ($(LOCAL),)
    LUA52_LMOD_INST := $(shell pkg-config --variable=INSTALL_LMOD $(LUA52_PKG))
    LUA52_CMOD_INST := $(shell pkg-config --variable=INSTALL_CMOD $(LUA52_PKG))
  else
    LUA52_LMOD_INST := /usr/local/share/lua/5.2
    LUA52_CMOD_INST := /usr/local/lib/lua/5.2
  endif
  lua-5.2-try: lua-5.2
  lua-5.2-try-install: lua-5.2-install
else
lua-5.2-try:
	@echo Luxio: Lua 5.2 could not be found, so luxio was not built for it.
lua-5.2-try-install:
	@echo Luxio: Lua 5.2 could not be found, so luxio was not installed for it.
endif


CFLAGS ?= -O2 -Wall
INSTALL := /usr/bin/install

LUALIBS := $(patsubst %,luxio/%.lua, \
	event simple subprocess)


clean:
	$(RM) *.so *.o luxio_constants.h luxio-5.1 luxio-5.2

luxio_constants.h: luxio_constants.h.in const-proc.lua
	$(LUA_REPL) ./const-proc.lua < $^ > $@

lua-5.1: luxio-5.1.so luxio-5.1
	ln -s -f luxio-5.1.so luxio.so

lua-5.1-install: lua-5.1
	$(INSTALL) -d $(DESTDIR)$(LUA51_CMOD_INST)
	$(INSTALL) -m 755 luxio-5.1.so $(DESTDIR)$(LUA51_CMOD_INST)/luxio.so
	$(INSTALL) -d $(DESTDIR)$(LUA51_LMOD_INST)/luxio
	$(INSTALL) -m 644 $(LUALIBS) $(DESTDIR)$(LUA51_LMOD_INST)/luxio/
	$(INSTALL) -d $(DESTDIR)$(PREFIX)/bin
	$(INSTALL) luxio-5.1 $(DESTDIR)$(PREFIX)/bin/luxio-5.1

luxio-5.1.so: luxio-5.1.o
	$(CC) $(LDFLAGS) -shared $(EXTRA_LIBS) -o luxio-5.1.so $^ $(LUA51_LIB)

luxio-5.1: lua-5.1.c luxio-5.1.o
	$(CC) -o luxio-5.1 $(CFLAGS) $(LUA51_INC) $(LDFLAGS) $(EXTRA_LIBS) lua-5.1.c luxio-5.1.o $(LUA51_LIB)

lua-5.2: luxio-5.2.so luxio-5.2
	ln -s -f luxio-5.2.so luxio.so

lua-5.2-install: lua-5.2
	$(INSTALL) -d $(DESTDIR)$(LUA52_CMOD_INST)
	$(INSTALL) -m 755 luxio-5.2.so $(DESTDIR)$(LUA52_CMOD_INST)/luxio.so
	$(INSTALL) -d $(DESTDIR)$(LUA52_LMOD_INST)/luxio
	$(INSTALL) -m 644 $(LUALIBS) $(DESTDIR)$(LUA52_LMOD_INST)/luxio/
	$(INSTALL) -d $(DESTDIR)$(PREFIX)/bin
	$(INSTALL) luxio-5.2 $(DESTDIR)$(PREFIX)/bin/luxio-5.2

luxio-5.2.so: luxio-5.2.o
	$(CC) $(LDFLAGS) -shared $(EXTRA_LIBS) -o luxio-5.2.so $^ $(LUA52_LIB)

luxio-5.2: lua-5.2.c luxio-5.2.o
	$(CC) -o luxio-5.2 $(CFLAGS) $(LDFLAGS) $(EXTRA_LIBS) $(LUA52_INC) lua-5.2.c luxio-5.2.o $(LUA52_LIB)

###

luxio-5.1.o: luxio.c luxio_constants.h 
	$(CC) $(CFLAGS) -fPIC $(LUA51_INC) -c $< -o $@

luxio-5.2.o: luxio.c luxio_constants.h
	$(CC) $(CFLAGS) -fPIC $(LUA52_INC) -c $< -o $@