1# non-recursive prologue
2sp := $(sp).x
3dirstack_$(sp) := $(d)
4d := $(abspath $(lastword $(MAKEFILE_LIST))/..)
5
6ifeq ($(origin GUARD_$(d)), undefined)
7GUARD_$(d) := 1
8
9
10all: # default target
11
12#
13# G N U  M A K E  F U N C T I O N S
14#
15KNOWN_APIS = 5.1 5.2 5.3 5.4
16
17# template for invoking luapath script
18LUAPATH := $(d)/mk/luapath
19LUAPATH_FN = $(shell env CC='$(subst ',\\',$(CC))' CPPFLAGS='$(subst ',\\',$(CPPFLAGS))' LDFLAGS='$(subst ',\\',$(LDFLAGS))' $(LUAPATH) -krxm3 -I '$(subst ',\\',$(DESTDIR)$(includedir))' -I/usr/include -I/usr/local/include -P '$(subst ',\\',$(DESTDIR)$(bindir))' -P '$(subst ',\\',$(bindir))' -L '$(subst ',\\',$(DESTDIR)$(libdir))' -L '$(subst ',\\',$(libdir))' -v$(1) $(2))
20
21# check whether luapath can locate Lua $(1) headers
22HAVE_API_FN = $(and $(filter $(1),$(call LUAPATH_FN,$(1),version)),$(1)$(info enabling Lua $(1)))
23
24# check whether $(1) in LUA_APIS or $(LUA$(1:.=)_CPPFLAGS) is non-empty
25WITH_API_FN = $$(and $$(or $$(filter $(1),$$(LUA_APIS)),$$(LUA$(subst .,,$(1))_CPPFLAGS)),$(1))
26
27#
28# E N V I R O N M E N T  C O N F I G U R A T I O N
29#
30-include $(d)/.config
31
32prefix ?= /usr/local
33includedir ?= $(prefix)/include
34libdir ?= $(prefix)/lib
35datadir ?= $(prefix)/share
36bindir ?= $(prefix)/bin
37lua51cpath ?= $(libdir)/lua/5.1
38lua51path ?= $(datadir)/lua/5.1
39lua52cpath ?= $(libdir)/lua/5.2
40lua52path ?= $(datadir)/lua/5.2
41lua53cpath ?= $(libdir)/lua/5.3
42lua53path ?= $(datadir)/lua/5.3
43lua54cpath ?= $(libdir)/lua/5.4
44lua54path ?= $(datadir)/lua/5.4
45
46
47AR ?= ar
48RANLIB ?= ranlib
49M4 ?= m4
50MV ?= mv
51RM ?= rm
52CP ?= cp
53RMDIR ?= rmdir
54MKDIR ?= mkdir
55CHMOD ?= chmod
56INSTALL ?= install
57INSTALL_DATA ?= $(INSTALL) -m 644
58TOUCH ?= touch
59TEE ?= tee
60TEE_A ?= $(TEE) -a
61
62# see Lua Autodetection, below
63
64.PHONY: $(d)/config
65
66PRINT_$(d) = printf "%s = %s\n" '$(1)' '$(subst ',\\',$(2))' | $(TEE_A) '$(3)'
67
68LAZY_$(d) = \
69	prefix includedir libdir datadir bindir \
70	lua51cpath lua51path lua52cpath lua52path lua53cpath lua53path lua54cpath lua54path \
71	CC ALL_CPPFLAGS CPPFLAGS ALL_CFLAGS CFLAGS ALL_LDFLAGS LDFLAGS \
72	ALL_SOFLAGS SOFLAGS ALL_LIB LIBS \
73	$(foreach API,$(KNOWN_APIS),ALL_LUA$(subst .,,$(API))_CPPFLAGS) \
74	AR RANLIB M4 MV RM CP RMDIR MKDIR CHMOD INSTALL INSTALL_DATA TOUCH \
75	TEE TEE_A
76
77NONLAZY_$(d) = \
78	LUA_APIS \
79	$(foreach API,$(KNOWN_APIS),LUAC$(subst .,,$(API))) \
80	$(foreach API,$(KNOWN_APIS),$(and $(call WITH_API_FN,$(API)),LUA$(subst .,,$(API))_CPPFLAGS))
81
82$(d)/config:
83	$(TOUCH) $(@D)/.config.tmp
84	@$(foreach V,$(LAZY_$(@D)), $(call PRINT_$(@D),$(V),$(value $(V)),$(@D)/.config.tmp);)
85	@$(foreach V,$(NONLAZY_$(@D)), $(call PRINT_$(@D),$(V),$($(V)),$(@D)/.config.tmp);)
86	$(MV) $(@D)/.config.tmp $(@D)/.config
87
88# add local targets if building from inside project tree
89ifneq "$(filter $(abspath $(d)/..)/%, $(abspath $(firstword $(MAKEFILE_LIST))))" ""
90.PHONY: config configure
91
92config configure: $(d)/config
93endif
94
95
96#
97# L U A  A U T O D E T E C T I O N
98#
99
100# set LUA_APIS if empty or "?"
101ifeq ($(or $(strip $(LUA_APIS)),?),?)
102override LUA_APIS := $(call HAVE_API_FN,5.1) $(call HAVE_API_FN,5.2) $(call HAVE_API_FN,5.3) $(call HAVE_API_FN,5.4)
103endif
104
105define LUAXY_template
106
107# set luaXYcpath if empty or "?"
108ifeq ($$(or $$(strip $$(lua$(subst .,,$(1))cpath)),?),?)
109override lua$(subst .,,$(1))cpath := $$(or $$(call LUAPATH_FN,$(1),cdir),$$(libdir)/lua/$(1))
110endif
111
112# set luaXYpath if empty or "?"
113ifeq ($$(or $$(strip $$(lua$(subst .,,$(1))path)),?),?)
114override lua$(subst .,,$(1))path = $$(or $$(call LUAPATH_FN,$(1),ldir),$$(datadir)/lua/$(1))
115endif
116
117# set LUAXY_CPPFLAGS if undefined or "?" (NB: can be empty if path already in $(CPPFLAGS))
118ifeq ($$(and $$(findstring undefined,$$(origin LUA$(subst .,,$(1))_CPPFLAGS)),?),?)
119override LUA$(subst .,,$(1))_CPPFLAGS = $$(and $$(call WITH_API_FN,$(1)),$$(call LUAPATH_FN,$(1),cppflags))
120endif
121
122# set ALL_LUAXY_CPPFLAGS if empty or "?"
123ifeq ($$(or $$(strip $$(ALL_LUA$(subst .,,$(1))_CPPFLAGS)),?),?)
124override ALL_LUA$(subst .,,$(1))_CPPFLAGS = -DLUA_COMPAT_APIINTCASTS $$(LUA$(subst .,,$(1))_CPPFLAGS)
125endif
126
127# set LUAXYC if empty or "?"
128ifeq ($$(or $$(strip $$(LUAC$(subst .,,$(1)))),?),?)
129override LUAC$(subst .,,$(1)) = $$(or $$(call LUAPATH_FN,$(1),luac),true)
130endif
131
132endef # LUAXY_template
133
134$(eval $(call LUAXY_template,5.1))
135$(eval $(call LUAXY_template,5.2))
136$(eval $(call LUAXY_template,5.3))
137$(eval $(call LUAXY_template,5.4))
138
139#
140# A U T O D E T E C T  C O M P I L A T I O N  F L A G S
141#
142cc-option ?= $(shell if $(CC) $(1) -S -o /dev/null -xc /dev/null \
143             > /dev/null 2>&1; then echo "$(1)"; else echo "$(2)"; fi;)
144
145VENDOR_OS_$(d) := $(shell $(d)/mk/vendor.os)
146VENDOR_CC_$(d) := $(shell env CC="$(CC)" $(d)/mk/vendor.cc)
147
148#
149# ALL_CPPFLAGS
150#
151ifeq ($(origin ALL_CPPFLAGS), undefined)
152
153ifneq ($(VENDOR_OS_$(d)), OpenBSD)
154ALL_CPPFLAGS += -D_REENTRANT -D_THREAD_SAFE -D_GNU_SOURCE
155endif
156
157ifeq ($(VENDOR_OS_$(d)), SunOS)
158ALL_CPPFLAGS += -Usun -D_XPG4_2 -D__EXTENSIONS__
159endif
160
161ALL_CPPFLAGS += $(CPPFLAGS)
162
163endif # ALL_CPPFLAGS
164
165#
166# ALL_CFLAGS
167#
168ifeq ($(origin ALL_CFLAGS), undefined)
169
170ifeq ($(VENDOR_CC_$(d)), gcc)
171ALL_CFLAGS += -O2 -std=gnu99 -fPIC
172ALL_CFLAGS += -g -Wall -Wextra $(call cc-option, -Wno-missing-field-initializers) $(call cc-option, -Wno-override-init) -Wno-unused
173endif
174
175ifeq ($(VENDOR_CC_$(d)), clang)
176ALL_CFLAGS += -O2 -std=gnu99 -fPIC
177ALL_CFLAGS += -g -Wall -Wextra -Wno-missing-field-initializers -Wno-initializer-overrides -Wno-unused -Wno-dollar-in-identifier-extension
178endif
179
180ifeq ($(VENDOR_CC_$(d)), sunpro)
181ALL_CFLAGS += -xcode=pic13
182ALL_CFLAGS += -g
183#
184# Solaris Studio supports anonymous unions just fine; but it complains
185# incessantly about them.
186#
187ALL_CFLAGS += -erroff=E_ANONYMOUS_UNION_DECL
188endif
189
190ifeq ($(VENDOR_OS_$(d)), Darwin)
191ALL_CFLAGS += -Wno-deprecated-declarations
192endif
193
194ALL_CFLAGS += $(CFLAGS)
195
196endif # ALL_CFLAGS
197
198#
199# ALL_SOFLAGS
200#
201ifeq ($(origin ALL_SOFLAGS), undefined)
202
203ifeq ($(VENDOR_OS_$(d)), Darwin)
204ALL_SOFLAGS += -bundle -undefined dynamic_lookup
205else
206ALL_SOFLAGS += -shared
207endif
208
209ALL_SOFLAGS += $(SOFLAGS)
210
211endif # ALL_SOFLAGS
212
213#
214# ALL_LDFLAGS
215#
216ifeq ($(origin ALL_LDFLAGS), undefined)
217
218ALL_LDFLAGS += -L$(DESTDIR)$(libdir) -L$(libdir)
219ALL_LDFLAGS += $(LDFLAGS)
220
221endif # ALL_LDFLAGS
222
223#
224# ALL_LIBS
225#
226ifeq ($(origin ALL_LIBS), undefined)
227
228# put $(LIBS) first as they're more likely to be higher-level dependencies
229ALL_LIBS += $(LIBS)
230ALL_LIBS += -lssl -lcrypto -lpthread
231
232# NetBSD, FreeBSD, OpenBSD (and presumably descendants) lack any libdl;
233# dlopen, et al are part of libc.
234ifneq ($(patsubst %BSD,BSD,$(VENDOR_OS_$(d))), BSD)
235ALL_LIBS += -ldl
236endif
237
238# This only seems to be necessary on Linux. Darwin and OpenBSD lack a librt.
239# On OpenBSD clock_gettime is part of libc. Others have librt, but linking
240# it in is unnecessary.
241ifeq ($(VENDOR_OS_$(d)), Linux)
242ALL_LIBS += -lrt
243endif
244
245ALL_LIBS += -lm
246
247endif # ALL_LIBS
248
249
250#
251# P R O J E C T  R U L E S
252#
253include $(d)/src/GNUmakefile
254include $(d)/regress/GNUmakefile
255
256$(d)/config.h: $(d)/config.h.guess
257	$(CP) $< $@
258
259
260#
261# C L E A N  R U L E S
262#
263.PHONY: $(d)/clean~ clean~
264
265$(d)/clean~:
266	$(RM) -f $(@D)/*~
267
268clean~: $(d)/clean~
269
270
271#
272# D E B I A N  R U L E S
273#
274ifneq "$(filter $(abspath $(d))/%, $(abspath $(firstword $(MAKEFILE_LIST))))" ""
275
276DPKG_BUILDPACKAGE ?= dpkg-buildpackage
277FAKEROOT ?= fakeroot
278DPKG_BUILDPACKAGE_OPTIONS ?= -b -uc -us
279
280.PHONY: $(d)/debian $(d)/debian-clean debian deb debian-clean deb-clean
281
282$(d)/debian:
283	cd $(@D) && $(DPKG_BUILDPACKAGE) -rfakeroot $(DPKG_BUILDPACKAGE_OPTIONS)
284
285$(d)/debian-clean:
286	cd $(@D) && $(FAKEROOT) ./debian/rules clean
287
288debian deb: $(d)/debian
289
290debian-clean deb-clean: $(d)/debian-clean
291
292endif # debian guard
293
294
295#
296# R E D H A T  R U L E S
297#
298ifneq "$(filter $(abspath $(d))/%, $(abspath $(firstword $(MAKEFILE_LIST))))" ""
299.PHONY: $(d)/redhat $(d)/redhat-clean redhat rpm redhat-clean rpm-clean
300
301redhat rpm: $(d)/redhat
302
303redhat-clean rpm-clean: $(d)/redhat-clean
304
305endif # redhat guard
306
307
308#
309# R E L E A S E  T A R B A L L  R U L E S
310#
311ifneq "$(filter $(abspath $(d))/%, $(abspath $(firstword $(MAKEFILE_LIST))))" ""
312
313CQUEUES_VERSION := $(shell $(d)/mk/changelog version)
314
315.PHONY: $(d)/cqueues-$(CQUEUES_VERSION).tgz release
316
317$(d)/cqueues-$(CQUEUES_VERSION).tgz:
318	cd $(@D) && git archive --format=tar --prefix=$(basename $(@F))/ HEAD | gzip -c > $@
319
320release: $(d)/cqueues-$(CQUEUES_VERSION).tgz
321
322endif # release guard
323
324
325endif # include guard
326
327# non-recursive epilogue
328d := $(dirstack_$(sp))
329sp := $(basename $(sp))
330