| Paul Duffin | 25ce04b | 2020-01-16 11:47:25 +0000 | [diff] [blame] | 1 | // Copyright 2020 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 | "path/filepath" | 
|  | 19 |  | 
|  | 20 | "android/soong/android" | 
| Martin Stjernholm | 10566a0 | 2020-03-24 01:19:52 +0000 | [diff] [blame] | 21 |  | 
| Paul Duffin | 25ce04b | 2020-01-16 11:47:25 +0000 | [diff] [blame] | 22 | "github.com/google/blueprint" | 
| Martin Stjernholm | 7130fab | 2020-05-28 22:58:01 +0100 | [diff] [blame] | 23 | "github.com/google/blueprint/proptools" | 
| Paul Duffin | 25ce04b | 2020-01-16 11:47:25 +0000 | [diff] [blame] | 24 | ) | 
|  | 25 |  | 
|  | 26 | func init() { | 
|  | 27 | android.RegisterSdkMemberType(ccBinarySdkMemberType) | 
|  | 28 | } | 
|  | 29 |  | 
|  | 30 | var ccBinarySdkMemberType = &binarySdkMemberType{ | 
|  | 31 | SdkMemberTypeBase: android.SdkMemberTypeBase{ | 
| Martin Stjernholm | caa47d7 | 2020-07-11 04:52:24 +0100 | [diff] [blame] | 32 | PropertyName:    "native_binaries", | 
|  | 33 | HostOsDependent: true, | 
| Paul Duffin | 25ce04b | 2020-01-16 11:47:25 +0000 | [diff] [blame] | 34 | }, | 
|  | 35 | } | 
|  | 36 |  | 
|  | 37 | type binarySdkMemberType struct { | 
|  | 38 | android.SdkMemberTypeBase | 
|  | 39 | } | 
|  | 40 |  | 
|  | 41 | func (mt *binarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) { | 
|  | 42 | targets := mctx.MultiTargets() | 
| Colin Cross | 3146c5c | 2020-09-30 15:34:40 -0700 | [diff] [blame] | 43 | for _, bin := range names { | 
| Paul Duffin | 25ce04b | 2020-01-16 11:47:25 +0000 | [diff] [blame] | 44 | for _, target := range targets { | 
| Colin Cross | 4250733 | 2020-08-21 16:15:23 -0700 | [diff] [blame] | 45 | variations := target.Variations() | 
|  | 46 | if mctx.Device() { | 
|  | 47 | variations = append(variations, | 
| Colin Cross | 3146c5c | 2020-09-30 15:34:40 -0700 | [diff] [blame] | 48 | blueprint.Variation{Mutator: "image", Variation: android.CoreVariation}) | 
| Colin Cross | 4250733 | 2020-08-21 16:15:23 -0700 | [diff] [blame] | 49 | } | 
| Colin Cross | 3146c5c | 2020-09-30 15:34:40 -0700 | [diff] [blame] | 50 | mctx.AddFarVariationDependencies(variations, dependencyTag, bin) | 
| Paul Duffin | 25ce04b | 2020-01-16 11:47:25 +0000 | [diff] [blame] | 51 | } | 
|  | 52 | } | 
|  | 53 | } | 
|  | 54 |  | 
|  | 55 | func (mt *binarySdkMemberType) IsInstance(module android.Module) bool { | 
|  | 56 | // Check the module to see if it can be used with this module type. | 
|  | 57 | if m, ok := module.(*Module); ok { | 
|  | 58 | for _, allowableMemberType := range m.sdkMemberTypes { | 
|  | 59 | if allowableMemberType == mt { | 
|  | 60 | return true | 
|  | 61 | } | 
|  | 62 | } | 
|  | 63 | } | 
|  | 64 |  | 
|  | 65 | return false | 
|  | 66 | } | 
|  | 67 |  | 
| Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame] | 68 | func (mt *binarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule { | 
| Martin Stjernholm | 7130fab | 2020-05-28 22:58:01 +0100 | [diff] [blame] | 69 | pbm := ctx.SnapshotBuilder().AddPrebuiltModule(member, "cc_prebuilt_binary") | 
|  | 70 |  | 
|  | 71 | ccModule := member.Variants()[0].(*Module) | 
|  | 72 |  | 
|  | 73 | if stl := ccModule.stl.Properties.Stl; stl != nil { | 
|  | 74 | pbm.AddProperty("stl", proptools.String(stl)) | 
|  | 75 | } | 
|  | 76 |  | 
|  | 77 | return pbm | 
| Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 78 | } | 
| Paul Duffin | 13ad94f | 2020-02-19 16:19:27 +0000 | [diff] [blame] | 79 |  | 
| Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 80 | func (mt *binarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties { | 
|  | 81 | return &nativeBinaryInfoProperties{} | 
| Paul Duffin | 25ce04b | 2020-01-16 11:47:25 +0000 | [diff] [blame] | 82 | } | 
|  | 83 |  | 
|  | 84 | const ( | 
|  | 85 | nativeBinaryDir = "bin" | 
|  | 86 | ) | 
|  | 87 |  | 
|  | 88 | // path to the native binary. Relative to <sdk_root>/<api_dir> | 
|  | 89 | func nativeBinaryPathFor(lib nativeBinaryInfoProperties) string { | 
| Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 90 | return filepath.Join(lib.OsPrefix(), lib.archType, | 
| Paul Duffin | 25ce04b | 2020-01-16 11:47:25 +0000 | [diff] [blame] | 91 | nativeBinaryDir, lib.outputFile.Base()) | 
|  | 92 | } | 
|  | 93 |  | 
|  | 94 | // nativeBinaryInfoProperties represents properties of a native binary | 
|  | 95 | // | 
|  | 96 | // The exported (capitalized) fields will be examined and may be changed during common value extraction. | 
|  | 97 | // The unexported fields will be left untouched. | 
|  | 98 | type nativeBinaryInfoProperties struct { | 
| Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 99 | android.SdkMemberPropertiesBase | 
| Paul Duffin | 25ce04b | 2020-01-16 11:47:25 +0000 | [diff] [blame] | 100 |  | 
|  | 101 | // archType is not exported as if set (to a non default value) it is always arch specific. | 
|  | 102 | // This is "" for common properties. | 
|  | 103 | archType string | 
|  | 104 |  | 
|  | 105 | // outputFile is not exported as it is always arch specific. | 
|  | 106 | outputFile android.Path | 
| Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 107 |  | 
|  | 108 | // The set of shared libraries | 
|  | 109 | // | 
|  | 110 | // This field is exported as its contents may not be arch specific. | 
|  | 111 | SharedLibs []string | 
|  | 112 |  | 
|  | 113 | // The set of system shared libraries | 
|  | 114 | // | 
|  | 115 | // This field is exported as its contents may not be arch specific. | 
|  | 116 | SystemSharedLibs []string | 
| Martin Stjernholm | 7130fab | 2020-05-28 22:58:01 +0100 | [diff] [blame] | 117 |  | 
|  | 118 | // Arch specific flags. | 
|  | 119 | StaticExecutable bool | 
|  | 120 | Nocrt            bool | 
| Paul Duffin | 25ce04b | 2020-01-16 11:47:25 +0000 | [diff] [blame] | 121 | } | 
|  | 122 |  | 
| Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame] | 123 | func (p *nativeBinaryInfoProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) { | 
| Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 124 | ccModule := variant.(*Module) | 
|  | 125 |  | 
|  | 126 | p.archType = ccModule.Target().Arch.ArchType.String() | 
| Paul Duffin | 712993c | 2020-05-05 14:11:57 +0100 | [diff] [blame] | 127 | p.outputFile = getRequiredMemberOutputFile(ctx, ccModule) | 
| Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 128 |  | 
| Martin Stjernholm | 7130fab | 2020-05-28 22:58:01 +0100 | [diff] [blame] | 129 | binaryLinker := ccModule.linker.(*binaryDecorator) | 
|  | 130 | p.StaticExecutable = binaryLinker.static() | 
|  | 131 | p.Nocrt = Bool(binaryLinker.baseLinker.Properties.Nocrt) | 
|  | 132 |  | 
| Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 133 | if ccModule.linker != nil { | 
|  | 134 | specifiedDeps := specifiedDeps{} | 
|  | 135 | specifiedDeps = ccModule.linker.linkerSpecifiedDeps(specifiedDeps) | 
|  | 136 |  | 
|  | 137 | p.SharedLibs = specifiedDeps.sharedLibs | 
|  | 138 | p.SystemSharedLibs = specifiedDeps.systemSharedLibs | 
|  | 139 | } | 
| Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 140 | } | 
|  | 141 |  | 
| Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame] | 142 | func (p *nativeBinaryInfoProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) { | 
| Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame] | 143 | builder := ctx.SnapshotBuilder() | 
| Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 144 | if p.outputFile != nil { | 
|  | 145 | propertySet.AddProperty("srcs", []string{nativeBinaryPathFor(*p)}) | 
|  | 146 |  | 
|  | 147 | builder.CopyToSnapshot(p.outputFile, nativeBinaryPathFor(*p)) | 
|  | 148 | } | 
| Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 149 |  | 
|  | 150 | if len(p.SharedLibs) > 0 { | 
|  | 151 | propertySet.AddPropertyWithTag("shared_libs", p.SharedLibs, builder.SdkMemberReferencePropertyTag(false)) | 
|  | 152 | } | 
|  | 153 |  | 
| Martin Stjernholm | 10566a0 | 2020-03-24 01:19:52 +0000 | [diff] [blame] | 154 | // SystemSharedLibs needs to be propagated if it's a list, even if it's empty, | 
|  | 155 | // so check for non-nil instead of nonzero length. | 
|  | 156 | if p.SystemSharedLibs != nil { | 
| Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 157 | propertySet.AddPropertyWithTag("system_shared_libs", p.SystemSharedLibs, builder.SdkMemberReferencePropertyTag(false)) | 
|  | 158 | } | 
| Martin Stjernholm | 7130fab | 2020-05-28 22:58:01 +0100 | [diff] [blame] | 159 |  | 
|  | 160 | if p.StaticExecutable { | 
|  | 161 | propertySet.AddProperty("static_executable", p.StaticExecutable) | 
|  | 162 | } | 
|  | 163 | if p.Nocrt { | 
|  | 164 | propertySet.AddProperty("nocrt", p.Nocrt) | 
|  | 165 | } | 
| Paul Duffin | 25ce04b | 2020-01-16 11:47:25 +0000 | [diff] [blame] | 166 | } |