3 # External command, intended to be called with run_command(), custom_target(),
4 # meson.add_install_script() and meson.add_dist_script().
7 # handle-built-files.py <subcommand> <xxx>...
13 from pathlib import Path
15 subcommand = sys.argv[1]
17 # Invoked from custom_target() in meson.build.
19 # argv[2] argv[3] argv[4]
20 # <include_dir> <input_file> <output_file>
22 include_dir = sys.argv[2]
23 input_file = sys.argv[3]
24 output_file = sys.argv[4]
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)
35 with open(output_file, mode='w') as output_file_obj:
36 return subprocess.run(cmd, stdout=output_file_obj).returncode
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>...
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])
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)
53 install_dir = os.path.join(install_dir_root, rel_dir)
55 install_dir = install_dir_root
57 print('Installing ', path_h, ' to ', install_dir)
59 # Create the installation directory, if it does not exist.
60 os.makedirs(install_dir, exist_ok=True)
62 # shutil.copy2() copies timestamps and some other file metadata.
63 shutil.copy2(path_h, install_dir)
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>...
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.
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
80 # Distribute .h and .cc files built from .m4 files, or generated MSVC files.
81 for file in sys.argv[4:]:
83 dist_dir = os.path.join(dist_dir_root, os.path.dirname(file))
85 # Create the distribution directory, if it does not exist.
86 os.makedirs(dist_dir, exist_ok=True)
88 shutil.copy(os.path.join(built_h_cc_dir, file), dist_dir)
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>...
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]
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.)
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)):
113 # Create the destination directory, if it does not exist.
114 os.makedirs(os.path.dirname(to_file), exist_ok=True)
116 shutil.copy(from_file, to_file)
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)