1#!/usr/bin/env python3
2
3# External command, intended to be called with meson.add_dist_script() in meson.build
4
5#                          argv[1]       argv[2]     argv[3:]
6# dist-build-scripts.py <root_src_dir> <script_dir> <no_dist>...
7
8# <script_dir> The directory with the build scripts, relative to <root_source_dir>.
9# <no_dist> Zero or more names (relative to MESON_PROJECT_DIST_ROOT)
10#           of files and directories that shall not be distributed.
11
12import os
13import sys
14import shutil
15
16# MESON_PROJECT_DIST_ROOT is set only if meson.version() >= 0.58.0.
17project_dist_root = os.getenv('MESON_PROJECT_DIST_ROOT', os.getenv('MESON_DIST_ROOT'))
18src_script_dir = os.path.join(sys.argv[1], sys.argv[2])
19dist_script_dir = os.path.join(project_dist_root, sys.argv[2])
20
21# Create the distribution script directory, if it does not exist.
22os.makedirs(dist_script_dir, exist_ok=True)
23
24# Distribute files that mm-common-get has copied to src_script_dir.
25files = [
26  'check-dllexport-usage.py',
27  'dist-build-scripts.py',
28  'dist-changelog.py',
29  'doc-reference.py',
30  'generate-binding.py'
31]
32for file in files:
33  shutil.copy(os.path.join(src_script_dir, file), dist_script_dir)
34
35# Don't distribute .gitignore files.
36for dirpath, dirnames, filenames in os.walk(project_dist_root):
37  if '.gitignore' in filenames:
38    os.remove(os.path.join(dirpath, '.gitignore'))
39
40# Remove an empty MESON_PROJECT_DIST_ROOT/build directory.
41dist_build_dir = os.path.join(project_dist_root, 'build')
42if os.path.isdir(dist_build_dir):
43  try:
44    os.rmdir(dist_build_dir)
45  except OSError:
46    # Ignore the error, if not empty.
47    pass
48
49# Remove specified files and directories from the MESON_PROJECT_DIST_ROOT directory.
50for rel_path in sys.argv[3:]:
51  abs_path = os.path.join(project_dist_root, rel_path)
52  if os.path.isfile(abs_path):
53    os.remove(abs_path)
54  elif os.path.isdir(abs_path):
55    shutil.rmtree(abs_path, ignore_errors=True)
56  else:
57    # Ignore non-existent files and directories.
58    print('--- Info:', abs_path, 'not found, not removed.')
59