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) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | type objectLinker struct { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 32 | *baseLinker |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 33 | Properties ObjectLinkerProperties |
| 34 | } |
| 35 | |
Pete Bentley | 74c9bba | 2019-08-16 20:25:06 +0100 | [diff] [blame] | 36 | type ObjectLinkerProperties struct { |
| 37 | // list of modules that should only provide headers for this module. |
| 38 | Header_libs []string `android:"arch_variant,variant_prepend"` |
| 39 | |
| 40 | // names of other cc_object modules to link into this module using partial linking |
| 41 | Objs []string `android:"arch_variant"` |
| 42 | |
| 43 | // if set, add an extra objcopy --prefix-symbols= step |
| 44 | Prefix_symbols *string |
| 45 | |
| 46 | // if set, the path to a linker script to pass to ld -r when combining multiple object files. |
| 47 | Linker_script *string `android:"path,arch_variant"` |
| 48 | } |
| 49 | |
Patrice Arruda | baff0ce | 2019-03-26 10:39:49 -0700 | [diff] [blame] | 50 | // cc_object runs the compiler without running the linker. It is rarely |
| 51 | // necessary, but sometimes used to generate .s files from .c files to use as |
| 52 | // input to a cc_genrule module. |
Colin Cross | e40b4ea | 2018-10-02 22:25:58 -0700 | [diff] [blame] | 53 | func ObjectFactory() android.Module { |
Dan Willemsen | 967c6a9 | 2016-11-17 00:30:56 -0800 | [diff] [blame] | 54 | module := newBaseModule(android.HostAndDeviceSupported, android.MultilibBoth) |
Peter Collingbourne | 1c648b8 | 2019-09-26 12:24:45 -0700 | [diff] [blame] | 55 | module.sanitize = &sanitize{} |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 56 | module.linker = &objectLinker{ |
Peter Collingbourne | 1c648b8 | 2019-09-26 12:24:45 -0700 | [diff] [blame] | 57 | baseLinker: NewBaseLinker(module.sanitize), |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 58 | } |
| 59 | module.compiler = NewBaseCompiler() |
Peter Collingbourne | 486e42c | 2018-10-25 10:53:44 -0700 | [diff] [blame] | 60 | |
| 61 | // Clang's address-significance tables are incompatible with ld -r. |
| 62 | module.compiler.appendCflags([]string{"-fno-addrsig"}) |
| 63 | |
Colin Cross | 907769f | 2018-09-27 11:02:39 -0700 | [diff] [blame] | 64 | module.stl = &stl{} |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 65 | return module.Init() |
| 66 | } |
| 67 | |
| 68 | func (object *objectLinker) appendLdflags(flags []string) { |
Jeff Gaston | af3cc2d | 2017-09-27 17:01:44 -0700 | [diff] [blame] | 69 | panic(fmt.Errorf("appendLdflags on objectLinker not supported")) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 72 | func (object *objectLinker) linkerProps() []interface{} { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 73 | return []interface{}{&object.Properties} |
| 74 | } |
| 75 | |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 76 | func (*objectLinker) linkerInit(ctx BaseModuleContext) {} |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 77 | |
Colin Cross | 37047f1 | 2016-12-13 17:06:13 -0800 | [diff] [blame] | 78 | func (object *objectLinker) linkerDeps(ctx DepsContext, deps Deps) Deps { |
Colin Cross | 87dd963 | 2017-11-03 13:31:05 -0700 | [diff] [blame] | 79 | if ctx.useVndk() && ctx.toolchain().Bionic() { |
Dan Willemsen | 0c16293 | 2017-09-18 16:33:37 -0700 | [diff] [blame] | 80 | // Needed for VNDK builds where bionic headers aren't automatically added. |
| 81 | deps.LateSharedLibs = append(deps.LateSharedLibs, "libc") |
| 82 | } |
| 83 | |
Paul Duffin | a37832a | 2019-07-18 12:31:26 +0100 | [diff] [blame] | 84 | deps.HeaderLibs = append(deps.HeaderLibs, object.Properties.Header_libs...) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 85 | deps.ObjFiles = append(deps.ObjFiles, object.Properties.Objs...) |
| 86 | return deps |
| 87 | } |
| 88 | |
Pete Bentley | 74c9bba | 2019-08-16 20:25:06 +0100 | [diff] [blame] | 89 | func (object *objectLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags { |
Dan Willemsen | 8536d6b | 2018-10-07 20:54:34 -0700 | [diff] [blame] | 90 | flags.LdFlags = append(flags.LdFlags, ctx.toolchain().ToolchainClangLdflags()) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 91 | |
Pete Bentley | 74c9bba | 2019-08-16 20:25:06 +0100 | [diff] [blame] | 92 | if lds := android.OptionalPathForModuleSrc(ctx, object.Properties.Linker_script); lds.Valid() { |
| 93 | flags.LdFlags = append(flags.LdFlags, "-Wl,-T,"+lds.String()) |
| 94 | flags.LdFlagsDeps = append(flags.LdFlagsDeps, lds.Path()) |
| 95 | } |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 96 | return flags |
| 97 | } |
| 98 | |
| 99 | func (object *objectLinker) link(ctx ModuleContext, |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 100 | flags Flags, deps PathDeps, objs Objects) android.Path { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 101 | |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 102 | objs = objs.Append(deps.Objs) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 103 | |
| 104 | var outputFile android.Path |
Dan Willemsen | efb1dd9 | 2017-09-18 22:47:20 -0700 | [diff] [blame] | 105 | builderFlags := flagsToBuilderFlags(flags) |
| 106 | |
Pete Bentley | ab65ba9 | 2019-10-18 12:39:56 +0100 | [diff] [blame^] | 107 | if len(objs.objFiles) == 1 && String(object.Properties.Linker_script) == "" { |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 108 | outputFile = objs.objFiles[0] |
Dan Willemsen | efb1dd9 | 2017-09-18 22:47:20 -0700 | [diff] [blame] | 109 | |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 110 | if String(object.Properties.Prefix_symbols) != "" { |
Dan Willemsen | efb1dd9 | 2017-09-18 22:47:20 -0700 | [diff] [blame] | 111 | output := android.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension) |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 112 | TransformBinaryPrefixSymbols(ctx, String(object.Properties.Prefix_symbols), outputFile, |
Dan Willemsen | efb1dd9 | 2017-09-18 22:47:20 -0700 | [diff] [blame] | 113 | builderFlags, output) |
| 114 | outputFile = output |
| 115 | } |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 116 | } else { |
| 117 | output := android.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 118 | outputFile = output |
Dan Willemsen | efb1dd9 | 2017-09-18 22:47:20 -0700 | [diff] [blame] | 119 | |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 120 | if String(object.Properties.Prefix_symbols) != "" { |
Dan Willemsen | efb1dd9 | 2017-09-18 22:47:20 -0700 | [diff] [blame] | 121 | input := android.PathForModuleOut(ctx, "unprefixed", ctx.ModuleName()+objectExtension) |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 122 | TransformBinaryPrefixSymbols(ctx, String(object.Properties.Prefix_symbols), input, |
Dan Willemsen | efb1dd9 | 2017-09-18 22:47:20 -0700 | [diff] [blame] | 123 | builderFlags, output) |
| 124 | output = input |
| 125 | } |
| 126 | |
Dan Willemsen | 724ab5d | 2019-09-19 10:50:18 -0700 | [diff] [blame] | 127 | TransformObjsToObj(ctx, objs.objFiles, builderFlags, output, flags.LdFlagsDeps) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | ctx.CheckbuildFile(outputFile) |
| 131 | return outputFile |
| 132 | } |
Jiyong Park | af6d895 | 2019-01-31 12:21:23 +0900 | [diff] [blame] | 133 | |
| 134 | func (object *objectLinker) unstrippedOutputFilePath() android.Path { |
| 135 | return nil |
| 136 | } |
Pirama Arumuga Nainar | 65c95ff | 2019-03-25 10:21:31 -0700 | [diff] [blame] | 137 | |
| 138 | func (object *objectLinker) nativeCoverage() bool { |
| 139 | return true |
| 140 | } |
Jiyong Park | ee9a98d | 2019-08-09 14:44:36 +0900 | [diff] [blame] | 141 | |
| 142 | func (object *objectLinker) coverageOutputFilePath() android.OptionalPath { |
| 143 | return android.OptionalPath{} |
| 144 | } |