blob: 79d2291425f53f55c1a9cdd1bcad14b1c30268c7 [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 ")
46 })
Paul Duffinf5ea9e12020-02-21 10:57:00 +000047 }
48}
Martin Stjernholme65c3ae2021-11-23 01:24:06 +000049
50func TestPrebuiltLibraryHeadersPreferred(t *testing.T) {
51 bp := `
52 cc_library_headers {
53 name: "headers",
54 export_include_dirs: ["my_include"],
55 }
56 cc_prebuilt_library_headers {
57 name: "headers",
58 prefer: %t,
59 export_include_dirs: ["my_include"],
60 }
61 cc_library_static {
62 name: "lib",
63 srcs: ["foo.c"],
64 header_libs: ["headers"],
65 }
66 `
67
68 for _, prebuiltPreferred := range []bool{false, true} {
69 t.Run(fmt.Sprintf("prebuilt prefer %t", prebuiltPreferred), func(t *testing.T) {
70 ctx := testCc(t, fmt.Sprintf(bp, prebuiltPreferred))
71 lib := ctx.ModuleForTests("lib", "android_arm64_armv8-a_static")
72 sourceDep := ctx.ModuleForTests("headers", "android_arm64_armv8-a")
73 prebuiltDep := ctx.ModuleForTests("prebuilt_headers", "android_arm64_armv8-a")
74 hasSourceDep := false
75 hasPrebuiltDep := false
76 ctx.VisitDirectDeps(lib.Module(), func(dep blueprint.Module) {
77 if dep == sourceDep.Module() {
78 hasSourceDep = true
79 }
80 if dep == prebuiltDep.Module() {
81 hasPrebuiltDep = true
82 }
83 })
84 android.AssertBoolEquals(t, "depends on source headers", !prebuiltPreferred, hasSourceDep)
85 android.AssertBoolEquals(t, "depends on prebuilt headers", prebuiltPreferred, hasPrebuiltDep)
86 })
87 }
88}