diff --git a/Makefile b/Makefile index 6fe257a..e11d690 100644 --- a/Makefile +++ b/Makefile @@ -22,6 +22,37 @@ DATA += $(filter-out $(EXTENSION_VERSION_FILES), $(versioned_out)) all: $B/cat_tools.sql $(versioned_out) installcheck: $B/cat_tools.sql $(versioned_out) EXTRA_CLEAN += $B/cat_tools.sql $(filter-out $(EXTENSION_VERSION_FILES), $(versioned_out)) +# Also clean the generated .sql.in for the current version +EXTRA_CLEAN += $(EXTENSION_VERSION_FILES:.sql=.sql.in) + +# Temporary ugly hack for 9.x — remove these two blocks when 9.x support is dropped. +# $@ is deferred via = and expands to the target name at recipe time. +ifeq ($(LT95),yes) +_sql_sed_95 = pgxntool/safesed $@.tmp -E -e 's/(.*)-- SED: REQUIRES 9\.5!/-- Requires 9.5: \1/' +else +_sql_sed_95 = pgxntool/safesed $@.tmp -E -e 's/(.*)-- SED: PRIOR TO 9\.5!/-- Not used prior to 9.5: \1/' +endif +ifeq ($(LT93),yes) +_sql_sed_93 = pgxntool/safesed $@.tmp -E -e 's/(.*)-- SED: REQUIRES 9\.3!/-- Requires 9.3: \1/' +else +_sql_sed_93 = pgxntool/safesed $@.tmp -E -e 's/(.*)-- SED: PRIOR TO 9\.3!/-- Not used prior to 9.3: \1/' +endif + +# Apply all version-conditional SED markers to $@.tmp. +# 9.x handled by the above variables (temporary hack, to be removed with 9.x support). +# 10+ handled generically via awk: REQUIRES N → commented if MAJORVER < N*10; +# PRIOR TO N → commented if MAJORVER >= N*10. +# IMPORTANT: Use only POSIX awk features here (no gawk extensions like gensub(), +# 3-arg match(), etc.) — awk availability and compatibility across platforms is +# the whole reason this approach was chosen over sed. +define _apply_version_seds + $(_sql_sed_95) + $(_sql_sed_93) + awk -v mv=$(MAJORVER) '\ + /-- SED: REQUIRES [1-9][0-9]+!/ {t=$$0; sub(/.*REQUIRES /,"",t); sub(/!.*/,"",t); if(mv=t*10) $$0="-- Not used prior to "t": "$$0}\ + {print}' $@.tmp > $@.tmp2 && mv $@.tmp2 $@.tmp +endef # TODO: refactor the version stuff into a function # @@ -35,16 +66,23 @@ EXTRA_CLEAN += $B/cat_tools.sql $(filter-out $(EXTENSION_VERSION_FILES), $(versi #.PRECIOUS: $B/cat_tools.sql $B/%.sql: sql/%.sql.in pgxntool/safesed (echo @generated@ && cat $< && echo @generated@) | sed -e 's#@generated@#-- GENERATED FILE! DO NOT EDIT! See $<#' > $@.tmp -ifeq ($(LT95),yes) - pgxntool/safesed $@.tmp -E -e 's/(.*)-- SED: REQUIRES 9.5!/-- Requires 9.5: \1/' -else - pgxntool/safesed $@.tmp -E -e 's/(.*)-- SED: PRIOR TO 9.5!/-- Not used prior to 9.5: \1/' -endif -ifeq ($(LT93),yes) - pgxntool/safesed $@.tmp -E -e 's/(.*)-- SED: REQUIRES 9.3!/-- Requires 9.3: \1/' -else - pgxntool/safesed $@.tmp -E -e 's/(.*)-- SED: PRIOR TO 9.3!/-- Not used prior to 9.3: \1/' -endif + $(_apply_version_seds) + mv $@.tmp $@ + +# Generate the current version's .sql.in by copying the base source. +# This intermediate file is then processed by the pattern rule above to produce +# the final .sql with version-conditional SED substitutions applied. +# (EXTENSION_VERSION_FILES is just sql/cat_tools--.sql) +$(EXTENSION_VERSION_FILES:.sql=.sql.in): sql/cat_tools.sql.in cat_tools.control + cp $< $@ + +# Override the control.mk rule that builds EXTENSION_VERSION_FILES directly from +# cat_tools.sql (bypassing SED processing). Instead, build from the .sql.in above +# so that version-conditional substitutions (-- SED: REQUIRES X!) are applied. +# Note: GNU Make will emit "overriding recipe" for this target — that is expected. +$(EXTENSION_VERSION_FILES): $(EXTENSION_VERSION_FILES:.sql=.sql.in) pgxntool/safesed + (echo @generated@ && cat $< && echo @generated@) | sed -e 's#@generated@#-- GENERATED FILE! DO NOT EDIT! See $<#' > $@.tmp + $(_apply_version_seds) mv $@.tmp $@ # Support for upgrade test @@ -72,10 +110,12 @@ $(TEST_BUILD_DIR)/active.sql: $(TEST_BUILD_DIR)/dep.mk $(TEST_BUILD_DIR)/$(TEST_ ln -sf $(TEST_LOAD_SOURCE).sql $@ $(TEST_BUILD_DIR)/upgrade.sql: test/load_upgrade.sql $(TEST_BUILD_DIR) old_version - (echo @generated@ && cat $< && echo @generated@) | sed -e 's#@generated@#-- GENERATED FILE! DO NOT EDIT! See $<#' > $@ + (echo @generated@ && cat $< && echo @generated@) | sed -e 's#@generated@#-- GENERATED FILE! DO NOT EDIT! See $<#' > $@.tmp + mv $@.tmp $@ $(TEST_BUILD_DIR)/new.sql: test/load_new.sql $(TEST_BUILD_DIR) - (echo @generated@ && cat $< && echo @generated@) | sed -e 's#@generated@#-- GENERATED FILE! DO NOT EDIT! See $<#' > $@ + (echo @generated@ && cat $< && echo @generated@) | sed -e 's#@generated@#-- GENERATED FILE! DO NOT EDIT! See $<#' > $@.tmp + mv $@.tmp $@ # TODO: figure out vpath EXTRA_CLEAN += $(TEST_BUILD_DIR)/ diff --git a/sql/.gitignore b/sql/.gitignore index 55e43ad..8898c91 100644 --- a/sql/.gitignore +++ b/sql/.gitignore @@ -1,6 +1,7 @@ +# cat_tools.sql is generated from cat_tools.sql.in by `make` and should not be committed. cat_tools.sql -# The following are generated by make from their .sql.in counterparts. -# Versions 0.1.0-0.1.5 have no .sql.in (pre-dates the conditional SED system -# or only pre-built SQL was published to PGXN), so they are committed as-is. -cat_tools--0.2.0.sql -cat_tools--0.2.1.sql +# Versioned .sql files are generated from .sql.in sources; ignore by default. +cat_tools--*.sql +# Historical exceptions — no .sql.in source; must be tracked directly. +!cat_tools--0.1.*.sql +!cat_tools--0.2.0--0.2.1.sql