blob: 43aff5c685abe40c6857d4bcabcebc30215813a9 [file] [log] [blame]
Yu Liu7f3605f2022-02-08 14:15:12 -08001// 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 (
Sasha Smundakedd16662022-10-07 14:44:50 -070018 "android/soong/bazel/cquery"
Yu Liu7f3605f2022-02-08 14:15:12 -080019 "testing"
20
21 "android/soong/android"
22)
23
24func TestCcBinaryWithBazel(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -040025 t.Parallel()
Yu Liu7f3605f2022-02-08 14:15:12 -080026 bp := `
27cc_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 Smundakedd16662022-10-07 14:44:50 -070035 LabelToCcBinary: map[string]cquery.CcUnstrippedInfo{
36 "//foo/bar:bar": cquery.CcUnstrippedInfo{
37 OutputFile: "foo",
38 UnstrippedOutput: "foo.unstripped",
39 },
Yu Liu7f3605f2022-02-08 14:15:12 -080040 },
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 Smundakedd16662022-10-07 14:44:50 -070054 expectedUnStrippedFile := "outputbase/execroot/__main__/foo.unstripped"
Yu Liu7f3605f2022-02-08 14:15:12 -080055 android.AssertStringEquals(t, "Unstripped output file", expectedUnStrippedFile, unStrippedFilePath.String())
56}
David Brazdil958c9572022-04-22 15:17:54 +010057
58func TestBinaryLinkerScripts(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -040059 t.Parallel()
David Brazdil958c9572022-04-22 15:17:54 +010060 result := PrepareForIntegrationTestWithCc.RunTestWithBp(t, `
61 cc_binary {
62 name: "foo",
63 srcs: ["foo.cc"],
64 linker_scripts: ["foo.ld", "bar.ld"],
65 }`)
66
67 binFoo := result.ModuleForTests("foo", "android_arm64_armv8-a").Rule("ld")
68
69 android.AssertStringListContains(t, "missing dependency on linker_scripts",
70 binFoo.Implicits.Strings(), "foo.ld")
71 android.AssertStringListContains(t, "missing dependency on linker_scripts",
72 binFoo.Implicits.Strings(), "bar.ld")
73 android.AssertStringDoesContain(t, "missing flag for linker_scripts",
David Brazdil3ac9d2b2022-05-09 19:35:10 +010074 binFoo.Args["ldFlags"], "-Wl,--script,foo.ld")
David Brazdil958c9572022-04-22 15:17:54 +010075 android.AssertStringDoesContain(t, "missing flag for linker_scripts",
David Brazdil3ac9d2b2022-05-09 19:35:10 +010076 binFoo.Args["ldFlags"], "-Wl,--script,bar.ld")
David Brazdil958c9572022-04-22 15:17:54 +010077}