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