1# PGXS: PostgreSQL extensions makefile
2
3# src/makefiles/pgxs.mk
4
5# This file contains generic rules to build many kinds of simple
6# extension modules.  You only need to set a few variables and include
7# this file, the rest will be done here.
8#
9# Use the following layout for your Makefile:
10#
11#   [variable assignments, see below]
12#
13#   PG_CONFIG = pg_config
14#   PGXS := $(shell $(PG_CONFIG) --pgxs)
15#   include $(PGXS)
16#
17#   [custom rules, rarely necessary]
18#
19# Set one of these three variables to specify what is built:
20#
21#   MODULES -- list of shared-library objects to be built from source files
22#     with same stem (do not include library suffixes in this list)
23#   MODULE_big -- a shared library to build from multiple source files
24#     (list object files in OBJS)
25#   PROGRAM -- an executable program to build (list object files in OBJS)
26#
27# The following variables can also be set:
28#
29#   EXTENSION -- name of extension (there must be a $EXTENSION.control file)
30#   MODULEDIR -- subdirectory of $PREFIX/share into which DATA and DOCS files
31#     should be installed (if not set, default is "extension" if EXTENSION
32#     is set, or "contrib" if not)
33#   DATA -- random files to install into $PREFIX/share/$MODULEDIR
34#   DATA_built -- random files to install into $PREFIX/share/$MODULEDIR,
35#     which need to be built first
36#   DATA_TSEARCH -- random files to install into $PREFIX/share/tsearch_data
37#   DOCS -- random files to install under $PREFIX/doc/$MODULEDIR
38#   SCRIPTS -- script files (not binaries) to install into $PREFIX/bin
39#   SCRIPTS_built -- script files (not binaries) to install into $PREFIX/bin,
40#     which need to be built first
41#   HEADERS -- files to install into $(includedir_server)/$MODULEDIR/$MODULE_big
42#   HEADERS_built -- as above but built first (but NOT cleaned)
43#   HEADERS_$(MODULE) -- files to install into
44#     $(includedir_server)/$MODULEDIR/$MODULE; the value of $MODULE must be
45#     listed in MODULES or MODULE_big
46#   HEADERS_built_$(MODULE) -- as above but built first (also NOT cleaned)
47#   REGRESS -- list of regression test cases (without suffix)
48#   REGRESS_OPTS -- additional switches to pass to pg_regress
49#   NO_INSTALLCHECK -- don't define an installcheck target, useful e.g. if
50#     tests require special configuration, or don't use pg_regress
51#   EXTRA_CLEAN -- extra files to remove in 'make clean'
52#   PG_CPPFLAGS -- will be prepended to CPPFLAGS
53#   PG_CFLAGS -- will be appended to CFLAGS
54#   PG_CXXFLAGS -- will be appended to CXXFLAGS
55#   PG_LDFLAGS -- will be prepended to LDFLAGS
56#   PG_LIBS -- will be added to PROGRAM link line
57#   PG_LIBS_INTERNAL -- same, for references to libraries within build tree
58#   SHLIB_LINK -- will be added to MODULE_big link line
59#   SHLIB_LINK_INTERNAL -- same, for references to libraries within build tree
60#   PG_CONFIG -- path to pg_config program for the PostgreSQL installation
61#     to build against (typically just "pg_config" to use the first one in
62#     your PATH)
63#
64# Better look at some of the existing uses for examples...
65
66ifndef PGXS
67ifndef NO_PGXS
68$(error pgxs error: makefile variable PGXS or NO_PGXS must be set)
69endif
70endif
71
72
73ifdef PGXS
74
75# External extensions must assume generated headers are available
76NO_GENERATED_HEADERS=yes
77# The temp-install rule won't work, either
78NO_TEMP_INSTALL=yes
79
80# We assume that we are in src/makefiles/, so top is ...
81top_builddir := $(dir $(PGXS))../..
82include $(top_builddir)/src/Makefile.global
83
84# These might be set in Makefile.global, but if they were not found
85# during the build of PostgreSQL, supply default values so that users
86# of pgxs can use the variables.
87ifeq ($(BISON),)
88BISON = bison
89endif
90ifeq ($(FLEX),)
91FLEX = flex
92endif
93
94endif # PGXS
95
96
97override CPPFLAGS := -I. -I$(srcdir) $(CPPFLAGS)
98
99ifdef MODULES
100override CFLAGS += $(CFLAGS_SL)
101endif
102
103ifdef MODULEDIR
104datamoduledir := $(MODULEDIR)
105docmoduledir := $(MODULEDIR)
106incmoduledir := $(MODULEDIR)
107else
108ifdef EXTENSION
109datamoduledir := extension
110docmoduledir := extension
111incmoduledir := extension
112else
113datamoduledir := contrib
114docmoduledir := contrib
115incmoduledir := contrib
116endif
117endif
118
119ifdef PG_CPPFLAGS
120override CPPFLAGS := $(PG_CPPFLAGS) $(CPPFLAGS)
121endif
122ifdef PG_CFLAGS
123override CFLAGS := $(CFLAGS) $(PG_CFLAGS)
124endif
125ifdef PG_CXXFLAGS
126override CXXFLAGS := $(CXXFLAGS) $(PG_CXXFLAGS)
127endif
128ifdef PG_LDFLAGS
129override LDFLAGS := $(PG_LDFLAGS) $(LDFLAGS)
130endif
131
132# logic for HEADERS_* stuff
133
134# get list of all names used with or without built_ prefix
135# note that use of HEADERS_built_foo will get both "foo" and "built_foo",
136# we cope with that later when filtering this list against MODULES.
137# If someone wants to name a module "built_foo", they can do that and it
138# works, but if they have MODULES = foo built_foo  then they will need to
139# force building of all headers and use HEADERS_built_foo and
140# HEADERS_built_built_foo.
141HEADER_alldirs := $(patsubst HEADERS_%,%,$(filter HEADERS_%, $(.VARIABLES)))
142HEADER_alldirs += $(patsubst HEADERS_built_%,%,$(filter HEADERS_built_%, $(.VARIABLES)))
143
144# collect all names of built headers to use as a dependency
145HEADER_allbuilt :=
146
147ifdef MODULE_big
148
149# we can unconditionally add $(MODULE_big) here, because we will strip it
150# back out below if it turns out not to actually define any headers.
151HEADER_dirs := $(MODULE_big)
152HEADER_unbuilt_$(MODULE_big) = $(HEADERS)
153HEADER_built_$(MODULE_big) = $(HEADERS_built)
154HEADER_allbuilt += $(HEADERS_built)
155# treat "built" as an exclusion below as well as "built_foo"
156HEADER_xdirs := built built_$(MODULE_big)
157
158else # not MODULE_big, so check MODULES
159
160# HEADERS is an error in the absence of MODULE_big to provide a dir name
161ifdef HEADERS
162$(error HEADERS requires MODULE_big to be set)
163endif
164# make list of modules that have either HEADERS_foo or HEADERS_built_foo
165HEADER_dirs := $(foreach m,$(MODULES),$(if $(filter $(m) built_$(m),$(HEADER_alldirs)),$(m)))
166# make list of conflicting names to exclude
167HEADER_xdirs := $(addprefix built_,$(HEADER_dirs))
168
169endif # MODULE_big or MODULES
170
171# HEADERS_foo requires that "foo" is in MODULES as a sanity check
172ifneq (,$(filter-out $(HEADER_dirs) $(HEADER_xdirs),$(HEADER_alldirs)))
173$(error $(patsubst %,HEADERS_%,$(filter-out $(HEADER_dirs) $(HEADER_xdirs),$(HEADER_alldirs))) defined with no module)
174endif
175
176# assign HEADER_unbuilt_foo and HEADER_built_foo, but make sure
177# that "built" takes precedence in the case of conflict, by removing
178# conflicting module names when matching the unbuilt name
179$(foreach m,$(filter-out $(HEADER_xdirs),$(HEADER_dirs)),$(eval HEADER_unbuilt_$(m) += $$(HEADERS_$(m))))
180$(foreach m,$(HEADER_dirs),$(eval HEADER_built_$(m) += $$(HEADERS_built_$(m))))
181$(foreach m,$(HEADER_dirs),$(eval HEADER_allbuilt += $$(HEADERS_built_$(m))))
182
183# expand out the list of headers for each dir, attaching source prefixes
184header_file_list = $(HEADER_built_$(1)) $(addprefix $(srcdir)/,$(HEADER_unbuilt_$(1)))
185$(foreach m,$(HEADER_dirs),$(eval HEADER_files_$(m) := $$(call header_file_list,$$(m))))
186
187# note that the caller's HEADERS* vars have all been expanded now, and
188# later changes will have no effect.
189
190# remove entries in HEADER_dirs that produced an empty list of files,
191# to ensure we don't try and install them
192HEADER_dirs := $(foreach m,$(HEADER_dirs),$(if $(strip $(HEADER_files_$(m))),$(m)))
193
194# Functions for generating install/uninstall commands; the blank lines
195# before the "endef" are required, don't lose them
196# $(call install_headers,dir,headers)
197define install_headers
198$(MKDIR_P) '$(DESTDIR)$(includedir_server)/$(incmoduledir)/$(1)/'
199$(INSTALL_DATA) $(2) '$(DESTDIR)$(includedir_server)/$(incmoduledir)/$(1)/'
200
201endef
202# $(call uninstall_headers,dir,headers)
203define uninstall_headers
204rm -f $(addprefix '$(DESTDIR)$(includedir_server)/$(incmoduledir)/$(1)'/, $(notdir $(2)))
205
206endef
207
208# end of HEADERS_* stuff
209
210
211all: $(PROGRAM) $(DATA_built) $(HEADER_allbuilt) $(SCRIPTS_built) $(addsuffix $(DLSUFFIX), $(MODULES)) $(addsuffix .control, $(EXTENSION))
212
213ifeq ($(with_llvm), yes)
214all: $(addsuffix .bc, $(MODULES)) $(patsubst %.o,%.bc, $(OBJS))
215endif
216
217ifdef MODULE_big
218# shared library parameters
219NAME = $(MODULE_big)
220
221include $(top_srcdir)/src/Makefile.shlib
222
223all: all-lib
224endif # MODULE_big
225
226
227install: all installdirs
228ifneq (,$(EXTENSION))
229	$(INSTALL_DATA) $(addprefix $(srcdir)/, $(addsuffix .control, $(EXTENSION))) '$(DESTDIR)$(datadir)/extension/'
230endif # EXTENSION
231ifneq (,$(DATA)$(DATA_built))
232	$(INSTALL_DATA) $(addprefix $(srcdir)/, $(DATA)) $(DATA_built) '$(DESTDIR)$(datadir)/$(datamoduledir)/'
233endif # DATA
234ifneq (,$(DATA_TSEARCH))
235	$(INSTALL_DATA) $(addprefix $(srcdir)/, $(DATA_TSEARCH)) '$(DESTDIR)$(datadir)/tsearch_data/'
236endif # DATA_TSEARCH
237ifdef MODULES
238	$(INSTALL_SHLIB) $(addsuffix $(DLSUFFIX), $(MODULES)) '$(DESTDIR)$(pkglibdir)/'
239ifeq ($(with_llvm), yes)
240	$(foreach mod, $(MODULES), $(call install_llvm_module,$(mod),$(mod).bc))
241endif # with_llvm
242endif # MODULES
243ifdef DOCS
244ifdef docdir
245	$(INSTALL_DATA) $(addprefix $(srcdir)/, $(DOCS)) '$(DESTDIR)$(docdir)/$(docmoduledir)/'
246endif # docdir
247endif # DOCS
248ifdef PROGRAM
249	$(INSTALL_PROGRAM) $(PROGRAM)$(X) '$(DESTDIR)$(bindir)'
250endif # PROGRAM
251ifdef SCRIPTS
252	$(INSTALL_SCRIPT) $(addprefix $(srcdir)/, $(SCRIPTS)) '$(DESTDIR)$(bindir)/'
253endif # SCRIPTS
254ifdef SCRIPTS_built
255	$(INSTALL_SCRIPT) $(SCRIPTS_built) '$(DESTDIR)$(bindir)/'
256endif # SCRIPTS_built
257ifneq (,$(strip $(HEADER_dirs)))
258	$(foreach dir,$(HEADER_dirs),$(call install_headers,$(dir),$(HEADER_files_$(dir))))
259endif # HEADERS
260ifdef MODULE_big
261ifeq ($(with_llvm), yes)
262	$(call install_llvm_module,$(MODULE_big),$(OBJS))
263endif # with_llvm
264
265install: install-lib
266endif # MODULE_big
267
268
269installdirs:
270ifneq (,$(EXTENSION))
271	$(MKDIR_P) '$(DESTDIR)$(datadir)/extension'
272endif
273ifneq (,$(DATA)$(DATA_built))
274	$(MKDIR_P) '$(DESTDIR)$(datadir)/$(datamoduledir)'
275endif
276ifneq (,$(DATA_TSEARCH))
277	$(MKDIR_P) '$(DESTDIR)$(datadir)/tsearch_data'
278endif
279ifneq (,$(MODULES))
280	$(MKDIR_P) '$(DESTDIR)$(pkglibdir)'
281endif
282ifdef DOCS
283ifdef docdir
284	$(MKDIR_P) '$(DESTDIR)$(docdir)/$(docmoduledir)'
285endif # docdir
286endif # DOCS
287ifneq (,$(PROGRAM)$(SCRIPTS)$(SCRIPTS_built))
288	$(MKDIR_P) '$(DESTDIR)$(bindir)'
289endif
290
291ifdef MODULE_big
292installdirs: installdirs-lib
293endif # MODULE_big
294
295
296uninstall:
297ifneq (,$(EXTENSION))
298	rm -f $(addprefix '$(DESTDIR)$(datadir)/extension'/, $(notdir $(addsuffix .control, $(EXTENSION))))
299endif
300ifneq (,$(DATA)$(DATA_built))
301	rm -f $(addprefix '$(DESTDIR)$(datadir)/$(datamoduledir)'/, $(notdir $(DATA) $(DATA_built)))
302endif
303ifneq (,$(DATA_TSEARCH))
304	rm -f $(addprefix '$(DESTDIR)$(datadir)/tsearch_data'/, $(notdir $(DATA_TSEARCH)))
305endif
306ifdef MODULES
307	rm -f $(addprefix '$(DESTDIR)$(pkglibdir)'/, $(addsuffix $(DLSUFFIX), $(MODULES)))
308ifeq ($(with_llvm), yes)
309	$(foreach mod, $(MODULES), $(call uninstall_llvm_module,$(mod)))
310endif # with_llvm
311endif # MODULES
312ifdef DOCS
313	rm -f $(addprefix '$(DESTDIR)$(docdir)/$(docmoduledir)'/, $(DOCS))
314endif
315ifdef PROGRAM
316	rm -f '$(DESTDIR)$(bindir)/$(PROGRAM)$(X)'
317endif
318ifdef SCRIPTS
319	rm -f $(addprefix '$(DESTDIR)$(bindir)'/, $(SCRIPTS))
320endif
321ifdef SCRIPTS_built
322	rm -f $(addprefix '$(DESTDIR)$(bindir)'/, $(SCRIPTS_built))
323endif
324ifneq (,$(strip $(HEADER_dirs)))
325	$(foreach dir,$(HEADER_dirs),$(call uninstall_headers,$(dir),$(HEADER_files_$(dir))))
326endif # HEADERS
327
328ifdef MODULE_big
329ifeq ($(with_llvm), yes)
330	$(call uninstall_llvm_module,$(MODULE_big))
331endif # with_llvm
332
333uninstall: uninstall-lib
334endif # MODULE_big
335
336
337clean:
338ifdef MODULES
339	rm -f $(addsuffix $(DLSUFFIX), $(MODULES)) $(addsuffix .o, $(MODULES)) $(if $(PGFILEDESC),$(WIN32RES)) \
340	    $(addsuffix .bc, $(MODULES))
341endif
342ifdef DATA_built
343	rm -f $(DATA_built)
344endif
345ifdef SCRIPTS_built
346	rm -f $(SCRIPTS_built)
347endif
348ifdef PROGRAM
349	rm -f $(PROGRAM)$(X)
350endif
351ifdef OBJS
352	rm -f $(OBJS) $(patsubst %.o,%.bc, $(OBJS))
353endif
354ifdef EXTRA_CLEAN
355	rm -rf $(EXTRA_CLEAN)
356endif
357ifdef REGRESS
358# things created by various check targets
359	rm -rf $(pg_regress_clean_files)
360ifeq ($(PORTNAME), win)
361	rm -f regress.def
362endif
363endif # REGRESS
364
365ifdef MODULE_big
366clean: clean-lib
367endif
368
369distclean maintainer-clean: clean
370
371
372ifdef REGRESS
373
374REGRESS_OPTS += --dbname=$(CONTRIB_TESTDB)
375
376# When doing a VPATH build, must copy over the data files so that the
377# driver script can find them.  We have to use an absolute path for
378# the targets, because otherwise make will try to locate the missing
379# files using VPATH, and will find them in $(srcdir), but the point
380# here is that we want to copy them from $(srcdir) to the build
381# directory.
382
383ifdef VPATH
384abs_builddir := $(shell pwd)
385test_files_src := $(wildcard $(srcdir)/data/*.data)
386test_files_build := $(patsubst $(srcdir)/%, $(abs_builddir)/%, $(test_files_src))
387
388all: $(test_files_build)
389$(test_files_build): $(abs_builddir)/%: $(srcdir)/%
390	$(MKDIR_P) $(dir $@)
391	ln -s $< $@
392endif # VPATH
393
394.PHONY: submake
395submake:
396ifndef PGXS
397	$(MAKE) -C $(top_builddir)/src/test/regress pg_regress$(X)
398endif
399
400# against installed postmaster
401ifndef NO_INSTALLCHECK
402installcheck: submake $(REGRESS_PREP)
403	$(pg_regress_installcheck) $(REGRESS_OPTS) $(REGRESS)
404endif
405
406ifdef PGXS
407check:
408	@echo '"$(MAKE) check" is not supported.'
409	@echo 'Do "$(MAKE) install", then "$(MAKE) installcheck" instead.'
410else
411check: submake $(REGRESS_PREP)
412	$(pg_regress_check) $(REGRESS_OPTS) $(REGRESS)
413endif
414endif # REGRESS
415
416ifndef NO_TEMP_INSTALL
417checkprep: EXTRA_INSTALL+=$(subdir)
418endif
419
420
421# STANDARD RULES
422
423ifneq (,$(MODULES)$(MODULE_big))
424%.sql: %.sql.in
425	sed 's,MODULE_PATHNAME,$$libdir/$*,g' $< >$@
426endif
427
428ifdef PROGRAM
429$(PROGRAM): $(OBJS)
430	$(CC) $(CFLAGS) $(OBJS) $(PG_LIBS_INTERNAL) $(LDFLAGS) $(LDFLAGS_EX) $(PG_LIBS) $(LIBS) -o $@$(X)
431endif
432