Skip to content

Commit

Permalink
Further fix subprojects logic in cross build scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
oleavr committed Mar 13, 2024
1 parent 1abc8a0 commit 4c1701c
Show file tree
Hide file tree
Showing 14 changed files with 172 additions and 56 deletions.
20 changes: 17 additions & 3 deletions mesonbuild/backend/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,10 +330,11 @@ def get_source_dir_include_args(self, target: build.BuildTarget, compiler: 'Comp
return compiler.get_include_args(tmppath, False)

def get_build_dir_include_args(self, target: build.BuildTarget, compiler: 'Compiler', *, absolute_path: bool = False) -> T.List[str]:
subdir = self.compute_build_subdir(target.get_subdir(), target.is_native_cross())
if absolute_path:
curdir = os.path.join(self.build_dir, target.get_subdir())
curdir = os.path.join(self.build_dir, subdir)
else:
curdir = target.get_subdir()
curdir = subdir
if curdir == '':
curdir = '.'
return compiler.get_include_args(curdir, False)
Expand Down Expand Up @@ -368,9 +369,11 @@ def get_target_dir(self, target: T.Union[build.Target, build.CustomTargetIndex])
# this produces no output, only a dummy top-level name
dirname = ''
elif self.environment.coredata.get_option(OptionKey('layout')) == 'mirror':
dirname = target.get_subdir()
dirname = self.compute_build_subdir(target.get_subdir(), target.is_native_cross())
else:
dirname = 'meson-out'
if target.is_native_cross():
dirname += '-native'
return dirname

def get_target_dir_relative_to(self, t: build.Target, o: build.Target) -> str:
Expand Down Expand Up @@ -409,6 +412,10 @@ def get_target_generated_dir(
# target that the GeneratedList is used in
return os.path.join(self.get_target_private_dir(target), src)

@classmethod
def compute_build_subdir(cls, subdir: str, is_native_cross: bool) -> str:
return compute_build_subdir(subdir, is_native_cross)

def get_unity_source_file(self, target: T.Union[build.BuildTarget, build.CustomTarget, build.CustomTargetIndex],
suffix: str, number: int) -> mesonlib.File:
# There is a potential conflict here, but it is unlikely that
Expand Down Expand Up @@ -2043,3 +2050,10 @@ def compile_target_to_generator(self, target: build.CompileTarget) -> build.Gene
all_sources = T.cast('_ALL_SOURCES_TYPE', target.sources) + T.cast('_ALL_SOURCES_TYPE', target.generated)
return self.compiler_to_generator(target, target.compiler, all_sources,
target.output_templ, target.depends)


def compute_build_subdir(subdir: str, is_native_cross: bool) -> str:
if is_native_cross:
assert subdir.startswith('subprojects')
return 'subprojects-native' + subdir[11:]
return subdir
9 changes: 5 additions & 4 deletions mesonbuild/backend/ninjabackend.py
Original file line number Diff line number Diff line change
Expand Up @@ -2778,7 +2778,7 @@ def generate_llvm_ir_compile(self, target, src):
return (rel_obj, rel_src)

@lru_cache(maxsize=None)
def generate_inc_dir(self, compiler: 'Compiler', d: str, basedir: str, is_system: bool) -> \
def generate_inc_dir(self, compiler: 'Compiler', d: str, basedir: str, is_system: bool, is_native_cross: bool) -> \
T.Tuple['ImmutableListProtocol[str]', 'ImmutableListProtocol[str]']:
# Avoid superfluous '/.' at the end of paths when d is '.'
if d not in ('', '.'):
Expand All @@ -2793,8 +2793,9 @@ def generate_inc_dir(self, compiler: 'Compiler', d: str, basedir: str, is_system
# inc = include_directories('foo/bar/baz')
#
# But never subdir()s into the actual dir.
if os.path.isdir(os.path.join(self.environment.get_build_dir(), expdir)):
bargs = compiler.get_include_args(expdir, is_system)
subdir = self.compute_build_subdir(expdir, is_native_cross)
if os.path.isdir(os.path.join(self.environment.get_build_dir(), subdir)):
bargs = compiler.get_include_args(subdir, is_system)
else:
bargs = []
return (sargs, bargs)
Expand Down Expand Up @@ -2843,7 +2844,7 @@ def _generate_single_compile_target_args(self, target: build.BuildTarget, compil
# flags will be added in reversed order.
for d in reversed(i.get_incdirs()):
# Add source subdir first so that the build subdir overrides it
(compile_obj, includeargs) = self.generate_inc_dir(compiler, d, basedir, i.is_system)
(compile_obj, includeargs) = self.generate_inc_dir(compiler, d, basedir, i.is_system, i.is_native_cross)
commands += compile_obj
commands += includeargs
for d in i.get_extra_build_dirs():
Expand Down
32 changes: 27 additions & 5 deletions mesonbuild/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,18 @@ def copy(self) -> Build:
def copy_to_native(self) -> Build:
other = Build(self.environment.copy_to_native())
self._copy_to(other)

other.tests = []
other.benchmarks = []
other.test_setups = {}
other.test_setup_default_name = None
other.find_overrides = {}
other.searched_programs = set()

other.dependency_overrides = PerMachineDefaultable.default(False, self.dependency_overrides.build, {})
other.devenv = []
other.modules = []

return other

def _copy_to(self, other: Build) -> None:
Expand All @@ -309,9 +321,13 @@ def _copy_to(self, other: Build) -> None:
other.__dict__[k] = v

def merge(self, other: Build) -> None:
is_native_cross = other.environment.coredata.is_native_cross()

for k, v in other.__dict__.items():
if k == 'environment':
continue
if is_native_cross and k == 'dependency_overrides':
continue
self.__dict__[k] = v

def ensure_static_linker(self, compiler: Compiler) -> None:
Expand Down Expand Up @@ -391,6 +407,7 @@ class IncludeDirs(HoldableObject):
curdir: str
incdirs: T.List[str]
is_system: bool
is_native_cross: bool
# Interpreter has validated that all given directories
# actually exist.
extra_build_dirs: T.List[str] = field(default_factory=list)
Expand Down Expand Up @@ -616,7 +633,7 @@ def get_basename(self) -> str:
return self.name

def get_subdir(self) -> str:
return self.environment.build_output_rpath(self.subdir)
return self.subdir

def get_typename(self) -> str:
return self.typename
Expand Down Expand Up @@ -655,7 +672,10 @@ def get_id(self) -> str:
if getattr(self, 'name_suffix_set', False):
name += '.' + self.suffix
return self.construct_id_from_path(
self.subdir, name, self.type_suffix(), '@native' if self.environment.coredata.is_native_clone else '')
self.subdir, name, self.type_suffix(), '@native' if self.is_native_cross() else '')

def is_native_cross(self) -> bool:
return self.environment.coredata.is_native_cross()

def process_kwargs_base(self, kwargs: T.Dict[str, T.Any]) -> None:
if 'build_by_default' in kwargs:
Expand Down Expand Up @@ -1501,8 +1521,6 @@ def check_can_link_together(self, t: BuildTargetTypes) -> None:
links_with_rust_abi = isinstance(t, BuildTarget) and t.uses_rust_abi()
if not self.uses_rust() and links_with_rust_abi:
raise InvalidArguments(f'Try to link Rust ABI library {t.name!r} with a non-Rust target {self.name!r}')
if isinstance(t, Target) and t.subproject and not self.environment.is_cross_build():
return
if self.for_machine is not t.for_machine and (not links_with_rust_abi or t.rust_crate_type != 'proc-macro'):
msg = f'Tried to mix libraries for machines {self.for_machine} and {t.for_machine} in target {self.name!r}'
if self.environment.is_cross_build():
Expand Down Expand Up @@ -1551,7 +1569,8 @@ def add_include_dirs(self, args: T.Sequence['IncludeDirs'], set_is_system: T.Opt
set_is_system = 'preserve'
if set_is_system != 'preserve':
is_system = set_is_system == 'system'
ids = [IncludeDirs(x.get_curdir(), x.get_incdirs(), is_system, x.get_extra_build_dirs()) for x in ids]
ids = [IncludeDirs(x.get_curdir(), x.get_incdirs(), is_system,
self.is_native_cross(), x.get_extra_build_dirs()) for x in ids]
self.include_dirs += ids

def get_aliases(self) -> T.List[T.Tuple[str, str, str]]:
Expand Down Expand Up @@ -2982,6 +3001,9 @@ def get_filename(self) -> str:
def get_id(self) -> str:
return self.target.get_id()

def is_native_cross(self) -> bool:
return self.target.is_native_cross()

def get_all_link_deps(self):
return self.target.get_all_link_deps()

Expand Down
12 changes: 8 additions & 4 deletions mesonbuild/coredata.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ def __init__(self, options: argparse.Namespace, scratch_dir: str, meson_command:
self.builtin_options_libdir_cross_fixup()
self.init_builtins('')

self.is_native_clone = False
self._is_native_clone = False

def copy_to_native(self) -> CoreData:
other = CoreData.__new__(CoreData)
Expand All @@ -597,13 +597,14 @@ def copy_to_native(self) -> CoreData:

other.compilers = PerMachine(OrderedDict(), OrderedDict())
other.compilers.build = self.compilers.build
other.compilers.host = self.compilers.build

other.deps = PerMachineDefaultable.default(
is_cross=False,
build=self.deps.build,
host=self.deps.host)

other.is_native_clone = True
other._is_native_clone = True

return other

Expand Down Expand Up @@ -928,6 +929,9 @@ def is_cross_build(self, when_building_for: MachineChoice = MachineChoice.HOST)
return False
return len(self.cross_files) > 0

def is_native_cross(self) -> bool:
return self._is_native_clone

def copy_build_options_from_regular_ones(self) -> bool:
dirty = False
assert not self.is_cross_build()
Expand All @@ -948,7 +952,7 @@ def copy_build_options_from_regular_ones(self) -> bool:
def set_options(self, options: T.Dict[OptionKey, T.Any], subproject: str = '', first_invocation: bool = False) -> bool:
dirty = False
if not self.is_cross_build():
other_machine = MachineChoice.HOST if self.is_native_clone else MachineChoice.BUILD
other_machine = MachineChoice.HOST if self.is_native_cross() else MachineChoice.BUILD
options = {k: v for k, v in options.items() if k.machine is not other_machine}

# Set prefix first because it's needed to sanitize other options
Expand All @@ -973,7 +977,7 @@ def set_options(self, options: T.Dict[OptionKey, T.Any], subproject: str = '', f
sub = f'In subproject {subproject}: ' if subproject else ''
raise MesonException(f'{sub}Unknown options: "{unknown_options_str}"')

if not self.is_cross_build() and not self.is_native_clone:
if not self.is_cross_build() and not self.is_native_cross():
dirty |= self.copy_build_options_from_regular_ones()

return dirty
Expand Down
8 changes: 5 additions & 3 deletions mesonbuild/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -869,9 +869,11 @@ def get_build_dir(self) -> str:
return self.build_dir

def build_output_rpath(self, subdir: str, *parts: T.Sequence[str]) -> str:
result = subdir
if self.coredata.is_native_clone:
result += '-native'
if self.coredata.is_native_cross():
assert subdir.startswith('subprojects')
result = 'subprojects-native' + subdir[11:]
else:
result = subdir
return os.path.join(result, *parts)

def get_import_lib_dir(self) -> str:
Expand Down
3 changes: 2 additions & 1 deletion mesonbuild/interpreter/dependencyfallbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ def _do_dependency(self, kwargs: TYPE_nkwargs, func_args: TYPE_nvar, func_kwargs
def _do_existing_subproject(self, kwargs: TYPE_nkwargs, func_args: TYPE_nvar, func_kwargs: TYPE_nkwargs) -> T.Optional[Dependency]:
subp_name = func_args[0]
varname = self.subproject_varname
if subp_name and self._get_subproject(subp_name, kwargs.get('native', False)):
native = kwargs.get('native', False)
if subp_name and self._get_subproject(subp_name, native):
return self._get_subproject_dep(subp_name, varname, kwargs)
return None

Expand Down
Loading

0 comments on commit 4c1701c

Please sign in to comment.