bpfix.go 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817
  1. // Copyright 2017 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. // This file implements the logic of bpfix and also provides a programmatic interface
  15. package bpfix
  16. import (
  17. "bytes"
  18. "errors"
  19. "flag"
  20. "fmt"
  21. "io"
  22. "io/ioutil"
  23. "os"
  24. "path/filepath"
  25. "reflect"
  26. "sort"
  27. "strings"
  28. "github.com/google/blueprint/parser"
  29. "github.com/google/blueprint/pathtools"
  30. )
  31. // Reformat takes a blueprint file as a string and returns a formatted version
  32. func Reformat(input string) (string, error) {
  33. tree, err := parse("<string>", bytes.NewBufferString(input))
  34. if err != nil {
  35. return "", err
  36. }
  37. res, err := parser.Print(tree)
  38. if err != nil {
  39. return "", err
  40. }
  41. return string(res), nil
  42. }
  43. // A FixRequest specifies the details of which fixes to apply to an individual file
  44. // A FixRequest doesn't specify whether to do a dry run or where to write the results; that's in cmd/bpfix.go
  45. type FixRequest struct {
  46. steps []FixStep
  47. }
  48. type FixStepsExtension struct {
  49. Name string
  50. Steps []FixStep
  51. }
  52. type FixStep struct {
  53. Name string
  54. Fix func(f *Fixer) error
  55. }
  56. var fixStepsExtensions = []*FixStepsExtension(nil)
  57. func RegisterFixStepExtension(extension *FixStepsExtension) {
  58. fixStepsExtensions = append(fixStepsExtensions, extension)
  59. }
  60. var fixSteps = []FixStep{
  61. {
  62. Name: "simplifyKnownRedundantVariables",
  63. Fix: runPatchListMod(simplifyKnownPropertiesDuplicatingEachOther),
  64. },
  65. {
  66. Name: "rewriteIncorrectAndroidmkPrebuilts",
  67. Fix: rewriteIncorrectAndroidmkPrebuilts,
  68. },
  69. {
  70. Name: "rewriteCtsModuleTypes",
  71. Fix: rewriteCtsModuleTypes,
  72. },
  73. {
  74. Name: "rewriteIncorrectAndroidmkAndroidLibraries",
  75. Fix: rewriteIncorrectAndroidmkAndroidLibraries,
  76. },
  77. {
  78. Name: "rewriteTestModuleTypes",
  79. Fix: rewriteTestModuleTypes,
  80. },
  81. {
  82. Name: "rewriteAndroidmkJavaLibs",
  83. Fix: rewriteAndroidmkJavaLibs,
  84. },
  85. {
  86. Name: "rewriteJavaStaticLibs",
  87. Fix: rewriteJavaStaticLibs,
  88. },
  89. {
  90. Name: "rewritePrebuiltEtc",
  91. Fix: rewriteAndroidmkPrebuiltEtc,
  92. },
  93. {
  94. Name: "mergeMatchingModuleProperties",
  95. Fix: runPatchListMod(mergeMatchingModuleProperties),
  96. },
  97. {
  98. Name: "reorderCommonProperties",
  99. Fix: runPatchListMod(reorderCommonProperties),
  100. },
  101. {
  102. Name: "removeTags",
  103. Fix: runPatchListMod(removeTags),
  104. },
  105. {
  106. Name: "rewriteAndroidTest",
  107. Fix: rewriteAndroidTest,
  108. },
  109. {
  110. Name: "rewriteAndroidAppImport",
  111. Fix: rewriteAndroidAppImport,
  112. },
  113. {
  114. Name: "removeEmptyLibDependencies",
  115. Fix: removeEmptyLibDependencies,
  116. },
  117. {
  118. Name: "removeHidlInterfaceTypes",
  119. Fix: removeHidlInterfaceTypes,
  120. },
  121. {
  122. Name: "removeSoongConfigBoolVariable",
  123. Fix: removeSoongConfigBoolVariable,
  124. },
  125. {
  126. Name: "removePdkProperty",
  127. Fix: runPatchListMod(removeObsoleteProperty("product_variables.pdk")),
  128. },
  129. {
  130. Name: "removeScudoProperty",
  131. Fix: runPatchListMod(removeObsoleteProperty("sanitize.scudo")),
  132. },
  133. {
  134. Name: "removeAndroidLicenseKinds",
  135. Fix: runPatchListMod(removeIncorrectProperties("android_license_kinds")),
  136. },
  137. {
  138. Name: "removeAndroidLicenseConditions",
  139. Fix: runPatchListMod(removeIncorrectProperties("android_license_conditions")),
  140. },
  141. {
  142. Name: "removeAndroidLicenseFiles",
  143. Fix: runPatchListMod(removeIncorrectProperties("android_license_files")),
  144. },
  145. {
  146. Name: "formatFlagProperties",
  147. Fix: runPatchListMod(formatFlagProperties),
  148. },
  149. {
  150. Name: "removeResourcesAndAssetsIfDefault",
  151. Fix: removeResourceAndAssetsIfDefault,
  152. },
  153. }
  154. // for fix that only need to run once
  155. var fixStepsOnce = []FixStep{
  156. {
  157. Name: "haveSameLicense",
  158. Fix: haveSameLicense,
  159. },
  160. {
  161. Name: "rewriteLicenseProperties",
  162. Fix: runPatchListMod(rewriteLicenseProperty(nil, "")),
  163. },
  164. }
  165. func NewFixRequest() FixRequest {
  166. return FixRequest{}
  167. }
  168. func (r FixRequest) AddAll() (result FixRequest) {
  169. result.steps = append([]FixStep(nil), r.steps...)
  170. result.steps = append(result.steps, fixSteps...)
  171. for _, extension := range fixStepsExtensions {
  172. result.steps = append(result.steps, extension.Steps...)
  173. }
  174. return result
  175. }
  176. func (r FixRequest) AddBase() (result FixRequest) {
  177. result.steps = append([]FixStep(nil), r.steps...)
  178. result.steps = append(result.steps, fixSteps...)
  179. return result
  180. }
  181. func (r FixRequest) AddMatchingExtensions(pattern string) (result FixRequest) {
  182. result.steps = append([]FixStep(nil), r.steps...)
  183. for _, extension := range fixStepsExtensions {
  184. if match, _ := filepath.Match(pattern, extension.Name); match {
  185. result.steps = append(result.steps, extension.Steps...)
  186. }
  187. }
  188. return result
  189. }
  190. type Fixer struct {
  191. tree *parser.File
  192. }
  193. func (f Fixer) Tree() *parser.File {
  194. return f.tree
  195. }
  196. func NewFixer(tree *parser.File) *Fixer {
  197. fixer := &Fixer{tree}
  198. // make a copy of the tree
  199. fixer.reparse()
  200. return fixer
  201. }
  202. // Fix repeatedly applies the fixes listed in the given FixRequest to the given File
  203. // until there is no fix that affects the tree
  204. func (f *Fixer) Fix(config FixRequest) (*parser.File, error) {
  205. prevIdentifier, err := f.fingerprint()
  206. if err != nil {
  207. return nil, err
  208. }
  209. // run fix that is expected to run once first
  210. configOnce := NewFixRequest()
  211. configOnce.steps = append(configOnce.steps, fixStepsOnce...)
  212. if len(configOnce.steps) > 0 {
  213. err = f.fixTreeOnce(configOnce)
  214. if err != nil {
  215. return nil, err
  216. }
  217. }
  218. maxNumIterations := 20
  219. i := 0
  220. for {
  221. err = f.fixTreeOnce(config)
  222. newIdentifier, err := f.fingerprint()
  223. if err != nil {
  224. return nil, err
  225. }
  226. if bytes.Equal(newIdentifier, prevIdentifier) {
  227. break
  228. }
  229. prevIdentifier = newIdentifier
  230. // any errors from a previous iteration generally get thrown away and overwritten by errors on the next iteration
  231. // detect infinite loop
  232. i++
  233. if i >= maxNumIterations {
  234. return nil, fmt.Errorf("Applied fixes %d times and yet the tree continued to change. Is there an infinite loop?", i)
  235. }
  236. }
  237. return f.tree, err
  238. }
  239. // returns a unique identifier for the given tree that can be used to determine whether the tree changed
  240. func (f *Fixer) fingerprint() (fingerprint []byte, err error) {
  241. bytes, err := parser.Print(f.tree)
  242. if err != nil {
  243. return nil, err
  244. }
  245. return bytes, nil
  246. }
  247. func (f *Fixer) reparse() ([]byte, error) {
  248. buf, err := parser.Print(f.tree)
  249. if err != nil {
  250. return nil, err
  251. }
  252. newTree, err := parse(f.tree.Name, bytes.NewReader(buf))
  253. if err != nil {
  254. return nil, err
  255. }
  256. f.tree = newTree
  257. return buf, nil
  258. }
  259. func parse(name string, r io.Reader) (*parser.File, error) {
  260. tree, errs := parser.Parse(name, r, parser.NewScope(nil))
  261. if errs != nil {
  262. s := "parse error: "
  263. for _, err := range errs {
  264. s += "\n" + err.Error()
  265. }
  266. return nil, errors.New(s)
  267. }
  268. return tree, nil
  269. }
  270. func (f *Fixer) fixTreeOnce(config FixRequest) error {
  271. for _, fix := range config.steps {
  272. err := fix.Fix(f)
  273. if err != nil {
  274. return err
  275. }
  276. }
  277. return nil
  278. }
  279. func simplifyKnownPropertiesDuplicatingEachOther(mod *parser.Module, buf []byte, patchList *parser.PatchList) error {
  280. // remove from local_include_dirs anything in export_include_dirs
  281. return removeMatchingModuleListProperties(mod, patchList,
  282. "export_include_dirs", "local_include_dirs")
  283. }
  284. func rewriteIncorrectAndroidmkPrebuilts(f *Fixer) error {
  285. for _, def := range f.tree.Defs {
  286. mod, ok := def.(*parser.Module)
  287. if !ok {
  288. continue
  289. }
  290. if mod.Type != "java_import" {
  291. continue
  292. }
  293. host, _ := getLiteralBoolPropertyValue(mod, "host")
  294. if host {
  295. mod.Type = "java_import_host"
  296. removeProperty(mod, "host")
  297. }
  298. srcs, ok := getLiteralListProperty(mod, "srcs")
  299. if !ok {
  300. continue
  301. }
  302. if len(srcs.Values) == 0 {
  303. continue
  304. }
  305. src, ok := srcs.Values[0].(*parser.String)
  306. if !ok {
  307. continue
  308. }
  309. switch filepath.Ext(src.Value) {
  310. case ".jar":
  311. renameProperty(mod, "srcs", "jars")
  312. case ".aar":
  313. renameProperty(mod, "srcs", "aars")
  314. mod.Type = "android_library_import"
  315. // An android_library_import doesn't get installed, so setting "installable = false" isn't supported
  316. removeProperty(mod, "installable")
  317. }
  318. }
  319. return nil
  320. }
  321. func rewriteCtsModuleTypes(f *Fixer) error {
  322. for _, def := range f.tree.Defs {
  323. mod, ok := def.(*parser.Module)
  324. if !ok {
  325. continue
  326. }
  327. if mod.Type != "cts_support_package" && mod.Type != "cts_package" &&
  328. mod.Type != "cts_target_java_library" &&
  329. mod.Type != "cts_host_java_library" {
  330. continue
  331. }
  332. var defStr string
  333. switch mod.Type {
  334. case "cts_support_package":
  335. mod.Type = "android_test_helper_app"
  336. defStr = "cts_support_defaults"
  337. case "cts_package":
  338. mod.Type = "android_test"
  339. defStr = "cts_defaults"
  340. case "cts_target_java_library":
  341. mod.Type = "java_library"
  342. defStr = "cts_defaults"
  343. case "cts_host_java_library":
  344. mod.Type = "java_library_host"
  345. defStr = "cts_defaults"
  346. }
  347. defaults := &parser.Property{
  348. Name: "defaults",
  349. Value: &parser.List{
  350. Values: []parser.Expression{
  351. &parser.String{
  352. Value: defStr,
  353. },
  354. },
  355. },
  356. }
  357. mod.Properties = append(mod.Properties, defaults)
  358. }
  359. return nil
  360. }
  361. func rewriteIncorrectAndroidmkAndroidLibraries(f *Fixer) error {
  362. for _, def := range f.tree.Defs {
  363. mod, ok := def.(*parser.Module)
  364. if !ok {
  365. continue
  366. }
  367. if !strings.HasPrefix(mod.Type, "java_") && !strings.HasPrefix(mod.Type, "android_") {
  368. continue
  369. }
  370. hasAndroidLibraries := hasNonEmptyLiteralListProperty(mod, "android_libs")
  371. hasStaticAndroidLibraries := hasNonEmptyLiteralListProperty(mod, "android_static_libs")
  372. hasResourceDirs := hasNonEmptyLiteralListProperty(mod, "resource_dirs")
  373. if hasAndroidLibraries || hasStaticAndroidLibraries || hasResourceDirs {
  374. if mod.Type == "java_library_static" || mod.Type == "java_library" {
  375. mod.Type = "android_library"
  376. }
  377. }
  378. if mod.Type == "java_import" && !hasStaticAndroidLibraries {
  379. removeProperty(mod, "android_static_libs")
  380. }
  381. // These may conflict with existing libs and static_libs properties, but the
  382. // mergeMatchingModuleProperties pass will fix it.
  383. renameProperty(mod, "shared_libs", "libs")
  384. renameProperty(mod, "android_libs", "libs")
  385. renameProperty(mod, "android_static_libs", "static_libs")
  386. }
  387. return nil
  388. }
  389. // rewriteTestModuleTypes looks for modules that are identifiable as tests but for which Make doesn't have a separate
  390. // module class, and moves them to the appropriate Soong module type.
  391. func rewriteTestModuleTypes(f *Fixer) error {
  392. for _, def := range f.tree.Defs {
  393. mod, ok := def.(*parser.Module)
  394. if !ok {
  395. continue
  396. }
  397. if !strings.HasPrefix(mod.Type, "java_") && !strings.HasPrefix(mod.Type, "android_") && mod.Type != "cc_binary" {
  398. continue
  399. }
  400. hasInstrumentationFor := hasNonEmptyLiteralStringProperty(mod, "instrumentation_for")
  401. hasTestSuites := hasNonEmptyLiteralListProperty(mod, "test_suites")
  402. tags, _ := getLiteralListPropertyValue(mod, "tags")
  403. var hasTestsTag bool
  404. for _, tag := range tags {
  405. if tag == "tests" {
  406. hasTestsTag = true
  407. }
  408. }
  409. isTest := hasInstrumentationFor || hasTestsTag || hasTestSuites
  410. if isTest {
  411. switch mod.Type {
  412. case "android_app":
  413. mod.Type = "android_test"
  414. case "android_app_import":
  415. mod.Type = "android_test_import"
  416. case "java_library", "java_library_installable":
  417. mod.Type = "java_test"
  418. case "java_library_host":
  419. mod.Type = "java_test_host"
  420. case "cc_binary":
  421. mod.Type = "cc_test"
  422. }
  423. }
  424. }
  425. return nil
  426. }
  427. // rewriteJavaStaticLibs rewrites java_library_static into java_library
  428. func rewriteJavaStaticLibs(f *Fixer) error {
  429. for _, def := range f.tree.Defs {
  430. mod, ok := def.(*parser.Module)
  431. if !ok {
  432. continue
  433. }
  434. if mod.Type == "java_library_static" {
  435. mod.Type = "java_library"
  436. }
  437. }
  438. return nil
  439. }
  440. // rewriteAndroidmkJavaLibs rewrites java_library_installable into java_library plus installable: true
  441. func rewriteAndroidmkJavaLibs(f *Fixer) error {
  442. for _, def := range f.tree.Defs {
  443. mod, ok := def.(*parser.Module)
  444. if !ok {
  445. continue
  446. }
  447. if mod.Type != "java_library_installable" {
  448. continue
  449. }
  450. mod.Type = "java_library"
  451. _, hasInstallable := mod.GetProperty("installable")
  452. if !hasInstallable {
  453. prop := &parser.Property{
  454. Name: "installable",
  455. Value: &parser.Bool{
  456. Value: true,
  457. },
  458. }
  459. mod.Properties = append(mod.Properties, prop)
  460. }
  461. }
  462. return nil
  463. }
  464. // Helper function to get the value of a string-valued property in a given compound property.
  465. func getStringProperty(prop *parser.Property, fieldName string) string {
  466. if propsAsMap, ok := prop.Value.(*parser.Map); ok {
  467. for _, propField := range propsAsMap.Properties {
  468. if fieldName == propField.Name {
  469. if propFieldAsString, ok := propField.Value.(*parser.String); ok {
  470. return propFieldAsString.Value
  471. } else {
  472. return ""
  473. }
  474. }
  475. }
  476. }
  477. return ""
  478. }
  479. // Set the value of the given attribute to the error message
  480. func indicateAttributeError(mod *parser.Module, attributeName string, format string, a ...interface{}) error {
  481. msg := fmt.Sprintf(format, a...)
  482. mod.Properties = append(mod.Properties, &parser.Property{
  483. Name: attributeName,
  484. Value: &parser.String{Value: "ERROR: " + msg},
  485. })
  486. return errors.New(msg)
  487. }
  488. // If a variable is LOCAL_MODULE, get its value from the 'name' attribute.
  489. // This handles the statement
  490. //
  491. // LOCAL_SRC_FILES := $(LOCAL_MODULE)
  492. //
  493. // which occurs often.
  494. func resolveLocalModule(mod *parser.Module, val parser.Expression) parser.Expression {
  495. if varLocalName, ok := val.(*parser.Variable); ok {
  496. if varLocalName.Name == "LOCAL_MODULE" {
  497. if v, ok := getLiteralStringProperty(mod, "name"); ok {
  498. return v
  499. }
  500. }
  501. }
  502. return val
  503. }
  504. // etcPrebuiltModuleUpdate contains information on updating certain parts of a defined module such as:
  505. // - changing the module type from prebuilt_etc to a different one
  506. // - stripping the prefix of the install path based on the module type
  507. // - appending additional boolean properties to the prebuilt module
  508. type etcPrebuiltModuleUpdate struct {
  509. // The prefix of the install path defined in local_module_path. The prefix is removed from local_module_path
  510. // before setting the 'filename' attribute.
  511. prefix string
  512. // There is only one prebuilt module type in makefiles. In Soong, there are multiple versions of
  513. // prebuilts based on local_module_path. By default, it is "prebuilt_etc" if modType is blank. An
  514. // example is if the local_module_path contains $(TARGET_OUT)/usr/share, the module type is
  515. // considered as prebuilt_usr_share.
  516. modType string
  517. // Additional boolean attributes to be added in the prebuilt module. Each added boolean attribute
  518. // has a value of true.
  519. flags []string
  520. }
  521. func (f etcPrebuiltModuleUpdate) update(m *parser.Module, path string) bool {
  522. updated := false
  523. if path == f.prefix {
  524. updated = true
  525. } else if trimmedPath := strings.TrimPrefix(path, f.prefix+"/"); trimmedPath != path {
  526. m.Properties = append(m.Properties, &parser.Property{
  527. Name: "relative_install_path",
  528. Value: &parser.String{Value: trimmedPath},
  529. })
  530. updated = true
  531. }
  532. if updated {
  533. for _, flag := range f.flags {
  534. m.Properties = append(m.Properties, &parser.Property{Name: flag, Value: &parser.Bool{Value: true, Token: "true"}})
  535. }
  536. if f.modType != "" {
  537. m.Type = f.modType
  538. }
  539. }
  540. return updated
  541. }
  542. var localModuleUpdate = map[string][]etcPrebuiltModuleUpdate{
  543. "HOST_OUT": {
  544. {prefix: "/etc", modType: "prebuilt_etc_host"},
  545. {prefix: "/usr/share", modType: "prebuilt_usr_share_host"},
  546. {prefix: "", modType: "prebuilt_root_host"},
  547. },
  548. "PRODUCT_OUT": {{prefix: "/system/etc"}, {prefix: "/vendor/etc", flags: []string{"proprietary"}}},
  549. "TARGET_OUT": {{prefix: "/usr/share", modType: "prebuilt_usr_share"}, {prefix: "/fonts", modType: "prebuilt_font"},
  550. {prefix: "/etc/firmware", modType: "prebuilt_firmware"}, {prefix: "/vendor/firmware", modType: "prebuilt_firmware", flags: []string{"proprietary"}},
  551. {prefix: "/etc"}},
  552. "TARGET_OUT_ETC": {{prefix: "/firmware", modType: "prebuilt_firmware"}, {prefix: ""}},
  553. "TARGET_OUT_PRODUCT": {{prefix: "/etc", flags: []string{"product_specific"}}, {prefix: "/fonts", modType: "prebuilt_font", flags: []string{"product_specific"}}},
  554. "TARGET_OUT_PRODUCT_ETC": {{prefix: "", flags: []string{"product_specific"}}},
  555. "TARGET_OUT_ODM": {{prefix: "/etc", flags: []string{"device_specific"}}},
  556. "TARGET_OUT_SYSTEM_EXT": {{prefix: "/etc", flags: []string{"system_ext_specific"}}},
  557. "TARGET_OUT_SYSTEM_EXT_ETC": {{prefix: "", flags: []string{"system_ext_specific"}}},
  558. "TARGET_OUT_VENDOR": {{prefix: "/etc", flags: []string{"proprietary"}}, {prefix: "/firmware", modType: "prebuilt_firmware", flags: []string{"proprietary"}}},
  559. "TARGET_OUT_VENDOR_ETC": {{prefix: "", flags: []string{"proprietary"}}},
  560. "TARGET_RECOVERY_ROOT_OUT": {{prefix: "/system/etc", flags: []string{"recovery"}}},
  561. }
  562. // rewriteAndroidPrebuiltEtc fixes prebuilt_etc rule
  563. func rewriteAndroidmkPrebuiltEtc(f *Fixer) error {
  564. for _, def := range f.tree.Defs {
  565. mod, ok := def.(*parser.Module)
  566. if !ok {
  567. continue
  568. }
  569. if mod.Type != "prebuilt_etc" && mod.Type != "prebuilt_etc_host" {
  570. continue
  571. }
  572. // 'srcs' --> 'src' conversion
  573. convertToSingleSource(mod, "src")
  574. renameProperty(mod, "sub_dir", "relative_install_path")
  575. // The rewriter converts LOCAL_MODULE_PATH attribute into a struct attribute
  576. // 'local_module_path'. Analyze its contents and create the correct sub_dir:,
  577. // filename: and boolean attributes combination
  578. const local_module_path = "local_module_path"
  579. if prop_local_module_path, ok := mod.GetProperty(local_module_path); ok {
  580. removeProperty(mod, local_module_path)
  581. prefixVariableName := getStringProperty(prop_local_module_path, "var")
  582. if moduleUpdates, ok := localModuleUpdate[prefixVariableName]; ok {
  583. path := getStringProperty(prop_local_module_path, "fixed")
  584. updated := false
  585. for i := 0; i < len(moduleUpdates) && !updated; i++ {
  586. updated = moduleUpdates[i].update(mod, path)
  587. }
  588. if !updated {
  589. expectedPrefices := ""
  590. sep := ""
  591. for _, moduleUpdate := range moduleUpdates {
  592. expectedPrefices += sep
  593. sep = ", "
  594. expectedPrefices += moduleUpdate.prefix
  595. }
  596. return indicateAttributeError(mod, "filename",
  597. "LOCAL_MODULE_PATH value under $(%s) should start with %s", prefixVariableName, expectedPrefices)
  598. }
  599. } else {
  600. return indicateAttributeError(mod, "filename", "Cannot handle $(%s) for the prebuilt_etc", prefixVariableName)
  601. }
  602. }
  603. }
  604. return nil
  605. }
  606. func rewriteAndroidTest(f *Fixer) error {
  607. for _, def := range f.tree.Defs {
  608. mod, ok := def.(*parser.Module)
  609. if !ok {
  610. // The definition is not a module.
  611. continue
  612. }
  613. if mod.Type != "android_test" && mod.Type != "android_test_helper_app" {
  614. // The module is not an android_test or android_test_helper_app.
  615. continue
  616. }
  617. // The rewriter converts LOCAL_MODULE_PATH attribute into a struct attribute
  618. // 'local_module_path'. For the android_test module, it should be $(TARGET_OUT_DATA_APPS),
  619. // that is, `local_module_path: { var: "TARGET_OUT_DATA_APPS"}`
  620. // 1. if the `key: val` pair matches, (key is `local_module_path`,
  621. // and val is `{ var: "TARGET_OUT_DATA_APPS"}`), this property is removed;
  622. // 2. o/w, an error msg is thrown.
  623. const local_module_path = "local_module_path"
  624. if prop_local_module_path, ok := mod.GetProperty(local_module_path); ok {
  625. removeProperty(mod, local_module_path)
  626. prefixVariableName := getStringProperty(prop_local_module_path, "var")
  627. path := getStringProperty(prop_local_module_path, "fixed")
  628. if prefixVariableName == "TARGET_OUT_DATA_APPS" && path == "" {
  629. continue
  630. }
  631. return indicateAttributeError(mod, "filename",
  632. "Only LOCAL_MODULE_PATH := $(TARGET_OUT_DATA_APPS) is allowed for the %s", mod.Type)
  633. }
  634. }
  635. return nil
  636. }
  637. func rewriteAndroidAppImport(f *Fixer) error {
  638. for _, def := range f.tree.Defs {
  639. mod, ok := def.(*parser.Module)
  640. if !(ok && mod.Type == "android_app_import") {
  641. continue
  642. }
  643. // 'srcs' --> 'apk' conversion
  644. convertToSingleSource(mod, "apk")
  645. // Handle special certificate value, "PRESIGNED".
  646. if cert, ok := mod.GetProperty("certificate"); ok {
  647. if certStr, ok := cert.Value.(*parser.String); ok {
  648. if certStr.Value == "PRESIGNED" {
  649. removeProperty(mod, "certificate")
  650. prop := &parser.Property{
  651. Name: "presigned",
  652. Value: &parser.Bool{
  653. Value: true,
  654. },
  655. }
  656. mod.Properties = append(mod.Properties, prop)
  657. }
  658. }
  659. }
  660. }
  661. return nil
  662. }
  663. func RewriteRuntimeResourceOverlay(f *Fixer) error {
  664. for _, def := range f.tree.Defs {
  665. mod, ok := def.(*parser.Module)
  666. if !(ok && mod.Type == "runtime_resource_overlay") {
  667. continue
  668. }
  669. // runtime_resource_overlays are always product specific in Make.
  670. if _, ok := mod.GetProperty("product_specific"); !ok {
  671. prop := &parser.Property{
  672. Name: "product_specific",
  673. Value: &parser.Bool{
  674. Value: true,
  675. },
  676. }
  677. mod.Properties = append(mod.Properties, prop)
  678. }
  679. }
  680. return nil
  681. }
  682. // Removes library dependencies which are empty (and restricted from usage in Soong)
  683. func removeEmptyLibDependencies(f *Fixer) error {
  684. emptyLibraries := []string{
  685. "libhidltransport",
  686. "libhwbinder",
  687. }
  688. relevantFields := []string{
  689. "export_shared_lib_headers",
  690. "export_static_lib_headers",
  691. "static_libs",
  692. "whole_static_libs",
  693. "shared_libs",
  694. }
  695. for _, def := range f.tree.Defs {
  696. mod, ok := def.(*parser.Module)
  697. if !ok {
  698. continue
  699. }
  700. for _, field := range relevantFields {
  701. listValue, ok := getLiteralListProperty(mod, field)
  702. if !ok {
  703. continue
  704. }
  705. newValues := []parser.Expression{}
  706. for _, v := range listValue.Values {
  707. stringValue, ok := v.(*parser.String)
  708. if !ok {
  709. return fmt.Errorf("Expecting string for %s.%s fields", mod.Type, field)
  710. }
  711. if inList(stringValue.Value, emptyLibraries) {
  712. continue
  713. }
  714. newValues = append(newValues, stringValue)
  715. }
  716. if len(newValues) == 0 && len(listValue.Values) != 0 {
  717. removeProperty(mod, field)
  718. } else {
  719. listValue.Values = newValues
  720. }
  721. }
  722. }
  723. return nil
  724. }
  725. // Removes hidl_interface 'types' which are no longer needed
  726. func removeHidlInterfaceTypes(f *Fixer) error {
  727. for _, def := range f.tree.Defs {
  728. mod, ok := def.(*parser.Module)
  729. if !(ok && mod.Type == "hidl_interface") {
  730. continue
  731. }
  732. removeProperty(mod, "types")
  733. }
  734. return nil
  735. }
  736. func removeSoongConfigBoolVariable(f *Fixer) error {
  737. found := map[string]bool{}
  738. newDefs := make([]parser.Definition, 0, len(f.tree.Defs))
  739. for _, def := range f.tree.Defs {
  740. if mod, ok := def.(*parser.Module); ok && mod.Type == "soong_config_bool_variable" {
  741. if name, ok := getLiteralStringPropertyValue(mod, "name"); ok {
  742. found[name] = true
  743. } else {
  744. return fmt.Errorf("Found soong_config_bool_variable without a name")
  745. }
  746. } else {
  747. newDefs = append(newDefs, def)
  748. }
  749. }
  750. f.tree.Defs = newDefs
  751. if len(found) == 0 {
  752. return nil
  753. }
  754. return runPatchListMod(func(mod *parser.Module, buf []byte, patchList *parser.PatchList) error {
  755. if mod.Type != "soong_config_module_type" {
  756. return nil
  757. }
  758. variables, ok := getLiteralListProperty(mod, "variables")
  759. if !ok {
  760. return nil
  761. }
  762. boolValues := strings.Builder{}
  763. empty := true
  764. for _, item := range variables.Values {
  765. nameValue, ok := item.(*parser.String)
  766. if !ok {
  767. empty = false
  768. continue
  769. }
  770. if found[nameValue.Value] {
  771. patchList.Add(item.Pos().Offset, item.End().Offset+2, "")
  772. boolValues.WriteString(`"`)
  773. boolValues.WriteString(nameValue.Value)
  774. boolValues.WriteString(`",`)
  775. } else {
  776. empty = false
  777. }
  778. }
  779. if empty {
  780. *patchList = parser.PatchList{}
  781. prop, _ := mod.GetProperty("variables")
  782. patchList.Add(prop.Pos().Offset, prop.End().Offset+2, "")
  783. }
  784. if boolValues.Len() == 0 {
  785. return nil
  786. }
  787. boolVariables, ok := getLiteralListProperty(mod, "bool_variables")
  788. if ok {
  789. patchList.Add(boolVariables.RBracePos.Offset, boolVariables.RBracePos.Offset, ","+boolValues.String())
  790. } else {
  791. patchList.Add(variables.RBracePos.Offset+2, variables.RBracePos.Offset+2,
  792. fmt.Sprintf(`bool_variables: [%s],`, boolValues.String()))
  793. }
  794. return nil
  795. })(f)
  796. return nil
  797. }
  798. func removeResourceAndAssetsIfDefault(f *Fixer) error {
  799. for _, def := range f.tree.Defs {
  800. mod, ok := def.(*parser.Module)
  801. if !ok {
  802. continue
  803. }
  804. resourceDirList, resourceDirFound := getLiteralListPropertyValue(mod, "resource_dirs")
  805. if resourceDirFound && len(resourceDirList) == 1 && resourceDirList[0] == "res" {
  806. removeProperty(mod, "resource_dirs")
  807. }
  808. assetDirList, assetDirFound := getLiteralListPropertyValue(mod, "asset_dirs")
  809. if assetDirFound && len(assetDirList) == 1 && assetDirList[0] == "assets" {
  810. removeProperty(mod, "asset_dirs")
  811. }
  812. }
  813. return nil
  814. }
  815. // Converts the default source list property, 'srcs', to a single source property with a given name.
  816. // "LOCAL_MODULE" reference is also resolved during the conversion process.
  817. func convertToSingleSource(mod *parser.Module, srcPropertyName string) {
  818. if srcs, ok := mod.GetProperty("srcs"); ok {
  819. if srcList, ok := srcs.Value.(*parser.List); ok {
  820. removeProperty(mod, "srcs")
  821. if len(srcList.Values) == 1 {
  822. mod.Properties = append(mod.Properties,
  823. &parser.Property{
  824. Name: srcPropertyName,
  825. NamePos: srcs.NamePos,
  826. ColonPos: srcs.ColonPos,
  827. Value: resolveLocalModule(mod, srcList.Values[0])})
  828. } else if len(srcList.Values) > 1 {
  829. indicateAttributeError(mod, srcPropertyName, "LOCAL_SRC_FILES should contain at most one item")
  830. }
  831. } else if _, ok = srcs.Value.(*parser.Variable); ok {
  832. removeProperty(mod, "srcs")
  833. mod.Properties = append(mod.Properties,
  834. &parser.Property{Name: srcPropertyName,
  835. NamePos: srcs.NamePos,
  836. ColonPos: srcs.ColonPos,
  837. Value: resolveLocalModule(mod, srcs.Value)})
  838. } else {
  839. renameProperty(mod, "srcs", "apk")
  840. }
  841. }
  842. }
  843. type patchListModFunction func(*parser.Module, []byte, *parser.PatchList) error
  844. func runPatchListMod(modFunc patchListModFunction) func(*Fixer) error {
  845. return func(f *Fixer) error {
  846. // Make sure all the offsets are accurate
  847. buf, err := f.reparse()
  848. if err != nil {
  849. return err
  850. }
  851. var patchlist parser.PatchList
  852. for _, def := range f.tree.Defs {
  853. mod, ok := def.(*parser.Module)
  854. if !ok {
  855. continue
  856. }
  857. err := modFunc(mod, buf, &patchlist)
  858. if err != nil {
  859. return err
  860. }
  861. }
  862. newBuf := new(bytes.Buffer)
  863. err = patchlist.Apply(bytes.NewReader(buf), newBuf)
  864. if err != nil {
  865. return err
  866. }
  867. // Save a copy of the buffer to print for errors below
  868. bufCopy := append([]byte(nil), newBuf.Bytes()...)
  869. newTree, err := parse(f.tree.Name, newBuf)
  870. if err != nil {
  871. return fmt.Errorf("Failed to parse: %v\nBuffer:\n%s", err, string(bufCopy))
  872. }
  873. f.tree = newTree
  874. return nil
  875. }
  876. }
  877. var commonPropertyPriorities = []string{
  878. "name",
  879. "defaults",
  880. "device_supported",
  881. "host_supported",
  882. "installable",
  883. }
  884. func reorderCommonProperties(mod *parser.Module, buf []byte, patchlist *parser.PatchList) error {
  885. if len(mod.Properties) == 0 {
  886. return nil
  887. }
  888. pos := mod.LBracePos.Offset + 1
  889. stage := ""
  890. for _, name := range commonPropertyPriorities {
  891. idx := propertyIndex(mod.Properties, name)
  892. if idx == -1 {
  893. continue
  894. }
  895. if idx == 0 {
  896. err := patchlist.Add(pos, pos, stage)
  897. if err != nil {
  898. return err
  899. }
  900. stage = ""
  901. pos = mod.Properties[0].End().Offset + 1
  902. mod.Properties = mod.Properties[1:]
  903. continue
  904. }
  905. prop := mod.Properties[idx]
  906. mod.Properties = append(mod.Properties[:idx], mod.Properties[idx+1:]...)
  907. stage += string(buf[prop.Pos().Offset : prop.End().Offset+1])
  908. err := patchlist.Add(prop.Pos().Offset, prop.End().Offset+2, "")
  909. if err != nil {
  910. return err
  911. }
  912. }
  913. if stage != "" {
  914. err := patchlist.Add(pos, pos, stage)
  915. if err != nil {
  916. return err
  917. }
  918. }
  919. return nil
  920. }
  921. func removeTags(mod *parser.Module, buf []byte, patchlist *parser.PatchList) error {
  922. prop, ok := mod.GetProperty("tags")
  923. if !ok {
  924. return nil
  925. }
  926. list, ok := prop.Value.(*parser.List)
  927. if !ok {
  928. return nil
  929. }
  930. replaceStr := ""
  931. for _, item := range list.Values {
  932. str, ok := item.(*parser.String)
  933. if !ok {
  934. replaceStr += fmt.Sprintf("// ERROR: Unable to parse tag %q\n", item)
  935. continue
  936. }
  937. switch str.Value {
  938. case "optional":
  939. continue
  940. case "debug":
  941. replaceStr += `// WARNING: Module tags are not supported in Soong.
  942. // Add this module to PRODUCT_PACKAGES_DEBUG in your product file if you want to
  943. // force installation for -userdebug and -eng builds.
  944. `
  945. case "eng":
  946. replaceStr += `// WARNING: Module tags are not supported in Soong.
  947. // Add this module to PRODUCT_PACKAGES_ENG in your product file if you want to
  948. // force installation for -eng builds.
  949. `
  950. case "tests":
  951. switch {
  952. case strings.Contains(mod.Type, "cc_test"),
  953. strings.Contains(mod.Type, "cc_library_static"),
  954. strings.Contains(mod.Type, "java_test"),
  955. mod.Type == "android_test",
  956. mod.Type == "android_test_import":
  957. continue
  958. case strings.Contains(mod.Type, "cc_lib"):
  959. replaceStr += `// WARNING: Module tags are not supported in Soong.
  960. // To make a shared library only for tests, use the "cc_test_library" module
  961. // type. If you don't use gtest, set "gtest: false".
  962. `
  963. case strings.Contains(mod.Type, "cc_bin"):
  964. replaceStr += `// WARNING: Module tags are not supported in Soong.
  965. // For native test binaries, use the "cc_test" module type. Some differences:
  966. // - If you don't use gtest, set "gtest: false"
  967. // - Binaries will be installed into /data/nativetest[64]/<name>/<name>
  968. // - Both 32 & 64 bit versions will be built (as appropriate)
  969. `
  970. case strings.Contains(mod.Type, "java_lib"):
  971. replaceStr += `// WARNING: Module tags are not supported in Soong.
  972. // For JUnit or similar tests, use the "java_test" module type. A dependency on
  973. // Junit will be added by default, if it is using some other runner, set "junit: false".
  974. `
  975. case mod.Type == "android_app":
  976. replaceStr += `// WARNING: Module tags are not supported in Soong.
  977. // For JUnit or instrumentataion app tests, use the "android_test" module type.
  978. `
  979. default:
  980. replaceStr += `// WARNING: Module tags are not supported in Soong.
  981. // In most cases, tests are now identified by their module type:
  982. // cc_test, java_test, python_test
  983. `
  984. }
  985. default:
  986. replaceStr += fmt.Sprintf("// WARNING: Unknown module tag %q\n", str.Value)
  987. }
  988. }
  989. return patchlist.Add(prop.Pos().Offset, prop.End().Offset+2, replaceStr)
  990. }
  991. type propertyProvider interface {
  992. GetProperty(string) (*parser.Property, bool)
  993. RemoveProperty(string) bool
  994. }
  995. func removeNestedProperty(mod *parser.Module, patchList *parser.PatchList, propName string) error {
  996. propNames := strings.Split(propName, ".")
  997. var propProvider, toRemoveFrom propertyProvider
  998. propProvider = mod
  999. var propToRemove *parser.Property
  1000. for i, name := range propNames {
  1001. p, ok := propProvider.GetProperty(name)
  1002. if !ok {
  1003. return nil
  1004. }
  1005. // if this is the inner most element, it's time to delete
  1006. if i == len(propNames)-1 {
  1007. if propToRemove == nil {
  1008. // if we cannot remove the properties that the current property is nested in,
  1009. // remove only the current property
  1010. propToRemove = p
  1011. toRemoveFrom = propProvider
  1012. }
  1013. // remove the property from the list, in case we remove other properties in this list
  1014. toRemoveFrom.RemoveProperty(propToRemove.Name)
  1015. // only removing the property would leave blank line(s), remove with a patch
  1016. if err := patchList.Add(propToRemove.Pos().Offset, propToRemove.End().Offset+2, ""); err != nil {
  1017. return err
  1018. }
  1019. } else {
  1020. propMap, ok := p.Value.(*parser.Map)
  1021. if !ok {
  1022. return nil
  1023. }
  1024. if len(propMap.Properties) > 1 {
  1025. // if there are other properties in this struct, we need to keep this struct
  1026. toRemoveFrom = nil
  1027. propToRemove = nil
  1028. } else if propToRemove == nil {
  1029. // otherwise, we can remove the empty struct entirely
  1030. toRemoveFrom = propProvider
  1031. propToRemove = p
  1032. }
  1033. propProvider = propMap
  1034. }
  1035. }
  1036. return nil
  1037. }
  1038. func removeObsoleteProperty(propName string) patchListModFunction {
  1039. return func(mod *parser.Module, buf []byte, patchList *parser.PatchList) error {
  1040. return removeNestedProperty(mod, patchList, propName)
  1041. }
  1042. }
  1043. func mergeMatchingModuleProperties(mod *parser.Module, buf []byte, patchlist *parser.PatchList) error {
  1044. return mergeMatchingProperties(&mod.Properties, buf, patchlist)
  1045. }
  1046. func mergeMatchingProperties(properties *[]*parser.Property, buf []byte, patchlist *parser.PatchList) error {
  1047. seen := make(map[string]*parser.Property)
  1048. for i := 0; i < len(*properties); i++ {
  1049. property := (*properties)[i]
  1050. if prev, exists := seen[property.Name]; exists {
  1051. err := mergeProperties(prev, property, buf, patchlist)
  1052. if err != nil {
  1053. return err
  1054. }
  1055. *properties = append((*properties)[:i], (*properties)[i+1:]...)
  1056. } else {
  1057. seen[property.Name] = property
  1058. if mapProperty, ok := property.Value.(*parser.Map); ok {
  1059. err := mergeMatchingProperties(&mapProperty.Properties, buf, patchlist)
  1060. if err != nil {
  1061. return err
  1062. }
  1063. }
  1064. }
  1065. }
  1066. return nil
  1067. }
  1068. func mergeProperties(a, b *parser.Property, buf []byte, patchlist *parser.PatchList) error {
  1069. // The value of one of the properties may be a variable reference with no type assigned
  1070. // Bail out in this case. Soong will notice duplicate entries and will tell to merge them.
  1071. if _, isVar := a.Value.(*parser.Variable); isVar {
  1072. return nil
  1073. }
  1074. if _, isVar := b.Value.(*parser.Variable); isVar {
  1075. return nil
  1076. }
  1077. if a.Value.Type() != b.Value.Type() {
  1078. return fmt.Errorf("type mismatch when merging properties %q: %s and %s", a.Name, a.Value.Type(), b.Value.Type())
  1079. }
  1080. switch a.Value.Type() {
  1081. case parser.StringType:
  1082. return fmt.Errorf("conflicting definitions of string property %q", a.Name)
  1083. case parser.ListType:
  1084. return mergeListProperties(a, b, buf, patchlist)
  1085. }
  1086. return nil
  1087. }
  1088. func mergeListProperties(a, b *parser.Property, buf []byte, patchlist *parser.PatchList) error {
  1089. aval, oka := a.Value.(*parser.List)
  1090. bval, okb := b.Value.(*parser.List)
  1091. if !oka || !okb {
  1092. // Merging expressions not supported yet
  1093. return nil
  1094. }
  1095. s := string(buf[bval.LBracePos.Offset+1 : bval.RBracePos.Offset])
  1096. if bval.LBracePos.Line != bval.RBracePos.Line {
  1097. if s[0] != '\n' {
  1098. panic("expected \n")
  1099. }
  1100. // If B is a multi line list, skip the first "\n" in case A already has a trailing "\n"
  1101. s = s[1:]
  1102. }
  1103. if aval.LBracePos.Line == aval.RBracePos.Line {
  1104. // A is a single line list with no trailing comma
  1105. if len(aval.Values) > 0 {
  1106. s = "," + s
  1107. }
  1108. }
  1109. err := patchlist.Add(aval.RBracePos.Offset, aval.RBracePos.Offset, s)
  1110. if err != nil {
  1111. return err
  1112. }
  1113. err = patchlist.Add(b.NamePos.Offset, b.End().Offset+2, "")
  1114. if err != nil {
  1115. return err
  1116. }
  1117. return nil
  1118. }
  1119. // removes from <items> every item present in <removals>
  1120. func filterExpressionList(patchList *parser.PatchList, items *parser.List, removals *parser.List) {
  1121. writeIndex := 0
  1122. for _, item := range items.Values {
  1123. included := true
  1124. for _, removal := range removals.Values {
  1125. equal, err := parser.ExpressionsAreSame(item, removal)
  1126. if err != nil {
  1127. continue
  1128. }
  1129. if equal {
  1130. included = false
  1131. break
  1132. }
  1133. }
  1134. if included {
  1135. items.Values[writeIndex] = item
  1136. writeIndex++
  1137. } else {
  1138. patchList.Add(item.Pos().Offset, item.End().Offset+2, "")
  1139. }
  1140. }
  1141. items.Values = items.Values[:writeIndex]
  1142. }
  1143. // Remove each modules[i].Properties[<legacyName>][j] that matches a modules[i].Properties[<canonicalName>][k]
  1144. func removeMatchingModuleListProperties(mod *parser.Module, patchList *parser.PatchList, canonicalName string, legacyName string) error {
  1145. legacyProp, ok := mod.GetProperty(legacyName)
  1146. if !ok {
  1147. return nil
  1148. }
  1149. legacyList, ok := legacyProp.Value.(*parser.List)
  1150. if !ok || len(legacyList.Values) == 0 {
  1151. return nil
  1152. }
  1153. canonicalList, ok := getLiteralListProperty(mod, canonicalName)
  1154. if !ok {
  1155. return nil
  1156. }
  1157. localPatches := parser.PatchList{}
  1158. filterExpressionList(&localPatches, legacyList, canonicalList)
  1159. if len(legacyList.Values) == 0 {
  1160. patchList.Add(legacyProp.Pos().Offset, legacyProp.End().Offset+2, "")
  1161. } else {
  1162. for _, p := range localPatches {
  1163. patchList.Add(p.Start, p.End, p.Replacement)
  1164. }
  1165. }
  1166. return nil
  1167. }
  1168. func hasNonEmptyLiteralListProperty(mod *parser.Module, name string) bool {
  1169. list, found := getLiteralListProperty(mod, name)
  1170. return found && len(list.Values) > 0
  1171. }
  1172. func hasNonEmptyLiteralStringProperty(mod *parser.Module, name string) bool {
  1173. s, found := getLiteralStringPropertyValue(mod, name)
  1174. return found && len(s) > 0
  1175. }
  1176. func getLiteralListProperty(mod *parser.Module, name string) (list *parser.List, found bool) {
  1177. prop, ok := mod.GetProperty(name)
  1178. if !ok {
  1179. return nil, false
  1180. }
  1181. list, ok = prop.Value.(*parser.List)
  1182. return list, ok
  1183. }
  1184. func getLiteralListPropertyValue(mod *parser.Module, name string) (list []string, found bool) {
  1185. listValue, ok := getLiteralListProperty(mod, name)
  1186. if !ok {
  1187. return nil, false
  1188. }
  1189. for _, v := range listValue.Values {
  1190. stringValue, ok := v.(*parser.String)
  1191. if !ok {
  1192. return nil, false
  1193. }
  1194. list = append(list, stringValue.Value)
  1195. }
  1196. return list, true
  1197. }
  1198. func getLiteralStringProperty(mod *parser.Module, name string) (s *parser.String, found bool) {
  1199. prop, ok := mod.GetProperty(name)
  1200. if !ok {
  1201. return nil, false
  1202. }
  1203. s, ok = prop.Value.(*parser.String)
  1204. return s, ok
  1205. }
  1206. func getLiteralStringPropertyValue(mod *parser.Module, name string) (s string, found bool) {
  1207. stringValue, ok := getLiteralStringProperty(mod, name)
  1208. if !ok {
  1209. return "", false
  1210. }
  1211. return stringValue.Value, true
  1212. }
  1213. func getLiteralBoolProperty(mod *parser.Module, name string) (b *parser.Bool, found bool) {
  1214. prop, ok := mod.GetProperty(name)
  1215. if !ok {
  1216. return nil, false
  1217. }
  1218. b, ok = prop.Value.(*parser.Bool)
  1219. return b, ok
  1220. }
  1221. func getLiteralBoolPropertyValue(mod *parser.Module, name string) (s bool, found bool) {
  1222. boolValue, ok := getLiteralBoolProperty(mod, name)
  1223. if !ok {
  1224. return false, false
  1225. }
  1226. return boolValue.Value, true
  1227. }
  1228. func propertyIndex(props []*parser.Property, propertyName string) int {
  1229. for i, prop := range props {
  1230. if prop.Name == propertyName {
  1231. return i
  1232. }
  1233. }
  1234. return -1
  1235. }
  1236. func renameProperty(mod *parser.Module, from, to string) {
  1237. for _, prop := range mod.Properties {
  1238. if prop.Name == from {
  1239. prop.Name = to
  1240. }
  1241. }
  1242. }
  1243. func removeProperty(mod *parser.Module, propertyName string) {
  1244. newList := make([]*parser.Property, 0, len(mod.Properties))
  1245. for _, prop := range mod.Properties {
  1246. if prop.Name != propertyName {
  1247. newList = append(newList, prop)
  1248. }
  1249. }
  1250. mod.Properties = newList
  1251. }
  1252. func inList(s string, list []string) bool {
  1253. for _, v := range list {
  1254. if s == v {
  1255. return true
  1256. }
  1257. }
  1258. return false
  1259. }
  1260. func formatFlagProperty(mod *parser.Module, field string, buf []byte, patchlist *parser.PatchList) error {
  1261. // the comment or empty lines in the value of the field are skipped
  1262. listValue, ok := getLiteralListProperty(mod, field)
  1263. if !ok {
  1264. // if do not find
  1265. return nil
  1266. }
  1267. for i := 0; i < len(listValue.Values); i++ {
  1268. curValue, ok := listValue.Values[i].(*parser.String)
  1269. if !ok {
  1270. return fmt.Errorf("Expecting string for %s.%s fields", mod.Type, field)
  1271. }
  1272. if !strings.HasPrefix(curValue.Value, "-") {
  1273. return fmt.Errorf("Expecting the string `%s` starting with '-'", curValue.Value)
  1274. }
  1275. if i+1 < len(listValue.Values) {
  1276. nextValue, ok := listValue.Values[i+1].(*parser.String)
  1277. if !ok {
  1278. return fmt.Errorf("Expecting string for %s.%s fields", mod.Type, field)
  1279. }
  1280. if !strings.HasPrefix(nextValue.Value, "-") {
  1281. // delete the line
  1282. err := patchlist.Add(curValue.Pos().Offset, curValue.End().Offset+2, "")
  1283. if err != nil {
  1284. return err
  1285. }
  1286. // replace the line
  1287. value := "\"" + curValue.Value + " " + nextValue.Value + "\","
  1288. err = patchlist.Add(nextValue.Pos().Offset, nextValue.End().Offset+1, value)
  1289. if err != nil {
  1290. return err
  1291. }
  1292. // combined two lines to one
  1293. i++
  1294. }
  1295. }
  1296. }
  1297. return nil
  1298. }
  1299. func formatFlagProperties(mod *parser.Module, buf []byte, patchlist *parser.PatchList) error {
  1300. relevantFields := []string{
  1301. // cc flags
  1302. "asflags",
  1303. "cflags",
  1304. "clang_asflags",
  1305. "clang_cflags",
  1306. "conlyflags",
  1307. "cppflags",
  1308. "ldflags",
  1309. "tidy_flags",
  1310. // java flags
  1311. "aaptflags",
  1312. "dxflags",
  1313. "javacflags",
  1314. "kotlincflags",
  1315. }
  1316. for _, field := range relevantFields {
  1317. err := formatFlagProperty(mod, field, buf, patchlist)
  1318. if err != nil {
  1319. return err
  1320. }
  1321. }
  1322. return nil
  1323. }
  1324. func rewriteLicenseProperty(fs pathtools.FileSystem, relativePath string) patchListModFunction {
  1325. return func(mod *parser.Module, buf []byte, patchList *parser.PatchList) error {
  1326. return rewriteLicenseProperties(mod, patchList, fs, relativePath)
  1327. }
  1328. }
  1329. // rewrite the "android_license_kinds" and "android_license_files" properties to a package module
  1330. // (and a license module when needed).
  1331. func rewriteLicenseProperties(mod *parser.Module, patchList *parser.PatchList, fs pathtools.FileSystem,
  1332. relativePath string) error {
  1333. // if a package module has been added, no more action is needed.
  1334. for _, patch := range *patchList {
  1335. if strings.Contains(patch.Replacement, "package {") {
  1336. return nil
  1337. }
  1338. }
  1339. // initial the fs
  1340. if fs == nil {
  1341. fs = pathtools.NewOsFs(os.Getenv("ANDROID_BUILD_TOP"))
  1342. }
  1343. // initial the relativePath
  1344. if len(relativePath) == 0 {
  1345. relativePath = getModuleRelativePath()
  1346. }
  1347. // validate the relativePath
  1348. ok := hasFile(relativePath+"/Android.mk", fs)
  1349. // some modules in the existing test cases in the androidmk_test.go do not have a valid path
  1350. if !ok && len(relativePath) > 0 {
  1351. return fmt.Errorf("Cannot find an Android.mk file at path %q", relativePath)
  1352. }
  1353. licenseKindsPropertyName := "android_license_kinds"
  1354. licenseFilesPropertyName := "android_license_files"
  1355. androidBpFileErr := "// Error: No Android.bp file is found at path\n" +
  1356. "// %s\n" +
  1357. "// Please add one there with the needed license module first.\n" +
  1358. "// Then reset the default_applicable_licenses property below with the license module name.\n"
  1359. licenseModuleErr := "// Error: Cannot get the name of the license module in the\n" +
  1360. "// %s file.\n" +
  1361. "// If no such license module exists, please add one there first.\n" +
  1362. "// Then reset the default_applicable_licenses property below with the license module name.\n"
  1363. defaultApplicableLicense := "Android-Apache-2.0"
  1364. var licenseModuleName, licensePatch string
  1365. var hasFileInParentDir bool
  1366. // when LOCAL_NOTICE_FILE is not empty
  1367. if hasNonEmptyLiteralListProperty(mod, licenseFilesPropertyName) {
  1368. hasFileInParentDir = hasValueStartWithTwoDotsLiteralList(mod, licenseFilesPropertyName)
  1369. // if have LOCAL_NOTICE_FILE outside the current directory, need to find and refer to the license
  1370. // module in the LOCAL_NOTICE_FILE location directly and no new license module needs to be created
  1371. if hasFileInParentDir {
  1372. bpPath, ok := getPathFromProperty(mod, licenseFilesPropertyName, fs, relativePath)
  1373. if !ok {
  1374. bpDir, err := getDirFromProperty(mod, licenseFilesPropertyName, fs, relativePath)
  1375. if err != nil {
  1376. return err
  1377. }
  1378. licensePatch += fmt.Sprintf(androidBpFileErr, bpDir)
  1379. defaultApplicableLicense = ""
  1380. } else {
  1381. licenseModuleName, _ = getModuleName(bpPath, "license", fs)
  1382. if len(licenseModuleName) == 0 {
  1383. licensePatch += fmt.Sprintf(licenseModuleErr, bpPath)
  1384. }
  1385. defaultApplicableLicense = licenseModuleName
  1386. }
  1387. } else {
  1388. // if have LOCAL_NOTICE_FILE in the current directory, need to create a new license module
  1389. if len(relativePath) == 0 {
  1390. return fmt.Errorf("Cannot obtain the relative path of the Android.mk file")
  1391. }
  1392. licenseModuleName = strings.Replace(relativePath, "/", "_", -1) + "_license"
  1393. defaultApplicableLicense = licenseModuleName
  1394. }
  1395. }
  1396. //add the package module
  1397. if hasNonEmptyLiteralListProperty(mod, licenseKindsPropertyName) {
  1398. licensePatch += "package {\n" +
  1399. " // See: http://go/android-license-faq\n" +
  1400. " default_applicable_licenses: [\n" +
  1401. " \"" + defaultApplicableLicense + "\",\n" +
  1402. " ],\n" +
  1403. "}\n" +
  1404. "\n"
  1405. }
  1406. // append the license module when necessary
  1407. // when LOCAL_NOTICE_FILE is not empty and in the current directory, create a new license module
  1408. // otherwise, use the above default license directly
  1409. if hasNonEmptyLiteralListProperty(mod, licenseFilesPropertyName) && !hasFileInParentDir {
  1410. licenseKinds, err := mergeLiteralListPropertyValue(mod, licenseKindsPropertyName)
  1411. if err != nil {
  1412. return err
  1413. }
  1414. licenseFiles, err := mergeLiteralListPropertyValue(mod, licenseFilesPropertyName)
  1415. if err != nil {
  1416. return err
  1417. }
  1418. licensePatch += "license {\n" +
  1419. " name: \"" + licenseModuleName + "\",\n" +
  1420. " visibility: [\":__subpackages__\"],\n" +
  1421. " license_kinds: [\n" +
  1422. licenseKinds +
  1423. " ],\n" +
  1424. " license_text: [\n" +
  1425. licenseFiles +
  1426. " ],\n" +
  1427. "}\n" +
  1428. "\n"
  1429. }
  1430. // add to the patchList
  1431. pos := mod.Pos().Offset
  1432. err := patchList.Add(pos, pos, licensePatch)
  1433. if err != nil {
  1434. return err
  1435. }
  1436. return nil
  1437. }
  1438. // merge the string vaules in a list property of a module into one string with expected format
  1439. func mergeLiteralListPropertyValue(mod *parser.Module, property string) (s string, err error) {
  1440. listValue, ok := getLiteralListPropertyValue(mod, property)
  1441. if !ok {
  1442. // if do not find
  1443. return "", fmt.Errorf("Cannot retrieve the %s.%s field", mod.Type, property)
  1444. }
  1445. for i := 0; i < len(listValue); i++ {
  1446. s += " \"" + listValue[i] + "\",\n"
  1447. }
  1448. return s, nil
  1449. }
  1450. // check whether a string list property has any value starting with `../`
  1451. func hasValueStartWithTwoDotsLiteralList(mod *parser.Module, property string) bool {
  1452. listValue, ok := getLiteralListPropertyValue(mod, property)
  1453. if ok {
  1454. for i := 0; i < len(listValue); i++ {
  1455. if strings.HasPrefix(listValue[i], "../") {
  1456. return true
  1457. }
  1458. }
  1459. }
  1460. return false
  1461. }
  1462. // get the relative path from ANDROID_BUILD_TOP to the Android.mk file to be converted
  1463. func getModuleRelativePath() string {
  1464. // get the absolute path of the top of the tree
  1465. rootPath := os.Getenv("ANDROID_BUILD_TOP")
  1466. // get the absolute path of the `Android.mk` file to be converted
  1467. absPath := getModuleAbsolutePath()
  1468. // get the relative path of the `Android.mk` file to top of the tree
  1469. relModulePath, err := filepath.Rel(rootPath, absPath)
  1470. if err != nil {
  1471. return ""
  1472. }
  1473. return relModulePath
  1474. }
  1475. // get the absolute path of the Android.mk file to be converted
  1476. func getModuleAbsolutePath() string {
  1477. // get the absolute path at where the `androidmk` commend is executed
  1478. curAbsPath, err := filepath.Abs(".")
  1479. if err != nil {
  1480. return ""
  1481. }
  1482. // the argument for the `androidmk` command could be
  1483. // 1. "./a/b/c/Android.mk"; 2. "a/b/c/Android.mk"; 3. "Android.mk"
  1484. argPath := flag.Arg(0)
  1485. if strings.HasPrefix(argPath, "./") {
  1486. argPath = strings.TrimPrefix(argPath, ".")
  1487. }
  1488. argPath = strings.TrimSuffix(argPath, "Android.mk")
  1489. if strings.HasSuffix(argPath, "/") {
  1490. argPath = strings.TrimSuffix(argPath, "/")
  1491. }
  1492. if len(argPath) > 0 && !strings.HasPrefix(argPath, "/") {
  1493. argPath = "/" + argPath
  1494. }
  1495. // get the absolute path of the `Android.mk` file to be converted
  1496. absPath := curAbsPath + argPath
  1497. return absPath
  1498. }
  1499. // check whether a file exists in a filesystem
  1500. func hasFile(path string, fs pathtools.FileSystem) bool {
  1501. ok, _, _ := fs.Exists(path)
  1502. return ok
  1503. }
  1504. // get the directory where an `Android.bp` file and the property files are expected to locate
  1505. func getDirFromProperty(mod *parser.Module, property string, fs pathtools.FileSystem, relativePath string) (string, error) {
  1506. listValue, ok := getLiteralListPropertyValue(mod, property)
  1507. if !ok {
  1508. // if do not find
  1509. return "", fmt.Errorf("Cannot retrieve the %s.%s property", mod.Type, property)
  1510. }
  1511. if len(listValue) == 0 {
  1512. // if empty
  1513. return "", fmt.Errorf("Cannot find the value of the %s.%s property", mod.Type, property)
  1514. }
  1515. if relativePath == "" {
  1516. relativePath = "."
  1517. }
  1518. _, isDir, _ := fs.Exists(relativePath)
  1519. if !isDir {
  1520. return "", fmt.Errorf("Cannot find the path %q", relativePath)
  1521. }
  1522. path := relativePath
  1523. for {
  1524. if !strings.HasPrefix(listValue[0], "../") {
  1525. break
  1526. }
  1527. path = filepath.Dir(path)
  1528. listValue[0] = strings.TrimPrefix(listValue[0], "../")
  1529. }
  1530. _, isDir, _ = fs.Exists(path)
  1531. if !isDir {
  1532. return "", fmt.Errorf("Cannot find the path %q", path)
  1533. }
  1534. return path, nil
  1535. }
  1536. // get the path of the `Android.bp` file at the expected location where the property files locate
  1537. func getPathFromProperty(mod *parser.Module, property string, fs pathtools.FileSystem, relativePath string) (string, bool) {
  1538. dir, err := getDirFromProperty(mod, property, fs, relativePath)
  1539. if err != nil {
  1540. return "", false
  1541. }
  1542. ok := hasFile(dir+"/Android.bp", fs)
  1543. if !ok {
  1544. return "", false
  1545. }
  1546. return dir + "/Android.bp", true
  1547. }
  1548. // parse an Android.bp file to get the name of the first module with type of moduleType
  1549. func getModuleName(path string, moduleType string, fs pathtools.FileSystem) (string, error) {
  1550. tree, err := parserPath(path, fs)
  1551. if err != nil {
  1552. return "", err
  1553. }
  1554. for _, def := range tree.Defs {
  1555. mod, ok := def.(*parser.Module)
  1556. if !ok || mod.Type != moduleType {
  1557. continue
  1558. }
  1559. prop, ok := mod.GetProperty("name")
  1560. if !ok {
  1561. return "", fmt.Errorf("Cannot get the %s."+"name property", mod.Type)
  1562. }
  1563. propVal, ok := prop.Value.(*parser.String)
  1564. if ok {
  1565. return propVal.Value, nil
  1566. }
  1567. }
  1568. return "", fmt.Errorf("Cannot find the value of the %s."+"name property", moduleType)
  1569. }
  1570. // parse an Android.bp file with the specific path
  1571. func parserPath(path string, fs pathtools.FileSystem) (tree *parser.File, err error) {
  1572. f, err := fs.Open(path)
  1573. if err != nil {
  1574. return tree, err
  1575. }
  1576. defer f.Close()
  1577. fileContent, _ := ioutil.ReadAll(f)
  1578. tree, err = parse(path, bytes.NewBufferString(string(fileContent)))
  1579. if err != nil {
  1580. return tree, err
  1581. }
  1582. return tree, nil
  1583. }
  1584. // remove the incorrect property that Soong does not support
  1585. func removeIncorrectProperties(propName string) patchListModFunction {
  1586. return removeObsoleteProperty(propName)
  1587. }
  1588. // the modules on the same Android.mk file are expected to have the same license
  1589. func haveSameLicense(f *Fixer) error {
  1590. androidLicenseProperties := []string{
  1591. "android_license_kinds",
  1592. "android_license_conditions",
  1593. "android_license_files",
  1594. }
  1595. var prevModuleName string
  1596. var prevLicenseKindsVals, prevLicenseConditionsVals, prevLicenseFilesVals []string
  1597. prevLicenseVals := [][]string{
  1598. prevLicenseKindsVals,
  1599. prevLicenseConditionsVals,
  1600. prevLicenseFilesVals,
  1601. }
  1602. for _, def := range f.tree.Defs {
  1603. mod, ok := def.(*parser.Module)
  1604. if !ok {
  1605. continue
  1606. }
  1607. for idx, property := range androidLicenseProperties {
  1608. curModuleName, ok := getLiteralStringPropertyValue(mod, "name")
  1609. // some modules in the existing test cases in the androidmk_test.go do not have name property
  1610. hasNameProperty := hasProperty(mod, "name")
  1611. if hasNameProperty && (!ok || len(curModuleName) == 0) {
  1612. return fmt.Errorf("Cannot retrieve the name property of a module of %s type.", mod.Type)
  1613. }
  1614. curVals, ok := getLiteralListPropertyValue(mod, property)
  1615. // some modules in the existing test cases in the androidmk_test.go do not have license-related property
  1616. hasLicenseProperty := hasProperty(mod, property)
  1617. if hasLicenseProperty && (!ok || len(curVals) == 0) {
  1618. // if do not find the property, or no value is found for the property
  1619. return fmt.Errorf("Cannot retrieve the %s.%s property", mod.Type, property)
  1620. }
  1621. if len(prevLicenseVals[idx]) > 0 {
  1622. if !reflect.DeepEqual(prevLicenseVals[idx], curVals) {
  1623. return fmt.Errorf("Modules %s and %s are expected to have the same %s property.",
  1624. prevModuleName, curModuleName, property)
  1625. }
  1626. }
  1627. sort.Strings(curVals)
  1628. prevLicenseVals[idx] = curVals
  1629. prevModuleName = curModuleName
  1630. }
  1631. }
  1632. return nil
  1633. }
  1634. func hasProperty(mod *parser.Module, propName string) bool {
  1635. _, ok := mod.GetProperty(propName)
  1636. return ok
  1637. }