Skip to content

Commit

Permalink
Remove duplicated code to canonicalize b_vscrt option value
Browse files Browse the repository at this point in the history
Add a common function that infers vscrt from buildtype in Compiler base
class.
  • Loading branch information
xclaesse committed Oct 19, 2023
1 parent 890dd31 commit 361f748
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 95 deletions.
9 changes: 1 addition & 8 deletions mesonbuild/backend/ninjabackend.py
Original file line number Diff line number Diff line change
Expand Up @@ -1973,13 +1973,7 @@ def generate_rust_target(self, target: build.BuildTarget) -> None:
try:
buildtype = target.get_option(OptionKey('buildtype'))
crt = target.get_option(OptionKey('b_vscrt'))
is_debug = buildtype == 'debug'

if crt == 'from_buildtype':
crt = 'mdd' if is_debug else 'md'
elif crt == 'static_from_buildtype':
crt = 'mtd' if is_debug else 'mt'

crt = rustc.get_crt_val(crt, buildtype)
if crt == 'mdd':
crt_link_args = ['-l', 'static=msvcrtd']
elif crt == 'md':
Expand All @@ -1989,7 +1983,6 @@ def generate_rust_target(self, target: build.BuildTarget) -> None:
crt_link_args = ['-l', 'static=libcmtd']
elif crt == 'mt':
crt_link_args = ['-l', 'static=libcmt']

except KeyError:
crt_args_injected = True

Expand Down
21 changes: 4 additions & 17 deletions mesonbuild/backend/vs2010backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -1311,28 +1311,15 @@ def add_non_makefile_vcxproj_elements(
ET.SubElement(clconf, 'OpenMPSupport').text = 'true'
# CRT type; debug or release
vscrt_type = target.get_option(OptionKey('b_vscrt'))
if vscrt_type == 'from_buildtype':
if self.buildtype == 'debug':
ET.SubElement(type_config, 'UseDebugLibraries').text = 'true'
ET.SubElement(clconf, 'RuntimeLibrary').text = 'MultiThreadedDebugDLL'
else:
ET.SubElement(type_config, 'UseDebugLibraries').text = 'false'
ET.SubElement(clconf, 'RuntimeLibrary').text = 'MultiThreadedDLL'
elif vscrt_type == 'static_from_buildtype':
if self.buildtype == 'debug':
ET.SubElement(type_config, 'UseDebugLibraries').text = 'true'
ET.SubElement(clconf, 'RuntimeLibrary').text = 'MultiThreadedDebug'
else:
ET.SubElement(type_config, 'UseDebugLibraries').text = 'false'
ET.SubElement(clconf, 'RuntimeLibrary').text = 'MultiThreaded'
elif vscrt_type == 'mdd':
vscrt_val = compiler.get_crt_val(vscrt_type, self.buildtype)
if vscrt_val == 'mdd':
ET.SubElement(type_config, 'UseDebugLibraries').text = 'true'
ET.SubElement(clconf, 'RuntimeLibrary').text = 'MultiThreadedDebugDLL'
elif vscrt_type == 'mt':
elif vscrt_val == 'mt':
# FIXME, wrong
ET.SubElement(type_config, 'UseDebugLibraries').text = 'false'
ET.SubElement(clconf, 'RuntimeLibrary').text = 'MultiThreaded'
elif vscrt_type == 'mtd':
elif vscrt_val == 'mtd':
# FIXME, wrong
ET.SubElement(type_config, 'UseDebugLibraries').text = 'true'
ET.SubElement(clconf, 'RuntimeLibrary').text = 'MultiThreadedDebug'
Expand Down
23 changes: 1 addition & 22 deletions mesonbuild/compilers/asm.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,28 +124,7 @@ def get_crt_compile_args(self, crt_val: str, buildtype: str) -> T.List[str]:
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".')
return self.crt_args[self.get_crt_val(crt_val, buildtype)]

class YasmCompiler(NasmCompiler):
id = 'yasm'
Expand Down
26 changes: 25 additions & 1 deletion mesonbuild/compilers/compilers.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,8 @@ class CompileCheckMode(enum.Enum):
True: ['-g']
}

MSCRT_VALS = ['none', 'md', 'mdd', 'mt', 'mtd']

base_options: 'KeyedOptionDictType' = {
OptionKey('b_pch'): coredata.UserBooleanOption('Use precompiled headers', True),
OptionKey('b_lto'): coredata.UserBooleanOption('Use link time optimization', False),
Expand All @@ -325,7 +327,7 @@ class CompileCheckMode(enum.Enum):
OptionKey('b_pie'): coredata.UserBooleanOption('Build executables as position independent', False),
OptionKey('b_bitcode'): coredata.UserBooleanOption('Generate and embed bitcode (only macOS/iOS/tvOS)', False),
OptionKey('b_vscrt'): coredata.UserComboOption('VS run-time library type to use.',
['none', 'md', 'mdd', 'mt', 'mtd', 'from_buildtype', 'static_from_buildtype'],
MSCRT_VALS + ['from_buildtype', 'static_from_buildtype'],
'from_buildtype'),
}

Expand Down Expand Up @@ -1105,6 +1107,28 @@ def get_assert_args(self, disable: bool) -> T.List[str]:
"""
return []

def get_crt_val(self, crt_val: str, buildtype: str) -> str:
if crt_val in MSCRT_VALS:
return 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 'none'
elif buildtype == 'debug':
return dbg
elif buildtype in {'debugoptimized', 'release', 'minsize'}:
return rel
else:
assert buildtype == 'custom'
raise EnvironmentException('Requested C runtime based on buildtype, but buildtype is "custom".')

def get_crt_compile_args(self, crt_val: str, buildtype: str) -> T.List[str]:
raise EnvironmentException('This compiler does not support Windows CRT selection')

Expand Down
26 changes: 1 addition & 25 deletions mesonbuild/compilers/d.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,31 +379,7 @@ def get_debug_args(self, is_debug: bool) -> T.List[str]:
def _get_crt_args(self, crt_val: str, buildtype: str) -> T.List[str]:
if not self.info.is_windows():
return []

if crt_val in self.mscrt_args:
return self.mscrt_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.mscrt_args[dbg]
elif buildtype == 'debugoptimized':
return self.mscrt_args[rel]
elif buildtype == 'release':
return self.mscrt_args[rel]
elif buildtype == 'minsize':
return self.mscrt_args[rel]
else:
assert buildtype == 'custom'
raise EnvironmentException('Requested C runtime based on buildtype, but buildtype is "custom".')
return self.mscrt_args[self.get_crt_val(crt_val, buildtype)]

def get_soname_args(self, env: 'Environment', prefix: str, shlib_name: str,
suffix: str, soversion: str,
Expand Down
24 changes: 2 additions & 22 deletions mesonbuild/compilers/mixins/visualstudio.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,28 +366,8 @@ def get_default_include_dirs(self) -> T.List[str]:
return os.environ['INCLUDE'].split(os.pathsep)

def get_crt_compile_args(self, crt_val: str, buildtype: str) -> T.List[str]:
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 mesonlib.EnvironmentException('Requested C runtime based on buildtype, but buildtype is "custom".')
crt_val = self.get_crt_val(crt_val, buildtype)
return self.crt_args[crt_val]

def has_func_attribute(self, name: str, env: 'Environment') -> T.Tuple[bool, bool]:
# MSVC doesn't have __attribute__ like Clang and GCC do, so just return
Expand Down

0 comments on commit 361f748

Please sign in to comment.