sysprop_library.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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. // sysprop package defines a module named sysprop_library that can implement sysprop as API
  15. // See https://source.android.com/devices/architecture/sysprops-apis for details
  16. package sysprop
  17. import (
  18. "fmt"
  19. "io"
  20. "os"
  21. "path"
  22. "sync"
  23. "github.com/google/blueprint"
  24. "github.com/google/blueprint/proptools"
  25. "android/soong/android"
  26. "android/soong/cc"
  27. "android/soong/java"
  28. )
  29. type dependencyTag struct {
  30. blueprint.BaseDependencyTag
  31. name string
  32. }
  33. type syspropGenProperties struct {
  34. Srcs []string `android:"path"`
  35. Scope string
  36. Name *string
  37. Check_api *string
  38. }
  39. type syspropJavaGenRule struct {
  40. android.ModuleBase
  41. properties syspropGenProperties
  42. genSrcjars android.Paths
  43. }
  44. var _ android.OutputFileProducer = (*syspropJavaGenRule)(nil)
  45. var (
  46. syspropJava = pctx.AndroidStaticRule("syspropJava",
  47. blueprint.RuleParams{
  48. Command: `rm -rf $out.tmp && mkdir -p $out.tmp && ` +
  49. `$syspropJavaCmd --scope $scope --java-output-dir $out.tmp $in && ` +
  50. `$soongZipCmd -jar -o $out -C $out.tmp -D $out.tmp && rm -rf $out.tmp`,
  51. CommandDeps: []string{
  52. "$syspropJavaCmd",
  53. "$soongZipCmd",
  54. },
  55. }, "scope")
  56. )
  57. func init() {
  58. pctx.HostBinToolVariable("soongZipCmd", "soong_zip")
  59. pctx.HostBinToolVariable("syspropJavaCmd", "sysprop_java")
  60. }
  61. // syspropJavaGenRule module generates srcjar containing generated java APIs.
  62. // It also depends on check api rule, so api check has to pass to use sysprop_library.
  63. func (g *syspropJavaGenRule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
  64. var checkApiFileTimeStamp android.WritablePath
  65. ctx.VisitDirectDeps(func(dep android.Module) {
  66. if m, ok := dep.(*syspropLibrary); ok {
  67. checkApiFileTimeStamp = m.checkApiFileTimeStamp
  68. }
  69. })
  70. for _, syspropFile := range android.PathsForModuleSrc(ctx, g.properties.Srcs) {
  71. srcJarFile := android.GenPathWithExt(ctx, "sysprop", syspropFile, "srcjar")
  72. ctx.Build(pctx, android.BuildParams{
  73. Rule: syspropJava,
  74. Description: "sysprop_java " + syspropFile.Rel(),
  75. Output: srcJarFile,
  76. Input: syspropFile,
  77. Implicit: checkApiFileTimeStamp,
  78. Args: map[string]string{
  79. "scope": g.properties.Scope,
  80. },
  81. })
  82. g.genSrcjars = append(g.genSrcjars, srcJarFile)
  83. }
  84. }
  85. func (g *syspropJavaGenRule) DepsMutator(ctx android.BottomUpMutatorContext) {
  86. // Add a dependency from the stubs to sysprop library so that the generator rule can depend on
  87. // the check API rule of the sysprop library.
  88. ctx.AddFarVariationDependencies(nil, nil, proptools.String(g.properties.Check_api))
  89. }
  90. func (g *syspropJavaGenRule) OutputFiles(tag string) (android.Paths, error) {
  91. switch tag {
  92. case "":
  93. return g.genSrcjars, nil
  94. default:
  95. return nil, fmt.Errorf("unsupported module reference tag %q", tag)
  96. }
  97. }
  98. func syspropJavaGenFactory() android.Module {
  99. g := &syspropJavaGenRule{}
  100. g.AddProperties(&g.properties)
  101. android.InitAndroidModule(g)
  102. return g
  103. }
  104. type syspropLibrary struct {
  105. android.ModuleBase
  106. android.ApexModuleBase
  107. properties syspropLibraryProperties
  108. checkApiFileTimeStamp android.WritablePath
  109. latestApiFile android.OptionalPath
  110. currentApiFile android.OptionalPath
  111. dumpedApiFile android.WritablePath
  112. }
  113. type syspropLibraryProperties struct {
  114. // Determine who owns this sysprop library. Possible values are
  115. // "Platform", "Vendor", or "Odm"
  116. Property_owner string
  117. // list of package names that will be documented and publicized as API
  118. Api_packages []string
  119. // If set to true, allow this module to be dexed and installed on devices.
  120. Installable *bool
  121. // Make this module available when building for recovery
  122. Recovery_available *bool
  123. // Make this module available when building for vendor
  124. Vendor_available *bool
  125. // Make this module available when building for product
  126. Product_available *bool
  127. // list of .sysprop files which defines the properties.
  128. Srcs []string `android:"path"`
  129. // If set to true, build a variant of the module for the host. Defaults to false.
  130. Host_supported *bool
  131. Cpp struct {
  132. // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
  133. // Forwarded to cc_library.min_sdk_version
  134. Min_sdk_version *string
  135. }
  136. Java struct {
  137. // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
  138. // Forwarded to java_library.min_sdk_version
  139. Min_sdk_version *string
  140. }
  141. }
  142. var (
  143. pctx = android.NewPackageContext("android/soong/sysprop")
  144. syspropCcTag = dependencyTag{name: "syspropCc"}
  145. syspropLibrariesKey = android.NewOnceKey("syspropLibraries")
  146. syspropLibrariesLock sync.Mutex
  147. )
  148. // List of sysprop_library used by property_contexts to perform type check.
  149. func syspropLibraries(config android.Config) *[]string {
  150. return config.Once(syspropLibrariesKey, func() interface{} {
  151. return &[]string{}
  152. }).(*[]string)
  153. }
  154. func SyspropLibraries(config android.Config) []string {
  155. return append([]string{}, *syspropLibraries(config)...)
  156. }
  157. func init() {
  158. registerSyspropBuildComponents(android.InitRegistrationContext)
  159. }
  160. func registerSyspropBuildComponents(ctx android.RegistrationContext) {
  161. ctx.RegisterModuleType("sysprop_library", syspropLibraryFactory)
  162. }
  163. func (m *syspropLibrary) Name() string {
  164. return m.BaseModuleName() + "_sysprop_library"
  165. }
  166. func (m *syspropLibrary) Owner() string {
  167. return m.properties.Property_owner
  168. }
  169. func (m *syspropLibrary) CcImplementationModuleName() string {
  170. return "lib" + m.BaseModuleName()
  171. }
  172. func (m *syspropLibrary) javaPublicStubName() string {
  173. return m.BaseModuleName() + "_public"
  174. }
  175. func (m *syspropLibrary) javaGenModuleName() string {
  176. return m.BaseModuleName() + "_java_gen"
  177. }
  178. func (m *syspropLibrary) javaGenPublicStubName() string {
  179. return m.BaseModuleName() + "_java_gen_public"
  180. }
  181. func (m *syspropLibrary) BaseModuleName() string {
  182. return m.ModuleBase.Name()
  183. }
  184. func (m *syspropLibrary) CurrentSyspropApiFile() android.OptionalPath {
  185. return m.currentApiFile
  186. }
  187. // GenerateAndroidBuildActions of sysprop_library handles API dump and API check.
  188. // generated java_library will depend on these API files.
  189. func (m *syspropLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
  190. baseModuleName := m.BaseModuleName()
  191. for _, syspropFile := range android.PathsForModuleSrc(ctx, m.properties.Srcs) {
  192. if syspropFile.Ext() != ".sysprop" {
  193. ctx.PropertyErrorf("srcs", "srcs contains non-sysprop file %q", syspropFile.String())
  194. }
  195. }
  196. if ctx.Failed() {
  197. return
  198. }
  199. apiDirectoryPath := path.Join(ctx.ModuleDir(), "api")
  200. currentApiFilePath := path.Join(apiDirectoryPath, baseModuleName+"-current.txt")
  201. latestApiFilePath := path.Join(apiDirectoryPath, baseModuleName+"-latest.txt")
  202. m.currentApiFile = android.ExistentPathForSource(ctx, currentApiFilePath)
  203. m.latestApiFile = android.ExistentPathForSource(ctx, latestApiFilePath)
  204. // dump API rule
  205. rule := android.NewRuleBuilder(pctx, ctx)
  206. m.dumpedApiFile = android.PathForModuleOut(ctx, "api-dump.txt")
  207. rule.Command().
  208. BuiltTool("sysprop_api_dump").
  209. Output(m.dumpedApiFile).
  210. Inputs(android.PathsForModuleSrc(ctx, m.properties.Srcs))
  211. rule.Build(baseModuleName+"_api_dump", baseModuleName+" api dump")
  212. // check API rule
  213. rule = android.NewRuleBuilder(pctx, ctx)
  214. // We allow that the API txt files don't exist, when the sysprop_library only contains internal
  215. // properties. But we have to feed current api file and latest api file to the rule builder.
  216. // Currently we can't get android.Path representing the null device, so we add any existing API
  217. // txt files to implicits, and then directly feed string paths, rather than calling Input(Path)
  218. // method.
  219. var apiFileList android.Paths
  220. currentApiArgument := os.DevNull
  221. if m.currentApiFile.Valid() {
  222. apiFileList = append(apiFileList, m.currentApiFile.Path())
  223. currentApiArgument = m.currentApiFile.String()
  224. }
  225. latestApiArgument := os.DevNull
  226. if m.latestApiFile.Valid() {
  227. apiFileList = append(apiFileList, m.latestApiFile.Path())
  228. latestApiArgument = m.latestApiFile.String()
  229. }
  230. // 1. compares current.txt to api-dump.txt
  231. // current.txt should be identical to api-dump.txt.
  232. msg := fmt.Sprintf(`\n******************************\n`+
  233. `API of sysprop_library %s doesn't match with current.txt\n`+
  234. `Please update current.txt by:\n`+
  235. `m %s-dump-api && mkdir -p %q && rm -rf %q && cp -f %q %q\n`+
  236. `******************************\n`, baseModuleName, baseModuleName,
  237. apiDirectoryPath, currentApiFilePath, m.dumpedApiFile.String(), currentApiFilePath)
  238. rule.Command().
  239. Text("( cmp").Flag("-s").
  240. Input(m.dumpedApiFile).
  241. Text(currentApiArgument).
  242. Text("|| ( echo").Flag("-e").
  243. Flag(`"` + msg + `"`).
  244. Text("; exit 38) )")
  245. // 2. compares current.txt to latest.txt (frozen API)
  246. // current.txt should be compatible with latest.txt
  247. msg = fmt.Sprintf(`\n******************************\n`+
  248. `API of sysprop_library %s doesn't match with latest version\n`+
  249. `Please fix the breakage and rebuild.\n`+
  250. `******************************\n`, baseModuleName)
  251. rule.Command().
  252. Text("( ").
  253. BuiltTool("sysprop_api_checker").
  254. Text(latestApiArgument).
  255. Text(currentApiArgument).
  256. Text(" || ( echo").Flag("-e").
  257. Flag(`"` + msg + `"`).
  258. Text("; exit 38) )").
  259. Implicits(apiFileList)
  260. m.checkApiFileTimeStamp = android.PathForModuleOut(ctx, "check_api.timestamp")
  261. rule.Command().
  262. Text("touch").
  263. Output(m.checkApiFileTimeStamp)
  264. rule.Build(baseModuleName+"_check_api", baseModuleName+" check api")
  265. }
  266. func (m *syspropLibrary) AndroidMk() android.AndroidMkData {
  267. return android.AndroidMkData{
  268. Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
  269. // sysprop_library module itself is defined as a FAKE module to perform API check.
  270. // Actual implementation libraries are created on LoadHookMutator
  271. fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
  272. fmt.Fprintf(w, "LOCAL_MODULE := %s\n", m.Name())
  273. data.Entries.WriteLicenseVariables(w)
  274. fmt.Fprintf(w, "LOCAL_MODULE_CLASS := FAKE\n")
  275. fmt.Fprintf(w, "LOCAL_MODULE_TAGS := optional\n")
  276. fmt.Fprintf(w, "include $(BUILD_SYSTEM)/base_rules.mk\n\n")
  277. fmt.Fprintf(w, "$(LOCAL_BUILT_MODULE): %s\n", m.checkApiFileTimeStamp.String())
  278. fmt.Fprintf(w, "\ttouch $@\n\n")
  279. fmt.Fprintf(w, ".PHONY: %s-check-api %s-dump-api\n\n", name, name)
  280. // dump API rule
  281. fmt.Fprintf(w, "%s-dump-api: %s\n\n", name, m.dumpedApiFile.String())
  282. // check API rule
  283. fmt.Fprintf(w, "%s-check-api: %s\n\n", name, m.checkApiFileTimeStamp.String())
  284. }}
  285. }
  286. var _ android.ApexModule = (*syspropLibrary)(nil)
  287. // Implements android.ApexModule
  288. func (m *syspropLibrary) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
  289. sdkVersion android.ApiLevel) error {
  290. return fmt.Errorf("sysprop_library is not supposed to be part of apex modules")
  291. }
  292. // sysprop_library creates schematized APIs from sysprop description files (.sysprop).
  293. // Both Java and C++ modules can link against sysprop_library, and API stability check
  294. // against latest APIs (see build/soong/scripts/freeze-sysprop-api-files.sh)
  295. // is performed.
  296. func syspropLibraryFactory() android.Module {
  297. m := &syspropLibrary{}
  298. m.AddProperties(
  299. &m.properties,
  300. )
  301. android.InitAndroidModule(m)
  302. android.InitApexModule(m)
  303. android.AddLoadHook(m, func(ctx android.LoadHookContext) { syspropLibraryHook(ctx, m) })
  304. return m
  305. }
  306. type ccLibraryProperties struct {
  307. Name *string
  308. Srcs []string
  309. Soc_specific *bool
  310. Device_specific *bool
  311. Product_specific *bool
  312. Sysprop struct {
  313. Platform *bool
  314. }
  315. Target struct {
  316. Android struct {
  317. Header_libs []string
  318. Shared_libs []string
  319. }
  320. Host struct {
  321. Static_libs []string
  322. }
  323. }
  324. Required []string
  325. Recovery *bool
  326. Recovery_available *bool
  327. Vendor_available *bool
  328. Product_available *bool
  329. Host_supported *bool
  330. Apex_available []string
  331. Min_sdk_version *string
  332. }
  333. type javaLibraryProperties struct {
  334. Name *string
  335. Srcs []string
  336. Soc_specific *bool
  337. Device_specific *bool
  338. Product_specific *bool
  339. Required []string
  340. Sdk_version *string
  341. Installable *bool
  342. Libs []string
  343. Stem *string
  344. SyspropPublicStub string
  345. Apex_available []string
  346. Min_sdk_version *string
  347. }
  348. func syspropLibraryHook(ctx android.LoadHookContext, m *syspropLibrary) {
  349. if len(m.properties.Srcs) == 0 {
  350. ctx.PropertyErrorf("srcs", "sysprop_library must specify srcs")
  351. }
  352. // ctx's Platform or Specific functions represent where this sysprop_library installed.
  353. installedInSystem := ctx.Platform() || ctx.SystemExtSpecific()
  354. installedInVendorOrOdm := ctx.SocSpecific() || ctx.DeviceSpecific()
  355. installedInProduct := ctx.ProductSpecific()
  356. isOwnerPlatform := false
  357. var javaSyspropStub string
  358. // javaSyspropStub contains stub libraries used by generated APIs, instead of framework stub.
  359. // This is to make sysprop_library link against core_current.
  360. if installedInVendorOrOdm {
  361. javaSyspropStub = "sysprop-library-stub-vendor"
  362. } else if installedInProduct {
  363. javaSyspropStub = "sysprop-library-stub-product"
  364. } else {
  365. javaSyspropStub = "sysprop-library-stub-platform"
  366. }
  367. switch m.Owner() {
  368. case "Platform":
  369. // Every partition can access platform-defined properties
  370. isOwnerPlatform = true
  371. case "Vendor":
  372. // System can't access vendor's properties
  373. if installedInSystem {
  374. ctx.ModuleErrorf("None of soc_specific, device_specific, product_specific is true. " +
  375. "System can't access sysprop_library owned by Vendor")
  376. }
  377. case "Odm":
  378. // Only vendor can access Odm-defined properties
  379. if !installedInVendorOrOdm {
  380. ctx.ModuleErrorf("Neither soc_speicifc nor device_specific is true. " +
  381. "Odm-defined properties should be accessed only in Vendor or Odm")
  382. }
  383. default:
  384. ctx.PropertyErrorf("property_owner",
  385. "Unknown value %s: must be one of Platform, Vendor or Odm", m.Owner())
  386. }
  387. // Generate a C++ implementation library.
  388. // cc_library can receive *.sysprop files as their srcs, generating sources itself.
  389. ccProps := ccLibraryProperties{}
  390. ccProps.Name = proptools.StringPtr(m.CcImplementationModuleName())
  391. ccProps.Srcs = m.properties.Srcs
  392. ccProps.Soc_specific = proptools.BoolPtr(ctx.SocSpecific())
  393. ccProps.Device_specific = proptools.BoolPtr(ctx.DeviceSpecific())
  394. ccProps.Product_specific = proptools.BoolPtr(ctx.ProductSpecific())
  395. ccProps.Sysprop.Platform = proptools.BoolPtr(isOwnerPlatform)
  396. ccProps.Target.Android.Header_libs = []string{"libbase_headers"}
  397. ccProps.Target.Android.Shared_libs = []string{"liblog"}
  398. ccProps.Target.Host.Static_libs = []string{"libbase", "liblog"}
  399. ccProps.Recovery_available = m.properties.Recovery_available
  400. ccProps.Vendor_available = m.properties.Vendor_available
  401. ccProps.Product_available = m.properties.Product_available
  402. ccProps.Host_supported = m.properties.Host_supported
  403. ccProps.Apex_available = m.ApexProperties.Apex_available
  404. ccProps.Min_sdk_version = m.properties.Cpp.Min_sdk_version
  405. ctx.CreateModule(cc.LibraryFactory, &ccProps)
  406. scope := "internal"
  407. // We need to only use public version, if the partition where sysprop_library will be installed
  408. // is different from owner.
  409. if ctx.ProductSpecific() {
  410. // Currently product partition can't own any sysprop_library. So product always uses public.
  411. scope = "public"
  412. } else if isOwnerPlatform && installedInVendorOrOdm {
  413. // Vendor or Odm should use public version of Platform's sysprop_library.
  414. scope = "public"
  415. }
  416. // Generate a Java implementation library.
  417. // Contrast to C++, syspropJavaGenRule module will generate srcjar and the srcjar will be fed
  418. // to Java implementation library.
  419. ctx.CreateModule(syspropJavaGenFactory, &syspropGenProperties{
  420. Srcs: m.properties.Srcs,
  421. Scope: scope,
  422. Name: proptools.StringPtr(m.javaGenModuleName()),
  423. Check_api: proptools.StringPtr(ctx.ModuleName()),
  424. })
  425. // if platform sysprop_library is installed in /system or /system-ext, we regard it as an API
  426. // and allow any modules (even from different partition) to link against the sysprop_library.
  427. // To do that, we create a public stub and expose it to modules with sdk_version: system_*.
  428. var publicStub string
  429. if isOwnerPlatform && installedInSystem {
  430. publicStub = m.javaPublicStubName()
  431. }
  432. ctx.CreateModule(java.LibraryFactory, &javaLibraryProperties{
  433. Name: proptools.StringPtr(m.BaseModuleName()),
  434. Srcs: []string{":" + m.javaGenModuleName()},
  435. Soc_specific: proptools.BoolPtr(ctx.SocSpecific()),
  436. Device_specific: proptools.BoolPtr(ctx.DeviceSpecific()),
  437. Product_specific: proptools.BoolPtr(ctx.ProductSpecific()),
  438. Installable: m.properties.Installable,
  439. Sdk_version: proptools.StringPtr("core_current"),
  440. Libs: []string{javaSyspropStub},
  441. SyspropPublicStub: publicStub,
  442. Apex_available: m.ApexProperties.Apex_available,
  443. Min_sdk_version: m.properties.Java.Min_sdk_version,
  444. })
  445. if publicStub != "" {
  446. ctx.CreateModule(syspropJavaGenFactory, &syspropGenProperties{
  447. Srcs: m.properties.Srcs,
  448. Scope: "public",
  449. Name: proptools.StringPtr(m.javaGenPublicStubName()),
  450. Check_api: proptools.StringPtr(ctx.ModuleName()),
  451. })
  452. ctx.CreateModule(java.LibraryFactory, &javaLibraryProperties{
  453. Name: proptools.StringPtr(publicStub),
  454. Srcs: []string{":" + m.javaGenPublicStubName()},
  455. Installable: proptools.BoolPtr(false),
  456. Sdk_version: proptools.StringPtr("core_current"),
  457. Libs: []string{javaSyspropStub},
  458. Stem: proptools.StringPtr(m.BaseModuleName()),
  459. })
  460. }
  461. // syspropLibraries will be used by property_contexts to check types.
  462. // Record absolute paths of sysprop_library to prevent soong_namespace problem.
  463. if m.ExportedToMake() {
  464. syspropLibrariesLock.Lock()
  465. defer syspropLibrariesLock.Unlock()
  466. libraries := syspropLibraries(ctx.Config())
  467. *libraries = append(*libraries, "//"+ctx.ModuleDir()+":"+ctx.ModuleName())
  468. }
  469. }