blob: 1924b2f2c0ff6f814c760be55ef84caa14669535 [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) {
Liz Kammer7c5d1592022-10-31 16:27:38 -040062 t.Parallel()
Martin Stjernholme65c3ae2021-11-23 01:24:06 +000063 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}