1// Copyright 2017 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	"path/filepath"
19	"strings"
20	"testing"
21
22	"android/soong/android"
23)
24
25func TestGen(t *testing.T) {
26	t.Run("simple", func(t *testing.T) {
27		ctx := testCc(t, `
28		cc_library_shared {
29			name: "libfoo",
30			srcs: [
31				"foo.c",
32				"b.aidl",
33			],
34		}`)
35
36		aidl := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_shared").Rule("aidl")
37		libfoo := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_shared").Module().(*Module)
38
39		expected := "-I" + filepath.Dir(aidl.Output.String())
40		actual := android.StringsRelativeToTop(ctx.Config(), libfoo.flags.Local.CommonFlags)
41		if !inList(expected, actual) {
42			t.Errorf("missing aidl includes in global flags, expected %q, actual %q", expected, actual)
43		}
44	})
45
46	t.Run("filegroup", func(t *testing.T) {
47		ctx := testCc(t, `
48		filegroup {
49			name: "fg",
50			srcs: ["sub/c.aidl"],
51			path: "sub",
52		}
53
54		cc_library_shared {
55			name: "libfoo",
56			srcs: [
57				"foo.c",
58				":fg",
59			],
60		}`)
61
62		aidl := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_shared").Rule("aidl")
63		aidlManifest := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_shared").Output("aidl.sbox.textproto")
64		libfoo := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_shared").Module().(*Module)
65
66		if !inList("-I"+filepath.Dir(aidl.Output.String()), android.StringsRelativeToTop(ctx.Config(), libfoo.flags.Local.CommonFlags)) {
67			t.Errorf("missing aidl includes in global flags")
68		}
69
70		aidlCommand := android.RuleBuilderSboxProtoForTests(t, aidlManifest).Commands[0].GetCommand()
71		if !strings.Contains(aidlCommand, "-Isub") {
72			t.Errorf("aidl command for c.aidl should contain \"-Isub\", but was %q", aidlCommand)
73		}
74
75	})
76
77}
78