blob: 9789ab04bc4f23da92d2486fb68865f26aa2db81 [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 "strings"
20 "testing"
Martin Stjernholme65c3ae2021-11-23 01:24:06 +000021
22 "android/soong/android"
23
24 "github.com/google/blueprint"
Paul Duffin1c6c1c72020-02-21 10:28:43 +000025)
26
27func TestLibraryHeaders(t *testing.T) {
28 ctx := testCc(t, `
29 cc_library_headers {
30 name: "headers",
31 export_include_dirs: ["my_include"],
32 }
33 cc_library_static {
34 name: "lib",
35 srcs: ["foo.c"],
36 header_libs: ["headers"],
37 }
38 `)
39
40 // test if header search paths are correctly added
41 cc := ctx.ModuleForTests("lib", "android_arm64_armv8-a_static").Rule("cc")
42 cflags := cc.Args["cFlags"]
43 if !strings.Contains(cflags, " -Imy_include ") {
44 t.Errorf("cflags for libsystem must contain -Imy_include, but was %#v.", cflags)
45 }
46}
Paul Duffinf5ea9e12020-02-21 10:57:00 +000047
48func TestPrebuiltLibraryHeaders(t *testing.T) {
49 ctx := testCc(t, `
50 cc_prebuilt_library_headers {
51 name: "headers",
52 export_include_dirs: ["my_include"],
53 }
54 cc_library_static {
55 name: "lib",
56 srcs: ["foo.c"],
57 header_libs: ["headers"],
58 }
59 `)
60
61 // test if header search paths are correctly added
62 cc := ctx.ModuleForTests("lib", "android_arm64_armv8-a_static").Rule("cc")
63 cflags := cc.Args["cFlags"]
64 if !strings.Contains(cflags, " -Imy_include ") {
65 t.Errorf("cflags for libsystem must contain -Imy_include, but was %#v.", cflags)
66 }
67}
Martin Stjernholme65c3ae2021-11-23 01:24:06 +000068
69func TestPrebuiltLibraryHeadersPreferred(t *testing.T) {
70 bp := `
71 cc_library_headers {
72 name: "headers",
73 export_include_dirs: ["my_include"],
74 }
75 cc_prebuilt_library_headers {
76 name: "headers",
77 prefer: %t,
78 export_include_dirs: ["my_include"],
79 }
80 cc_library_static {
81 name: "lib",
82 srcs: ["foo.c"],
83 header_libs: ["headers"],
84 }
85 `
86
87 for _, prebuiltPreferred := range []bool{false, true} {
88 t.Run(fmt.Sprintf("prebuilt prefer %t", prebuiltPreferred), func(t *testing.T) {
89 ctx := testCc(t, fmt.Sprintf(bp, prebuiltPreferred))
90 lib := ctx.ModuleForTests("lib", "android_arm64_armv8-a_static")
91 sourceDep := ctx.ModuleForTests("headers", "android_arm64_armv8-a")
92 prebuiltDep := ctx.ModuleForTests("prebuilt_headers", "android_arm64_armv8-a")
93 hasSourceDep := false
94 hasPrebuiltDep := false
95 ctx.VisitDirectDeps(lib.Module(), func(dep blueprint.Module) {
96 if dep == sourceDep.Module() {
97 hasSourceDep = true
98 }
99 if dep == prebuiltDep.Module() {
100 hasPrebuiltDep = true
101 }
102 })
103 android.AssertBoolEquals(t, "depends on source headers", !prebuiltPreferred, hasSourceDep)
104 android.AssertBoolEquals(t, "depends on prebuilt headers", prebuiltPreferred, hasPrebuiltDep)
105 })
106 }
107}