Paul Duffin | 1c6c1c7 | 2020-02-21 10:28:43 +0000 | [diff] [blame] | 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 | |
| 15 | package cc |
| 16 | |
| 17 | import ( |
Martin Stjernholm | e65c3ae | 2021-11-23 01:24:06 +0000 | [diff] [blame] | 18 | "fmt" |
Paul Duffin | 1c6c1c7 | 2020-02-21 10:28:43 +0000 | [diff] [blame] | 19 | "testing" |
Martin Stjernholm | e65c3ae | 2021-11-23 01:24:06 +0000 | [diff] [blame] | 20 | |
| 21 | "android/soong/android" |
| 22 | |
| 23 | "github.com/google/blueprint" |
Paul Duffin | 1c6c1c7 | 2020-02-21 10:28:43 +0000 | [diff] [blame] | 24 | ) |
| 25 | |
| 26 | func TestLibraryHeaders(t *testing.T) { |
Martin Stjernholm | 611e140 | 2021-11-30 18:37:16 +0000 | [diff] [blame] | 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 | ` |
Paul Duffin | 1c6c1c7 | 2020-02-21 10:28:43 +0000 | [diff] [blame] | 38 | |
Martin Stjernholm | 611e140 | 2021-11-30 18:37:16 +0000 | [diff] [blame] | 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)) |
Paul Duffin | f5ea9e1 | 2020-02-21 10:57:00 +0000 | [diff] [blame] | 42 | |
Martin Stjernholm | 611e140 | 2021-11-30 18:37:16 +0000 | [diff] [blame] | 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 ") |
Martin Stjernholm | d51cb5c | 2021-11-30 18:49:03 +0000 | [diff] [blame] | 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") |
Martin Stjernholm | 611e140 | 2021-11-30 18:37:16 +0000 | [diff] [blame] | 57 | }) |
Paul Duffin | f5ea9e1 | 2020-02-21 10:57:00 +0000 | [diff] [blame] | 58 | } |
| 59 | } |
Martin Stjernholm | e65c3ae | 2021-11-23 01:24:06 +0000 | [diff] [blame] | 60 | |
| 61 | func TestPrebuiltLibraryHeadersPreferred(t *testing.T) { |
Liz Kammer | 7c5d159 | 2022-10-31 16:27:38 -0400 | [diff] [blame] | 62 | t.Parallel() |
Martin Stjernholm | e65c3ae | 2021-11-23 01:24:06 +0000 | [diff] [blame] | 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 | } |