commit 52f721604c4c181c625329d099e746c87fa5fd8a
Author: Tom Lane <tgl@sss.pgh.pa.us>
Date:   Thu Feb 9 13:05:20 2023 -0500

    Integrate pg_bsd_indent into our build infrastructure.
    
    We don't want to build this by default, and certainly not install
    it by default, but the directory should be subject to cleaning.
    Also update the Makefile and build directions for in-tree build.
    
    This patch doesn't address building it with Meson, but somebody
    might like to add that.
    
    Also, exclude it from pgindent's purview; at least for now,
    we'll leave it formatted similarly to the FreeBSD original.
    
    Discussion: https://postgr.es/m/20200812223409.6di3y2qsnvynao7a@alap3.anarazel.de

diff --git a/src/Makefile b/src/Makefile
index 79e274a476..94649c36c7 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -66,11 +66,13 @@ clean:
 	$(MAKE) -C test $@
 	$(MAKE) -C tutorial NO_PGXS=1 $@
 	$(MAKE) -C test/isolation $@
+	$(MAKE) -C tools/pg_bsd_indent $@
 
 distclean maintainer-clean:
 	$(MAKE) -C test $@
 	$(MAKE) -C tutorial NO_PGXS=1 $@
 	$(MAKE) -C test/isolation $@
+	$(MAKE) -C tools/pg_bsd_indent $@
 	rm -f Makefile.port Makefile.global
 
 
diff --git a/src/tools/pg_bsd_indent/.gitignore b/src/tools/pg_bsd_indent/.gitignore
index 4c5d8dc691..f9393d4452 100644
--- a/src/tools/pg_bsd_indent/.gitignore
+++ b/src/tools/pg_bsd_indent/.gitignore
@@ -1,9 +1,3 @@
-# Global excludes across all subdirectories
-*.o
-*.obj
-*.exe
-
-# Local excludes in root directory
 /pg_bsd_indent
 /*.out
 /*.list
diff --git a/src/tools/pg_bsd_indent/Makefile b/src/tools/pg_bsd_indent/Makefile
index ee046f36f0..3bd50b4e1c 100644
--- a/src/tools/pg_bsd_indent/Makefile
+++ b/src/tools/pg_bsd_indent/Makefile
@@ -1,33 +1,56 @@
 #-------------------------------------------------------------------------
 #
-# Makefile for pg_bsd_indent
+# src/tools/pg_bsd_indent/Makefile
 #
-# Copyright (c) 2017, PostgreSQL Global Development Group
+# Copyright (c) 2017-2023, PostgreSQL Global Development Group
 #
 #-------------------------------------------------------------------------
 
 PGFILEDESC = "pg_bsd_indent - indent C code nicely"
 PGAPPICON = win32
 
-PROGRAM = pg_bsd_indent
-OBJS	= args.o err.o indent.o io.o lexi.o parse.o pr_comment.o $(WIN32RES)
+subdir = src/tools/pg_bsd_indent
+top_builddir = ../../..
+include $(top_builddir)/src/Makefile.global
 
-# clean junk left behind by "make test"
-EXTRA_CLEAN = *.out *.list tests.diff
+OBJS = \
+	$(WIN32RES) \
+	args.o \
+	err.o \
+	indent.o \
+	io.o \
+	lexi.o \
+	parse.o \
+	pr_comment.o
 
-PG_CONFIG = pg_config
-PGXS := $(shell $(PG_CONFIG) --pgxs)
-include $(PGXS)
+all: pg_bsd_indent
 
-# pgxs.mk assumes too much about what "make check" means, so call it "test"
+pg_bsd_indent: $(OBJS) | submake-libpgport
+	$(CC) $(CFLAGS) $^ $(LDFLAGS) $(LDFLAGS_EX) $(LIBS) -o $@$(X)
+
+install: all installdirs
+	$(INSTALL_PROGRAM) pg_bsd_indent$(X) '$(DESTDIR)$(bindir)/pg_bsd_indent$(X)'
+
+installdirs:
+	$(MKDIR_P) '$(DESTDIR)$(bindir)'
+
+uninstall:
+	rm -f '$(DESTDIR)$(bindir)/pg_bsd_indent$(X)'
+
+clean distclean maintainer-clean:
+	rm -f pg_bsd_indent$(X) $(OBJS)
+	rm -f *.out *.list tests.diff
+
+# We don't use "make check" because that would insist on building
+# all of the surrounding Postgres installation.
 .PHONY: test
 
-test: $(PROGRAM)
+test: pg_bsd_indent
 	@rm -f tests.diff
 	@cp $(srcdir)/tests/*.list .
 	@for testsrc in $(srcdir)/tests/*.0; do \
 		test=`basename "$$testsrc" .0`; \
-		./$(PROGRAM) $$testsrc $$test.out -P$(srcdir)/tests/$$test.pro || echo FAILED >>$$test.out; \
+		./pg_bsd_indent $$testsrc $$test.out -P$(srcdir)/tests/$$test.pro || echo FAILED >>$$test.out; \
 		diff -u $$testsrc.stdout $$test.out >>tests.diff 2>&1 || true; \
 	done
 	@cat tests.diff
diff --git a/src/tools/pg_bsd_indent/README b/src/tools/pg_bsd_indent/README
index 846855d318..0aa3b69fe6 100644
--- a/src/tools/pg_bsd_indent/README
+++ b/src/tools/pg_bsd_indent/README
@@ -1,3 +1,36 @@
+src/tools/pg_bsd_indent/README
+
+This is a lightly modified version of the "indent" program maintained
+by the FreeBSD project.  The modifications are mostly to make it portable
+to non-BSD-ish platforms, though we do have one formatting switch we
+couldn't convince upstream to take.
+
+To build it, configure the surrounding Postgres source tree,
+then run "make" in this directory.
+Optionally, run "make test" for some simple sanity checks.
+
+You'll need to install pg_bsd_indent somewhere in your PATH before
+using it.  Most likely, if you're a developer, you don't want to
+put it in the same place as where the surrounding Postgres build
+gets installed.  Therefore, do this part with something like
+
+	make install prefix=/usr/local
+
+TODO: add build support and instructions for Windows
+
+
+If you happen to be hacking upon the indent source code, the closest
+approximation to the existing indentation style seems to be
+
+	./pg_bsd_indent -i4 -l79 -di12 -nfc1 -nlp -sac somefile.c
+
+although this has by no means been rigorously adhered to.
+(What was that saw about the shoemaker's children?)
+We're not planning to re-indent to Postgres style, because that
+would make it difficult to compare to the FreeBSD sources.
+
+----------
+
 The FreeBSD originals of the files in this directory bear the
 "4-clause" version of the BSD license.  We have removed the
 "advertising" clauses, as per UC Berkeley's directive here:
diff --git a/src/tools/pg_bsd_indent/README.pg_bsd_indent b/src/tools/pg_bsd_indent/README.pg_bsd_indent
deleted file mode 100644
index 85c3dcac1c..0000000000
--- a/src/tools/pg_bsd_indent/README.pg_bsd_indent
+++ /dev/null
@@ -1,30 +0,0 @@
-pg_bsd_indent
-
-This is a lightly modified version of the "indent" program maintained
-by the FreeBSD project.  The modifications are mostly to make it portable
-to non-BSD-ish platforms, though we do have one formatting switch we
-couldn't convince upstream to take.
-
-To build it, you will need a Postgres installation, version 9.5 or newer.
-(Once built, the program doesn't depend on that installation.)
-
-To build, just say "make"; or if pg_config from your Postgres installation
-isn't in your PATH, say
-	make PG_CONFIG=path/to/pg_config
-Optionally, run "make test" for some simple sanity checks.
-
-To install, copy pg_bsd_indent to somewhere in your usual PATH.
-(If you say "make install", it will try to put it in your Postgres
-installation directory, which is most likely not what you want for
-long-term use.)
-
-TODO: add build support and instructions for Windows
-
-
-If you happen to be hacking upon the indent source code, the closest
-approximation to the existing indentation style seems to be
-
-	./pg_bsd_indent -i4 -l79 -di12 -nfc1 -nlp -sac somefile.c
-
-although this has by no means been rigorously adhered to.
-(What was that saw about the shoemaker's children?)
diff --git a/src/tools/pgindent/exclude_file_patterns b/src/tools/pgindent/exclude_file_patterns
index f5c8857e31..6405a00511 100644
--- a/src/tools/pgindent/exclude_file_patterns
+++ b/src/tools/pgindent/exclude_file_patterns
@@ -47,6 +47,10 @@ src/pl/plperl/ppport\.h$
 src/pl/plperl/SPI\.c$
 src/pl/plperl/Util\.c$
 #
+# pg_bsd_indent has its own, idiosyncratic indentation style.
+# We'll stick to that to permit comparison with the FreeBSD upstream.
+src/tools/pg_bsd_indent/.*
+#
 # Exclude any temporary installations that may be in the tree.
 /tmp_check/
 /tmp_install/
