Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2071132e authored by Dan Willemsen's avatar Dan Willemsen
Browse files

Fix long mac test times; only initialize host settings once

It looks like sometime in late February our Mac builds started taking
~10 minutes longer than before. On my local workstation the Soong tests
were taking >25 minutes before completing (likely because I don't have
the older SDKs installed, and we iterate from older to newer to find the
oldest installed SDK).

Most of this time was spend running the `xcrun` tools to interrogate the
system about which Mac SDKs are installed and where the tools are. This
will never change during any build or test, so wrap it all in a
sync.Once so that we only ever call them once.

And remove the macSdkPath variable, which has been unused for years and
no longer works (as we don't allow the use of xcode-select during the
build).

Bug: 153010389
Test: prebuilts/build-tools/build-prebuilts.sh on a Mac
Change-Id: I39b2d49739e628e4c11bec4805b25039115d2fd0
Merged-In: I39b2d49739e628e4c11bec4805b25039115d2fd0
(cherry picked from commit 6ba5367a)
parent 66e36556
Loading
Loading
Loading
Loading
+59 −37
Original line number Diff line number Diff line
@@ -15,9 +15,11 @@
package config

import (
	"fmt"
	"os/exec"
	"path/filepath"
	"strings"
	"sync"

	"android/soong/android"
)
@@ -89,28 +91,20 @@ const (
)

func init() {
	pctx.VariableFunc("macSdkPath", func(ctx android.PackageVarContext) string {
		xcodeselect := ctx.Config().HostSystemTool("xcode-select")
		bytes, err := exec.Command(xcodeselect, "--print-path").Output()
		if err != nil {
			ctx.Errorf("xcode-select failed with: %q", err.Error())
		}
		return strings.TrimSpace(string(bytes))
	})
	pctx.VariableFunc("macSdkRoot", func(ctx android.PackageVarContext) string {
		return xcrunSdk(ctx, "--show-sdk-path")
		return getMacTools(ctx).sdkRoot
	})
	pctx.StaticVariable("macMinVersion", "10.10")
	pctx.VariableFunc("MacArPath", func(ctx android.PackageVarContext) string {
		return xcrun(ctx, "--find", "ar")
		return getMacTools(ctx).arPath
	})

	pctx.VariableFunc("MacStripPath", func(ctx android.PackageVarContext) string {
		return xcrun(ctx, "--find", "strip")
		return getMacTools(ctx).stripPath
	})

	pctx.VariableFunc("MacToolPath", func(ctx android.PackageVarContext) string {
		return filepath.Dir(xcrun(ctx, "--find", "ld"))
		return getMacTools(ctx).toolPath
	})

	pctx.StaticVariable("DarwinGccVersion", darwinGccVersion)
@@ -126,40 +120,68 @@ func init() {
	pctx.StaticVariable("DarwinYasmFlags", "-f macho -m amd64")
}

func xcrun(ctx android.PackageVarContext, args ...string) string {
	xcrun := ctx.Config().HostSystemTool("xcrun")
	bytes, err := exec.Command(xcrun, args...).Output()
type macPlatformTools struct {
	once sync.Once
	err  error

	sdkRoot   string
	arPath    string
	stripPath string
	toolPath  string
}

var macTools = &macPlatformTools{}

func getMacTools(ctx android.PackageVarContext) *macPlatformTools {
	macTools.once.Do(func() {
		xcrunTool := ctx.Config().HostSystemTool("xcrun")

		xcrun := func(args ...string) string {
			if macTools.err != nil {
				return ""
			}

			bytes, err := exec.Command(xcrunTool, args...).Output()
			if err != nil {
		ctx.Errorf("xcrun failed with: %q", err.Error())
				macTools.err = fmt.Errorf("xcrun %q failed with: %q", args, err)
				return ""
			}

			return strings.TrimSpace(string(bytes))
		}

func xcrunSdk(ctx android.PackageVarContext, arg string) string {
	xcrun := ctx.Config().HostSystemTool("xcrun")
		xcrunSdk := func(arg string) string {
			if selected := ctx.Config().Getenv("MAC_SDK_VERSION"); selected != "" {
				if !inList(selected, darwinSupportedSdkVersions) {
			ctx.Errorf("MAC_SDK_VERSION %s isn't supported: %q", selected, darwinSupportedSdkVersions)
					macTools.err = fmt.Errorf("MAC_SDK_VERSION %s isn't supported: %q", selected, darwinSupportedSdkVersions)
					return ""
				}

		bytes, err := exec.Command(xcrun, "--sdk", "macosx"+selected, arg).Output()
		if err != nil {
			ctx.Errorf("MAC_SDK_VERSION %s is not installed", selected)
		}
		return strings.TrimSpace(string(bytes))
				return xcrun("--sdk", "macosx"+selected, arg)
			}

			for _, sdk := range darwinSupportedSdkVersions {
		bytes, err := exec.Command(xcrun, "--sdk", "macosx"+sdk, arg).Output()
				bytes, err := exec.Command(xcrunTool, "--sdk", "macosx"+sdk, arg).Output()
				if err == nil {
					return strings.TrimSpace(string(bytes))
				}
			}
	ctx.Errorf("Could not find a supported mac sdk: %q", darwinSupportedSdkVersions)
			macTools.err = fmt.Errorf("Could not find a supported mac sdk: %q", darwinSupportedSdkVersions)
			return ""
		}

		macTools.sdkRoot = xcrunSdk("--show-sdk-path")

		macTools.arPath = xcrun("--find", "ar")
		macTools.stripPath = xcrun("--find", "strip")
		macTools.toolPath = filepath.Dir(xcrun("--find", "ld"))
	})
	if macTools.err != nil {
		ctx.Errorf("%q", macTools.err)
	}
	return macTools
}

type toolchainDarwin struct {
	cFlags, ldFlags string
	toolchain64Bit