Try harder to use the C compiler for compiling asm

Use an ordered dict for the compiler dictionary and sort it according
to a priority order: fortran, c, c++, etc.

This also ensures that builds are reproducible because it would be
a toss-up whether a C or a C++ compiler would be used based on the
order in which compilers.items() would return items.

Closes https://github.com/mesonbuild/meson/issues/1370
pull/1505/head
Nirbheek Chauhan 8 years ago
parent ff4b32741a
commit 14d0f38158
  1. 23
      mesonbuild/build.py
  2. 5
      mesonbuild/coredata.py
  3. 14
      mesonbuild/interpreter.py

@ -390,13 +390,6 @@ class BuildTarget(Target):
msg = 'Bad source of type {!r} in target {!r}.'.format(type(s).__name__, self.name) msg = 'Bad source of type {!r} in target {!r}.'.format(type(s).__name__, self.name)
raise InvalidArguments(msg) raise InvalidArguments(msg)
@staticmethod
def can_compile_sources(compiler, sources):
for s in sources:
if compiler.can_compile(s):
return True
return False
@staticmethod @staticmethod
def can_compile_remove_sources(compiler, sources): def can_compile_remove_sources(compiler, sources):
removed = False removed = False
@ -442,13 +435,15 @@ class BuildTarget(Target):
if not s.endswith(lang_suffixes['vala']): if not s.endswith(lang_suffixes['vala']):
sources.append(s) sources.append(s)
if sources: if sources:
# Add compilers based on the above sources # For each source, try to add one compiler that can compile it.
for lang, compiler in compilers.items(): # It's ok if no compilers can do so, because users are expected to
# We try to be conservative because sometimes people add files # be able to add arbitrary non-source files to the sources list.
# in the list of sources that we can't determine the type based for s in sources:
# just on the suffix. for lang, compiler in compilers.items():
if self.can_compile_sources(compiler, sources): if compiler.can_compile(s):
self.compilers[lang] = compiler if lang not in self.compilers:
self.compilers[lang] = compiler
break
else: else:
# No source files, target consists of only object files of unknown # No source files, target consists of only object files of unknown
# origin. Just add the first clike compiler that we have and hope # origin. Just add the first clike compiler that we have and hope

@ -14,6 +14,7 @@
import pickle, os, uuid import pickle, os, uuid
from pathlib import PurePath from pathlib import PurePath
from collections import OrderedDict
from .mesonlib import MesonException, commonpath from .mesonlib import MesonException, commonpath
from .mesonlib import default_libdir, default_libexecdir, default_prefix from .mesonlib import default_libdir, default_libexecdir, default_prefix
@ -128,8 +129,8 @@ class CoreData:
else: else:
self.cross_file = None self.cross_file = None
self.wrap_mode = options.wrap_mode self.wrap_mode = options.wrap_mode
self.compilers = {} self.compilers = OrderedDict()
self.cross_compilers = {} self.cross_compilers = OrderedDict()
self.deps = {} self.deps = {}
self.modules = {} self.modules = {}
# Only to print a warning if it changes between Meson invocations. # Only to print a warning if it changes between Meson invocations.

@ -1752,10 +1752,22 @@ class Interpreter(InterpreterBase):
self.coredata.compiler_options = new_options self.coredata.compiler_options = new_options
return comp, cross_comp return comp, cross_comp
@staticmethod
def sort_clike(lang):
'''
Sorting function to sort the list of languages according to
reversed(compilers.clike_langs) and append the unknown langs in the end.
The purpose is to prefer C over C++ for files that can be compiled by
both such as assembly, C, etc. Also applies to ObjC, ObjC++, etc.
'''
if lang not in compilers.clike_langs:
return 1
return -compilers.clike_langs.index(lang)
def add_languages(self, args, required): def add_languages(self, args, required):
success = True success = True
need_cross_compiler = self.environment.is_cross_build() and self.environment.cross_info.need_cross_compiler() need_cross_compiler = self.environment.is_cross_build() and self.environment.cross_info.need_cross_compiler()
for lang in args: for lang in sorted(args, key=self.sort_clike):
lang = lang.lower() lang = lang.lower()
if lang in self.coredata.compilers: if lang in self.coredata.compilers:
comp = self.coredata.compilers[lang] comp = self.coredata.compilers[lang]

Loading…
Cancel
Save