]> git.tdb.fi Git - ext/sigc++-2.0.git/blob - tools/handle-built-files.py
Adjust the name of the library to match upstream
[ext/sigc++-2.0.git] / tools / handle-built-files.py
1 #!/usr/bin/env python3
2
3 # External command, intended to be called with run_command(), custom_target(),
4 # meson.add_install_script() and meson.add_dist_script().
5
6 #                         argv[1]   argv[2:]
7 # handle-built-files.py <subcommand> <xxx>...
8
9 import os
10 import sys
11 import shutil
12 import subprocess
13 from pathlib import Path
14
15 subcommand = sys.argv[1]
16
17 # Invoked from custom_target() in meson.build.
18 def build_from_m4():
19   #     argv[2]      argv[3]      argv[4]
20   # <include_dir> <input_file> <output_file>
21
22   include_dir = sys.argv[2]
23   input_file = sys.argv[3]
24   output_file = sys.argv[4]
25
26   # Create the destination directory, if it does not exist.
27   output_dir = os.path.dirname(output_file)
28   os.makedirs(output_dir, exist_ok=True)
29
30   cmd = [
31     'm4',
32     '-I', include_dir,
33     input_file,
34   ]
35   with open(output_file, mode='w') as output_file_obj:
36     return subprocess.run(cmd, stdout=output_file_obj).returncode
37
38 # Invoked from meson.add_install_script().
39 def install_built_h_files():
40   #    argv[2]       argv[3]          argv[4:]
41   # <built_h_dir> <install_subdir> <built_h_files>...
42
43   # <built_h_dir> is an absolute path in the build directory or source directory.
44   # <install_subdir> is an installation directory, relative to {prefix}.
45   built_h_dir = sys.argv[2]
46   install_dir_root = os.path.join(os.getenv('MESON_INSTALL_DESTDIR_PREFIX'), sys.argv[3])
47
48   quiet = bool(os.getenv('MESON_INSTALL_QUIET'))
49   for file in sys.argv[4:]:
50     path_h = os.path.join(built_h_dir, file)
51     rel_dir = os.path.dirname(file)
52     if rel_dir:
53       install_dir = os.path.join(install_dir_root, rel_dir)
54     else:
55       install_dir = install_dir_root
56     if not quiet:
57       print('Installing ', path_h, ' to ', install_dir)
58
59     # Create the installation directory, if it does not exist.
60     os.makedirs(install_dir, exist_ok=True)
61
62     # shutil.copy2() copies timestamps and some other file metadata.
63     shutil.copy2(path_h, install_dir)
64   return 0
65
66 # Invoked from meson.add_dist_script().
67 def dist_built_files(is_msvc_files=False):
68   #     argv[2]        argv[3]     argv[4:]
69   # <built_h_cc_dir> <dist_dir> <built_files>...
70
71   # <built_h_cc_dir> is an absolute path in the build directory or source directory.
72   # <dist_dir> is a distribution directory, relative to MESON_PROJECT_DIST_ROOT.
73
74   # MESON_PROJECT_DIST_ROOT is set only if meson.version() >= 0.58.0.
75   project_dist_root = os.getenv('MESON_PROJECT_DIST_ROOT', os.getenv('MESON_DIST_ROOT'))
76   built_h_cc_dir = sys.argv[2]
77   dist_dir_root = os.path.join(project_dist_root, sys.argv[3])
78   dist_dir = dist_dir_root
79
80   # Distribute .h and .cc files built from .m4 files, or generated MSVC files.
81   for file in sys.argv[4:]:
82     if not is_msvc_files:
83       dist_dir = os.path.join(dist_dir_root, os.path.dirname(file))
84
85     # Create the distribution directory, if it does not exist.
86     os.makedirs(dist_dir, exist_ok=True)
87
88     shutil.copy(os.path.join(built_h_cc_dir, file), dist_dir)
89   return 0
90
91 # Invoked from run_command() in meson.build.
92 def copy_built_files():
93   #  argv[2]    argv[3]    argv[4:]
94   # <from_dir> <to_dir> <file_names>...
95
96   # <from_dir> is an absolute or relative path of the directory to copy from.
97   # <to_dir> is an absolute or relative path of the directory to copy to.
98   from_dir_root = sys.argv[2]
99   to_dir_root = sys.argv[3]
100
101   # Copy some built files if they exist in from_dir, but not in the destination
102   # directory, or if they are not up to date in the destination directory.
103   # (The term "source directory" is avoided here, because from_dir might not
104   # be what Meson calls a source directory as opposed to a build directory.)
105
106   # Copy .h and .cc files built from .m4 files.
107   for file in sys.argv[4:]:
108     from_file = os.path.join(from_dir_root, file)
109     to_file = os.path.join(to_dir_root, file)
110     if os.path.isfile(from_file) and ((not os.path.isfile(to_file))
111        or (os.stat(from_file).st_mtime > os.stat(to_file).st_mtime)):
112
113       # Create the destination directory, if it does not exist.
114       os.makedirs(os.path.dirname(to_file), exist_ok=True)
115
116       shutil.copy(from_file, to_file)
117   return 0
118
119 # ----- Main -----
120 if subcommand == 'build_from_m4':
121   sys.exit(build_from_m4())
122 if subcommand == 'install_built_h_files':
123   sys.exit(install_built_h_files())
124 if subcommand == 'dist_built_files':
125   sys.exit(dist_built_files())
126 if subcommand == 'copy_built_files':
127   sys.exit(copy_built_files())
128 if subcommand == 'dist_gen_msvc_files':
129   sys.exit(dist_built_files(True))
130 print(sys.argv[0], ': illegal subcommand,', subcommand)
131 sys.exit(1)