blob: 339b3028158333115fc318760fb238238b88b58f [file] [log] [blame]
Chih-Hung Hsieh104f51f2022-04-20 15:48:41 -07001// Copyright 2022 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package cc
16
17import (
18 "fmt"
19 "testing"
20
21 "android/soong/android"
22)
23
24func TestWithTidy(t *testing.T) {
25 // When WITH_TIDY=1 or (ALLOW_LOCAL_TIDY_TRUE=1 and local tidy:true)
26 // a C++ library should depend on .tidy files.
27 testCases := []struct {
28 withTidy, allowLocalTidyTrue string // "_" means undefined
29 needTidyFile []bool // for {libfoo_0, libfoo_1} and {libbar_0, libbar_1}
30 }{
31 {"_", "_", []bool{false, false, false}},
32 {"_", "0", []bool{false, false, false}},
33 {"_", "1", []bool{false, true, false}},
34 {"_", "true", []bool{false, true, false}},
35 {"0", "_", []bool{false, false, false}},
36 {"0", "1", []bool{false, true, false}},
37 {"1", "_", []bool{true, true, false}},
38 {"1", "false", []bool{true, true, false}},
39 {"1", "1", []bool{true, true, false}},
40 {"true", "_", []bool{true, true, false}},
41 }
42 bp := `
43 cc_library_shared {
44 name: "libfoo_0", // depends on .tidy if WITH_TIDY=1
45 srcs: ["foo.c"],
46 }
47 cc_library_shared { // depends on .tidy if WITH_TIDY=1 or ALLOW_LOCAL_TIDY_TRUE=1
48 name: "libfoo_1",
49 srcs: ["foo.c"],
50 tidy: true,
51 }
52 cc_library_shared { // no .tidy
53 name: "libfoo_2",
54 srcs: ["foo.c"],
55 tidy: false,
56 }
57 cc_library_static {
58 name: "libbar_0", // depends on .tidy if WITH_TIDY=1
59 srcs: ["bar.c"],
60 }
61 cc_library_static { // depends on .tidy if WITH_TIDY=1 or ALLOW_LOCAL_TIDY_TRUE=1
62 name: "libbar_1",
63 srcs: ["bar.c"],
64 tidy: true,
65 }
66 cc_library_static { // no .tidy
67 name: "libbar_2",
68 srcs: ["bar.c"],
69 tidy: false,
70 }`
71 for index, test := range testCases {
72 testName := fmt.Sprintf("case%d,%v,%v", index, test.withTidy, test.allowLocalTidyTrue)
73 t.Run(testName, func(t *testing.T) {
74 testEnv := map[string]string{}
75 if test.withTidy != "_" {
76 testEnv["WITH_TIDY"] = test.withTidy
77 }
78 if test.allowLocalTidyTrue != "_" {
79 testEnv["ALLOW_LOCAL_TIDY_TRUE"] = test.allowLocalTidyTrue
80 }
81 ctx := android.GroupFixturePreparers(prepareForCcTest, android.FixtureMergeEnv(testEnv)).RunTestWithBp(t, bp)
82 for n := 0; n < 3; n++ {
83 checkLibraryRule := func(foo, variant, ruleName string) {
84 libName := fmt.Sprintf("lib%s_%d", foo, n)
85 tidyFile := "out/soong/.intermediates/" + libName + "/" + variant + "/obj/" + foo + ".tidy"
86 depFiles := ctx.ModuleForTests(libName, variant).Rule(ruleName).Validations.Strings()
87 if test.needTidyFile[n] {
88 android.AssertStringListContains(t, libName+" needs .tidy file", depFiles, tidyFile)
89 } else {
90 android.AssertStringListDoesNotContain(t, libName+" does not need .tidy file", depFiles, tidyFile)
91 }
92 }
93 checkLibraryRule("foo", "android_arm64_armv8-a_shared", "ld")
94 checkLibraryRule("bar", "android_arm64_armv8-a_static", "ar")
95 }
96 })
97 }
98}