From 963892a7c339ffa3d5b15a13e98b170f6ca85427 Mon Sep 17 00:00:00 2001 From: Axel Waggershauser Date: Thu, 5 Feb 2015 23:35:49 +0100 Subject: [PATCH 1/5] basic support for 'build_always' feature of custom targets --- build.py | 4 +++- ninjabackend.py | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/build.py b/build.py index d60c503d0..7b05f1909 100644 --- a/build.py +++ b/build.py @@ -667,7 +667,8 @@ class CustomTarget: 'output' : True, 'command' : True, 'install' : True, - 'install_dir' : True} + 'install_dir' : True, + 'build_always' : True} def __init__(self, name, subdir, kwargs): self.name = name self.subdir = subdir @@ -740,6 +741,7 @@ class CustomTarget: raise InvalidArguments('"install_dir" must be a string.') else: self.install = False + self.build_always = kwargs.get('build_always', False) def get_basename(self): return self.name diff --git a/ninjabackend.py b/ninjabackend.py index abb2760e3..fb7dfae89 100644 --- a/ninjabackend.py +++ b/ninjabackend.py @@ -281,6 +281,8 @@ class NinjaBackend(backends.Backend): deps = [os.path.join(i.get_subdir(), self.hackety_hack(i.get_filename())) for i in target.get_dependencies()] srcs = [os.path.join(self.build_to_src, target.subdir, i) for i in target.sources] deps += srcs + if target.build_always: + deps.append('PHONY') elem = NinjaBuildElement(ofilenames, 'CUSTOM_COMMAND', deps) cmd = [] for i in target.command: From 129bb902bc94547c3a2a8e4ff29ee91c8d5fc427 Mon Sep 17 00:00:00 2001 From: Axel Waggershauser Date: Fri, 6 Feb 2015 00:06:32 +0100 Subject: [PATCH 2/5] added basic version of VCS supprt for configuring files based on VCS state during build time --- authors.txt | 1 + interpreter.py | 60 +++++++++++++++++++++++++++++++++++++++++++++++ meson_vcs_info.py | 60 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 121 insertions(+) create mode 100755 meson_vcs_info.py diff --git a/authors.txt b/authors.txt index e3652c85f..85a4770f5 100644 --- a/authors.txt +++ b/authors.txt @@ -9,3 +9,4 @@ Peter Koval Masashi Fujita Juhani Simola Robin McCorkell +Axel Waggershauser diff --git a/interpreter.py b/interpreter.py index 33dbbefb9..cd36d2915 100644 --- a/interpreter.py +++ b/interpreter.py @@ -726,6 +726,8 @@ class Interpreter(): 'get_option' : self.func_get_option, 'subproject' : self.func_subproject, 'pkgconfig_gen' : self.func_pkgconfig_gen, + 'vcs_info' : self.func_vcs_info, + 'vcs_configure' : self.func_vcs_configure, } def get_build_def_files(self): @@ -829,6 +831,13 @@ class Interpreter(): if not isinstance(actual, wanted): raise InvalidArguments('Incorrect argument type.') + def validate_target_name(self, name): + if name in coredata.forbidden_target_names: + raise InvalidArguments('Target name "%s" is reserved for Meson\'s internal use. Please rename.'\ + % name) + if name in self.build.targets: + raise InvalidCode('Tried to create target "%s", but a target of that name already exists.' % name) + def func_run_command(self, node, args, kwargs): if len(args) < 1: raise InterpreterException('Not enough arguments') @@ -1119,6 +1128,57 @@ class Interpreter(): self.build.targets[name] = tg.held_object return tg + def func_vcs_info(self, nodes, args, kwargs): + # required: none; optional: name, vcs_cmd, vcs_dir, output, build_always + name = args[0] if len(args) > 0 else 'vcs_info' + self.validate_target_name(name) + vcs_dir = kwargs.pop('vcs_dir', self.environment.get_source_dir()) + vcs_cmd = None + + vcs_systems = [ + dict(name = 'git', cmd = 'git', get_rev = 'git describe', dep = '.git/logs/HEAD', get_root = 'git rev-parse --show-toplevel'), + dict(name = 'mercurial', cmd = 'hg', get_rev = 'hg id -n', dep = '.hg/dirstate', get_root = 'hg root'), +# dict(name = 'subversion', cmd = 'svn', get_rev = 'svn info', dep = '.svn/wc.db', get_root = 'svn info'), + ] + for vcs in vcs_systems: + if vcs['cmd'] and shutil.which(vcs['cmd']): + vcs_root = subprocess.Popen(vcs['get_root'], shell=True, cwd=vcs_dir, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL).communicate()[0].decode().strip() + vcs_dep = os.path.join(vcs_root, vcs['dep']) + if os.path.exists(vcs_dep): + vcs_cmd = vcs['get_rev'] + kwargs['input'] = vcs_dep + mlog.log('Found %s repository at %s' % (vcs['name'], vcs_root)) + break + + vcs_cmd = kwargs.pop('vcs_cmd', vcs_cmd ) + if not vcs_cmd: + raise InvalidArguments('Could not autodetect vcs system and no custom vcs_cmd was specified.') + kwargs.setdefault('output', name) + script = os.path.join(self.environment.get_script_dir(), 'meson_vcs_info.py') + kwargs['command'] = [script, 'update', vcs_cmd, vcs_dir, '@OUTPUT0@'] + tg = CustomTargetHolder(name, self.subdir, kwargs) + self.build.targets[name] = tg.held_object + return tg + + def func_vcs_configure(self, nodes, args, kwargs): + # required: name, input, vcs_info; optional: output, config_var_name + name = args[0] + self.validate_target_name(name) + kwargs.setdefault('input', args[1] if len(args)>1 else None) + vcs_info = kwargs.pop('vcs_info', None) + if not vcs_info: + raise InvalidArguments('Missing argument "vcs_info".') + if not isinstance(vcs_info, CustomTargetHolder): + raise InterpreterException('Argument "vcs_info" is not of type vcs_info') + vcs_info = vcs_info.held_object + config_var_name = kwargs.pop('config_var_name', 'VCS_INFO') + kwargs.setdefault('output', name) + script = os.path.join(self.environment.get_script_dir(), 'meson_vcs_info.py') + kwargs['command'] = [script, 'configure', vcs_info, config_var_name, '@INPUT0@', '@OUTPUT0@'] + tg = CustomTargetHolder(name, self.subdir, kwargs) + self.build.targets[name] = tg.held_object + return tg + def func_run_target(self, node, args, kwargs): if len(args) < 2: raise InterpreterException('Incorrect number of arguments') diff --git a/meson_vcs_info.py b/meson_vcs_info.py new file mode 100755 index 000000000..9f5e46245 --- /dev/null +++ b/meson_vcs_info.py @@ -0,0 +1,60 @@ +#!/usr/bin/env python3 + +# Copyright 2013-2015 The Meson development team + +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at + +# http://www.apache.org/licenses/LICENSE-2.0 + +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import sys, os, shutil, shlex, subprocess +import backends + +def update_vcs_info(vcs_cmd, vcs_dir, vcs_info_file): + """Update the string in the vcs_info_file with the stdout of vcs_cmd executed in vcs_dir.""" + + cmd = shlex.split(vcs_cmd) + # Is the command an executable in path or maybe a script in the source tree? + cmd[0] = shutil.which(cmd[0]) or os.path.join(vcs_dir, cmd[0]) + + try: + info = subprocess.check_output(cmd, cwd=vcs_dir) + except FileNotFoundError: + print('Could not execute command "%s".' % ' '.join(cmd)) + sys.exit(1) + except subprocess.CalledProcessError as e: + print('Failed to get repository information from %s.\n' % vcs_dir) + sys.exit(e.returncode) + + info = info.strip() + + if (not os.path.exists(vcs_info_file)) or (open(vcs_info_file, 'rb').read() != info): + open(vcs_info_file, 'wb').write(info) + + +def configure_vcs_info(vcs_info_file, config_var_name, input_file, output_file): + """Configure the input_file by replacing the variable config_var_name with the contents of the vcs_info_file and save it as output_file.""" + + info = open(vcs_info_file).read() + backends.do_conf_file(input_file, output_file, {config_var_name : info}) + + +if __name__ == '__main__': + if not len(sys.argv) in [5,6]: + print('Version Control Systems helper script for Meson. Do not run on your own, mmm\'kay?') + print('%s update ' % sys.argv[0]) + print('%s configure ' % sys.argv[0]) + sys.exit(1) + + if sys.argv[1] == 'update': + update_vcs_info( *sys.argv[2:5] ) + elif sys.argv[1] == 'configure': + configure_vcs_info( *sys.argv[2:6] ) + From 223596d7bf8ab9b839e0031022c132a3ccd4fd1d Mon Sep 17 00:00:00 2001 From: Axel Waggershauser Date: Wed, 11 Feb 2015 03:27:40 +0100 Subject: [PATCH 3/5] added support for optional custom command and replace_string parameter of vcs_tag --- interpreter.py | 105 +++++++++--------------- meson_vcs_info.py | 60 -------------- test cases/common/73 vcstag/meson.build | 6 ++ vcstagger.py | 53 +++--------- 4 files changed, 59 insertions(+), 165 deletions(-) delete mode 100755 meson_vcs_info.py diff --git a/interpreter.py b/interpreter.py index 75a413541..d406e9f47 100644 --- a/interpreter.py +++ b/interpreter.py @@ -830,13 +830,6 @@ class Interpreter(): if not isinstance(actual, wanted): raise InvalidArguments('Incorrect argument type.') - def validate_target_name(self, name): - if name in coredata.forbidden_target_names: - raise InvalidArguments('Target name "%s" is reserved for Meson\'s internal use. Please rename.'\ - % name) - if name in self.build.targets: - raise InvalidCode('Tried to create target "%s", but a target of that name already exists.' % name) - def func_run_command(self, node, args, kwargs): if len(args) < 1: raise InterpreterException('Not enough arguments') @@ -1113,15 +1106,50 @@ class Interpreter(): def func_jar(self, node, args, kwargs): return self.build_target(node, args, kwargs, JarHolder) + def detect_vcs(self, source_dir): + vcs_systems = [ + dict(name = 'git', cmd = 'git', repo_dir = '.git', get_rev = 'git describe --dirty=+', rev_regex = '(.*)', dep = '.git/logs/HEAD'), + dict(name = 'mercurial', cmd = 'hg', repo_dir = '.hg', get_rev = 'hg id -n', rev_regex = '(.*)', dep = '.hg/dirstate'), + dict(name = 'subversion', cmd = 'svn', repo_dir = '.svn', get_rev = 'svn info', rev_regex = 'Revision: (.*)', dep = '.svn/wc.db'), + dict(name = 'bazaar', cmd = 'bzr', repo_dir = '.bzr', get_rev = 'bzr revno', rev_regex = '(.*)', dep = '.bzr'), + ] + + segs = source_dir.replace('\\', '/').split('/') + for i in range(len(segs), -1, -1): + curdir = '/'.join(segs[:i]) + for vcs in vcs_systems: + if os.path.isdir(os.path.join(curdir, vcs['repo_dir'])) and shutil.which(vcs['cmd']): + vcs['wc_dir'] = curdir + return vcs + return None + def func_vcs_tag(self, node, args, kwargs): - fallback = kwargs.get('fallback', None) + fallback = kwargs.pop('fallback', None) if not isinstance(fallback, str): raise InterpreterException('Keyword argument must exist and be a string.') - del kwargs['fallback'] - scriptfile = os.path.join(os.path.split(__file__)[0], 'vcstagger.py') - kwargs['command'] = [sys.executable, scriptfile, '@INPUT@', '@OUTPUT@', fallback] - kwargs['build_always'] = True - return self.func_custom_target(node, ['vcstag'], kwargs) + replace_string = kwargs.pop('replace_string', '@VCS_TAG@') + regex_selector = '(.*)' # default regex selector for custom command: use complete output + vcs_cmd = kwargs.get('command', None) + if vcs_cmd and not isinstance(vcs_cmd, list): + vcs_cmd = [vcs_cmd] + # source_dir = os.path.split(os.path.abspath(kwargs.get('infile')))[0] + source_dir = os.path.join(self.environment.get_source_dir(), self.subdir) + if vcs_cmd: + # Is the command an executable in path or maybe a script in the source tree? + vcs_cmd[0] = shutil.which(vcs_cmd[0]) or os.path.join(source_dir, vcs_cmd[0]) + else: + vcs = self.detect_vcs(source_dir) + if vcs: + mlog.log('Found %s repository at %s' % (vcs['name'], vcs['wc_dir'])) + vcs_cmd = vcs['get_rev'].split() + regex_selector = vcs['rev_regex'] + else: + vcs_cmd = [' '] # executing this cmd will fail in vcstagger.py and force to use the fallback string + scriptfile = os.path.join(self.environment.get_script_dir(), 'vcstagger.py') + # vcstagger.py parameters: infile, outfile, fallback, source_dir, replace_string, regex_selector, command... + kwargs['command'] = [sys.executable, scriptfile, '@INPUT0@', '@OUTPUT0@', fallback, source_dir, replace_string, regex_selector] + vcs_cmd + kwargs.setdefault('build_always', True) + return self.func_custom_target(node, [kwargs['output']], kwargs) def func_custom_target(self, node, args, kwargs): if len(args) != 1: @@ -1138,57 +1166,6 @@ class Interpreter(): self.build.targets[name] = tg.held_object return tg - def func_vcs_info(self, nodes, args, kwargs): - # required: none; optional: name, vcs_cmd, vcs_dir, output, build_always - name = args[0] if len(args) > 0 else 'vcs_info' - self.validate_target_name(name) - vcs_dir = kwargs.pop('vcs_dir', self.environment.get_source_dir()) - vcs_cmd = None - - vcs_systems = [ - dict(name = 'git', cmd = 'git', get_rev = 'git describe', dep = '.git/logs/HEAD', get_root = 'git rev-parse --show-toplevel'), - dict(name = 'mercurial', cmd = 'hg', get_rev = 'hg id -n', dep = '.hg/dirstate', get_root = 'hg root'), -# dict(name = 'subversion', cmd = 'svn', get_rev = 'svn info', dep = '.svn/wc.db', get_root = 'svn info'), - ] - for vcs in vcs_systems: - if vcs['cmd'] and shutil.which(vcs['cmd']): - vcs_root = subprocess.Popen(vcs['get_root'], shell=True, cwd=vcs_dir, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL).communicate()[0].decode().strip() - vcs_dep = os.path.join(vcs_root, vcs['dep']) - if os.path.exists(vcs_dep): - vcs_cmd = vcs['get_rev'] - kwargs['input'] = vcs_dep - mlog.log('Found %s repository at %s' % (vcs['name'], vcs_root)) - break - - vcs_cmd = kwargs.pop('vcs_cmd', vcs_cmd ) - if not vcs_cmd: - raise InvalidArguments('Could not autodetect vcs system and no custom vcs_cmd was specified.') - kwargs.setdefault('output', name) - script = os.path.join(self.environment.get_script_dir(), 'meson_vcs_info.py') - kwargs['command'] = [script, 'update', vcs_cmd, vcs_dir, '@OUTPUT0@'] - tg = CustomTargetHolder(name, self.subdir, kwargs) - self.build.targets[name] = tg.held_object - return tg - - def func_vcs_configure(self, nodes, args, kwargs): - # required: name, input, vcs_info; optional: output, config_var_name - name = args[0] - self.validate_target_name(name) - kwargs.setdefault('input', args[1] if len(args)>1 else None) - vcs_info = kwargs.pop('vcs_info', None) - if not vcs_info: - raise InvalidArguments('Missing argument "vcs_info".') - if not isinstance(vcs_info, CustomTargetHolder): - raise InterpreterException('Argument "vcs_info" is not of type vcs_info') - vcs_info = vcs_info.held_object - config_var_name = kwargs.pop('config_var_name', 'VCS_INFO') - kwargs.setdefault('output', name) - script = os.path.join(self.environment.get_script_dir(), 'meson_vcs_info.py') - kwargs['command'] = [script, 'configure', vcs_info, config_var_name, '@INPUT0@', '@OUTPUT0@'] - tg = CustomTargetHolder(name, self.subdir, kwargs) - self.build.targets[name] = tg.held_object - return tg - def func_run_target(self, node, args, kwargs): if len(args) < 2: raise InterpreterException('Incorrect number of arguments') diff --git a/meson_vcs_info.py b/meson_vcs_info.py deleted file mode 100755 index 9f5e46245..000000000 --- a/meson_vcs_info.py +++ /dev/null @@ -1,60 +0,0 @@ -#!/usr/bin/env python3 - -# Copyright 2013-2015 The Meson development team - -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at - -# http://www.apache.org/licenses/LICENSE-2.0 - -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import sys, os, shutil, shlex, subprocess -import backends - -def update_vcs_info(vcs_cmd, vcs_dir, vcs_info_file): - """Update the string in the vcs_info_file with the stdout of vcs_cmd executed in vcs_dir.""" - - cmd = shlex.split(vcs_cmd) - # Is the command an executable in path or maybe a script in the source tree? - cmd[0] = shutil.which(cmd[0]) or os.path.join(vcs_dir, cmd[0]) - - try: - info = subprocess.check_output(cmd, cwd=vcs_dir) - except FileNotFoundError: - print('Could not execute command "%s".' % ' '.join(cmd)) - sys.exit(1) - except subprocess.CalledProcessError as e: - print('Failed to get repository information from %s.\n' % vcs_dir) - sys.exit(e.returncode) - - info = info.strip() - - if (not os.path.exists(vcs_info_file)) or (open(vcs_info_file, 'rb').read() != info): - open(vcs_info_file, 'wb').write(info) - - -def configure_vcs_info(vcs_info_file, config_var_name, input_file, output_file): - """Configure the input_file by replacing the variable config_var_name with the contents of the vcs_info_file and save it as output_file.""" - - info = open(vcs_info_file).read() - backends.do_conf_file(input_file, output_file, {config_var_name : info}) - - -if __name__ == '__main__': - if not len(sys.argv) in [5,6]: - print('Version Control Systems helper script for Meson. Do not run on your own, mmm\'kay?') - print('%s update ' % sys.argv[0]) - print('%s configure ' % sys.argv[0]) - sys.exit(1) - - if sys.argv[1] == 'update': - update_vcs_info( *sys.argv[2:5] ) - elif sys.argv[1] == 'configure': - configure_vcs_info( *sys.argv[2:6] ) - diff --git a/test cases/common/73 vcstag/meson.build b/test cases/common/73 vcstag/meson.build index 23ff487f2..b37ca6ad9 100644 --- a/test cases/common/73 vcstag/meson.build +++ b/test cases/common/73 vcstag/meson.build @@ -4,5 +4,11 @@ version_src = vcs_tag(input : 'vcstag.c.in', output : 'vcstag.c', fallback : '1.0.0') +version_src_custom = vcs_tag(input : 'vcstag.c.in', +output : 'vcstag-cusom.c', +command : ['git', 'show-ref', '-s', 'refs/heads/master'], +fallback : '1.0.0') + executable('tagprog', 'tagprog.c', version_src) +executable('tagprog-custom', 'tagprog.c', version_src_custom) diff --git a/vcstagger.py b/vcstagger.py index f754358f5..ccc584e92 100755 --- a/vcstagger.py +++ b/vcstagger.py @@ -14,49 +14,20 @@ # See the License for the specific language governing permissions and # limitations under the License. -import sys, os, subprocess +import sys, os, subprocess, re -def tag(infile, outfile, fallback): - tagid = get_string(infile, fallback) - newdata = open(infile).read().replace('@VCS_TAG@', tagid) +def config_vcs_tag(infile, outfile, fallback, source_dir, replace_string, regex_selector, cmd): try: - olddata = open(outfile).read() - if olddata == newdata: - return + output = subprocess.check_output(cmd, cwd=source_dir) + new_string = re.search(regex_selector, output.decode()).group(1).strip() except Exception: - pass - open(outfile, 'w').write(newdata) - -def get_string(infile, fallback): - absfile = os.path.join(os.getcwd(), infile) - directory = os.path.split(absfile)[0] - segs = directory.replace('\\', '/').split('/') - for i in range(len(segs), -1, -1): - curdir = '/'.join(segs[:i]) - if os.path.isdir(os.path.join(curdir, '.git')): - output = subprocess.check_output(['git', 'describe'], - cwd = directory) - return output.decode().strip() - elif os.path.isdir(os.path.join(curdir, '.hg')): - output = subprocess.check_output(['hg', 'identify'], - cwd=directory) - return output.decode().strip() - elif os.path.isdir(os.path.join(curdir, '.bzr')): - output = subprocess.check_output(['bzr', 'revno'], - cwd=directory) - return output.decode().strip() - elif os.path.isdir(os.path.join(curdir, '.svn')): - output = subprocess.check_output(['svn', 'info'], - cwd=directory) - for line in output.decode().split('\n'): - (k, v) = line.split(':', 1) - if k.strip() == 'Revision': - return v.strip() - raise RuntimeError('Svn output malformed.') - return fallback + new_string = fallback + + new_data = open(infile).read().replace(replace_string, new_string) + if (not os.path.exists(outfile)) or (open(outfile).read() != new_data): + open(outfile, 'w').write(new_data) if __name__ == '__main__': - infile = sys.argv[1] - outfile = sys.argv[2] - fallback = sys.argv[3] - tag(infile, outfile, fallback) + infile, outfile, fallback, source_dir, replace_string, regex_selector = sys.argv[1:7] + command = sys.argv[7:] + config_vcs_tag(infile, outfile, fallback, source_dir, replace_string, regex_selector, command) From 5a0cd64f9a0d6683dd542dd69e40f39dc0727cdd Mon Sep 17 00:00:00 2001 From: Axel Waggershauser Date: Sat, 14 Feb 2015 01:55:15 +0100 Subject: [PATCH 4/5] fixed typo in filename --- test cases/common/73 vcstag/meson.build | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test cases/common/73 vcstag/meson.build b/test cases/common/73 vcstag/meson.build index b37ca6ad9..001b42da0 100644 --- a/test cases/common/73 vcstag/meson.build +++ b/test cases/common/73 vcstag/meson.build @@ -5,7 +5,7 @@ output : 'vcstag.c', fallback : '1.0.0') version_src_custom = vcs_tag(input : 'vcstag.c.in', -output : 'vcstag-cusom.c', +output : 'vcstag-custom.c', command : ['git', 'show-ref', '-s', 'refs/heads/master'], fallback : '1.0.0') From 0a95e4b4a929f848b81bd723124d3c9a774cbce0 Mon Sep 17 00:00:00 2001 From: Axel Waggershauser Date: Sat, 14 Feb 2015 01:56:27 +0100 Subject: [PATCH 5/5] moved helper function detect_vcs into mesonlib.py --- interpreter.py | 20 ++------------------ mesonlib.py | 19 ++++++++++++++++++- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/interpreter.py b/interpreter.py index d406e9f47..a00a1ae64 100644 --- a/interpreter.py +++ b/interpreter.py @@ -20,6 +20,7 @@ import mlog import build import optinterpreter import wrap +import mesonlib import os, sys, platform, subprocess, shutil, uuid class InterpreterException(coredata.MesonException): @@ -1106,23 +1107,6 @@ class Interpreter(): def func_jar(self, node, args, kwargs): return self.build_target(node, args, kwargs, JarHolder) - def detect_vcs(self, source_dir): - vcs_systems = [ - dict(name = 'git', cmd = 'git', repo_dir = '.git', get_rev = 'git describe --dirty=+', rev_regex = '(.*)', dep = '.git/logs/HEAD'), - dict(name = 'mercurial', cmd = 'hg', repo_dir = '.hg', get_rev = 'hg id -n', rev_regex = '(.*)', dep = '.hg/dirstate'), - dict(name = 'subversion', cmd = 'svn', repo_dir = '.svn', get_rev = 'svn info', rev_regex = 'Revision: (.*)', dep = '.svn/wc.db'), - dict(name = 'bazaar', cmd = 'bzr', repo_dir = '.bzr', get_rev = 'bzr revno', rev_regex = '(.*)', dep = '.bzr'), - ] - - segs = source_dir.replace('\\', '/').split('/') - for i in range(len(segs), -1, -1): - curdir = '/'.join(segs[:i]) - for vcs in vcs_systems: - if os.path.isdir(os.path.join(curdir, vcs['repo_dir'])) and shutil.which(vcs['cmd']): - vcs['wc_dir'] = curdir - return vcs - return None - def func_vcs_tag(self, node, args, kwargs): fallback = kwargs.pop('fallback', None) if not isinstance(fallback, str): @@ -1138,7 +1122,7 @@ class Interpreter(): # Is the command an executable in path or maybe a script in the source tree? vcs_cmd[0] = shutil.which(vcs_cmd[0]) or os.path.join(source_dir, vcs_cmd[0]) else: - vcs = self.detect_vcs(source_dir) + vcs = mesonlib.detect_vcs(source_dir) if vcs: mlog.log('Found %s repository at %s' % (vcs['name'], vcs['wc_dir'])) vcs_cmd = vcs['get_rev'].split() diff --git a/mesonlib.py b/mesonlib.py index 7e1577047..f2d424c90 100644 --- a/mesonlib.py +++ b/mesonlib.py @@ -14,7 +14,7 @@ """A library of random helper functionality.""" -import platform, subprocess, operator +import platform, subprocess, operator, os, shutil def is_osx(): return platform.system().lower() == 'darwin' @@ -42,6 +42,23 @@ def exe_exists(arglist): pass return False +def detect_vcs(source_dir): + vcs_systems = [ + dict(name = 'git', cmd = 'git', repo_dir = '.git', get_rev = 'git describe --dirty=+', rev_regex = '(.*)', dep = '.git/logs/HEAD'), + dict(name = 'mercurial', cmd = 'hg', repo_dir = '.hg', get_rev = 'hg id -n', rev_regex = '(.*)', dep = '.hg/dirstate'), + dict(name = 'subversion', cmd = 'svn', repo_dir = '.svn', get_rev = 'svn info', rev_regex = 'Revision: (.*)', dep = '.svn/wc.db'), + dict(name = 'bazaar', cmd = 'bzr', repo_dir = '.bzr', get_rev = 'bzr revno', rev_regex = '(.*)', dep = '.bzr'), + ] + + segs = source_dir.replace('\\', '/').split('/') + for i in range(len(segs), -1, -1): + curdir = '/'.join(segs[:i]) + for vcs in vcs_systems: + if os.path.isdir(os.path.join(curdir, vcs['repo_dir'])) and shutil.which(vcs['cmd']): + vcs['wc_dir'] = curdir + return vcs + return None + def version_compare(vstr1, vstr2): if vstr2.startswith('>='): cmpop = operator.ge