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" |
| 21 | "github.com/google/blueprint" |
| 22 | ) |
| 23 | |
| 24 | func init() { |
| 25 | android.RegisterSdkMemberType(ccBinarySdkMemberType) |
| 26 | } |
| 27 | |
| 28 | var ccBinarySdkMemberType = &binarySdkMemberType{ |
| 29 | SdkMemberTypeBase: android.SdkMemberTypeBase{ |
| 30 | PropertyName: "native_binaries", |
| 31 | }, |
| 32 | } |
| 33 | |
| 34 | type binarySdkMemberType struct { |
| 35 | android.SdkMemberTypeBase |
| 36 | } |
| 37 | |
| 38 | func (mt *binarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) { |
| 39 | targets := mctx.MultiTargets() |
| 40 | for _, lib := range names { |
| 41 | for _, target := range targets { |
| 42 | name, version := StubsLibNameAndVersion(lib) |
| 43 | if version == "" { |
| 44 | version = LatestStubsVersionFor(mctx.Config(), name) |
| 45 | } |
| 46 | mctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{ |
| 47 | {Mutator: "version", Variation: version}, |
| 48 | }...), dependencyTag, name) |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | func (mt *binarySdkMemberType) IsInstance(module android.Module) bool { |
| 54 | // Check the module to see if it can be used with this module type. |
| 55 | if m, ok := module.(*Module); ok { |
| 56 | for _, allowableMemberType := range m.sdkMemberTypes { |
| 57 | if allowableMemberType == mt { |
| 58 | return true |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | return false |
| 64 | } |
| 65 | |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame] | 66 | func (mt *binarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule { |
| 67 | return ctx.SnapshotBuilder().AddPrebuiltModule(member, "cc_prebuilt_binary") |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 68 | } |
Paul Duffin | 13ad94f | 2020-02-19 16:19:27 +0000 | [diff] [blame] | 69 | |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 70 | func (mt *binarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties { |
| 71 | return &nativeBinaryInfoProperties{} |
Paul Duffin | 25ce04b | 2020-01-16 11:47:25 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | const ( |
| 75 | nativeBinaryDir = "bin" |
| 76 | ) |
| 77 | |
| 78 | // path to the native binary. Relative to <sdk_root>/<api_dir> |
| 79 | func nativeBinaryPathFor(lib nativeBinaryInfoProperties) string { |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 80 | return filepath.Join(lib.OsPrefix(), lib.archType, |
Paul Duffin | 25ce04b | 2020-01-16 11:47:25 +0000 | [diff] [blame] | 81 | nativeBinaryDir, lib.outputFile.Base()) |
| 82 | } |
| 83 | |
| 84 | // nativeBinaryInfoProperties represents properties of a native binary |
| 85 | // |
| 86 | // The exported (capitalized) fields will be examined and may be changed during common value extraction. |
| 87 | // The unexported fields will be left untouched. |
| 88 | type nativeBinaryInfoProperties struct { |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 89 | android.SdkMemberPropertiesBase |
Paul Duffin | 25ce04b | 2020-01-16 11:47:25 +0000 | [diff] [blame] | 90 | |
| 91 | // archType is not exported as if set (to a non default value) it is always arch specific. |
| 92 | // This is "" for common properties. |
| 93 | archType string |
| 94 | |
| 95 | // outputFile is not exported as it is always arch specific. |
| 96 | outputFile android.Path |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 97 | |
| 98 | // The set of shared libraries |
| 99 | // |
| 100 | // This field is exported as its contents may not be arch specific. |
| 101 | SharedLibs []string |
| 102 | |
| 103 | // The set of system shared libraries |
| 104 | // |
| 105 | // This field is exported as its contents may not be arch specific. |
| 106 | SystemSharedLibs []string |
Paul Duffin | 25ce04b | 2020-01-16 11:47:25 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame] | 109 | func (p *nativeBinaryInfoProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) { |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 110 | ccModule := variant.(*Module) |
| 111 | |
| 112 | p.archType = ccModule.Target().Arch.ArchType.String() |
| 113 | p.outputFile = ccModule.OutputFile().Path() |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 114 | |
| 115 | if ccModule.linker != nil { |
| 116 | specifiedDeps := specifiedDeps{} |
| 117 | specifiedDeps = ccModule.linker.linkerSpecifiedDeps(specifiedDeps) |
| 118 | |
| 119 | p.SharedLibs = specifiedDeps.sharedLibs |
| 120 | p.SystemSharedLibs = specifiedDeps.systemSharedLibs |
| 121 | } |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame] | 124 | func (p *nativeBinaryInfoProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) { |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 125 | if p.Compile_multilib != "" { |
| 126 | propertySet.AddProperty("compile_multilib", p.Compile_multilib) |
| 127 | } |
| 128 | |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame] | 129 | builder := ctx.SnapshotBuilder() |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 130 | if p.outputFile != nil { |
| 131 | propertySet.AddProperty("srcs", []string{nativeBinaryPathFor(*p)}) |
| 132 | |
| 133 | builder.CopyToSnapshot(p.outputFile, nativeBinaryPathFor(*p)) |
| 134 | } |
Paul Duffin | 13f0271 | 2020-03-06 12:30:43 +0000 | [diff] [blame] | 135 | |
| 136 | if len(p.SharedLibs) > 0 { |
| 137 | propertySet.AddPropertyWithTag("shared_libs", p.SharedLibs, builder.SdkMemberReferencePropertyTag(false)) |
| 138 | } |
| 139 | |
| 140 | if len(p.SystemSharedLibs) > 0 { |
| 141 | propertySet.AddPropertyWithTag("system_shared_libs", p.SystemSharedLibs, builder.SdkMemberReferencePropertyTag(false)) |
| 142 | } |
Paul Duffin | 25ce04b | 2020-01-16 11:47:25 +0000 | [diff] [blame] | 143 | } |