1// Copyright 2016 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	"fmt"
19
20	"android/soong/android"
21)
22
23//
24// Objects (for crt*.o)
25//
26
27func init() {
28	android.RegisterModuleType("cc_object", ObjectFactory)
29	android.RegisterSdkMemberType(ccObjectSdkMemberType)
30}
31
32var ccObjectSdkMemberType = &librarySdkMemberType{
33	SdkMemberTypeBase: android.SdkMemberTypeBase{
34		PropertyName: "native_objects",
35		SupportsSdk:  true,
36	},
37	prebuiltModuleType: "cc_prebuilt_object",
38	linkTypes:          nil,
39}
40
41type objectLinker struct {
42	*baseLinker
43	Properties ObjectLinkerProperties
44}
45
46type ObjectLinkerProperties struct {
47	// list of modules that should only provide headers for this module.
48	Header_libs []string `android:"arch_variant,variant_prepend"`
49
50	// names of other cc_object modules to link into this module using partial linking
51	Objs []string `android:"arch_variant"`
52
53	// if set, add an extra objcopy --prefix-symbols= step
54	Prefix_symbols *string
55
56	// if set, the path to a linker script to pass to ld -r when combining multiple object files.
57	Linker_script *string `android:"path,arch_variant"`
58}
59
60func newObject() *Module {
61	module := newBaseModule(android.HostAndDeviceSupported, android.MultilibBoth)
62	module.sanitize = &sanitize{}
63	module.stl = &stl{}
64	return module
65}
66
67// cc_object runs the compiler without running the linker. It is rarely
68// necessary, but sometimes used to generate .s files from .c files to use as
69// input to a cc_genrule module.
70func ObjectFactory() android.Module {
71	module := newObject()
72	module.linker = &objectLinker{
73		baseLinker: NewBaseLinker(module.sanitize),
74	}
75	module.compiler = NewBaseCompiler()
76
77	// Clang's address-significance tables are incompatible with ld -r.
78	module.compiler.appendCflags([]string{"-fno-addrsig"})
79
80	module.sdkMemberTypes = []android.SdkMemberType{ccObjectSdkMemberType}
81	return module.Init()
82}
83
84func (object *objectLinker) appendLdflags(flags []string) {
85	panic(fmt.Errorf("appendLdflags on objectLinker not supported"))
86}
87
88func (object *objectLinker) linkerProps() []interface{} {
89	return []interface{}{&object.Properties}
90}
91
92func (*objectLinker) linkerInit(ctx BaseModuleContext) {}
93
94func (object *objectLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
95	if ctx.useVndk() && ctx.toolchain().Bionic() {
96		// Needed for VNDK builds where bionic headers aren't automatically added.
97		deps.LateSharedLibs = append(deps.LateSharedLibs, "libc")
98	}
99
100	deps.HeaderLibs = append(deps.HeaderLibs, object.Properties.Header_libs...)
101	deps.ObjFiles = append(deps.ObjFiles, object.Properties.Objs...)
102	return deps
103}
104
105func (object *objectLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags {
106	flags.Global.LdFlags = append(flags.Global.LdFlags, ctx.toolchain().ToolchainClangLdflags())
107
108	if lds := android.OptionalPathForModuleSrc(ctx, object.Properties.Linker_script); lds.Valid() {
109		flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-T,"+lds.String())
110		flags.LdFlagsDeps = append(flags.LdFlagsDeps, lds.Path())
111	}
112	return flags
113}
114
115func (object *objectLinker) link(ctx ModuleContext,
116	flags Flags, deps PathDeps, objs Objects) android.Path {
117
118	objs = objs.Append(deps.Objs)
119
120	var outputFile android.Path
121	builderFlags := flagsToBuilderFlags(flags)
122
123	if len(objs.objFiles) == 1 && String(object.Properties.Linker_script) == "" {
124		outputFile = objs.objFiles[0]
125
126		if String(object.Properties.Prefix_symbols) != "" {
127			output := android.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension)
128			TransformBinaryPrefixSymbols(ctx, String(object.Properties.Prefix_symbols), outputFile,
129				builderFlags, output)
130			outputFile = output
131		}
132	} else {
133		output := android.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension)
134		outputFile = output
135
136		if String(object.Properties.Prefix_symbols) != "" {
137			input := android.PathForModuleOut(ctx, "unprefixed", ctx.ModuleName()+objectExtension)
138			TransformBinaryPrefixSymbols(ctx, String(object.Properties.Prefix_symbols), input,
139				builderFlags, output)
140			output = input
141		}
142
143		TransformObjsToObj(ctx, objs.objFiles, builderFlags, output, flags.LdFlagsDeps)
144	}
145
146	ctx.CheckbuildFile(outputFile)
147	return outputFile
148}
149
150func (object *objectLinker) unstrippedOutputFilePath() android.Path {
151	return nil
152}
153
154func (object *objectLinker) nativeCoverage() bool {
155	return true
156}
157
158func (object *objectLinker) coverageOutputFilePath() android.OptionalPath {
159	return android.OptionalPath{}
160}
161
162func (object *objectLinker) object() bool {
163	return true
164}
165