From a739950d06678f28f08deb48d080939041dc5584 Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Wed, 15 Mar 2023 16:29:27 -0700
Subject: [PATCH v1 2/5] meson: make install_test_files more generic, rename to
 install_files

Now it supports installing directories and directory contents as well. This
will be used in a subsequent patch to install doc contents.
---
 meson.build                  |  5 ++-
 src/tools/install_files      | 71 ++++++++++++++++++++++++++++++++++++
 src/tools/install_test_files | 33 -----------------
 3 files changed, 74 insertions(+), 35 deletions(-)
 create mode 100644 src/tools/install_files
 delete mode 100644 src/tools/install_test_files

diff --git a/meson.build b/meson.build
index 2ebdf914c1b..b4b87f1a9fc 100644
--- a/meson.build
+++ b/meson.build
@@ -369,6 +369,8 @@ flex_cmd = [python, flex_wrapper,
 wget = find_program('wget', required: false, native: true)
 wget_flags = ['-O', '@OUTPUT0@', '--no-use-server-timestamps']
 
+install_files = files('src/tools/install_files')
+
 
 
 ###############################################################
@@ -2859,9 +2861,8 @@ testprep_targets += test_install_libs
 
 
 # command to install files used for tests, which aren't installed by default
-install_test_files = files('src/tools/install_test_files')
 install_test_files_args = [
-  install_test_files,
+  install_files,
   '--prefix', dir_prefix,
   '--install', contrib_data_dir, test_install_data,
   '--install', dir_lib_pkg, test_install_libs,
diff --git a/src/tools/install_files b/src/tools/install_files
new file mode 100644
index 00000000000..23ffd0aa2f7
--- /dev/null
+++ b/src/tools/install_files
@@ -0,0 +1,71 @@
+#!/usr/bin/env python3
+
+# Helper to install additional files into the temporary installation
+# for tests, beyond those that are installed by meson/ninja install.
+
+import argparse
+import shutil
+import os
+from pathlib import PurePath
+
+parser = argparse.ArgumentParser()
+
+parser.add_argument('--destdir', type=str,
+                    default=os.environ.get('DESTDIR', None))
+parser.add_argument('--prefix', type=str)
+parser.add_argument('--install', type=str, nargs='+',
+                    action='append', default=[])
+parser.add_argument('--install-dirs', type=str, nargs='+',
+                    action='append', default=[])
+parser.add_argument('--install-dir-contents', type=str, nargs='+',
+                    action='append', default=[])
+
+args = parser.parse_args()
+
+
+def error_exit(msg: str):
+    if os.path.isdir(src):
+        error_exit(f"{src} is a directory")
+
+
+def create_target_dir(prefix: str, destdir: str, targetdir: str):
+    if not os.path.isabs(targetdir):
+        targetdir = os.path.join(prefix, targetdir)
+
+    if destdir is not None:
+        # copy of meson's logic for joining destdir and install paths
+        targetdir = str(PurePath(destdir, *PurePath(targetdir).parts[1:]))
+
+    os.makedirs(targetdir, exist_ok=True)
+
+    return targetdir
+
+
+def copy_files(targetdir: str, src_list: list):
+    for src in src_list:
+        shutil.copy2(src, targetdir)
+
+
+def copy_dirs(targetdir: str, src_list: list, contents: bool):
+    for src in src_list:
+        if not os.path.isdir(src):
+            error_exit('{0} is not a directory'.format(src))
+
+        if contents:
+            target = targetdir
+        else:
+            target = os.path.join(targetdir, os.path.split(src)[1])
+        shutil.copytree(src, target, dirs_exist_ok=True)
+
+
+for installs in args.install:
+    targetdir = create_target_dir(args.prefix, args.destdir, installs[0])
+    copy_files(targetdir, installs[1:])
+
+for installs in args.install_dirs:
+    targetdir = create_target_dir(args.prefix, args.destdir, installs[0])
+    copy_dirs(targetdir, installs[1:], contents=False)
+
+for installs in args.install_dir_contents:
+    targetdir = create_target_dir(args.prefix, args.destdir, installs[0])
+    copy_dirs(targetdir, installs[1:], contents=True)
diff --git a/src/tools/install_test_files b/src/tools/install_test_files
deleted file mode 100644
index 8e0b36a74d1..00000000000
--- a/src/tools/install_test_files
+++ /dev/null
@@ -1,33 +0,0 @@
-#!/usr/bin/env python3
-
-# Helper to install additional files into the temporary installation
-# for tests, beyond those that are installed by meson/ninja install.
-
-import argparse
-import shutil
-import os
-from pathlib import PurePath
-
-parser = argparse.ArgumentParser()
-
-parser.add_argument('--destdir', type=str, default=os.environ.get('DESTDIR', None))
-parser.add_argument('--prefix', type=str)
-parser.add_argument('--install', type=str, nargs='+', action='append')
-
-args = parser.parse_args()
-
-def copy_files(prefix: str, destdir: str, targetdir: str, src_list: list):
-    if not os.path.isabs(targetdir):
-        targetdir = os.path.join(prefix, targetdir)
-
-    if destdir is not None:
-        # copy of meson's logic for joining destdir and install paths
-        targetdir = str(PurePath(destdir, *PurePath(targetdir).parts[1:]))
-
-    os.makedirs(targetdir, exist_ok=True)
-
-    for src in src_list:
-        shutil.copy2(src, targetdir)
-
-for installs in args.install:
-    copy_files(args.prefix, args.destdir, installs[0], installs[1:])
-- 
2.38.0

