Merge pull request #2840 from alyst/fix_detect_location

Fix meson location detection from other meson tools
pull/2892/head
Jussi Pakkanen 7 years ago committed by GitHub
commit 392b3d8bc7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 47
      mesonbuild/mesonlib.py

@ -25,29 +25,40 @@ from glob import glob
def detect_meson_py_location(): def detect_meson_py_location():
c = sys.argv[0] c = sys.argv[0]
c_fname = os.path.split(c)[1] c_dir, c_fname = os.path.split(c)
if c_fname == 'meson' or c_fname == 'meson.py':
# $ /foo/meson.py <args> # get the absolute path to the <mesontool> folder
if os.path.isabs(c): m_dir = None
return c if os.path.isabs(c):
# $ meson <args> (gets run from /usr/bin/meson) # $ /foo/<mesontool>.py <args>
m_dir = c_dir
elif c_dir == '':
# $ <mesontool> <args> (gets run from /usr/bin/<mesontool>)
in_path_exe = shutil.which(c_fname) in_path_exe = shutil.which(c_fname)
if in_path_exe: if in_path_exe:
# Special case: when run like "./meson.py <opts>" and user has m_dir, c_fname = os.path.split(in_path_exe)
# period in PATH, we need to expand it out, because, for example, # Special case: when run like "./meson.py <opts>",
# we need to expand it out, because, for example,
# "ninja test" will be run from a different directory. # "ninja test" will be run from a different directory.
if '.' in os.environ['PATH'].split(':'): if m_dir == '.':
p, f = os.path.split(in_path_exe) m_dir = os.getcwd()
if p == '' or p == '.': else:
return os.path.join(os.getcwd(), f) m_dir = os.path.abspath(c_dir)
return in_path_exe
# $ python3 ./meson.py <args> # find meson in m_dir
if os.path.exists(c): if m_dir is not None:
return os.path.join(os.getcwd(), c) for fname in ['meson', 'meson.py']:
m_path = os.path.join(m_dir, fname)
if os.path.exists(m_path):
return m_path
# No meson found, which means that either:
# a) meson is not installed
# b) meson is installed to a non-standard location
# c) the script that invoked mesonlib is not the one of meson tools (e.g. run_unittests.py)
# The only thing remaining is to try to find the bundled executable and # The only thing remaining is to try to find the bundled executable and
# pray distro packagers have not moved it. # pray distro packagers have not moved it.
fname = os.path.join(os.path.dirname(__file__), '..', 'meson.py') fname = os.path.normpath(os.path.join(os.path.dirname(__file__), '..', 'meson.py'))
if not os.path.exists(fname): if not os.path.exists(fname):
raise RuntimeError('Could not determine how to run Meson. Please file a bug with details.') raise RuntimeError('Could not determine how to run Meson. Please file a bug with details.')
return fname return fname

Loading…
Cancel
Save