# Description: allow using -j make option to build firebird using parallel
#  makes. This speeds up the build significantly
#  An early version of the patch was forwarded upstream, but resulted in no
#  comments.
# Author: Damyan Ivanov <dmn@debian.org>
# Forwarded: no
--- a/builds/posix/Makefile.in.firebird
+++ b/builds/posix/Makefile.in.firebird
@@ -53,7 +53,13 @@ export DYLD_FRAMEWORK_PATH
 export LD_LIBRARY_PATH
 
 CPU = 1
-CPU_OPTION = -j$(CPU)
+CPU_OPTION =
+
+# There are some interdependencies between the targets here, which are not yet
+# clear. To avoid clashes, we disable satisfying them in paralleli. This still
+# allows any -j option propagation to the sub-makes and when satisfying each of
+# the targets
+.NOTPARALLEL:
 
 ######################################################################
 ###########  Build process overview  #################################
@@ -132,7 +138,7 @@ CPU_OPTION = -j$(CPU)
 # MOD 28-July-2002)
 .PHONY: all firebird firebird_boot firebird_basic 
 .PHONY: firebird_embedded firebird_server classic_programs super_programs
-.PHONY: firebird_super firebird_classic btyacc_binary
+.PHONY: firebird_super firebird_classic
 .PHONY: fbtrace
 
 
@@ -157,7 +163,7 @@ firebird_super firebird_server: firebird
 # some complications particularly for super in needing to start the server
 # that you are currently building. MOD 28-July-2002
 
-firebird_basic: btyacc_binary external_libraries firebird_boot basic_targets
+firebird_basic: external_libraries firebird_boot basic_targets
 
 firebird_boot: updateBuildNum boot_phase1 boot_phase2 
 
@@ -197,8 +203,6 @@ ifeq ($(EDITLINE_FLG),Y)
 endif
 endif
 
-btyacc_binary: $(BTYACC)
-
 $(BTYACC): 
 	$(MAKE) -C $(ROOT)/extern/btyacc
 
@@ -225,17 +229,12 @@ boot_phase1: $(GPRE_STATIC)
 $(GPRE_STATIC):
 	$(MAKE) boot_phase1_build
 
-boot_phase1_build: gpre_boot libfbstatic gpre_static
-
-
-gpre_boot:
-	$(MAKE) $(CPU_OPTION) -f $(GEN_ROOT)/Makefile.boot.gpre $@
-
-libfbstatic:
-	$(MAKE) $(CPU_OPTION) -f $(GEN_ROOT)/Makefile.libfbstatic $@
-
-gpre_static:
-	$(MAKE) $(CPU_OPTION) -f $(GEN_ROOT)/Makefile.static.gpre $@
+boot_phase1_build: boot_phase1_build.stamp
+boot_phase1_build.stamp: $(BTYACC)
+	$(MAKE) $(CPU_OPTION) -f $(GEN_ROOT)/Makefile.boot.gpre gpre_boot
+	$(MAKE) $(CPU_OPTION) -f $(GEN_ROOT)/Makefile.libfbstatic libfbstatic
+	$(MAKE) $(CPU_OPTION) -f $(GEN_ROOT)/Makefile.static.gpre gpre_static
+	touch $@
 
 
 
@@ -257,23 +256,23 @@ gpre_static:
 
 .PHONY: create_db empty_db gbak_static isql_static ref_databases
 
-boot_phase2 : libfbstatic $(ISC_GDB)
+boot_phase2 : boot_phase1 libfbstatic $(ISC_GDB)
 
 $(ISC_GDB): create_db empty_db gbak_static isql_static ref_databases
 
-create_db:
+create_db: boot_phase1
 	$(MAKE) -f $(GEN_ROOT)/Makefile.static.createdb
 
-gbak_static:
+gbak_static: boot_phase1 empty_db
 	$(MAKE) $(CPU_OPTION) -f $(GEN_ROOT)/Makefile.static.gbak
 
-isql_static:
+isql_static: boot_phase1 empty_db
 	$(MAKE) $(CPU_OPTION) -f $(GEN_ROOT)/Makefile.static.isql
 
-empty_db :
+empty_db : create_db
 	$(MAKE) -f $(GEN_ROOT)/Makefile.refDatabases $@
 
-ref_databases : $(GBAK_STATIC)
+ref_databases : create_db gbak_static isql_static
 	$(MAKE) -f $(GEN_ROOT)/Makefile.refDatabases $@
 
 
@@ -302,26 +301,26 @@ basic_targets: ref_databases msgs msgs_i
                intl extlib includes examples_cp
 
 # hack to make code regeneration work
-generated_headers :
+generated_headers : firebird_boot
 	$(MAKE) -f $(GEN_ROOT)/Makefile.codes $@
 
-msgs:                  # messages file for user $(FIREBIRD)/firebird.msg
+msgs: firebird_boot    # messages file for user $(FIREBIRD)/firebird.msg
 	$(MAKE) -f $(GEN_ROOT)/Makefile.msgs $@
 
-msgs_intl:             # international version of msgs file
+msgs_intl: msgs intl firebird_boot   # international version of msgs file
 	$(MAKE) -f $(GEN_ROOT)/Makefile.msgs $@
 
-intl:                  # international lang components $(FIREBIRD)/intl/
+intl: firebird_boot    # international lang components $(FIREBIRD)/intl/
 	$(MAKE) $(CPU_OPTION) -f $(GEN_ROOT)/Makefile.intl
 
-extlib:                # external programs than can be called
+extlib: firebird_boot  # external programs than can be called
 	$(MAKE) -f $(GEN_ROOT)/Makefile.extlib
 
                        # distribution header include files
 includes:	include_generic 
 
                        # examples still need a good makefile to ship with them
-examples_cp:
+examples_cp: firebird_boot intl
 	$(MAKE) -f Makefile.examples -C $(GEN_ROOT)/examples/
 
 
@@ -333,37 +332,37 @@ examples_cp:
 classic_targets: $(PLAT_CLASSIC_PRE_TARGET) libfbembed inet_server fb_smp_server embed_gfix embed_gbak embed_isql \
 	embed_gpre libfbclient embed_util embed_gdef embed_qli embed_fbudf $(PLAT_CLASSIC_POST_TARGET)
 
-libfbembed:
+libfbembed: firebird_boot
 	$(MAKE) $(CPU_OPTION) -f $(GEN_ROOT)/Makefile.libfbembed $@
 
-inet_server:
+inet_server: firebird_boot libfbembed
 	$(MAKE) $(CPU_OPTION) -f $(GEN_ROOT)/Makefile.inet_server
 
-fb_smp_server:
+fb_smp_server: firebird_boot libfbembed
 	$(MAKE) $(CPU_OPTION) -f $(GEN_ROOT)/Makefile.smp_server
 
-embed_gfix:
+embed_gfix: firebird_boot libfbembed
 	$(MAKE) $(CPU_OPTION) -f $(GEN_ROOT)/Makefile.embed.gfix
 
-embed_gbak:
+embed_gbak: firebird_boot libfbembed
 	$(MAKE) $(CPU_OPTION) -f $(GEN_ROOT)/Makefile.embed.gbak
 
-embed_isql:
+embed_isql: firebird_boot libfbembed
 	$(MAKE) $(CPU_OPTION) -f $(GEN_ROOT)/Makefile.embed.isql
 
-embed_gpre:
+embed_gpre: firebird_boot libfbembed
 	$(MAKE) $(CPU_OPTION) -f $(GEN_ROOT)/Makefile.embed.gpre
 
-embed_util:
+embed_util: firebird_boot libfbembed
 	$(MAKE) $(CPU_OPTION) -f $(GEN_ROOT)/Makefile.embed.util	
 
-embed_gdef:
+embed_gdef: firebird_boot libfbembed
 	$(MAKE) $(CPU_OPTION) -f $(GEN_ROOT)/Makefile.embed.gdef
 
-embed_qli:
+embed_qli: firebird_boot libfbembed
 	$(MAKE) $(CPU_OPTION) -f $(GEN_ROOT)/Makefile.embed.qli
 
-embed_fbudf:
+embed_fbudf: firebird_boot libfbembed
 	$(MAKE) $(CPU_OPTION) -f $(GEN_ROOT)/Makefile.embed.fbudf
 
 #_ Firebird Server Targets (super and super client)__________________________
@@ -374,40 +373,40 @@ embed_fbudf:
 super_targets: $(PLAT_SUPER_PRE_TARGET) fbserver libfbclient client_gfix client_gbak client_gsec\
 			client_isql client_gpre client_util client_fbudf client_gdef client_qli $(PLAT_SUPER_POST_TRAGET) 
 
-fbserver:
+fbserver: firebird_boot
 	$(MAKE) $(CPU_OPTION) -f $(GEN_ROOT)/Makefile.fbserver $@
 
-libfbclient:
+libfbclient: firebird_boot
 	$(MAKE) $(CPU_OPTION) -f $(GEN_ROOT)/Makefile.libfbclient $@
 
-client_gfix:
+client_gfix: libfbclient
 	$(MAKE) $(CPU_OPTION) -f $(GEN_ROOT)/Makefile.client.gfix
 
-client_gbak:
+client_gbak: libfbclient
 	$(MAKE) $(CPU_OPTION) -f $(GEN_ROOT)/Makefile.client.gbak
 
-client_gsec:
+client_gsec: libfbclient
 	$(MAKE) $(CPU_OPTION) -f $(GEN_ROOT)/Makefile.client.gsec
 
-client_gpre:
+client_gpre: libfbclient
 	$(MAKE) $(CPU_OPTION) -f $(GEN_ROOT)/Makefile.client.gpre
 
-client_isql:
+client_isql: libfbclient
 	$(MAKE) $(CPU_OPTION) -f $(GEN_ROOT)/Makefile.client.isql
 
-client_util:
+client_util: libfbclient
 	$(MAKE) $(CPU_OPTION) -f $(GEN_ROOT)/Makefile.client.util	
 
-client_fbudf:
+client_fbudf: firebird_boot
 	$(MAKE) $(CPU_OPTION) -f $(GEN_ROOT)/Makefile.client.fbudf
 
 # Not sure we need this target in super - problems with WAL includes
 # MOD 04-Oct-2002
 
-client_gdef:
+client_gdef: libfbclient
 	$(MAKE) $(CPU_OPTION) -f $(GEN_ROOT)/Makefile.client.gdef
 
-client_qli:
+client_qli: libfbclient
 	$(MAKE) $(CPU_OPTION) -f $(GEN_ROOT)/Makefile.client.qli
 
 
@@ -418,7 +417,7 @@ client_qli:
 
 #---------------------------------------------------------------------------
 
-fbtrace:
+fbtrace: libfbclient
 	$(MAKE) $(CPU_OPTION) -f $(GEN_ROOT)/Makefile.fbtrace
 
 
@@ -505,6 +504,7 @@ $(SrcTarFile) :
 
 clean: clean_objects clean_dependancies clean_extern_objects clean_build \
 		clean_yacc_gen clean_gpre_gen clean_icu clean_dbs clean_examples
+	$(RM) boot_phase1_build.stamp
 
 ifeq ($(EDITLINE_FLG),Y)
 ifeq ($(STD_EDITLINE),false)
--- a/builds/posix/Makefile.in.libfbstatic
+++ b/builds/posix/Makefile.in.libfbstatic
@@ -57,7 +57,7 @@ PARSE_Sources = $(addprefix $(SRC_ROOT)/
 # will not be different from the above fbmem_boot.a library
 # compile time macros being the main (if there) difference
 
-libfbstatic: $(PARSE_Sources) $(LIB)/libfbstatic.a
+libfbstatic: $(LIB)/libfbstatic.a
 
 $(LIB)/libfbstatic.a:	$(LIBFBSTATIC_Objects)
 	-$(RM) $@
@@ -65,6 +65,7 @@ $(LIB)/libfbstatic.a:	$(LIBFBSTATIC_Obje
 	-$(RANLIB) $@
 	$(CHMOD_6) $@
 
+$(LIBFBSTATIC_Objects): $(PARSE_Sources)
 
 include $(ROOT)/gen/make.shared.targets
 
--- a/builds/posix/Makefile.in.examples
+++ b/builds/posix/Makefile.in.examples
@@ -87,7 +87,7 @@ all: examples
 # examples: $(EMPLOYEE_DB) $(INTLEMP_DB) $(FIREBIRD)/examples/README
 examples: $(EMPLOYEE_DB) $(FIREBIRD)/examples/README
 
-$(FIREBIRD)/examples/README:
+$(FIREBIRD)/examples/README: $(EMPLOYEE_DB)
 	$(CP) $(ROOT)/examples/readme $(FIREBIRD)/examples/README
 	$(CP) $(ROOT)/examples/*.* $(FIREBIRD)/examples/
 	$(CP) $(ROOT)/examples/api/*.* $(FIREBIRD)/examples/api/
--- a/builds/posix/Makefile.in.client.gfix
+++ b/builds/posix/Makefile.in.client.gfix
@@ -54,7 +54,7 @@ Dependencies = $(AllObjects:.o=.d)
 .PHONY: gfix
 
 
-gfix : $(LIBFBCLIENT_SO) $(GFIX)
+gfix : $(GFIX)
 
 $(GFIX):	$(AllObjects)
 	$(EXE_LINK) $(LINK_EMPTY_SYMBOLS) $(LINK_OPTS) $^ -o $@ -L$(LIB) -lfbclient $(LINK_LIBS)
--- a/builds/posix/Makefile.in.extlib
+++ b/builds/posix/Makefile.in.extlib
@@ -73,9 +73,9 @@ all: lib_ib_util lib_ib_udf
 # User defined functions.  (I've put the .$(SHRLIB_EXT) extension back onto the library)
 # 
 
-lib_ib_udf: $(LIBIBUTIL_SO) $(UDF)/ib_udf.$(SHRLIB_EXT)
+lib_ib_udf: $(UDF)/ib_udf.$(SHRLIB_EXT)
 
-$(UDF)/ib_udf.$(SHRLIB_EXT): $(UDF_Objects)
+$(UDF)/ib_udf.$(SHRLIB_EXT): $(UDF_Objects) $(LIBIBUTIL_SO)
 	$(call LINK_UDF,ib_udf) -o $@ $^ $(LINK_UDF_LIBS)
 
 # ib_util
