blob: 3e448ba26f7645fb78aa6218e78492c7a2077fea [file] [log] [blame]
Paul Duffin1c6c1c72020-02-21 10:28:43 +00001// 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 (
Martin Stjernholme65c3ae2021-11-23 01:24:06 +000018 "fmt"
Paul Duffin1c6c1c72020-02-21 10:28:43 +000019 "testing"
Martin Stjernholme65c3ae2021-11-23 01:24:06 +000020
21 "android/soong/android"
22
23 "github.com/google/blueprint"
Paul Duffin1c6c1c72020-02-21 10:28:43 +000024)
25
26func TestLibraryHeaders(t *testing.T) {
Martin Stjernholm611e1402021-11-30 18:37:16 +000027 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 `
Paul Duffin1c6c1c72020-02-21 10:28:43 +000038
Martin Stjernholm611e1402021-11-30 18:37:16 +000039 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))
Paul Duffinf5ea9e12020-02-21 10:57:00 +000042
Martin Stjernholm611e1402021-11-30 18:37:16 +000043 // 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 ")
Martin Stjernholmd51cb5c2021-11-30 18:49:03 +000046
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")
Martin Stjernholm611e1402021-11-30 18:37:16 +000057 })
Paul Duffinf5ea9e12020-02-21 10:57:00 +000058 }
59}
Martin Stjernholme65c3ae2021-11-23 01:24:06 +000060
61func TestPrebuiltLibraryHeadersPreferred(t *testing.T) {
62 bp := `
63 cc_library_headers {
64 name: "headers",
65 export_include_dirs: ["my_include"],
66 }
67 cc_prebuilt_library_headers {
68 name: "headers",
69 prefer: %t,
70 export_include_dirs: ["my_include"],
71 }
72 cc_library_static {
73 name: "lib",
74 srcs: ["foo.c"],
75 header_libs: ["headers"],
76 }
77 `
78
79 for _, prebuiltPreferred := range []bool{false, true} {
80 t.Run(fmt.Sprintf("prebuilt prefer %t", prebuiltPreferred), func(t *testing.T) {
81 ctx := testCc(t, fmt.Sprintf(bp, prebuiltPreferred))
82 lib := ctx.ModuleForTests("lib", "android_arm64_armv8-a_static")
83 sourceDep := ctx.ModuleForTests("headers", "android_arm64_armv8-a")
84 prebuiltDep := ctx.ModuleForTests("prebuilt_headers", "android_arm64_armv8-a")
85 hasSourceDep := false
86 hasPrebuiltDep := false
87 ctx.VisitDirectDeps(lib.Module(), func(dep blueprint.Module) {
88 if dep == sourceDep.Module() {
89 hasSourceDep = true
90 }
91 if dep == prebuiltDep.Module() {
92 hasPrebuiltDep = true
93 }
94 })
95 android.AssertBoolEquals(t, "depends on source headers", !prebuiltPreferred, hasSourceDep)
96 android.AssertBoolEquals(t, "depends on prebuilt headers", prebuiltPreferred, hasPrebuiltDep)
97 })
98 }
99}