• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2019 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	"android/soong/android"
19	"testing"
20)
21
22func TestMinSdkVersionsOfCrtObjects(t *testing.T) {
23	ctx := testCc(t, `
24		cc_object {
25			name: "crt_foo",
26			srcs: ["foo.c"],
27			crt: true,
28			stl: "none",
29			min_sdk_version: "28",
30
31		}`)
32
33	arch := "android_arm64_armv8-a"
34	for _, v := range []string{"", "28", "29", "30", "current"} {
35		var variant string
36		// platform variant
37		if v == "" {
38			variant = arch
39		} else {
40			variant = arch + "_sdk_" + v
41		}
42		cflags := ctx.ModuleForTests("crt_foo", variant).Rule("cc").Args["cFlags"]
43		vNum := v
44		if v == "current" || v == "" {
45			vNum = "10000"
46		}
47		expected := "-target aarch64-linux-android" + vNum + " "
48		android.AssertStringDoesContain(t, "cflag", cflags, expected)
49	}
50}
51
52func TestUseCrtObjectOfCorrectVersion(t *testing.T) {
53	ctx := testCc(t, `
54		cc_binary {
55			name: "bin",
56			srcs: ["foo.c"],
57			stl: "none",
58			min_sdk_version: "29",
59			sdk_version: "current",
60		}
61		`)
62
63	// Sdk variant uses the crt object of the matching min_sdk_version
64	variant := "android_arm64_armv8-a_sdk"
65	crt := ctx.ModuleForTests("bin", variant).Rule("ld").Args["crtBegin"]
66	android.AssertStringDoesContain(t, "crt dep of sdk variant", crt,
67		variant+"_29/crtbegin_dynamic.o")
68
69	// platform variant uses the crt object built for platform
70	variant = "android_arm64_armv8-a"
71	crt = ctx.ModuleForTests("bin", variant).Rule("ld").Args["crtBegin"]
72	android.AssertStringDoesContain(t, "crt dep of platform variant", crt,
73		variant+"/crtbegin_dynamic.o")
74}
75
76func TestLinkerScript(t *testing.T) {
77	t.Run("script", func(t *testing.T) {
78		testCc(t, `
79		cc_object {
80			name: "foo",
81			srcs: ["baz.o"],
82			linker_script: "foo.lds",
83		}`)
84	})
85}
86
87func TestCcObjectWithBazel(t *testing.T) {
88	bp := `
89cc_object {
90	name: "foo",
91	srcs: ["baz.o"],
92	bazel_module: { label: "//foo/bar:bar" },
93}`
94	config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
95	config.BazelContext = android.MockBazelContext{
96		OutputBaseDir: "outputbase",
97		LabelToOutputFiles: map[string][]string{
98			"//foo/bar:bar": []string{"bazel_out.o"}}}
99	ctx := testCcWithConfig(t, config)
100
101	module := ctx.ModuleForTests("foo", "android_arm_armv7-a-neon").Module()
102	outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
103	if err != nil {
104		t.Errorf("Unexpected error getting cc_object outputfiles %s", err)
105	}
106
107	expectedOutputFiles := []string{"outputbase/execroot/__main__/bazel_out.o"}
108	android.AssertDeepEquals(t, "output files", expectedOutputFiles, outputFiles.Strings())
109}
110