binary_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright 2022 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. "android/soong/bazel/cquery"
  17. "testing"
  18. "android/soong/android"
  19. )
  20. func TestCcBinaryWithBazel(t *testing.T) {
  21. t.Parallel()
  22. bp := `
  23. cc_binary {
  24. name: "foo",
  25. srcs: ["foo.cc"],
  26. bazel_module: { label: "//foo/bar:bar" },
  27. }`
  28. config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
  29. config.BazelContext = android.MockBazelContext{
  30. OutputBaseDir: "outputbase",
  31. LabelToCcBinary: map[string]cquery.CcUnstrippedInfo{
  32. "//foo/bar:bar": cquery.CcUnstrippedInfo{
  33. OutputFile: "foo",
  34. UnstrippedOutput: "foo.unstripped",
  35. },
  36. },
  37. }
  38. ctx := testCcWithConfig(t, config)
  39. binMod := ctx.ModuleForTests("foo", "android_arm64_armv8-a").Module()
  40. producer := binMod.(android.OutputFileProducer)
  41. outputFiles, err := producer.OutputFiles("")
  42. if err != nil {
  43. t.Errorf("Unexpected error getting cc_binary outputfiles %s", err)
  44. }
  45. expectedOutputFiles := []string{"outputbase/execroot/__main__/foo"}
  46. android.AssertDeepEquals(t, "output files", expectedOutputFiles, outputFiles.Strings())
  47. unStrippedFilePath := binMod.(*Module).UnstrippedOutputFile()
  48. expectedUnStrippedFile := "outputbase/execroot/__main__/foo.unstripped"
  49. android.AssertStringEquals(t, "Unstripped output file", expectedUnStrippedFile, unStrippedFilePath.String())
  50. }
  51. func TestCcBinaryWithBazelValidations(t *testing.T) {
  52. t.Parallel()
  53. bp := `
  54. cc_binary {
  55. name: "foo",
  56. srcs: ["foo.cc"],
  57. bazel_module: { label: "//foo/bar:bar" },
  58. tidy: true,
  59. }`
  60. config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
  61. config.BazelContext = android.MockBazelContext{
  62. OutputBaseDir: "outputbase",
  63. LabelToCcBinary: map[string]cquery.CcUnstrippedInfo{
  64. "//foo/bar:bar": cquery.CcUnstrippedInfo{
  65. OutputFile: "foo",
  66. UnstrippedOutput: "foo.unstripped",
  67. TidyFiles: []string{"foo.c.tidy"},
  68. },
  69. },
  70. }
  71. ctx := android.GroupFixturePreparers(
  72. prepareForCcTest,
  73. android.FixtureMergeEnv(map[string]string{
  74. "ALLOW_LOCAL_TIDY_TRUE": "1",
  75. }),
  76. ).RunTestWithConfig(t, config).TestContext
  77. binMod := ctx.ModuleForTests("foo", "android_arm64_armv8-a").Module()
  78. producer := binMod.(android.OutputFileProducer)
  79. outputFiles, err := producer.OutputFiles("")
  80. if err != nil {
  81. t.Errorf("Unexpected error getting cc_binary outputfiles %s", err)
  82. }
  83. expectedOutputFiles := []string{"out/soong/.intermediates/foo/android_arm64_armv8-a/validated/foo"}
  84. android.AssertPathsRelativeToTopEquals(t, "output files", expectedOutputFiles, outputFiles)
  85. unStrippedFilePath := binMod.(*Module).UnstrippedOutputFile()
  86. expectedUnStrippedFile := "outputbase/execroot/__main__/foo.unstripped"
  87. android.AssertStringEquals(t, "Unstripped output file", expectedUnStrippedFile, unStrippedFilePath.String())
  88. }
  89. func TestBinaryLinkerScripts(t *testing.T) {
  90. t.Parallel()
  91. result := PrepareForIntegrationTestWithCc.RunTestWithBp(t, `
  92. cc_binary {
  93. name: "foo",
  94. srcs: ["foo.cc"],
  95. linker_scripts: ["foo.ld", "bar.ld"],
  96. }`)
  97. binFoo := result.ModuleForTests("foo", "android_arm64_armv8-a").Rule("ld")
  98. android.AssertStringListContains(t, "missing dependency on linker_scripts",
  99. binFoo.Implicits.Strings(), "foo.ld")
  100. android.AssertStringListContains(t, "missing dependency on linker_scripts",
  101. binFoo.Implicits.Strings(), "bar.ld")
  102. android.AssertStringDoesContain(t, "missing flag for linker_scripts",
  103. binFoo.Args["ldFlags"], "-Wl,--script,foo.ld")
  104. android.AssertStringDoesContain(t, "missing flag for linker_scripts",
  105. binFoo.Args["ldFlags"], "-Wl,--script,bar.ld")
  106. }