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 15package cc 16 17import ( 18 "testing" 19 20 "android/soong/bazel/cquery" 21 22 "android/soong/android" 23) 24 25func TestCcBinaryWithBazel(t *testing.T) { 26 t.Parallel() 27 bp := ` 28cc_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", 36 LabelToCcBinary: map[string]cquery.CcUnstrippedInfo{ 37 "//foo/bar:bar": cquery.CcUnstrippedInfo{ 38 OutputFile: "foo", 39 UnstrippedOutput: "foo.unstripped", 40 }, 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() 55 expectedUnStrippedFile := "outputbase/execroot/__main__/foo.unstripped" 56 android.AssertStringEquals(t, "Unstripped output file", expectedUnStrippedFile, unStrippedFilePath.String()) 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]) 60} 61 62func TestCcBinaryWithBazelValidations(t *testing.T) { 63 t.Parallel() 64 bp := ` 65cc_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 103func TestBinaryLinkerScripts(t *testing.T) { 104 t.Parallel() 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", 119 binFoo.Args["ldFlags"], "-Wl,--script,foo.ld") 120 android.AssertStringDoesContain(t, "missing flag for linker_scripts", 121 binFoo.Args["ldFlags"], "-Wl,--script,bar.ld") 122} 123