aboutsummaryrefslogtreecommitdiff
path: root/mesonbuild
diff options
context:
space:
mode:
authorJussi Pakkanen <jpakkane@gmail.com>2020-03-12 00:57:52 +0200
committerGitHub <noreply@github.com>2020-03-12 00:57:52 +0200
commit88e40c7081b9bf8a337022ad2f12efe485fa8021 (patch)
tree66585d887ea72954270a7836afb2f3afaba1ab16 /mesonbuild
parent85489ab6c311cc7b72595479eafff78b36176fd1 (diff)
parentd0172432a7eb95f1f5d16825d4440bc7e2da758f (diff)
downloadmeson-88e40c7081b9bf8a337022ad2f12efe485fa8021.zip
meson-88e40c7081b9bf8a337022ad2f12efe485fa8021.tar.gz
meson-88e40c7081b9bf8a337022ad2f12efe485fa8021.tar.bz2
Merge pull request #6356 from dcbaker/fix-d-compiler-abstractions
Fix d compiler abstractions
Diffstat (limited to 'mesonbuild')
-rw-r--r--mesonbuild/compilers/compilers.py19
-rw-r--r--mesonbuild/compilers/d.py85
-rw-r--r--mesonbuild/compilers/mixins/visualstudio.py4
-rw-r--r--mesonbuild/environment.py90
-rw-r--r--mesonbuild/linkers.py15
5 files changed, 141 insertions, 72 deletions
diff --git a/mesonbuild/compilers/compilers.py b/mesonbuild/compilers/compilers.py
index 366bb50..e13256e 100644
--- a/mesonbuild/compilers/compilers.py
+++ b/mesonbuild/compilers/compilers.py
@@ -536,6 +536,15 @@ class CompilerArgs(collections.abc.MutableSequence):
# both of which are invalid.
if arg in cls.dedup2_prefixes:
return 0
+ if arg.startswith('-L='):
+ # DMD and LDC proxy all linker arguments using -L=; in conjunction
+ # with ld64 on macOS this can lead to command line arguments such
+ # as: `-L=-compatibility_version -L=0 -L=current_version -L=0`.
+ # These cannot be combined, ld64 insists they must be passed with
+ # spaces and quoting does not work. if we deduplicate these then
+ # one of the -L=0 arguments will be removed and the version
+ # argument will consume the next argument instead.
+ return 0
if arg in cls.dedup2_args or \
arg.startswith(cls.dedup2_prefixes) or \
arg.endswith(cls.dedup2_suffixes):
@@ -571,7 +580,17 @@ class CompilerArgs(collections.abc.MutableSequence):
isinstance(self.compiler.linker, (GnuLikeDynamicLinkerMixin, SolarisDynamicLinker))):
group_start = -1
group_end = -1
+ is_soname = False
for i, each in enumerate(new):
+ if is_soname:
+ is_soname = False
+ continue
+ elif '-soname' in each:
+ # To proxy these arguments with D you need to split the
+ # arguments, thus you get `-L=-soname -L=lib.so` we don't
+ # want to put the lib in a link -roup
+ is_soname = True
+ continue
if not each.startswith(('-Wl,-l', '-l')) and not each.endswith('.a') and \
not soregex.match(each):
continue
diff --git a/mesonbuild/compilers/d.py b/mesonbuild/compilers/d.py
index b974504..9a46a4e 100644
--- a/mesonbuild/compilers/d.py
+++ b/mesonbuild/compilers/d.py
@@ -28,7 +28,6 @@ from .compilers import (
CompilerArgs,
)
from .mixins.gnu import GnuCompiler
-from .mixins.islinker import LinkerEnvVarsMixin, BasicLinkerIsCompilerMixin
if T.TYPE_CHECKING:
from ..envconfig import MachineInfo
@@ -69,7 +68,7 @@ dmd_optimization_args = {'0': [],
class DmdLikeCompilerMixin:
- LINKER_PREFIX = '-L'
+ LINKER_PREFIX = '-L='
def get_output_args(self, target):
return ['-of=' + target]
@@ -214,26 +213,30 @@ class DmdLikeCompilerMixin:
return []
def gen_import_library_args(self, implibname):
- return ['-Wl,--out-implib=' + implibname]
+ return self.linker.import_library_args(implibname)
def build_rpath_args(self, env, build_dir, from_dir, rpath_paths, build_rpath, install_rpath):
if self.info.is_windows():
return []
- # This method is to be used by LDC and DMD.
- # GDC can deal with the verbatim flags.
- if not rpath_paths and not install_rpath:
- return []
- paths = ':'.join([os.path.join(build_dir, p) for p in rpath_paths])
- if build_rpath != '':
- paths += ':' + build_rpath
- if len(paths) < len(install_rpath):
- padding = 'X' * (len(install_rpath) - len(paths))
- if not paths:
- paths = padding
- else:
- paths = paths + ':' + padding
- return ['-Wl,-rpath,{}'.format(paths)]
+ # GNU ld, solaris ld, and lld acting like GNU ld
+ if self.linker.id.startswith('ld'):
+ # The way that dmd and ldc pass rpath to gcc is different than we would
+ # do directly, each argument -rpath and the value to rpath, need to be
+ # split into two separate arguments both prefaced with the -L=.
+ args = []
+ for r in super().build_rpath_args(
+ env, build_dir, from_dir, rpath_paths, build_rpath, install_rpath):
+ if ',' in r:
+ a, b = r.split(',', maxsplit=1)
+ args.append(a)
+ args.append(self.LINKER_PREFIX + b)
+ else:
+ args.append(r)
+ return args
+
+ return super().build_rpath_args(
+ env, build_dir, from_dir, rpath_paths, build_rpath, install_rpath)
def translate_args_to_nongnu(self, args):
dcargs = []
@@ -391,15 +394,33 @@ class DmdLikeCompilerMixin:
def get_soname_args(self, *args, **kwargs) -> T.List[str]:
# LDC and DMD actually do use a linker, but they proxy all of that with
# their own arguments
- soargs = []
- for arg in Compiler.get_soname_args(self, *args, **kwargs):
- soargs.append('-L=' + arg)
- return soargs
+ if self.linker.id.startswith('ld.'):
+ soargs = []
+ for arg in super().get_soname_args(*args, **kwargs):
+ a, b = arg.split(',', maxsplit=1)
+ soargs.append(a)
+ soargs.append(self.LINKER_PREFIX + b)
+ return soargs
+ elif self.linker.id.startswith('ld64'):
+ soargs = []
+ for arg in super().get_soname_args(*args, **kwargs):
+ if not arg.startswith(self.LINKER_PREFIX):
+ soargs.append(self.LINKER_PREFIX + arg)
+ else:
+ soargs.append(arg)
+ return soargs
+ else:
+ return super().get_soname_args(*args, **kwargs)
def get_allow_undefined_link_args(self) -> T.List[str]:
- args = []
- for arg in self.linker.get_allow_undefined_args():
- args.append('-L=' + arg)
+ args = self.linker.get_allow_undefined_args()
+ if self.info.is_darwin():
+ # On macOS we're passing these options to the C compiler, but
+ # they're linker options and need -Wl, so clang/gcc knows what to
+ # do with them. I'm assuming, but don't know for certain, that
+ # ldc/dmd do some kind of mapping internally for arguments they
+ # understand, but pass arguments they don't understand directly.
+ args = [a.replace('-L=', '-Xcc=-Wl,') for a in args]
return args
@@ -600,7 +621,7 @@ class DCompiler(Compiler):
return []
def thread_link_flags(self, env):
- return ['-pthread']
+ return self.linker.thread_flags(env)
def name_string(self):
return ' '.join(self.exelist)
@@ -658,7 +679,7 @@ class GnuDCompiler(DCompiler, GnuCompiler):
return self.linker.get_allow_undefined_args()
-class LLVMDCompiler(DmdLikeCompilerMixin, LinkerEnvVarsMixin, BasicLinkerIsCompilerMixin, DCompiler):
+class LLVMDCompiler(DmdLikeCompilerMixin, DCompiler):
def __init__(self, exelist, version, for_machine: MachineChoice,
info: 'MachineInfo', arch, **kwargs):
@@ -687,9 +708,6 @@ class LLVMDCompiler(DmdLikeCompilerMixin, LinkerEnvVarsMixin, BasicLinkerIsCompi
def get_pic_args(self):
return ['-relocation-model=pic']
- def get_std_shared_lib_link_args(self):
- return ['-shared']
-
def get_crt_link_args(self, crt_val, buildtype):
return self.get_crt_args(crt_val, buildtype)
@@ -699,8 +717,12 @@ class LLVMDCompiler(DmdLikeCompilerMixin, LinkerEnvVarsMixin, BasicLinkerIsCompi
def get_optimization_args(self, optimization_level):
return ldc_optimization_args[optimization_level]
+ @classmethod
+ def use_linker_args(cls, linker: str) -> T.List[str]:
+ return ['-linker={}'.format(linker)]
+
-class DmdDCompiler(DmdLikeCompilerMixin, LinkerEnvVarsMixin, BasicLinkerIsCompilerMixin, DCompiler):
+class DmdDCompiler(DmdLikeCompilerMixin, DCompiler):
def __init__(self, exelist, version, for_machine: MachineChoice,
info: 'MachineInfo', arch, **kwargs):
@@ -760,3 +782,6 @@ class DmdDCompiler(DmdLikeCompilerMixin, LinkerEnvVarsMixin, BasicLinkerIsCompil
def get_optimization_args(self, optimization_level):
return dmd_optimization_args[optimization_level]
+
+ def can_linker_accept_rsp(self) -> bool:
+ return False
diff --git a/mesonbuild/compilers/mixins/visualstudio.py b/mesonbuild/compilers/mixins/visualstudio.py
index a994379..44aefc8 100644
--- a/mesonbuild/compilers/mixins/visualstudio.py
+++ b/mesonbuild/compilers/mixins/visualstudio.py
@@ -205,10 +205,6 @@ class VisualStudioLikeCompiler(metaclass=abc.ABCMeta):
objname = os.path.splitext(pchname)[0] + '.obj'
return objname, ['/Yc' + header, '/Fp' + pchname, '/Fo' + objname]
- def gen_import_library_args(self, implibname: str) -> T.List[str]:
- "The name of the outputted import library"
- return ['/IMPLIB:' + implibname]
-
def openmp_flags(self) -> T.List[str]:
return ['/openmp']
diff --git a/mesonbuild/environment.py b/mesonbuild/environment.py
index b995a59..f25c671 100644
--- a/mesonbuild/environment.py
+++ b/mesonbuild/environment.py
@@ -752,7 +752,8 @@ class Environment:
def _guess_win_linker(self, compiler: T.List[str], comp_class: Compiler,
for_machine: MachineChoice, *,
- use_linker_prefix: bool = True) -> 'DynamicLinker':
+ use_linker_prefix: bool = True, invoked_directly: bool = True,
+ extra_args: T.Optional[T.List[str]] = None) -> 'DynamicLinker':
self.coredata.add_lang_args(comp_class.language, comp_class, for_machine, self)
# Explicitly pass logo here so that we can get the version of link.exe
@@ -771,25 +772,29 @@ class Environment:
override = comp_class.use_linker_args(value[0])
check_args += override
+ if extra_args is not None:
+ check_args.extend(extra_args)
+
p, o, _ = Popen_safe(compiler + check_args)
if o.startswith('LLD'):
if '(compatible with GNU linkers)' in o:
return LLVMDynamicLinker(
compiler, for_machine, comp_class.LINKER_PREFIX,
- override, version=search_version(o))
-
- if value is not None:
+ override, version=search_version(o), direct=invoked_directly)
+
+ if value is not None and invoked_directly:
compiler = value
+ # We've already hanedled the non-direct case above
p, o, e = Popen_safe(compiler + check_args)
if o.startswith('LLD'):
return ClangClDynamicLinker(
for_machine, [],
prefix=comp_class.LINKER_PREFIX if use_linker_prefix else [],
- exelist=compiler, version=search_version(o))
+ exelist=compiler, version=search_version(o), direct=invoked_directly)
elif 'OPTLINK' in o:
# Opltink's stdout *may* beging with a \r character.
- return OptlinkDynamicLinker(for_machine, version=search_version(o))
+ return OptlinkDynamicLinker(compiler, for_machine, version=search_version(o))
elif o.startswith('Microsoft') or e.startswith('Microsoft'):
out = o or e
match = re.search(r'.*(X86|X64|ARM|ARM64).*', out)
@@ -801,7 +806,7 @@ class Environment:
return MSVCDynamicLinker(
for_machine, [], machine=target, exelist=compiler,
prefix=comp_class.LINKER_PREFIX if use_linker_prefix else [],
- version=search_version(out))
+ version=search_version(out), direct=invoked_directly)
elif 'GNU coreutils' in o:
raise EnvironmentException(
"Found GNU link.exe instead of MSVC link.exe. This link.exe "
@@ -1496,28 +1501,31 @@ class Environment:
if 'LLVM D compiler' in out:
# LDC seems to require a file
- if info.is_windows() or info.is_cygwin():
- # Getting LDC on windows to give useful linker output when
- # not doing real work is painfully hard. It ships with a
- # version of lld-link, so unless we think the user wants
- # link.exe, just assume that we're going to use lld-link
- # with it.
- linker = self._guess_win_linker(
- ['link' if is_msvc else 'lld-link'],
- compilers.LLVMDCompiler, for_machine, use_linker_prefix=False)
- else:
- with tempfile.NamedTemporaryFile(suffix='.d') as f:
+ # We cannot use NamedTemproraryFile on windows, its documented
+ # to not work for our uses. So, just use mkstemp and only have
+ # one path for simplicity.
+ o, f = tempfile.mkstemp('.d')
+ os.close(o)
+
+ try:
+ if info.is_windows() or info.is_cygwin():
+ objfile = os.path.basename(f)[:-1] + 'obj'
+ linker = self._guess_win_linker(
+ exelist,
+ compilers.LLVMDCompiler, for_machine,
+ use_linker_prefix=True, invoked_directly=False,
+ extra_args=[f])
+ else:
# LDC writes an object file to the current working directory.
# Clean it up.
- objectfile = os.path.basename(f.name)[:-1] + 'o'
+ objfile = os.path.basename(f)[:-1] + 'o'
linker = self._guess_nix_linker(
exelist, compilers.LLVMDCompiler, for_machine,
- extra_args=[f.name])
- try:
- os.unlink(objectfile)
- except Exception:
- # Thank you Windows file system semantics and virus scanners.
- pass
+ extra_args=[f])
+ finally:
+ mesonlib.windows_proof_rm(f)
+ mesonlib.windows_proof_rm(objfile)
+
return compilers.LLVMDCompiler(
exelist, version, for_machine, info, arch,
full_version=full_version, linker=linker)
@@ -1528,20 +1536,30 @@ class Environment:
full_version=full_version, linker=linker)
elif 'The D Language Foundation' in out or 'Digital Mars' in out:
# DMD seems to require a file
- if info.is_windows() or info.is_cygwin():
- if is_msvc:
- linker_cmd = ['link']
- elif arch == 'x86':
- linker_cmd = ['optlink']
+ # We cannot use NamedTemproraryFile on windows, its documented
+ # to not work for our uses. So, just use mkstemp and only have
+ # one path for simplicity.
+ o, f = tempfile.mkstemp('.d')
+ os.close(o)
+
+ # DMD as different detection logic for x86 and x86_64
+ arch_arg = '-m64' if arch == 'x86_64' else '-m32'
+
+ try:
+ if info.is_windows() or info.is_cygwin():
+ objfile = os.path.basename(f)[:-1] + 'obj'
+ linker = self._guess_win_linker(
+ exelist, compilers.DmdDCompiler, for_machine,
+ invoked_directly=False, extra_args=[f, arch_arg])
else:
- linker_cmd = ['lld-link']
- linker = self._guess_win_linker(linker_cmd, compilers.DmdDCompiler, for_machine,
- use_linker_prefix=False)
- else:
- with tempfile.NamedTemporaryFile(suffix='.d') as f:
+ objfile = os.path.basename(f)[:-1] + 'o'
linker = self._guess_nix_linker(
exelist, compilers.DmdDCompiler, for_machine,
- extra_args=[f.name])
+ extra_args=[f, arch_arg])
+ finally:
+ mesonlib.windows_proof_rm(f)
+ mesonlib.windows_proof_rm(objfile)
+
return compilers.DmdDCompiler(
exelist, version, for_machine, info, arch,
full_version=full_version, linker=linker)
diff --git a/mesonbuild/linkers.py b/mesonbuild/linkers.py
index 73cdeef..e4fffe3 100644
--- a/mesonbuild/linkers.py
+++ b/mesonbuild/linkers.py
@@ -901,6 +901,10 @@ class VisualStudioLikeLinkerMixin:
is_shared_module: bool) -> T.List[str]:
return []
+ def import_library_args(self, implibname: str) -> T.List[str]:
+ """The command to generate the import library."""
+ return self._apply_prefix(['/IMPLIB:' + implibname])
+
class MSVCDynamicLinker(VisualStudioLikeLinkerMixin, DynamicLinker):
@@ -993,15 +997,22 @@ class OptlinkDynamicLinker(VisualStudioLikeLinkerMixin, DynamicLinker):
"""Digital Mars dynamic linker for windows."""
- def __init__(self, for_machine: mesonlib.MachineChoice,
+ def __init__(self, exelist: T.List[str], for_machine: mesonlib.MachineChoice,
*, version: str = 'unknown version'):
# Use optlink instead of link so we don't interfer with other link.exe
# implementations.
- super().__init__('optlink', ['optlink.exe'], for_machine, '', [], version=version)
+ super().__init__('optlink', exelist, for_machine, '', [], version=version)
def get_allow_undefined_args(self) -> T.List[str]:
return []
+ def get_debugfile_args(self, targetfile: str) -> T.List[str]:
+ # Optlink does not generate pdb files.
+ return []
+
+ def get_always_args(self) -> T.List[str]:
+ return []
+
class CudaLinker(PosixDynamicLinkerMixin, DynamicLinker):
"""Cuda linker (nvlink)"""