1 // Copyright (C) 2019 The Android Open Source Project 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 sysprop 16 17 import ( 18 "android/soong/android" 19 "android/soong/cc" 20 "android/soong/java" 21 "github.com/google/blueprint" 22 "github.com/google/blueprint/proptools" 23 ) 24 25 type dependencyTag struct { 26 blueprint.BaseDependencyTag 27 name string 28 } 29 30 type syspropLibrary struct { 31 java.SdkLibrary 32 33 commonProperties commonProperties 34 syspropLibraryProperties syspropLibraryProperties 35 } 36 37 type syspropLibraryProperties struct { 38 // Determine who owns this sysprop library. Possible values are 39 // "Platform", "Vendor", or "Odm" 40 Property_owner string 41 42 // list of package names that will be documented and publicized as API 43 Api_packages []string 44 } 45 46 type commonProperties struct { 47 Srcs []string 48 Recovery *bool 49 Recovery_available *bool 50 Vendor_available *bool 51 } 52 53 var ( 54 Bool = proptools.Bool 55 syspropCcTag = dependencyTag{name: "syspropCc"} 56 ) 57 58 func init() { 59 android.RegisterModuleType("sysprop_library", syspropLibraryFactory) 60 } 61 62 func (m *syspropLibrary) CcModuleName() string { 63 return "lib" + m.Name() 64 } 65 66 func (m *syspropLibrary) SyspropJavaModule() *java.SdkLibrary { 67 return &m.SdkLibrary 68 } 69 70 func syspropLibraryFactory() android.Module { 71 m := &syspropLibrary{} 72 73 m.AddProperties( 74 &m.commonProperties, 75 &m.syspropLibraryProperties, 76 ) 77 m.InitSdkLibraryProperties() 78 m.SetNoDist() 79 android.InitAndroidMultiTargetsArchModule(m, android.DeviceSupported, "common") 80 android.AddLoadHook(m, func(ctx android.LoadHookContext) { syspropLibraryHook(ctx, m) }) 81 82 return m 83 } 84 85 func syspropLibraryHook(ctx android.LoadHookContext, m *syspropLibrary) { 86 if len(m.commonProperties.Srcs) == 0 { 87 ctx.PropertyErrorf("srcs", "sysprop_library must specify srcs") 88 } 89 90 if len(m.syspropLibraryProperties.Api_packages) == 0 { 91 ctx.PropertyErrorf("api_packages", "sysprop_library must specify api_packages") 92 } 93 94 socSpecific := ctx.SocSpecific() 95 deviceSpecific := ctx.DeviceSpecific() 96 productSpecific := ctx.ProductSpecific() 97 98 owner := m.syspropLibraryProperties.Property_owner 99 100 switch owner { 101 case "Platform": 102 // Every partition can access platform-defined properties 103 break 104 case "Vendor": 105 // System can't access vendor's properties 106 if !socSpecific && !deviceSpecific && !productSpecific { 107 ctx.ModuleErrorf("None of soc_specific, device_specific, product_specific is true. " + 108 "System can't access sysprop_library owned by Vendor") 109 } 110 case "Odm": 111 // Only vendor can access Odm-defined properties 112 if !socSpecific && !deviceSpecific { 113 ctx.ModuleErrorf("Neither soc_speicifc nor device_specific is true. " + 114 "Odm-defined properties should be accessed only in Vendor or Odm") 115 } 116 default: 117 ctx.PropertyErrorf("property_owner", 118 "Unknown value %s: must be one of Platform, Vendor or Odm", owner) 119 } 120 121 ccProps := struct { 122 Name *string 123 Soc_specific *bool 124 Device_specific *bool 125 Product_specific *bool 126 Sysprop struct { 127 Platform *bool 128 } 129 }{} 130 131 ccProps.Name = proptools.StringPtr(m.CcModuleName()) 132 ccProps.Soc_specific = proptools.BoolPtr(socSpecific) 133 ccProps.Device_specific = proptools.BoolPtr(deviceSpecific) 134 ccProps.Product_specific = proptools.BoolPtr(productSpecific) 135 ccProps.Sysprop.Platform = proptools.BoolPtr(owner == "Platform") 136 137 ctx.CreateModule(android.ModuleFactoryAdaptor(cc.LibraryFactory), &m.commonProperties, &ccProps) 138 } 139