Yu Liu | 7f3605f | 2022-02-08 14:15:12 -0800 | [diff] [blame] | 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 | |
| 15 | package cc |
| 16 | |
| 17 | import ( |
Sasha Smundak | edd1666 | 2022-10-07 14:44:50 -0700 | [diff] [blame] | 18 | "android/soong/bazel/cquery" |
Yu Liu | 7f3605f | 2022-02-08 14:15:12 -0800 | [diff] [blame] | 19 | "testing" |
| 20 | |
| 21 | "android/soong/android" |
| 22 | ) |
| 23 | |
| 24 | func TestCcBinaryWithBazel(t *testing.T) { |
Liz Kammer | 7c5d159 | 2022-10-31 16:27:38 -0400 | [diff] [blame] | 25 | t.Parallel() |
Yu Liu | 7f3605f | 2022-02-08 14:15:12 -0800 | [diff] [blame] | 26 | bp := ` |
| 27 | cc_binary { |
| 28 | name: "foo", |
| 29 | srcs: ["foo.cc"], |
| 30 | bazel_module: { label: "//foo/bar:bar" }, |
| 31 | }` |
| 32 | config := TestConfig(t.TempDir(), android.Android, nil, bp, nil) |
| 33 | config.BazelContext = android.MockBazelContext{ |
| 34 | OutputBaseDir: "outputbase", |
Sasha Smundak | edd1666 | 2022-10-07 14:44:50 -0700 | [diff] [blame] | 35 | LabelToCcBinary: map[string]cquery.CcUnstrippedInfo{ |
| 36 | "//foo/bar:bar": cquery.CcUnstrippedInfo{ |
| 37 | OutputFile: "foo", |
| 38 | UnstrippedOutput: "foo.unstripped", |
| 39 | }, |
Yu Liu | 7f3605f | 2022-02-08 14:15:12 -0800 | [diff] [blame] | 40 | }, |
| 41 | } |
| 42 | ctx := testCcWithConfig(t, config) |
| 43 | |
| 44 | binMod := ctx.ModuleForTests("foo", "android_arm64_armv8-a").Module() |
| 45 | producer := binMod.(android.OutputFileProducer) |
| 46 | outputFiles, err := producer.OutputFiles("") |
| 47 | if err != nil { |
| 48 | t.Errorf("Unexpected error getting cc_binary outputfiles %s", err) |
| 49 | } |
| 50 | expectedOutputFiles := []string{"outputbase/execroot/__main__/foo"} |
| 51 | android.AssertDeepEquals(t, "output files", expectedOutputFiles, outputFiles.Strings()) |
| 52 | |
| 53 | unStrippedFilePath := binMod.(*Module).UnstrippedOutputFile() |
Sasha Smundak | edd1666 | 2022-10-07 14:44:50 -0700 | [diff] [blame] | 54 | expectedUnStrippedFile := "outputbase/execroot/__main__/foo.unstripped" |
Yu Liu | 7f3605f | 2022-02-08 14:15:12 -0800 | [diff] [blame] | 55 | android.AssertStringEquals(t, "Unstripped output file", expectedUnStrippedFile, unStrippedFilePath.String()) |
| 56 | } |
David Brazdil | 958c957 | 2022-04-22 15:17:54 +0100 | [diff] [blame] | 57 | |
Sam Delmerico | 4ed95e2 | 2023-02-03 18:12:15 -0500 | [diff] [blame] | 58 | func TestCcBinaryWithBazelValidations(t *testing.T) { |
| 59 | t.Parallel() |
| 60 | bp := ` |
| 61 | cc_binary { |
| 62 | name: "foo", |
| 63 | srcs: ["foo.cc"], |
| 64 | bazel_module: { label: "//foo/bar:bar" }, |
| 65 | tidy: true, |
| 66 | }` |
| 67 | config := TestConfig(t.TempDir(), android.Android, nil, bp, nil) |
| 68 | config.BazelContext = android.MockBazelContext{ |
| 69 | OutputBaseDir: "outputbase", |
| 70 | LabelToCcBinary: map[string]cquery.CcUnstrippedInfo{ |
| 71 | "//foo/bar:bar": cquery.CcUnstrippedInfo{ |
| 72 | OutputFile: "foo", |
| 73 | UnstrippedOutput: "foo.unstripped", |
| 74 | TidyFiles: []string{"foo.c.tidy"}, |
| 75 | }, |
| 76 | }, |
| 77 | } |
| 78 | ctx := android.GroupFixturePreparers( |
| 79 | prepareForCcTest, |
| 80 | android.FixtureMergeEnv(map[string]string{ |
| 81 | "ALLOW_LOCAL_TIDY_TRUE": "1", |
| 82 | }), |
| 83 | ).RunTestWithConfig(t, config).TestContext |
| 84 | |
| 85 | binMod := ctx.ModuleForTests("foo", "android_arm64_armv8-a").Module() |
| 86 | producer := binMod.(android.OutputFileProducer) |
| 87 | outputFiles, err := producer.OutputFiles("") |
| 88 | if err != nil { |
| 89 | t.Errorf("Unexpected error getting cc_binary outputfiles %s", err) |
| 90 | } |
| 91 | expectedOutputFiles := []string{"out/soong/.intermediates/foo/android_arm64_armv8-a/validated/foo"} |
| 92 | android.AssertPathsRelativeToTopEquals(t, "output files", expectedOutputFiles, outputFiles) |
| 93 | |
| 94 | unStrippedFilePath := binMod.(*Module).UnstrippedOutputFile() |
| 95 | expectedUnStrippedFile := "outputbase/execroot/__main__/foo.unstripped" |
| 96 | android.AssertStringEquals(t, "Unstripped output file", expectedUnStrippedFile, unStrippedFilePath.String()) |
| 97 | } |
| 98 | |
David Brazdil | 958c957 | 2022-04-22 15:17:54 +0100 | [diff] [blame] | 99 | func TestBinaryLinkerScripts(t *testing.T) { |
Liz Kammer | 7c5d159 | 2022-10-31 16:27:38 -0400 | [diff] [blame] | 100 | t.Parallel() |
David Brazdil | 958c957 | 2022-04-22 15:17:54 +0100 | [diff] [blame] | 101 | result := PrepareForIntegrationTestWithCc.RunTestWithBp(t, ` |
| 102 | cc_binary { |
| 103 | name: "foo", |
| 104 | srcs: ["foo.cc"], |
| 105 | linker_scripts: ["foo.ld", "bar.ld"], |
| 106 | }`) |
| 107 | |
| 108 | binFoo := result.ModuleForTests("foo", "android_arm64_armv8-a").Rule("ld") |
| 109 | |
| 110 | android.AssertStringListContains(t, "missing dependency on linker_scripts", |
| 111 | binFoo.Implicits.Strings(), "foo.ld") |
| 112 | android.AssertStringListContains(t, "missing dependency on linker_scripts", |
| 113 | binFoo.Implicits.Strings(), "bar.ld") |
| 114 | android.AssertStringDoesContain(t, "missing flag for linker_scripts", |
David Brazdil | 3ac9d2b | 2022-05-09 19:35:10 +0100 | [diff] [blame] | 115 | binFoo.Args["ldFlags"], "-Wl,--script,foo.ld") |
David Brazdil | 958c957 | 2022-04-22 15:17:54 +0100 | [diff] [blame] | 116 | android.AssertStringDoesContain(t, "missing flag for linker_scripts", |
David Brazdil | 3ac9d2b | 2022-05-09 19:35:10 +0100 | [diff] [blame] | 117 | binFoo.Args["ldFlags"], "-Wl,--script,bar.ld") |
David Brazdil | 958c957 | 2022-04-22 15:17:54 +0100 | [diff] [blame] | 118 | } |