blob: 9fa0ac90f6d631620fcc512d90e887100926d2f6 [file] [log] [blame]
Colin Cross4d9c2d12016-07-29 12:48:20 -07001// 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
Colin Cross4d9c2d12016-07-29 12:48:20 -070020 "android/soong/android"
21)
22
23//
24// Objects (for crt*.o)
25//
26
27func init() {
Colin Crosse40b4ea2018-10-02 22:25:58 -070028 android.RegisterModuleType("cc_object", ObjectFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -070029}
30
31type objectLinker struct {
Colin Crossb916a382016-07-29 17:28:03 -070032 *baseLinker
Colin Cross4d9c2d12016-07-29 12:48:20 -070033 Properties ObjectLinkerProperties
34}
35
Patrice Arrudabaff0ce2019-03-26 10:39:49 -070036// cc_object runs the compiler without running the linker. It is rarely
37// necessary, but sometimes used to generate .s files from .c files to use as
38// input to a cc_genrule module.
Colin Crosse40b4ea2018-10-02 22:25:58 -070039func ObjectFactory() android.Module {
Dan Willemsen967c6a92016-11-17 00:30:56 -080040 module := newBaseModule(android.HostAndDeviceSupported, android.MultilibBoth)
Colin Crossb916a382016-07-29 17:28:03 -070041 module.linker = &objectLinker{
Dan Albert61f32122018-07-26 14:00:24 -070042 baseLinker: NewBaseLinker(nil),
Colin Crossb916a382016-07-29 17:28:03 -070043 }
44 module.compiler = NewBaseCompiler()
Peter Collingbourne486e42c2018-10-25 10:53:44 -070045
46 // Clang's address-significance tables are incompatible with ld -r.
47 module.compiler.appendCflags([]string{"-fno-addrsig"})
48
Colin Cross907769f2018-09-27 11:02:39 -070049 module.stl = &stl{}
Colin Cross4d9c2d12016-07-29 12:48:20 -070050 return module.Init()
51}
52
53func (object *objectLinker) appendLdflags(flags []string) {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -070054 panic(fmt.Errorf("appendLdflags on objectLinker not supported"))
Colin Cross4d9c2d12016-07-29 12:48:20 -070055}
56
Colin Cross42742b82016-08-01 13:20:05 -070057func (object *objectLinker) linkerProps() []interface{} {
Colin Cross4d9c2d12016-07-29 12:48:20 -070058 return []interface{}{&object.Properties}
59}
60
Colin Cross42742b82016-08-01 13:20:05 -070061func (*objectLinker) linkerInit(ctx BaseModuleContext) {}
Colin Cross4d9c2d12016-07-29 12:48:20 -070062
Colin Cross37047f12016-12-13 17:06:13 -080063func (object *objectLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Cross87dd9632017-11-03 13:31:05 -070064 if ctx.useVndk() && ctx.toolchain().Bionic() {
Dan Willemsen0c162932017-09-18 16:33:37 -070065 // Needed for VNDK builds where bionic headers aren't automatically added.
66 deps.LateSharedLibs = append(deps.LateSharedLibs, "libc")
67 }
68
Paul Duffina37832a2019-07-18 12:31:26 +010069 deps.HeaderLibs = append(deps.HeaderLibs, object.Properties.Header_libs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -070070 deps.ObjFiles = append(deps.ObjFiles, object.Properties.Objs...)
71 return deps
72}
73
Colin Cross42742b82016-08-01 13:20:05 -070074func (*objectLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Dan Willemsen8536d6b2018-10-07 20:54:34 -070075 flags.LdFlags = append(flags.LdFlags, ctx.toolchain().ToolchainClangLdflags())
Colin Cross4d9c2d12016-07-29 12:48:20 -070076
77 return flags
78}
79
80func (object *objectLinker) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -070081 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -070082
Dan Willemsen5cb580f2016-09-26 17:33:01 -070083 objs = objs.Append(deps.Objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -070084
85 var outputFile android.Path
Dan Willemsenefb1dd92017-09-18 22:47:20 -070086 builderFlags := flagsToBuilderFlags(flags)
87
Dan Willemsen5cb580f2016-09-26 17:33:01 -070088 if len(objs.objFiles) == 1 {
89 outputFile = objs.objFiles[0]
Dan Willemsenefb1dd92017-09-18 22:47:20 -070090
Nan Zhang0007d812017-11-07 10:57:05 -080091 if String(object.Properties.Prefix_symbols) != "" {
Dan Willemsenefb1dd92017-09-18 22:47:20 -070092 output := android.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension)
Nan Zhang0007d812017-11-07 10:57:05 -080093 TransformBinaryPrefixSymbols(ctx, String(object.Properties.Prefix_symbols), outputFile,
Dan Willemsenefb1dd92017-09-18 22:47:20 -070094 builderFlags, output)
95 outputFile = output
96 }
Colin Cross4d9c2d12016-07-29 12:48:20 -070097 } else {
98 output := android.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension)
Colin Cross4d9c2d12016-07-29 12:48:20 -070099 outputFile = output
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700100
Nan Zhang0007d812017-11-07 10:57:05 -0800101 if String(object.Properties.Prefix_symbols) != "" {
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700102 input := android.PathForModuleOut(ctx, "unprefixed", ctx.ModuleName()+objectExtension)
Nan Zhang0007d812017-11-07 10:57:05 -0800103 TransformBinaryPrefixSymbols(ctx, String(object.Properties.Prefix_symbols), input,
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700104 builderFlags, output)
105 output = input
106 }
107
108 TransformObjsToObj(ctx, objs.objFiles, builderFlags, output)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700109 }
110
111 ctx.CheckbuildFile(outputFile)
112 return outputFile
113}
Jiyong Parkaf6d8952019-01-31 12:21:23 +0900114
115func (object *objectLinker) unstrippedOutputFilePath() android.Path {
116 return nil
117}
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700118
119func (object *objectLinker) nativeCoverage() bool {
120 return true
121}