aboutsummaryrefslogtreecommitdiff
path: root/compiler_wrapper/sysroot_flag.go
blob: 0256abcd5ed4efc08cae4dcb4bcee4fddb1f5e52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright 2019 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package main

import (
	"path/filepath"
	"strings"
)

const skipSysrootAutodetectionFlag = "--cros-skip-wrapper-sysroot-autodetection"

func processSysrootFlag(builder *commandBuilder) {
	hadSkipSysrootMagicFlag := false
	fromUser := false
	userSysroot := ""
	builder.transformArgs(func(arg builderArg) string {
		switch {
		// In rare cases (e.g., glibc), we want all sysroot autodetection logic to be
		// disabled. This flag can be passed to disable that.
		case arg.value == skipSysrootAutodetectionFlag:
			hadSkipSysrootMagicFlag = true
			return ""

		case arg.fromUser && strings.HasPrefix(arg.value, "--sysroot="):
			fromUser = true
			sysrootArg := strings.Split(arg.value, "=")
			if len(sysrootArg) == 2 {
				userSysroot = sysrootArg[1]
			}
			return arg.value

		default:
			return arg.value
		}
	})

	if hadSkipSysrootMagicFlag {
		return
	}

	sysroot, syrootPresent := builder.env.getenv("SYSROOT")
	if syrootPresent {
		builder.updateEnv("SYSROOT=")
	}
	if sysroot == "" {
		// Use the bundled sysroot by default.
		sysroot = filepath.Join(builder.rootPath, "usr", builder.target.target)
	}
	if !fromUser {
		builder.addPreUserArgs("--sysroot=" + sysroot)
	} else {
		sysroot = userSysroot
	}

	libdir := "-L" + sysroot + "/usr/lib"
	if strings.Contains(builder.target.target, "64") {
		libdir += "64"
	}
	builder.addPostUserArgs(libdir)
}