diff options
28 files changed, 532 insertions, 439 deletions
diff --git a/.appveyor.yml b/.appveyor.yml index 09f67e4..38ebe56 100644 --- a/.appveyor.yml +++ b/.appveyor.yml @@ -18,4 +18,8 @@ build_script: - cmd: echo No build step. test_script: - - cmd: PATH c:\python34;%PATH% && python3 run_tests.py --backend=ninja + - cmd: PATH c:\python34;%PATH%; && python3 run_tests.py --backend=ninja + +on_finish: + - appveyor PushArtifact meson-test-run.txt -DeploymentName "Text test logs" + - appveyor PushArtifact meson-test-run.xml -DeploymentName "XML test logs" @@ -17,14 +17,11 @@ from mesonbuild import mesonmain import sys, os -def main(): - thisfile = __file__ - if not os.path.isabs(thisfile): - thisfile = os.path.normpath(os.path.join(os.getcwd(), thisfile)) - if __package__ == '': - thisfile = os.path.dirname(thisfile) - - sys.exit(mesonmain.run(thisfile, sys.argv[1:])) - -if __name__ == '__main__': - main() +thisfile = __file__ +if not os.path.isabs(thisfile): + thisfile = os.path.normpath(os.path.join(os.getcwd(), thisfile)) + +# The first argument *must* be an absolute path because +# the user may have launched the program from a dir +# that is not in path. +sys.exit(mesonmain.run(thisfile, sys.argv[1:])) diff --git a/mesonbuild/backend/backends.py b/mesonbuild/backend/backends.py index 5681f7c..d5af056 100644 --- a/mesonbuild/backend/backends.py +++ b/mesonbuild/backend/backends.py @@ -72,7 +72,6 @@ class Backend(): self.build = build self.environment = build.environment self.processed_targets = {} - self.dep_rules = {} self.build_to_src = os.path.relpath(self.environment.get_source_dir(), self.environment.get_build_dir()) for t in self.build.targets: @@ -238,27 +237,6 @@ class Backend(): self.write_benchmark_file(datafile) return (test_data, benchmark_data) - def has_source_suffix(self, target, suffix): - for s in target.get_sources(): - if s.endswith(suffix): - return True - return False - - def has_vala(self, target): - return self.has_source_suffix(target, '.vala') - - def has_rust(self, target): - return self.has_source_suffix(target, '.rs') - - def has_cs(self, target): - return self.has_source_suffix(target, '.cs') - - def has_swift(self, target): - return self.has_source_suffix(target, '.swift') - - def has_d(self, target): - return self.has_source_suffix(target, '.d') - def determine_linker(self, target, src): if isinstance(target, build.StaticLibrary): if self.build.static_cross_linker is not None: @@ -641,7 +619,7 @@ class Backend(): final_commands = [] previous = '-fsuch_arguments=woof' for c in commands: - if c.startswith(('-I' '-L', '/LIBPATH')): + if c.startswith(('-I', '-L', '/LIBPATH')): if c in includes: continue includes[c] = True diff --git a/mesonbuild/backend/ninjabackend.py b/mesonbuild/backend/ninjabackend.py index 41f96f1..6b6b4ea 100644 --- a/mesonbuild/backend/ninjabackend.py +++ b/mesonbuild/backend/ninjabackend.py @@ -38,6 +38,12 @@ class RawFilename(): def __init__(self, fname): self.fname = fname + def __str__(self): + return self.fname + + def __repr__(self): + return '<RawFilename: {0}>'.format(self.fname) + def split(self, c): return self.fname.split(c) @@ -233,23 +239,19 @@ int dummy; if isinstance(target, build.Jar): self.generate_jar_target(target, outfile) return - if 'rust' in self.environment.coredata.compilers.keys() and self.has_rust(target): + if 'rust' in target.compilers: self.generate_rust_target(target, outfile) return - if 'cs' in self.environment.coredata.compilers.keys() and self.has_cs(target): + if 'cs' in target.compilers: self.generate_cs_target(target, outfile) return - if 'vala' in self.environment.coredata.compilers.keys() and self.has_vala(target): - vc = self.environment.coredata.compilers['vala'] - vala_output_files = self.generate_vala_compile(vc, target, outfile) - gen_src_deps += vala_output_files - if 'swift' in self.environment.coredata.compilers.keys() and self.has_swift(target): + if 'swift' in target.compilers: self.generate_swift_target(target, outfile) return + if 'vala' in target.compilers: + vala_output_files = self.generate_vala_compile(target, outfile) + gen_src_deps += vala_output_files self.scan_fortran_module_outputs(target) - # The following deals with C/C++ compilation. - (gen_src, gen_other_deps) = self.process_dep_gens(outfile, target) - gen_src_deps += gen_src self.process_target_dependencies(target, outfile) self.generate_custom_generator_rules(target, outfile) outname = self.get_target_filename(target) @@ -260,22 +262,30 @@ int dummy; pch_objects = self.generate_pch(target, outfile) else: pch_objects = [] - header_deps = gen_other_deps + header_deps = [] unity_src = [] unity_deps = [] # Generated sources that must be built before compiling a Unity target. header_deps += self.get_generated_headers(target) - generator_output_sources = [] # Needed to determine the linker + src_list = [] + + # Get a list of all generated *sources* (sources files, headers, + # objects, etc). Needed to determine the linker. + generated_output_sources = [] + # 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 gensource in target.get_generated_sources(): if isinstance(gensource, build.CustomTarget): for src in gensource.output: src = os.path.join(self.get_target_dir(gensource), src) - generator_output_sources.append(src) + generated_output_sources.append(src) if self.environment.is_source(src) and not self.environment.is_header(src): if is_unity: unity_deps.append(os.path.join(self.environment.get_build_dir(), RawFilename(src))) else: - obj_list.append(self.generate_single_compile(target, outfile, RawFilename(src), True, - header_deps)) + generated_source_files.append(RawFilename(src)) elif self.environment.is_object(src): obj_list.append(src) elif self.environment.is_library(src): @@ -287,7 +297,7 @@ int dummy; header_deps.append(RawFilename(src)) else: for src in gensource.get_outfilelist(): - generator_output_sources.append(src) + generated_output_sources.append(src) if self.environment.is_object(src): obj_list.append(os.path.join(self.get_target_private_dir(target), src)) elif not self.environment.is_header(src): @@ -300,9 +310,16 @@ int dummy; abs_src = os.path.join(self.environment.get_build_dir(), rel_src) unity_src.append(abs_src) else: - obj_list.append(self.generate_single_compile(target, outfile, src, True, - header_deps=header_deps)) - src_list = [] + generated_source_files.append(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: + src_list.append(src) + obj_list.append(self.generate_single_compile(target, outfile, src, True, + header_deps=header_deps)) + # Generate compilation targets for sources belonging to this target that + # are generated by other rules (this is only used for Vala right now) for src in gen_src_deps: src_list.append(src) if is_unity: @@ -318,6 +335,7 @@ int dummy; header_deps.append(src) else: obj_list.append(self.generate_single_compile(target, outfile, src, True, [], header_deps)) + # Generate compile targets for all the pre-existing sources for this target for src in target.get_sources(): if src.endswith('.vala'): continue @@ -333,7 +351,7 @@ int dummy; 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, src_list + generator_output_sources) + linker = self.determine_linker(target, src_list + generated_output_sources) 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) @@ -718,8 +736,7 @@ int dummy; outname_rel = os.path.join(self.get_target_dir(target), fname) src_list = target.get_sources() class_list = [] - compiler = self.get_compiler_for_source(src_list[0], False) - assert(compiler.get_language() == 'java') + compiler = target.compilers['java'] c = 'c' m = '' e = '' @@ -769,8 +786,7 @@ int dummy; fname = target.get_filename() outname_rel = os.path.join(self.get_target_dir(target), fname) src_list = target.get_sources() - compiler = self.get_compiler_for_source(src_list[0], False) - assert(compiler.get_language() == 'cs') + compiler = target.compilers['cs'] rel_srcs = [s.rel_to_builddir(self.build_to_src) for s in src_list] deps = [] commands = target.extra_args.get('cs', []) @@ -823,14 +839,14 @@ int dummy; outfile.write('\n') def split_vala_sources(self, sources): - src = [] + other_src = [] vapi_src = [] for s in sources: if s.endswith('.vapi'): vapi_src.append(s) else: - src.append(s) - return (src, vapi_src) + other_src.append(s) + return (other_src, vapi_src) def determine_dep_vapis(self, target): result = [] @@ -845,17 +861,13 @@ int dummy; break return result - def generate_vala_compile(self, compiler, target, outfile): + def generate_vala_compile(self, target, outfile): """Vala is compiled into C. Set up all necessary build steps here.""" - valac = self.environment.coredata.compilers['vala'] - (src, vapi_src) = self.split_vala_sources(target.get_sources()) + valac = target.compilers['vala'] + (other_src, vapi_src) = self.split_vala_sources(target.get_sources()) vapi_src = [x.rel_to_builddir(self.build_to_src) for x in vapi_src] extra_dep_files = [] - vala_input_files = [] - for s in src: - if s.endswith('.vala'): - vala_input_files.append(s.rel_to_builddir(self.build_to_src)) - if len(src) == 0: + if len(other_src) == 0: raise InvalidArguments('Vala library has no Vala source files.') namebase = target.name base_h = namebase + '.h' @@ -866,8 +878,8 @@ int dummy; generated_c_files = [] outputs = [vapiname] args = [] - args += self.build.get_global_args(compiler) - args += compiler.get_buildtype_args(self.environment.coredata.get_builtin_option('buildtype')) + args += self.build.get_global_args(valac) + args += valac.get_buildtype_args(self.environment.coredata.get_builtin_option('buildtype')) args += ['-d', self.get_target_private_dir(target)] args += ['-C']#, '-o', cname] if not isinstance(target, build.Executable): @@ -875,9 +887,23 @@ int dummy; args += ['-H', hname] args += ['--library=' + target.name] args += ['--vapi=' + os.path.join('..', base_vapi)] - for src in vala_input_files: - namebase = os.path.splitext(os.path.split(src)[1])[0] + '.c' - full_c = os.path.join(self.get_target_private_dir(target), namebase) + vala_src = [] + for s in other_src: + if not s.endswith('.vala'): + continue + vala_file = s.rel_to_builddir(self.build_to_src) + vala_src.append(vala_file) + # Figure out where the Vala compiler will write the compiled C file + dirname, basename = os.path.split(vala_file) + # If the Vala file is in a subdir of the build dir (in our case + # because it was generated/built by something else), the subdir path + # components will be preserved in the output path. But if the Vala + # file is outside the build directory, the path components will be + # stripped and just the basename will be used. + c_file = os.path.splitext(basename)[0] + '.c' + if s.is_built: + c_file = os.path.join(dirname, c_file) + full_c = os.path.join(self.get_target_private_dir(target), c_file) generated_c_files.append(full_c) outputs.append(full_c) if self.environment.coredata.get_builtin_option('werror'): @@ -903,14 +929,14 @@ int dummy; args += dependency_vapis element = NinjaBuildElement(self.all_outputs, outputs, valac.get_language() + '_COMPILER', - vala_input_files + vapi_src) + vala_src + vapi_src) element.add_item('ARGS', args) element.add_dep(extra_dep_files) element.write(outfile) return generated_c_files def generate_rust_target(self, target, outfile): - rustc = self.environment.coredata.compilers['rust'] + rustc = target.compilers['rust'] relsrc = [] for i in target.get_sources(): if not rustc.can_compile(i): @@ -1001,7 +1027,7 @@ int dummy; def generate_swift_target(self, target, outfile): module_name = self.target_swift_modulename(target) - swiftc = self.environment.coredata.compilers['swift'] + swiftc = target.compilers['swift'] abssrc = [] abs_headers = [] header_imports = [] @@ -1891,36 +1917,6 @@ rule FORTRAN_DEP_HACK gcda_elem.add_item('description', 'Deleting gcda files') gcda_elem.write(outfile) - def is_compilable_file(self, filename): - if filename.endswith('.cpp') or\ - filename.endswith('.c') or\ - filename.endswith('.cxx') or\ - filename.endswith('.cc') or\ - filename.endswith('.C'): - return True - return False - - def process_dep_gens(self, outfile, target): - src_deps = [] - other_deps = [] - for rule in self.dep_rules.values(): - srcs = target.get_original_kwargs().get(rule.src_keyword, []) - if isinstance(srcs, str): - srcs = [srcs] - for src in srcs: - plainname = os.path.split(src)[1] - basename = plainname.split('.')[0] - outname = rule.name_templ.replace('@BASENAME@', basename).replace('@PLAINNAME@', plainname) - outfilename = os.path.join(self.get_target_private_dir(target), outname) - infilename = os.path.join(self.build_to_src, target.get_source_subdir(), src) - elem = NinjaBuildElement(self.all_outputs, outfilename, rule.name, infilename) - elem.write(outfile) - if self.is_compilable_file(outfilename): - src_deps.append(outfilename) - else: - other_deps.append(outfilename) - return (src_deps, other_deps) - # For things like scan-build and other helper tools we might have. def generate_utils(self, outfile): cmd = [sys.executable, self.environment.get_build_command(), @@ -1932,8 +1928,16 @@ rule FORTRAN_DEP_HACK elem.write(outfile) def generate_ending(self, outfile): - targetlist = [self.get_target_filename(t) for t in self.build.get_targets().values()\ - if not isinstance(t, build.RunTarget)] + targetlist = [] + for t in self.build.get_targets().values(): + # RunTargets are meant to be invoked manually + if isinstance(t, build.RunTarget): + continue + # CustomTargets that aren't installed should only be built if they + # are used by something else or are meant to be always built + if isinstance(t, build.CustomTarget) and not (t.install or t.build_always): + continue + targetlist.append(self.get_target_filename(t)) elem = NinjaBuildElement(self.all_outputs, 'all', 'phony', targetlist) elem.write(outfile) diff --git a/mesonbuild/build.py b/mesonbuild/build.py index dce0236..1ef183b 100644 --- a/mesonbuild/build.py +++ b/mesonbuild/build.py @@ -51,36 +51,16 @@ known_shlib_kwargs.update({'version' : True, 'name_suffix' : True, 'vs_module_defs' : True}) -def sources_are_suffix(sources, suffix): - for source in sources: - if source.endswith('.' + suffix): - return True - return False - -def compiler_is_msvc(sources, is_cross, env): +def compilers_are_msvc(compilers): """ - Since each target does not currently have the compiler information attached - to it, we must do this detection manually here. - - This detection is purposely incomplete and will cause bugs if other code is - extended and this piece of code is forgotten. + Check if all the listed compilers are MSVC. Used by Executable, + StaticLibrary, and SharedLibrary for deciding when to use MSVC-specific + file naming. """ - compiler = None - if sources_are_suffix(sources, 'c'): - try: - compiler = env.detect_c_compiler(is_cross) - except MesonException: + for compiler in compilers.values(): + if compiler.get_id() != 'msvc': return False - elif sources_are_suffix(sources, 'cxx') or \ - sources_are_suffix(sources, 'cpp') or \ - sources_are_suffix(sources, 'cc'): - try: - compiler = env.detect_cpp_compiler(is_cross) - except MesonException: - return False - if compiler and compiler.get_id() == 'msvc': - return True - return False + return True class InvalidArguments(MesonException): @@ -234,7 +214,9 @@ class BuildTarget(): self.subdir = subdir self.subproject = subproject # Can not be calculated from subdir as subproject dirname can be changed per project. self.is_cross = is_cross + self.environment = environment self.sources = [] + self.compilers = {} self.objects = [] self.external_deps = [] self.include_dirs = [] @@ -256,6 +238,7 @@ class BuildTarget(): len(self.generated) == 0 and \ len(self.objects) == 0: raise InvalidArguments('Build target %s has no sources.' % name) + self.process_compilers() self.validate_sources() def __repr__(self): @@ -320,16 +303,52 @@ class BuildTarget(): msg = 'Bad source of type {!r} in target {!r}.'.format(type(s).__name__, self.name) raise InvalidArguments(msg) + @staticmethod + def can_compile_remove_sources(compiler, sources): + removed = False + for s in sources[:]: + if compiler.can_compile(s): + sources.remove(s) + removed = True + return removed + + def process_compilers(self): + if len(self.sources) == 0: + return + sources = list(self.sources) + if self.is_cross: + compilers = self.environment.coredata.cross_compilers + else: + compilers = self.environment.coredata.compilers + for lang, compiler in compilers.items(): + if self.can_compile_remove_sources(compiler, sources): + self.compilers[lang] = compiler + def validate_sources(self): - if len(self.sources) > 0: + if len(self.sources) == 0: + return + for lang in ('cs', 'java'): + if lang in self.compilers: + check_sources = list(self.sources) + compiler = self.compilers[lang] + if not self.can_compile_remove_sources(compiler, check_sources): + m = 'No {} sources found in target {!r}'.format(lang, self.name) + raise InvalidArguments(m) + if check_sources: + m = '{0} targets can only contain {0} files:\n'.format(lang.capitalize()) + m += '\n'.join([repr(c) for c in check_sources]) + raise InvalidArguments(m) + # CSharp and Java targets can't contain any other file types + assert(len(self.compilers) == 1) + return + if 'rust' in self.compilers: firstname = self.sources[0] if isinstance(firstname, File): firstname = firstname.fname first = os.path.split(firstname)[1] (base, suffix) = os.path.splitext(first) - if suffix == '.rs': - if self.name != base: - raise InvalidArguments('In Rust targets, the first source file must be named projectname.rs.') + if suffix != '.rs' or self.name != base: + raise InvalidArguments('In Rust targets, the first source file must be named projectname.rs.') def get_original_kwargs(self): return self.kwargs @@ -768,7 +787,7 @@ class Executable(BuildTarget): self.prefix = '' if not hasattr(self, 'suffix'): # Executable for Windows or C#/Mono - if for_windows(is_cross, environment) or sources_are_suffix(self.sources, 'cs'): + if for_windows(is_cross, environment) or 'cs' in self.compilers: self.suffix = 'exe' else: self.suffix = '' @@ -777,8 +796,7 @@ class Executable(BuildTarget): self.filename += '.' + self.suffix # See determine_debug_filenames() in build.SharedLibrary buildtype = environment.coredata.get_builtin_option('buildtype') - if compiler_is_msvc(self.sources, is_cross, environment) and \ - buildtype.startswith('debug'): + if compilers_are_msvc(self.compilers) and buildtype.startswith('debug'): self.debug_filename = self.prefix + self.name + '.pdb' def type_suffix(self): @@ -787,7 +805,7 @@ class Executable(BuildTarget): class StaticLibrary(BuildTarget): def __init__(self, name, subdir, subproject, is_cross, sources, objects, environment, kwargs): super().__init__(name, subdir, subproject, is_cross, sources, objects, environment, kwargs) - if sources_are_suffix(self.sources, 'cs'): + if 'cs' in self.compilers: raise InvalidArguments('Static libraries not supported for C#.') # By default a static library is named libfoo.a even on Windows because # MSVC does not have a consistent convention for what static libraries @@ -800,15 +818,14 @@ class StaticLibrary(BuildTarget): self.prefix = 'lib' if not hasattr(self, 'suffix'): # Rust static library crates have .rlib suffix - if sources_are_suffix(self.sources, 'rs'): + if 'rust' in self.compilers: self.suffix = 'rlib' else: self.suffix = 'a' self.filename = self.prefix + self.name + '.' + self.suffix # See determine_debug_filenames() in build.SharedLibrary buildtype = environment.coredata.get_builtin_option('buildtype') - if compiler_is_msvc(self.sources, is_cross, environment) and \ - buildtype.startswith('debug'): + if compilers_are_msvc(self.compilers) and buildtype.startswith('debug'): self.debug_filename = self.prefix + self.name + '.pdb' def type_suffix(self): @@ -864,12 +881,12 @@ class SharedLibrary(BuildTarget): if self.prefix != None and self.suffix != None: pass # C# and Mono - elif sources_are_suffix(self.sources, 'cs'): + elif 'cs' in self.compilers: prefix = '' suffix = 'dll' self.filename_tpl = '{0.prefix}{0.name}.{0.suffix}' # Rust - elif sources_are_suffix(self.sources, 'rs'): + elif 'rust' in self.compilers: # Currently, we always build --crate-type=rlib prefix = 'lib' suffix = 'rlib' @@ -881,7 +898,7 @@ class SharedLibrary(BuildTarget): suffix = 'dll' self.vs_import_filename = '{0}.lib'.format(self.name) self.gcc_import_filename = 'lib{0}.dll.a'.format(self.name) - if compiler_is_msvc(self.sources, is_cross, env): + if compilers_are_msvc(self.compilers): # Shared library is of the form foo.dll prefix = '' # Import library is called foo.lib @@ -928,7 +945,7 @@ class SharedLibrary(BuildTarget): determine_filenames() above. """ buildtype = env.coredata.get_builtin_option('buildtype') - if compiler_is_msvc(self.sources, is_cross, env) and buildtype.startswith('debug'): + if compilers_are_msvc(self.compilers) and buildtype.startswith('debug'): # Currently we only implement separate debug symbol files for MSVC # since the toolchain does it for us. Other toolchains embed the # debugging symbols in the file itself by default. diff --git a/mesonbuild/compilers.py b/mesonbuild/compilers.py index c5b932c..19fb888 100644 --- a/mesonbuild/compilers.py +++ b/mesonbuild/compilers.py @@ -23,12 +23,27 @@ from . import coredata 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'] -cpp_suffixes = ['cc', 'cpp', 'cxx', 'h', 'hh', 'hpp', 'ipp', 'hxx', 'c++'] -c_suffixes = ['c'] -clike_suffixes = c_suffixes + cpp_suffixes -obj_suffixes = ['o', 'obj', 'res'] -lib_suffixes = ['a', 'lib', 'dll', 'dylib', 'so'] +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'), + 'fortran': ('f', 'f90', 'f95'), + 'd': ('d', 'di'), + 'objc': ('m',), + 'objcpp': ('mm',), + 'rust': ('rs',), + 'vala': ('vala', 'vapi'), + 'cs': ('cs',), + 'swift': ('swift',), + 'java': ('java',), +} +cpp_suffixes = lang_suffixes['cpp'] + ('h',) +c_suffixes = lang_suffixes['c'] + ('h',) +clike_suffixes = lang_suffixes['c'] + lang_suffixes['cpp'] + ('h',) def is_header(fname): if hasattr(fname, 'fname'): @@ -300,9 +315,38 @@ class Compiler(): self.exelist = exelist else: raise TypeError('Unknown argument to Compiler') + # In case it's been overriden by a child class already + if not hasattr(self, 'file_suffixes'): + self.file_suffixes = lang_suffixes[self.language] + if not hasattr(self, 'can_compile_suffixes'): + self.can_compile_suffixes = set(self.file_suffixes) + self.default_suffix = self.file_suffixes[0] self.version = version self.base_options = [] + def can_compile(self, src): + if hasattr(src, 'fname'): + src = src.fname + suffix = os.path.splitext(src)[1].lower() + if suffix and suffix[1:] in self.can_compile_suffixes: + return True + return False + + def get_id(self): + return self.id + + def get_language(self): + return self.language + + def get_exelist(self): + return self.exelist[:] + + def get_define(self, *args, **kwargs): + raise EnvironmentException('%s does not support get_define.' % self.id) + + def has_define(self, *args, **kwargs): + raise EnvironmentException('%s does not support has_define.' % self.id) + def get_always_args(self): return [] @@ -391,11 +435,13 @@ class Compiler(): class CCompiler(Compiler): def __init__(self, exelist, version, is_cross, exe_wrapper=None): + # If a child ObjC or CPP class has already set it, don't set it ourselves + if not hasattr(self, 'language'): + self.language = 'c' super().__init__(exelist, version) - self.language = 'c' - self.default_suffix = 'c' self.id = 'unknown' self.is_cross = is_cross + self.can_compile_suffixes.add('h') if isinstance(exe_wrapper, str): self.exe_wrapper = [exe_wrapper] else: @@ -434,9 +480,6 @@ class CCompiler(Compiler): def build_rpath_args(self, build_dir, rpath_paths, install_rpath): return build_unix_rpath_args(build_dir, rpath_paths, install_rpath) - def get_id(self): - return self.id - def get_dependency_gen_args(self, outtarget, outfile): return ['-MMD', '-MQ', outtarget, '-MF', outfile] @@ -446,9 +489,6 @@ class CCompiler(Compiler): def get_depfile_suffix(self): return 'd' - def get_language(self): - return self.language - def get_default_suffix(self): return self.default_suffix @@ -502,12 +542,6 @@ class CCompiler(Compiler): return libstr.split(':') return [] - def can_compile(self, filename): - suffix = filename.split('.')[-1] - if suffix == 'c' or suffix == 'h': - return True - return False - def get_pic_args(self): return ['-fPIC'] @@ -993,15 +1027,10 @@ void bar() { class CPPCompiler(CCompiler): def __init__(self, exelist, version, is_cross, exe_wrap): + # If a child ObjCPP class has already set it, don't set it ourselves + if not hasattr(self, 'language'): + self.language = 'cpp' CCompiler.__init__(self, exelist, version, is_cross, exe_wrap) - self.language = 'cpp' - self.default_suffix = 'cpp' - - def can_compile(self, filename): - suffix = filename.split('.')[-1] - if suffix in cpp_suffixes: - return True - return False def sanity_check(self, work_dir, environment): code = 'class breakCCompiler;int main(int argc, char **argv) { return 0; }\n' @@ -1009,15 +1038,8 @@ class CPPCompiler(CCompiler): class ObjCCompiler(CCompiler): def __init__(self, exelist, version, is_cross, exe_wrap): - CCompiler.__init__(self, exelist, version, is_cross, exe_wrap) self.language = 'objc' - self.default_suffix = 'm' - - def can_compile(self, filename): - suffix = filename.split('.')[-1] - if suffix == 'm' or suffix == 'h': - return True - return False + CCompiler.__init__(self, exelist, version, is_cross, exe_wrap) def sanity_check(self, work_dir, environment): # TODO try to use sanity_check_impl instead of duplicated code @@ -1043,15 +1065,8 @@ class ObjCCompiler(CCompiler): class ObjCPPCompiler(CPPCompiler): def __init__(self, exelist, version, is_cross, exe_wrap): - CPPCompiler.__init__(self, exelist, version, is_cross, exe_wrap) self.language = 'objcpp' - self.default_suffix = 'mm' - - def can_compile(self, filename): - suffix = filename.split('.')[-1] - if suffix == 'mm' or suffix == 'h': - return True - return False + CPPCompiler.__init__(self, exelist, version, is_cross, exe_wrap) def sanity_check(self, work_dir, environment): # TODO try to use sanity_check_impl instead of duplicated code @@ -1078,9 +1093,8 @@ class ObjCPPCompiler(CPPCompiler): class MonoCompiler(Compiler): def __init__(self, exelist, version): - super().__init__(exelist, version) self.language = 'cs' - self.default_suffix = 'cs' + super().__init__(exelist, version) self.id = 'mono' self.monorunner = 'mono' @@ -1102,21 +1116,12 @@ class MonoCompiler(Compiler): def build_rpath_args(self, build_dir, rpath_paths, install_rpath): return [] - def get_id(self): - return self.id - def get_dependency_gen_args(self, outtarget, outfile): return [] - def get_language(self): - return self.language - def get_default_suffix(self): return self.default_suffix - def get_exelist(self): - return self.exelist[:] - def get_linker_exelist(self): return self.exelist[:] @@ -1141,12 +1146,6 @@ class MonoCompiler(Compiler): def get_std_shared_lib_link_args(self): return [] - def can_compile(self, filename): - suffix = filename.split('.')[-1] - if suffix == 'cs': - return True - return False - def get_pic_args(self): return [] @@ -1187,9 +1186,8 @@ class MonoCompiler(Compiler): class JavaCompiler(Compiler): def __init__(self, exelist, version): - super().__init__(exelist, version) self.language = 'java' - self.default_suffix = 'java' + super().__init__(exelist, version) self.id = 'unknown' self.javarunner = 'java' @@ -1205,21 +1203,12 @@ class JavaCompiler(Compiler): def build_rpath_args(self, build_dir, rpath_paths, install_rpath): return [] - def get_id(self): - return self.id - def get_dependency_gen_args(self, outtarget, outfile): return [] - def get_language(self): - return self.language - def get_default_suffix(self): return self.default_suffix - def get_exelist(self): - return self.exelist[:] - def get_linker_exelist(self): return self.exelist[:] @@ -1249,12 +1238,6 @@ class JavaCompiler(Compiler): def get_std_shared_lib_link_args(self): return [] - def can_compile(self, filename): - suffix = filename.split('.')[-1] - if suffix == 'java': - return True - return False - def get_pic_args(self): return [] @@ -1296,10 +1279,10 @@ class JavaCompiler(Compiler): class ValaCompiler(Compiler): def __init__(self, exelist, version): + self.language = 'vala' super().__init__(exelist, version) self.version = version - self.id = 'unknown' - self.language = 'vala' + self.id = 'valac' self.is_cross = False def name_string(self): @@ -1308,15 +1291,9 @@ class ValaCompiler(Compiler): def needs_static_linker(self): return False # Because compiles into C. - def get_exelist(self): - return self.exelist[:] - def get_werror_args(self): return ['--fatal-warnings'] - def get_language(self): - return self.language - def sanity_check(self, work_dir, environment): src = 'valatest.vala' source_name = os.path.join(work_dir, src) @@ -1330,10 +1307,6 @@ class ValaCompiler(Compiler): if pc.returncode != 0: raise EnvironmentException('Vala compiler %s can not compile programs.' % self.name_string()) - def can_compile(self, filename): - suffix = filename.split('.')[-1] - return suffix in ('vala', 'vapi') - def get_buildtype_args(self, buildtype): if buildtype == 'debug' or buildtype == 'debugoptimized' or buildtype == 'minsize': return ['--debug'] @@ -1341,9 +1314,9 @@ class ValaCompiler(Compiler): class RustCompiler(Compiler): def __init__(self, exelist, version): - super().__init__(exelist, version) - self.id = 'unknown' self.language = 'rust' + super().__init__(exelist, version) + self.id = 'rustc' def needs_static_linker(self): return False @@ -1351,15 +1324,6 @@ class RustCompiler(Compiler): def name_string(self): return ' '.join(self.exelist) - def get_exelist(self): - return self.exelist[:] - - def get_id(self): - return self.id - - def get_language(self): - return self.language - def sanity_check(self, work_dir, environment): source_name = os.path.join(work_dir, 'sanity.rs') output_name = os.path.join(work_dir, 'rusttest') @@ -1374,9 +1338,6 @@ class RustCompiler(Compiler): if subprocess.call(output_name) != 0: raise EnvironmentException('Executables created by Rust compiler %s are not runnable.' % self.name_string()) - def can_compile(self, fname): - return fname.endswith('.rs') - def get_dependency_gen_args(self, outfile): return ['--dep-info', outfile] @@ -1385,15 +1346,12 @@ class RustCompiler(Compiler): class SwiftCompiler(Compiler): def __init__(self, exelist, version): + self.language = 'swift' super().__init__(exelist, version) self.version = version self.id = 'llvm' - self.language = 'swift' self.is_cross = False - def get_id(self): - return self.id - def get_linker_exelist(self): return self.exelist[:] @@ -1403,15 +1361,9 @@ class SwiftCompiler(Compiler): def needs_static_linker(self): return True - def get_exelist(self): - return self.exelist[:] - def get_werror_args(self): return ['--fatal-warnings'] - def get_language(self): - return self.language - def get_dependency_gen_args(self, outtarget, outfile): return ['-emit-dependencies'] @@ -1472,15 +1424,11 @@ class SwiftCompiler(Compiler): if subprocess.call(output_name) != 0: raise EnvironmentException('Executables created by Swift compiler %s are not runnable.' % self.name_string()) - def can_compile(self, filename): - suffix = filename.split('.')[-1] - return suffix in ('swift') - class DCompiler(Compiler): def __init__(self, exelist, version, is_cross): + self.language = 'd' super().__init__(exelist, version) self.id = 'unknown' - self.language = 'd' self.is_cross = is_cross def sanity_check(self, work_dir, environment): @@ -1503,19 +1451,6 @@ class DCompiler(Compiler): def name_string(self): return ' '.join(self.exelist) - def get_exelist(self): - return self.exelist - - def get_id(self): - return self.id - - def get_language(self): - return self.language - - def can_compile(self, fname): - suffix = fname.split('.')[-1] - return suffix in ('d', 'di') - def get_linker_exelist(self): return self.exelist[:] @@ -1918,17 +1853,11 @@ class VisualStudioCCompiler(CCompiler): class VisualStudioCPPCompiler(VisualStudioCCompiler): def __init__(self, exelist, version, is_cross, exe_wrap): - VisualStudioCCompiler.__init__(self, exelist, version, is_cross, exe_wrap) self.language = 'cpp' + VisualStudioCCompiler.__init__(self, exelist, version, is_cross, exe_wrap) self.default_suffix = 'cpp' self.base_options = ['b_pch'] # FIXME add lto, pgo and the like - def can_compile(self, filename): - suffix = filename.split('.')[-1] - if suffix in cpp_suffixes: - return True - return False - def get_options(self): return {'cpp_eh' : coredata.UserComboOption('cpp_eh', 'C++ exception handling type.', @@ -1974,9 +1903,10 @@ def get_gcc_soname_args(gcc_type, shlib_name, path, soversion): class GnuCompiler: # Functionality that is common to all GNU family compilers. - def __init__(self, gcc_type): + def __init__(self, gcc_type, defines): self.id = 'gcc' self.gcc_type = gcc_type + self.defines = defines or {} self.base_options = ['b_pch', 'b_lto', 'b_pgo', 'b_sanitize', 'b_coverage', 'b_colorout', 'b_ndebug'] if self.gcc_type != GCC_OSX: @@ -1996,6 +1926,13 @@ class GnuCompiler: args[args.index('-Wpedantic')] = '-pedantic' return args + def has_define(self, define): + return define in self.defines + + def get_define(self, define): + if define in self.defines: + return defines[define] + def get_pic_args(self): if self.gcc_type == GCC_MINGW: return [] # On Window gcc defaults to fpic being always on. @@ -2020,16 +1957,15 @@ class GnuCompiler: return get_gcc_soname_args(self.gcc_type, shlib_name, path, soversion) class GnuCCompiler(GnuCompiler, CCompiler): - def __init__(self, exelist, version, gcc_type, is_cross, exe_wrapper=None): + def __init__(self, exelist, version, gcc_type, is_cross, exe_wrapper=None, defines=None): CCompiler.__init__(self, exelist, version, is_cross, exe_wrapper) - GnuCompiler.__init__(self, gcc_type) + GnuCompiler.__init__(self, gcc_type, defines) + # Gcc can do asm, too. + self.can_compile_suffixes.add('s') self.warn_args = {'1': ['-Wall', '-Winvalid-pch'], '2': ['-Wall', '-Wextra', '-Winvalid-pch'], '3' : ['-Wall', '-Wpedantic', '-Wextra', '-Winvalid-pch']} - def can_compile(self, filename): - return super().can_compile(filename) or filename.split('.')[-1].lower() == 's' # Gcc can do asm, too. - def get_options(self): opts = {'c_std' : coredata.UserComboOption('c_std', 'C language standard to use', ['none', 'c89', 'c99', 'c11', 'gnu89', 'gnu99', 'gnu11'], @@ -2055,9 +1991,9 @@ class GnuCCompiler(GnuCompiler, CCompiler): class GnuCPPCompiler(GnuCompiler, CPPCompiler): - def __init__(self, exelist, version, gcc_type, is_cross, exe_wrap): + def __init__(self, exelist, version, gcc_type, is_cross, exe_wrap, defines): CPPCompiler.__init__(self, exelist, version, is_cross, exe_wrap) - GnuCompiler.__init__(self, gcc_type) + GnuCompiler.__init__(self, gcc_type, defines) self.warn_args = {'1': ['-Wall', '-Winvalid-pch', '-Wnon-virtual-dtor'], '2': ['-Wall', '-Wextra', '-Winvalid-pch', '-Wnon-virtual-dtor'], '3': ['-Wall', '-Wpedantic', '-Wextra', '-Winvalid-pch', '-Wnon-virtual-dtor']} @@ -2093,22 +2029,22 @@ class GnuCPPCompiler(GnuCompiler, CPPCompiler): class GnuObjCCompiler(GnuCompiler,ObjCCompiler): - def __init__(self, exelist, version, is_cross, exe_wrapper=None): + def __init__(self, exelist, version, is_cross, exe_wrapper=None, defines=None): ObjCCompiler.__init__(self, exelist, version, is_cross, exe_wrapper) # Not really correct, but GNU objc is only used on non-OSX non-win. File a bug # if this breaks your use case. - GnuCompiler.__init__(self, GCC_STANDARD) + GnuCompiler.__init__(self, GCC_STANDARD, defines) self.warn_args = {'1': ['-Wall', '-Winvalid-pch'], '2': ['-Wall', '-Wextra', '-Winvalid-pch'], '3' : ['-Wall', '-Wpedantic', '-Wextra', '-Winvalid-pch']} class GnuObjCPPCompiler(GnuCompiler, ObjCPPCompiler): - def __init__(self, exelist, version, is_cross, exe_wrapper=None): - ObjCCompiler.__init__(self, exelist, version, is_cross, exe_wrapper) + def __init__(self, exelist, version, is_cross, exe_wrapper=None, defines=None): + ObjCPPCompiler.__init__(self, exelist, version, is_cross, exe_wrapper) # Not really correct, but GNU objc is only used on non-OSX non-win. File a bug # if this breaks your use case. - GnuCompiler.__init__(self, GCC_STANDARD) + GnuCompiler.__init__(self, GCC_STANDARD, defines) self.warn_args = {'1': ['-Wall', '-Winvalid-pch', '-Wnon-virtual-dtor'], '2': ['-Wall', '-Wextra', '-Winvalid-pch', '-Wnon-virtual-dtor'], '3' : ['-Wall', '-Wpedantic', '-Wextra', '-Winvalid-pch', '-Wnon-virtual-dtor']} @@ -2138,10 +2074,23 @@ class ClangCompiler(): # so it might change semantics at any time. return ['-include-pch', os.path.join (pch_dir, self.get_pch_name (header))] + def get_soname_args(self, shlib_name, path, soversion): + if self.clang_type == CLANG_STANDARD: + gcc_type = GCC_STANDARD + elif self.clang_type == CLANG_OSX: + gcc_type = GCC_OSX + elif self.clang_type == CLANG_WIN: + gcc_type = GCC_MINGW + else: + raise MesonException('Unreachable code when converting clang type to gcc type.') + return get_gcc_soname_args(gcc_type, shlib_name, path, soversion) + class ClangCCompiler(ClangCompiler, CCompiler): def __init__(self, exelist, version, clang_type, is_cross, exe_wrapper=None): CCompiler.__init__(self, exelist, version, is_cross, exe_wrapper) ClangCompiler.__init__(self, clang_type) + # Clang can do asm, too. + self.can_compile_suffixes.add('s') self.warn_args = {'1': ['-Wall', '-Winvalid-pch'], '2': ['-Wall', '-Wextra', '-Winvalid-pch'], '3' : ['-Wall', '-Wpedantic', '-Wextra', '-Winvalid-pch']} @@ -2164,9 +2113,6 @@ class ClangCCompiler(ClangCompiler, CCompiler): def has_argument(self, arg, env): return super().has_argument(['-Werror=unknown-warning-option', arg], env) - def can_compile(self, filename): - return super().can_compile(filename) or filename.split('.')[-1].lower() == 's' # Clang can do asm, too. - class ClangCPPCompiler(ClangCompiler, CPPCompiler): def __init__(self, exelist, version, cltype, is_cross, exe_wrapper=None): @@ -2194,9 +2140,6 @@ class ClangCPPCompiler(ClangCompiler, CPPCompiler): def has_argument(self, arg, env): return super().has_argument(['-Werror=unknown-warning-option', arg], env) - def can_compile(self, filename): - return super().can_compile(filename) or filename.split('.')[-1].lower() == 's' # Clang can do asm, too. - class ClangObjCCompiler(GnuObjCCompiler): def __init__(self, exelist, version, cltype, is_cross, exe_wrapper=None): super().__init__(exelist, version, is_cross, exe_wrapper) @@ -2219,26 +2162,17 @@ class ClangObjCPPCompiler(GnuObjCPPCompiler): class FortranCompiler(Compiler): def __init__(self, exelist, version, is_cross, exe_wrapper=None): + self.language = 'fortran' super().__init__(exelist, version) self.is_cross = is_cross self.exe_wrapper = exe_wrapper - self.language = 'fortran' # Not really correct but I don't have Fortran compilers to test with. Sorry. self.gcc_type = GCC_STANDARD self.id = "IMPLEMENTATION CLASSES MUST SET THIS" - def get_id(self): - return self.id - def name_string(self): return ' '.join(self.exelist) - def get_exelist(self): - return self.exelist[:] - - def get_language(self): - return self.language - def get_pic_args(self): if self.gcc_type == GCC_MINGW: return [] # On Windows gcc defaults to fpic being always on. @@ -2308,14 +2242,6 @@ end program prog def get_linker_output_args(self, outputname): return ['-o', outputname] - def can_compile(self, src): - if hasattr(src, 'fname'): - src = src.fname - suffix = os.path.splitext(src)[1].lower() - if suffix == '.f' or suffix == '.f95' or suffix == '.f90': - return True - return False - def get_include_args(self, path, is_system): return ['-I' + path] @@ -2342,11 +2268,19 @@ end program prog class GnuFortranCompiler(FortranCompiler): - def __init__(self, exelist, version, gcc_type, is_cross, exe_wrapper=None): + def __init__(self, exelist, version, gcc_type, is_cross, exe_wrapper=None, defines=None): super().__init__(exelist, version, is_cross, exe_wrapper=None) self.gcc_type = gcc_type + self.defines = defines or {} self.id = 'gcc' + def has_define(self, define): + return define in self.defines + + def get_define(self, define): + if define in self.defines: + return defines[define] + def get_always_args(self): return ['-pipe'] @@ -2398,18 +2332,13 @@ class IntelFortranCompiler(FortranCompiler): std_warn_args = ['-warn', 'all'] def __init__(self, exelist, version, is_cross, exe_wrapper=None): + self.file_suffixes = ('f', 'f90') super().__init__(exelist, version, is_cross, exe_wrapper=None) self.id = 'intel' def get_module_outdir_args(self, path): return ['-module', path] - def can_compile(self, src): - suffix = os.path.splitext(src)[1].lower() - if suffix == '.f' or suffix == '.f90': - return True - return False - def get_warn_args(self, level): return IntelFortranCompiler.std_warn_args @@ -2423,12 +2352,6 @@ class PathScaleFortranCompiler(FortranCompiler): def get_module_outdir_args(self, path): return ['-module', path] - def can_compile(self, src): - suffix = os.path.splitext(src)[1].lower() - if suffix == '.f' or suffix == '.f90' or suffix == '.f95': - return True - return False - def get_std_warn_args(self, level): return PathScaleFortranCompiler.std_warn_args @@ -2442,12 +2365,6 @@ class PGIFortranCompiler(FortranCompiler): def get_module_outdir_args(self, path): return ['-module', path] - def can_compile(self, src): - suffix = os.path.splitext(src)[1].lower() - if suffix == '.f' or suffix == '.f90' or suffix == '.f95': - return True - return False - def get_warn_args(self, level): return PGIFortranCompiler.std_warn_args @@ -2462,12 +2379,6 @@ class Open64FortranCompiler(FortranCompiler): def get_module_outdir_args(self, path): return ['-module', path] - def can_compile(self, src): - suffix = os.path.splitext(src)[1].lower() - if suffix == '.f' or suffix == '.f90' or suffix == '.f95': - return True - return False - def get_warn_args(self, level): return Open64FortranCompiler.std_warn_args @@ -2484,12 +2395,6 @@ class NAGFortranCompiler(FortranCompiler): def get_always_args(self): return [] - def can_compile(self, src): - suffix = os.path.splitext(src)[1].lower() - if suffix == '.f' or suffix == '.f90' or suffix == '.f95': - return True - return False - def get_warn_args(self, level): return NAGFortranCompiler.std_warn_args diff --git a/mesonbuild/dependencies.py b/mesonbuild/dependencies.py index 106273c..1b9e6f4 100644 --- a/mesonbuild/dependencies.py +++ b/mesonbuild/dependencies.py @@ -25,6 +25,7 @@ import sysconfig from . mesonlib import MesonException from . import mlog from . import mesonlib +from .environment import detect_cpu_family class DependencyException(MesonException): def __init__(self, *args, **kwargs): @@ -474,7 +475,12 @@ class BoostDependency(Dependency): def __init__(self, environment, kwargs): Dependency.__init__(self) self.name = 'boost' + self.environment = environment self.libdir = '' + if 'native' in kwargs and environment.is_cross_build(): + want_cross = not kwargs['native'] + else: + want_cross = environment.is_cross_build() try: self.boost_root = os.environ['BOOST_ROOT'] if not os.path.isabs(self.boost_root): @@ -482,6 +488,8 @@ class BoostDependency(Dependency): except KeyError: self.boost_root = None if self.boost_root is None: + if want_cross: + raise DependencyException('BOOST_ROOT is needed while cross-compiling') if mesonlib.is_windows(): self.boost_root = self.detect_win_root() self.incdir = self.boost_root @@ -575,11 +583,21 @@ class BoostDependency(Dependency): return self.detect_lib_modules_nix() def detect_lib_modules_win(self): - if mesonlib.is_32bit(): + arch = detect_cpu_family(self.environment.coredata.compilers) + # Guess the libdir + if arch == 'x86': gl = 'lib32*' - else: + elif arch == 'x86_64': gl = 'lib64*' - libdir = glob.glob(os.path.join(self.boost_root, gl)) + else: + # Does anyone do Boost cross-compiling to other archs on Windows? + gl = None + # See if the libdir is valid + if gl: + libdir = glob.glob(os.path.join(self.boost_root, gl)) + else: + libdir = [] + # Can't find libdir, bail if len(libdir) == 0: return libdir = libdir[0] diff --git a/mesonbuild/environment.py b/mesonbuild/environment.py index 341e5e8..e411687 100644 --- a/mesonbuild/environment.py +++ b/mesonbuild/environment.py @@ -57,26 +57,89 @@ def detect_ninja(): if p.returncode == 0 and mesonlib.version_compare(version, ">=1.6"): return n -def detect_cpu_family(): +def detect_native_windows_arch(): + """ + The architecture of Windows itself: x86 or amd64 + """ + # These env variables are always available. See: + # https://msdn.microsoft.com/en-us/library/aa384274(VS.85).aspx + # https://blogs.msdn.microsoft.com/david.wang/2006/03/27/howto-detect-process-bitness/ + arch = os.environ.get('PROCESSOR_ARCHITEW6432', '').lower() + if not arch: + try: + # If this doesn't exist, something is messing with the environment + arch = os.environ['PROCESSOR_ARCHITECTURE'].lower() + except KeyError: + raise InterpreterException('Unable to detect native OS architecture') + return arch + +def detect_windows_arch(compilers): + """ + Detecting the 'native' architecture of Windows is not a trivial task. We + cannot trust that the architecture that Python is built for is the 'native' + one because you can run 32-bit apps on 64-bit Windows using WOW64 and + people sometimes install 32-bit Python on 64-bit Windows. + + We also can't rely on the architecture of the OS itself, since it's + perfectly normal to compile and run 32-bit applications on Windows as if + they were native applications. It's a terrible experience to require the + user to supply a cross-info file to compile 32-bit applications on 64-bit + Windows. Thankfully, the only way to compile things with Visual Studio on + Windows is by entering the 'msvc toolchain' environment, which can be + easily detected. + + In the end, the sanest method is as follows: + 1. Check if we're in an MSVC toolchain environment, and if so, return the + MSVC toolchain architecture as our 'native' architecture. + 2. If not, check environment variables that are set by Windows and WOW64 to + find out the architecture that Windows is built for, and use that as our + 'native' architecture. + """ + os_arch = detect_native_windows_arch() + if os_arch != 'amd64': + return os_arch + # If we're on 64-bit Windows, 32-bit apps can be compiled without + # cross-compilation. So if we're doing that, just set the native arch as + # 32-bit and pretend like we're running under WOW64. Else, return the + # actual Windows architecture that we deduced above. + for compiler in compilers.values(): + # Check if we're using and inside an MSVC toolchain environment + if compiler.id == 'msvc' and 'VCINSTALLDIR' in os.environ: + # 'Platform' is only set when the target arch is not 'x86'. + # It's 'x64' when targetting x86_64 and 'arm' when targetting ARM. + platform = os.environ.get('Platform', 'x86').lower() + if platform == 'x86': + return platform + if compiler.id == 'gcc' and compiler.has_define('__i386__'): + return 'x86' + return os_arch + +def detect_cpu_family(compilers): """ Python is inconsistent in its platform module. It returns different values for the same cpu. For x86 it might return 'x86', 'i686' or somesuch. Do some canonicalization. """ - trial = platform.machine().lower() + if mesonlib.is_windows(): + trial = detect_windows_arch(compilers) + else: + trial = platform.machine().lower() if trial.startswith('i') and trial.endswith('86'): return 'x86' if trial.startswith('arm'): return 'arm' - if trial == 'amd64': + if trial in ('amd64', 'x64'): return 'x86_64' # Add fixes here as bugs are reported. return trial -def detect_cpu(): - trial = platform.machine().lower() - if trial == 'amd64': +def detect_cpu(compilers): + if mesonlib.is_windows(): + trial = detect_windows_arch(compilers) + else: + trial = platform.machine().lower() + if trial in ('amd64', 'x64'): return 'x86_64' # Add fixes here as bugs are reported. return trial @@ -225,6 +288,45 @@ class Environment(): if type(oldval) != type(value): self.coredata.user_options[name] = value + @staticmethod + def get_gnu_compiler_defines(compiler): + """ + Detect GNU compiler platform type (Apple, MinGW, Unix) + """ + # Arguments to output compiler pre-processor defines to stdout + # gcc, g++, and gfortran all support these arguments + args = compiler + ['-E', '-dM', '-'] + p = subprocess.Popen(args, universal_newlines=True, + stdin=subprocess.PIPE, stdout=subprocess.PIPE) + output = p.communicate('')[0] + if p.returncode != 0: + raise EnvironmentException('Unable to detect GNU compiler type:\n' + output) + # Parse several lines of the type: + # `#define ___SOME_DEF some_value` + # and extract `___SOME_DEF` + defines = {} + for line in output.split('\n'): + if not line: + continue + d, *rest = line.split(' ', 2) + if d != '#define': + continue + if len(rest) == 1: + defines[rest] = True + if len(rest) == 2: + defines[rest[0]] = rest[1] + return defines + + @staticmethod + def get_gnu_compiler_type(defines): + # Detect GCC type (Apple, MinGW, Cygwin, Unix) + if '__APPLE__' in defines: + return GCC_OSX + elif '__MINGW32__' in defines or '__MINGW64__' in defines: + return GCC_MINGW + # We ignore Cygwin for now, and treat it as a standard GCC + return GCC_STANDARD + def detect_c_compiler(self, want_cross): evar = 'CC' if self.is_cross_build() and want_cross: @@ -266,16 +368,13 @@ class Environment(): version = vmatch.group(0) else: version = 'unknown version' - if 'apple' in out and 'Free Software Foundation' in out: - return GnuCCompiler(ccache + [compiler], version, GCC_OSX, is_cross, exe_wrap) - if (out.startswith('cc') or 'gcc' in out.lower()) and \ - 'Free Software Foundation' in out: - lowerout = out.lower() - if 'mingw' in lowerout or 'msys' in lowerout or 'mingw' in compiler.lower(): - gtype = GCC_MINGW - else: - gtype = GCC_STANDARD - return GnuCCompiler(ccache + [compiler], version, gtype, is_cross, exe_wrap) + if 'Free Software Foundation' in out: + defines = self.get_gnu_compiler_defines([compiler]) + if not defines: + popen_exceptions[compiler] = 'no pre-processor defines' + continue + gtype = self.get_gnu_compiler_type(defines) + return GnuCCompiler(ccache + [compiler], version, gtype, is_cross, exe_wrap, defines) if 'clang' in out: if 'Apple' in out: cltype = CLANG_OSX @@ -331,13 +430,12 @@ class Environment(): version = vmatch.group(0) if 'GNU Fortran' in out: - if mesonlib.is_osx(): - gcctype = GCC_OSX - elif mesonlib.is_windows(): - gcctype = GCC_MINGW - else: - gcctype = GCC_STANDARD - return GnuFortranCompiler([compiler], version, gcctype, is_cross, exe_wrap) + defines = self.get_gnu_compiler_defines([compiler]) + if not defines: + popen_exceptions[compiler] = 'no pre-processor defines' + continue + gtype = self.get_gnu_compiler_type(defines) + return GnuFortranCompiler([compiler], version, gtype, is_cross, exe_wrap, defines) if 'G95' in out: return G95FortranCompiler([compiler], version, is_cross, exe_wrap) @@ -419,16 +517,13 @@ class Environment(): version = vmatch.group(0) else: version = 'unknown version' - if 'apple' in out and 'Free Software Foundation' in out: - return GnuCPPCompiler(ccache + [compiler], version, GCC_OSX, is_cross, exe_wrap) - if (out.startswith('c++ ') or 'g++' in out or 'GCC' in out) and \ - 'Free Software Foundation' in out: - lowerout = out.lower() - if 'mingw' in lowerout or 'msys' in lowerout or 'mingw' in compiler.lower(): - gtype = GCC_MINGW - else: - gtype = GCC_STANDARD - return GnuCPPCompiler(ccache + [compiler], version, gtype, is_cross, exe_wrap) + if 'Free Software Foundation' in out: + defines = self.get_gnu_compiler_defines([compiler]) + if not defines: + popen_exceptions[compiler] = 'no pre-processor defines' + continue + gtype = self.get_gnu_compiler_type(defines) + return GnuCPPCompiler(ccache + [compiler], version, gtype, is_cross, exe_wrap, defines) if 'clang' in out: if 'Apple' in out: cltype = CLANG_OSX @@ -469,13 +564,11 @@ class Environment(): version = vmatch.group(0) else: version = 'unknown version' - if (out.startswith('cc ') or 'gcc' in out) and \ - 'Free Software Foundation' in out: - return GnuObjCCompiler(exelist, version, is_cross, exe_wrap) + if 'Free Software Foundation' in out: + defines = self.get_gnu_compiler_defines(exelist) + return GnuObjCCompiler(exelist, version, is_cross, exe_wrap, defines) if out.startswith('Apple LLVM'): return ClangObjCCompiler(exelist, version, CLANG_OSX, is_cross, exe_wrap) - if 'apple' in out and 'Free Software Foundation' in out: - return GnuObjCCompiler(exelist, version, is_cross, exe_wrap) raise EnvironmentException('Unknown compiler "' + ' '.join(exelist) + '"') def detect_objcpp_compiler(self, want_cross): @@ -502,13 +595,11 @@ class Environment(): version = vmatch.group(0) else: version = 'unknown version' - if (out.startswith('c++ ') or out.startswith('g++')) and \ - 'Free Software Foundation' in out: - return GnuObjCPPCompiler(exelist, version, is_cross, exe_wrap) + if 'Free Software Foundation' in out: + defines = self.get_gnu_compiler_defines(exelist) + return GnuObjCPPCompiler(exelist, version, is_cross, exe_wrap, defines) if out.startswith('Apple LLVM'): return ClangObjCPPCompiler(exelist, version, CLANG_OSX, is_cross, exe_wrap) - if 'apple' in out and 'Free Software Foundation' in out: - return GnuObjCPPCompiler(exelist, version, is_cross, exe_wrap) raise EnvironmentException('Unknown compiler "' + ' '.join(exelist) + '"') def detect_java_compiler(self): @@ -846,10 +937,12 @@ class CrossBuildInfo(): return 'host_machine' in self.config def need_exe_wrapper(self): - if self.has_host() and detect_cpu_family() == 'x86_64' and \ + # Can almost always run 32-bit binaries on 64-bit natively if the host + # and build systems are the same. We don't pass any compilers to + # detect_cpu_family() here because we always want to know the OS + # architecture, not what the compiler environment tells us. + if self.has_host() and detect_cpu_family({}) == 'x86_64' and \ self.config['host_machine']['cpu_family'] == 'x86' and \ self.config['host_machine']['system'] == detect_system(): - # Can almost always run 32-bit binaries on 64-bit natively if the - # host and build systems are the same return False return True diff --git a/mesonbuild/interpreter.py b/mesonbuild/interpreter.py index 385e07a..cc85e77 100644 --- a/mesonbuild/interpreter.py +++ b/mesonbuild/interpreter.py @@ -375,7 +375,8 @@ class GeneratedListHolder(InterpreterObject): self.held_object.add_file(a) class BuildMachine(InterpreterObject): - def __init__(self): + def __init__(self, compilers): + self.compilers = compilers InterpreterObject.__init__(self) self.methods.update({'system' : self.system_method, 'cpu_family' : self.cpu_family_method, @@ -384,10 +385,10 @@ class BuildMachine(InterpreterObject): }) def cpu_family_method(self, args, kwargs): - return environment.detect_cpu_family() + return environment.detect_cpu_family(self.compilers) def cpu_method(self, args, kwargs): - return environment.detect_cpu() + return environment.detect_cpu(self.compilers) def system_method(self, args, kwargs): return environment.detect_system() @@ -1099,6 +1100,8 @@ class Interpreter(): def __init__(self, build, backend, subproject='', subdir='', subproject_dir='subprojects'): self.build = build + self.environment = build.environment + self.coredata = self.environment.get_coredata() self.backend = backend self.subproject = subproject self.subdir = subdir @@ -1126,7 +1129,8 @@ class Interpreter(): self.sanity_check_ast() self.variables = {} self.builtin = {} - self.builtin['build_machine'] = BuildMachine() + self.parse_project() + self.builtin['build_machine'] = BuildMachine(self.coredata.compilers) if not self.build.environment.is_cross_build(): self.builtin['host_machine'] = self.builtin['build_machine'] self.builtin['target_machine'] = self.builtin['build_machine'] @@ -1141,10 +1145,8 @@ class Interpreter(): else: self.builtin['target_machine'] = self.builtin['host_machine'] self.builtin['meson'] = MesonMain(build, self) - self.environment = build.environment self.build_func_dict() self.build_def_files = [os.path.join(self.subdir, environment.build_filename)] - self.coredata = self.environment.get_coredata() self.generators = [] self.visited_subdirs = {} self.global_args_frozen = False @@ -1196,6 +1198,15 @@ class Interpreter(): 'environment' : self.func_environment, } + def parse_project(self): + """ + Parses project() and initializes languages, compilers etc. Do this + early because we need this before we parse the rest of the AST. + """ + project = self.ast.lines[0] + args, kwargs = self.reduce_arguments(project.args) + self.func_project(project, args, kwargs) + def module_method_callback(self, invalues): unwrap_single = False if invalues is None: @@ -1244,6 +1255,10 @@ class Interpreter(): first = self.ast.lines[0] if not isinstance(first, mparser.FunctionNode) or first.func_name != 'project': raise InvalidCode('First statement must be a call to project') + args = self.reduce_arguments(first.args)[0] + if len(args) < 2: + raise InvalidArguments('Not enough arguments to project(). Needs at least the project name and one language') + def check_cross_stdlibs(self): if self.build.environment.is_cross_build(): @@ -1262,10 +1277,12 @@ class Interpreter(): pass def run(self): - self.evaluate_codeblock(self.ast) + # Evaluate everything after the first line, which is project() because + # we already parsed that in self.parse_project() + self.evaluate_codeblock(self.ast, start=1) mlog.log('Build targets in project:', mlog.bold(str(len(self.build.targets)))) - def evaluate_codeblock(self, node): + def evaluate_codeblock(self, node, start=0): if node is None: return if not isinstance(node, mparser.CodeBlockNode): @@ -1274,7 +1291,7 @@ class Interpreter(): e.colno = node.colno raise e statements = node.lines - i = 0 + i = start while i < len(statements): cur = statements[i] try: @@ -1595,9 +1612,6 @@ class Interpreter(): @stringArgs def func_project(self, node, args, kwargs): - if len(args) < 2: - raise InvalidArguments('Not enough arguments to project(). Needs at least the project name and one language') - if not self.is_subproject(): self.build.project_name = args[0] if self.environment.first_invocation and 'default_options' in kwargs: @@ -1619,7 +1633,7 @@ class Interpreter(): raise InterpreterException('Meson version is %s but project requires %s.' % (cv, pv)) self.build.projects[self.subproject] = args[0] mlog.log('Project name: ', mlog.bold(args[0]), sep='') - self.add_languages(node, args[1:], True) + self.add_languages(args[1:], True) langs = self.coredata.compilers.keys() if 'vala' in langs: if not 'c' in langs: @@ -1629,7 +1643,7 @@ class Interpreter(): @stringArgs def func_add_languages(self, node, args, kwargs): - return self.add_languages(node, args, kwargs.get('required', True)) + return self.add_languages(args, kwargs.get('required', True)) @noKwargs def func_message(self, node, args, kwargs): @@ -1649,8 +1663,6 @@ class Interpreter(): raise InvalidArguments('Function accepts only strings, integers, lists and lists thereof.') mlog.log(mlog.bold('Message:'), argstr) - return - @noKwargs def func_error(self, node, args, kwargs): @@ -1727,7 +1739,7 @@ class Interpreter(): self.coredata.compiler_options = new_options return (comp, cross_comp) - def add_languages(self, node, args, required): + def add_languages(self, args, required): success = True need_cross_compiler = self.environment.is_cross_build() and self.environment.cross_info.need_cross_compiler() for lang in args: diff --git a/mesonbuild/mesonlib.py b/mesonbuild/mesonlib.py index 7294a54..abb5641 100644 --- a/mesonbuild/mesonlib.py +++ b/mesonbuild/mesonlib.py @@ -91,9 +91,6 @@ def is_windows(): platname = platform.system().lower() return platname == 'windows' or 'mingw' in platname -def is_32bit(): - return not(sys.maxsize > 2**32) - def is_debianlike(): return os.path.isfile('/etc/debian_version') diff --git a/mesonbuild/modules/gnome.py b/mesonbuild/modules/gnome.py index ddff5aa..2a9b6a0 100644 --- a/mesonbuild/modules/gnome.py +++ b/mesonbuild/modules/gnome.py @@ -45,6 +45,9 @@ class GnomeModule: if not isinstance(source_dirs, list): source_dirs = [source_dirs] + # Always include current directory, but after paths set by user + source_dirs.append(os.path.join(state.environment.get_source_dir(), state.subdir)) + if len(args) < 2: raise MesonException('Not enough arguments; The name of the resource and the path to the XML file are required') @@ -220,6 +223,7 @@ class GnomeModule: extra_args = mesonlib.stringlistify(kwargs.pop('extra_args', [])) scan_command += extra_args + scan_command += ['-I' + os.path.join(state.environment.get_source_dir(), state.subdir)] scan_command += self.get_include_args(state, girtarget.get_include_dirs()) if 'link_with' in kwargs: diff --git a/test cases/common/117 custom target capture/meson.build b/test cases/common/117 custom target capture/meson.build index 6c19752..fa59d51 100644 --- a/test cases/common/117 custom target capture/meson.build +++ b/test cases/common/117 custom target capture/meson.build @@ -7,10 +7,10 @@ python = find_program('python3') comp = '@0@/@1@'.format(meson.current_source_dir(), 'my_compiler.py') mytarget = custom_target('bindat', -output : 'data.dat', -input : 'data_source.txt', -capture : true, -command : [python, comp, '@INPUT@'], -install : true, -install_dir : 'subdir' + output : 'data.dat', + input : 'data_source.txt', + capture : true, + command : [python, comp, '@INPUT@'], + install : true, + install_dir : 'subdir' ) diff --git a/test cases/common/95 dep fallback/gensrc.py b/test cases/common/95 dep fallback/gensrc.py new file mode 100644 index 0000000..da503e2 --- /dev/null +++ b/test cases/common/95 dep fallback/gensrc.py @@ -0,0 +1,6 @@ +#!/usr/bin/env python + +import sys +import shutil + +shutil.copyfile(sys.argv[1], sys.argv[2]) diff --git a/test cases/common/95 dep fallback/meson.build b/test cases/common/95 dep fallback/meson.build index 4cf0577..13541f2 100644 --- a/test cases/common/95 dep fallback/meson.build +++ b/test cases/common/95 dep fallback/meson.build @@ -6,5 +6,14 @@ if not bob.found() endif jimmy = dependency('jimmylib', fallback : ['jimmylib', 'jimmy_dep'], required: false) -exe = executable('bobtester', 'tester.c', dependencies : bob) +gensrc_py = find_program('gensrc.py') +gensrc = custom_target('gensrc.c', + input : 'tester.c', + output : 'gensrc.c', + command : [gensrc_py, '@INPUT@', '@OUTPUT@']) + +exe = executable('bobtester', + [gensrc], + dependencies : bob) + test('bobtester', exe) diff --git a/test cases/common/95 dep fallback/subprojects/boblib/bob.c b/test cases/common/95 dep fallback/subprojects/boblib/bob.c index b483a55..ae0f394 100644 --- a/test cases/common/95 dep fallback/subprojects/boblib/bob.c +++ b/test cases/common/95 dep fallback/subprojects/boblib/bob.c @@ -1,5 +1,8 @@ #include"bob.h" +#ifdef _MSC_VER +__declspec(dllexport) +#endif const char* get_bob() { return "bob"; } diff --git a/test cases/common/95 dep fallback/subprojects/boblib/bob.h b/test cases/common/95 dep fallback/subprojects/boblib/bob.h index bc170ef..f874ae7 100644 --- a/test cases/common/95 dep fallback/subprojects/boblib/bob.h +++ b/test cases/common/95 dep fallback/subprojects/boblib/bob.h @@ -1,3 +1,6 @@ #pragma once +#ifdef _MSC_VER +__declspec(dllimport) +#endif const char* get_bob(); diff --git a/test cases/common/95 dep fallback/subprojects/boblib/genbob.py b/test cases/common/95 dep fallback/subprojects/boblib/genbob.py new file mode 100644 index 0000000..824194b --- /dev/null +++ b/test cases/common/95 dep fallback/subprojects/boblib/genbob.py @@ -0,0 +1,7 @@ +#!/usr/bin/env python + +import os +import sys + +with open(sys.argv[1], 'w') as f: + f.write('') diff --git a/test cases/common/95 dep fallback/subprojects/boblib/meson.build b/test cases/common/95 dep fallback/subprojects/boblib/meson.build index 8dc1f3a..bb250e4 100644 --- a/test cases/common/95 dep fallback/subprojects/boblib/meson.build +++ b/test cases/common/95 dep fallback/subprojects/boblib/meson.build @@ -1,7 +1,16 @@ project('bob', 'c') -boblib = static_library('bob', 'bob.c') +gensrc_py = find_program('genbob.py') +genbob_h = custom_target('genbob.h', + output : 'genbob.h', + command : [gensrc_py, '@OUTPUT@']) +genbob_c = custom_target('genbob.c', + output : 'genbob.c', + command : [gensrc_py, '@OUTPUT@']) + +boblib = library('bob', ['bob.c', genbob_c]) bobinc = include_directories('.') bob_dep = declare_dependency(link_with : boblib, + sources : [genbob_h], include_directories : bobinc) diff --git a/test cases/common/95 dep fallback/tester.c b/test cases/common/95 dep fallback/tester.c index 59e1635..e6651d9 100644 --- a/test cases/common/95 dep fallback/tester.c +++ b/test cases/common/95 dep fallback/tester.c @@ -1,4 +1,5 @@ #include"bob.h" +#include"genbob.h" #include<string.h> #include<stdio.h> diff --git a/test cases/frameworks/7 gnome/schemas/meson.build b/test cases/frameworks/7 gnome/schemas/meson.build index b4765b6..1947604 100644 --- a/test cases/frameworks/7 gnome/schemas/meson.build +++ b/test cases/frameworks/7 gnome/schemas/meson.build @@ -1,8 +1,8 @@ -gnome.compile_schemas() +compiled = gnome.compile_schemas() install_data('com.github.meson.gschema.xml', install_dir : 'share/glib-2.0/schemas') -schemaexe = executable('schemaprog', 'schemaprog.c', +schemaexe = executable('schemaprog', 'schemaprog.c', compiled, dependencies : gio) test('schema test', schemaexe) diff --git a/test cases/swift/2 multifile/main.swift b/test cases/swift/2 multifile/main.swift index 61d67e2..8f39ba6 100644 --- a/test cases/swift/2 multifile/main.swift +++ b/test cases/swift/2 multifile/main.swift @@ -1 +1 @@ -printSomething("String from main") +printSomething(text:"String from main") diff --git a/test cases/swift/4 generate/gen/main.swift b/test cases/swift/4 generate/gen/main.swift index 27398fd..03acdbb 100644 --- a/test cases/swift/4 generate/gen/main.swift +++ b/test cases/swift/4 generate/gen/main.swift @@ -4,7 +4,11 @@ import Glibc #endif +#if swift(>=3.0) +let fname = CommandLine.arguments[1] +#else let fname = Process.arguments[1] +#endif let code = "public func getGenerated() -> Int {\n return 42\n}\n" let f = fopen(fname, "w") diff --git a/test cases/vala/8 generated source/installed_files.txt b/test cases/vala/8 generated source/installed_files.txt new file mode 100644 index 0000000..a4c37f6 --- /dev/null +++ b/test cases/vala/8 generated source/installed_files.txt @@ -0,0 +1 @@ +usr/bin/generatedtest diff --git a/test cases/vala/8 generated source/meson.build b/test cases/vala/8 generated source/meson.build new file mode 100644 index 0000000..7271821 --- /dev/null +++ b/test cases/vala/8 generated source/meson.build @@ -0,0 +1,7 @@ +project('mytest', 'vala', 'c') + +cd = configuration_data() +cd.set('x', 'y') + +subdir('src') +subdir('tools') diff --git a/test cases/vala/8 generated source/src/config.vala.in b/test cases/vala/8 generated source/src/config.vala.in new file mode 100644 index 0000000..a5196fd --- /dev/null +++ b/test cases/vala/8 generated source/src/config.vala.in @@ -0,0 +1,3 @@ +namespace Config { + public static const string x = "@x@"; +} diff --git a/test cases/vala/8 generated source/src/meson.build b/test cases/vala/8 generated source/src/meson.build new file mode 100644 index 0000000..9096c67 --- /dev/null +++ b/test cases/vala/8 generated source/src/meson.build @@ -0,0 +1,5 @@ +config = configure_file(input: 'config.vala.in', + output: 'config.vala', + configuration: cd) + +src = files('test.vala') diff --git a/test cases/vala/8 generated source/src/test.vala b/test cases/vala/8 generated source/src/test.vala new file mode 100644 index 0000000..98d6821 --- /dev/null +++ b/test cases/vala/8 generated source/src/test.vala @@ -0,0 +1,3 @@ +void main() { + print (Config.x); +} diff --git a/test cases/vala/8 generated source/tools/meson.build b/test cases/vala/8 generated source/tools/meson.build new file mode 100644 index 0000000..834ec1a --- /dev/null +++ b/test cases/vala/8 generated source/tools/meson.build @@ -0,0 +1,3 @@ +executable('generatedtest', [src, config], + install : true, + dependencies: [dependency('glib-2.0'), dependency('gobject-2.0')]) |