From aaca6b3d2a0454183599d8f5f0d342d339b8a1a0 Mon Sep 17 00:00:00 2001
From: Jelte Fennema-Nio <postgres@jeltef.nl>
Date: Wed, 31 Dec 2025 10:36:30 +0100
Subject: [PATCH v1 1/3] Add "format" target for make and ninja

Running pgindent manually requires putting pg_bsd_indent in your path.
This allows formatting the sources without having to do that.

In follow on commits this will introduce more tools to these targets,
like perltidy.
---
 GNUmakefile.in                 |  6 +++++-
 doc/src/sgml/targets-meson.txt |  1 +
 meson.build                    | 10 ++++++++++
 src/tools/pgindent/README      | 18 ++++++++++++++----
 4 files changed, 30 insertions(+), 5 deletions(-)

diff --git a/GNUmakefile.in b/GNUmakefile.in
index cf6e759486e..65c2dffba4e 100644
--- a/GNUmakefile.in
+++ b/GNUmakefile.in
@@ -134,4 +134,8 @@ headerscheck: submake-generated-headers
 cpluspluscheck: submake-generated-headers
 	$(top_srcdir)/src/tools/pginclude/headerscheck --cplusplus $(top_srcdir) $(abs_top_builddir)
 
-.PHONY: dist distcheck docs install-docs world check-world install-world installcheck-world headerscheck cpluspluscheck
+format: submake-libpgport
+	@$(MAKE) -C $(top_builddir)/src/tools/pg_bsd_indent
+	$(PERL) $(top_srcdir)/src/tools/pgindent/pgindent --indent=$(top_builddir)/src/tools/pg_bsd_indent/pg_bsd_indent --typedefs=$(top_srcdir)/src/tools/pgindent/typedefs.list --excludes=$(top_srcdir)/src/tools/pgindent/exclude_file_patterns $(top_srcdir)/src $(top_srcdir)/contrib
+
+.PHONY: dist distcheck docs install-docs world check-world install-world installcheck-world headerscheck cpluspluscheck format
diff --git a/doc/src/sgml/targets-meson.txt b/doc/src/sgml/targets-meson.txt
index d0021a5eb10..b9486b9fba0 100644
--- a/doc/src/sgml/targets-meson.txt
+++ b/doc/src/sgml/targets-meson.txt
@@ -14,6 +14,7 @@ Code Targets:
   pl                            Build procedural languages
 
 Developer Targets:
+  format                        Run pgindent on C source files
   reformat-dat-files            Rewrite catalog data files into standard format
   expand-dat-files              Expand all data files to include defaults
   update-unicode                Update unicode data to new version
diff --git a/meson.build b/meson.build
index ec08cd49056..585925ecebc 100644
--- a/meson.build
+++ b/meson.build
@@ -3837,6 +3837,16 @@ run_target('help',
   ]
 )
 
+run_target('format',
+  command: [perl, files('src/tools/pgindent/pgindent'),
+            '--indent', pg_bsd_indent,
+            '--typedefs', files('src/tools/pgindent/typedefs.list'),
+            '--excludes', files('src/tools/pgindent/exclude_file_patterns'),
+            meson.project_source_root() / 'src',
+            meson.project_source_root() / 'contrib'],
+  depends: [pg_bsd_indent],
+)
+
 
 
 ###############################################################
diff --git a/src/tools/pgindent/README b/src/tools/pgindent/README
index b6cd4c6f6b7..17b65c29c7d 100644
--- a/src/tools/pgindent/README
+++ b/src/tools/pgindent/README
@@ -11,9 +11,11 @@ http://adpgtech.blogspot.com/2015/05/running-pgindent-on-non-core-code-or.html
 
 PREREQUISITES:
 
-1) Install pg_bsd_indent in your PATH.  Its source code is in the
-   sibling directory src/tools/pg_bsd_indent; see the directions
-   in that directory's README file.
+1) Install pg_bsd_indent in your PATH.  If you use the "ninja format"
+   or "make format" build targets, they will build pg_bsd_indent for you,
+   so this step can be skipped.  Otherwise, its source code is in the
+   sibling directory src/tools/pg_bsd_indent; see the directions in that
+   directory's README file.
 
 2) Install perltidy.  Please be sure it is version 20230309 (older and newer
    versions make different formatting choices, and we want consistency).
@@ -31,7 +33,15 @@ DOING THE INDENT RUN BEFORE A NORMAL COMMIT:
 
 1) Change directory to the top of the source tree.
 
-2) Run pgindent on the C files:
+2) Run pgindent on the C files.  With Meson:
+
+	ninja -C build format
+
+   Or with make:
+
+	make format
+
+   Or directly:
 
 	src/tools/pgindent/pgindent .
 

base-commit: 915711c8a4e60f606a8417ad033cea5385364c07
-- 
2.52.0

