androidmk.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. package main
  15. import (
  16. "bytes"
  17. "flag"
  18. "fmt"
  19. "io/ioutil"
  20. "os"
  21. "strings"
  22. "text/scanner"
  23. "android/soong/bpfix/bpfix"
  24. mkparser "android/soong/androidmk/parser"
  25. bpparser "github.com/google/blueprint/parser"
  26. )
  27. var usage = func() {
  28. fmt.Fprintf(os.Stderr, "usage: androidmk [flags] <inputFile>\n"+
  29. "\nandroidmk parses <inputFile> as an Android.mk file and attempts to output an analogous Android.bp file (to standard out)\n")
  30. flag.PrintDefaults()
  31. os.Exit(1)
  32. }
  33. // TODO: non-expanded variables with expressions
  34. type bpFile struct {
  35. comments []*bpparser.CommentGroup
  36. defs []bpparser.Definition
  37. localAssignments map[string]*bpparser.Property
  38. globalAssignments map[string]*bpparser.Expression
  39. scope mkparser.Scope
  40. module *bpparser.Module
  41. mkPos scanner.Position // Position of the last handled line in the makefile
  42. bpPos scanner.Position // Position of the last emitted line to the blueprint file
  43. inModule bool
  44. }
  45. func (f *bpFile) insertComment(s string) {
  46. f.comments = append(f.comments, &bpparser.CommentGroup{
  47. Comments: []*bpparser.Comment{
  48. &bpparser.Comment{
  49. Comment: []string{s},
  50. Slash: f.bpPos,
  51. },
  52. },
  53. })
  54. f.bpPos.Offset += len(s)
  55. }
  56. func (f *bpFile) insertExtraComment(s string) {
  57. f.insertComment(s)
  58. f.bpPos.Line++
  59. }
  60. // records that the given node failed to be converted and includes an explanatory message
  61. func (f *bpFile) errorf(failedNode mkparser.Node, message string, args ...interface{}) {
  62. orig := failedNode.Dump()
  63. message = fmt.Sprintf(message, args...)
  64. f.addErrorText(fmt.Sprintf("// ANDROIDMK TRANSLATION ERROR: %s", message))
  65. lines := strings.Split(orig, "\n")
  66. for _, l := range lines {
  67. f.insertExtraComment("// " + l)
  68. }
  69. }
  70. // records that something unexpected occurred
  71. func (f *bpFile) warnf(message string, args ...interface{}) {
  72. message = fmt.Sprintf(message, args...)
  73. f.addErrorText(fmt.Sprintf("// ANDROIDMK TRANSLATION WARNING: %s", message))
  74. }
  75. // adds the given error message as-is to the bottom of the (in-progress) file
  76. func (f *bpFile) addErrorText(message string) {
  77. f.insertExtraComment(message)
  78. }
  79. func (f *bpFile) setMkPos(pos, end scanner.Position) {
  80. // It is unusual but not forbidden for pos.Line to be smaller than f.mkPos.Line
  81. // For example:
  82. //
  83. // if true # this line is emitted 1st
  84. // if true # this line is emitted 2nd
  85. // some-target: some-file # this line is emitted 3rd
  86. // echo doing something # this recipe is emitted 6th
  87. // endif #some comment # this endif is emitted 4th; this comment is part of the recipe
  88. // echo doing more stuff # this is part of the recipe
  89. // endif # this endif is emitted 5th
  90. //
  91. // However, if pos.Line < f.mkPos.Line, we treat it as though it were equal
  92. if pos.Line >= f.mkPos.Line {
  93. f.bpPos.Line += (pos.Line - f.mkPos.Line)
  94. f.mkPos = end
  95. }
  96. }
  97. type conditional struct {
  98. cond string
  99. eq bool
  100. }
  101. func main() {
  102. flag.Usage = usage
  103. flag.Parse()
  104. if len(flag.Args()) != 1 {
  105. usage()
  106. }
  107. filePathToRead := flag.Arg(0)
  108. b, err := ioutil.ReadFile(filePathToRead)
  109. if err != nil {
  110. fmt.Println(err.Error())
  111. return
  112. }
  113. output, errs := convertFile(os.Args[1], bytes.NewBuffer(b))
  114. if len(errs) > 0 {
  115. for _, err := range errs {
  116. fmt.Fprintln(os.Stderr, "ERROR: ", err)
  117. }
  118. os.Exit(1)
  119. }
  120. fmt.Print(output)
  121. }
  122. func convertFile(filename string, buffer *bytes.Buffer) (string, []error) {
  123. p := mkparser.NewParser(filename, buffer)
  124. nodes, errs := p.Parse()
  125. if len(errs) > 0 {
  126. return "", errs
  127. }
  128. file := &bpFile{
  129. scope: androidScope(),
  130. localAssignments: make(map[string]*bpparser.Property),
  131. globalAssignments: make(map[string]*bpparser.Expression),
  132. }
  133. var conds []*conditional
  134. var assignmentCond *conditional
  135. for _, node := range nodes {
  136. file.setMkPos(p.Unpack(node.Pos()), p.Unpack(node.End()))
  137. switch x := node.(type) {
  138. case *mkparser.Comment:
  139. file.insertComment("//" + x.Comment)
  140. case *mkparser.Assignment:
  141. handleAssignment(file, x, assignmentCond)
  142. case *mkparser.Directive:
  143. switch x.Name {
  144. case "include":
  145. val := x.Args.Value(file.scope)
  146. switch {
  147. case soongModuleTypes[val]:
  148. handleModuleConditionals(file, x, conds)
  149. makeModule(file, val)
  150. case val == clear_vars:
  151. resetModule(file)
  152. default:
  153. file.errorf(x, "unsupported include")
  154. continue
  155. }
  156. case "ifeq", "ifneq", "ifdef", "ifndef":
  157. args := x.Args.Dump()
  158. eq := x.Name == "ifeq" || x.Name == "ifdef"
  159. if _, ok := conditionalTranslations[args]; ok {
  160. newCond := conditional{args, eq}
  161. conds = append(conds, &newCond)
  162. if file.inModule {
  163. if assignmentCond == nil {
  164. assignmentCond = &newCond
  165. } else {
  166. file.errorf(x, "unsupported nested conditional in module")
  167. }
  168. }
  169. } else {
  170. file.errorf(x, "unsupported conditional")
  171. conds = append(conds, nil)
  172. continue
  173. }
  174. case "else":
  175. if len(conds) == 0 {
  176. file.errorf(x, "missing if before else")
  177. continue
  178. } else if conds[len(conds)-1] == nil {
  179. file.errorf(x, "else from unsupported contitional")
  180. continue
  181. }
  182. conds[len(conds)-1].eq = !conds[len(conds)-1].eq
  183. case "endif":
  184. if len(conds) == 0 {
  185. file.errorf(x, "missing if before endif")
  186. continue
  187. } else if conds[len(conds)-1] == nil {
  188. file.errorf(x, "endif from unsupported contitional")
  189. conds = conds[:len(conds)-1]
  190. } else {
  191. if assignmentCond == conds[len(conds)-1] {
  192. assignmentCond = nil
  193. }
  194. conds = conds[:len(conds)-1]
  195. }
  196. default:
  197. file.errorf(x, "unsupported directive")
  198. continue
  199. }
  200. default:
  201. file.errorf(x, "unsupported line")
  202. }
  203. }
  204. tree := &bpparser.File{
  205. Defs: file.defs,
  206. Comments: file.comments,
  207. }
  208. // check for common supported but undesirable structures and clean them up
  209. fixed, err := bpfix.FixTree(tree, bpfix.NewFixRequest().AddAll())
  210. if err != nil {
  211. return "", []error{err}
  212. }
  213. out, err := bpparser.Print(fixed)
  214. if err != nil {
  215. return "", []error{err}
  216. }
  217. return string(out), nil
  218. }
  219. func handleAssignment(file *bpFile, assignment *mkparser.Assignment, c *conditional) {
  220. if !assignment.Name.Const() {
  221. file.errorf(assignment, "unsupported non-const variable name")
  222. return
  223. }
  224. if assignment.Target != nil {
  225. file.errorf(assignment, "unsupported target assignment")
  226. return
  227. }
  228. name := assignment.Name.Value(nil)
  229. prefix := ""
  230. if strings.HasPrefix(name, "LOCAL_") {
  231. for _, x := range propertyPrefixes {
  232. if strings.HasSuffix(name, "_"+x.mk) {
  233. name = strings.TrimSuffix(name, "_"+x.mk)
  234. prefix = x.bp
  235. break
  236. }
  237. }
  238. if c != nil {
  239. if prefix != "" {
  240. file.errorf(assignment, "prefix assignment inside conditional, skipping conditional")
  241. } else {
  242. var ok bool
  243. if prefix, ok = conditionalTranslations[c.cond][c.eq]; !ok {
  244. panic("unknown conditional")
  245. }
  246. }
  247. }
  248. } else {
  249. if c != nil {
  250. eq := "eq"
  251. if !c.eq {
  252. eq = "neq"
  253. }
  254. file.errorf(assignment, "conditional %s %s on global assignment", eq, c.cond)
  255. }
  256. }
  257. appendVariable := assignment.Type == "+="
  258. var err error
  259. if prop, ok := rewriteProperties[name]; ok {
  260. err = prop(variableAssignmentContext{file, prefix, assignment.Value, appendVariable})
  261. } else {
  262. switch {
  263. case name == "LOCAL_ARM_MODE":
  264. // This is a hack to get the LOCAL_ARM_MODE value inside
  265. // of an arch: { arm: {} } block.
  266. armModeAssign := assignment
  267. armModeAssign.Name = mkparser.SimpleMakeString("LOCAL_ARM_MODE_HACK_arm", assignment.Name.Pos())
  268. handleAssignment(file, armModeAssign, c)
  269. case strings.HasPrefix(name, "LOCAL_"):
  270. file.errorf(assignment, "unsupported assignment to %s", name)
  271. return
  272. default:
  273. var val bpparser.Expression
  274. val, err = makeVariableToBlueprint(file, assignment.Value, bpparser.ListType)
  275. if err == nil {
  276. err = setVariable(file, appendVariable, prefix, name, val, false)
  277. }
  278. }
  279. }
  280. if err != nil {
  281. file.errorf(assignment, err.Error())
  282. }
  283. }
  284. func handleModuleConditionals(file *bpFile, directive *mkparser.Directive, conds []*conditional) {
  285. for _, c := range conds {
  286. if c == nil {
  287. continue
  288. }
  289. if _, ok := conditionalTranslations[c.cond]; !ok {
  290. panic("unknown conditional " + c.cond)
  291. }
  292. disabledPrefix := conditionalTranslations[c.cond][!c.eq]
  293. // Create a fake assignment with enabled = false
  294. val, err := makeVariableToBlueprint(file, mkparser.SimpleMakeString("false", mkparser.NoPos), bpparser.BoolType)
  295. if err == nil {
  296. err = setVariable(file, false, disabledPrefix, "enabled", val, true)
  297. }
  298. if err != nil {
  299. file.errorf(directive, err.Error())
  300. }
  301. }
  302. }
  303. func makeModule(file *bpFile, t string) {
  304. file.module.Type = t
  305. file.module.TypePos = file.module.LBracePos
  306. file.module.RBracePos = file.bpPos
  307. file.defs = append(file.defs, file.module)
  308. file.inModule = false
  309. }
  310. func resetModule(file *bpFile) {
  311. file.module = &bpparser.Module{}
  312. file.module.LBracePos = file.bpPos
  313. file.localAssignments = make(map[string]*bpparser.Property)
  314. file.inModule = true
  315. }
  316. func makeVariableToBlueprint(file *bpFile, val *mkparser.MakeString,
  317. typ bpparser.Type) (bpparser.Expression, error) {
  318. var exp bpparser.Expression
  319. var err error
  320. switch typ {
  321. case bpparser.ListType:
  322. exp, err = makeToListExpression(val, file.scope)
  323. case bpparser.StringType:
  324. exp, err = makeToStringExpression(val, file.scope)
  325. case bpparser.BoolType:
  326. exp, err = makeToBoolExpression(val)
  327. default:
  328. panic("unknown type")
  329. }
  330. if err != nil {
  331. return nil, err
  332. }
  333. return exp, nil
  334. }
  335. func setVariable(file *bpFile, plusequals bool, prefix, name string, value bpparser.Expression, local bool) error {
  336. if prefix != "" {
  337. name = prefix + "." + name
  338. }
  339. pos := file.bpPos
  340. var oldValue *bpparser.Expression
  341. if local {
  342. oldProp := file.localAssignments[name]
  343. if oldProp != nil {
  344. oldValue = &oldProp.Value
  345. }
  346. } else {
  347. oldValue = file.globalAssignments[name]
  348. }
  349. if local {
  350. if oldValue != nil && plusequals {
  351. val, err := addValues(*oldValue, value)
  352. if err != nil {
  353. return fmt.Errorf("unsupported addition: %s", err.Error())
  354. }
  355. val.(*bpparser.Operator).OperatorPos = pos
  356. *oldValue = val
  357. } else {
  358. names := strings.Split(name, ".")
  359. if file.module == nil {
  360. file.warnf("No 'include $(CLEAR_VARS)' detected before first assignment; clearing vars now")
  361. resetModule(file)
  362. }
  363. container := &file.module.Properties
  364. for i, n := range names[:len(names)-1] {
  365. fqn := strings.Join(names[0:i+1], ".")
  366. prop := file.localAssignments[fqn]
  367. if prop == nil {
  368. prop = &bpparser.Property{
  369. Name: n,
  370. NamePos: pos,
  371. Value: &bpparser.Map{
  372. Properties: []*bpparser.Property{},
  373. },
  374. }
  375. file.localAssignments[fqn] = prop
  376. *container = append(*container, prop)
  377. }
  378. container = &prop.Value.(*bpparser.Map).Properties
  379. }
  380. prop := &bpparser.Property{
  381. Name: names[len(names)-1],
  382. NamePos: pos,
  383. Value: value,
  384. }
  385. file.localAssignments[name] = prop
  386. *container = append(*container, prop)
  387. }
  388. } else {
  389. if oldValue != nil && plusequals {
  390. a := &bpparser.Assignment{
  391. Name: name,
  392. NamePos: pos,
  393. Value: value,
  394. OrigValue: value,
  395. EqualsPos: pos,
  396. Assigner: "+=",
  397. }
  398. file.defs = append(file.defs, a)
  399. } else {
  400. a := &bpparser.Assignment{
  401. Name: name,
  402. NamePos: pos,
  403. Value: value,
  404. OrigValue: value,
  405. EqualsPos: pos,
  406. Assigner: "=",
  407. }
  408. file.globalAssignments[name] = &a.Value
  409. file.defs = append(file.defs, a)
  410. }
  411. }
  412. return nil
  413. }