aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorXavier Claessens <xavier.claessens@collabora.com>2021-10-09 15:55:32 -0400
committerXavier Claessens <xclaesse@gmail.com>2022-10-24 11:06:57 +0200
commit01ee14133906e4afa55cdc52eeb1c9e78bbeced5 (patch)
treec0efa434aa9e75479943d4fcd18bae0721eea67f
parent4e374d5cefd25dfa0de8debf0c53e8c6f74380d9 (diff)
downloadmeson-01ee14133906e4afa55cdc52eeb1c9e78bbeced5.zip
meson-01ee14133906e4afa55cdc52eeb1c9e78bbeced5.tar.gz
meson-01ee14133906e4afa55cdc52eeb1c9e78bbeced5.tar.bz2
Add NASM compiler
-rw-r--r--docs/markdown/snippets/asm.md18
-rw-r--r--mesonbuild/build.py9
-rw-r--r--mesonbuild/compilers/asm.py77
-rw-r--r--mesonbuild/compilers/compilers.py3
-rw-r--r--mesonbuild/compilers/detect.py31
-rw-r--r--mesonbuild/envconfig.py1
-rw-r--r--test cases/nasm/2 asm language/hello.asm21
-rw-r--r--test cases/nasm/2 asm language/meson.build25
8 files changed, 184 insertions, 1 deletions
diff --git a/docs/markdown/snippets/asm.md b/docs/markdown/snippets/asm.md
new file mode 100644
index 0000000..be8b0cb
--- /dev/null
+++ b/docs/markdown/snippets/asm.md
@@ -0,0 +1,18 @@
+## New language `nasm`
+
+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.
+
+Note that GNU Assembly files usually have `.s` extension and were already built
+using C compiler such as GCC or CLANG.
+
+```meson
+project('test', 'nasm')
+
+exe = executable('hello', 'hello.asm')
+test('hello', exe)
+```
diff --git a/mesonbuild/build.py b/mesonbuild/build.py
index 9f4fe6a..4f9cf64 100644
--- a/mesonbuild/build.py
+++ b/mesonbuild/build.py
@@ -1568,6 +1568,15 @@ You probably should put it in link_with instead.''')
# Pretty hard to fix because the return value is passed everywhere
return linker, stdlib_args
+ # None of our compilers can do clink, this happens for example if the
+ # target only has ASM sources. Pick the first capable compiler.
+ for l in clink_langs:
+ try:
+ comp = self.all_compilers[l]
+ return comp, comp.language_stdlib_only_link_flags(self.environment)
+ except KeyError:
+ pass
+
raise AssertionError(f'Could not get a dynamic linker for build target {self.name!r}')
def uses_rust(self) -> bool:
diff --git a/mesonbuild/compilers/asm.py b/mesonbuild/compilers/asm.py
new file mode 100644
index 0000000..52ff779
--- /dev/null
+++ b/mesonbuild/compilers/asm.py
@@ -0,0 +1,77 @@
+import os
+import typing as T
+
+from ..mesonlib import EnvironmentException
+from .compilers import Compiler
+
+if T.TYPE_CHECKING:
+ from ..environment import Environment
+
+
+class NasmCompiler(Compiler):
+ language = 'nasm'
+ id = 'nasm'
+
+ def needs_static_linker(self) -> bool:
+ return True
+
+ def get_always_args(self) -> T.List[str]:
+ cpu = '64' if self.info.is_64_bit else '32'
+ if self.info.is_windows() or self.info.is_cygwin():
+ plat = 'win'
+ define = f'WIN{cpu}'
+ elif self.info.is_darwin():
+ plat = 'macho'
+ define = 'MACHO'
+ else:
+ plat = 'elf'
+ define = 'ELF'
+ args = ['-f', f'{plat}{cpu}', f'-D{define}']
+ if self.info.is_64_bit:
+ args.append('-D__x86_64__')
+ return args
+
+ def get_werror_args(self) -> T.List[str]:
+ return ['-Werror']
+
+ def get_output_args(self, outputname: str) -> T.List[str]:
+ return ['-o', outputname]
+
+ def get_optimization_args(self, optimization_level: str) -> T.List[str]:
+ return [f'-O{optimization_level}']
+
+ def get_debug_args(self, is_debug: bool) -> T.List[str]:
+ if is_debug:
+ if self.info.is_windows():
+ return []
+ return ['-g', '-F', 'dwarf']
+ return []
+
+ def get_depfile_suffix(self) -> str:
+ return 'd'
+
+ def get_dependency_gen_args(self, outtarget: str, outfile: str) -> T.List[str]:
+ return ['-MD', '-MQ', outtarget, '-MF', outfile]
+
+ def sanity_check(self, work_dir: str, environment: 'Environment') -> None:
+ if self.info.cpu_family not in {'x86', 'x86_64'}:
+ raise EnvironmentException(f'ASM compiler {self.id!r} does not support {self.info.cpu_family} CPU family')
+
+ def get_buildtype_args(self, buildtype: str) -> T.List[str]:
+ # FIXME: Not implemented
+ return []
+
+ def get_pic_args(self) -> T.List[str]:
+ return []
+
+ def get_include_args(self, path: str, is_system: bool) -> T.List[str]:
+ if not path:
+ path = '.'
+ return ['-I' + path]
+
+ def compute_parameters_with_absolute_paths(self, parameter_list: T.List[str],
+ build_dir: str) -> T.List[str]:
+ for idx, i in enumerate(parameter_list):
+ if i[:2] == '-I':
+ parameter_list[idx] = i[:2] + os.path.normpath(os.path.join(build_dir, i[2:]))
+ return parameter_list
diff --git a/mesonbuild/compilers/compilers.py b/mesonbuild/compilers/compilers.py
index b3191a8..147ca46 100644
--- a/mesonbuild/compilers/compilers.py
+++ b/mesonbuild/compilers/compilers.py
@@ -70,12 +70,13 @@ lang_suffixes = {
'swift': ('swift',),
'java': ('java',),
'cython': ('pyx', ),
+ 'nasm': ('asm',),
}
all_languages = lang_suffixes.keys()
c_cpp_suffixes = {'h'}
cpp_suffixes = set(lang_suffixes['cpp']) | c_cpp_suffixes
c_suffixes = set(lang_suffixes['c']) | c_cpp_suffixes
-assembler_suffixes = {'s', 'S'}
+assembler_suffixes = {'s', 'S', 'asm'}
llvm_ir_suffixes = {'ll'}
all_suffixes = set(itertools.chain(*lang_suffixes.values(), assembler_suffixes, llvm_ir_suffixes, c_cpp_suffixes))
source_suffixes = all_suffixes - header_suffixes
diff --git a/mesonbuild/compilers/detect.py b/mesonbuild/compilers/detect.py
index 5c63d2d..f5c232d 100644
--- a/mesonbuild/compilers/detect.py
+++ b/mesonbuild/compilers/detect.py
@@ -88,6 +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']
def compiler_from_language(env: 'Environment', lang: str, for_machine: MachineChoice) -> T.Optional[Compiler]:
@@ -105,6 +106,7 @@ def compiler_from_language(env: 'Environment', lang: str, for_machine: MachineCh
'fortran': detect_fortran_compiler,
'swift': detect_swift_compiler,
'cython': detect_cython_compiler,
+ 'nasm': detect_nasm_compiler,
}
return lang_map[lang](env, for_machine) if lang in lang_map else None
@@ -1134,6 +1136,35 @@ 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
+ compilers, _, _ = _get_compilers(env, 'nasm', for_machine)
+ is_cross = env.is_cross_build(for_machine)
+
+ # We need a C compiler to properly detect the machine info and linker
+ cc = detect_c_compiler(env, for_machine)
+ if not is_cross:
+ from ..environment import detect_machine_info
+ info = detect_machine_info({'c': cc})
+ else:
+ info = env.machines[for_machine]
+
+ popen_exceptions: T.Dict[str, Exception] = {}
+ for comp in compilers:
+ try:
+ output = Popen_safe(comp + ['--version'])[1]
+ except OSError as e:
+ popen_exceptions[' '.join(comp + ['--version'])] = e
+ continue
+
+ version = search_version(output)
+ if 'NASM' in output:
+ 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)
+ _handle_exceptions(popen_exceptions, compilers)
+ raise EnvironmentException('Unreachable code (exception to make mypy happy)')
+
# GNU/Clang defines and version
# =============================
diff --git a/mesonbuild/envconfig.py b/mesonbuild/envconfig.py
index d6d9c47..2069664 100644
--- a/mesonbuild/envconfig.py
+++ b/mesonbuild/envconfig.py
@@ -101,6 +101,7 @@ ENV_VAR_COMPILER_MAP: T.Mapping[str, str] = {
'objcpp': 'OBJCXX',
'rust': 'RUSTC',
'vala': 'VALAC',
+ 'nasm': 'NASM',
# Linkers
'c_ld': 'CC_LD',
diff --git a/test cases/nasm/2 asm language/hello.asm b/test cases/nasm/2 asm language/hello.asm
new file mode 100644
index 0000000..b153c8c
--- /dev/null
+++ b/test cases/nasm/2 asm language/hello.asm
@@ -0,0 +1,21 @@
+%include "config.asm"
+
+global main
+extern puts
+
+section .data
+ hi db 'Hello, World', 0
+
+%ifdef FOO
+%define RETVAL HELLO
+%endif
+
+section .text
+main:
+ push rbp
+ lea rdi, [rel hi]
+ call puts wrt ..plt
+ pop rbp
+ mov ebx,RETVAL
+ mov eax,1
+ int 0x80
diff --git a/test cases/nasm/2 asm language/meson.build b/test cases/nasm/2 asm language/meson.build
new file mode 100644
index 0000000..1fe0671
--- /dev/null
+++ b/test cases/nasm/2 asm language/meson.build
@@ -0,0 +1,25 @@
+project('test', 'c')
+
+nasm = find_program('nasm', required: false)
+if not nasm.found()
+ assert(not add_languages('nasm', required: false))
+ error('MESON_SKIP_TEST: nasm not available')
+endif
+
+if not host_machine.cpu_family().startswith('x86')
+ assert(not add_languages('nasm', required: false))
+ error('MESON_SKIP_TEST: nasm only supported for x86 and x86_64')
+endif
+
+add_languages('nasm')
+
+config_file = configure_file(
+ output: 'config.asm',
+ configuration: {'HELLO': 0},
+ output_format: 'nasm',
+)
+
+exe = executable('hello', 'hello.asm',
+ nasm_args: '-DFOO',
+)
+test('hello', exe)