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#   REGRESS -- list of regression test cases (without suffix)
42#   REGRESS_OPTS -- additional switches to pass to pg_regress
43#   NO_INSTALLCHECK -- don't define an installcheck target, useful e.g. if
44#     tests require special configuration, or don't use pg_regress
45#   EXTRA_CLEAN -- extra files to remove in 'make clean'
46#   PG_CPPFLAGS -- will be prepended to CPPFLAGS
47#   PG_CFLAGS -- will be appended to CFLAGS
48#   PG_CXXFLAGS -- will be appended to CXXFLAGS
49#   PG_LDFLAGS -- will be prepended to LDFLAGS
50#   PG_LIBS -- will be added to PROGRAM link line
51#   PG_LIBS_INTERNAL -- same, for references to libraries within build tree
52#   SHLIB_LINK -- will be added to MODULE_big link line
53#   SHLIB_LINK_INTERNAL -- same, for references to libraries within build tree
54#   PG_CONFIG -- path to pg_config program for the PostgreSQL installation
55#     to build against (typically just "pg_config" to use the first one in
56#     your PATH)
57#
58# Better look at some of the existing uses for examples...
59
60ifndef PGXS
61ifndef NO_PGXS
62$(error pgxs error: makefile variable PGXS or NO_PGXS must be set)
63endif
64endif
65
66
67ifdef PGXS
68# We assume that we are in src/makefiles/, so top is ...
69top_builddir := $(dir $(PGXS))../..
70include $(top_builddir)/src/Makefile.global
71
72# These might be set in Makefile.global, but if they were not found
73# during the build of PostgreSQL, supply default values so that users
74# of pgxs can use the variables.
75ifeq ($(BISON),)
76BISON = bison
77endif
78ifeq ($(FLEX),)
79FLEX = flex
80endif
81endif
82
83
84override CPPFLAGS := -I. -I$(srcdir) $(CPPFLAGS)
85
86ifdef MODULES
87override CFLAGS += $(CFLAGS_SL)
88endif
89
90ifdef MODULEDIR
91datamoduledir := $(MODULEDIR)
92docmoduledir := $(MODULEDIR)
93else
94ifdef EXTENSION
95datamoduledir := extension
96docmoduledir := extension
97else
98datamoduledir := contrib
99docmoduledir := contrib
100endif
101endif
102
103ifdef PG_CPPFLAGS
104override CPPFLAGS := $(PG_CPPFLAGS) $(CPPFLAGS)
105endif
106ifdef PG_CFLAGS
107override CFLAGS := $(CFLAGS) $(PG_CFLAGS)
108endif
109ifdef PG_CXXFLAGS
110override CXXFLAGS := $(CXXFLAGS) $(PG_CXXFLAGS)
111endif
112ifdef PG_LDFLAGS
113override LDFLAGS := $(PG_LDFLAGS) $(LDFLAGS)
114endif
115
116all: $(PROGRAM) $(DATA_built) $(SCRIPTS_built) $(addsuffix $(DLSUFFIX), $(MODULES)) $(addsuffix .control, $(EXTENSION))
117
118ifdef MODULE_big
119# shared library parameters
120NAME = $(MODULE_big)
121
122include $(top_srcdir)/src/Makefile.shlib
123
124all: all-lib
125endif # MODULE_big
126
127
128install: all installdirs
129ifneq (,$(EXTENSION))
130	$(INSTALL_DATA) $(addprefix $(srcdir)/, $(addsuffix .control, $(EXTENSION))) '$(DESTDIR)$(datadir)/extension/'
131endif # EXTENSION
132ifneq (,$(DATA)$(DATA_built))
133	$(INSTALL_DATA) $(addprefix $(srcdir)/, $(DATA)) $(DATA_built) '$(DESTDIR)$(datadir)/$(datamoduledir)/'
134endif # DATA
135ifneq (,$(DATA_TSEARCH))
136	$(INSTALL_DATA) $(addprefix $(srcdir)/, $(DATA_TSEARCH)) '$(DESTDIR)$(datadir)/tsearch_data/'
137endif # DATA_TSEARCH
138ifdef MODULES
139	$(INSTALL_SHLIB) $(addsuffix $(DLSUFFIX), $(MODULES)) '$(DESTDIR)$(pkglibdir)/'
140endif # MODULES
141ifdef DOCS
142ifdef docdir
143	$(INSTALL_DATA) $(addprefix $(srcdir)/, $(DOCS)) '$(DESTDIR)$(docdir)/$(docmoduledir)/'
144endif # docdir
145endif # DOCS
146ifdef PROGRAM
147	$(INSTALL_PROGRAM) $(PROGRAM)$(X) '$(DESTDIR)$(bindir)'
148endif # PROGRAM
149ifdef SCRIPTS
150	$(INSTALL_SCRIPT) $(addprefix $(srcdir)/, $(SCRIPTS)) '$(DESTDIR)$(bindir)/'
151endif # SCRIPTS
152ifdef SCRIPTS_built
153	$(INSTALL_SCRIPT) $(SCRIPTS_built) '$(DESTDIR)$(bindir)/'
154endif # SCRIPTS_built
155
156ifdef MODULE_big
157install: install-lib
158endif # MODULE_big
159
160
161installdirs:
162ifneq (,$(EXTENSION))
163	$(MKDIR_P) '$(DESTDIR)$(datadir)/extension'
164endif
165ifneq (,$(DATA)$(DATA_built))
166	$(MKDIR_P) '$(DESTDIR)$(datadir)/$(datamoduledir)'
167endif
168ifneq (,$(DATA_TSEARCH))
169	$(MKDIR_P) '$(DESTDIR)$(datadir)/tsearch_data'
170endif
171ifneq (,$(MODULES))
172	$(MKDIR_P) '$(DESTDIR)$(pkglibdir)'
173endif
174ifdef DOCS
175ifdef docdir
176	$(MKDIR_P) '$(DESTDIR)$(docdir)/$(docmoduledir)'
177endif # docdir
178endif # DOCS
179ifneq (,$(PROGRAM)$(SCRIPTS)$(SCRIPTS_built))
180	$(MKDIR_P) '$(DESTDIR)$(bindir)'
181endif
182
183ifdef MODULE_big
184installdirs: installdirs-lib
185endif # MODULE_big
186
187
188uninstall:
189ifneq (,$(EXTENSION))
190	rm -f $(addprefix '$(DESTDIR)$(datadir)/extension'/, $(notdir $(addsuffix .control, $(EXTENSION))))
191endif
192ifneq (,$(DATA)$(DATA_built))
193	rm -f $(addprefix '$(DESTDIR)$(datadir)/$(datamoduledir)'/, $(notdir $(DATA) $(DATA_built)))
194endif
195ifneq (,$(DATA_TSEARCH))
196	rm -f $(addprefix '$(DESTDIR)$(datadir)/tsearch_data'/, $(notdir $(DATA_TSEARCH)))
197endif
198ifdef MODULES
199	rm -f $(addprefix '$(DESTDIR)$(pkglibdir)'/, $(addsuffix $(DLSUFFIX), $(MODULES)))
200endif
201ifdef DOCS
202	rm -f $(addprefix '$(DESTDIR)$(docdir)/$(docmoduledir)'/, $(DOCS))
203endif
204ifdef PROGRAM
205	rm -f '$(DESTDIR)$(bindir)/$(PROGRAM)$(X)'
206endif
207ifdef SCRIPTS
208	rm -f $(addprefix '$(DESTDIR)$(bindir)'/, $(SCRIPTS))
209endif
210ifdef SCRIPTS_built
211	rm -f $(addprefix '$(DESTDIR)$(bindir)'/, $(SCRIPTS_built))
212endif
213
214ifdef MODULE_big
215uninstall: uninstall-lib
216endif # MODULE_big
217
218
219clean:
220ifdef MODULES
221	rm -f $(addsuffix $(DLSUFFIX), $(MODULES)) $(addsuffix .o, $(MODULES)) $(if $(PGFILEDESC),$(WIN32RES))
222endif
223ifdef DATA_built
224	rm -f $(DATA_built)
225endif
226ifdef SCRIPTS_built
227	rm -f $(SCRIPTS_built)
228endif
229ifdef PROGRAM
230	rm -f $(PROGRAM)$(X)
231endif
232ifdef OBJS
233	rm -f $(OBJS)
234endif
235ifdef EXTRA_CLEAN
236	rm -rf $(EXTRA_CLEAN)
237endif
238ifdef REGRESS
239# things created by various check targets
240	rm -rf $(pg_regress_clean_files)
241ifeq ($(PORTNAME), win)
242	rm -f regress.def
243endif
244endif # REGRESS
245
246ifdef MODULE_big
247clean: clean-lib
248endif
249
250distclean maintainer-clean: clean
251
252
253ifdef REGRESS
254
255REGRESS_OPTS += --dbname=$(CONTRIB_TESTDB)
256
257# When doing a VPATH build, must copy over the data files so that the
258# driver script can find them.  We have to use an absolute path for
259# the targets, because otherwise make will try to locate the missing
260# files using VPATH, and will find them in $(srcdir), but the point
261# here is that we want to copy them from $(srcdir) to the build
262# directory.
263
264ifdef VPATH
265abs_builddir := $(shell pwd)
266test_files_src := $(wildcard $(srcdir)/data/*.data)
267test_files_build := $(patsubst $(srcdir)/%, $(abs_builddir)/%, $(test_files_src))
268
269all: $(test_files_build)
270$(test_files_build): $(abs_builddir)/%: $(srcdir)/%
271	$(MKDIR_P) $(dir $@)
272	ln -s $< $@
273endif # VPATH
274
275.PHONY: submake
276submake:
277ifndef PGXS
278	$(MAKE) -C $(top_builddir)/src/test/regress pg_regress$(X)
279endif
280
281# against installed postmaster
282ifndef NO_INSTALLCHECK
283installcheck: submake $(REGRESS_PREP)
284	$(pg_regress_installcheck) $(REGRESS_OPTS) $(REGRESS)
285endif
286
287ifdef PGXS
288check:
289	@echo '"$(MAKE) check" is not supported.'
290	@echo 'Do "$(MAKE) install", then "$(MAKE) installcheck" instead.'
291else
292check: submake $(REGRESS_PREP)
293	$(pg_regress_check) $(REGRESS_OPTS) $(REGRESS)
294endif
295endif # REGRESS
296
297ifndef NO_TEMP_INSTALL
298checkprep: EXTRA_INSTALL+=$(subdir)
299endif
300
301
302# STANDARD RULES
303
304ifneq (,$(MODULES)$(MODULE_big))
305%.sql: %.sql.in
306	sed 's,MODULE_PATHNAME,$$libdir/$*,g' $< >$@
307endif
308
309ifdef PROGRAM
310$(PROGRAM): $(OBJS)
311	$(CC) $(CFLAGS) $(OBJS) $(PG_LIBS_INTERNAL) $(LDFLAGS) $(LDFLAGS_EX) $(PG_LIBS) $(LIBS) -o $@$(X)
312endif
313