aboutsummaryrefslogtreecommitdiff
path: root/mesonbuild
diff options
context:
space:
mode:
authorAdam C. Foltzer <acfoltzer@galois.com>2017-06-09 14:51:58 -0700
committerAdam C. Foltzer <acfoltzer@galois.com>2017-06-09 15:14:02 -0700
commitbdd4c45b173055931db1c758cd7f016693d3a924 (patch)
treea868e261a3771996dbd3c1dd03c5bac049f60389 /mesonbuild
parent22cfd44221ada3219d9096e15dc8b00d32e0f9f6 (diff)
downloadmeson-bdd4c45b173055931db1c758cd7f016693d3a924.zip
meson-bdd4c45b173055931db1c758cd7f016693d3a924.tar.gz
meson-bdd4c45b173055931db1c758cd7f016693d3a924.tar.bz2
Enhance Rust support
- Adds a `crate_type` kwarg to library targets, allowing the different types of Rust [linkage][1]. - Shared libraries use the `dylib` crate type by default, but can also be `cdylib` - Static libraries use the `rlib` crate type by default, but can also be `staticlib` - If any Rust target has shared library dependencies, add the appropriate linker arguments, including rpath for the sysroot of the Rust compiler [1]: https://doc.rust-lang.org/reference/linkage.html
Diffstat (limited to 'mesonbuild')
-rw-r--r--mesonbuild/backend/ninjabackend.py23
-rw-r--r--mesonbuild/build.py45
-rw-r--r--mesonbuild/compilers.py8
3 files changed, 67 insertions, 9 deletions
diff --git a/mesonbuild/backend/ninjabackend.py b/mesonbuild/backend/ninjabackend.py
index 40a05bf..5764b54 100644
--- a/mesonbuild/backend/ninjabackend.py
+++ b/mesonbuild/backend/ninjabackend.py
@@ -1157,8 +1157,10 @@ int dummy;
args = ['--crate-type']
if isinstance(target, build.Executable):
cratetype = 'bin'
+ elif hasattr(target, 'crate_type'):
+ cratetype = target.crate_type
elif isinstance(target, build.SharedLibrary):
- cratetype = 'rlib'
+ cratetype = 'dylib'
elif isinstance(target, build.StaticLibrary):
cratetype = 'rlib'
else:
@@ -1177,6 +1179,23 @@ int dummy;
if d == '':
d = '.'
args += ['-L', d]
+ has_shared_deps = False
+ for dep in target.get_dependencies():
+ if isinstance(dep, build.SharedLibrary):
+ has_shared_deps = True
+ if isinstance(target, build.SharedLibrary) or has_shared_deps:
+ # add prefer-dynamic if any of the Rust libraries we link
+ # against are dynamic, otherwise we'll end up with
+ # multiple implementations of crates
+ args += ['-C', 'prefer-dynamic']
+ # build the usual rpath arguments as well...
+ rpath_args = rustc.build_rpath_args(self.environment.get_build_dir(),
+ self.determine_rpath_dirs(target),
+ target.install_rpath)
+ # ... but then add rustc's sysroot to account for rustup
+ # installations
+ for rpath_arg in rpath_args:
+ args += ['-C', 'link-arg=' + rpath_arg + ':' + os.path.join(rustc.get_sysroot(), 'lib')]
element = NinjaBuildElement(self.all_outputs, target_name, 'rust_COMPILER', relsrc)
if len(orderdeps) > 0:
element.add_orderdep(orderdeps)
@@ -1184,6 +1203,8 @@ int dummy;
element.add_item('targetdep', depfile)
element.add_item('cratetype', cratetype)
element.write(outfile)
+ if isinstance(target, build.SharedLibrary):
+ self.generate_shsym(outfile, target)
def swift_module_file_name(self, target):
return os.path.join(self.get_target_private_dir(target),
diff --git a/mesonbuild/build.py b/mesonbuild/build.py
index 2c55ed4..f4b3382 100644
--- a/mesonbuild/build.py
+++ b/mesonbuild/build.py
@@ -71,6 +71,7 @@ known_lib_kwargs.update({'version': True, # Only for shared libs
'vala_vapi': True,
'vala_gir': True,
'pic': True, # Only for static libs
+ 'crate_type': True, # Only for Rust libs
})
@@ -1123,6 +1124,14 @@ class StaticLibrary(BuildTarget):
super().__init__(name, subdir, subproject, is_cross, sources, objects, environment, kwargs)
if 'cs' in self.compilers:
raise InvalidArguments('Static libraries not supported for C#.')
+ if 'rust' in self.compilers:
+ # If no crate type is specified, or it's the generic lib type, use rlib
+ if not hasattr(self, 'crate_type') or self.crate_type == 'lib':
+ mlog.debug('Defaulting Rust static library target crate type to rlib')
+ self.crate_type = 'rlib'
+ # Don't let configuration proceed with a non-static crate type
+ elif self.crate_type not in ['rlib', 'staticlib']:
+ raise InvalidArguments('Crate type "{0}" invalid for static libraries; must be "rlib" or "staticlib"'.format(self.crate_type))
# By default a static library is named libfoo.a even on Windows because
# MSVC does not have a consistent convention for what static libraries
# are called. The MSVC CRT uses libfoo.lib syntax but nothing else uses
@@ -1133,9 +1142,12 @@ class StaticLibrary(BuildTarget):
if not hasattr(self, 'prefix'):
self.prefix = 'lib'
if not hasattr(self, 'suffix'):
- # Rust static library crates have .rlib suffix
if 'rust' in self.compilers:
- self.suffix = 'rlib'
+ if not hasattr(self, 'crate_type') or self.crate_type == 'rlib':
+ # default Rust static library suffix
+ self.suffix = 'rlib'
+ elif self.crate_type == 'staticlib':
+ self.suffix = 'a'
else:
self.suffix = 'a'
self.filename = self.prefix + self.name + '.' + self.suffix
@@ -1147,6 +1159,15 @@ class StaticLibrary(BuildTarget):
def check_unknown_kwargs(self, kwargs):
self.check_unknown_kwargs_int(kwargs, known_lib_kwargs)
+ def process_kwargs(self, kwargs, environment):
+ super().process_kwargs(kwargs, environment)
+ if 'crate_type' in kwargs:
+ crate_type = kwargs['crate_type']
+ if isinstance(crate_type, str):
+ self.crate_type = crate_type
+ else:
+ raise InvalidArguments('Invalid crate_type "{0}": must be a string.'.format(crate_type))
+
class SharedLibrary(BuildTarget):
def __init__(self, name, subdir, subproject, is_cross, sources, objects, environment, kwargs):
self.soversion = None
@@ -1159,6 +1180,14 @@ class SharedLibrary(BuildTarget):
# The import library that GCC would generate (and prefer)
self.gcc_import_filename = None
super().__init__(name, subdir, subproject, is_cross, sources, objects, environment, kwargs)
+ if 'rust' in self.compilers:
+ # If no crate type is specified, or it's the generic lib type, use dylib
+ if not hasattr(self, 'crate_type') or self.crate_type == 'lib':
+ mlog.debug('Defaulting Rust dynamic library target crate type to "dylib"')
+ self.crate_type = 'dylib'
+ # Don't let configuration proceed with a non-dynamic crate type
+ elif self.crate_type not in ['dylib', 'cdylib']:
+ raise InvalidArguments('Crate type "{0}" invalid for dynamic libraries; must be "dylib" or "cdylib"'.format(self.crate_type))
if not hasattr(self, 'prefix'):
self.prefix = None
if not hasattr(self, 'suffix'):
@@ -1200,12 +1229,6 @@ class SharedLibrary(BuildTarget):
prefix = ''
suffix = 'dll'
self.filename_tpl = '{0.prefix}{0.name}.{0.suffix}'
- # Rust
- elif 'rust' in self.compilers:
- # Currently, we always build --crate-type=rlib
- prefix = 'lib'
- suffix = 'rlib'
- self.filename_tpl = '{0.prefix}{0.name}.{0.suffix}'
# C, C++, Swift, Vala
# Only Windows uses a separate import library for linking
# For all other targets/platforms import_filename stays None
@@ -1315,6 +1338,12 @@ class SharedLibrary(BuildTarget):
raise InvalidArguments(
'Shared library vs_module_defs must be either a string, '
'a file object or a Custom Target')
+ if 'crate_type' in kwargs:
+ crate_type = kwargs['crate_type']
+ if isinstance(crate_type, str):
+ self.crate_type = crate_type
+ else:
+ raise InvalidArguments('Invalid crate_type "{0}": must be a string.'.format(crate_type))
def check_unknown_kwargs(self, kwargs):
self.check_unknown_kwargs_int(kwargs, known_lib_kwargs)
diff --git a/mesonbuild/compilers.py b/mesonbuild/compilers.py
index c9cfb46..6e7283c 100644
--- a/mesonbuild/compilers.py
+++ b/mesonbuild/compilers.py
@@ -1809,6 +1809,14 @@ class RustCompiler(Compiler):
def get_buildtype_args(self, buildtype):
return rust_buildtype_args[buildtype]
+ def build_rpath_args(self, build_dir, rpath_paths, install_rpath):
+ return build_unix_rpath_args(build_dir, rpath_paths, install_rpath)
+
+ def get_sysroot(self):
+ cmd = self.exelist + ['--print', 'sysroot']
+ p, stdo, stde = Popen_safe(cmd)
+ return stdo.split('\n')[0]
+
class SwiftCompiler(Compiler):
def __init__(self, exelist, version):
self.language = 'swift'