xref: /aosp_15_r20/external/toolchain-utils/compiler_wrapper/android_config_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	"path/filepath"
10	"testing"
11)
12
13const androidGoldenDir = "testdata/android_golden"
14
15func TestAndroidConfigDoesNotSpecifyCrashDir(t *testing.T) {
16	withTestContext(t, func(ctx *testContext) {
17		useLlvmNext := false
18		useCCache := false
19		cfg, err := getConfig("android", useCCache, useLlvmNext, "123")
20		if err != nil {
21			t.Fatal(err)
22		}
23		ctx.updateConfig(cfg)
24
25		cmd := ctx.must(callCompiler(ctx, ctx.cfg, ctx.newCommand(clangX86_64, mainCc)))
26		if err := verifyArgCount(cmd, 0, "-fcrash-diagnostics-dir=.*"); err != nil {
27			t.Error(err)
28		}
29	})
30}
31
32func TestAndroidConfig(t *testing.T) {
33	withTestContext(t, func(ctx *testContext) {
34		useLlvmNext := false
35		useCCache := false
36		cfg, err := getConfig("android", useCCache, useLlvmNext, "123")
37		if err != nil {
38			t.Fatal(err)
39		}
40		ctx.updateConfig(cfg)
41
42		runGoldenRecords(ctx, androidGoldenDir, []goldenFile{
43			createAndroidClangPathGoldenInputs(ctx),
44			createBisectGoldenInputs(filepath.Join(ctx.tempDir, "clang")),
45			createAndroidCompileWithFallbackGoldenInputs(ctx),
46		})
47	})
48}
49
50func createAndroidClangPathGoldenInputs(ctx *testContext) goldenFile {
51	gomaPath := path.Join(ctx.tempDir, "gomacc")
52	ctx.writeFile(gomaPath, "")
53	defaultPath := filepath.Join(ctx.tempDir, "clang")
54	clangTidyPath := filepath.Join(ctx.tempDir, "clang-tidy")
55
56	deepPath := "a/b/c/d/e/f/g/clang"
57	linkedDeepPath := "symlinked/clang_other"
58	ctx.writeFile(filepath.Join(ctx.tempDir, "/pathenv/clang"), "")
59	ctx.symlink(deepPath, linkedDeepPath)
60	return goldenFile{
61		Name: "clang_path.json",
62		Records: []goldenRecord{
63			{
64				WrapperCmd: newGoldenCmd(defaultPath, mainCc),
65				Cmds:       okResults,
66			},
67			{
68				WrapperCmd: newGoldenCmd(defaultPath, mainCc),
69				Cmds:       errorResults,
70			},
71			{
72				Env:        []string{"WITH_TIDY=1"},
73				WrapperCmd: newGoldenCmd(defaultPath, mainCc),
74				Cmds:       okResults,
75			},
76			{
77				WrapperCmd: newGoldenCmd(filepath.Join(ctx.tempDir, "clang++"), mainCc),
78				Cmds:       okResults,
79			},
80			{
81				WrapperCmd: newGoldenCmd(clangTidyPath, mainCc),
82				Cmds:       okResults,
83			},
84			{
85				Env:        []string{"WITH_TIDY=1"},
86				WrapperCmd: newGoldenCmd(clangTidyPath, mainCc),
87				Cmds:       okResults,
88			},
89			{
90				WrapperCmd: newGoldenCmd(deepPath, mainCc),
91				Cmds:       okResults,
92			},
93			{
94				WrapperCmd: newGoldenCmd(linkedDeepPath, mainCc),
95				Cmds:       okResults,
96			},
97			{
98				Env:        []string{"PATH=" + filepath.Join(ctx.tempDir, "/pathenv")},
99				WrapperCmd: newGoldenCmd("clang", mainCc),
100				Cmds:       okResults,
101			},
102			{
103				WrapperCmd: newGoldenCmd(defaultPath, mainCc, "--gomacc-path", gomaPath),
104				Cmds:       okResults,
105			},
106		},
107	}
108}
109
110func createAndroidCompileWithFallbackGoldenInputs(ctx *testContext) goldenFile {
111	env := []string{
112		"ANDROID_LLVM_PREBUILT_COMPILER_PATH=fallback_compiler",
113		"ANDROID_LLVM_STDERR_REDIRECT=" + filepath.Join(ctx.tempDir, "fallback_stderr"),
114		"ANDROID_LLVM_FALLBACK_DISABLED_WARNINGS=-a -b",
115	}
116	defaultPath := filepath.Join(ctx.tempDir, "clang")
117	return goldenFile{
118		Name: "compile_with_fallback.json",
119		Records: []goldenRecord{
120			{
121				WrapperCmd: newGoldenCmd(defaultPath, mainCc),
122				Env:        env,
123				Cmds:       okResults,
124			},
125			{
126				WrapperCmd: newGoldenCmd(defaultPath, mainCc),
127				Env:        env,
128				Cmds: []commandResult{
129					{
130						ExitCode: 1,
131					},
132					okResult,
133				},
134			},
135			{
136				WrapperCmd: newGoldenCmd(defaultPath, mainCc),
137				Env:        env,
138				Cmds: []commandResult{
139					{
140						ExitCode: 1,
141					},
142					{
143						ExitCode: 1,
144					},
145				},
146			},
147		},
148	}
149}
150