Skip to content

Commit

Permalink
vala: Add support for cross-compilation.
Browse files Browse the repository at this point in the history
It is very useful to be able to pass -DFOO and do #if FOO in Vala code,
for the few cases where platform-dependent Vala code is needed.

Fixes mesonbuild#1771
  • Loading branch information
oleavr committed Aug 9, 2018
1 parent 9a113e8 commit db0aea1
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 23 deletions.
20 changes: 14 additions & 6 deletions mesonbuild/backend/ninjabackend.py
Original file line number Diff line number Diff line change
Expand Up @@ -1294,8 +1294,12 @@ def generate_vala_compile(self, target, outfile):
dependency_vapis = self.determine_dep_vapis(target)
extra_dep_files += dependency_vapis
args += extra_args
if target.is_cross:
crstr = '_CROSS'
else:
crstr = ''
element = NinjaBuildElement(self.all_outputs, valac_outputs,
valac.get_language() + '_COMPILER',
'%s%s_COMPILER' % (valac.get_language(), crstr),
all_files + dependency_vapis)
element.add_item('ARGS', args)
element.add_dep(extra_dep_files)
Expand Down Expand Up @@ -1661,9 +1665,14 @@ def generate_cs_compile_rule(self, compiler, outfile):
outfile.write(description)
outfile.write('\n')

def generate_vala_compile_rules(self, compiler, outfile):
rule = 'rule %s_COMPILER\n' % compiler.get_language()
invoc = ' '.join([ninja_quote(i) for i in compiler.get_exelist()])
def generate_vala_compile_rules(self, compiler, is_cross, outfile):
if is_cross:
crstr = '_CROSS'
else:
crstr = ''
rule = 'rule %s%s_COMPILER\n' % (compiler.get_language(), crstr)
cross_args = self.get_cross_info_lang_args('vala', is_cross)
invoc = ' '.join([ninja_quote(i) for i in compiler.get_exelist() + cross_args])
command = ' command = %s $ARGS $in\n' % invoc
description = ' description = Compiling Vala source $in.\n'
restat = ' restat = 1\n' # ValaC does this always to take advantage of it.
Expand Down Expand Up @@ -1764,8 +1773,7 @@ def generate_compile_rule_for(self, langname, compiler, is_cross, outfile):
self.generate_cs_compile_rule(compiler, outfile)
return
if langname == 'vala':
if not is_cross:
self.generate_vala_compile_rules(compiler, outfile)
self.generate_vala_compile_rules(compiler, is_cross, outfile)
return
if langname == 'rust':
self.generate_rust_compile_rules(compiler, outfile, is_cross)
Expand Down
4 changes: 2 additions & 2 deletions mesonbuild/compilers/vala.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
from .compilers import Compiler

class ValaCompiler(Compiler):
def __init__(self, exelist, version):
def __init__(self, exelist, version, is_cross):
self.language = 'vala'
super().__init__(exelist, version)
self.version = version
self.id = 'valac'
self.is_cross = False
self.is_cross = is_cross
self.base_options = ['b_colorout']

def name_string(self):
Expand Down
33 changes: 20 additions & 13 deletions mesonbuild/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ def __init__(self, source_dir, build_dir, options):
self.default_objc = ['cc']
self.default_objcpp = ['c++']
self.default_fortran = ['gfortran', 'g95', 'f95', 'f90', 'f77', 'ifort']
self.default_vala = ['valac']
self.default_rust = ['rustc']
self.default_static_linker = ['ar']
self.vs_static_linker = ['lib']
Expand Down Expand Up @@ -771,19 +772,25 @@ def detect_cs_compiler(self):

self._handle_exceptions(popen_exceptions, compilers)

def detect_vala_compiler(self):
if 'VALAC' in os.environ:
exelist = shlex.split(os.environ['VALAC'])
else:
exelist = ['valac']
try:
p, out = Popen_safe(exelist + ['--version'])[0:2]
except OSError:
raise EnvironmentException('Could not execute Vala compiler "%s"' % ' '.join(exelist))
version = search_version(out)
if 'Vala' in out:
return ValaCompiler(exelist, version)
raise EnvironmentException('Unknown compiler "' + ' '.join(exelist) + '"')
def detect_vala_compiler(self, want_cross):
popen_exceptions = {}
compilers, ccache, is_cross, exe_wrap = self._get_compilers('vala', 'VALAC', want_cross)
for compiler in compilers:
if isinstance(compiler, str):
compiler = [compiler]
arg = ['--version']
try:
p, out = Popen_safe(compiler + arg)[0:2]
except OSError as e:
popen_exceptions[' '.join(compiler + arg)] = e
continue

version = search_version(out)

if 'Vala' in out:
return ValaCompiler(compiler, version, is_cross)

self._handle_exceptions(popen_exceptions, compilers)

def detect_rust_compiler(self, want_cross):
popen_exceptions = {}
Expand Down
4 changes: 2 additions & 2 deletions mesonbuild/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2578,9 +2578,9 @@ def detect_compilers(self, lang, need_cross_compiler):
if need_cross_compiler:
cross_comp = comp # C# is platform independent.
elif lang == 'vala':
comp = self.environment.detect_vala_compiler()
comp = self.environment.detect_vala_compiler(False)
if need_cross_compiler:
cross_comp = comp # Vala compiles to platform-independent C
cross_comp = self.environment.detect_vala_compiler(True)
elif lang == 'd':
comp = self.environment.detect_d_compiler(False)
if need_cross_compiler:
Expand Down

0 comments on commit db0aea1

Please sign in to comment.