def concretize_architecture()

in lib/ramble/spack/concretize.py [0:0]


    def concretize_architecture(self, spec):
        """If the spec is empty provide the defaults of the platform. If the
        architecture is not a string type, then check if either the platform,
        target or operating system are concretized. If any of the fields are
        changed then return True. If everything is concretized (i.e the
        architecture attribute is a namedtuple of classes) then return False.
        If the target is a string type, then convert the string into a
        concretized architecture. If it has no architecture and the root of the
        DAG has an architecture, then use the root otherwise use the defaults
        on the platform.
        """
        # ensure type safety for the architecture
        if spec.architecture is None:
            spec.architecture = spack.spec.ArchSpec()

        if spec.architecture.concrete:
            return False

        # Get platform of nearest spec with a platform, including spec
        # If spec has a platform, easy
        if spec.architecture.platform:
            new_plat = spack.platforms.by_name(spec.architecture.platform)
        else:
            # Else if anyone else has a platform, take the closest one
            # Search up, then down, along build/link deps first
            # Then any nearest. Algorithm from compilerspec search
            platform_spec = find_spec(
                spec, lambda x: x.architecture and x.architecture.platform
            )
            if platform_spec:
                new_plat = spack.platforms.by_name(platform_spec.architecture.platform)
            else:
                # If no platform anywhere in this spec, grab the default
                new_plat = spack.platforms.host()

        # Get nearest spec with relevant platform and an os
        # Generally, same algorithm as finding platform, except we only
        # consider specs that have a platform
        if spec.architecture.os:
            new_os = spec.architecture.os
        else:
            new_os_spec = find_spec(
                spec, lambda x: (x.architecture and
                                 x.architecture.platform == str(new_plat) and
                                 x.architecture.os)
            )
            if new_os_spec:
                new_os = new_os_spec.architecture.os
            else:
                new_os = new_plat.operating_system('default_os')

        # Get the nearest spec with relevant platform and a target
        # Generally, same algorithm as finding os
        curr_target = None
        if spec.architecture.target:
            curr_target = spec.architecture.target
        if spec.architecture.target and spec.architecture.target_concrete:
            new_target = spec.architecture.target
        else:
            new_target_spec = find_spec(
                spec, lambda x: (x.architecture and
                                 x.architecture.platform == str(new_plat) and
                                 x.architecture.target and
                                 x.architecture.target != curr_target)
            )
            if new_target_spec:
                if curr_target:
                    # constrain one target by the other
                    new_target_arch = spack.spec.ArchSpec(
                        (None, None, new_target_spec.architecture.target))
                    curr_target_arch = spack.spec.ArchSpec(
                        (None, None, curr_target))
                    curr_target_arch.constrain(new_target_arch)
                    new_target = curr_target_arch.target
                else:
                    new_target = new_target_spec.architecture.target
            else:
                # To get default platform, consider package prefs
                if PackagePrefs.has_preferred_targets(spec.name):
                    new_target = self.target_from_package_preferences(spec)
                else:
                    new_target = new_plat.target('default_target')
                if curr_target:
                    # convert to ArchSpec to compare satisfaction
                    new_target_arch = spack.spec.ArchSpec(
                        (None, None, str(new_target)))
                    curr_target_arch = spack.spec.ArchSpec(
                        (None, None, str(curr_target)))

                    if not new_target_arch.satisfies(curr_target_arch):
                        # new_target is an incorrect guess based on preferences
                        # and/or default
                        valid_target_ranges = str(curr_target).split(',')
                        for target_range in valid_target_ranges:
                            t_min, t_sep, t_max = target_range.partition(':')
                            if not t_sep:
                                new_target = t_min
                                break
                            elif t_max:
                                new_target = t_max
                                break
                            elif t_min:
                                # TODO: something better than picking first
                                new_target = t_min
                                break

        # Construct new architecture, compute whether spec changed
        arch_spec = (str(new_plat), str(new_os), str(new_target))
        new_arch = spack.spec.ArchSpec(arch_spec)
        spec_changed = new_arch != spec.architecture
        spec.architecture = new_arch
        return spec_changed