nasm: Link with windows CRT libs when nasm is used as linker language

This commit is contained in:
Xavier Claessens 2023-02-16 12:14:21 -05:00 committed by Xavier Claessens
parent 2fd39dc2b0
commit 744e6ebe1d
6 changed files with 82 additions and 2 deletions

View File

@ -1,11 +1,14 @@
import os
import typing as T
from ..mesonlib import EnvironmentException, get_meson_command
from ..mesonlib import EnvironmentException, OptionKey, get_meson_command
from .compilers import Compiler
if T.TYPE_CHECKING:
from ..environment import Environment
from ..linkers import DynamicLinker
from ..mesonlib import MachineChoice
from ..envconfig import MachineInfo
nasm_optimization_args = {
'plain': [],
@ -22,6 +25,23 @@ class NasmCompiler(Compiler):
language = 'nasm'
id = 'nasm'
# https://learn.microsoft.com/en-us/cpp/c-runtime-library/crt-library-features
crt_args: T.Dict[str, T.List[str]] = {
'none': [],
'md': ['/DEFAULTLIB:ucrt.lib', '/DEFAULTLIB:vcruntime.lib', '/DEFAULTLIB:msvcrt.lib'],
'mdd': ['/DEFAULTLIB:ucrtd.lib', '/DEFAULTLIB:vcruntimed.lib', '/DEFAULTLIB:msvcrtd.lib'],
'mt': ['/DEFAULTLIB:libucrt.lib', '/DEFAULTLIB:libvcruntime.lib', '/DEFAULTLIB:libcmt.lib'],
'mtd': ['/DEFAULTLIB:libucrtd.lib', '/DEFAULTLIB:libvcruntimed.lib', '/DEFAULTLIB:libcmtd.lib'],
}
def __init__(self, ccache: T.List[str], exelist: T.List[str], version: str,
for_machine: 'MachineChoice', info: 'MachineInfo',
linker: T.Optional['DynamicLinker'] = None,
full_version: T.Optional[str] = None, is_cross: bool = False):
super().__init__(ccache, exelist, version, for_machine, info, linker, full_version, is_cross)
if 'link' in self.linker.id:
self.base_options.add(OptionKey('b_vscrt'))
def needs_static_linker(self) -> bool:
return True
@ -97,6 +117,35 @@ class NasmCompiler(Compiler):
def get_crt_compile_args(self, crt_val: str, buildtype: str) -> T.List[str]:
return []
# Linking ASM-only objects into an executable or DLL
# require this, otherwise it'll fail to find
# _WinMain or _DllMainCRTStartup.
def get_crt_link_args(self, crt_val: str, buildtype: str) -> T.List[str]:
if not self.info.is_windows():
return []
if crt_val in self.crt_args:
return self.crt_args[crt_val]
assert crt_val in {'from_buildtype', 'static_from_buildtype'}
dbg = 'mdd'
rel = 'md'
if crt_val == 'static_from_buildtype':
dbg = 'mtd'
rel = 'mt'
# Match what build type flags used to do.
if buildtype == 'plain':
return []
elif buildtype == 'debug':
return self.crt_args[dbg]
elif buildtype == 'debugoptimized':
return self.crt_args[rel]
elif buildtype == 'release':
return self.crt_args[rel]
elif buildtype == 'minsize':
return self.crt_args[rel]
else:
assert buildtype == 'custom'
raise EnvironmentException('Requested C runtime based on buildtype, but buildtype is "custom".')
class YasmCompiler(NasmCompiler):
id = 'yasm'

View File

@ -84,7 +84,7 @@ source_suffixes = all_suffixes - header_suffixes
# List of languages that by default consume and output libraries following the
# C ABI; these can generally be used interchangeably
# This must be sorted, see sort_clink().
clib_langs = ('objcpp', 'cpp', 'objc', 'c', 'fortran')
clib_langs = ('objcpp', 'cpp', 'objc', 'c', 'nasm', 'fortran')
# List of languages that can be linked with C code directly by the linker
# used in build.py:process_compilers() and build.py:get_dynamic_linker()
# This must be sorted, see sort_clink().

View File

@ -5,6 +5,14 @@ if not host_machine.cpu_family().startswith('x86')
error('MESON_SKIP_TEST: nasm only supported for x86 and x86_64')
endif
if host_machine.system() == 'windows'
error('MESON_SKIP_TEST: this test asm is not made for Windows')
endif
if meson.backend().startswith('vs')
error('MESON_SKIP_TEST: VS backend does not recognise NASM yet')
endif
if not add_languages('nasm', required: false)
nasm = find_program('nasm', 'yasm', required: false)
assert(not nasm.found())

View File

@ -0,0 +1,4 @@
global dummy
section .rdata align=16
dummy:
dd 0x00010203

View File

@ -0,0 +1,2 @@
EXPORTS
dummy

View File

@ -0,0 +1,17 @@
project('nasm only')
if not add_languages('nasm', required: false)
error('MESON_SKIP_TEST: nasm not found')
endif
if meson.backend().startswith('vs')
error('MESON_SKIP_TEST: VS backend does not recognise NASM yet')
endif
sources = files('dummy.asm')
dummy = library(
'dummy',
sources,
vs_module_defs: 'dummy.def',
)