From 578fbb407ce89293c8c955179e61b10aadc1709f Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Thu, 24 Oct 2024 07:23:05 -0400
Subject: [PATCH v4 1/7] meson: Add generated header stamps

Otherwise build commands become too long and this has visible effect on
creation time of meson build files.

Author: Andres Freund <andres@anarazel.de>
Author: Nazir Bilal Yavuz <byavuz81@gmail.com>
Discussion: https://postgr.es/m/206b001d-1884-4081-bd02-bed5c92f02ba%40eisentraut.org
---
 src/include/meson.build  | 18 ++++++++++++++++++
 src/backend/meson.build  |  2 +-
 src/fe_utils/meson.build |  2 +-
 meson.build              | 16 +++++++++-------
 4 files changed, 29 insertions(+), 9 deletions(-)

diff --git a/src/include/meson.build b/src/include/meson.build
index 2e4b7aa529e..c488a5dc4c9 100644
--- a/src/include/meson.build
+++ b/src/include/meson.build
@@ -177,3 +177,21 @@ install_subdir('catalog',
 
 # autoconf generates the file there, ensure we get a conflict
 generated_sources_ac += {'src/include': ['stamp-h']}
+
+# Instead of having targets depending directly on the generated headers, have
+# them depend on a stamp files for all of them. Dependencies on headers are
+# implemented as order-only dependencies in meson (later using compiler
+# generated dependencies). The benefit of using a stamp file is that it makes
+# ninja.build smaller and meson setup faster.
+generated_headers_stamp = custom_target('generated-headers-stamp.h',
+  output: 'generated-headers-stamp.h',
+  input: generated_headers,
+  command: stamp_cmd,
+)
+
+generated_backend_headers_stamp = custom_target('generated-backend-headers-stamp.h',
+  output: 'generated-backend-headers-stamp.h',
+  input: generated_backend_headers,
+  depends: generated_headers_stamp,
+  command: stamp_cmd,
+)
diff --git a/src/backend/meson.build b/src/backend/meson.build
index 2b0db214804..7fc649c3ebd 100644
--- a/src/backend/meson.build
+++ b/src/backend/meson.build
@@ -169,7 +169,7 @@ backend_mod_code = declare_dependency(
   compile_args: pg_mod_c_args,
   include_directories: postgres_inc,
   link_args: pg_mod_link_args,
-  sources: generated_headers + generated_backend_headers,
+  sources: [generated_backend_headers_stamp],
   dependencies: backend_mod_deps,
 )
 
diff --git a/src/fe_utils/meson.build b/src/fe_utils/meson.build
index a18cbc939e4..5a9ddb73463 100644
--- a/src/fe_utils/meson.build
+++ b/src/fe_utils/meson.build
@@ -29,7 +29,7 @@ generated_sources += psqlscan
 fe_utils_sources += psqlscan
 
 fe_utils = static_library('libpgfeutils',
-  fe_utils_sources + generated_headers,
+  fe_utils_sources,
   c_pch: pch_postgres_fe_h,
   include_directories: [postgres_inc, libpq_inc],
   c_args: host_system == 'windows' ? ['-DFD_SETSIZE=1024'] : [],
diff --git a/meson.build b/meson.build
index 13c13748e5d..9bfa96ad255 100644
--- a/meson.build
+++ b/meson.build
@@ -2973,6 +2973,8 @@ gen_export_kwargs = {
   'install': false,
 }
 
+# command to create stamp files on all OSs
+stamp_cmd = [python, '-c', 'import sys; open(sys.argv[1], "w")', '@OUTPUT0@']
 
 
 ###
@@ -3090,14 +3092,14 @@ subdir('src/port')
 frontend_common_code = declare_dependency(
   compile_args: ['-DFRONTEND'],
   include_directories: [postgres_inc],
-  sources: generated_headers,
+  sources: generated_headers_stamp,
   dependencies: [os_deps, zlib, zstd, lz4],
 )
 
 backend_common_code = declare_dependency(
   compile_args: ['-DBUILDING_DLL'],
   include_directories: [postgres_inc],
-  sources: generated_headers,
+  sources: generated_headers_stamp,
   dependencies: [os_deps, zlib, zstd],
 )
 
@@ -3112,7 +3114,7 @@ shlib_code = declare_dependency(
 frontend_stlib_code = declare_dependency(
   include_directories: [postgres_inc],
   link_with: [common_static, pgport_static],
-  sources: generated_headers,
+  sources: generated_headers_stamp,
   dependencies: [os_deps, libintl],
 )
 
@@ -3120,7 +3122,7 @@ frontend_stlib_code = declare_dependency(
 frontend_shlib_code = declare_dependency(
   include_directories: [postgres_inc],
   link_with: [common_shlib, pgport_shlib],
-  sources: generated_headers,
+  sources: generated_headers_stamp,
   dependencies: [shlib_code, os_deps, libintl],
 )
 
@@ -3130,7 +3132,7 @@ frontend_shlib_code = declare_dependency(
 frontend_no_fe_utils_code = declare_dependency(
   include_directories: [postgres_inc],
   link_with: [common_static, pgport_static],
-  sources: generated_headers,
+  sources: generated_headers_stamp,
   dependencies: [os_deps, libintl],
 )
 
@@ -3156,7 +3158,7 @@ subdir('src/fe_utils')
 frontend_code = declare_dependency(
   include_directories: [postgres_inc],
   link_with: [fe_utils, common_static, pgport_static],
-  sources: generated_headers,
+  sources: generated_headers_stamp,
   dependencies: [os_deps, libintl],
 )
 
@@ -3184,7 +3186,7 @@ backend_code = declare_dependency(
   include_directories: [postgres_inc],
   link_args: ldflags_be,
   link_with: [],
-  sources: generated_headers + generated_backend_headers,
+  sources: [generated_backend_headers_stamp],
   dependencies: os_deps + backend_both_deps + backend_deps,
 )
 
-- 
2.47.2

