1// Copyright 2020 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	"github.com/google/blueprint"
24)
25
26func TestLibraryHeaders(t *testing.T) {
27	bp := `
28		%s {
29			name: "headers",
30			export_include_dirs: ["my_include"],
31		}
32		cc_library_static {
33			name: "lib",
34			srcs: ["foo.c"],
35			header_libs: ["headers"],
36		}
37	`
38
39	for _, headerModule := range []string{"cc_library_headers", "cc_prebuilt_library_headers"} {
40		t.Run(headerModule, func(t *testing.T) {
41			ctx := testCc(t, fmt.Sprintf(bp, headerModule))
42
43			// test if header search paths are correctly added
44			cc := ctx.ModuleForTests("lib", "android_arm64_armv8-a_static").Rule("cc")
45			android.AssertStringDoesContain(t, "cFlags for lib module", cc.Args["cFlags"], " -Imy_include ")
46
47			// Test that there's a valid AndroidMk entry.
48			headers := ctx.ModuleForTests("headers", "android_arm64_armv8-a").Module()
49			e := android.AndroidMkEntriesForTest(t, ctx, headers)[0]
50
51			// This duplicates the tests done in AndroidMkEntries.write. It would be
52			// better to test its output, but there are no test functions that capture that.
53			android.AssertBoolEquals(t, "AndroidMkEntries.Disabled", false, e.Disabled)
54			android.AssertBoolEquals(t, "AndroidMkEntries.OutputFile.Valid()", true, e.OutputFile.Valid())
55
56			android.AssertStringListContains(t, "LOCAL_EXPORT_CFLAGS for headers module", e.EntryMap["LOCAL_EXPORT_CFLAGS"], "-Imy_include")
57		})
58	}
59}
60
61func TestPrebuiltLibraryHeadersPreferred(t *testing.T) {
62	t.Parallel()
63	bp := `
64		cc_library_headers {
65			name: "headers",
66			export_include_dirs: ["my_include"],
67		}
68		cc_prebuilt_library_headers {
69			name: "headers",
70			prefer: %t,
71			export_include_dirs: ["my_include"],
72		}
73		cc_library_static {
74			name: "lib",
75			srcs: ["foo.c"],
76			header_libs: ["headers"],
77		}
78	`
79
80	for _, prebuiltPreferred := range []bool{false, true} {
81		t.Run(fmt.Sprintf("prebuilt prefer %t", prebuiltPreferred), func(t *testing.T) {
82			ctx := testCc(t, fmt.Sprintf(bp, prebuiltPreferred))
83			lib := ctx.ModuleForTests("lib", "android_arm64_armv8-a_static")
84			sourceDep := ctx.ModuleForTests("headers", "android_arm64_armv8-a")
85			prebuiltDep := ctx.ModuleForTests("prebuilt_headers", "android_arm64_armv8-a")
86			hasSourceDep := false
87			hasPrebuiltDep := false
88			ctx.VisitDirectDeps(lib.Module(), func(dep blueprint.Module) {
89				if dep == sourceDep.Module() {
90					hasSourceDep = true
91				}
92				if dep == prebuiltDep.Module() {
93					hasPrebuiltDep = true
94				}
95			})
96			android.AssertBoolEquals(t, "depends on source headers", !prebuiltPreferred, hasSourceDep)
97			android.AssertBoolEquals(t, "depends on prebuilt headers", prebuiltPreferred, hasPrebuiltDep)
98		})
99	}
100}
101