Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 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 | |
| 15 | package cc |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 20 | "android/soong/android" |
| 21 | ) |
| 22 | |
| 23 | // |
| 24 | // Objects (for crt*.o) |
| 25 | // |
| 26 | |
| 27 | func init() { |
Colin Cross | e40b4ea | 2018-10-02 22:25:58 -0700 | [diff] [blame] | 28 | android.RegisterModuleType("cc_object", ObjectFactory) |
Martin Stjernholm | cd07bce | 2020-03-10 22:37:59 +0000 | [diff] [blame] | 29 | android.RegisterSdkMemberType(ccObjectSdkMemberType) |
| 30 | } |
| 31 | |
| 32 | var ccObjectSdkMemberType = &librarySdkMemberType{ |
| 33 | SdkMemberTypeBase: android.SdkMemberTypeBase{ |
| 34 | PropertyName: "native_objects", |
| 35 | SupportsSdk: true, |
| 36 | }, |
| 37 | prebuiltModuleType: "cc_prebuilt_object", |
| 38 | linkTypes: nil, |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | type objectLinker struct { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 42 | *baseLinker |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 43 | Properties ObjectLinkerProperties |
| 44 | } |
| 45 | |
Pete Bentley | 74c9bba | 2019-08-16 20:25:06 +0100 | [diff] [blame] | 46 | type 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 | |
Martin Stjernholm | 0b92ac8 | 2020-03-11 21:45:49 +0000 | [diff] [blame] | 60 | func newObject() *Module { |
| 61 | module := newBaseModule(android.HostAndDeviceSupported, android.MultilibBoth) |
| 62 | module.sanitize = &sanitize{} |
| 63 | module.stl = &stl{} |
| 64 | return module |
| 65 | } |
| 66 | |
Patrice Arruda | baff0ce | 2019-03-26 10:39:49 -0700 | [diff] [blame] | 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. |
Colin Cross | e40b4ea | 2018-10-02 22:25:58 -0700 | [diff] [blame] | 70 | func ObjectFactory() android.Module { |
Martin Stjernholm | 0b92ac8 | 2020-03-11 21:45:49 +0000 | [diff] [blame] | 71 | module := newObject() |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 72 | module.linker = &objectLinker{ |
Peter Collingbourne | 1c648b8 | 2019-09-26 12:24:45 -0700 | [diff] [blame] | 73 | baseLinker: NewBaseLinker(module.sanitize), |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 74 | } |
| 75 | module.compiler = NewBaseCompiler() |
Peter Collingbourne | 486e42c | 2018-10-25 10:53:44 -0700 | [diff] [blame] | 76 | |
| 77 | // Clang's address-significance tables are incompatible with ld -r. |
| 78 | module.compiler.appendCflags([]string{"-fno-addrsig"}) |
| 79 | |
Martin Stjernholm | cd07bce | 2020-03-10 22:37:59 +0000 | [diff] [blame] | 80 | module.sdkMemberTypes = []android.SdkMemberType{ccObjectSdkMemberType} |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 81 | return module.Init() |
| 82 | } |
| 83 | |
| 84 | func (object *objectLinker) appendLdflags(flags []string) { |
Jeff Gaston | af3cc2d | 2017-09-27 17:01:44 -0700 | [diff] [blame] | 85 | panic(fmt.Errorf("appendLdflags on objectLinker not supported")) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 88 | func (object *objectLinker) linkerProps() []interface{} { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 89 | return []interface{}{&object.Properties} |
| 90 | } |
| 91 | |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 92 | func (*objectLinker) linkerInit(ctx BaseModuleContext) {} |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 93 | |
Colin Cross | 37047f1 | 2016-12-13 17:06:13 -0800 | [diff] [blame] | 94 | func (object *objectLinker) linkerDeps(ctx DepsContext, deps Deps) Deps { |
Colin Cross | 87dd963 | 2017-11-03 13:31:05 -0700 | [diff] [blame] | 95 | if ctx.useVndk() && ctx.toolchain().Bionic() { |
Dan Willemsen | 0c16293 | 2017-09-18 16:33:37 -0700 | [diff] [blame] | 96 | // Needed for VNDK builds where bionic headers aren't automatically added. |
| 97 | deps.LateSharedLibs = append(deps.LateSharedLibs, "libc") |
| 98 | } |
| 99 | |
Paul Duffin | a37832a | 2019-07-18 12:31:26 +0100 | [diff] [blame] | 100 | deps.HeaderLibs = append(deps.HeaderLibs, object.Properties.Header_libs...) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 101 | deps.ObjFiles = append(deps.ObjFiles, object.Properties.Objs...) |
| 102 | return deps |
| 103 | } |
| 104 | |
Pete Bentley | 74c9bba | 2019-08-16 20:25:06 +0100 | [diff] [blame] | 105 | func (object *objectLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags { |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 106 | flags.Global.LdFlags = append(flags.Global.LdFlags, ctx.toolchain().ToolchainClangLdflags()) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 107 | |
Pete Bentley | 74c9bba | 2019-08-16 20:25:06 +0100 | [diff] [blame] | 108 | if lds := android.OptionalPathForModuleSrc(ctx, object.Properties.Linker_script); lds.Valid() { |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 109 | flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-T,"+lds.String()) |
Pete Bentley | 74c9bba | 2019-08-16 20:25:06 +0100 | [diff] [blame] | 110 | flags.LdFlagsDeps = append(flags.LdFlagsDeps, lds.Path()) |
| 111 | } |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 112 | return flags |
| 113 | } |
| 114 | |
| 115 | func (object *objectLinker) link(ctx ModuleContext, |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 116 | flags Flags, deps PathDeps, objs Objects) android.Path { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 117 | |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 118 | objs = objs.Append(deps.Objs) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 119 | |
| 120 | var outputFile android.Path |
Dan Willemsen | efb1dd9 | 2017-09-18 22:47:20 -0700 | [diff] [blame] | 121 | builderFlags := flagsToBuilderFlags(flags) |
| 122 | |
Pete Bentley | ab65ba9 | 2019-10-18 12:39:56 +0100 | [diff] [blame] | 123 | if len(objs.objFiles) == 1 && String(object.Properties.Linker_script) == "" { |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 124 | outputFile = objs.objFiles[0] |
Dan Willemsen | efb1dd9 | 2017-09-18 22:47:20 -0700 | [diff] [blame] | 125 | |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 126 | if String(object.Properties.Prefix_symbols) != "" { |
Dan Willemsen | efb1dd9 | 2017-09-18 22:47:20 -0700 | [diff] [blame] | 127 | output := android.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension) |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 128 | TransformBinaryPrefixSymbols(ctx, String(object.Properties.Prefix_symbols), outputFile, |
Dan Willemsen | efb1dd9 | 2017-09-18 22:47:20 -0700 | [diff] [blame] | 129 | builderFlags, output) |
| 130 | outputFile = output |
| 131 | } |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 132 | } else { |
| 133 | output := android.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 134 | outputFile = output |
Dan Willemsen | efb1dd9 | 2017-09-18 22:47:20 -0700 | [diff] [blame] | 135 | |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 136 | if String(object.Properties.Prefix_symbols) != "" { |
Dan Willemsen | efb1dd9 | 2017-09-18 22:47:20 -0700 | [diff] [blame] | 137 | input := android.PathForModuleOut(ctx, "unprefixed", ctx.ModuleName()+objectExtension) |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 138 | TransformBinaryPrefixSymbols(ctx, String(object.Properties.Prefix_symbols), input, |
Dan Willemsen | efb1dd9 | 2017-09-18 22:47:20 -0700 | [diff] [blame] | 139 | builderFlags, output) |
| 140 | output = input |
| 141 | } |
| 142 | |
Dan Willemsen | 724ab5d | 2019-09-19 10:50:18 -0700 | [diff] [blame] | 143 | TransformObjsToObj(ctx, objs.objFiles, builderFlags, output, flags.LdFlagsDeps) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | ctx.CheckbuildFile(outputFile) |
| 147 | return outputFile |
| 148 | } |
Jiyong Park | af6d895 | 2019-01-31 12:21:23 +0900 | [diff] [blame] | 149 | |
| 150 | func (object *objectLinker) unstrippedOutputFilePath() android.Path { |
| 151 | return nil |
| 152 | } |
Pirama Arumuga Nainar | 65c95ff | 2019-03-25 10:21:31 -0700 | [diff] [blame] | 153 | |
| 154 | func (object *objectLinker) nativeCoverage() bool { |
| 155 | return true |
| 156 | } |
Jiyong Park | ee9a98d | 2019-08-09 14:44:36 +0900 | [diff] [blame] | 157 | |
| 158 | func (object *objectLinker) coverageOutputFilePath() android.OptionalPath { |
| 159 | return android.OptionalPath{} |
| 160 | } |
Inseob Kim | 1042d29 | 2020-06-01 23:23:05 +0900 | [diff] [blame^] | 161 | |
| 162 | func (object *objectLinker) object() bool { |
| 163 | return true |
| 164 | } |