files: # Copyright 2012-2016 The Meson development team
  
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  
  #     http://www.apache.org/licenses/LICENSE-2.0
  
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  
  import os, pickle, re
  from .. import build
  from .. import dependencies
  from .. import mesonlib
  from .. import mlog
  from .. import compilers
  import json
  import subprocess
  from ..mesonlib import MesonException
  from ..mesonlib import get_compiler_for_source, classify_unity_sources
  from ..compilers import CompilerArgs
  from collections import OrderedDict
  import shlex
  
  class CleanTrees:
      '''
      Directories outputted by custom targets that have to be manually cleaned
      because on Linux `ninja clean` only deletes empty directories.
      '''
      def __init__(self, build_dir, trees):
          self.build_dir = build_dir
          self.trees = trees
  
  class InstallData:
      def __init__(self, source_dir, build_dir, prefix, strip_bin, mesonintrospect):
          self.source_dir = source_dir
          self.build_dir = build_dir
          self.prefix = prefix
          self.strip_bin = strip_bin
          self.targets = []
          self.headers = []
          self.man = []
          self.data = []
          self.po_package_name = ''
          self.po = []
          self.install_scripts = []
          self.install_subdirs = []
          self.mesonintrospect = mesonintrospect
  
  class ExecutableSerialisation:
      def __init__(self, name, fname, cmd_args, env, is_cross, exe_wrapper,
                   workdir, extra_paths, capture):
          self.name = name
          self.fname = fname
          self.cmd_args = cmd_args
          self.env = env
          self.is_cross = is_cross
          self.exe_runner = exe_wrapper
          self.workdir = workdir
          self.extra_paths = extra_paths
          self.capture = capture
  
  class TestSerialisation:
      def __init__(self, name, project, suite, fname, is_cross_built, exe_wrapper, is_parallel,
                   cmd_args, env, should_fail, timeout, workdir, extra_paths):
          self.name = name
          self.project_name = project
          self.suite = suite
          self.fname = fname
          self.is_cross_built = is_cross_built
          self.exe_runner = exe_wrapper
          self.is_parallel = is_parallel
          self.cmd_args = cmd_args
          self.env = env
          self.should_fail = should_fail
          self.timeout = timeout
          self.workdir = workdir
          self.extra_paths = extra_paths
  
  class OptionProxy:
      def __init__(self, name, value):
          self.name = name
          self.value = value
  
  class OptionOverrideProxy:
      '''Mimic an option list but transparently override
      selected option values.'''
      def __init__(self, overrides, *options):
          self.overrides = overrides
          self.options = options
  
      def __getitem__(self, option_name):
          for opts in self.options:
              if option_name in opts:
                  return self._get_override(option_name, opts[option_name])
          raise KeyError('Option not found', option_name)
  
      def _get_override(self, option_name, base_opt):
          if option_name in self.overrides:
              return OptionProxy(base_opt.name, base_opt.validate_value(self.overrides[option_name]))
          return base_opt
  
  # This class contains the basic functionality that is needed by all backends.
  # Feel free to move stuff in and out of it as you see fit.
  class Backend:
      def __init__(self, build):
          self.build = build
          self.environment = build.environment
          self.processed_targets = {}
          self.build_to_src = os.path.relpath(self.environment.get_source_dir(),
                                              self.environment.get_build_dir())
  
      def get_target_filename(self, t):
          if isinstance(t, build.CustomTarget):
              if len(t.get_outputs()) != 1:
                  mlog.warning('custom_target {!r} has more than one output! '
                               'Using the first one.'.format(t.name))
              filename = t.get_outputs()[0]
          else:
              assert(isinstance(t, build.BuildTarget))
              filename = t.get_filename()
          return os.path.join(self.get_target_dir(t), filename)
  
      def get_target_filename_abs(self, target):
          return os.path.join(self.environment.get_build_dir(), self.get_target_filename(target))
  
      def get_builtin_options_for_target(self, target):
          return OptionOverrideProxy(target.option_overrides,
                                     self.environment.coredata.builtins)
  
      def get_base_options_for_target(self, target):
          return OptionOverrideProxy(target.option_overrides,
                                     self.environment.coredata.builtins,
                                     self.environment.coredata.base_options)
  
      def get_compiler_options_for_target(self, target):
          return OptionOverrideProxy(target.option_overrides,
                                     # no code depends on builtins for now
                                     self.environment.coredata.compiler_options)
  
      def get_option_for_target(self, option_name, target):
          if option_name in target.option_overrides:
              override = target.option_overrides[option_name]
              return self.environment.coredata.validate_option_value(option_name, override)
          return self.environment.coredata.get_builtin_option(option_name)
  
      def get_target_filename_for_linking(self, target):
          # On some platforms (msvc for instance), the file that is used for
          # dynamic linking is not the same as the dynamic library itself. This
          # file is called an import library, and we want to link against that.
          # On all other platforms, we link to the library directly.
          if isinstance(target, build.SharedLibrary):
              link_lib = target.get_import_filename() or target.get_filename()
              return os.path.join(self.get_target_dir(target), link_lib)
          elif isinstance(target, build.StaticLibrary):
              return os.path.join(self.get_target_dir(target), target.get_filename())
          elif isinstance(target, build.Executable):
              if target.import_filename:
                  return os.path.join(self.get_target_dir(target), target.get_import_filename())
              else:
                  return None
          raise AssertionError('BUG: Tried to link to {!r} which is not linkable'.format(target))
  
      def get_target_dir(self, target):
          if self.environment.coredata.get_builtin_option('layout') == 'mirror':
              dirname = target.get_subdir()
          else:
              dirname = 'meson-out'
          return dirname
  
      def get_target_dir_relative_to(self, t, o):
          '''Get a target dir relative to another target's directory'''
          target_dir = os.path.join(self.environment.get_build_dir(), self.get_target_dir(t))
          othert_dir = os.path.join(self.environment.get_build_dir(), self.get_target_dir(o))
          return os.path.relpath(target_dir, othert_dir)
  
      def get_target_source_dir(self, target):
          # if target dir is empty, avoid extraneous trailing / from os.path.join()
          target_dir = self.get_target_dir(target)
          if target_dir:
              return os.path.join(self.build_to_src, target_dir)
          return self.build_to_src
  
      def get_target_private_dir(self, target):
          return os.path.join(self.get_target_dir(target), target.get_id())
  
      def get_target_private_dir_abs(self, target):
          return os.path.join(self.environment.get_build_dir(), self.get_target_private_dir(target))
  
      def get_target_generated_dir(self, target, gensrc, src):
          """
          Takes a BuildTarget, a generator source (CustomTarget or GeneratedList),
          and a generated source filename.
          Returns the full path of the generated source relative to the build root
          """
          # CustomTarget generators output to the build dir of the CustomTarget
          if isinstance(gensrc, (build.CustomTarget, build.CustomTargetIndex)):
              return os.path.join(self.get_target_dir(gensrc), src)
          # GeneratedList generators output to the private build directory of the
          # target that the GeneratedList is used in
          return os.path.join(self.get_target_private_dir(target), src)
  
      def get_unity_source_file(self, target, suffix):
          osrc = target.name + '-unity.' + suffix
          return mesonlib.File.from_built_file(self.get_target_private_dir(target), osrc)
  
      def generate_unity_files(self, target, unity_src):
          abs_files = []
          result = []
          compsrcs = classify_unity_sources(target.compilers.values(), unity_src)
  
          def init_language_file(suffix):
              unity_src = self.get_unity_source_file(target, suffix)
              outfileabs = unity_src.absolute_path(self.environment.get_source_dir(),
                                                   self.environment.get_build_dir())
              outfileabs_tmp = outfileabs + '.tmp'
              abs_files.append(outfileabs)
              outfileabs_tmp_dir = os.path.dirname(outfileabs_tmp)
              if not os.path.exists(outfileabs_tmp_dir):
                  os.makedirs(outfileabs_tmp_dir)
              result.append(unity_src)
              return open(outfileabs_tmp, 'w')
  
          # For each language, generate a unity source file and return the list
          for comp, srcs in compsrcs.items():
              with init_language_file(comp.get_default_suffix()) as ofile:
                  for src in srcs:
                      ofile.write('#include<%s>\n' % src)
          [mesonlib.replace_if_different(x, x + '.tmp') for x in abs_files]
          return result
  
      def relpath(self, todir, fromdir):
          return os.path.relpath(os.path.join('dummyprefixdir', todir),
                                 os.path.join('dummyprefixdir', fromdir))
  
      def flatten_object_list(self, target, proj_dir_to_build_root=''):
          obj_list = []
          for obj in target.get_objects():
              if isinstance(obj, str):
                  o = os.path.join(proj_dir_to_build_root,
                                   self.build_to_src, target.get_subdir(), obj)
                  obj_list.append(o)
              elif isinstance(obj, mesonlib.File):
                  obj_list.append(obj.rel_to_builddir(self.build_to_src))
              elif isinstance(obj, build.ExtractedObjects):
                  obj_list += self.determine_ext_objs(target, obj, proj_dir_to_build_root)
              else:
                  raise MesonException('Unknown data type in object list.')
          return obj_list
  
      def serialize_executable(self, exe, cmd_args, workdir, env={},
                               extra_paths=None, capture=None):
          import hashlib
          if extra_paths is None:
              # The callee didn't check if we needed extra paths, so check it here
              if mesonlib.is_windows() or mesonlib.is_cygwin():
                  extra_paths = self.determine_windows_extra_paths(exe, [])
              else:
                  extra_paths = []
          # Can't just use exe.name here; it will likely be run more than once
          if isinstance(exe, (dependencies.ExternalProgram,
                              build.BuildTarget, build.CustomTarget)):
              basename = exe.name
          else:
              basename = os.path.basename(exe)
          # Take a digest of the cmd args, env, workdir, and capture. This avoids
          # collisions and also makes the name deterministic over regenerations
          # which avoids a rebuild by Ninja because the cmdline stays the same.
          data = bytes(str(sorted(env.items())) + str(cmd_args) + str(workdir) + str(capture),
                       encoding='utf-8')
          digest = hashlib.sha1(data).hexdigest()
          scratch_file = 'meson_exe_{0}_{1}.dat'.format(basename, digest)
          exe_data = os.path.join(self.environment.get_scratch_dir(), scratch_file)
          with open(exe_data, 'wb') as f:
              if isinstance(exe, dependencies.ExternalProgram):
                  exe_cmd = exe.get_command()
                  exe_needs_wrapper = False
              elif isinstance(exe, (build.BuildTarget, build.CustomTarget)):
                  exe_cmd = [self.get_target_filename_abs(exe)]
                  exe_needs_wrapper = exe.is_cross
              else:
                  exe_cmd = [exe]
                  exe_needs_wrapper = False
              is_cross_built = exe_needs_wrapper and \
                  self.environment.is_cross_build() and \
                  self.environment.cross_info.need_cross_compiler() and \
                  self.environment.cross_info.need_exe_wrapper()
              if is_cross_built:
                  exe_wrapper = self.environment.cross_info.config['binaries'].get('exe_wrapper', None)
              else:
                  exe_wrapper = None
              es = ExecutableSerialisation(basename, exe_cmd, cmd_args, env,
                                           is_cross_built, exe_wrapper, workdir,
                                           extra_paths, capture)
              pickle.dump(es, f)
          return exe_data
  
      def serialize_tests(self):
          test_data = os.path.join(self.environment.get_scratch_dir(), 'meson_test_setup.dat')
          with open(test_data, 'wb') as datafile:
              self.write_test_file(datafile)
          benchmark_data = os.path.join(self.environment.get_scratch_dir(), 'meson_benchmark_setup.dat')
          with open(benchmark_data, 'wb') as datafile:
              self.write_benchmark_file(datafile)
          return test_data, benchmark_data
  
      def determine_linker(self, target):
          '''
          If we're building a static library, there is only one static linker.
          Otherwise, we query the target for the dynamic linker.
          '''
          if isinstance(target, build.StaticLibrary):
              if target.is_cross:
                  return self.build.static_cross_linker
              else:
                  return self.build.static_linker
          l = target.get_clike_dynamic_linker()
          if not l:
              m = "Couldn't determine linker for target {!r}"
              raise MesonException(m.format(target.name))
          return l
  
      def rpaths_for_bundled_shared_libraries(self, target):
          paths = []
          for dep in target.external_deps:
              if isinstance(dep, (dependencies.ExternalLibrary, dependencies.PkgConfigDependency)):
                  la = dep.link_args
                  if len(la) == 1 and os.path.isabs(la[0]):
                      # The only link argument is an absolute path to a library file.
                      libpath = la[0]
                      if libpath.startswith(('/usr/lib', '/lib')):
                          # No point in adding system paths.
                          continue
                      if os.path.splitext(libpath)[1] not in ['.dll', '.lib', '.so']:
                          continue
                      absdir = os.path.dirname(libpath)
                      if absdir.startswith(self.environment.get_source_dir()):
                          rel_to_src = absdir[len(self.environment.get_source_dir()) + 1:]
                          assert not os.path.isabs(rel_to_src), 'rel_to_src: {} is absolute'.format(rel_to_src)
                          paths.append(os.path.join(self.build_to_src, rel_to_src))
                      else:
                          paths.append(absdir)
          return paths
  
      def determine_rpath_dirs(self, target):
          link_deps = target.get_all_link_deps()
          result = []
          for ld in link_deps:
              if ld is target:
                  continue
              prospective = self.get_target_dir(ld)
              if prospective not in result:
                  result.append(prospective)
          for rp in self.rpaths_for_bundled_shared_libraries(target):
              if rp not in result:
                  result += [rp]
          return result
  
      def object_filename_from_source(self, target, source, is_unity):
          assert isinstance(source, mesonlib.File)
          build_dir = self.environment.get_build_dir()
          rel_src = source.rel_to_builddir(self.build_to_src)
  
          if (not self.environment.is_source(rel_src) or
             self.environment.is_header(rel_src)) and not is_unity:
              return None
  
          # foo.vala files compile down to foo.c and then foo.c.o, not foo.vala.o
          if rel_src.endswith(('.vala', '.gs')):
              # See description in generate_vala_compile for this logic.
              if source.is_built:
                  if os.path.isabs(rel_src):
                      rel_src = rel_src[len(build_dir) + 1:]
                  rel_src = os.path.relpath(rel_src, self.get_target_private_dir(target))
              else:
                  rel_src = os.path.basename(rel_src)
              if is_unity:
                  return 'meson-generated_' + rel_src[:-5] + '.c.' + self.environment.get_object_suffix()
              # A meson- prefixed directory is reserved; hopefully no-one creates a file name with such a weird prefix.
              source = 'meson-generated_' + rel_src[:-5] + '.c'
          elif source.is_built:
              if os.path.isabs(rel_src):
                  rel_src = rel_src[len(build_dir) + 1:]
              targetdir = self.get_target_private_dir(target)
              # A meson- prefixed directory is reserved; hopefully no-one creates a file name with such a weird prefix.
              source = 'meson-generated_' + os.path.relpath(rel_src, targetdir)
          else:
              if os.path.isabs(rel_src):
                  # Not from the source directory; hopefully this doesn't conflict with user's source files.
                  source = os.path.basename(rel_src)
              else:
                  source = os.path.relpath(os.path.join(build_dir, rel_src),
                                           os.path.join(self.environment.get_source_dir(), target.get_subdir()))
          return source.replace('/', '_').replace('\\', '_') + '.' + self.environment.get_object_suffix()
  
      def determine_ext_objs(self, target, extobj, proj_dir_to_build_root):
          result = []
          targetdir = self.get_target_private_dir(extobj.target)
          # With unity builds, there's just one object that contains all the
          # sources, and we only support extracting all the objects in this mode,
          # so just return that.
          if self.is_unity(target):
              comp = get_compiler_for_source(extobj.target.compilers.values(),
                                             extobj.srclist[0])
              # There is a potential conflict here, but it is unlikely that
              # anyone both enables unity builds and has a file called foo-unity.cpp.
              osrc = self.get_unity_source_file(extobj.target,
                                                comp.get_default_suffix())
              objname = self.object_filename_from_source(extobj.target, osrc, True)
              objname = objname.replace('/', '_').replace('\\', '_')
              objpath = os.path.join(proj_dir_to_build_root, targetdir, objname)
              return [objpath]
          for osrc in extobj.srclist:
              objname = self.object_filename_from_source(extobj.target, osrc, False)
              if objname:
                  objpath = os.path.join(proj_dir_to_build_root, targetdir, objname)
                  result.append(objpath)
          return result
  
      def get_pch_include_args(self, compiler, target):
          args = []
          pchpath = self.get_target_private_dir(target)
          includeargs = compiler.get_include_args(pchpath, False)
          p = target.get_pch(compiler.get_language())
          if p:
              args += compiler.get_pch_use_args(pchpath, p[0])
          return includeargs + args
  
      @staticmethod
      def escape_extra_args(compiler, args):
          # No extra escaping/quoting needed when not running on Windows
          if not mesonlib.is_windows():
              return args
          extra_args = []
          # Compiler-specific escaping is needed for -D args but not for any others
          if compiler.get_id() == 'msvc':
              # MSVC needs escaping when a -D argument ends in \ or \"
              for arg in args:
                  if arg.startswith('-D') or arg.startswith('/D'):
                      # Without extra escaping for these two, the next character
                      # gets eaten
                      if arg.endswith('\\'):
                          arg += '\\'
                      elif arg.endswith('\\"'):
                          arg = arg[:-2] + '\\\\"'
                  extra_args.append(arg)
          else:
              # MinGW GCC needs all backslashes in defines to be doubly-escaped
              # FIXME: Not sure about Cygwin or Clang
              for arg in args:
                  if arg.startswith('-D') or arg.startswith('/D'):
                      arg = arg.replace('\\', '\\\\')
                  extra_args.append(arg)
          return extra_args
  
      def generate_basic_compiler_args(self, target, compiler, no_warn_args=False):
          # Create an empty commands list, and start adding arguments from
          # various sources in the order in which they must override each other
          # starting from hard-coded defaults followed by build options and so on.
          commands = CompilerArgs(compiler)
  
          copt_proxy = self.get_compiler_options_for_target(target)
          # First, the trivial ones that are impossible to override.
          #
          # Add -nostdinc/-nostdinc++ if needed; can't be overridden
          commands += self.get_cross_stdlib_args(target, compiler)
          # Add things like /NOLOGO or -pipe; usually can't be overridden
          commands += compiler.get_always_args()
          # Only add warning-flags by default if the buildtype enables it, and if
          # we weren't explicitly asked to not emit warnings (for Vala, f.ex)
          if no_warn_args:
              commands += compiler.get_no_warn_args()
          elif self.get_option_for_target('buildtype', target) != 'plain':
              commands += compiler.get_warn_args(self.get_option_for_target('warning_level', target))
          # Add -Werror if werror=true is set in the build options set on the
          # command-line or default_options inside project(). This only sets the
          # action to be done for warnings if/when they are emitted, so it's ok
          # to set it after get_no_warn_args() or get_warn_args().
          if self.get_option_for_target('werror', target):
              commands += compiler.get_werror_args()
          # Add compile args for c_* or cpp_* build options set on the
          # command-line or default_options inside project().
          commands += compiler.get_option_compile_args(copt_proxy)
          # Add buildtype args: optimization level, debugging, etc.
          commands += compiler.get_buildtype_args(self.get_option_for_target('buildtype', target))
          # Add compile args added using add_project_arguments()
          commands += self.build.get_project_args(compiler, target.subproject)
          # Add compile args added using add_global_arguments()
          # These override per-project arguments
          commands += self.build.get_global_args(compiler)
          if not target.is_cross:
              # Compile args added from the env: CFLAGS/CXXFLAGS, etc. We want these
             
  
  [... the rest is omitted ...]
files: mesonbuild/backend/backends.py
files: # Copyright 2012-2016 The Meson development team
  
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  
  #     http://www.apache.org/licenses/LICENSE-2.0
  
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  
  import os, pickle, re
  from .. import build
  from .. import dependencies
  from .. import mesonlib
  from .. import mlog
  from .. import compilers
  import json
  import subprocess
  from ..mesonlib import MesonException
  from ..mesonlib import get_compiler_for_source, classify_unity_sources
  from ..compilers import CompilerArgs
  from collections import OrderedDict
  import shlex
  
  class CleanTrees:
      '''
      Directories outputted by custom targets that have to be manually cleaned
      because on Linux `ninja clean` only deletes empty directories.
      '''
      def __init__(self, build_dir, trees):
          self.build_dir = build_dir
          self.trees = trees
  
  class InstallData:
      def __init__(self, source_dir, build_dir, prefix, strip_bin, mesonintrospect):
          self.source_dir = source_dir
          self.build_dir = build_dir
          self.prefix = prefix
          self.strip_bin = strip_bin
          self.targets = []
          self.headers = []
          self.man = []
          self.data = []
          self.po_package_name = ''
          self.po = []
          self.install_scripts = []
          self.install_subdirs = []
          self.mesonintrospect = mesonintrospect
  
  class ExecutableSerialisation:
      def __init__(self, name, fname, cmd_args, env, is_cross, exe_wrapper,
                   workdir, extra_paths, capture):
          self.name = name
          self.fname = fname
          self.cmd_args = cmd_args
          self.env = env
          self.is_cross = is_cross
          self.exe_runner = exe_wrapper
          self.workdir = workdir
          self.extra_paths = extra_paths
          self.capture = capture
  
  class TestSerialisation:
      def __init__(self, name, project, suite, fname, is_cross_built, exe_wrapper, is_parallel,
                   cmd_args, env, should_fail, timeout, workdir, extra_paths):
          self.name = name
          self.project_name = project
          self.suite = suite
          self.fname = fname
          self.is_cross_built = is_cross_built
          self.exe_runner = exe_wrapper
          self.is_parallel = is_parallel
          self.cmd_args = cmd_args
          self.env = env
          self.should_fail = should_fail
          self.timeout = timeout
          self.workdir = workdir
          self.extra_paths = extra_paths
  
  class OptionProxy:
      def __init__(self, name, value):
          self.name = name
          self.value = value
  
  class OptionOverrideProxy:
      '''Mimic an option list but transparently override
      selected option values.'''
      def __init__(self, overrides, options):
          self.overrides = overrides
          self.options = options
  
      def __getitem__(self, option_name):
          base_opt = self.options[option_name]
          if option_name in self.overrides:
              return OptionProxy(base_opt.name, base_opt.validate_value(self.overrides[option_name]))
          return base_opt
  
  # This class contains the basic functionality that is needed by all backends.
  # Feel free to move stuff in and out of it as you see fit.
  class Backend:
      def __init__(self, build):
          self.build = build
          self.environment = build.environment
          self.processed_targets = {}
          self.build_to_src = os.path.relpath(self.environment.get_source_dir(),
                                              self.environment.get_build_dir())
  
      def get_target_filename(self, t):
          if isinstance(t, build.CustomTarget):
              if len(t.get_outputs()) != 1:
                  mlog.warning('custom_target {!r} has more than one output! '
                               'Using the first one.'.format(t.name))
              filename = t.get_outputs()[0]
          else:
              assert(isinstance(t, build.BuildTarget))
              filename = t.get_filename()
          return os.path.join(self.get_target_dir(t), filename)
  
      def get_target_filename_abs(self, target):
          return os.path.join(self.environment.get_build_dir(), self.get_target_filename(target))
  
      def get_option_for_target(self, option_name, target):
          if option_name in target.option_overrides:
              override = target.option_overrides[option_name]
              return self.environment.coredata.validate_option_value(option_name, override)
          return self.environment.coredata.get_builtin_option(option_name)
  
      def get_target_filename_for_linking(self, target):
          # On some platforms (msvc for instance), the file that is used for
          # dynamic linking is not the same as the dynamic library itself. This
          # file is called an import library, and we want to link against that.
          # On all other platforms, we link to the library directly.
          if isinstance(target, build.SharedLibrary):
              link_lib = target.get_import_filename() or target.get_filename()
              return os.path.join(self.get_target_dir(target), link_lib)
          elif isinstance(target, build.StaticLibrary):
              return os.path.join(self.get_target_dir(target), target.get_filename())
          elif isinstance(target, build.Executable):
              if target.import_filename:
                  return os.path.join(self.get_target_dir(target), target.get_import_filename())
              else:
                  return None
          raise AssertionError('BUG: Tried to link to {!r} which is not linkable'.format(target))
  
      def get_target_dir(self, target):
          if self.environment.coredata.get_builtin_option('layout') == 'mirror':
              dirname = target.get_subdir()
          else:
              dirname = 'meson-out'
          return dirname
  
      def get_target_dir_relative_to(self, t, o):
          '''Get a target dir relative to another target's directory'''
          target_dir = os.path.join(self.environment.get_build_dir(), self.get_target_dir(t))
          othert_dir = os.path.join(self.environment.get_build_dir(), self.get_target_dir(o))
          return os.path.relpath(target_dir, othert_dir)
  
      def get_target_source_dir(self, target):
          # if target dir is empty, avoid extraneous trailing / from os.path.join()
          target_dir = self.get_target_dir(target)
          if target_dir:
              return os.path.join(self.build_to_src, target_dir)
          return self.build_to_src
  
      def get_target_private_dir(self, target):
          return os.path.join(self.get_target_dir(target), target.get_id())
  
      def get_target_private_dir_abs(self, target):
          return os.path.join(self.environment.get_build_dir(), self.get_target_private_dir(target))
  
      def get_target_generated_dir(self, target, gensrc, src):
          """
          Takes a BuildTarget, a generator source (CustomTarget or GeneratedList),
          and a generated source filename.
          Returns the full path of the generated source relative to the build root
          """
          # CustomTarget generators output to the build dir of the CustomTarget
          if isinstance(gensrc, (build.CustomTarget, build.CustomTargetIndex)):
              return os.path.join(self.get_target_dir(gensrc), src)
          # GeneratedList generators output to the private build directory of the
          # target that the GeneratedList is used in
          return os.path.join(self.get_target_private_dir(target), src)
  
      def get_unity_source_file(self, target, suffix):
          osrc = target.name + '-unity.' + suffix
          return mesonlib.File.from_built_file(self.get_target_private_dir(target), osrc)
  
      def generate_unity_files(self, target, unity_src):
          abs_files = []
          result = []
          compsrcs = classify_unity_sources(target.compilers.values(), unity_src)
  
          def init_language_file(suffix):
              unity_src = self.get_unity_source_file(target, suffix)
              outfileabs = unity_src.absolute_path(self.environment.get_source_dir(),
                                                   self.environment.get_build_dir())
              outfileabs_tmp = outfileabs + '.tmp'
              abs_files.append(outfileabs)
              outfileabs_tmp_dir = os.path.dirname(outfileabs_tmp)
              if not os.path.exists(outfileabs_tmp_dir):
                  os.makedirs(outfileabs_tmp_dir)
              result.append(unity_src)
              return open(outfileabs_tmp, 'w')
  
          # For each language, generate a unity source file and return the list
          for comp, srcs in compsrcs.items():
              with init_language_file(comp.get_default_suffix()) as ofile:
                  for src in srcs:
                      ofile.write('#include<%s>\n' % src)
          [mesonlib.replace_if_different(x, x + '.tmp') for x in abs_files]
          return result
  
      def relpath(self, todir, fromdir):
          return os.path.relpath(os.path.join('dummyprefixdir', todir),
                                 os.path.join('dummyprefixdir', fromdir))
  
      def flatten_object_list(self, target, proj_dir_to_build_root=''):
          obj_list = []
          for obj in target.get_objects():
              if isinstance(obj, str):
                  o = os.path.join(proj_dir_to_build_root,
                                   self.build_to_src, target.get_subdir(), obj)
                  obj_list.append(o)
              elif isinstance(obj, mesonlib.File):
                  obj_list.append(obj.rel_to_builddir(self.build_to_src))
              elif isinstance(obj, build.ExtractedObjects):
                  obj_list += self.determine_ext_objs(target, obj, proj_dir_to_build_root)
              else:
                  raise MesonException('Unknown data type in object list.')
          return obj_list
  
      def serialize_executable(self, exe, cmd_args, workdir, env={},
                               extra_paths=None, capture=None):
          import hashlib
          if extra_paths is None:
              # The callee didn't check if we needed extra paths, so check it here
              if mesonlib.is_windows() or mesonlib.is_cygwin():
                  extra_paths = self.determine_windows_extra_paths(exe, [])
              else:
                  extra_paths = []
          # Can't just use exe.name here; it will likely be run more than once
          if isinstance(exe, (dependencies.ExternalProgram,
                              build.BuildTarget, build.CustomTarget)):
              basename = exe.name
          else:
              basename = os.path.basename(exe)
          # Take a digest of the cmd args, env, workdir, and capture. This avoids
          # collisions and also makes the name deterministic over regenerations
          # which avoids a rebuild by Ninja because the cmdline stays the same.
          data = bytes(str(sorted(env.items())) + str(cmd_args) + str(workdir) + str(capture),
                       encoding='utf-8')
          digest = hashlib.sha1(data).hexdigest()
          scratch_file = 'meson_exe_{0}_{1}.dat'.format(basename, digest)
          exe_data = os.path.join(self.environment.get_scratch_dir(), scratch_file)
          with open(exe_data, 'wb') as f:
              if isinstance(exe, dependencies.ExternalProgram):
                  exe_cmd = exe.get_command()
                  exe_needs_wrapper = False
              elif isinstance(exe, (build.BuildTarget, build.CustomTarget)):
                  exe_cmd = [self.get_target_filename_abs(exe)]
                  exe_needs_wrapper = exe.is_cross
              else:
                  exe_cmd = [exe]
                  exe_needs_wrapper = False
              is_cross_built = exe_needs_wrapper and \
                  self.environment.is_cross_build() and \
                  self.environment.cross_info.need_cross_compiler() and \
                  self.environment.cross_info.need_exe_wrapper()
              if is_cross_built:
                  exe_wrapper = self.environment.cross_info.config['binaries'].get('exe_wrapper', None)
              else:
                  exe_wrapper = None
              es = ExecutableSerialisation(basename, exe_cmd, cmd_args, env,
                                           is_cross_built, exe_wrapper, workdir,
                                           extra_paths, capture)
              pickle.dump(es, f)
          return exe_data
  
      def serialize_tests(self):
          test_data = os.path.join(self.environment.get_scratch_dir(), 'meson_test_setup.dat')
          with open(test_data, 'wb') as datafile:
              self.write_test_file(datafile)
          benchmark_data = os.path.join(self.environment.get_scratch_dir(), 'meson_benchmark_setup.dat')
          with open(benchmark_data, 'wb') as datafile:
              self.write_benchmark_file(datafile)
          return test_data, benchmark_data
  
      def determine_linker(self, target):
          '''
          If we're building a static library, there is only one static linker.
          Otherwise, we query the target for the dynamic linker.
          '''
          if isinstance(target, build.StaticLibrary):
              if target.is_cross:
                  return self.build.static_cross_linker
              else:
                  return self.build.static_linker
          l = target.get_clike_dynamic_linker()
          if not l:
              m = "Couldn't determine linker for target {!r}"
              raise MesonException(m.format(target.name))
          return l
  
      def rpaths_for_bundled_shared_libraries(self, target):
          paths = []
          for dep in target.external_deps:
              if isinstance(dep, (dependencies.ExternalLibrary, dependencies.PkgConfigDependency)):
                  la = dep.link_args
                  if len(la) == 1 and os.path.isabs(la[0]):
                      # The only link argument is an absolute path to a library file.
                      libpath = la[0]
                      if libpath.startswith(('/usr/lib', '/lib')):
                          # No point in adding system paths.
                          continue
                      if os.path.splitext(libpath)[1] not in ['.dll', '.lib', '.so']:
                          continue
                      absdir = os.path.dirname(libpath)
                      if absdir.startswith(self.environment.get_source_dir()):
                          rel_to_src = absdir[len(self.environment.get_source_dir()) + 1:]
                          assert not os.path.isabs(rel_to_src), 'rel_to_src: {} is absolute'.format(rel_to_src)
                          paths.append(os.path.join(self.build_to_src, rel_to_src))
                      else:
                          paths.append(absdir)
          return paths
  
      def determine_rpath_dirs(self, target):
          link_deps = target.get_all_link_deps()
          result = []
          for ld in link_deps:
              if ld is target:
                  continue
              prospective = self.get_target_dir(ld)
              if prospective not in result:
                  result.append(prospective)
          for rp in self.rpaths_for_bundled_shared_libraries(target):
              if rp not in result:
                  result += [rp]
          return result
  
      def object_filename_from_source(self, target, source, is_unity):
          assert isinstance(source, mesonlib.File)
          build_dir = self.environment.get_build_dir()
          rel_src = source.rel_to_builddir(self.build_to_src)
  
          if (not self.environment.is_source(rel_src) or
             self.environment.is_header(rel_src)) and not is_unity:
              return None
  
          # foo.vala files compile down to foo.c and then foo.c.o, not foo.vala.o
          if rel_src.endswith(('.vala', '.gs')):
              # See description in generate_vala_compile for this logic.
              if source.is_built:
                  if os.path.isabs(rel_src):
                      rel_src = rel_src[len(build_dir) + 1:]
                  rel_src = os.path.relpath(rel_src, self.get_target_private_dir(target))
              else:
                  rel_src = os.path.basename(rel_src)
              if is_unity:
                  return 'meson-generated_' + rel_src[:-5] + '.c.' + self.environment.get_object_suffix()
              # A meson- prefixed directory is reserved; hopefully no-one creates a file name with such a weird prefix.
              source = 'meson-generated_' + rel_src[:-5] + '.c'
          elif source.is_built:
              if os.path.isabs(rel_src):
                  rel_src = rel_src[len(build_dir) + 1:]
              targetdir = self.get_target_private_dir(target)
              # A meson- prefixed directory is reserved; hopefully no-one creates a file name with such a weird prefix.
              source = 'meson-generated_' + os.path.relpath(rel_src, targetdir)
          else:
              if os.path.isabs(rel_src):
                  # Not from the source directory; hopefully this doesn't conflict with user's source files.
                  source = os.path.basename(rel_src)
              else:
                  source = os.path.relpath(os.path.join(build_dir, rel_src),
                                           os.path.join(self.environment.get_source_dir(), target.get_subdir()))
          return source.replace('/', '_').replace('\\', '_') + '.' + self.environment.get_object_suffix()
  
      def determine_ext_objs(self, target, extobj, proj_dir_to_build_root):
          result = []
          targetdir = self.get_target_private_dir(extobj.target)
          # With unity builds, there's just one object that contains all the
          # sources, and we only support extracting all the objects in this mode,
          # so just return that.
          if self.is_unity(target):
              comp = get_compiler_for_source(extobj.target.compilers.values(),
                                             extobj.srclist[0])
              # There is a potential conflict here, but it is unlikely that
              # anyone both enables unity builds and has a file called foo-unity.cpp.
              osrc = self.get_unity_source_file(extobj.target,
                                                comp.get_default_suffix())
              objname = self.object_filename_from_source(extobj.target, osrc, True)
              objname = objname.replace('/', '_').replace('\\', '_')
              objpath = os.path.join(proj_dir_to_build_root, targetdir, objname)
              return [objpath]
          for osrc in extobj.srclist:
              objname = self.object_filename_from_source(extobj.target, osrc, False)
              if objname:
                  objpath = os.path.join(proj_dir_to_build_root, targetdir, objname)
                  result.append(objpath)
          return result
  
      def get_pch_include_args(self, compiler, target):
          args = []
          pchpath = self.get_target_private_dir(target)
          includeargs = compiler.get_include_args(pchpath, False)
          p = target.get_pch(compiler.get_language())
          if p:
              args += compiler.get_pch_use_args(pchpath, p[0])
          return includeargs + args
  
      @staticmethod
      def escape_extra_args(compiler, args):
          # No extra escaping/quoting needed when not running on Windows
          if not mesonlib.is_windows():
              return args
          extra_args = []
          # Compiler-specific escaping is needed for -D args but not for any others
          if compiler.get_id() == 'msvc':
              # MSVC needs escaping when a -D argument ends in \ or \"
              for arg in args:
                  if arg.startswith('-D') or arg.startswith('/D'):
                      # Without extra escaping for these two, the next character
                      # gets eaten
                      if arg.endswith('\\'):
                          arg += '\\'
                      elif arg.endswith('\\"'):
                          arg = arg[:-2] + '\\\\"'
                  extra_args.append(arg)
          else:
              # MinGW GCC needs all backslashes in defines to be doubly-escaped
              # FIXME: Not sure about Cygwin or Clang
              for arg in args:
                  if arg.startswith('-D') or arg.startswith('/D'):
                      arg = arg.replace('\\', '\\\\')
                  extra_args.append(arg)
          return extra_args
  
      def generate_basic_compiler_args(self, target, compiler, no_warn_args=False):
          # Create an empty commands list, and start adding arguments from
          # various sources in the order in which they must override each other
          # starting from hard-coded defaults followed by build options and so on.
          commands = CompilerArgs(compiler)
  
          copt_proxy = OptionOverrideProxy(target.option_overrides, self.environment.coredata.compiler_options)
          # First, the trivial ones that are impossible to override.
          #
          # Add -nostdinc/-nostdinc++ if needed; can't be overridden
          commands += self.get_cross_stdlib_args(target, compiler)
          # Add things like /NOLOGO or -pipe; usually can't be overridden
          commands += compiler.get_always_args()
          # Only add warning-flags by default if the buildtype enables it, and if
          # we weren't explicitly asked to not emit warnings (for Vala, f.ex)
          if no_warn_args:
              commands += compiler.get_no_warn_args()
          elif self.get_option_for_target('buildtype', target) != 'plain':
              commands += compiler.get_warn_args(self.get_option_for_target('warning_level', target))
          # Add -Werror if werror=true is set in the build options set on the
          # command-line or default_options inside project(). This only sets the
          # action to be done for warnings if/when they are emitted, so it's ok
          # to set it after get_no_warn_args() or get_warn_args().
          if self.get_option_for_target('werror', target):
              commands += compiler.get_werror_args()
          # Add compile args for c_* or cpp_* build options set on the
          # command-line or default_options inside project().
          commands += compiler.get_option_compile_args(copt_proxy)
          # Add buildtype args: optimization level, debugging, etc.
          commands += compiler.get_buildtype_args(self.get_option_for_target('buildtype', target))
          # Add compile args added using add_project_arguments()
          commands += self.build.get_project_args(compiler, target.subproject)
          # Add compile args added using add_global_arguments()
          # These override per-project arguments
          commands += self.build.get_global_args(compiler)
          if not target.is_cross:
              # Compile args added from the env: CFLAGS/CXXFLAGS, etc. We want these
              # to override all the defaults, but not the per-target compile args.
              commands += self.environment.coredata.external_args[compiler.get_language()]
          # Always set -fPIC for shared libraries
          if isinstance(target, build.SharedLibrary):
              commands += compiler.get_pic_args()
          # Set -fPIC for static libraries by default unless explicitly disabled
          if isinstance(target, build.StaticLibrary) and target.pic:
              commands += compiler.get_pic_args()
          # Add compile args needed to find external dependencies. Link args are
          # added while generating the link command.
          # NOTE: We must preserve the order in which external deps are
          # specified, so we reverse the list before iterating over it.
          for
  
  [... the rest is omitted ...]
files: mesonbuild/backend/backends.py
files: # Copyright 2012-2017 The Meson development team
  
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  
  #     http://www.apache.org/licenses/LICENSE-2.0
  
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  
  import os, pickle, re, shlex, subprocess
  from collections import OrderedDict
  import itertools
  from pathlib import PurePath
  
  from . import backends
  from .. import modules
  from .. import environment, mesonlib
  from .. import build
  from .. import mlog
  from .. import dependencies
  from .. import compilers
  from ..compilers import CompilerArgs
  from ..linkers import ArLinker
  from ..mesonlib import File, MesonException, OrderedSet
  from ..mesonlib import get_compiler_for_source, has_path_sep
  from .backends import CleanTrees, InstallData
  from ..build import InvalidArguments
  
  if mesonlib.is_windows():
      quote_func = lambda s: '"{}"'.format(s)
      execute_wrapper = 'cmd /c'
      rmfile_prefix = 'del /f /s /q {} &&'
  else:
      quote_func = shlex.quote
      execute_wrapper = ''
      rmfile_prefix = 'rm -f {} &&'
  
  def ninja_quote(text, is_build_line=False):
      if is_build_line:
          qcs = ('$', ' ', ':')
      else:
          qcs = ('$', ' ')
      for char in qcs:
          text = text.replace(char, '$' + char)
      if '\n' in text:
          errmsg = '''Ninja does not support newlines in rules. The content was:
  
  %s
  
  Please report this error with a test case to the Meson bug tracker.''' % text
          raise MesonException(errmsg)
      return text
  
  
  class NinjaBuildElement:
      def __init__(self, all_outputs, outfilenames, rule, infilenames):
          if isinstance(outfilenames, str):
              self.outfilenames = [outfilenames]
          else:
              self.outfilenames = outfilenames
          assert(isinstance(rule, str))
          self.rule = rule
          if isinstance(infilenames, str):
              self.infilenames = [infilenames]
          else:
              self.infilenames = infilenames
          self.deps = set()
          self.orderdeps = set()
          self.elems = []
          self.all_outputs = all_outputs
  
      def add_dep(self, dep):
          if isinstance(dep, list):
              self.deps.update(dep)
          else:
              self.deps.add(dep)
  
      def add_orderdep(self, dep):
          if isinstance(dep, list):
              self.orderdeps.update(dep)
          else:
              self.orderdeps.add(dep)
  
      def add_item(self, name, elems):
          if isinstance(elems, str):
              elems = [elems]
          self.elems.append((name, elems))
  
      def write(self, outfile):
          self.check_outputs()
          line = 'build %s: %s %s' % (' '.join([ninja_quote(i, True) for i in self.outfilenames]),
                                      self.rule,
                                      ' '.join([ninja_quote(i, True) for i in self.infilenames]))
          if len(self.deps) > 0:
              line += ' | ' + ' '.join([ninja_quote(x, True) for x in self.deps])
          if len(self.orderdeps) > 0:
              line += ' || ' + ' '.join([ninja_quote(x, True) for x in self.orderdeps])
          line += '\n'
          # This is the only way I could find to make this work on all
          # platforms including Windows command shell. Slash is a dir separator
          # on Windows, too, so all characters are unambiguous and, more importantly,
          # do not require quoting, unless explicitely specified, which is necessary for
          # the csc compiler.
          line = line.replace('\\', '/')
          outfile.write(line)
  
          # All the entries that should remain unquoted
          raw_names = {'DEPFILE', 'DESC', 'pool', 'description'}
  
          for e in self.elems:
              (name, elems) = e
              should_quote = name not in raw_names
              line = ' %s = ' % name
              newelems = []
              for i in elems:
                  if not should_quote or i == '&&': # Hackety hack hack
                      quoter = ninja_quote
                  else:
                      quoter = lambda x: ninja_quote(quote_func(x))
                  i = i.replace('\\', '\\\\')
                  if quote_func('') == '""':
                      i = i.replace('"', '\\"')
                  newelems.append(quoter(i))
              line += ' '.join(newelems)
              line += '\n'
              outfile.write(line)
          outfile.write('\n')
  
      def check_outputs(self):
          for n in self.outfilenames:
              if n in self.all_outputs:
                  raise MesonException('Multiple producers for Ninja target "%s". Please rename your targets.' % n)
              self.all_outputs[n] = True
  
  class NinjaBackend(backends.Backend):
  
      def __init__(self, build):
          super().__init__(build)
          self.name = 'ninja'
          self.ninja_filename = 'build.ninja'
          self.target_arg_cache = {}
          self.fortran_deps = {}
          self.all_outputs = {}
  
      def create_target_alias(self, to_target, outfile):
          # We need to use aliases for targets that might be used as directory
          # names to workaround a Ninja bug that breaks `ninja -t clean`.
          # This is used for 'reserved' targets such as 'test', 'install',
          # 'benchmark', etc, and also for RunTargets.
          # https://github.com/mesonbuild/meson/issues/1644
          if not to_target.startswith('meson-'):
              m = 'Invalid usage of create_target_alias with {!r}'
              raise AssertionError(m.format(to_target))
          from_target = to_target[len('meson-'):]
          elem = NinjaBuildElement(self.all_outputs, from_target, 'phony', to_target)
          elem.write(outfile)
  
      def detect_vs_dep_prefix(self, tempfilename):
          '''VS writes its dependency in a locale dependent format.
          Detect the search prefix to use.'''
          for compiler in self.build.compilers.values():
              # Have to detect the dependency format
              if compiler.id == 'msvc':
                  break
          else:
              # None of our compilers are MSVC, we're done.
              return open(tempfilename, 'a')
          filename = os.path.join(self.environment.get_scratch_dir(),
                                  'incdetect.c')
          with open(filename, 'w') as f:
              f.write('''#include<stdio.h>
  int dummy;
  ''')
  
          # The output of cl dependency information is language
          # and locale dependent. Any attempt at converting it to
          # Python strings leads to failure. We _must_ do this detection
          # in raw byte mode and write the result in raw bytes.
          pc = subprocess.Popen(['cl', '/showIncludes', '/c', 'incdetect.c'],
                                cwd=self.environment.get_scratch_dir(),
                                stdout=subprocess.PIPE, stderr=subprocess.PIPE)
          (stdo, _) = pc.communicate()
  
          # We want to match 'Note: including file: ' in the line
          # 'Note: including file: d:\MyDir\include\stdio.h', however
          # different locales have different messages with a different
          # number of colons. Match up to the the drive name 'd:\'.
          matchre = re.compile(rb"^(.*\s)[a-zA-Z]:\\.*stdio.h$")
          for line in stdo.split(b'\r\n'):
              match = matchre.match(line)
              if match:
                  with open(tempfilename, 'ab') as binfile:
                      binfile.write(b'msvc_deps_prefix = ' + match.group(1) + b'\n')
                  return open(tempfilename, 'a')
          raise MesonException('Could not determine vs dep dependency prefix string.')
  
      def generate(self, interp):
          self.interpreter = interp
          self.ninja_command = environment.detect_ninja(log=True)
          if self.ninja_command is None:
              raise MesonException('Could not detect Ninja v1.5 or newer')
          outfilename = os.path.join(self.environment.get_build_dir(), self.ninja_filename)
          tempfilename = outfilename + '~'
          with open(tempfilename, 'w') as outfile:
              outfile.write('# This is the build file for project "%s"\n' %
                            self.build.get_project())
              outfile.write('# It is autogenerated by the Meson build system.\n')
              outfile.write('# Do not edit by hand.\n\n')
              outfile.write('ninja_required_version = 1.5.1\n\n')
          with self.detect_vs_dep_prefix(tempfilename) as outfile:
              self.generate_rules(outfile)
              self.generate_phony(outfile)
              outfile.write('# Build rules for targets\n\n')
              for t in self.build.get_targets().values():
                  self.generate_target(t, outfile)
              outfile.write('# Test rules\n\n')
              self.generate_tests(outfile)
              outfile.write('# Install rules\n\n')
              self.generate_install(outfile)
              self.generate_dist(outfile)
              if 'b_coverage' in self.environment.coredata.base_options and \
                      self.environment.coredata.base_options['b_coverage'].value:
                  outfile.write('# Coverage rules\n\n')
                  self.generate_coverage_rules(outfile)
              outfile.write('# Suffix\n\n')
              self.generate_utils(outfile)
              self.generate_ending(outfile)
          # Only overwrite the old build file after the new one has been
          # fully created.
          os.replace(tempfilename, outfilename)
          self.generate_compdb()
  
      # http://clang.llvm.org/docs/JSONCompilationDatabase.html
      def generate_compdb(self):
          pch_compilers = ['%s_PCH' % i for i in self.build.compilers]
          native_compilers = ['%s_COMPILER' % i for i in self.build.compilers]
          cross_compilers = ['%s_CROSS_COMPILER' % i for i in self.build.cross_compilers]
          ninja_compdb = [self.ninja_command, '-t', 'compdb'] + pch_compilers + native_compilers + cross_compilers
          builddir = self.environment.get_build_dir()
          try:
              jsondb = subprocess.check_output(ninja_compdb, cwd=builddir)
              with open(os.path.join(builddir, 'compile_commands.json'), 'wb') as f:
                  f.write(jsondb)
          except Exception:
              mlog.warning('Could not create compilation database.')
  
      # Get all generated headers. Any source file might need them so
      # we need to add an order dependency to them.
      def get_generated_headers(self, target):
          header_deps = []
          # XXX: Why don't we add deps to CustomTarget headers here?
          for genlist in target.get_generated_sources():
              if isinstance(genlist, (build.CustomTarget, build.CustomTargetIndex)):
                  continue
              for src in genlist.get_outputs():
                  if self.environment.is_header(src):
                      header_deps.append(self.get_target_generated_dir(target, genlist, src))
          if 'vala' in target.compilers and not isinstance(target, build.Executable):
              vala_header = File.from_built_file(self.get_target_dir(target), target.vala_header)
              header_deps.append(vala_header)
          # Recurse and find generated headers
          for dep in itertools.chain(target.link_targets, target.link_whole_targets):
              if isinstance(dep, (build.StaticLibrary, build.SharedLibrary)):
                  header_deps += self.get_generated_headers(dep)
          return header_deps
  
      def get_target_generated_sources(self, target):
          """
          Returns a dictionary with the keys being the path to the file
          (relative to the build directory) of that type and the value
          being the GeneratorList or CustomTarget that generated it.
          """
          srcs = OrderedDict()
          for gensrc in target.get_generated_sources():
              for s in gensrc.get_outputs():
                  f = self.get_target_generated_dir(target, gensrc, s)
                  srcs[f] = s
          return srcs
  
      def get_target_sources(self, target):
          srcs = OrderedDict()
          for s in target.get_sources():
              # BuildTarget sources are always mesonlib.File files which are
              # either in the source root, or generated with configure_file and
              # in the build root
              if not isinstance(s, File):
                  raise InvalidArguments('All sources in target {!r} must be of type mesonlib.File'.format(s))
              f = s.rel_to_builddir(self.build_to_src)
              srcs[f] = s
          return srcs
  
      # Languages that can mix with C or C++ but don't support unity builds yet
      # because the syntax we use for unity builds is specific to C/++/ObjC/++.
      # Assembly files cannot be unitified and neither can LLVM IR files
      langs_cant_unity = ('d', 'fortran')
  
      def get_target_source_can_unity(self, target, source):
          if isinstance(source, File):
              source = source.fname
          if self.environment.is_llvm_ir(source) or \
             self.environment.is_assembly(source):
              return False
          suffix = os.path.splitext(source)[1][1:]
          for lang in self.langs_cant_unity:
              if lang not in target.compilers:
                  continue
              if suffix in target.compilers[lang].file_suffixes:
                  return False
          return True
  
      def generate_target(self, target, outfile):
          if isinstance(target, build.CustomTarget):
              self.generate_custom_target(target, outfile)
          if isinstance(target, build.RunTarget):
              self.generate_run_target(target, outfile)
          name = target.get_id()
          if name in self.processed_targets:
              return
          self.processed_targets[name] = True
          # Generate rules for all dependency targets
          self.process_target_dependencies(target, outfile)
          # If target uses a language that cannot link to C objects,
          # just generate for that language and return.
          if isinstance(target, build.Jar):
              self.generate_jar_target(target, outfile)
              return
          if 'rust' in target.compilers:
              self.generate_rust_target(target, outfile)
              return
          if 'cs' in target.compilers:
              self.generate_cs_target(target, outfile)
              return
          if 'swift' in target.compilers:
              self.generate_swift_target(target, outfile)
              return
  
          # Now we handle the following languages:
          # ObjC++, ObjC, C++, C, D, Fortran, Vala
  
          # target_sources:
          #   Pre-existing target C/C++ sources to be built; dict of full path to
          #   source relative to build root and the original File object.
          # generated_sources:
          #   GeneratedList and CustomTarget sources to be built; dict of the full
          #   path to source relative to build root and the generating target/list
          # vala_generated_sources:
          #   Array of sources generated by valac that have to be compiled
          if 'vala' in target.compilers:
              # Sources consumed by valac are filtered out. These only contain
              # C/C++ sources, objects, generated libs, and unknown sources now.
              target_sources, generated_sources, \
                  vala_generated_sources = self.generate_vala_compile(target, outfile)
          else:
              target_sources = self.get_target_sources(target)
              generated_sources = self.get_target_generated_sources(target)
              vala_generated_sources = []
          self.scan_fortran_module_outputs(target)
          # Generate rules for GeneratedLists
          self.generate_generator_list_rules(target, outfile)
  
          # Generate rules for building the remaining source files in this target
          outname = self.get_target_filename(target)
          obj_list = []
          use_pch = self.environment.coredata.base_options.get('b_pch', False)
          is_unity = self.is_unity(target)
          if use_pch and target.has_pch():
              pch_objects = self.generate_pch(target, outfile)
          else:
              pch_objects = []
          header_deps = []
          unity_src = []
          unity_deps = [] # Generated sources that must be built before compiling a Unity target.
          header_deps += self.get_generated_headers(target)
  
          if is_unity:
              # Warn about incompatible sources if a unity build is enabled
              langs = set(target.compilers.keys())
              langs_cant = langs.intersection(self.langs_cant_unity)
              if langs_cant:
                  langs_are = langs = ', '.join(langs_cant).upper()
                  langs_are += ' are' if len(langs_cant) > 1 else ' is'
                  msg = '{} not supported in Unity builds yet, so {} ' \
                        'sources in the {!r} target will be compiled normally' \
                        ''.format(langs_are, langs, target.name)
                  mlog.log(mlog.red('FIXME'), msg)
  
          # Get a list of all generated headers that will be needed while building
          # this target's sources (generated sources and pre-existing sources).
          # This will be set as dependencies of all the target's sources. At the
          # same time, also deal with generated sources that need to be compiled.
          generated_source_files = []
          for rel_src, gensrc in generated_sources.items():
              dirpart, fnamepart = os.path.split(rel_src)
              raw_src = File(True, dirpart, fnamepart)
              if self.environment.is_source(rel_src) and not self.environment.is_header(rel_src):
                  if is_unity and self.get_target_source_can_unity(target, rel_src):
                      unity_deps.append(raw_src)
                      abs_src = os.path.join(self.environment.get_build_dir(), rel_src)
                      unity_src.append(abs_src)
                  else:
                      generated_source_files.append(raw_src)
              elif self.environment.is_object(rel_src):
                  obj_list.append(rel_src)
              elif self.environment.is_library(rel_src):
                  pass
              else:
                  # Assume anything not specifically a source file is a header. This is because
                  # people generate files with weird suffixes (.inc, .fh) that they then include
                  # in their source files.
                  header_deps.append(raw_src)
          # These are the generated source files that need to be built for use by
          # this target. We create the Ninja build file elements for this here
          # because we need `header_deps` to be fully generated in the above loop.
          for src in generated_source_files:
              if self.environment.is_llvm_ir(src):
                  o = self.generate_llvm_ir_compile(target, outfile, src)
              else:
                  o = self.generate_single_compile(target, outfile, src, True,
                                                   header_deps=header_deps)
              obj_list.append(o)
  
          # Generate compilation targets for C sources generated from Vala
          # sources. This can be extended to other $LANG->C compilers later if
          # necessary. This needs to be separate for at least Vala
          vala_generated_source_files = []
          for src in vala_generated_sources:
              dirpart, fnamepart = os.path.split(src)
              raw_src = File(True, dirpart, fnamepart)
              if is_unity:
                  unity_src.append(os.path.join(self.environment.get_build_dir(), src))
                  header_deps.append(raw_src)
              else:
                  # Generated targets are ordered deps because the must exist
                  # before the sources compiling them are used. After the first
                  # compile we get precise dependency info from dep files.
                  # This should work in all cases. If it does not, then just
                  # move them from orderdeps to proper deps.
                  if self.environment.is_header(src):
                      header_deps.append(raw_src)
                  else:
                      # We gather all these and generate compile rules below
                      # after `header_deps` (above) is fully generated
                      vala_generated_source_files.append(raw_src)
          for src in vala_generated_source_files:
              # Passing 'vala' here signifies that we want the compile
              # arguments to be specialized for C code generated by
              # valac. For instance, no warnings should be emitted.
              obj_list.append(self.generate_single_compile(target, outfile, src, 'vala', [], header_deps))
  
          # Generate compile targets for all the pre-existing sources for this target
          for f, src in target_sources.items():
              if not self.environment.is_header(src):
                  if self.environment.is_llvm_ir(src):
                      obj_list.append(self.generate_llvm_ir_compile(target, outfile, src))
                  elif is_unity and self.get_target_source_can_unity(target, src):
                      abs_src = os.path.join(self.environment.get_build_dir(),
                                             src.rel_to_builddir(self.build_to_src))
                      unity_src.append(abs_src)
                  else:
                      obj_list.append(self.generate_single_compile(target, outfile, src, False, [], header_deps))
          obj_list += self.flatten_object_list(target)
          if is_unity:
              for src in self.generate_unity_files(target, unity_src):
                  obj_list.append(self.generate_single_compile(target, outfile, src, True, unity_deps + header_deps))
          linker = self.determine_linker(target)
          elem = self.generate_link(target, outfile, outname, obj_list, linker, pch_objects)
          self.generate_shlib_aliases(target, self.get_target_dir(target))
          elem.write(outfile)
  
      def process_target_dependencies(self, target, outfile):
          for t in target.get_dependencies():
              if t.get_id() not in self.processed_targets:
                  self.generate_target(t, outfile)
  
      def custom_target_generator_inputs(self, target, outfile):
          for s in target.sources:
              if hasattr(s, 'held_object'):
                  s = s.held_object
              if isinstance(s, build.GeneratedList):
                  self.generate_genlist_for_target(s, target, outfile)
  
      def unwrap_dep_list(self, target):
          deps = []
          for i in target.get_dependencies():
              # FIXME, should not grab element at zero but rather expand all.
              if isinstance(i, list):
                  i = i[0]
              # Add a dependency on all the outputs of this target
              for output in i.get_outputs():
                  deps.append(os.path.join(self.get_target_dir(i), output))
          return deps
  
      def generate_custom_target(self, target, outfile):
          self.custom_target_generator_inputs(target, outfile)
          (srcs, ofilenames, cmd) = self.eval_custom_target_command(target)
          deps
  
  [... the rest is omitted ...]
files: mesonbuild/backend/ninjabackend.py
files: # Copyright 2012-2017 The Meson development team
  
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  
  #     http://www.apache.org/licenses/LICENSE-2.0
  
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  
  import os, pickle, re, shlex, subprocess
  from collections import OrderedDict
  import itertools
  from pathlib import PurePath
  
  from . import backends
  from .. import modules
  from .. import environment, mesonlib
  from .. import build
  from .. import mlog
  from .. import dependencies
  from .. import compilers
  from ..compilers import CompilerArgs
  from ..linkers import ArLinker
  from ..mesonlib import File, MesonException, OrderedSet
  from ..mesonlib import get_compiler_for_source, has_path_sep
  from .backends import CleanTrees, InstallData
  from ..build import InvalidArguments
  
  if mesonlib.is_windows():
      quote_func = lambda s: '"{}"'.format(s)
      execute_wrapper = 'cmd /c'
      rmfile_prefix = 'del /f /s /q {} &&'
  else:
      quote_func = shlex.quote
      execute_wrapper = ''
      rmfile_prefix = 'rm -f {} &&'
  
  def ninja_quote(text, is_build_line=False):
      if is_build_line:
          qcs = ('$', ' ', ':')
      else:
          qcs = ('$', ' ')
      for char in qcs:
          text = text.replace(char, '$' + char)
      if '\n' in text:
          errmsg = '''Ninja does not support newlines in rules. The content was:
  
  %s
  
  Please report this error with a test case to the Meson bug tracker.''' % text
          raise MesonException(errmsg)
      return text
  
  
  class NinjaBuildElement:
      def __init__(self, all_outputs, outfilenames, rule, infilenames):
          if isinstance(outfilenames, str):
              self.outfilenames = [outfilenames]
          else:
              self.outfilenames = outfilenames
          assert(isinstance(rule, str))
          self.rule = rule
          if isinstance(infilenames, str):
              self.infilenames = [infilenames]
          else:
              self.infilenames = infilenames
          self.deps = set()
          self.orderdeps = set()
          self.elems = []
          self.all_outputs = all_outputs
  
      def add_dep(self, dep):
          if isinstance(dep, list):
              self.deps.update(dep)
          else:
              self.deps.add(dep)
  
      def add_orderdep(self, dep):
          if isinstance(dep, list):
              self.orderdeps.update(dep)
          else:
              self.orderdeps.add(dep)
  
      def add_item(self, name, elems):
          if isinstance(elems, str):
              elems = [elems]
          self.elems.append((name, elems))
  
      def write(self, outfile):
          self.check_outputs()
          line = 'build %s: %s %s' % (' '.join([ninja_quote(i, True) for i in self.outfilenames]),
                                      self.rule,
                                      ' '.join([ninja_quote(i, True) for i in self.infilenames]))
          if len(self.deps) > 0:
              line += ' | ' + ' '.join([ninja_quote(x, True) for x in self.deps])
          if len(self.orderdeps) > 0:
              line += ' || ' + ' '.join([ninja_quote(x, True) for x in self.orderdeps])
          line += '\n'
          # This is the only way I could find to make this work on all
          # platforms including Windows command shell. Slash is a dir separator
          # on Windows, too, so all characters are unambiguous and, more importantly,
          # do not require quoting, unless explicitely specified, which is necessary for
          # the csc compiler.
          line = line.replace('\\', '/')
          outfile.write(line)
  
          # All the entries that should remain unquoted
          raw_names = {'DEPFILE', 'DESC', 'pool', 'description'}
  
          for e in self.elems:
              (name, elems) = e
              should_quote = name not in raw_names
              line = ' %s = ' % name
              newelems = []
              for i in elems:
                  if not should_quote or i == '&&': # Hackety hack hack
                      quoter = ninja_quote
                  else:
                      quoter = lambda x: ninja_quote(quote_func(x))
                  i = i.replace('\\', '\\\\')
                  if quote_func('') == '""':
                      i = i.replace('"', '\\"')
                  newelems.append(quoter(i))
              line += ' '.join(newelems)
              line += '\n'
              outfile.write(line)
          outfile.write('\n')
  
      def check_outputs(self):
          for n in self.outfilenames:
              if n in self.all_outputs:
                  raise MesonException('Multiple producers for Ninja target "%s". Please rename your targets.' % n)
              self.all_outputs[n] = True
  
  class NinjaBackend(backends.Backend):
  
      def __init__(self, build):
          super().__init__(build)
          self.name = 'ninja'
          self.ninja_filename = 'build.ninja'
          self.target_arg_cache = {}
          self.fortran_deps = {}
          self.all_outputs = {}
  
      def create_target_alias(self, to_target, outfile):
          # We need to use aliases for targets that might be used as directory
          # names to workaround a Ninja bug that breaks `ninja -t clean`.
          # This is used for 'reserved' targets such as 'test', 'install',
          # 'benchmark', etc, and also for RunTargets.
          # https://github.com/mesonbuild/meson/issues/1644
          if not to_target.startswith('meson-'):
              m = 'Invalid usage of create_target_alias with {!r}'
              raise AssertionError(m.format(to_target))
          from_target = to_target[len('meson-'):]
          elem = NinjaBuildElement(self.all_outputs, from_target, 'phony', to_target)
          elem.write(outfile)
  
      def detect_vs_dep_prefix(self, tempfilename):
          '''VS writes its dependency in a locale dependent format.
          Detect the search prefix to use.'''
          for compiler in self.build.compilers.values():
              # Have to detect the dependency format
              if compiler.id == 'msvc':
                  break
          else:
              # None of our compilers are MSVC, we're done.
              return open(tempfilename, 'a')
          filename = os.path.join(self.environment.get_scratch_dir(),
                                  'incdetect.c')
          with open(filename, 'w') as f:
              f.write('''#include<stdio.h>
  int dummy;
  ''')
  
          # The output of cl dependency information is language
          # and locale dependent. Any attempt at converting it to
          # Python strings leads to failure. We _must_ do this detection
          # in raw byte mode and write the result in raw bytes.
          pc = subprocess.Popen(['cl', '/showIncludes', '/c', 'incdetect.c'],
                                cwd=self.environment.get_scratch_dir(),
                                stdout=subprocess.PIPE, stderr=subprocess.PIPE)
          (stdo, _) = pc.communicate()
  
          # We want to match 'Note: including file: ' in the line
          # 'Note: including file: d:\MyDir\include\stdio.h', however
          # different locales have different messages with a different
          # number of colons. Match up to the the drive name 'd:\'.
          matchre = re.compile(rb"^(.*\s)[a-zA-Z]:\\.*stdio.h$")
          for line in stdo.split(b'\r\n'):
              match = matchre.match(line)
              if match:
                  with open(tempfilename, 'ab') as binfile:
                      binfile.write(b'msvc_deps_prefix = ' + match.group(1) + b'\n')
                  return open(tempfilename, 'a')
          raise MesonException('Could not determine vs dep dependency prefix string.')
  
      def generate(self, interp):
          self.interpreter = interp
          self.ninja_command = environment.detect_ninja(log=True)
          if self.ninja_command is None:
              raise MesonException('Could not detect Ninja v1.5 or newer')
          outfilename = os.path.join(self.environment.get_build_dir(), self.ninja_filename)
          tempfilename = outfilename + '~'
          with open(tempfilename, 'w') as outfile:
              outfile.write('# This is the build file for project "%s"\n' %
                            self.build.get_project())
              outfile.write('# It is autogenerated by the Meson build system.\n')
              outfile.write('# Do not edit by hand.\n\n')
              outfile.write('ninja_required_version = 1.5.1\n\n')
          with self.detect_vs_dep_prefix(tempfilename) as outfile:
              self.generate_rules(outfile)
              self.generate_phony(outfile)
              outfile.write('# Build rules for targets\n\n')
              for t in self.build.get_targets().values():
                  self.generate_target(t, outfile)
              outfile.write('# Test rules\n\n')
              self.generate_tests(outfile)
              outfile.write('# Install rules\n\n')
              self.generate_install(outfile)
              self.generate_dist(outfile)
              if 'b_coverage' in self.environment.coredata.base_options and \
                      self.environment.coredata.base_options['b_coverage'].value:
                  outfile.write('# Coverage rules\n\n')
                  self.generate_coverage_rules(outfile)
              outfile.write('# Suffix\n\n')
              self.generate_utils(outfile)
              self.generate_ending(outfile)
          # Only overwrite the old build file after the new one has been
          # fully created.
          os.replace(tempfilename, outfilename)
          self.generate_compdb()
  
      # http://clang.llvm.org/docs/JSONCompilationDatabase.html
      def generate_compdb(self):
          pch_compilers = ['%s_PCH' % i for i in self.build.compilers]
          native_compilers = ['%s_COMPILER' % i for i in self.build.compilers]
          cross_compilers = ['%s_CROSS_COMPILER' % i for i in self.build.cross_compilers]
          ninja_compdb = [self.ninja_command, '-t', 'compdb'] + pch_compilers + native_compilers + cross_compilers
          builddir = self.environment.get_build_dir()
          try:
              jsondb = subprocess.check_output(ninja_compdb, cwd=builddir)
              with open(os.path.join(builddir, 'compile_commands.json'), 'wb') as f:
                  f.write(jsondb)
          except Exception:
              mlog.warning('Could not create compilation database.')
  
      # Get all generated headers. Any source file might need them so
      # we need to add an order dependency to them.
      def get_generated_headers(self, target):
          header_deps = []
          # XXX: Why don't we add deps to CustomTarget headers here?
          for genlist in target.get_generated_sources():
              if isinstance(genlist, (build.CustomTarget, build.CustomTargetIndex)):
                  continue
              for src in genlist.get_outputs():
                  if self.environment.is_header(src):
                      header_deps.append(self.get_target_generated_dir(target, genlist, src))
          if 'vala' in target.compilers and not isinstance(target, build.Executable):
              vala_header = File.from_built_file(self.get_target_dir(target), target.vala_header)
              header_deps.append(vala_header)
          # Recurse and find generated headers
          for dep in itertools.chain(target.link_targets, target.link_whole_targets):
              if isinstance(dep, (build.StaticLibrary, build.SharedLibrary)):
                  header_deps += self.get_generated_headers(dep)
          return header_deps
  
      def get_target_generated_sources(self, target):
          """
          Returns a dictionary with the keys being the path to the file
          (relative to the build directory) of that type and the value
          being the GeneratorList or CustomTarget that generated it.
          """
          srcs = OrderedDict()
          for gensrc in target.get_generated_sources():
              for s in gensrc.get_outputs():
                  f = self.get_target_generated_dir(target, gensrc, s)
                  srcs[f] = s
          return srcs
  
      def get_target_sources(self, target):
          srcs = OrderedDict()
          for s in target.get_sources():
              # BuildTarget sources are always mesonlib.File files which are
              # either in the source root, or generated with configure_file and
              # in the build root
              if not isinstance(s, File):
                  raise InvalidArguments('All sources in target {!r} must be of type mesonlib.File'.format(s))
              f = s.rel_to_builddir(self.build_to_src)
              srcs[f] = s
          return srcs
  
      # Languages that can mix with C or C++ but don't support unity builds yet
      # because the syntax we use for unity builds is specific to C/++/ObjC/++.
      # Assembly files cannot be unitified and neither can LLVM IR files
      langs_cant_unity = ('d', 'fortran')
  
      def get_target_source_can_unity(self, target, source):
          if isinstance(source, File):
              source = source.fname
          if self.environment.is_llvm_ir(source) or \
             self.environment.is_assembly(source):
              return False
          suffix = os.path.splitext(source)[1][1:]
          for lang in self.langs_cant_unity:
              if lang not in target.compilers:
                  continue
              if suffix in target.compilers[lang].file_suffixes:
                  return False
          return True
  
      def generate_target(self, target, outfile):
          if isinstance(target, build.CustomTarget):
              self.generate_custom_target(target, outfile)
          if isinstance(target, build.RunTarget):
              self.generate_run_target(target, outfile)
          name = target.get_id()
          if name in self.processed_targets:
              return
          self.processed_targets[name] = True
          # Generate rules for all dependency targets
          self.process_target_dependencies(target, outfile)
          # If target uses a language that cannot link to C objects,
          # just generate for that language and return.
          if isinstance(target, build.Jar):
              self.generate_jar_target(target, outfile)
              return
          if 'rust' in target.compilers:
              self.generate_rust_target(target, outfile)
              return
          if 'cs' in target.compilers:
              self.generate_cs_target(target, outfile)
              return
          if 'swift' in target.compilers:
              self.generate_swift_target(target, outfile)
              return
  
          # Now we handle the following languages:
          # ObjC++, ObjC, C++, C, D, Fortran, Vala
  
          # target_sources:
          #   Pre-existing target C/C++ sources to be built; dict of full path to
          #   source relative to build root and the original File object.
          # generated_sources:
          #   GeneratedList and CustomTarget sources to be built; dict of the full
          #   path to source relative to build root and the generating target/list
          # vala_generated_sources:
          #   Array of sources generated by valac that have to be compiled
          if 'vala' in target.compilers:
              # Sources consumed by valac are filtered out. These only contain
              # C/C++ sources, objects, generated libs, and unknown sources now.
              target_sources, generated_sources, \
                  vala_generated_sources = self.generate_vala_compile(target, outfile)
          else:
              target_sources = self.get_target_sources(target)
              generated_sources = self.get_target_generated_sources(target)
              vala_generated_sources = []
          self.scan_fortran_module_outputs(target)
          # Generate rules for GeneratedLists
          self.generate_generator_list_rules(target, outfile)
  
          # Generate rules for building the remaining source files in this target
          outname = self.get_target_filename(target)
          obj_list = []
          use_pch = self.environment.coredata.base_options.get('b_pch', False)
          is_unity = self.is_unity(target)
          if use_pch and target.has_pch():
              pch_objects = self.generate_pch(target, outfile)
          else:
              pch_objects = []
          header_deps = []
          unity_src = []
          unity_deps = [] # Generated sources that must be built before compiling a Unity target.
          header_deps += self.get_generated_headers(target)
  
          if is_unity:
              # Warn about incompatible sources if a unity build is enabled
              langs = set(target.compilers.keys())
              langs_cant = langs.intersection(self.langs_cant_unity)
              if langs_cant:
                  langs_are = langs = ', '.join(langs_cant).upper()
                  langs_are += ' are' if len(langs_cant) > 1 else ' is'
                  msg = '{} not supported in Unity builds yet, so {} ' \
                        'sources in the {!r} target will be compiled normally' \
                        ''.format(langs_are, langs, target.name)
                  mlog.log(mlog.red('FIXME'), msg)
  
          # Get a list of all generated headers that will be needed while building
          # this target's sources (generated sources and pre-existing sources).
          # This will be set as dependencies of all the target's sources. At the
          # same time, also deal with generated sources that need to be compiled.
          generated_source_files = []
          for rel_src, gensrc in generated_sources.items():
              dirpart, fnamepart = os.path.split(rel_src)
              raw_src = File(True, dirpart, fnamepart)
              if self.environment.is_source(rel_src) and not self.environment.is_header(rel_src):
                  if is_unity and self.get_target_source_can_unity(target, rel_src):
                      unity_deps.append(raw_src)
                      abs_src = os.path.join(self.environment.get_build_dir(), rel_src)
                      unity_src.append(abs_src)
                  else:
                      generated_source_files.append(raw_src)
              elif self.environment.is_object(rel_src):
                  obj_list.append(rel_src)
              elif self.environment.is_library(rel_src):
                  pass
              else:
                  # Assume anything not specifically a source file is a header. This is because
                  # people generate files with weird suffixes (.inc, .fh) that they then include
                  # in their source files.
                  header_deps.append(raw_src)
          # These are the generated source files that need to be built for use by
          # this target. We create the Ninja build file elements for this here
          # because we need `header_deps` to be fully generated in the above loop.
          for src in generated_source_files:
              if self.environment.is_llvm_ir(src):
                  o = self.generate_llvm_ir_compile(target, outfile, src)
              else:
                  o = self.generate_single_compile(target, outfile, src, True,
                                                   header_deps=header_deps)
              obj_list.append(o)
  
          # Generate compilation targets for C sources generated from Vala
          # sources. This can be extended to other $LANG->C compilers later if
          # necessary. This needs to be separate for at least Vala
          vala_generated_source_files = []
          for src in vala_generated_sources:
              dirpart, fnamepart = os.path.split(src)
              raw_src = File(True, dirpart, fnamepart)
              if is_unity:
                  unity_src.append(os.path.join(self.environment.get_build_dir(), src))
                  header_deps.append(raw_src)
              else:
                  # Generated targets are ordered deps because the must exist
                  # before the sources compiling them are used. After the first
                  # compile we get precise dependency info from dep files.
                  # This should work in all cases. If it does not, then just
                  # move them from orderdeps to proper deps.
                  if self.environment.is_header(src):
                      header_deps.append(raw_src)
                  else:
                      # We gather all these and generate compile rules below
                      # after `header_deps` (above) is fully generated
                      vala_generated_source_files.append(raw_src)
          for src in vala_generated_source_files:
              # Passing 'vala' here signifies that we want the compile
              # arguments to be specialized for C code generated by
              # valac. For instance, no warnings should be emitted.
              obj_list.append(self.generate_single_compile(target, outfile, src, 'vala', [], header_deps))
  
          # Generate compile targets for all the pre-existing sources for this target
          for f, src in target_sources.items():
              if not self.environment.is_header(src):
                  if self.environment.is_llvm_ir(src):
                      obj_list.append(self.generate_llvm_ir_compile(target, outfile, src))
                  elif is_unity and self.get_target_source_can_unity(target, src):
                      abs_src = os.path.join(self.environment.get_build_dir(),
                                             src.rel_to_builddir(self.build_to_src))
                      unity_src.append(abs_src)
                  else:
                      obj_list.append(self.generate_single_compile(target, outfile, src, False, [], header_deps))
          obj_list += self.flatten_object_list(target)
          if is_unity:
              for src in self.generate_unity_files(target, unity_src):
                  obj_list.append(self.generate_single_compile(target, outfile, src, True, unity_deps + header_deps))
          linker = self.determine_linker(target)
          elem = self.generate_link(target, outfile, outname, obj_list, linker, pch_objects)
          self.generate_shlib_aliases(target, self.get_target_dir(target))
          elem.write(outfile)
  
      def process_target_dependencies(self, target, outfile):
          for t in target.get_dependencies():
              if t.get_id() not in self.processed_targets:
                  self.generate_target(t, outfile)
  
      def custom_target_generator_inputs(self, target, outfile):
          for s in target.sources:
              if hasattr(s, 'held_object'):
                  s = s.held_object
              if isinstance(s, build.GeneratedList):
                  self.generate_genlist_for_target(s, target, outfile)
  
      def unwrap_dep_list(self, target):
          deps = []
          for i in target.get_dependencies():
              # FIXME, should not grab element at zero but rather expand all.
              if isinstance(i, list):
                  i = i[0]
              # Add a dependency on all the outputs of this target
              for output in i.get_outputs():
                  deps.append(os.path.join(self.get_target_dir(i), output))
          return deps
  
      def generate_custom_target(self, target, outfile):
          self.custom_target_generator_inputs(target, outfile)
          (srcs, ofilenames, cmd) = self.eval_custom_target_command(target)
          deps
  
  [... the rest is omitted ...]
files: mesonbuild/backend/ninjabackend.py
files: # Copyright 2012-2017 The Meson development team
  
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  
  #     http://www.apache.org/licenses/LICENSE-2.0
  
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  
  import contextlib, os.path, re, tempfile
  import subprocess
  
  from ..linkers import StaticLinker
  from .. import coredata
  from .. import mlog
  from .. import mesonlib
  from ..mesonlib import EnvironmentException, MesonException, version_compare, Popen_safe
  
  """This file contains the data files of all compilers Meson knows
  about. To support a new compiler, add its information below.
  Also add corresponding autodetection code in environment.py."""
  
  header_suffixes = ('h', 'hh', 'hpp', 'hxx', 'H', 'ipp', 'moc', 'vapi', 'di')
  obj_suffixes = ('o', 'obj', 'res')
  lib_suffixes = ('a', 'lib', 'dll', 'dylib', 'so')
  # Mapping of language to suffixes of files that should always be in that language
  # This means we can't include .h headers here since they could be C, C++, ObjC, etc.
  lang_suffixes = {
      'c': ('c',),
      'cpp': ('cpp', 'cc', 'cxx', 'c++', 'hh', 'hpp', 'ipp', 'hxx'),
      # f90, f95, f03, f08 are for free-form fortran ('f90' recommended)
      # f, for, ftn, fpp are for fixed-form fortran ('f' or 'for' recommended)
      'fortran': ('f90', 'f95', 'f03', 'f08', 'f', 'for', 'ftn', 'fpp'),
      'd': ('d', 'di'),
      'objc': ('m',),
      'objcpp': ('mm',),
      'rust': ('rs',),
      'vala': ('vala', 'vapi', 'gs'),
      'cs': ('cs',),
      'swift': ('swift',),
      'java': ('java',),
  }
  cpp_suffixes = lang_suffixes['cpp'] + ('h',)
  c_suffixes = lang_suffixes['c'] + ('h',)
  # 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()
  clike_langs = ('d', 'objcpp', 'cpp', 'objc', 'c', 'fortran',)
  clike_suffixes = ()
  for _l in clike_langs:
      clike_suffixes += lang_suffixes[_l]
  clike_suffixes += ('h', 'll', 's')
  
  # XXX: Use this in is_library()?
  soregex = re.compile(r'.*\.so(\.[0-9]+)?(\.[0-9]+)?(\.[0-9]+)?$')
  
  # All these are only for C-like languages; see `clike_langs` above.
  
  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 clike_langs:
          return 1
      return -clike_langs.index(lang)
  
  def is_header(fname):
      if hasattr(fname, 'fname'):
          fname = fname.fname
      suffix = fname.split('.')[-1]
      return suffix in header_suffixes
  
  def is_source(fname):
      if hasattr(fname, 'fname'):
          fname = fname.fname
      suffix = fname.split('.')[-1].lower()
      return suffix in clike_suffixes
  
  def is_assembly(fname):
      if hasattr(fname, 'fname'):
          fname = fname.fname
      return fname.split('.')[-1].lower() == 's'
  
  def is_llvm_ir(fname):
      if hasattr(fname, 'fname'):
          fname = fname.fname
      return fname.split('.')[-1] == 'll'
  
  def is_object(fname):
      if hasattr(fname, 'fname'):
          fname = fname.fname
      suffix = fname.split('.')[-1]
      return suffix in obj_suffixes
  
  def is_library(fname):
      if hasattr(fname, 'fname'):
          fname = fname.fname
      suffix = fname.split('.')[-1]
      return suffix in lib_suffixes
  
  gnulike_buildtype_args = {'plain': [],
                            # -O0 is passed for improved debugging information with gcc
                            # See https://github.com/mesonbuild/meson/pull/509
                            'debug': ['-O0', '-g'],
                            'debugoptimized': ['-O2', '-g'],
                            'release': ['-O3'],
                            'minsize': ['-Os', '-g']}
  
  msvc_buildtype_args = {'plain': [],
                         'debug': ["/MDd", "/ZI", "/Ob0", "/Od", "/RTC1"],
                         'debugoptimized': ["/MD", "/Zi", "/O2", "/Ob1"],
                         'release': ["/MD", "/O2", "/Ob2"],
                         'minsize': ["/MD", "/Zi", "/Os", "/Ob1"],
                         }
  
  apple_buildtype_linker_args = {'plain': [],
                                 'debug': [],
                                 'debugoptimized': [],
                                 'release': [],
                                 'minsize': [],
                                 }
  
  gnulike_buildtype_linker_args = {'plain': [],
                                   'debug': [],
                                   'debugoptimized': [],
                                   'release': ['-Wl,-O1'],
                                   'minsize': [],
                                   }
  
  msvc_buildtype_linker_args = {'plain': [],
                                'debug': [],
                                'debugoptimized': [],
                                # The otherwise implicit REF and ICF linker
                                # optimisations are disabled by /DEBUG.
                                # REF implies ICF.
                                'release': ['/OPT:REF'],
                                'minsize': ['/INCREMENTAL:NO', '/OPT:REF'],
                                }
  
  java_buildtype_args = {'plain': [],
                         'debug': ['-g'],
                         'debugoptimized': ['-g'],
                         'release': [],
                         'minsize': [],
                         }
  
  rust_buildtype_args = {'plain': [],
                         'debug': ['-C', 'debuginfo=2'],
                         'debugoptimized': ['-C', 'debuginfo=2', '-C', 'opt-level=2'],
                         'release': ['-C', 'opt-level=3'],
                         'minsize': [], # In a future release: ['-C', 'opt-level=s'],
                         }
  
  d_gdc_buildtype_args = {'plain': [],
                          'debug': ['-g', '-O0'],
                          'debugoptimized': ['-g', '-O'],
                          'release': ['-O3', '-frelease'],
                          'minsize': [],
                          }
  
  d_ldc_buildtype_args = {'plain': [],
                          'debug': ['-g', '-O0'],
                          'debugoptimized': ['-g', '-O'],
                          'release': ['-O3', '-release'],
                          'minsize': [],
                          }
  
  d_dmd_buildtype_args = {'plain': [],
                          'debug': ['-g'],
                          'debugoptimized': ['-g', '-O'],
                          'release': ['-O', '-release'],
                          'minsize': [],
                          }
  
  mono_buildtype_args = {'plain': [],
                         'debug': ['-debug'],
                         'debugoptimized': ['-debug', '-optimize+'],
                         'release': ['-optimize+'],
                         'minsize': [],
                         }
  
  swift_buildtype_args = {'plain': [],
                          'debug': ['-g'],
                          'debugoptimized': ['-g', '-O'],
                          'release': ['-O'],
                          'minsize': [],
                          }
  
  gnu_winlibs = ['-lkernel32', '-luser32', '-lgdi32', '-lwinspool', '-lshell32',
                 '-lole32', '-loleaut32', '-luuid', '-lcomdlg32', '-ladvapi32']
  
  msvc_winlibs = ['kernel32.lib', 'user32.lib', 'gdi32.lib',
                  'winspool.lib', 'shell32.lib', 'ole32.lib', 'oleaut32.lib',
                  'uuid.lib', 'comdlg32.lib', 'advapi32.lib']
  
  gnu_color_args = {'auto': ['-fdiagnostics-color=auto'],
                    'always': ['-fdiagnostics-color=always'],
                    'never': ['-fdiagnostics-color=never'],
                    }
  
  clang_color_args = {'auto': ['-Xclang', '-fcolor-diagnostics'],
                      'always': ['-Xclang', '-fcolor-diagnostics'],
                      'never': ['-Xclang', '-fno-color-diagnostics'],
                      }
  
  base_options = {'b_pch': coredata.UserBooleanOption('b_pch', 'Use precompiled headers', True),
                  'b_lto': coredata.UserBooleanOption('b_lto', 'Use link time optimization', False),
                  'b_sanitize': coredata.UserComboOption('b_sanitize',
                                                         'Code sanitizer to use',
                                                         ['none', 'address', 'thread', 'undefined', 'memory', 'address,undefined'],
                                                         'none'),
                  'b_lundef': coredata.UserBooleanOption('b_lundef', 'Use -Wl,--no-undefined when linking', True),
                  'b_asneeded': coredata.UserBooleanOption('b_asneeded', 'Use -Wl,--as-needed when linking', True),
                  'b_pgo': coredata.UserComboOption('b_pgo', 'Use profile guided optimization',
                                                    ['off', 'generate', 'use'],
                                                    'off'),
                  'b_coverage': coredata.UserBooleanOption('b_coverage',
                                                           'Enable coverage tracking.',
                                                           False),
                  'b_colorout': coredata.UserComboOption('b_colorout', 'Use colored output',
                                                         ['auto', 'always', 'never'],
                                                         'always'),
                  'b_ndebug': coredata.UserComboOption('b_ndebug', 'Disable asserts',
                                                       ['true', 'false', 'if-release'], 'false'),
                  'b_staticpic': coredata.UserBooleanOption('b_staticpic',
                                                            'Build static libraries as position independent',
                                                            True),
                  }
  
  gnulike_instruction_set_args = {'mmx': ['-mmmx'],
                                  'sse': ['-msse'],
                                  'sse2': ['-msse2'],
                                  'sse3': ['-msse3'],
                                  'ssse3': ['-mssse3'],
                                  'sse41': ['-msse4.1'],
                                  'sse42': ['-msse4.2'],
                                  'avx': ['-mavx'],
                                  'avx2': ['-mavx2'],
                                  'neon': ['-mfpu=neon'],
                                  }
  
  vs32_instruction_set_args = {'mmx': ['/arch:SSE'], # There does not seem to be a flag just for MMX
                               'sse': ['/arch:SSE'],
                               'sse2': ['/arch:SSE2'],
                               'sse3': ['/arch:AVX'], # VS leaped from SSE2 directly to AVX.
                               'sse41': ['/arch:AVX'],
                               'sse42': ['/arch:AVX'],
                               'avx': ['/arch:AVX'],
                               'avx2': ['/arch:AVX2'],
                               'neon': None,
                               }
  
  # The 64 bit compiler defaults to /arch:avx.
  vs64_instruction_set_args = {'mmx': ['/arch:AVX'],
                               'sse': ['/arch:AVX'],
                               'sse2': ['/arch:AVX'],
                               'sse3': ['/arch:AVX'],
                               'ssse3': ['/arch:AVX'],
                               'sse41': ['/arch:AVX'],
                               'sse42': ['/arch:AVX'],
                               'avx': ['/arch:AVX'],
                               'avx2': ['/arch:AVX2'],
                               'neon': None,
                               }
  
  
  def sanitizer_compile_args(value):
      if value == 'none':
          return []
      args = ['-fsanitize=' + value]
      if 'address' in value: # For -fsanitize=address,undefined
          args.append('-fno-omit-frame-pointer')
      return args
  
  def sanitizer_link_args(value):
      if value == 'none':
          return []
      args = ['-fsanitize=' + value]
      return args
  
  def get_base_compile_args(options, compiler):
      args = []
      # FIXME, gcc/clang specific.
      try:
          if options['b_lto'].value:
              args.append('-flto')
      except KeyError:
          pass
      try:
          args += compiler.get_colorout_args(options['b_colorout'].value)
      except KeyError:
          pass
      try:
          args += sanitizer_compile_args(options['b_sanitize'].value)
      except KeyError:
          pass
      try:
          pgo_val = options['b_pgo'].value
          if pgo_val == 'generate':
              args.append('-fprofile-generate')
          elif pgo_val == 'use':
              args.append('-fprofile-use')
      except KeyError:
          pass
      try:
          if options['b_coverage'].value:
              args += compiler.get_coverage_args()
      except KeyError:
          pass
      try:
          if (options['b_ndebug'].value == 'true' or
                  (options['b_ndebug'].value == 'if-release' and
                   options['buildtype'].value == 'release')):
              args += ['-DNDEBUG']
      except KeyError:
          pass
      return args
  
  def get_base_link_args(options, linker, is_shared_module):
      args = []
      # FIXME, gcc/clang specific.
      try:
          if options['b_lto'].value:
              args.append('-flto')
      except KeyError:
          pass
      try:
          args += sanitizer_link_args(options['b_sanitize'].value)
      except KeyError:
          pass
      try:
          pgo_val = options['b_pgo'].value
          if pgo_val == 'generate':
              args.append('-fprofile-generate')
          elif pgo_val == 'use':
              args.append('-fprofile-use')
      except KeyError:
          pass
      try:
          if not is_shared_module and 'b_lundef' in linker.base_options and options['b_lundef'].value:
              args.append('-Wl,--no-undefined')
      except KeyError:
          pass
      try:
          if 'b_asneeded' in linker.base_options and options['b_asneeded'].value:
              args.append(linker.get_asneeded_args())
      except KeyError:
          pass
      try:
          if options['b_coverage'].value:
              args += linker.get_coverage_link_args()
      except KeyError:
          pass
      return args
  
  class CrossNoRunException(MesonException):
      pass
  
  class RunResult:
      def __init__(self, compiled, returncode=999, stdout='UNDEFINED', stderr='UNDEFINED'):
          self.compiled = compiled
          self.returncode = returncode
          self.stdout = stdout
          self.stderr = stderr
  
  class CompilerArgs(list):
      '''
      Class derived from list() that manages a list of compiler arguments. Should
      be used while constructing compiler arguments from various sources. Can be
      operated with ordinary lists, so this does not need to be used everywhere.
  
      All arguments must be inserted and stored in GCC-style (-lfoo, -Idir, etc)
      and can converted to the native type of each compiler by using the
      .to_native() method to which you must pass an instance of the compiler or
      the compiler class.
  
      New arguments added to this class (either with .append(), .extend(), or +=)
      are added in a way that ensures that they override previous arguments.
      For example:
  
      >>> a = ['-Lfoo', '-lbar']
      >>> a += ['-Lpho', '-lbaz']
      >>> print(a)
      ['-Lpho', '-Lfoo', '-lbar', '-lbaz']
  
      Arguments will also be de-duped if they can be de-duped safely.
  
      Note that because of all this, this class is not commutative and does not
      preserve the order of arguments if it is safe to not. For example:
      >>> ['-Ifoo', '-Ibar'] + ['-Ifez', '-Ibaz', '-Werror']
      ['-Ifez', '-Ibaz', '-Ifoo', '-Ibar', '-Werror']
      >>> ['-Ifez', '-Ibaz', '-Werror'] + ['-Ifoo', '-Ibar']
      ['-Ifoo', '-Ibar', '-Ifez', '-Ibaz', '-Werror']
  
      '''
      # NOTE: currently this class is only for C-like compilers, but it can be
      # extended to other languages easily. Just move the following to the
      # compiler class and initialize when self.compiler is set.
  
      # Arg prefixes that override by prepending instead of appending
      prepend_prefixes = ('-I', '-L')
      # Arg prefixes and args that must be de-duped by returning 2
      dedup2_prefixes = ('-I', '-L', '-D', '-U')
      dedup2_suffixes = ()
      dedup2_args = ()
      # Arg prefixes and args that must be de-duped by returning 1
      dedup1_prefixes = ('-l',)
      dedup1_suffixes = ('.lib', '.dll', '.so', '.dylib', '.a')
      # Match a .so of the form path/to/libfoo.so.0.1.0
      # Only UNIX shared libraries require this. Others have a fixed extension.
      dedup1_regex = re.compile(r'([\/\\]|\A)lib.*\.so(\.[0-9]+)?(\.[0-9]+)?(\.[0-9]+)?$')
      dedup1_args = ('-c', '-S', '-E', '-pipe', '-pthread')
      compiler = None
  
      def _check_args(self, args):
          cargs = []
          if len(args) > 2:
              raise TypeError("CompilerArgs() only accepts at most 2 arguments: "
                              "The compiler, and optionally an initial list")
          elif not args:
              return cargs
          elif len(args) == 1:
              if isinstance(args[0], (Compiler, StaticLinker)):
                  self.compiler = args[0]
              else:
                  raise TypeError("you must pass a Compiler instance as one of "
                                  "the arguments")
          elif len(args) == 2:
              if isinstance(args[0], (Compiler, StaticLinker)):
                  self.compiler = args[0]
                  cargs = args[1]
              elif isinstance(args[1], (Compiler, StaticLinker)):
                  cargs = args[0]
                  self.compiler = args[1]
              else:
                  raise TypeError("you must pass a Compiler instance as one of "
                                  "the two arguments")
          else:
              raise AssertionError('Not reached')
          return cargs
  
      def __init__(self, *args):
          super().__init__(self._check_args(args))
  
      @classmethod
      def _can_dedup(cls, arg):
          '''
          Returns whether the argument can be safely de-duped. This is dependent
          on three things:
  
          a) Whether an argument can be 'overridden' by a later argument.  For
             example, -DFOO defines FOO and -UFOO undefines FOO. In this case, we
             can safely remove the previous occurrence and add a new one. The same
             is true for include paths and library paths with -I and -L. For
             these we return `2`. See `dedup2_prefixes` and `dedup2_args`.
          b) Arguments that once specified cannot be undone, such as `-c` or
             `-pipe`. New instances of these can be completely skipped. For these
             we return `1`. See `dedup1_prefixes` and `dedup1_args`.
          c) Whether it matters where or how many times on the command-line
             a particular argument is present. This can matter for symbol
             resolution in static or shared libraries, so we cannot de-dup or
             reorder them. For these we return `0`. This is the default.
  
          In addition to these, we handle library arguments specially.
          With GNU ld, we surround library arguments with -Wl,--start/end-group
          to recursively search for symbols in the libraries. This is not needed
          with other linkers.
          '''
  
          # A standalone argument must never be deduplicated because it is
          # defined by what comes _after_ it. Thus dedupping this:
          # -D FOO -D BAR
          # would yield either
          # -D FOO BAR
          # or
          # FOO -D BAR
          # both of which are invalid.
          if arg in cls.dedup2_prefixes:
              return 0
          if arg in cls.dedup2_args or \
             arg.startswith(cls.dedup2_prefixes) or \
             arg.endswith(cls.dedup2_suffixes):
              return 2
          if arg in cls.dedup1_args or \
             arg.startswith(cls.dedup1_prefixes) or \
             arg.endswith(cls.dedup1_suffixes) or \
             re.search(cls.dedup1_regex, arg):
              return 1
          return 0
  
      @classmethod
      def _should_prepend(cls, arg):
          if arg.startswith(cls.prepend_prefixes):
              return True
          return False
  
      def to_native(self):
          # Check if we need to add --start/end-group for circular dependencies
          # between static libraries, and for recursively searching for symbols
          # needed by static libraries that are provided by object files or
          # shared libraries.
          if get_compiler_uses_gnuld(self.compiler):
              global soregex
              group_start = -1
              for each in self:
                  if not each.startswith('-l') and not each.endswith('.a') and \
                     not soregex.match(each):
                      continue
                  i = self.index(each)
                  if group_start < 0:
                      # First occurrence of a library
                      group_start = i
              if group_start >= 0:
                  # Last occurrence of a library
                  self.insert(i + 1, '-Wl,--end-group')
                  self.insert(group_start, '-Wl,--start-group')
          return self.compiler.unix_args_to_native(self)
  
      def append_direct(self, arg):
          '''
          Append the specified argument without any reordering or de-dup
          '''
          super().append(arg)
  
      def extend_direct(self, iterable):
          '''
          Extend using the elements in the specified iterable without any
          reordering or de-
  
  [... the rest is omitted ...]
files: mesonbuild/compilers/compilers.py
files: # Copyright 2012-2017 The Meson development team
  
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  
  #     http://www.apache.org/licenses/LICENSE-2.0
  
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  
  import contextlib, os.path, re, tempfile
  import subprocess
  
  from ..linkers import StaticLinker
  from .. import coredata
  from .. import mlog
  from .. import mesonlib
  from ..mesonlib import EnvironmentException, MesonException, version_compare, Popen_safe
  
  """This file contains the data files of all compilers Meson knows
  about. To support a new compiler, add its information below.
  Also add corresponding autodetection code in environment.py."""
  
  header_suffixes = ('h', 'hh', 'hpp', 'hxx', 'H', 'ipp', 'moc', 'vapi', 'di')
  obj_suffixes = ('o', 'obj', 'res')
  lib_suffixes = ('a', 'lib', 'dll', 'dylib', 'so')
  # Mapping of language to suffixes of files that should always be in that language
  # This means we can't include .h headers here since they could be C, C++, ObjC, etc.
  lang_suffixes = {
      'c': ('c',),
      'cpp': ('cpp', 'cc', 'cxx', 'c++', 'hh', 'hpp', 'ipp', 'hxx'),
      # f90, f95, f03, f08 are for free-form fortran ('f90' recommended)
      # f, for, ftn, fpp are for fixed-form fortran ('f' or 'for' recommended)
      'fortran': ('f90', 'f95', 'f03', 'f08', 'f', 'for', 'ftn', 'fpp'),
      'd': ('d', 'di'),
      'objc': ('m',),
      'objcpp': ('mm',),
      'rust': ('rs',),
      'vala': ('vala', 'vapi', 'gs'),
      'cs': ('cs',),
      'swift': ('swift',),
      'java': ('java',),
  }
  cpp_suffixes = lang_suffixes['cpp'] + ('h',)
  c_suffixes = lang_suffixes['c'] + ('h',)
  # 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()
  clike_langs = ('d', 'objcpp', 'cpp', 'objc', 'c', 'fortran',)
  clike_suffixes = ()
  for _l in clike_langs:
      clike_suffixes += lang_suffixes[_l]
  clike_suffixes += ('h', 'll', 's')
  
  # XXX: Use this in is_library()?
  soregex = re.compile(r'.*\.so(\.[0-9]+)?(\.[0-9]+)?(\.[0-9]+)?$')
  
  # All these are only for C-like languages; see `clike_langs` above.
  
  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 clike_langs:
          return 1
      return -clike_langs.index(lang)
  
  def is_header(fname):
      if hasattr(fname, 'fname'):
          fname = fname.fname
      suffix = fname.split('.')[-1]
      return suffix in header_suffixes
  
  def is_source(fname):
      if hasattr(fname, 'fname'):
          fname = fname.fname
      suffix = fname.split('.')[-1].lower()
      return suffix in clike_suffixes
  
  def is_assembly(fname):
      if hasattr(fname, 'fname'):
          fname = fname.fname
      return fname.split('.')[-1].lower() == 's'
  
  def is_llvm_ir(fname):
      if hasattr(fname, 'fname'):
          fname = fname.fname
      return fname.split('.')[-1] == 'll'
  
  def is_object(fname):
      if hasattr(fname, 'fname'):
          fname = fname.fname
      suffix = fname.split('.')[-1]
      return suffix in obj_suffixes
  
  def is_library(fname):
      if hasattr(fname, 'fname'):
          fname = fname.fname
      suffix = fname.split('.')[-1]
      return suffix in lib_suffixes
  
  gnulike_buildtype_args = {'plain': [],
                            # -O0 is passed for improved debugging information with gcc
                            # See https://github.com/mesonbuild/meson/pull/509
                            'debug': ['-O0', '-g'],
                            'debugoptimized': ['-O2', '-g'],
                            'release': ['-O3'],
                            'minsize': ['-Os', '-g']}
  
  msvc_buildtype_args = {'plain': [],
                         'debug': ["/MDd", "/ZI", "/Ob0", "/Od", "/RTC1"],
                         'debugoptimized': ["/MD", "/Zi", "/O2", "/Ob1"],
                         'release': ["/MD", "/O2", "/Ob2"],
                         'minsize': ["/MD", "/Zi", "/Os", "/Ob1"],
                         }
  
  apple_buildtype_linker_args = {'plain': [],
                                 'debug': [],
                                 'debugoptimized': [],
                                 'release': [],
                                 'minsize': [],
                                 }
  
  gnulike_buildtype_linker_args = {'plain': [],
                                   'debug': [],
                                   'debugoptimized': [],
                                   'release': ['-Wl,-O1'],
                                   'minsize': [],
                                   }
  
  msvc_buildtype_linker_args = {'plain': [],
                                'debug': [],
                                'debugoptimized': [],
                                # The otherwise implicit REF and ICF linker
                                # optimisations are disabled by /DEBUG.
                                # REF implies ICF.
                                'release': ['/OPT:REF'],
                                'minsize': ['/INCREMENTAL:NO', '/OPT:REF'],
                                }
  
  java_buildtype_args = {'plain': [],
                         'debug': ['-g'],
                         'debugoptimized': ['-g'],
                         'release': [],
                         'minsize': [],
                         }
  
  rust_buildtype_args = {'plain': [],
                         'debug': ['-C', 'debuginfo=2'],
                         'debugoptimized': ['-C', 'debuginfo=2', '-C', 'opt-level=2'],
                         'release': ['-C', 'opt-level=3'],
                         'minsize': [], # In a future release: ['-C', 'opt-level=s'],
                         }
  
  d_gdc_buildtype_args = {'plain': [],
                          'debug': ['-g', '-O0'],
                          'debugoptimized': ['-g', '-O'],
                          'release': ['-O3', '-frelease'],
                          'minsize': [],
                          }
  
  d_ldc_buildtype_args = {'plain': [],
                          'debug': ['-g', '-O0'],
                          'debugoptimized': ['-g', '-O'],
                          'release': ['-O3', '-release'],
                          'minsize': [],
                          }
  
  d_dmd_buildtype_args = {'plain': [],
                          'debug': ['-g'],
                          'debugoptimized': ['-g', '-O'],
                          'release': ['-O', '-release'],
                          'minsize': [],
                          }
  
  mono_buildtype_args = {'plain': [],
                         'debug': ['-debug'],
                         'debugoptimized': ['-debug', '-optimize+'],
                         'release': ['-optimize+'],
                         'minsize': [],
                         }
  
  swift_buildtype_args = {'plain': [],
                          'debug': ['-g'],
                          'debugoptimized': ['-g', '-O'],
                          'release': ['-O'],
                          'minsize': [],
                          }
  
  gnu_winlibs = ['-lkernel32', '-luser32', '-lgdi32', '-lwinspool', '-lshell32',
                 '-lole32', '-loleaut32', '-luuid', '-lcomdlg32', '-ladvapi32']
  
  msvc_winlibs = ['kernel32.lib', 'user32.lib', 'gdi32.lib',
                  'winspool.lib', 'shell32.lib', 'ole32.lib', 'oleaut32.lib',
                  'uuid.lib', 'comdlg32.lib', 'advapi32.lib']
  
  gnu_color_args = {'auto': ['-fdiagnostics-color=auto'],
                    'always': ['-fdiagnostics-color=always'],
                    'never': ['-fdiagnostics-color=never'],
                    }
  
  clang_color_args = {'auto': ['-Xclang', '-fcolor-diagnostics'],
                      'always': ['-Xclang', '-fcolor-diagnostics'],
                      'never': ['-Xclang', '-fno-color-diagnostics'],
                      }
  
  base_options = {'b_pch': coredata.UserBooleanOption('b_pch', 'Use precompiled headers', True),
                  'b_lto': coredata.UserBooleanOption('b_lto', 'Use link time optimization', False),
                  'b_sanitize': coredata.UserComboOption('b_sanitize',
                                                         'Code sanitizer to use',
                                                         ['none', 'address', 'thread', 'undefined', 'memory', 'address,undefined'],
                                                         'none'),
                  'b_lundef': coredata.UserBooleanOption('b_lundef', 'Use -Wl,--no-undefined when linking', True),
                  'b_asneeded': coredata.UserBooleanOption('b_asneeded', 'Use -Wl,--as-needed when linking', True),
                  'b_pgo': coredata.UserComboOption('b_pgo', 'Use profile guided optimization',
                                                    ['off', 'generate', 'use'],
                                                    'off'),
                  'b_coverage': coredata.UserBooleanOption('b_coverage',
                                                           'Enable coverage tracking.',
                                                           False),
                  'b_colorout': coredata.UserComboOption('b_colorout', 'Use colored output',
                                                         ['auto', 'always', 'never'],
                                                         'always'),
                  'b_ndebug': coredata.UserComboOption('b_ndebug', 'Disable asserts',
                                                       ['true', 'false', 'if-release'], 'false'),
                  'b_staticpic': coredata.UserBooleanOption('b_staticpic',
                                                            'Build static libraries as position independent',
                                                            True),
                  }
  
  gnulike_instruction_set_args = {'mmx': ['-mmmx'],
                                  'sse': ['-msse'],
                                  'sse2': ['-msse2'],
                                  'sse3': ['-msse3'],
                                  'ssse3': ['-mssse3'],
                                  'sse41': ['-msse4.1'],
                                  'sse42': ['-msse4.2'],
                                  'avx': ['-mavx'],
                                  'avx2': ['-mavx2'],
                                  'neon': ['-mfpu=neon'],
                                  }
  
  vs32_instruction_set_args = {'mmx': ['/arch:SSE'], # There does not seem to be a flag just for MMX
                               'sse': ['/arch:SSE'],
                               'sse2': ['/arch:SSE2'],
                               'sse3': ['/arch:AVX'], # VS leaped from SSE2 directly to AVX.
                               'sse41': ['/arch:AVX'],
                               'sse42': ['/arch:AVX'],
                               'avx': ['/arch:AVX'],
                               'avx2': ['/arch:AVX2'],
                               'neon': None,
                               }
  
  # The 64 bit compiler defaults to /arch:avx.
  vs64_instruction_set_args = {'mmx': ['/arch:AVX'],
                               'sse': ['/arch:AVX'],
                               'sse2': ['/arch:AVX'],
                               'sse3': ['/arch:AVX'],
                               'ssse3': ['/arch:AVX'],
                               'sse41': ['/arch:AVX'],
                               'sse42': ['/arch:AVX'],
                               'avx': ['/arch:AVX'],
                               'avx2': ['/arch:AVX2'],
                               'neon': None,
                               }
  
  
  def sanitizer_compile_args(value):
      if value == 'none':
          return []
      args = ['-fsanitize=' + value]
      if 'address' in value: # For -fsanitize=address,undefined
          args.append('-fno-omit-frame-pointer')
      return args
  
  def sanitizer_link_args(value):
      if value == 'none':
          return []
      args = ['-fsanitize=' + value]
      return args
  
  def get_base_compile_args(options, compiler):
      args = []
      # FIXME, gcc/clang specific.
      try:
          if options['b_lto'].value:
              args.append('-flto')
      except KeyError:
          pass
      try:
          args += compiler.get_colorout_args(options['b_colorout'].value)
      except KeyError:
          pass
      try:
          args += sanitizer_compile_args(options['b_sanitize'].value)
      except KeyError:
          pass
      try:
          pgo_val = options['b_pgo'].value
          if pgo_val == 'generate':
              args.append('-fprofile-generate')
          elif pgo_val == 'use':
              args.append('-fprofile-use')
      except KeyError:
          pass
      try:
          if options['b_coverage'].value:
              args += compiler.get_coverage_args()
      except KeyError:
          pass
      try:
          if options['b_ndebug'].value == 'true' or (options['b_ndebug'].value == 'if-release' and options['buildtype'] == 'release'):
              args += ['-DNDEBUG']
      except KeyError:
          pass
      return args
  
  def get_base_link_args(options, linker, is_shared_module):
      args = []
      # FIXME, gcc/clang specific.
      try:
          if options['b_lto'].value:
              args.append('-flto')
      except KeyError:
          pass
      try:
          args += sanitizer_link_args(options['b_sanitize'].value)
      except KeyError:
          pass
      try:
          pgo_val = options['b_pgo'].value
          if pgo_val == 'generate':
              args.append('-fprofile-generate')
          elif pgo_val == 'use':
              args.append('-fprofile-use')
      except KeyError:
          pass
      try:
          if not is_shared_module and 'b_lundef' in linker.base_options and options['b_lundef'].value:
              args.append('-Wl,--no-undefined')
      except KeyError:
          pass
      try:
          if 'b_asneeded' in linker.base_options and options['b_asneeded'].value:
              args.append(linker.get_asneeded_args())
      except KeyError:
          pass
      try:
          if options['b_coverage'].value:
              args += linker.get_coverage_link_args()
      except KeyError:
          pass
      return args
  
  class CrossNoRunException(MesonException):
      pass
  
  class RunResult:
      def __init__(self, compiled, returncode=999, stdout='UNDEFINED', stderr='UNDEFINED'):
          self.compiled = compiled
          self.returncode = returncode
          self.stdout = stdout
          self.stderr = stderr
  
  class CompilerArgs(list):
      '''
      Class derived from list() that manages a list of compiler arguments. Should
      be used while constructing compiler arguments from various sources. Can be
      operated with ordinary lists, so this does not need to be used everywhere.
  
      All arguments must be inserted and stored in GCC-style (-lfoo, -Idir, etc)
      and can converted to the native type of each compiler by using the
      .to_native() method to which you must pass an instance of the compiler or
      the compiler class.
  
      New arguments added to this class (either with .append(), .extend(), or +=)
      are added in a way that ensures that they override previous arguments.
      For example:
  
      >>> a = ['-Lfoo', '-lbar']
      >>> a += ['-Lpho', '-lbaz']
      >>> print(a)
      ['-Lpho', '-Lfoo', '-lbar', '-lbaz']
  
      Arguments will also be de-duped if they can be de-duped safely.
  
      Note that because of all this, this class is not commutative and does not
      preserve the order of arguments if it is safe to not. For example:
      >>> ['-Ifoo', '-Ibar'] + ['-Ifez', '-Ibaz', '-Werror']
      ['-Ifez', '-Ibaz', '-Ifoo', '-Ibar', '-Werror']
      >>> ['-Ifez', '-Ibaz', '-Werror'] + ['-Ifoo', '-Ibar']
      ['-Ifoo', '-Ibar', '-Ifez', '-Ibaz', '-Werror']
  
      '''
      # NOTE: currently this class is only for C-like compilers, but it can be
      # extended to other languages easily. Just move the following to the
      # compiler class and initialize when self.compiler is set.
  
      # Arg prefixes that override by prepending instead of appending
      prepend_prefixes = ('-I', '-L')
      # Arg prefixes and args that must be de-duped by returning 2
      dedup2_prefixes = ('-I', '-L', '-D', '-U')
      dedup2_suffixes = ()
      dedup2_args = ()
      # Arg prefixes and args that must be de-duped by returning 1
      dedup1_prefixes = ('-l',)
      dedup1_suffixes = ('.lib', '.dll', '.so', '.dylib', '.a')
      # Match a .so of the form path/to/libfoo.so.0.1.0
      # Only UNIX shared libraries require this. Others have a fixed extension.
      dedup1_regex = re.compile(r'([\/\\]|\A)lib.*\.so(\.[0-9]+)?(\.[0-9]+)?(\.[0-9]+)?$')
      dedup1_args = ('-c', '-S', '-E', '-pipe', '-pthread')
      compiler = None
  
      def _check_args(self, args):
          cargs = []
          if len(args) > 2:
              raise TypeError("CompilerArgs() only accepts at most 2 arguments: "
                              "The compiler, and optionally an initial list")
          elif not args:
              return cargs
          elif len(args) == 1:
              if isinstance(args[0], (Compiler, StaticLinker)):
                  self.compiler = args[0]
              else:
                  raise TypeError("you must pass a Compiler instance as one of "
                                  "the arguments")
          elif len(args) == 2:
              if isinstance(args[0], (Compiler, StaticLinker)):
                  self.compiler = args[0]
                  cargs = args[1]
              elif isinstance(args[1], (Compiler, StaticLinker)):
                  cargs = args[0]
                  self.compiler = args[1]
              else:
                  raise TypeError("you must pass a Compiler instance as one of "
                                  "the two arguments")
          else:
              raise AssertionError('Not reached')
          return cargs
  
      def __init__(self, *args):
          super().__init__(self._check_args(args))
  
      @classmethod
      def _can_dedup(cls, arg):
          '''
          Returns whether the argument can be safely de-duped. This is dependent
          on three things:
  
          a) Whether an argument can be 'overridden' by a later argument.  For
             example, -DFOO defines FOO and -UFOO undefines FOO. In this case, we
             can safely remove the previous occurrence and add a new one. The same
             is true for include paths and library paths with -I and -L. For
             these we return `2`. See `dedup2_prefixes` and `dedup2_args`.
          b) Arguments that once specified cannot be undone, such as `-c` or
             `-pipe`. New instances of these can be completely skipped. For these
             we return `1`. See `dedup1_prefixes` and `dedup1_args`.
          c) Whether it matters where or how many times on the command-line
             a particular argument is present. This can matter for symbol
             resolution in static or shared libraries, so we cannot de-dup or
             reorder them. For these we return `0`. This is the default.
  
          In addition to these, we handle library arguments specially.
          With GNU ld, we surround library arguments with -Wl,--start/end-group
          to recursively search for symbols in the libraries. This is not needed
          with other linkers.
          '''
  
          # A standalone argument must never be deduplicated because it is
          # defined by what comes _after_ it. Thus dedupping this:
          # -D FOO -D BAR
          # would yield either
          # -D FOO BAR
          # or
          # FOO -D BAR
          # both of which are invalid.
          if arg in cls.dedup2_prefixes:
              return 0
          if arg in cls.dedup2_args or \
             arg.startswith(cls.dedup2_prefixes) or \
             arg.endswith(cls.dedup2_suffixes):
              return 2
          if arg in cls.dedup1_args or \
             arg.startswith(cls.dedup1_prefixes) or \
             arg.endswith(cls.dedup1_suffixes) or \
             re.search(cls.dedup1_regex, arg):
              return 1
          return 0
  
      @classmethod
      def _should_prepend(cls, arg):
          if arg.startswith(cls.prepend_prefixes):
              return True
          return False
  
      def to_native(self):
          # Check if we need to add --start/end-group for circular dependencies
          # between static libraries, and for recursively searching for symbols
          # needed by static libraries that are provided by object files or
          # shared libraries.
          if get_compiler_uses_gnuld(self.compiler):
              global soregex
              group_start = -1
              for each in self:
                  if not each.startswith('-l') and not each.endswith('.a') and \
                     not soregex.match(each):
                      continue
                  i = self.index(each)
                  if group_start < 0:
                      # First occurrence of a library
                      group_start = i
              if group_start >= 0:
                  # Last occurrence of a library
                  self.insert(i + 1, '-Wl,--end-group')
                  self.insert(group_start, '-Wl,--start-group')
          return self.compiler.unix_args_to_native(self)
  
      def append_direct(self, arg):
          '''
          Append the specified argument without any reordering or de-dup
          '''
          super().append(arg)
  
      def extend_direct(self, iterable):
          '''
          Extend using the elements in the specified iterable without any
          reordering or de-dup
          '''
          super().
  
  [... the rest is omitted ...]
files: mesonbuild/compilers/compilers.py