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 | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame^] | 66 | func (mt *binarySdkMemberType) AddPrebuiltModule(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) android.BpModule { |
Paul Duffin | 25ce04b | 2020-01-16 11:47:25 +0000 | [diff] [blame] | 67 | pbm := builder.AddPrebuiltModule(member, "cc_prebuilt_binary") |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame^] | 68 | return pbm |
| 69 | } |
Paul Duffin | 13ad94f | 2020-02-19 16:19:27 +0000 | [diff] [blame] | 70 | |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame^] | 71 | func (mt *binarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties { |
| 72 | return &nativeBinaryInfoProperties{} |
Paul Duffin | 25ce04b | 2020-01-16 11:47:25 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | const ( |
| 76 | nativeBinaryDir = "bin" |
| 77 | ) |
| 78 | |
| 79 | // path to the native binary. Relative to <sdk_root>/<api_dir> |
| 80 | func nativeBinaryPathFor(lib nativeBinaryInfoProperties) string { |
| 81 | return filepath.Join(lib.archType, |
| 82 | nativeBinaryDir, lib.outputFile.Base()) |
| 83 | } |
| 84 | |
| 85 | // nativeBinaryInfoProperties represents properties of a native binary |
| 86 | // |
| 87 | // The exported (capitalized) fields will be examined and may be changed during common value extraction. |
| 88 | // The unexported fields will be left untouched. |
| 89 | type nativeBinaryInfoProperties struct { |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame^] | 90 | android.SdkMemberPropertiesBase |
Paul Duffin | 25ce04b | 2020-01-16 11:47:25 +0000 | [diff] [blame] | 91 | |
| 92 | // archType is not exported as if set (to a non default value) it is always arch specific. |
| 93 | // This is "" for common properties. |
| 94 | archType string |
| 95 | |
| 96 | // outputFile is not exported as it is always arch specific. |
| 97 | outputFile android.Path |
| 98 | } |
| 99 | |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame^] | 100 | func (p *nativeBinaryInfoProperties) PopulateFromVariant(variant android.SdkAware) { |
| 101 | ccModule := variant.(*Module) |
| 102 | |
| 103 | p.archType = ccModule.Target().Arch.ArchType.String() |
| 104 | p.outputFile = ccModule.OutputFile().Path() |
| 105 | } |
| 106 | |
| 107 | func (p *nativeBinaryInfoProperties) AddToPropertySet(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, propertySet android.BpPropertySet) { |
| 108 | if p.Compile_multilib != "" { |
| 109 | propertySet.AddProperty("compile_multilib", p.Compile_multilib) |
| 110 | } |
| 111 | |
| 112 | if p.outputFile != nil { |
| 113 | propertySet.AddProperty("srcs", []string{nativeBinaryPathFor(*p)}) |
| 114 | |
| 115 | builder.CopyToSnapshot(p.outputFile, nativeBinaryPathFor(*p)) |
| 116 | } |
Paul Duffin | 25ce04b | 2020-01-16 11:47:25 +0000 | [diff] [blame] | 117 | } |