From 386bab39b951a71336c57611f8ff10977995ec9f Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Wed, 27 Oct 2021 09:59:33 -0700
Subject: [PATCH v5 12/16] prereq: make unicode targets work in vpath builds.

---
 contrib/unaccent/Makefile                         |  4 ++--
 src/common/unicode/Makefile                       |  8 ++++----
 src/common/unicode/generate-unicode_norm_table.pl | 11 ++++++-----
 3 files changed, 12 insertions(+), 11 deletions(-)

diff --git a/contrib/unaccent/Makefile b/contrib/unaccent/Makefile
index b8307d1601e..d6c466e07ad 100644
--- a/contrib/unaccent/Makefile
+++ b/contrib/unaccent/Makefile
@@ -27,12 +27,12 @@ include $(top_builddir)/src/Makefile.global
 include $(top_srcdir)/contrib/contrib-global.mk
 endif
 
-update-unicode: unaccent.rules
+update-unicode: $(srcdir)/unaccent.rules
 
 # Allow running this even without --with-python
 PYTHON ?= python
 
-unaccent.rules: generate_unaccent_rules.py ../../src/common/unicode/UnicodeData.txt Latin-ASCII.xml
+$(srcdir)/unaccent.rules: generate_unaccent_rules.py ../../src/common/unicode/UnicodeData.txt Latin-ASCII.xml
 	$(PYTHON) $< --unicode-data-file $(word 2,$^) --latin-ascii-file $(word 3,$^) >$@
 
 # Only download it once; dependencies must match src/common/unicode/
diff --git a/src/common/unicode/Makefile b/src/common/unicode/Makefile
index a3683dd86b9..40a5f7bc0fe 100644
--- a/src/common/unicode/Makefile
+++ b/src/common/unicode/Makefile
@@ -12,14 +12,14 @@ subdir = src/common/unicode
 top_builddir = ../../..
 include $(top_builddir)/src/Makefile.global
 
-override CPPFLAGS := -DFRONTEND $(CPPFLAGS)
+override CPPFLAGS := -DFRONTEND -I$(abs_top_builddir)/src/common/unicode $(CPPFLAGS)
 LIBS += $(PTHREAD_LIBS)
 
 # By default, do nothing.
 all:
 
 update-unicode: unicode_norm_table.h unicode_combining_table.h unicode_east_asian_fw_table.h unicode_normprops_table.h unicode_norm_hashfunc.h
-	mv $^ ../../../src/include/common/
+	mv $^ $(top_srcdir)/src/include/common/
 	$(MAKE) normalization-check
 
 # These files are part of the Unicode Character Database. Download
@@ -33,7 +33,7 @@ UnicodeData.txt EastAsianWidth.txt DerivedNormalizationProps.txt CompositionExcl
 unicode_norm_hashfunc.h: unicode_norm_table.h
 
 unicode_norm_table.h: generate-unicode_norm_table.pl UnicodeData.txt CompositionExclusions.txt
-	$(PERL) generate-unicode_norm_table.pl
+	$(PERL) $< $(CURDIR)
 
 unicode_combining_table.h: generate-unicode_combining_table.pl UnicodeData.txt
 	$(PERL) $^ >$@
@@ -58,7 +58,7 @@ submake-common:
 	$(MAKE) -C .. all
 
 norm_test_table.h: generate-norm_test_table.pl NormalizationTest.txt
-	perl generate-norm_test_table.pl NormalizationTest.txt $@
+	perl $^ $@
 
 .PHONY: normalization-check
 
diff --git a/src/common/unicode/generate-unicode_norm_table.pl b/src/common/unicode/generate-unicode_norm_table.pl
index 114ab30d3f1..4d2c603ff27 100644
--- a/src/common/unicode/generate-unicode_norm_table.pl
+++ b/src/common/unicode/generate-unicode_norm_table.pl
@@ -15,15 +15,16 @@ use FindBin;
 use lib "$FindBin::RealBin/../../tools/";
 use PerfectHash;
 
-my $output_table_file = "unicode_norm_table.h";
-my $output_func_file  = "unicode_norm_hashfunc.h";
+my $directory = $ARGV[0];
+my $output_table_file = "$directory/unicode_norm_table.h";
+my $output_func_file  = "$directory/unicode_norm_hashfunc.h";
 
 my $FH;
 
 # Read list of codes that should be excluded from re-composition.
 my @composition_exclusion_codes = ();
-open($FH, '<', "CompositionExclusions.txt")
-  or die "Could not open CompositionExclusions.txt: $!.";
+open($FH, '<', "$directory/CompositionExclusions.txt")
+  or die "Could not open $directory/CompositionExclusions.txt: $!.";
 while (my $line = <$FH>)
 {
 	if ($line =~ /^([[:xdigit:]]+)/)
@@ -38,7 +39,7 @@ close $FH;
 # and character decomposition mapping
 my @characters     = ();
 my %character_hash = ();
-open($FH, '<', "UnicodeData.txt")
+open($FH, '<', "$directory/UnicodeData.txt")
   or die "Could not open UnicodeData.txt: $!.";
 while (my $line = <$FH>)
 {
-- 
2.23.0.385.gbc12974a89

