added basic version of VCS supprt for configuring files based on VCS state during build time

pull/34/head
Axel Waggershauser 10 years ago
parent 963892a7c3
commit 129bb902bc
  1. 1
      authors.txt
  2. 60
      interpreter.py
  3. 60
      meson_vcs_info.py

@ -9,3 +9,4 @@ Peter Koval
Masashi Fujita
Juhani Simola
Robin McCorkell
Axel Waggershauser

@ -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')

@ -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 <vcs_cmd> <vcs_dir> <vcs_info_file>' % sys.argv[0])
print('%s configure <vcs_info_file> <config_var_name> <input_file> <output_file>' % 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] )
Loading…
Cancel
Save