From 6c50253645dad81afbfd2e1bca2c5d9e08f42e05 Mon Sep 17 00:00:00 2001 From: Emanuele Aina Date: Tue, 27 Sep 2016 15:31:38 +0200 Subject: Use argv[0] to internally relaunch meson.py When installing Meson, distutils may choose to put shim scripts in the `PATH` that only set up the egg requirements before launching the real `meson.py` contained in the egg. This means that `__file__` points to the real `meson.py` file, but launching it directly is doomed to fail as it's missing the metadata contained in the shim to set up the path egg, resulting in errors when trying to import the `mesonbuild` module. A similar issue affects Meson when installed as a zipapp, with the current code going great lengths to figure out how to relaunch itself. Using `argv[0]` avoids these issues as it gives us the way the current executable has been launched, so we are pretty much guaranteed that using it will create another instance of the same executable. We only need to resolve relative paths as the current working directory may get changed before re-launching the script, and using `realpath()` for that saves us the trouble of manually resolving links and getting caught in endless loops. This also mean that `meson_script_file` no longer necessarily point to a absolute file, so rename it to `_launcher` which hopefully would be less prone to inducing false assumptions. --- mesonbuild/mesonmain.py | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) (limited to 'mesonbuild/mesonmain.py') diff --git a/mesonbuild/mesonmain.py b/mesonbuild/mesonmain.py index f7da1e0..7574c27 100644 --- a/mesonbuild/mesonmain.py +++ b/mesonbuild/mesonmain.py @@ -65,7 +65,7 @@ parser.add_argument('directories', nargs='*') class MesonApp(): - def __init__(self, dir1, dir2, script_file, handshake, options, original_cmd_line_args): + def __init__(self, dir1, dir2, script_launcher, handshake, options, original_cmd_line_args): (self.source_dir, self.build_dir) = self.validate_dirs(dir1, dir2, handshake) if not os.path.isabs(options.prefix): raise RuntimeError('--prefix value \'{0}\' must be an absolute path: '.format(options.prefix)) @@ -76,7 +76,7 @@ class MesonApp(): pass else: options.prefix = options.prefix[:-1] - self.meson_script_file = script_file + self.meson_script_launcher = script_launcher self.options = options self.original_cmd_line_args = original_cmd_line_args @@ -124,7 +124,7 @@ itself as required.''' env.coredata.pkgconf_envvar = curvar def generate(self): - env = environment.Environment(self.source_dir, self.build_dir, self.meson_script_file, self.options, self.original_cmd_line_args) + env = environment.Environment(self.source_dir, self.build_dir, self.meson_script_launcher, self.options, self.original_cmd_line_args) mlog.initialize(env.get_log_dir()) mlog.debug('Build started at', datetime.datetime.now().isoformat()) mlog.debug('Python binary:', sys.executable) @@ -262,12 +262,6 @@ def run(mainfile, args): dir2 = args[1] else: dir2 = '.' - while os.path.islink(mainfile): - resolved = os.readlink(mainfile) - if resolved[0] != '/': - mainfile = os.path.join(os.path.dirname(mainfile), resolved) - else: - mainfile = resolved try: app = MesonApp(dir1, dir2, mainfile, handshake, options, sys.argv) except Exception as e: -- cgit v1.1