xref: /aosp_15_r20/external/toolchain-utils/compiler_wrapper/sysroot_flag_test.go (revision 760c253c1ed00ce9abd48f8546f08516e57485fe)
1// Copyright 2019 The ChromiumOS Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5package main
6
7import (
8	"path"
9	"regexp"
10	"testing"
11)
12
13func TestOmitSysrootGivenSysrootSuppressionFlag(t *testing.T) {
14	escapedAutodetectionFlag := regexp.QuoteMeta(skipSysrootAutodetectionFlag)
15	withTestContext(t, func(ctx *testContext) {
16		runWithCompiler := func(compiler string) {
17			cmd := ctx.must(callCompiler(ctx, ctx.cfg,
18				ctx.newCommand(compiler, skipSysrootAutodetectionFlag, mainCc)))
19			if err := verifyArgOrder(cmd, mainCc); err != nil {
20				t.Error(err)
21			}
22			if err := verifyArgCount(cmd, 0, "--sysroot.*"); err != nil {
23				t.Error(err)
24			}
25			if err := verifyArgCount(cmd, 0, "-L.*"); err != nil {
26				t.Error(err)
27			}
28			if err := verifyArgCount(cmd, 0, escapedAutodetectionFlag); err != nil {
29				t.Error(err)
30			}
31		}
32
33		runWithCompiler(gccX86_64)
34		runWithCompiler(clangX86_64)
35	})
36}
37
38func TestOmitSysrootGivenUserDefinedSysroot(t *testing.T) {
39	withTestContext(t, func(ctx *testContext) {
40		runWithCompiler := func(compiler string) {
41			cmd := ctx.must(callCompiler(ctx, ctx.cfg,
42				ctx.newCommand(compiler, "--sysroot=/somepath", mainCc)))
43			if err := verifyArgOrder(cmd, "--sysroot=/somepath", mainCc); err != nil {
44				t.Error(err)
45			}
46			if err := verifyArgCount(cmd, 1, "--sysroot.*"); err != nil {
47				t.Error(err)
48			}
49		}
50
51		runWithCompiler(gccX86_64)
52		runWithCompiler(clangX86_64)
53	})
54}
55
56func TestSetSysrootFlagFromEnv(t *testing.T) {
57	withTestContext(t, func(ctx *testContext) {
58		ctx.env = []string{"SYSROOT=/envpath"}
59		cmd := ctx.must(callCompiler(ctx, ctx.cfg,
60			ctx.newCommand(gccX86_64, mainCc)))
61		if err := verifyEnvUpdate(cmd, "SYSROOT="); err != nil {
62			t.Error(err)
63		}
64		if err := verifyArgOrder(cmd, "--sysroot=/envpath", mainCc); err != nil {
65			t.Error(err)
66		}
67	})
68}
69
70func TestClearEmptySysrootFlagInEnv(t *testing.T) {
71	withTestContext(t, func(ctx *testContext) {
72		ctx.env = []string{"SYSROOT="}
73		cmd := ctx.must(callCompiler(ctx, ctx.cfg,
74			ctx.newCommand(gccX86_64, mainCc)))
75		if err := verifyEnvUpdate(cmd, "SYSROOT="); err != nil {
76			t.Error(err)
77		}
78		if err := verifyArgOrder(cmd, "--sysroot=.*/x86_64-cros-linux-gnu", mainCc); err != nil {
79			t.Error(err)
80		}
81	})
82}
83
84func TestSetSysrootRelativeToWrapperPath(t *testing.T) {
85	withTestContext(t, func(ctx *testContext) {
86		ctx.cfg.gccRootRelPath = "somepath"
87		cmd := ctx.must(callCompiler(ctx, ctx.cfg,
88			ctx.newCommand(gccX86_64, mainCc)))
89		if err := verifyArgOrder(cmd,
90			"--sysroot="+ctx.tempDir+"/somepath/usr/x86_64-cros-linux-gnu", mainCc); err != nil {
91			t.Error(err)
92		}
93	})
94}
95
96func TestSetSysrootRelativeToSymlinkedWrapperPath(t *testing.T) {
97	withTestContext(t, func(ctx *testContext) {
98		ctx.cfg.gccRootRelPath = "somepath"
99		linkedWrapperPath := path.Join(ctx.tempDir, "a/linked/path/x86_64-cros-linux-gnu-gcc")
100		ctx.symlink(path.Join(ctx.tempDir, gccX86_64), linkedWrapperPath)
101
102		cmd := ctx.must(callCompiler(ctx, ctx.cfg,
103			ctx.newCommand(linkedWrapperPath, mainCc)))
104		if err := verifyArgOrder(cmd,
105			"--sysroot="+ctx.tempDir+"/somepath/usr/x86_64-cros-linux-gnu", mainCc); err != nil {
106			t.Error(err)
107		}
108	})
109}
110