File: dh-lua.Makefile.single

package info (click to toggle)
dh-lua 15
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 172 kB
  • sloc: ansic: 98; perl: 81; makefile: 46; sh: 46
file content (453 lines) | stat: -rw-r--r-- 13,714 bytes parent folder | download
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
# Copyright: © 2012 Enrico Tassi <gareuselesinge@debian.org>
# License: MIT/X
# vim: foldmethod=marker:ft=make

# override for more verbose output
ifeq "$(DH_VERBOSE)-$(findstring verbose,$(DH_LUA_OPTS))" "-"
H=@
else
H=
endif

include $(CONFIGURATION_FILE)

### {{{gnu make haks
empty:=
space:=$(empty) $(empty)
### }}}

### {{{ a bunch of definitions (standard at least for debian)
### you should not need to change this

# LUA_VERSION
ifeq "$(LUA_VERSION)" ""
LUA_VERSION:=$(if $(shell echo $(CONFIGURATION_FILE) | grep ^debian/lua5\.2),5.2)
endif
ifeq "$(LUA_VERSION)" ""
LUA_VERSION:=$(if $(shell echo $(CONFIGURATION_FILE) | grep ^debian/lua5\.1),5.1)
endif

# UID for the LUA_VERSION
UID=$(shell pwd)/$(LUA_VERSION)-$(PKG_NAME)

# misc
DH_LUA_SUPPORT_FILES=/usr/share/dh-lua/
LUA=lua$(LUA_VERSION)
C_TEST_FILE=$(DH_LUA_SUPPORT_FILES)test/$(LUA_VERSION)/app.c
PREFIX=$(DESTDIR)/usr/
LBTL=libtool --silent --tag=CC
ifeq "$(LUA_SOURCES_MANGLER)" ""
	LUA_SOURCES_MANGLER:=cat
endif

# .pc
PKG_CONF_NAME=$(LUA)-$(PKG_NAME_DASH).pc
DEB_PKGCONFIG_TEMPL=$(DH_LUA_SUPPORT_FILES)/template/pkg-config.pc.in
PKGCONF=pkg-config $(LUA) --define-variable=prefix=$(PREFIX)
ifeq "$(PKG_VERSION)" ""
PKG_VERSION=$(shell dpkg-parsechangelog|grep ^Ver|cut -d ' ' -f 2|cut -d '-' -f 1)
endif

# CFLAGS
LUA_CFLAGS := $(shell $(PKGCONF) --cflags)
CFLAGS := $(shell dpkg-buildflags --get CFLAGS)
CFLAGS += $(shell dpkg-buildflags --get CPPFLAGS)
CFLAGS += $(LUA_CFLAGS) $(CLIB_CFLAGS) -Wall -Wextra
STD_LDFLAGS := $(shell dpkg-buildflags --get LDFLAGS)
LDFLAGS_STATIC = $(STD_LDFLAGS) $(CLIB_LDFLAGS_STATIC)
LDFLAGS        = $(STD_LDFLAGS) $(CLIB_LDFLAGS)

# liblua5.1-foo.so.0.0.0
VERSION_INFO ?= 0:0:0
LIBNAME=lib$(LUA)-$(PKG_NAME_DASH)
REALSO=$(LIBNAME).so.$(subst :,.,$(VERSION_INFO))

# /usr/lib/liblua5.1.la
LUA_LBTL_S := $(shell $(PKGCONF) --libs --static)
LUA_LBTL_D := $(shell $(PKGCONF) --libs)

# /usr/lib/, /usr/lib/lua/5.1, /usr/share/lua/5.1/
LUA_CPATH:=$(shell $(PKGCONF) --variable=INSTALL_CMOD)
LUA_SPATH:=$(shell $(PKGCONF) --variable=INSTALL_LMOD)
DEB_HOST_MULTIARCH:=$(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
LUA_LPATH=$(PREFIX)/lib/$(DEB_HOST_MULTIARCH)

# /usr/include/lua5.1
LUA_HPATH:=$(shell $(PKGCONF) --variable=includedir)/$(LUA)

# debian packages name and derivates
PKG_NAME_DASH=$(subst .,-,$(PKG_NAME))
DEB_PKG_LIB=lua-$(PKG_NAME_DASH)
DEB_PKG_DEV=$(DEB_PKG_LIB)-dev

# .h
ifeq "$(LUA_HEADER)" ""
  LUA_HEADER=$(UID)/lua-$(PKG_NAME_DASH).h
endif
DEB_HEADER_TEMPL=$(DH_LUA_SUPPORT_FILES)template/header.h.in

# names for require and for luaopen
ifeq "$(LUA_MODNAME)" ""
  LUA_MODNAME=$(PKG_NAME)
endif
ifeq "$(LUA_MODNAME_CPART)" ""
  LUA_MODNAME_CPART=$(LUA_MODNAME)
endif

# luasql.mysql -> /usr/lib/lua/5.1/sql/mysql
LUA_MODNAME_PATH=$(subst .,/,$(LUA_MODNAME_CPART))

# luaopen_luasql_mysql(lua_State* L)
LUA_MODNAME_CPART_UNDERSCORE=$(subst .,_,$(LUA_MODNAME_CPART))

# depending on the nesting, a '../' for every level
LUA_MODNAME_DOTS=$(shell echo $(LUA_MODNAME_CPART) | sed 's/[^\.]//g' | sed 's/\./..\//g')

# .install
DEB_INST_LIB=debian/$(DEB_PKG_LIB).install
DEB_INST_LIB_TEMPL=$(DH_LUA_SUPPORT_FILES)template/lib.install.in
DEB_INST_DEV=debian/$(DEB_PKG_DEV).install
DEB_INST_DEV_TEMPL=$(DH_LUA_SUPPORT_FILES)template/dev.install.in

# where to find stuff
LUA_INIT=' package.path="$(UID)/?.lua;$(UID)/?/init.lua;"..package.path; package.cpath="$(UID)/?.so;"..package.cpath; '

# to help crosscompiling
DEB_BUILD_ARCH:=$(shell dpkg-architecture -qDEB_BUILD_ARCH)
DEB_HOST_ARCH:=$(shell dpkg-architecture -qDEB_HOST_ARCH)

# lua5.2 specific
ifeq "$(LUA_VERSION)" "5.2"
	LUA_TEST := $(subst /usr/bin/shake,,$(LUA_TEST))
endif

# test
LUA_TEST_KIND:=real
ifeq "$(LUA_TEST)" ""
	LUA_TEST_KIND:=fake
endif


### }}}

.PHONY: build clean test install configure sanity

# ---------------------- Here starts the Makefile ------------------------------

# {{{ entrypoints 
ifeq "$(CLIB_OBJS)" ""
build: sanity
test: sanity test-lua-dynamic 
install: sanity installcommon
else
build: sanity $(UID)/$(LIBNAME).la 
test: sanity test-lua-dynamic test-app-dynamic test-app-static
install: sanity installcommon installso
endif
clean: sanity
	$(H)$(RM) -rf $(UID)/
	$(H)$(call empty_trash)
	# fix for leftovers of dh-lua < 14
	$(H)$(RM) -f debian/backup

sanity:
	$(H)if [ -z "$(LUA_VERSION)" ]; then\
		echo;\
		echo ERROR: LUA_VERSION is not set;\
		echo;\
		exit 1;\
	fi

# }}}

# {{{ helper macros
define trash
	echo $(1) >> debian/trash
endef

define empty_trash
	if [ -e debian/trash ]; then \
		$(RM) -f `cat debian/trash`; fi
endef

define subst_vars
	sed \
	 -e 's|@@PKG_VERSION@@|$(PKG_VERSION)|g' \
	 -e 's|@@PKG_NAME@@|$(PKG_NAME_DASH)|g' \
	 -e 's|@@PKG_LUA@@|$(LUA)|g' \
	 -e 's|@@PKG_LIBS_PRIVATE@@|$(PKG_LIBS_PRIVATE)|g' \
	 -e 's|@@PKG_URL@@|$(PKG_URL)|g' \
	 -e 's|@@PKG_REQUIRES@@|$(PKG_REQUIRES)|g' \
	 -e 's|@@PKG_CONFLICTS@@|$(PKG_CONFLICTS)|g' \
	 -e 's|@@LIBNAME@@|$(LIBNAME)|g' \
	 -e 's|@@DEB_HOST_MULTIARCH@@|$(DEB_HOST_MULTIARCH)|g' \
	 -e 's|@@LUA@@|$(LUA)|g' \
	 -e 's|@@LUA_VERSION@@|$(LUA_VERSION)|g' \
	 -e 's|@@LUA_MODNAME_CPART_UNDERSCORE@@|$(LUA_MODNAME_CPART_UNDERSCORE)|g' \
	 -e 's|@@LUA_MODNAME_CPART@@|$(LUA_MODNAME_CPART)|g' \
	 -e 's|@@LUA_MODNAME_PATH@@|$(LUA_MODNAME_PATH)|g' \
	 -e 's|@@LUA_HEADER@@|$(notdir $(LUA_HEADER))|g' \
	 -e 's|@@LUA_SOURCES@@|$(LUA_SOURCES)|g' \
	 -e 's|@@LUA_SOURCES_MANGLER@@|$(LUA_SOURCES_MANGLER)|g' \
	 -e 's|@@CLIB_OBJS@@|$(CLIB_OBJS)|g' \
	 -e 's|@@[A-Z_]*@@||g' \
	 -e '/^test /e'
endef

define merge_with
	(while read line; do\
		[ -e $(1) ] || touch $(1);\
		if grep -q -F "$$line" $(1); then\
			echo "Skipping already existing line: $$line";\
		else\
			echo "Adding new line: $$line";\
			echo "$$line" >> $(1);\
		fi;\
	done)
endef

define assert
	if [ "$(1)" != "$(2)" ]; then\
		echo;\
		echo "ERROR:" $(3);\
		echo;\
		exit 1;\
	fi
endef

define all_packages_with_luaVersions
	grep-dctrl -s Package -F XB-Lua-Versions lua:Versions debian/control | \
			cut -d : -f 2
endef

define all_packages_with_luaProvides
	grep-dctrl -s Package -F Provides lua:Provides debian/control | \
			cut -d : -f 2
endef

define run_single_test
	$(if $(findstring @@LUA@@,$(1)),($(subst @@LUA@@,$(2),$(1))),$(2) $(1))
endef

define run_multiple_tests
	$(foreach test,$(subst ;,$(space),$(subst $(space),_space_,$(1))),\
		echo "Test: $(subst _space_,$(space),$(test))" && \
		cd $(shell pwd) && \
		$(call run_single_test,$(subst _space_,$(space),$(test)),\
			HOME=$(shell pwd)/debian LUA_INIT=$(LUA_INIT) SEGFAULT_SIGNALS=all \
			PATH="$(UID)/:$$PATH" $(2)) &&) true
endef

# }}}

# ---------------------------- Testing -----------------------------------------

# {{{ test the module, from lua and from a dynamically/statically linked C app
test-lua-dynamic-fake:
	@echo "********************** lua dynamic ($(LUA_VERSION)) *********"
	@echo SKIPPED!
	@echo "**************************************************"

test-app-static-fake: $(UID)/app-static 
	@echo "*********************** app static ($(LUA_VERSION)) *********"
	@echo SKIPPED!
	@echo "**************************************************"

test-app-dynamic-fake:  $(UID)/app-dynamic
	@echo "********************** app dynamic ($(LUA_VERSION)) *********"
	@echo SKIPPED!
	@echo "**************************************************"

test-lua-dynamic-real:
	@echo "********************** lua dynamic ($(LUA_VERSION)) *********"
	$(H)$(call run_multiple_tests,$(LUA_TEST),catchsegv $(LUA) -l$(LUA_MODNAME))
	@echo "**************************************************"

test-app-static-real: $(UID)/app-static
	@echo "*********************** app static ($(LUA_VERSION)) *********"
	$(H)$(call run_multiple_tests,$(LUA_TEST),catchsegv $(UID)/app-static)
	@echo "**************************************************"

test-app-dynamic-real: $(UID)/app-dynamic
	@echo "********************** app dynamic ($(LUA_VERSION)) *********"
	$(H)$(call run_multiple_tests,$(LUA_TEST),\
			$(LBTL) --mode=execute -dlopen $(UID)/$(LIBNAME).la \
			catchsegv $(UID)/app-dynamic)
	@echo "**************************************************"

ifneq "$(DEB_HOST_ARCH)" "$(DEB_BUILD_ARCH)"
ifeq "$(CLIB_OBJS)" ""
test-lua-dynamic: test-lua-dynamic-$(LUA_TEST_KIND)
else
test-lua-dynamic: test-lua-dynamic-fake
endif
test-app-static: test-app-static-fake
test-app-dynamic: test-app-dynamic-fake
else
ifneq "$(findstring skip-test-lua-dynamic,$(DH_LUA_OPTS))" ""
test-lua-dynamic: test-lua-dynamic-fake
else
test-lua-dynamic: test-lua-dynamic-$(LUA_TEST_KIND)
endif
ifneq "$(findstring skip-test-app-static,$(DH_LUA_OPTS))" ""
test-app-static: test-app-static-fake
else
test-app-static: test-app-static-$(LUA_TEST_KIND)
endif
ifneq "$(findstring skip-test-app-dynamic,$(DH_LUA_OPTS))" ""
test-app-dynamic: test-app-dynamic-fake
else
test-app-dynamic: test-app-dynamic-$(LUA_TEST_KIND)
endif
endif
# }}}

# ------------------------- Installation ---------------------------------------
  
# {{{ install static and dynamic libraries for module to global location
installso: $(UID)/$(LIBNAME).la $(UID)/$(PKG_CONF_NAME) $(LUA_HEADER)
	# .so
	$(H)echo Installing $(LIBNAME)
	$(H)mkdir -p $(LUA_CPATH)
	$(H)mkdir -p $(LUA_LPATH)
	$(H)$(LBTL) --mode=install install \
		$(UID)/$(LIBNAME).la \
		$(shell pwd)/$(LUA_LPATH)/$(LIBNAME).la
	$(LBTL) --finish $(LUA_LPATH)
	$(H)cd $(LUA_CPATH); \
		echo Creating symlink $(LUA_MODNAME_PATH).so;\
		mkdir -p $(dir $(LUA_MODNAME_PATH));\
		ln -s $(LUA_MODNAME_DOTS)../../$(REALSO) $(LUA_MODNAME_PATH).so
	# .pc
	$(H)echo Installing $(PKG_CONF_NAME)
	$(H)mkdir -p $(LUA_LPATH)/pkgconfig/
	$(H)$(LBTL) --mode=install install -m 0644 \
		$(UID)/$(PKG_CONF_NAME) \
		$(shell pwd)/$(LUA_LPATH)/pkgconfig/
	# .h
	$(H)if [ ! -z "$(CLIB_OBJS)" ]; then\
		echo Installing $(LUA_HEADER);\
		mkdir -p $(LUA_HPATH);\
		$(LBTL) --mode=install install -m 0644 \
			$(LUA_HEADER) $(shell pwd)/$(LUA_HPATH)/;\
	fi

installcommon:
	# .lua
	$(H)mkdir -p $(LUA_SPATH)
	$(H)if [ ! -z "$(LUA_SOURCES)" ]; then\
		echo Installing $(LUA_SOURCES); \
		for X in $(LUA_SOURCES); do\
		  mkdir -p `dirname $(LUA_SPATH)/$$(echo $$X|$(LUA_SOURCES_MANGLER))`;\
	   	  cp $$X $(LUA_SPATH)/`echo $$X|$(LUA_SOURCES_MANGLER)`;\
		done;\
	fi
	# debian/substvars
	$(H)for P in $(shell $(call all_packages_with_luaVersions)); do \
		echo Filling in debian/$$P.substvars; \
		echo lua:Versions=`cat debian/lua_versions` | \
			$(call merge_with, debian/$$P.substvars);\
	done
	$(H)for P in $(shell $(call all_packages_with_luaProvides)); do \
		ACC=""; \
		for V in `cat debian/lua_versions`; do \
			PV=`echo $$P | sed "s/^lua-/lua$$V-/"`; \
			ACC="$$PV, $$ACC"; \
		done; \
		echo Filling in debian/$$P.substvars; \
		echo lua:Provides=$$ACC | $(call merge_with, debian/$$P.substvars);\
	done

# }}}

# ------------------------- C compilation --------------------------------------

# {{{ compilation rules: .lo .la app
$(UID)/%.lo: %.c
	$(LBTL) --mode=compile $(CC) -c $(CFLAGS) -o $@ $< 

$(UID)/$(LIBNAME).la $(UID)/$(LUA_MODNAME_PATH).so: $(addprefix $(UID)/,$(CLIB_OBJS))
	$(LBTL) --mode=link $(CC) \
	    -rpath $(LUA_LPATH) -version-info $(VERSION_INFO) -Wl,--no-add-needed \
		-o $(UID)/$(LIBNAME).la \
	    $(patsubst %,$(UID)/%,$(CLIB_OBJS)) \
		$(LDFLAGS)
	$(H)mkdir -p $(UID)/$(dir $(LUA_MODNAME_PATH))
	$(H)ln -sf $(LUA_MODNAME_DOTS)./.libs/$(REALSO) \
		$(UID)/$(LUA_MODNAME_PATH).so
	ldd $(UID)/$(LUA_MODNAME_PATH).so

$(UID)/app-static: $(UID)/$(LIBNAME).la
	$(LBTL) --mode=link $(CC) $(CFLAGS) -Wl,--no-add-needed \
		-static -o $@ -I . -I $(UID)/ \
		$(C_TEST_FILE) $(UID)/$(LIBNAME).la \
		$(LDFLAGS_STATIC) $(LUA_LBTL_S)
	ldd $(UID)/app-static

$(UID)/app-dynamic: $(UID)/$(LIBNAME).la
	$(LBTL) --mode=link $(CC) $(CFLAGS) -Wl,--no-add-needed \
		-o $@ -I . -I $(UID)/ \
		$(C_TEST_FILE) $(UID)/$(LIBNAME).la \
		$(LDFLAGS) $(LUA_LBTL_D)
	$(LBTL) --mode=execute -dlopen $(UID)/$(LIBNAME).la \
			ldd $(UID)/app-dynamic

# }}}

# ------------------------- configuration --------------------------------------

# {{{ pkgconfig, .install and (evental) .h generation
$(UID):
	$(H)mkdir -p $(UID)/

$(LUA_HEADER):
	$(H)if [ ! -e $@ ]; then \
		cat $(DEB_HEADER_TEMPL) | $(call subst_vars) > $@; \
	fi

$(UID)/$(PKG_CONF_NAME): $(UID)
	$(H)cat $(DEB_PKGCONFIG_TEMPL) | $(call subst_vars) > $@

$(UID)/app.c.conf: $(UID)
	$(H)cat $(C_TEST_FILE).conf.in | $(call subst_vars) > $@ 

configure: sanity $(UID) $(LUA_HEADER) $(UID)/$(PKG_CONF_NAME) $(UID)/app.c.conf
	# .install
	$(H)$(call trash, $(DEB_INST_LIB))
	$(H)if [ -e $(DEB_INST_LIB).in ]; then\
		echo "Filling in $(DEB_INST_LIB) using $(DEB_INST_LIB).in";\
		cat $(DEB_INST_LIB).in | $(call subst_vars) | \
			$(call merge_with, $(DEB_INST_LIB));\
	else\
		echo "Filling in $(DEB_INST_LIB) using $(DEB_INST_LIB_TEMPL)";\
		cat $(DEB_INST_LIB_TEMPL) | $(call subst_vars) | \
			$(call merge_with, $(DEB_INST_LIB));\
	fi
	$(H)$(call trash, $(DEB_INST_DEV))
	$(H)if [ -e $(DEB_INST_DEV).in ]; then\
		echo "Filling in $(DEB_INST_DEV) using $(DEB_INST_DEV).in";\
		cat $(DEB_INST_DEV).in | $(call subst_vars) | \
			$(call merge_with, $(DEB_INST_DEV));\
	else\
		echo "Filling in $(DEB_INST_DEV) using $(DEB_INST_DEV_TEMPL)";\
		cat $(DEB_INST_DEV_TEMPL) | $(call subst_vars) | \
			$(call merge_with, $(DEB_INST_DEV));\
	fi
	# lua_versions
	$(H)$(call trash, debian/lua_versions)
	$(H)echo Filling in debian/lua_versions
	$(H)echo $(LUA_VERSION) | $(call merge_with, debian/lua_versions)
	# tests
	$(H)ln -sf `which $(LUA)` $(UID)/lua
	$(H)if [ ! -z "$(LUA_SOURCES)" ]; then\
		echo Copying $(LUA_SOURCES) in $(UID) for test; \
		for X in $(LUA_SOURCES); do\
			mkdir -p `dirname $(UID)/$$(echo $$X|$(LUA_SOURCES_MANGLER))`;\
	   		cp $$X $(UID)/`echo $$X|$(LUA_SOURCES_MANGLER)`;\
		done;\
	fi

# }}}

# vim:set ts=4:
# eof