Add NASM compiler

pull/10922/head
Xavier Claessens 3 years ago committed by Xavier Claessens
parent 4e374d5cef
commit 01ee141339
  1. 18
      docs/markdown/snippets/asm.md
  2. 9
      mesonbuild/build.py
  3. 77
      mesonbuild/compilers/asm.py
  4. 3
      mesonbuild/compilers/compilers.py
  5. 31
      mesonbuild/compilers/detect.py
  6. 1
      mesonbuild/envconfig.py
  7. 21
      test cases/nasm/2 asm language/hello.asm
  8. 25
      test cases/nasm/2 asm language/meson.build

@ -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)
```

@ -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:

@ -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

@ -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

@ -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
# =============================

@ -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',

@ -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

@ -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)
Loading…
Cancel
Save