From d29ef2b128c651fa83f1d923290d66cc8eb63223 Mon Sep 17 00:00:00 2001 From: Xavier Claessens Date: Wed, 9 Mar 2022 12:34:04 -0500 Subject: [PATCH] Add yasm as fallback for nasm language --- docs/markdown/snippets/asm.md | 5 +---- mesonbuild/compilers/asm.py | 20 +++++++++++++++++++- mesonbuild/compilers/detect.py | 8 ++++++-- mesonbuild/scripts/yasm.py | 22 ++++++++++++++++++++++ test cases/nasm/2 asm language/meson.build | 3 ++- 5 files changed, 50 insertions(+), 8 deletions(-) create mode 100644 mesonbuild/scripts/yasm.py diff --git a/docs/markdown/snippets/asm.md b/docs/markdown/snippets/asm.md index be8b0cb48..a68f1631f 100644 --- a/docs/markdown/snippets/asm.md +++ b/docs/markdown/snippets/asm.md @@ -2,10 +2,7 @@ When the `nasm` language is added to the project, `.asm` files are automatically compiled with NASM. This is only supported for x86 and x86_64 CPU -family. - -Support for other compilers compatible with NASM language, such as YASM, could -be added in the future. +family. `yasm` is used as fallback if `nasm` command is not found. Note that GNU Assembly files usually have `.s` extension and were already built using C compiler such as GCC or CLANG. diff --git a/mesonbuild/compilers/asm.py b/mesonbuild/compilers/asm.py index 52ff7791d..4135a5b0f 100644 --- a/mesonbuild/compilers/asm.py +++ b/mesonbuild/compilers/asm.py @@ -1,7 +1,7 @@ import os import typing as T -from ..mesonlib import EnvironmentException +from ..mesonlib import EnvironmentException, get_meson_command from .compilers import Compiler if T.TYPE_CHECKING: @@ -75,3 +75,21 @@ class NasmCompiler(Compiler): if i[:2] == '-I': parameter_list[idx] = i[:2] + os.path.normpath(os.path.join(build_dir, i[2:])) return parameter_list + +class YasmCompiler(NasmCompiler): + id = 'yasm' + + def get_exelist(self) -> T.List[str]: + # Wrap yasm executable with an internal script that will write depfile. + exelist = super().get_exelist() + return get_meson_command() + ['--internal', 'yasm'] + exelist + + def get_debug_args(self, is_debug: bool) -> T.List[str]: + if is_debug: + if self.info.is_windows(): + return ['-g', 'null'] + return ['-g', 'dwarf2'] + return [] + + def get_dependency_gen_args(self, outtarget: str, outfile: str) -> T.List[str]: + return ['--depfile', outfile] diff --git a/mesonbuild/compilers/detect.py b/mesonbuild/compilers/detect.py index f5c232dd8..8644d75c0 100644 --- a/mesonbuild/compilers/detect.py +++ b/mesonbuild/compilers/detect.py @@ -88,7 +88,7 @@ defaults['clang_cl_static_linker'] = ['llvm-lib'] defaults['cuda_static_linker'] = ['nvlink'] defaults['gcc_static_linker'] = ['gcc-ar'] defaults['clang_static_linker'] = ['llvm-ar'] -defaults['nasm'] = ['nasm'] +defaults['nasm'] = ['nasm', 'yasm'] def compiler_from_language(env: 'Environment', lang: str, for_machine: MachineChoice) -> T.Optional[Compiler]: @@ -1137,7 +1137,7 @@ def detect_swift_compiler(env: 'Environment', for_machine: MachineChoice) -> Com raise EnvironmentException('Unknown compiler: ' + join_args(exelist)) def detect_nasm_compiler(env: 'Environment', for_machine: MachineChoice) -> Compiler: - from .asm import NasmCompiler + from .asm import NasmCompiler, YasmCompiler compilers, _, _ = _get_compilers(env, 'nasm', for_machine) is_cross = env.is_cross_build(for_machine) @@ -1162,6 +1162,10 @@ def detect_nasm_compiler(env: 'Environment', for_machine: MachineChoice) -> Comp comp_class = NasmCompiler env.coredata.add_lang_args(comp_class.language, comp_class, for_machine, env) return comp_class(comp, version, for_machine, info, cc.linker, is_cross=is_cross) + elif 'yasm' in output: + comp_class = YasmCompiler + env.coredata.add_lang_args(comp_class.language, comp_class, for_machine, env) + return comp_class(comp, version, for_machine, info, cc.linker, is_cross=is_cross) _handle_exceptions(popen_exceptions, compilers) raise EnvironmentException('Unreachable code (exception to make mypy happy)') diff --git a/mesonbuild/scripts/yasm.py b/mesonbuild/scripts/yasm.py new file mode 100644 index 000000000..730ff3e16 --- /dev/null +++ b/mesonbuild/scripts/yasm.py @@ -0,0 +1,22 @@ +import argparse +import subprocess +import typing as T + +def run(args: T.List[str]) -> int: + parser = argparse.ArgumentParser() + parser.add_argument('--depfile') + options, yasm_cmd = parser.parse_known_args(args) + + # Compile + returncode = subprocess.call(yasm_cmd) + if returncode != 0: + return returncode + + # Capture and write depfile + ret = subprocess.run(yasm_cmd + ['-M'], capture_output=True) + if ret.returncode != 0: + return ret.returncode + with open(options.depfile, 'wb') as f: + f.write(ret.stdout) + + return 0 diff --git a/test cases/nasm/2 asm language/meson.build b/test cases/nasm/2 asm language/meson.build index 1fe06715f..ced2ded62 100644 --- a/test cases/nasm/2 asm language/meson.build +++ b/test cases/nasm/2 asm language/meson.build @@ -1,7 +1,8 @@ project('test', 'c') nasm = find_program('nasm', required: false) -if not nasm.found() +yasm = find_program('yasm', required: false) +if not nasm.found() and not yasm.found() assert(not add_languages('nasm', required: false)) error('MESON_SKIP_TEST: nasm not available') endif