binary_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. "testing"
  17. "android/soong/bazel/cquery"
  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. entries := android.AndroidMkEntriesForTest(t, ctx, binMod)[0]
  51. android.AssertStringEquals(t, "unexpected LOCAL_SOONG_MODULE_TYPE", "cc_binary", entries.EntryMap["LOCAL_SOONG_MODULE_TYPE"][0])
  52. }
  53. func TestCcBinaryWithBazelValidations(t *testing.T) {
  54. t.Parallel()
  55. bp := `
  56. cc_binary {
  57. name: "foo",
  58. srcs: ["foo.cc"],
  59. bazel_module: { label: "//foo/bar:bar" },
  60. tidy: true,
  61. }`
  62. config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
  63. config.BazelContext = android.MockBazelContext{
  64. OutputBaseDir: "outputbase",
  65. LabelToCcBinary: map[string]cquery.CcUnstrippedInfo{
  66. "//foo/bar:bar": cquery.CcUnstrippedInfo{
  67. OutputFile: "foo",
  68. UnstrippedOutput: "foo.unstripped",
  69. TidyFiles: []string{"foo.c.tidy"},
  70. },
  71. },
  72. }
  73. ctx := android.GroupFixturePreparers(
  74. prepareForCcTest,
  75. android.FixtureMergeEnv(map[string]string{
  76. "ALLOW_LOCAL_TIDY_TRUE": "1",
  77. }),
  78. ).RunTestWithConfig(t, config).TestContext
  79. binMod := ctx.ModuleForTests("foo", "android_arm64_armv8-a").Module()
  80. producer := binMod.(android.OutputFileProducer)
  81. outputFiles, err := producer.OutputFiles("")
  82. if err != nil {
  83. t.Errorf("Unexpected error getting cc_binary outputfiles %s", err)
  84. }
  85. expectedOutputFiles := []string{"out/soong/.intermediates/foo/android_arm64_armv8-a/validated/foo"}
  86. android.AssertPathsRelativeToTopEquals(t, "output files", expectedOutputFiles, outputFiles)
  87. unStrippedFilePath := binMod.(*Module).UnstrippedOutputFile()
  88. expectedUnStrippedFile := "outputbase/execroot/__main__/foo.unstripped"
  89. android.AssertStringEquals(t, "Unstripped output file", expectedUnStrippedFile, unStrippedFilePath.String())
  90. }
  91. func TestBinaryLinkerScripts(t *testing.T) {
  92. t.Parallel()
  93. result := PrepareForIntegrationTestWithCc.RunTestWithBp(t, `
  94. cc_binary {
  95. name: "foo",
  96. srcs: ["foo.cc"],
  97. linker_scripts: ["foo.ld", "bar.ld"],
  98. }`)
  99. binFoo := result.ModuleForTests("foo", "android_arm64_armv8-a").Rule("ld")
  100. android.AssertStringListContains(t, "missing dependency on linker_scripts",
  101. binFoo.Implicits.Strings(), "foo.ld")
  102. android.AssertStringListContains(t, "missing dependency on linker_scripts",
  103. binFoo.Implicits.Strings(), "bar.ld")
  104. android.AssertStringDoesContain(t, "missing flag for linker_scripts",
  105. binFoo.Args["ldFlags"], "-Wl,--script,foo.ld")
  106. android.AssertStringDoesContain(t, "missing flag for linker_scripts",
  107. binFoo.Args["ldFlags"], "-Wl,--script,bar.ld")
  108. }