Link postgres from all object files at once, to avoid the error-prone
authorPeter Eisentraut <peter_e@gmx.net>
Mon, 25 Feb 2008 17:55:42 +0000 (17:55 +0000)
committerPeter Eisentraut <peter_e@gmx.net>
Mon, 25 Feb 2008 17:55:42 +0000 (17:55 +0000)
SUBSYS.o step and allow for better optimization by the linker.

Instead of partial linking into SUBSYS.o, the list of object files is
assembled in objfiles.txt files that are expanded when the final
linking is done.

Because we are not yet sure how long command lines different platforms
can handle, the old way of linking is still available, by defining the
make variable PARTIAL_LINKING (e.g., make all PARTIAL_LINKING=1).  If
we determine that this is necessary for some platforms, then we will
document this in a more prominent place.

src/backend/Makefile
src/backend/common.mk

index e79359af5afea90e8892d004a68b273a5efafd34..4362dd51b8bae6bbfebb93e705813950a238f3a3 100644 (file)
@@ -14,17 +14,17 @@ subdir = src/backend
 top_builddir = ../..
 include $(top_builddir)/src/Makefile.global
 
-DIRS = access bootstrap catalog parser commands executor lib libpq \
+SUBDIRS = access bootstrap catalog parser commands executor lib libpq \
        main nodes optimizer port postmaster regex rewrite \
        storage tcop tsearch utils $(top_builddir)/src/timezone
 
-SUBSYSOBJS = $(DIRS:%=%/SUBSYS.o)
+include $(srcdir)/common.mk
 
 ifeq ($(enable_dtrace), yes)
 LOCALOBJS += utils/probes.o
 endif
 
-OBJS = $(SUBSYSOBJS) $(LOCALOBJS) $(top_builddir)/src/port/libpgport_srv.a
+OBJS = $(SUBDIROBJS) $(LOCALOBJS) $(top_builddir)/src/port/libpgport_srv.a
 
 # We put libpgport into OBJS, so remove it from LIBS; also add libldap
 LIBS := $(filter-out -lpgport, $(LIBS)) $(LDAP_LIBS_BE)
@@ -41,7 +41,7 @@ ifneq ($(PORTNAME), win32)
 ifneq ($(PORTNAME), aix)
 
 postgres: $(OBJS)
-       $(CC) $(CFLAGS) $(LDFLAGS) $(export_dynamic) $^ $(LIBS) -o $@
+       $(CC) $(CFLAGS) $(LDFLAGS) $(export_dynamic) $(call expand_subsys,$^) $(LIBS) -o $@
 
 endif
 endif
@@ -51,13 +51,13 @@ ifeq ($(PORTNAME), cygwin)
 
 postgres: $(OBJS) postgres.def libpostgres.a
        $(DLLTOOL) --dllname $@$(X) --output-exp $@.exp --def postgres.def
-       $(CC) $(CFLAGS) $(LDFLAGS) -o $@$(X) -Wl,--base-file,$@.base $@.exp $(OBJS) $(LIBS)
+       $(CC) $(CFLAGS) $(LDFLAGS) -o $@$(X) -Wl,--base-file,$@.base $@.exp $(call expand_subsys,$(OBJS)) $(LIBS)
        $(DLLTOOL) --dllname $@$(X) --base-file $@.base --output-exp $@.exp --def postgres.def
-       $(CC) $(CFLAGS) $(LDFLAGS) -Wl,--stack,$(WIN32_STACK_RLIMIT) -o $@$(X) $@.exp $(OBJS) $(LIBS)
+       $(CC) $(CFLAGS) $(LDFLAGS) -Wl,--stack,$(WIN32_STACK_RLIMIT) -o $@$(X) $@.exp $(call expand_subsys,$(OBJS)) $(LIBS)
        rm -f $@.exp $@.base
 
 postgres.def: $(OBJS)
-       $(DLLTOOL) --export-all --output-def $@ $^
+       $(DLLTOOL) --export-all --output-def $@ $(call expand_subsys,$^)
 
 libpostgres.a: postgres.def
        $(DLLTOOL) --dllname postgres.exe --def postgres.def --output-lib $@
@@ -69,13 +69,13 @@ LIBS += -lsecur32
 
 postgres: $(OBJS) postgres.def libpostgres.a $(WIN32RES)
        $(DLLTOOL) --dllname $@$(X) --output-exp $@.exp --def postgres.def
-       $(CC) $(CFLAGS) $(LDFLAGS) -o $@$(X) -Wl,--base-file,$@.base $@.exp $(OBJS) $(WIN32RES) $(LIBS)
+       $(CC) $(CFLAGS) $(LDFLAGS) -o $@$(X) -Wl,--base-file,$@.base $@.exp $(call expand_subsys,$(OBJS)) $(WIN32RES) $(LIBS)
        $(DLLTOOL) --dllname $@$(X) --base-file $@.base --output-exp $@.exp --def postgres.def
-       $(CC) $(CFLAGS) $(LDFLAGS) -Wl,--stack=$(WIN32_STACK_RLIMIT) -o $@$(X) $@.exp $(OBJS) $(WIN32RES) $(LIBS)
+       $(CC) $(CFLAGS) $(LDFLAGS) -Wl,--stack=$(WIN32_STACK_RLIMIT) -o $@$(X) $@.exp $(call expand_subsys,$(OBJS)) $(WIN32RES) $(LIBS)
        rm -f $@.exp $@.base
 
 postgres.def: $(OBJS)
-       $(DLLTOOL) --export-all --output-def $@ $^
+       $(DLLTOOL) --export-all --output-def $@ $(call expand_subsys,$^)
 
 libpostgres.a: postgres.def
        $(DLLTOOL) --dllname postgres.exe --def postgres.def --output-lib $@
@@ -85,10 +85,10 @@ endif # win32
 ifeq ($(PORTNAME), aix)
 
 postgres: $(POSTGRES_IMP)
-       $(CC) $(CFLAGS) $(LDFLAGS) $(OBJS) -Wl,-bE:$(top_builddir)/src/backend/$(POSTGRES_IMP) $(LIBS) -o $@
+       $(CC) $(CFLAGS) $(LDFLAGS) $(call expand_subsys,$(OBJS)) -Wl,-bE:$(top_builddir)/src/backend/$(POSTGRES_IMP) $(LIBS) -o $@
 
 $(POSTGRES_IMP): $(OBJS)
-       $(LD) $(LDREL) $(LDOUT) SUBSYS.o $^
+       $(LD) $(LDREL) $(LDOUT) SUBSYS.o $(call expand_subsys,$^)
 ifeq ($(host_os), aix3.2.5)
        $(MKLDEXPORT) SUBSYS.o $(bindir)/postgres > $@
 else
@@ -102,19 +102,14 @@ endif
 
 endif # aix
 
-# Parallel make trickery
-$(SUBSYSOBJS): $(DIRS:%=%-recursive) ;
-
-.PHONY: $(DIRS:%=%-recursive)
 # Update the commonly used headers before building the subdirectories
-$(DIRS:%=%-recursive): $(top_builddir)/src/include/parser/parse.h $(top_builddir)/src/include/utils/fmgroids.h
-       $(MAKE) -C $(subst -recursive,,$@) all
+$(SUBDIRS:%=%-recursive): $(top_builddir)/src/include/parser/parse.h $(top_builddir)/src/include/utils/fmgroids.h
 
 
 # The postgres.o target is needed by the rule in Makefile.global that
 # creates the exports file when MAKE_EXPORTS = true.
 postgres.o: $(OBJS)
-       $(CC) $(LDREL) $(LDFLAGS) $^ $(LIBS) -o $@
+       $(CC) $(LDREL) $(LDFLAGS) $(call expand_subsys,$^) $(LIBS) -o $@
 
 
 # The following targets are specified in make commands that appear in
@@ -141,7 +136,7 @@ $(top_builddir)/src/include/utils/fmgroids.h: utils/fmgroids.h
            $(LN_S) ../../../$(subdir)/utils/fmgroids.h .
 
 
-utils/probes.o: utils/probes.d $(SUBSYSOBJS)
+utils/probes.o: utils/probes.d $(SUBDIROBJS)
        $(DTRACE) $(DTRACEFLAGS) -G -s $^ -o $@
 
 
@@ -241,7 +236,6 @@ endif
 ifeq ($(PORTNAME), win32)
        rm -f postgres.dll postgres.def libpostgres.a $(WIN32RES)
 endif
-       for i in $(DIRS); do $(MAKE) -C $$i clean || exit; done
 
 distclean: clean
        rm -f port/tas.s port/dynloader.c port/pg_sema.c port/pg_shmem.c
@@ -264,4 +258,4 @@ maintainer-clean: distclean
 # are up to date.  It saves the time of doing all the submakes.
 .PHONY: quick
 quick: $(OBJS)
-       $(CC) $(CFLAGS) $(LDFLAGS) $(export_dynamic) $^ $(LIBS) -o postgres
+       $(CC) $(CFLAGS) $(LDFLAGS) $(export_dynamic) $(call expand_subsys,$^) $(LIBS) -o postgres
index 17eec6f8da78463a2c6f5c5cfee95e95c8e37f46..46c75bd17ac7782971e6571ed89ea8e56b7bc230 100644 (file)
@@ -4,22 +4,44 @@
 # $PostgreSQL$
 #
 
-SUBDIROBJS = $(SUBDIRS:%=%/SUBSYS.o)
+# When including this file, set OBJS to the object files created in
+# this directory and SUBDIRS to subdirectories containing more things
+# to build.
 
-all: SUBSYS.o
+ifdef PARTIAL_LINKING
+# old style: linking using SUBSYS.o
+subsysfilename = SUBSYS.o
+else
+# new style: linking all object files at once
+subsysfilename = objfiles.txt
+endif
+
+SUBDIROBJS = $(SUBDIRS:%=%/$(subsysfilename))
+
+# top-level backend directory obviously has its own "all" target
+ifneq ($(subdir), src/backend)
+all: $(subsysfilename)
+endif
 
 SUBSYS.o: $(SUBDIROBJS) $(OBJS)
        $(LD) $(LDREL) $(LDOUT) $@ $^
 
+objfiles.txt: $(SUBDIROBJS) $(OBJS)
+       ( $(if $(SUBDIROBJS),cat $(SUBDIROBJS); )echo $(addprefix $(subdir)/,$(OBJS)) ) >$@
+
+# make function to expand objfiles.txt contents
+expand_subsys = $(foreach file,$(filter %/objfiles.txt,$(1)),$(patsubst ../../src/backend/%,%,$(addprefix $(top_builddir)/,$(shell cat $(file))))) $(filter-out %/objfiles.txt,$(1))
+
+# Parallel make trickery
 $(SUBDIROBJS): $(SUBDIRS:%=%-recursive) ;
 
 .PHONY: $(SUBDIRS:%=%-recursive)
 $(SUBDIRS:%=%-recursive):
-       $(MAKE) -C $(subst -recursive,,$@) SUBSYS.o
+       $(MAKE) -C $(subst -recursive,,$@) all
 
 clean: clean-local
 clean-local:
 ifdef SUBDIRS
        for dir in $(SUBDIRS); do $(MAKE) -C $$dir clean || exit; done
 endif
-       rm -f SUBSYS.o $(OBJS)
+       rm -f $(subsysfilename) $(OBJS)