library_headers_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. package cc
  15. import (
  16. "fmt"
  17. "testing"
  18. "android/soong/android"
  19. "github.com/google/blueprint"
  20. )
  21. func TestLibraryHeaders(t *testing.T) {
  22. bp := `
  23. %s {
  24. name: "headers",
  25. export_include_dirs: ["my_include"],
  26. }
  27. cc_library_static {
  28. name: "lib",
  29. srcs: ["foo.c"],
  30. header_libs: ["headers"],
  31. }
  32. `
  33. for _, headerModule := range []string{"cc_library_headers", "cc_prebuilt_library_headers"} {
  34. t.Run(headerModule, func(t *testing.T) {
  35. ctx := testCc(t, fmt.Sprintf(bp, headerModule))
  36. // test if header search paths are correctly added
  37. cc := ctx.ModuleForTests("lib", "android_arm64_armv8-a_static").Rule("cc")
  38. android.AssertStringDoesContain(t, "cFlags for lib module", cc.Args["cFlags"], " -Imy_include ")
  39. // Test that there's a valid AndroidMk entry.
  40. headers := ctx.ModuleForTests("headers", "android_arm64_armv8-a").Module()
  41. e := android.AndroidMkEntriesForTest(t, ctx, headers)[0]
  42. // This duplicates the tests done in AndroidMkEntries.write. It would be
  43. // better to test its output, but there are no test functions that capture that.
  44. android.AssertBoolEquals(t, "AndroidMkEntries.Disabled", false, e.Disabled)
  45. android.AssertBoolEquals(t, "AndroidMkEntries.OutputFile.Valid()", true, e.OutputFile.Valid())
  46. android.AssertStringListContains(t, "LOCAL_EXPORT_CFLAGS for headers module", e.EntryMap["LOCAL_EXPORT_CFLAGS"], "-Imy_include")
  47. })
  48. }
  49. }
  50. func TestPrebuiltLibraryHeadersPreferred(t *testing.T) {
  51. t.Parallel()
  52. bp := `
  53. cc_library_headers {
  54. name: "headers",
  55. export_include_dirs: ["my_include"],
  56. }
  57. cc_prebuilt_library_headers {
  58. name: "headers",
  59. prefer: %t,
  60. export_include_dirs: ["my_include"],
  61. }
  62. cc_library_static {
  63. name: "lib",
  64. srcs: ["foo.c"],
  65. header_libs: ["headers"],
  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. }