xref: /aosp_15_r20/build/soong/cc/gen_test.go (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
1*333d2b36SAndroid Build Coastguard Worker// Copyright 2017 Google Inc. All rights reserved.
2*333d2b36SAndroid Build Coastguard Worker//
3*333d2b36SAndroid Build Coastguard Worker// Licensed under the Apache License, Version 2.0 (the "License");
4*333d2b36SAndroid Build Coastguard Worker// you may not use this file except in compliance with the License.
5*333d2b36SAndroid Build Coastguard Worker// You may obtain a copy of the License at
6*333d2b36SAndroid Build Coastguard Worker//
7*333d2b36SAndroid Build Coastguard Worker//     http://www.apache.org/licenses/LICENSE-2.0
8*333d2b36SAndroid Build Coastguard Worker//
9*333d2b36SAndroid Build Coastguard Worker// Unless required by applicable law or agreed to in writing, software
10*333d2b36SAndroid Build Coastguard Worker// distributed under the License is distributed on an "AS IS" BASIS,
11*333d2b36SAndroid Build Coastguard Worker// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*333d2b36SAndroid Build Coastguard Worker// See the License for the specific language governing permissions and
13*333d2b36SAndroid Build Coastguard Worker// limitations under the License.
14*333d2b36SAndroid Build Coastguard Worker
15*333d2b36SAndroid Build Coastguard Workerpackage cc
16*333d2b36SAndroid Build Coastguard Worker
17*333d2b36SAndroid Build Coastguard Workerimport (
18*333d2b36SAndroid Build Coastguard Worker	"path/filepath"
19*333d2b36SAndroid Build Coastguard Worker	"strings"
20*333d2b36SAndroid Build Coastguard Worker	"testing"
21*333d2b36SAndroid Build Coastguard Worker
22*333d2b36SAndroid Build Coastguard Worker	"android/soong/android"
23*333d2b36SAndroid Build Coastguard Worker)
24*333d2b36SAndroid Build Coastguard Worker
25*333d2b36SAndroid Build Coastguard Workerfunc TestGen(t *testing.T) {
26*333d2b36SAndroid Build Coastguard Worker	t.Run("simple", func(t *testing.T) {
27*333d2b36SAndroid Build Coastguard Worker		ctx := testCc(t, `
28*333d2b36SAndroid Build Coastguard Worker		cc_library_shared {
29*333d2b36SAndroid Build Coastguard Worker			name: "libfoo",
30*333d2b36SAndroid Build Coastguard Worker			srcs: [
31*333d2b36SAndroid Build Coastguard Worker				"foo.c",
32*333d2b36SAndroid Build Coastguard Worker				"b.aidl",
33*333d2b36SAndroid Build Coastguard Worker			],
34*333d2b36SAndroid Build Coastguard Worker		}`)
35*333d2b36SAndroid Build Coastguard Worker
36*333d2b36SAndroid Build Coastguard Worker		aidl := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_shared").Rule("aidl")
37*333d2b36SAndroid Build Coastguard Worker		libfoo := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_shared").Module().(*Module)
38*333d2b36SAndroid Build Coastguard Worker
39*333d2b36SAndroid Build Coastguard Worker		expected := "-I" + filepath.Dir(aidl.Output.String())
40*333d2b36SAndroid Build Coastguard Worker		actual := android.StringsRelativeToTop(ctx.Config(), libfoo.flags.Local.CommonFlags)
41*333d2b36SAndroid Build Coastguard Worker		if !inList(expected, actual) {
42*333d2b36SAndroid Build Coastguard Worker			t.Errorf("missing aidl includes in global flags, expected %q, actual %q", expected, actual)
43*333d2b36SAndroid Build Coastguard Worker		}
44*333d2b36SAndroid Build Coastguard Worker	})
45*333d2b36SAndroid Build Coastguard Worker
46*333d2b36SAndroid Build Coastguard Worker	t.Run("filegroup", func(t *testing.T) {
47*333d2b36SAndroid Build Coastguard Worker		ctx := testCc(t, `
48*333d2b36SAndroid Build Coastguard Worker		filegroup {
49*333d2b36SAndroid Build Coastguard Worker			name: "fg",
50*333d2b36SAndroid Build Coastguard Worker			srcs: ["sub/c.aidl"],
51*333d2b36SAndroid Build Coastguard Worker			path: "sub",
52*333d2b36SAndroid Build Coastguard Worker		}
53*333d2b36SAndroid Build Coastguard Worker
54*333d2b36SAndroid Build Coastguard Worker		cc_library_shared {
55*333d2b36SAndroid Build Coastguard Worker			name: "libfoo",
56*333d2b36SAndroid Build Coastguard Worker			srcs: [
57*333d2b36SAndroid Build Coastguard Worker				"foo.c",
58*333d2b36SAndroid Build Coastguard Worker				":fg",
59*333d2b36SAndroid Build Coastguard Worker			],
60*333d2b36SAndroid Build Coastguard Worker		}`)
61*333d2b36SAndroid Build Coastguard Worker
62*333d2b36SAndroid Build Coastguard Worker		aidl := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_shared").Rule("aidl")
63*333d2b36SAndroid Build Coastguard Worker		aidlManifest := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_shared").Output("aidl.sbox.textproto")
64*333d2b36SAndroid Build Coastguard Worker		libfoo := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_shared").Module().(*Module)
65*333d2b36SAndroid Build Coastguard Worker
66*333d2b36SAndroid Build Coastguard Worker		if !inList("-I"+filepath.Dir(aidl.Output.String()), android.StringsRelativeToTop(ctx.Config(), libfoo.flags.Local.CommonFlags)) {
67*333d2b36SAndroid Build Coastguard Worker			t.Errorf("missing aidl includes in global flags")
68*333d2b36SAndroid Build Coastguard Worker		}
69*333d2b36SAndroid Build Coastguard Worker
70*333d2b36SAndroid Build Coastguard Worker		aidlCommand := android.RuleBuilderSboxProtoForTests(t, ctx, aidlManifest).Commands[0].GetCommand()
71*333d2b36SAndroid Build Coastguard Worker		if !strings.Contains(aidlCommand, "-Isub") {
72*333d2b36SAndroid Build Coastguard Worker			t.Errorf("aidl command for c.aidl should contain \"-Isub\", but was %q", aidlCommand)
73*333d2b36SAndroid Build Coastguard Worker		}
74*333d2b36SAndroid Build Coastguard Worker
75*333d2b36SAndroid Build Coastguard Worker	})
76*333d2b36SAndroid Build Coastguard Worker
77*333d2b36SAndroid Build Coastguard Worker	t.Run("sysprop", func(t *testing.T) {
78*333d2b36SAndroid Build Coastguard Worker		ctx := testCc(t, `
79*333d2b36SAndroid Build Coastguard Worker		cc_library {
80*333d2b36SAndroid Build Coastguard Worker			name: "libsysprop",
81*333d2b36SAndroid Build Coastguard Worker			srcs: [
82*333d2b36SAndroid Build Coastguard Worker				"path/to/foo.sysprop",
83*333d2b36SAndroid Build Coastguard Worker			],
84*333d2b36SAndroid Build Coastguard Worker		}`)
85*333d2b36SAndroid Build Coastguard Worker
86*333d2b36SAndroid Build Coastguard Worker		outDir := "out/soong/.intermediates/libsysprop/android_arm64_armv8-a_static/gen"
87*333d2b36SAndroid Build Coastguard Worker		syspropBuildParams := ctx.ModuleForTests("libsysprop", "android_arm64_armv8-a_static").Rule("sysprop")
88*333d2b36SAndroid Build Coastguard Worker
89*333d2b36SAndroid Build Coastguard Worker		android.AssertStringEquals(t, "header output directory does not match", outDir+"/sysprop/include/path/to", syspropBuildParams.Args["headerOutDir"])
90*333d2b36SAndroid Build Coastguard Worker		android.AssertStringEquals(t, "public output directory does not match", outDir+"/sysprop/public/include/path/to", syspropBuildParams.Args["publicOutDir"])
91*333d2b36SAndroid Build Coastguard Worker		android.AssertStringEquals(t, "src output directory does not match", outDir+"/sysprop/path/to", syspropBuildParams.Args["srcOutDir"])
92*333d2b36SAndroid Build Coastguard Worker		android.AssertStringEquals(t, "output include name does not match", "path/to/foo.sysprop.h", syspropBuildParams.Args["includeName"])
93*333d2b36SAndroid Build Coastguard Worker		android.AssertStringEquals(t, "Input file does not match", "path/to/foo.sysprop", syspropBuildParams.Input.String())
94*333d2b36SAndroid Build Coastguard Worker		android.AssertStringEquals(t, "Output file does not match", outDir+"/sysprop/path/to/foo.sysprop.cpp", syspropBuildParams.Output.String())
95*333d2b36SAndroid Build Coastguard Worker		android.AssertStringListContains(t, "Implicit outputs does not contain header file", syspropBuildParams.ImplicitOutputs.Strings(), outDir+"/sysprop/include/path/to/foo.sysprop.h")
96*333d2b36SAndroid Build Coastguard Worker		android.AssertStringListContains(t, "Implicit outputs does not contain public header file", syspropBuildParams.ImplicitOutputs.Strings(), outDir+"/sysprop/public/include/path/to/foo.sysprop.h")
97*333d2b36SAndroid Build Coastguard Worker		android.AssertIntEquals(t, "Implicit outputs contains the incorrect number of elements", 2, len(syspropBuildParams.ImplicitOutputs.Strings()))
98*333d2b36SAndroid Build Coastguard Worker	})
99*333d2b36SAndroid Build Coastguard Worker}
100