paths.go 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214
  1. // Copyright 2015 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 android
  15. import (
  16. "fmt"
  17. "io/ioutil"
  18. "os"
  19. "path/filepath"
  20. "reflect"
  21. "regexp"
  22. "sort"
  23. "strings"
  24. "github.com/google/blueprint"
  25. "github.com/google/blueprint/bootstrap"
  26. "github.com/google/blueprint/pathtools"
  27. )
  28. var absSrcDir string
  29. // PathContext is the subset of a (Module|Singleton)Context required by the
  30. // Path methods.
  31. type PathContext interface {
  32. Config() Config
  33. AddNinjaFileDeps(deps ...string)
  34. }
  35. type PathGlobContext interface {
  36. GlobWithDeps(globPattern string, excludes []string) ([]string, error)
  37. }
  38. var _ PathContext = SingletonContext(nil)
  39. var _ PathContext = ModuleContext(nil)
  40. // "Null" path context is a minimal path context for a given config.
  41. type NullPathContext struct {
  42. config Config
  43. }
  44. func (NullPathContext) AddNinjaFileDeps(...string) {}
  45. func (ctx NullPathContext) Config() Config { return ctx.config }
  46. // EarlyModulePathContext is a subset of EarlyModuleContext methods required by the
  47. // Path methods. These path methods can be called before any mutators have run.
  48. type EarlyModulePathContext interface {
  49. PathContext
  50. PathGlobContext
  51. ModuleDir() string
  52. ModuleErrorf(fmt string, args ...interface{})
  53. }
  54. var _ EarlyModulePathContext = ModuleContext(nil)
  55. // Glob globs files and directories matching globPattern relative to ModuleDir(),
  56. // paths in the excludes parameter will be omitted.
  57. func Glob(ctx EarlyModulePathContext, globPattern string, excludes []string) Paths {
  58. ret, err := ctx.GlobWithDeps(globPattern, excludes)
  59. if err != nil {
  60. ctx.ModuleErrorf("glob: %s", err.Error())
  61. }
  62. return pathsForModuleSrcFromFullPath(ctx, ret, true)
  63. }
  64. // GlobFiles globs *only* files (not directories) matching globPattern relative to ModuleDir().
  65. // Paths in the excludes parameter will be omitted.
  66. func GlobFiles(ctx EarlyModulePathContext, globPattern string, excludes []string) Paths {
  67. ret, err := ctx.GlobWithDeps(globPattern, excludes)
  68. if err != nil {
  69. ctx.ModuleErrorf("glob: %s", err.Error())
  70. }
  71. return pathsForModuleSrcFromFullPath(ctx, ret, false)
  72. }
  73. // ModuleWithDepsPathContext is a subset of *ModuleContext methods required by
  74. // the Path methods that rely on module dependencies having been resolved.
  75. type ModuleWithDepsPathContext interface {
  76. EarlyModulePathContext
  77. VisitDirectDepsBlueprint(visit func(blueprint.Module))
  78. OtherModuleDependencyTag(m blueprint.Module) blueprint.DependencyTag
  79. }
  80. // ModuleMissingDepsPathContext is a subset of *ModuleContext methods required by
  81. // the Path methods that rely on module dependencies having been resolved and ability to report
  82. // missing dependency errors.
  83. type ModuleMissingDepsPathContext interface {
  84. ModuleWithDepsPathContext
  85. AddMissingDependencies(missingDeps []string)
  86. }
  87. type ModuleInstallPathContext interface {
  88. BaseModuleContext
  89. InstallInData() bool
  90. InstallInTestcases() bool
  91. InstallInSanitizerDir() bool
  92. InstallInRamdisk() bool
  93. InstallInVendorRamdisk() bool
  94. InstallInDebugRamdisk() bool
  95. InstallInRecovery() bool
  96. InstallInRoot() bool
  97. InstallForceOS() (*OsType, *ArchType)
  98. }
  99. var _ ModuleInstallPathContext = ModuleContext(nil)
  100. // errorfContext is the interface containing the Errorf method matching the
  101. // Errorf method in blueprint.SingletonContext.
  102. type errorfContext interface {
  103. Errorf(format string, args ...interface{})
  104. }
  105. var _ errorfContext = blueprint.SingletonContext(nil)
  106. // moduleErrorf is the interface containing the ModuleErrorf method matching
  107. // the ModuleErrorf method in blueprint.ModuleContext.
  108. type moduleErrorf interface {
  109. ModuleErrorf(format string, args ...interface{})
  110. }
  111. var _ moduleErrorf = blueprint.ModuleContext(nil)
  112. // reportPathError will register an error with the attached context. It
  113. // attempts ctx.ModuleErrorf for a better error message first, then falls
  114. // back to ctx.Errorf.
  115. func reportPathError(ctx PathContext, err error) {
  116. ReportPathErrorf(ctx, "%s", err.Error())
  117. }
  118. // ReportPathErrorf will register an error with the attached context. It
  119. // attempts ctx.ModuleErrorf for a better error message first, then falls
  120. // back to ctx.Errorf.
  121. func ReportPathErrorf(ctx PathContext, format string, args ...interface{}) {
  122. if mctx, ok := ctx.(moduleErrorf); ok {
  123. mctx.ModuleErrorf(format, args...)
  124. } else if ectx, ok := ctx.(errorfContext); ok {
  125. ectx.Errorf(format, args...)
  126. } else {
  127. panic(fmt.Sprintf(format, args...))
  128. }
  129. }
  130. func pathContextName(ctx PathContext, module blueprint.Module) string {
  131. if x, ok := ctx.(interface{ ModuleName(blueprint.Module) string }); ok {
  132. return x.ModuleName(module)
  133. } else if x, ok := ctx.(interface{ OtherModuleName(blueprint.Module) string }); ok {
  134. return x.OtherModuleName(module)
  135. }
  136. return "unknown"
  137. }
  138. type Path interface {
  139. // Returns the path in string form
  140. String() string
  141. // Ext returns the extension of the last element of the path
  142. Ext() string
  143. // Base returns the last element of the path
  144. Base() string
  145. // Rel returns the portion of the path relative to the directory it was created from. For
  146. // example, Rel on a PathsForModuleSrc would return the path relative to the module source
  147. // directory, and OutputPath.Join("foo").Rel() would return "foo".
  148. Rel() string
  149. // RelativeToTop returns a new path relative to the top, it is provided solely for use in tests.
  150. //
  151. // It is guaranteed to always return the same type as it is called on, e.g. if called on an
  152. // InstallPath then the returned value can be converted to an InstallPath.
  153. //
  154. // A standard build has the following structure:
  155. // ../top/
  156. // out/ - make install files go here.
  157. // out/soong - this is the soongOutDir passed to NewTestConfig()
  158. // ... - the source files
  159. //
  160. // This function converts a path so that it appears relative to the ../top/ directory, i.e.
  161. // * Make install paths, which have the pattern "soongOutDir/../<path>" are converted into the top
  162. // relative path "out/<path>"
  163. // * Soong install paths and other writable paths, which have the pattern "soongOutDir/<path>" are
  164. // converted into the top relative path "out/soong/<path>".
  165. // * Source paths are already relative to the top.
  166. // * Phony paths are not relative to anything.
  167. // * toolDepPath have an absolute but known value in so don't need making relative to anything in
  168. // order to test.
  169. RelativeToTop() Path
  170. }
  171. const (
  172. OutDir = "out"
  173. OutSoongDir = OutDir + "/soong"
  174. )
  175. // WritablePath is a type of path that can be used as an output for build rules.
  176. type WritablePath interface {
  177. Path
  178. // return the path to the build directory.
  179. getSoongOutDir() string
  180. // the writablePath method doesn't directly do anything,
  181. // but it allows a struct to distinguish between whether or not it implements the WritablePath interface
  182. writablePath()
  183. ReplaceExtension(ctx PathContext, ext string) OutputPath
  184. }
  185. type genPathProvider interface {
  186. genPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleGenPath
  187. }
  188. type objPathProvider interface {
  189. objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath
  190. }
  191. type resPathProvider interface {
  192. resPathWithName(ctx ModuleOutPathContext, name string) ModuleResPath
  193. }
  194. // GenPathWithExt derives a new file path in ctx's generated sources directory
  195. // from the current path, but with the new extension.
  196. func GenPathWithExt(ctx ModuleOutPathContext, subdir string, p Path, ext string) ModuleGenPath {
  197. if path, ok := p.(genPathProvider); ok {
  198. return path.genPathWithExt(ctx, subdir, ext)
  199. }
  200. ReportPathErrorf(ctx, "Tried to create generated file from unsupported path: %s(%s)", reflect.TypeOf(p).Name(), p)
  201. return PathForModuleGen(ctx)
  202. }
  203. // ObjPathWithExt derives a new file path in ctx's object directory from the
  204. // current path, but with the new extension.
  205. func ObjPathWithExt(ctx ModuleOutPathContext, subdir string, p Path, ext string) ModuleObjPath {
  206. if path, ok := p.(objPathProvider); ok {
  207. return path.objPathWithExt(ctx, subdir, ext)
  208. }
  209. ReportPathErrorf(ctx, "Tried to create object file from unsupported path: %s (%s)", reflect.TypeOf(p).Name(), p)
  210. return PathForModuleObj(ctx)
  211. }
  212. // ResPathWithName derives a new path in ctx's output resource directory, using
  213. // the current path to create the directory name, and the `name` argument for
  214. // the filename.
  215. func ResPathWithName(ctx ModuleOutPathContext, p Path, name string) ModuleResPath {
  216. if path, ok := p.(resPathProvider); ok {
  217. return path.resPathWithName(ctx, name)
  218. }
  219. ReportPathErrorf(ctx, "Tried to create res file from unsupported path: %s (%s)", reflect.TypeOf(p).Name(), p)
  220. return PathForModuleRes(ctx)
  221. }
  222. // OptionalPath is a container that may or may not contain a valid Path.
  223. type OptionalPath struct {
  224. path Path // nil if invalid.
  225. invalidReason string // Not applicable if path != nil. "" if the reason is unknown.
  226. }
  227. // OptionalPathForPath returns an OptionalPath containing the path.
  228. func OptionalPathForPath(path Path) OptionalPath {
  229. return OptionalPath{path: path}
  230. }
  231. // InvalidOptionalPath returns an OptionalPath that is invalid with the given reason.
  232. func InvalidOptionalPath(reason string) OptionalPath {
  233. return OptionalPath{invalidReason: reason}
  234. }
  235. // Valid returns whether there is a valid path
  236. func (p OptionalPath) Valid() bool {
  237. return p.path != nil
  238. }
  239. // Path returns the Path embedded in this OptionalPath. You must be sure that
  240. // there is a valid path, since this method will panic if there is not.
  241. func (p OptionalPath) Path() Path {
  242. if p.path == nil {
  243. msg := "Requesting an invalid path"
  244. if p.invalidReason != "" {
  245. msg += ": " + p.invalidReason
  246. }
  247. panic(msg)
  248. }
  249. return p.path
  250. }
  251. // InvalidReason returns the reason that the optional path is invalid, or "" if it is valid.
  252. func (p OptionalPath) InvalidReason() string {
  253. if p.path != nil {
  254. return ""
  255. }
  256. if p.invalidReason == "" {
  257. return "unknown"
  258. }
  259. return p.invalidReason
  260. }
  261. // AsPaths converts the OptionalPath into Paths.
  262. //
  263. // It returns nil if this is not valid, or a single length slice containing the Path embedded in
  264. // this OptionalPath.
  265. func (p OptionalPath) AsPaths() Paths {
  266. if p.path == nil {
  267. return nil
  268. }
  269. return Paths{p.path}
  270. }
  271. // RelativeToTop returns an OptionalPath with the path that was embedded having been replaced by the
  272. // result of calling Path.RelativeToTop on it.
  273. func (p OptionalPath) RelativeToTop() OptionalPath {
  274. if p.path == nil {
  275. return p
  276. }
  277. p.path = p.path.RelativeToTop()
  278. return p
  279. }
  280. // String returns the string version of the Path, or "" if it isn't valid.
  281. func (p OptionalPath) String() string {
  282. if p.path != nil {
  283. return p.path.String()
  284. } else {
  285. return ""
  286. }
  287. }
  288. // Paths is a slice of Path objects, with helpers to operate on the collection.
  289. type Paths []Path
  290. // RelativeToTop creates a new Paths containing the result of calling Path.RelativeToTop on each
  291. // item in this slice.
  292. func (p Paths) RelativeToTop() Paths {
  293. ensureTestOnly()
  294. if p == nil {
  295. return p
  296. }
  297. ret := make(Paths, len(p))
  298. for i, path := range p {
  299. ret[i] = path.RelativeToTop()
  300. }
  301. return ret
  302. }
  303. func (paths Paths) containsPath(path Path) bool {
  304. for _, p := range paths {
  305. if p == path {
  306. return true
  307. }
  308. }
  309. return false
  310. }
  311. // PathsForSource returns Paths rooted from SrcDir, *not* rooted from the module's local source
  312. // directory
  313. func PathsForSource(ctx PathContext, paths []string) Paths {
  314. ret := make(Paths, len(paths))
  315. for i, path := range paths {
  316. ret[i] = PathForSource(ctx, path)
  317. }
  318. return ret
  319. }
  320. // ExistentPathsForSources returns a list of Paths rooted from SrcDir, *not* rooted from the
  321. // module's local source directory, that are found in the tree. If any are not found, they are
  322. // omitted from the list, and dependencies are added so that we're re-run when they are added.
  323. func ExistentPathsForSources(ctx PathContext, paths []string) Paths {
  324. ret := make(Paths, 0, len(paths))
  325. for _, path := range paths {
  326. p := ExistentPathForSource(ctx, path)
  327. if p.Valid() {
  328. ret = append(ret, p.Path())
  329. }
  330. }
  331. return ret
  332. }
  333. // PathsForModuleSrc returns a Paths{} containing the resolved references in paths:
  334. // * filepath, relative to local module directory, resolves as a filepath relative to the local
  335. // source directory
  336. // * glob, relative to the local module directory, resolves as filepath(s), relative to the local
  337. // source directory.
  338. // * other modules using the ":name{.tag}" syntax. These modules must implement SourceFileProducer
  339. // or OutputFileProducer. These resolve as a filepath to an output filepath or generated source
  340. // filepath.
  341. // Properties passed as the paths argument must have been annotated with struct tag
  342. // `android:"path"` so that dependencies on SourceFileProducer modules will have already been handled by the
  343. // path_deps mutator.
  344. // If a requested module is not found as a dependency:
  345. // * if ctx.Config().AllowMissingDependencies() is true, this module to be marked as having
  346. // missing dependencies
  347. // * otherwise, a ModuleError is thrown.
  348. func PathsForModuleSrc(ctx ModuleMissingDepsPathContext, paths []string) Paths {
  349. return PathsForModuleSrcExcludes(ctx, paths, nil)
  350. }
  351. type SourceInput struct {
  352. Context ModuleMissingDepsPathContext
  353. Paths []string
  354. ExcludePaths []string
  355. IncludeDirs bool
  356. }
  357. // PathsForModuleSrcExcludes returns a Paths{} containing the resolved references in paths, minus
  358. // those listed in excludes. Elements of paths and excludes are resolved as:
  359. // * filepath, relative to local module directory, resolves as a filepath relative to the local
  360. // source directory
  361. // * glob, relative to the local module directory, resolves as filepath(s), relative to the local
  362. // source directory. Not valid in excludes.
  363. // * other modules using the ":name{.tag}" syntax. These modules must implement SourceFileProducer
  364. // or OutputFileProducer. These resolve as a filepath to an output filepath or generated source
  365. // filepath.
  366. // excluding the items (similarly resolved
  367. // Properties passed as the paths argument must have been annotated with struct tag
  368. // `android:"path"` so that dependencies on SourceFileProducer modules will have already been handled by the
  369. // path_deps mutator.
  370. // If a requested module is not found as a dependency:
  371. // * if ctx.Config().AllowMissingDependencies() is true, this module to be marked as having
  372. // missing dependencies
  373. // * otherwise, a ModuleError is thrown.
  374. func PathsForModuleSrcExcludes(ctx ModuleMissingDepsPathContext, paths, excludes []string) Paths {
  375. return PathsRelativeToModuleSourceDir(SourceInput{
  376. Context: ctx,
  377. Paths: paths,
  378. ExcludePaths: excludes,
  379. IncludeDirs: true,
  380. })
  381. }
  382. func PathsRelativeToModuleSourceDir(input SourceInput) Paths {
  383. ret, missingDeps := PathsAndMissingDepsRelativeToModuleSourceDir(input)
  384. if input.Context.Config().AllowMissingDependencies() {
  385. input.Context.AddMissingDependencies(missingDeps)
  386. } else {
  387. for _, m := range missingDeps {
  388. input.Context.ModuleErrorf(`missing dependency on %q, is the property annotated with android:"path"?`, m)
  389. }
  390. }
  391. return ret
  392. }
  393. // OutputPaths is a slice of OutputPath objects, with helpers to operate on the collection.
  394. type OutputPaths []OutputPath
  395. // Paths returns the OutputPaths as a Paths
  396. func (p OutputPaths) Paths() Paths {
  397. if p == nil {
  398. return nil
  399. }
  400. ret := make(Paths, len(p))
  401. for i, path := range p {
  402. ret[i] = path
  403. }
  404. return ret
  405. }
  406. // Strings returns the string forms of the writable paths.
  407. func (p OutputPaths) Strings() []string {
  408. if p == nil {
  409. return nil
  410. }
  411. ret := make([]string, len(p))
  412. for i, path := range p {
  413. ret[i] = path.String()
  414. }
  415. return ret
  416. }
  417. // PathForGoBinary returns the path to the installed location of a bootstrap_go_binary module.
  418. func PathForGoBinary(ctx PathContext, goBinary bootstrap.GoBinaryTool) Path {
  419. goBinaryInstallDir := pathForInstall(ctx, ctx.Config().BuildOS, ctx.Config().BuildArch, "bin", false)
  420. rel := Rel(ctx, goBinaryInstallDir.String(), goBinary.InstallPath())
  421. return goBinaryInstallDir.Join(ctx, rel)
  422. }
  423. // Expands Paths to a SourceFileProducer or OutputFileProducer module dependency referenced via ":name" or ":name{.tag}" syntax.
  424. // If the dependency is not found, a missingErrorDependency is returned.
  425. // If the module dependency is not a SourceFileProducer or OutputFileProducer, appropriate errors will be returned.
  426. func getPathsFromModuleDep(ctx ModuleWithDepsPathContext, path, moduleName, tag string) (Paths, error) {
  427. module := GetModuleFromPathDep(ctx, moduleName, tag)
  428. if module == nil {
  429. return nil, missingDependencyError{[]string{moduleName}}
  430. }
  431. if aModule, ok := module.(Module); ok && !aModule.Enabled() {
  432. return nil, missingDependencyError{[]string{moduleName}}
  433. }
  434. if outProducer, ok := module.(OutputFileProducer); ok {
  435. outputFiles, err := outProducer.OutputFiles(tag)
  436. if err != nil {
  437. return nil, fmt.Errorf("path dependency %q: %s", path, err)
  438. }
  439. return outputFiles, nil
  440. } else if tag != "" {
  441. return nil, fmt.Errorf("path dependency %q is not an output file producing module", path)
  442. } else if goBinary, ok := module.(bootstrap.GoBinaryTool); ok {
  443. goBinaryPath := PathForGoBinary(ctx, goBinary)
  444. return Paths{goBinaryPath}, nil
  445. } else if srcProducer, ok := module.(SourceFileProducer); ok {
  446. return srcProducer.Srcs(), nil
  447. } else {
  448. return nil, fmt.Errorf("path dependency %q is not a source file producing module", path)
  449. }
  450. }
  451. // GetModuleFromPathDep will return the module that was added as a dependency automatically for
  452. // properties tagged with `android:"path"` or manually using ExtractSourceDeps or
  453. // ExtractSourcesDeps.
  454. //
  455. // The moduleName and tag supplied to this should be the values returned from SrcIsModuleWithTag.
  456. // Or, if no tag is expected then the moduleName should be the value returned by SrcIsModule and
  457. // the tag must be "".
  458. //
  459. // If tag is "" then the returned module will be the dependency that was added for ":moduleName".
  460. // Otherwise, it is the dependency that was added for ":moduleName{tag}".
  461. func GetModuleFromPathDep(ctx ModuleWithDepsPathContext, moduleName, tag string) blueprint.Module {
  462. var found blueprint.Module
  463. // The sourceOrOutputDepTag uniquely identifies the module dependency as it contains both the
  464. // module name and the tag. Dependencies added automatically for properties tagged with
  465. // `android:"path"` are deduped so are guaranteed to be unique. It is possible for duplicate
  466. // dependencies to be added manually using ExtractSourcesDeps or ExtractSourceDeps but even then
  467. // it will always be the case that the dependencies will be identical, i.e. the same tag and same
  468. // moduleName referring to the same dependency module.
  469. //
  470. // It does not matter whether the moduleName is a fully qualified name or if the module
  471. // dependency is a prebuilt module. All that matters is the same information is supplied to
  472. // create the tag here as was supplied to create the tag when the dependency was added so that
  473. // this finds the matching dependency module.
  474. expectedTag := sourceOrOutputDepTag(moduleName, tag)
  475. ctx.VisitDirectDepsBlueprint(func(module blueprint.Module) {
  476. depTag := ctx.OtherModuleDependencyTag(module)
  477. if depTag == expectedTag {
  478. found = module
  479. }
  480. })
  481. return found
  482. }
  483. // PathsAndMissingDepsForModuleSrcExcludes returns a Paths{} containing the resolved references in
  484. // paths, minus those listed in excludes. Elements of paths and excludes are resolved as:
  485. // * filepath, relative to local module directory, resolves as a filepath relative to the local
  486. // source directory
  487. // * glob, relative to the local module directory, resolves as filepath(s), relative to the local
  488. // source directory. Not valid in excludes.
  489. // * other modules using the ":name{.tag}" syntax. These modules must implement SourceFileProducer
  490. // or OutputFileProducer. These resolve as a filepath to an output filepath or generated source
  491. // filepath.
  492. // and a list of the module names of missing module dependencies are returned as the second return.
  493. // Properties passed as the paths argument must have been annotated with struct tag
  494. // `android:"path"` so that dependencies on SourceFileProducer modules will have already been handled by the
  495. // path_deps mutator.
  496. func PathsAndMissingDepsForModuleSrcExcludes(ctx ModuleMissingDepsPathContext, paths, excludes []string) (Paths, []string) {
  497. return PathsAndMissingDepsRelativeToModuleSourceDir(SourceInput{
  498. Context: ctx,
  499. Paths: paths,
  500. ExcludePaths: excludes,
  501. IncludeDirs: true,
  502. })
  503. }
  504. func PathsAndMissingDepsRelativeToModuleSourceDir(input SourceInput) (Paths, []string) {
  505. prefix := pathForModuleSrc(input.Context).String()
  506. var expandedExcludes []string
  507. if input.ExcludePaths != nil {
  508. expandedExcludes = make([]string, 0, len(input.ExcludePaths))
  509. }
  510. var missingExcludeDeps []string
  511. for _, e := range input.ExcludePaths {
  512. if m, t := SrcIsModuleWithTag(e); m != "" {
  513. modulePaths, err := getPathsFromModuleDep(input.Context, e, m, t)
  514. if m, ok := err.(missingDependencyError); ok {
  515. missingExcludeDeps = append(missingExcludeDeps, m.missingDeps...)
  516. } else if err != nil {
  517. reportPathError(input.Context, err)
  518. } else {
  519. expandedExcludes = append(expandedExcludes, modulePaths.Strings()...)
  520. }
  521. } else {
  522. expandedExcludes = append(expandedExcludes, filepath.Join(prefix, e))
  523. }
  524. }
  525. if input.Paths == nil {
  526. return nil, missingExcludeDeps
  527. }
  528. var missingDeps []string
  529. expandedSrcFiles := make(Paths, 0, len(input.Paths))
  530. for _, s := range input.Paths {
  531. srcFiles, err := expandOneSrcPath(sourcePathInput{
  532. context: input.Context,
  533. path: s,
  534. expandedExcludes: expandedExcludes,
  535. includeDirs: input.IncludeDirs,
  536. })
  537. if depErr, ok := err.(missingDependencyError); ok {
  538. missingDeps = append(missingDeps, depErr.missingDeps...)
  539. } else if err != nil {
  540. reportPathError(input.Context, err)
  541. }
  542. expandedSrcFiles = append(expandedSrcFiles, srcFiles...)
  543. }
  544. return expandedSrcFiles, append(missingDeps, missingExcludeDeps...)
  545. }
  546. type missingDependencyError struct {
  547. missingDeps []string
  548. }
  549. func (e missingDependencyError) Error() string {
  550. return "missing dependencies: " + strings.Join(e.missingDeps, ", ")
  551. }
  552. type sourcePathInput struct {
  553. context ModuleWithDepsPathContext
  554. path string
  555. expandedExcludes []string
  556. includeDirs bool
  557. }
  558. // Expands one path string to Paths rooted from the module's local source
  559. // directory, excluding those listed in the expandedExcludes.
  560. // Expands globs, references to SourceFileProducer or OutputFileProducer modules using the ":name" and ":name{.tag}" syntax.
  561. func expandOneSrcPath(input sourcePathInput) (Paths, error) {
  562. excludePaths := func(paths Paths) Paths {
  563. if len(input.expandedExcludes) == 0 {
  564. return paths
  565. }
  566. remainder := make(Paths, 0, len(paths))
  567. for _, p := range paths {
  568. if !InList(p.String(), input.expandedExcludes) {
  569. remainder = append(remainder, p)
  570. }
  571. }
  572. return remainder
  573. }
  574. if m, t := SrcIsModuleWithTag(input.path); m != "" {
  575. modulePaths, err := getPathsFromModuleDep(input.context, input.path, m, t)
  576. if err != nil {
  577. return nil, err
  578. } else {
  579. return excludePaths(modulePaths), nil
  580. }
  581. } else {
  582. p := pathForModuleSrc(input.context, input.path)
  583. if pathtools.IsGlob(input.path) {
  584. paths := GlobFiles(input.context, p.String(), input.expandedExcludes)
  585. return PathsWithModuleSrcSubDir(input.context, paths, ""), nil
  586. } else {
  587. if exists, _, err := input.context.Config().fs.Exists(p.String()); err != nil {
  588. ReportPathErrorf(input.context, "%s: %s", p, err.Error())
  589. } else if !exists && !input.context.Config().TestAllowNonExistentPaths {
  590. ReportPathErrorf(input.context, "module source path %q does not exist", p)
  591. } else if !input.includeDirs {
  592. if isDir, err := input.context.Config().fs.IsDir(p.String()); exists && err != nil {
  593. ReportPathErrorf(input.context, "%s: %s", p, err.Error())
  594. } else if isDir {
  595. ReportPathErrorf(input.context, "module source path %q is a directory", p)
  596. }
  597. }
  598. if InList(p.String(), input.expandedExcludes) {
  599. return nil, nil
  600. }
  601. return Paths{p}, nil
  602. }
  603. }
  604. }
  605. // pathsForModuleSrcFromFullPath returns Paths rooted from the module's local
  606. // source directory, but strip the local source directory from the beginning of
  607. // each string. If incDirs is false, strip paths with a trailing '/' from the list.
  608. // It intended for use in globs that only list files that exist, so it allows '$' in
  609. // filenames.
  610. func pathsForModuleSrcFromFullPath(ctx EarlyModulePathContext, paths []string, incDirs bool) Paths {
  611. prefix := ctx.ModuleDir() + "/"
  612. if prefix == "./" {
  613. prefix = ""
  614. }
  615. ret := make(Paths, 0, len(paths))
  616. for _, p := range paths {
  617. if !incDirs && strings.HasSuffix(p, "/") {
  618. continue
  619. }
  620. path := filepath.Clean(p)
  621. if !strings.HasPrefix(path, prefix) {
  622. ReportPathErrorf(ctx, "Path %q is not in module source directory %q", p, prefix)
  623. continue
  624. }
  625. srcPath, err := safePathForSource(ctx, ctx.ModuleDir(), path[len(prefix):])
  626. if err != nil {
  627. reportPathError(ctx, err)
  628. continue
  629. }
  630. srcPath.basePath.rel = srcPath.path
  631. ret = append(ret, srcPath)
  632. }
  633. return ret
  634. }
  635. // PathsWithOptionalDefaultForModuleSrc returns Paths rooted from the module's local source
  636. // directory. If input is nil, use the default if it exists. If input is empty, returns nil.
  637. func PathsWithOptionalDefaultForModuleSrc(ctx ModuleMissingDepsPathContext, input []string, def string) Paths {
  638. if input != nil {
  639. return PathsForModuleSrc(ctx, input)
  640. }
  641. // Use Glob so that if the default doesn't exist, a dependency is added so that when it
  642. // is created, we're run again.
  643. path := filepath.Join(ctx.ModuleDir(), def)
  644. return Glob(ctx, path, nil)
  645. }
  646. // Strings returns the Paths in string form
  647. func (p Paths) Strings() []string {
  648. if p == nil {
  649. return nil
  650. }
  651. ret := make([]string, len(p))
  652. for i, path := range p {
  653. ret[i] = path.String()
  654. }
  655. return ret
  656. }
  657. func CopyOfPaths(paths Paths) Paths {
  658. return append(Paths(nil), paths...)
  659. }
  660. // FirstUniquePaths returns all unique elements of a Paths, keeping the first copy of each. It
  661. // modifies the Paths slice contents in place, and returns a subslice of the original slice.
  662. func FirstUniquePaths(list Paths) Paths {
  663. // 128 was chosen based on BenchmarkFirstUniquePaths results.
  664. if len(list) > 128 {
  665. return firstUniquePathsMap(list)
  666. }
  667. return firstUniquePathsList(list)
  668. }
  669. // SortedUniquePaths returns all unique elements of a Paths in sorted order. It modifies the
  670. // Paths slice contents in place, and returns a subslice of the original slice.
  671. func SortedUniquePaths(list Paths) Paths {
  672. unique := FirstUniquePaths(list)
  673. sort.Slice(unique, func(i, j int) bool {
  674. return unique[i].String() < unique[j].String()
  675. })
  676. return unique
  677. }
  678. func firstUniquePathsList(list Paths) Paths {
  679. k := 0
  680. outer:
  681. for i := 0; i < len(list); i++ {
  682. for j := 0; j < k; j++ {
  683. if list[i] == list[j] {
  684. continue outer
  685. }
  686. }
  687. list[k] = list[i]
  688. k++
  689. }
  690. return list[:k]
  691. }
  692. func firstUniquePathsMap(list Paths) Paths {
  693. k := 0
  694. seen := make(map[Path]bool, len(list))
  695. for i := 0; i < len(list); i++ {
  696. if seen[list[i]] {
  697. continue
  698. }
  699. seen[list[i]] = true
  700. list[k] = list[i]
  701. k++
  702. }
  703. return list[:k]
  704. }
  705. // FirstUniqueInstallPaths returns all unique elements of an InstallPaths, keeping the first copy of each. It
  706. // modifies the InstallPaths slice contents in place, and returns a subslice of the original slice.
  707. func FirstUniqueInstallPaths(list InstallPaths) InstallPaths {
  708. // 128 was chosen based on BenchmarkFirstUniquePaths results.
  709. if len(list) > 128 {
  710. return firstUniqueInstallPathsMap(list)
  711. }
  712. return firstUniqueInstallPathsList(list)
  713. }
  714. func firstUniqueInstallPathsList(list InstallPaths) InstallPaths {
  715. k := 0
  716. outer:
  717. for i := 0; i < len(list); i++ {
  718. for j := 0; j < k; j++ {
  719. if list[i] == list[j] {
  720. continue outer
  721. }
  722. }
  723. list[k] = list[i]
  724. k++
  725. }
  726. return list[:k]
  727. }
  728. func firstUniqueInstallPathsMap(list InstallPaths) InstallPaths {
  729. k := 0
  730. seen := make(map[InstallPath]bool, len(list))
  731. for i := 0; i < len(list); i++ {
  732. if seen[list[i]] {
  733. continue
  734. }
  735. seen[list[i]] = true
  736. list[k] = list[i]
  737. k++
  738. }
  739. return list[:k]
  740. }
  741. // LastUniquePaths returns all unique elements of a Paths, keeping the last copy of each. It
  742. // modifies the Paths slice contents in place, and returns a subslice of the original slice.
  743. func LastUniquePaths(list Paths) Paths {
  744. totalSkip := 0
  745. for i := len(list) - 1; i >= totalSkip; i-- {
  746. skip := 0
  747. for j := i - 1; j >= totalSkip; j-- {
  748. if list[i] == list[j] {
  749. skip++
  750. } else {
  751. list[j+skip] = list[j]
  752. }
  753. }
  754. totalSkip += skip
  755. }
  756. return list[totalSkip:]
  757. }
  758. // ReversePaths returns a copy of a Paths in reverse order.
  759. func ReversePaths(list Paths) Paths {
  760. if list == nil {
  761. return nil
  762. }
  763. ret := make(Paths, len(list))
  764. for i := range list {
  765. ret[i] = list[len(list)-1-i]
  766. }
  767. return ret
  768. }
  769. func indexPathList(s Path, list []Path) int {
  770. for i, l := range list {
  771. if l == s {
  772. return i
  773. }
  774. }
  775. return -1
  776. }
  777. func inPathList(p Path, list []Path) bool {
  778. return indexPathList(p, list) != -1
  779. }
  780. func FilterPathList(list []Path, filter []Path) (remainder []Path, filtered []Path) {
  781. return FilterPathListPredicate(list, func(p Path) bool { return inPathList(p, filter) })
  782. }
  783. func FilterPathListPredicate(list []Path, predicate func(Path) bool) (remainder []Path, filtered []Path) {
  784. for _, l := range list {
  785. if predicate(l) {
  786. filtered = append(filtered, l)
  787. } else {
  788. remainder = append(remainder, l)
  789. }
  790. }
  791. return
  792. }
  793. // HasExt returns true of any of the paths have extension ext, otherwise false
  794. func (p Paths) HasExt(ext string) bool {
  795. for _, path := range p {
  796. if path.Ext() == ext {
  797. return true
  798. }
  799. }
  800. return false
  801. }
  802. // FilterByExt returns the subset of the paths that have extension ext
  803. func (p Paths) FilterByExt(ext string) Paths {
  804. ret := make(Paths, 0, len(p))
  805. for _, path := range p {
  806. if path.Ext() == ext {
  807. ret = append(ret, path)
  808. }
  809. }
  810. return ret
  811. }
  812. // FilterOutByExt returns the subset of the paths that do not have extension ext
  813. func (p Paths) FilterOutByExt(ext string) Paths {
  814. ret := make(Paths, 0, len(p))
  815. for _, path := range p {
  816. if path.Ext() != ext {
  817. ret = append(ret, path)
  818. }
  819. }
  820. return ret
  821. }
  822. // DirectorySortedPaths is a slice of paths that are sorted such that all files in a directory
  823. // (including subdirectories) are in a contiguous subslice of the list, and can be found in
  824. // O(log(N)) time using a binary search on the directory prefix.
  825. type DirectorySortedPaths Paths
  826. func PathsToDirectorySortedPaths(paths Paths) DirectorySortedPaths {
  827. ret := append(DirectorySortedPaths(nil), paths...)
  828. sort.Slice(ret, func(i, j int) bool {
  829. return ret[i].String() < ret[j].String()
  830. })
  831. return ret
  832. }
  833. // PathsInDirectory returns a subslice of the DirectorySortedPaths as a Paths that contains all entries
  834. // that are in the specified directory and its subdirectories.
  835. func (p DirectorySortedPaths) PathsInDirectory(dir string) Paths {
  836. prefix := filepath.Clean(dir) + "/"
  837. start := sort.Search(len(p), func(i int) bool {
  838. return prefix < p[i].String()
  839. })
  840. ret := p[start:]
  841. end := sort.Search(len(ret), func(i int) bool {
  842. return !strings.HasPrefix(ret[i].String(), prefix)
  843. })
  844. ret = ret[:end]
  845. return Paths(ret)
  846. }
  847. // WritablePaths is a slice of WritablePath, used for multiple outputs.
  848. type WritablePaths []WritablePath
  849. // RelativeToTop creates a new WritablePaths containing the result of calling Path.RelativeToTop on
  850. // each item in this slice.
  851. func (p WritablePaths) RelativeToTop() WritablePaths {
  852. ensureTestOnly()
  853. if p == nil {
  854. return p
  855. }
  856. ret := make(WritablePaths, len(p))
  857. for i, path := range p {
  858. ret[i] = path.RelativeToTop().(WritablePath)
  859. }
  860. return ret
  861. }
  862. // Strings returns the string forms of the writable paths.
  863. func (p WritablePaths) Strings() []string {
  864. if p == nil {
  865. return nil
  866. }
  867. ret := make([]string, len(p))
  868. for i, path := range p {
  869. ret[i] = path.String()
  870. }
  871. return ret
  872. }
  873. // Paths returns the WritablePaths as a Paths
  874. func (p WritablePaths) Paths() Paths {
  875. if p == nil {
  876. return nil
  877. }
  878. ret := make(Paths, len(p))
  879. for i, path := range p {
  880. ret[i] = path
  881. }
  882. return ret
  883. }
  884. type basePath struct {
  885. path string
  886. rel string
  887. }
  888. func (p basePath) Ext() string {
  889. return filepath.Ext(p.path)
  890. }
  891. func (p basePath) Base() string {
  892. return filepath.Base(p.path)
  893. }
  894. func (p basePath) Rel() string {
  895. if p.rel != "" {
  896. return p.rel
  897. }
  898. return p.path
  899. }
  900. func (p basePath) String() string {
  901. return p.path
  902. }
  903. func (p basePath) withRel(rel string) basePath {
  904. p.path = filepath.Join(p.path, rel)
  905. p.rel = rel
  906. return p
  907. }
  908. // SourcePath is a Path representing a file path rooted from SrcDir
  909. type SourcePath struct {
  910. basePath
  911. // The sources root, i.e. Config.SrcDir()
  912. srcDir string
  913. }
  914. func (p SourcePath) RelativeToTop() Path {
  915. ensureTestOnly()
  916. return p
  917. }
  918. var _ Path = SourcePath{}
  919. func (p SourcePath) withRel(rel string) SourcePath {
  920. p.basePath = p.basePath.withRel(rel)
  921. return p
  922. }
  923. // safePathForSource is for paths that we expect are safe -- only for use by go
  924. // code that is embedding ninja variables in paths
  925. func safePathForSource(ctx PathContext, pathComponents ...string) (SourcePath, error) {
  926. p, err := validateSafePath(pathComponents...)
  927. ret := SourcePath{basePath{p, ""}, "."}
  928. if err != nil {
  929. return ret, err
  930. }
  931. // absolute path already checked by validateSafePath
  932. // special-case api surface gen files for now
  933. if strings.HasPrefix(ret.String(), ctx.Config().soongOutDir) && !strings.Contains(ret.String(), ctx.Config().soongOutDir+"/.export") {
  934. return ret, fmt.Errorf("source path %q is in output", ret.String())
  935. }
  936. return ret, err
  937. }
  938. // pathForSource creates a SourcePath from pathComponents, but does not check that it exists.
  939. func pathForSource(ctx PathContext, pathComponents ...string) (SourcePath, error) {
  940. p, err := validatePath(pathComponents...)
  941. ret := SourcePath{basePath{p, ""}, "."}
  942. if err != nil {
  943. return ret, err
  944. }
  945. // absolute path already checked by validatePath
  946. // special-case for now
  947. if strings.HasPrefix(ret.String(), ctx.Config().soongOutDir) && !strings.Contains(ret.String(), ctx.Config().soongOutDir+"/.export") {
  948. return ret, fmt.Errorf("source path %q is in output", ret.String())
  949. }
  950. return ret, nil
  951. }
  952. // existsWithDependencies returns true if the path exists, and adds appropriate dependencies to rerun if the
  953. // path does not exist.
  954. func existsWithDependencies(ctx PathContext, path SourcePath) (exists bool, err error) {
  955. var files []string
  956. if gctx, ok := ctx.(PathGlobContext); ok {
  957. // Use glob to produce proper dependencies, even though we only want
  958. // a single file.
  959. files, err = gctx.GlobWithDeps(path.String(), nil)
  960. } else {
  961. var result pathtools.GlobResult
  962. // We cannot add build statements in this context, so we fall back to
  963. // AddNinjaFileDeps
  964. result, err = ctx.Config().fs.Glob(path.String(), nil, pathtools.FollowSymlinks)
  965. ctx.AddNinjaFileDeps(result.Deps...)
  966. files = result.Matches
  967. }
  968. if err != nil {
  969. return false, fmt.Errorf("glob: %s", err.Error())
  970. }
  971. return len(files) > 0, nil
  972. }
  973. // PathForSource joins the provided path components and validates that the result
  974. // neither escapes the source dir nor is in the out dir.
  975. // On error, it will return a usable, but invalid SourcePath, and report a ModuleError.
  976. func PathForSource(ctx PathContext, pathComponents ...string) SourcePath {
  977. path, err := pathForSource(ctx, pathComponents...)
  978. if err != nil {
  979. reportPathError(ctx, err)
  980. }
  981. if pathtools.IsGlob(path.String()) {
  982. ReportPathErrorf(ctx, "path may not contain a glob: %s", path.String())
  983. }
  984. if modCtx, ok := ctx.(ModuleMissingDepsPathContext); ok && ctx.Config().AllowMissingDependencies() {
  985. exists, err := existsWithDependencies(ctx, path)
  986. if err != nil {
  987. reportPathError(ctx, err)
  988. }
  989. if !exists {
  990. modCtx.AddMissingDependencies([]string{path.String()})
  991. }
  992. } else if exists, _, err := ctx.Config().fs.Exists(path.String()); err != nil {
  993. ReportPathErrorf(ctx, "%s: %s", path, err.Error())
  994. } else if !exists && !ctx.Config().TestAllowNonExistentPaths {
  995. ReportPathErrorf(ctx, "source path %q does not exist", path)
  996. }
  997. return path
  998. }
  999. // ExistentPathForSource returns an OptionalPath with the SourcePath, rooted from SrcDir, *not*
  1000. // rooted from the module's local source directory, if the path exists, or an empty OptionalPath if
  1001. // it doesn't exist. Dependencies are added so that the ninja file will be regenerated if the state
  1002. // of the path changes.
  1003. func ExistentPathForSource(ctx PathContext, pathComponents ...string) OptionalPath {
  1004. path, err := pathForSource(ctx, pathComponents...)
  1005. if err != nil {
  1006. reportPathError(ctx, err)
  1007. // No need to put the error message into the returned path since it has been reported already.
  1008. return OptionalPath{}
  1009. }
  1010. if pathtools.IsGlob(path.String()) {
  1011. ReportPathErrorf(ctx, "path may not contain a glob: %s", path.String())
  1012. return OptionalPath{}
  1013. }
  1014. exists, err := existsWithDependencies(ctx, path)
  1015. if err != nil {
  1016. reportPathError(ctx, err)
  1017. return OptionalPath{}
  1018. }
  1019. if !exists {
  1020. return InvalidOptionalPath(path.String() + " does not exist")
  1021. }
  1022. return OptionalPathForPath(path)
  1023. }
  1024. func (p SourcePath) String() string {
  1025. return filepath.Join(p.srcDir, p.path)
  1026. }
  1027. // Join creates a new SourcePath with paths... joined with the current path. The
  1028. // provided paths... may not use '..' to escape from the current path.
  1029. func (p SourcePath) Join(ctx PathContext, paths ...string) SourcePath {
  1030. path, err := validatePath(paths...)
  1031. if err != nil {
  1032. reportPathError(ctx, err)
  1033. }
  1034. return p.withRel(path)
  1035. }
  1036. // join is like Join but does less path validation.
  1037. func (p SourcePath) join(ctx PathContext, paths ...string) SourcePath {
  1038. path, err := validateSafePath(paths...)
  1039. if err != nil {
  1040. reportPathError(ctx, err)
  1041. }
  1042. return p.withRel(path)
  1043. }
  1044. // OverlayPath returns the overlay for `path' if it exists. This assumes that the
  1045. // SourcePath is the path to a resource overlay directory.
  1046. func (p SourcePath) OverlayPath(ctx ModuleMissingDepsPathContext, path Path) OptionalPath {
  1047. var relDir string
  1048. if srcPath, ok := path.(SourcePath); ok {
  1049. relDir = srcPath.path
  1050. } else {
  1051. ReportPathErrorf(ctx, "Cannot find relative path for %s(%s)", reflect.TypeOf(path).Name(), path)
  1052. // No need to put the error message into the returned path since it has been reported already.
  1053. return OptionalPath{}
  1054. }
  1055. dir := filepath.Join(p.srcDir, p.path, relDir)
  1056. // Use Glob so that we are run again if the directory is added.
  1057. if pathtools.IsGlob(dir) {
  1058. ReportPathErrorf(ctx, "Path may not contain a glob: %s", dir)
  1059. }
  1060. paths, err := ctx.GlobWithDeps(dir, nil)
  1061. if err != nil {
  1062. ReportPathErrorf(ctx, "glob: %s", err.Error())
  1063. return OptionalPath{}
  1064. }
  1065. if len(paths) == 0 {
  1066. return InvalidOptionalPath(dir + " does not exist")
  1067. }
  1068. relPath := Rel(ctx, p.srcDir, paths[0])
  1069. return OptionalPathForPath(PathForSource(ctx, relPath))
  1070. }
  1071. // OutputPath is a Path representing an intermediates file path rooted from the build directory
  1072. type OutputPath struct {
  1073. basePath
  1074. // The soong build directory, i.e. Config.SoongOutDir()
  1075. soongOutDir string
  1076. fullPath string
  1077. }
  1078. func (p OutputPath) withRel(rel string) OutputPath {
  1079. p.basePath = p.basePath.withRel(rel)
  1080. p.fullPath = filepath.Join(p.fullPath, rel)
  1081. return p
  1082. }
  1083. func (p OutputPath) WithoutRel() OutputPath {
  1084. p.basePath.rel = filepath.Base(p.basePath.path)
  1085. return p
  1086. }
  1087. func (p OutputPath) getSoongOutDir() string {
  1088. return p.soongOutDir
  1089. }
  1090. func (p OutputPath) RelativeToTop() Path {
  1091. return p.outputPathRelativeToTop()
  1092. }
  1093. func (p OutputPath) outputPathRelativeToTop() OutputPath {
  1094. p.fullPath = StringPathRelativeToTop(p.soongOutDir, p.fullPath)
  1095. p.soongOutDir = OutSoongDir
  1096. return p
  1097. }
  1098. func (p OutputPath) objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath {
  1099. return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
  1100. }
  1101. var _ Path = OutputPath{}
  1102. var _ WritablePath = OutputPath{}
  1103. var _ objPathProvider = OutputPath{}
  1104. // toolDepPath is a Path representing a dependency of the build tool.
  1105. type toolDepPath struct {
  1106. basePath
  1107. }
  1108. func (t toolDepPath) RelativeToTop() Path {
  1109. ensureTestOnly()
  1110. return t
  1111. }
  1112. var _ Path = toolDepPath{}
  1113. // pathForBuildToolDep returns a toolDepPath representing the given path string.
  1114. // There is no validation for the path, as it is "trusted": It may fail
  1115. // normal validation checks. For example, it may be an absolute path.
  1116. // Only use this function to construct paths for dependencies of the build
  1117. // tool invocation.
  1118. func pathForBuildToolDep(ctx PathContext, path string) toolDepPath {
  1119. return toolDepPath{basePath{path, ""}}
  1120. }
  1121. // PathForOutput joins the provided paths and returns an OutputPath that is
  1122. // validated to not escape the build dir.
  1123. // On error, it will return a usable, but invalid OutputPath, and report a ModuleError.
  1124. func PathForOutput(ctx PathContext, pathComponents ...string) OutputPath {
  1125. path, err := validatePath(pathComponents...)
  1126. if err != nil {
  1127. reportPathError(ctx, err)
  1128. }
  1129. fullPath := filepath.Join(ctx.Config().soongOutDir, path)
  1130. path = fullPath[len(fullPath)-len(path):]
  1131. return OutputPath{basePath{path, ""}, ctx.Config().soongOutDir, fullPath}
  1132. }
  1133. // PathsForOutput returns Paths rooted from soongOutDir
  1134. func PathsForOutput(ctx PathContext, paths []string) WritablePaths {
  1135. ret := make(WritablePaths, len(paths))
  1136. for i, path := range paths {
  1137. ret[i] = PathForOutput(ctx, path)
  1138. }
  1139. return ret
  1140. }
  1141. func (p OutputPath) writablePath() {}
  1142. func (p OutputPath) String() string {
  1143. return p.fullPath
  1144. }
  1145. // Join creates a new OutputPath with paths... joined with the current path. The
  1146. // provided paths... may not use '..' to escape from the current path.
  1147. func (p OutputPath) Join(ctx PathContext, paths ...string) OutputPath {
  1148. path, err := validatePath(paths...)
  1149. if err != nil {
  1150. reportPathError(ctx, err)
  1151. }
  1152. return p.withRel(path)
  1153. }
  1154. // ReplaceExtension creates a new OutputPath with the extension replaced with ext.
  1155. func (p OutputPath) ReplaceExtension(ctx PathContext, ext string) OutputPath {
  1156. if strings.Contains(ext, "/") {
  1157. ReportPathErrorf(ctx, "extension %q cannot contain /", ext)
  1158. }
  1159. ret := PathForOutput(ctx, pathtools.ReplaceExtension(p.path, ext))
  1160. ret.rel = pathtools.ReplaceExtension(p.rel, ext)
  1161. return ret
  1162. }
  1163. // InSameDir creates a new OutputPath from the directory of the current OutputPath joined with the elements in paths.
  1164. func (p OutputPath) InSameDir(ctx PathContext, paths ...string) OutputPath {
  1165. path, err := validatePath(paths...)
  1166. if err != nil {
  1167. reportPathError(ctx, err)
  1168. }
  1169. ret := PathForOutput(ctx, filepath.Dir(p.path), path)
  1170. ret.rel = filepath.Join(filepath.Dir(p.rel), path)
  1171. return ret
  1172. }
  1173. // PathForIntermediates returns an OutputPath representing the top-level
  1174. // intermediates directory.
  1175. func PathForIntermediates(ctx PathContext, paths ...string) OutputPath {
  1176. path, err := validatePath(paths...)
  1177. if err != nil {
  1178. reportPathError(ctx, err)
  1179. }
  1180. return PathForOutput(ctx, ".intermediates", path)
  1181. }
  1182. var _ genPathProvider = SourcePath{}
  1183. var _ objPathProvider = SourcePath{}
  1184. var _ resPathProvider = SourcePath{}
  1185. // PathForModuleSrc returns a Path representing the paths... under the
  1186. // module's local source directory.
  1187. func PathForModuleSrc(ctx ModuleMissingDepsPathContext, pathComponents ...string) Path {
  1188. // Just join the components textually just to make sure that it does not corrupt a fully qualified
  1189. // module reference, e.g. if the pathComponents is "://other:foo" then using filepath.Join() or
  1190. // validatePath() will corrupt it, e.g. replace "//" with "/". If the path is not a module
  1191. // reference then it will be validated by expandOneSrcPath anyway when it calls expandOneSrcPath.
  1192. p := strings.Join(pathComponents, string(filepath.Separator))
  1193. paths, err := expandOneSrcPath(sourcePathInput{context: ctx, path: p, includeDirs: true})
  1194. if err != nil {
  1195. if depErr, ok := err.(missingDependencyError); ok {
  1196. if ctx.Config().AllowMissingDependencies() {
  1197. ctx.AddMissingDependencies(depErr.missingDeps)
  1198. } else {
  1199. ctx.ModuleErrorf(`%s, is the property annotated with android:"path"?`, depErr.Error())
  1200. }
  1201. } else {
  1202. reportPathError(ctx, err)
  1203. }
  1204. return nil
  1205. } else if len(paths) == 0 {
  1206. ReportPathErrorf(ctx, "%q produced no files, expected exactly one", p)
  1207. return nil
  1208. } else if len(paths) > 1 {
  1209. ReportPathErrorf(ctx, "%q produced %d files, expected exactly one", p, len(paths))
  1210. }
  1211. return paths[0]
  1212. }
  1213. func pathForModuleSrc(ctx EarlyModulePathContext, paths ...string) SourcePath {
  1214. p, err := validatePath(paths...)
  1215. if err != nil {
  1216. reportPathError(ctx, err)
  1217. }
  1218. path, err := pathForSource(ctx, ctx.ModuleDir(), p)
  1219. if err != nil {
  1220. reportPathError(ctx, err)
  1221. }
  1222. path.basePath.rel = p
  1223. return path
  1224. }
  1225. // PathsWithModuleSrcSubDir takes a list of Paths and returns a new list of Paths where Rel() on each path
  1226. // will return the path relative to subDir in the module's source directory. If any input paths are not located
  1227. // inside subDir then a path error will be reported.
  1228. func PathsWithModuleSrcSubDir(ctx EarlyModulePathContext, paths Paths, subDir string) Paths {
  1229. paths = append(Paths(nil), paths...)
  1230. subDirFullPath := pathForModuleSrc(ctx, subDir)
  1231. for i, path := range paths {
  1232. rel := Rel(ctx, subDirFullPath.String(), path.String())
  1233. paths[i] = subDirFullPath.join(ctx, rel)
  1234. }
  1235. return paths
  1236. }
  1237. // PathWithModuleSrcSubDir takes a Path and returns a Path where Rel() will return the path relative to subDir in the
  1238. // module's source directory. If the input path is not located inside subDir then a path error will be reported.
  1239. func PathWithModuleSrcSubDir(ctx EarlyModulePathContext, path Path, subDir string) Path {
  1240. subDirFullPath := pathForModuleSrc(ctx, subDir)
  1241. rel := Rel(ctx, subDirFullPath.String(), path.String())
  1242. return subDirFullPath.Join(ctx, rel)
  1243. }
  1244. // OptionalPathForModuleSrc returns an OptionalPath. The OptionalPath contains a
  1245. // valid path if p is non-nil.
  1246. func OptionalPathForModuleSrc(ctx ModuleMissingDepsPathContext, p *string) OptionalPath {
  1247. if p == nil {
  1248. return OptionalPath{}
  1249. }
  1250. return OptionalPathForPath(PathForModuleSrc(ctx, *p))
  1251. }
  1252. func (p SourcePath) genPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleGenPath {
  1253. return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
  1254. }
  1255. func (p SourcePath) objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath {
  1256. return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
  1257. }
  1258. func (p SourcePath) resPathWithName(ctx ModuleOutPathContext, name string) ModuleResPath {
  1259. // TODO: Use full directory if the new ctx is not the current ctx?
  1260. return PathForModuleRes(ctx, p.path, name)
  1261. }
  1262. // ModuleOutPath is a Path representing a module's output directory.
  1263. type ModuleOutPath struct {
  1264. OutputPath
  1265. }
  1266. func (p ModuleOutPath) RelativeToTop() Path {
  1267. p.OutputPath = p.outputPathRelativeToTop()
  1268. return p
  1269. }
  1270. var _ Path = ModuleOutPath{}
  1271. var _ WritablePath = ModuleOutPath{}
  1272. func (p ModuleOutPath) objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath {
  1273. return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
  1274. }
  1275. // ModuleOutPathContext Subset of ModuleContext functions necessary for output path methods.
  1276. type ModuleOutPathContext interface {
  1277. PathContext
  1278. ModuleName() string
  1279. ModuleDir() string
  1280. ModuleSubDir() string
  1281. }
  1282. func pathForModuleOut(ctx ModuleOutPathContext) OutputPath {
  1283. return PathForOutput(ctx, ".intermediates", ctx.ModuleDir(), ctx.ModuleName(), ctx.ModuleSubDir())
  1284. }
  1285. // PathForVndkRefAbiDump returns an OptionalPath representing the path of the
  1286. // reference abi dump for the given module. This is not guaranteed to be valid.
  1287. func PathForVndkRefAbiDump(ctx ModuleInstallPathContext, version, fileName string,
  1288. isNdk, isLlndkOrVndk, isGzip bool) OptionalPath {
  1289. currentArchType := ctx.Arch().ArchType
  1290. primaryArchType := ctx.Config().DevicePrimaryArchType()
  1291. archName := currentArchType.String()
  1292. if currentArchType != primaryArchType {
  1293. archName += "_" + primaryArchType.String()
  1294. }
  1295. var dirName string
  1296. if isNdk {
  1297. dirName = "ndk"
  1298. } else if isLlndkOrVndk {
  1299. dirName = "vndk"
  1300. } else {
  1301. dirName = "platform" // opt-in libs
  1302. }
  1303. binderBitness := ctx.DeviceConfig().BinderBitness()
  1304. var ext string
  1305. if isGzip {
  1306. ext = ".lsdump.gz"
  1307. } else {
  1308. ext = ".lsdump"
  1309. }
  1310. return ExistentPathForSource(ctx, "prebuilts", "abi-dumps", dirName,
  1311. version, binderBitness, archName, "source-based",
  1312. fileName+ext)
  1313. }
  1314. // PathForModuleOut returns a Path representing the paths... under the module's
  1315. // output directory.
  1316. func PathForModuleOut(ctx ModuleOutPathContext, paths ...string) ModuleOutPath {
  1317. p, err := validatePath(paths...)
  1318. if err != nil {
  1319. reportPathError(ctx, err)
  1320. }
  1321. return ModuleOutPath{
  1322. OutputPath: pathForModuleOut(ctx).withRel(p),
  1323. }
  1324. }
  1325. // ModuleGenPath is a Path representing the 'gen' directory in a module's output
  1326. // directory. Mainly used for generated sources.
  1327. type ModuleGenPath struct {
  1328. ModuleOutPath
  1329. }
  1330. func (p ModuleGenPath) RelativeToTop() Path {
  1331. p.OutputPath = p.outputPathRelativeToTop()
  1332. return p
  1333. }
  1334. var _ Path = ModuleGenPath{}
  1335. var _ WritablePath = ModuleGenPath{}
  1336. var _ genPathProvider = ModuleGenPath{}
  1337. var _ objPathProvider = ModuleGenPath{}
  1338. // PathForModuleGen returns a Path representing the paths... under the module's
  1339. // `gen' directory.
  1340. func PathForModuleGen(ctx ModuleOutPathContext, paths ...string) ModuleGenPath {
  1341. p, err := validatePath(paths...)
  1342. if err != nil {
  1343. reportPathError(ctx, err)
  1344. }
  1345. return ModuleGenPath{
  1346. ModuleOutPath: ModuleOutPath{
  1347. OutputPath: pathForModuleOut(ctx).withRel("gen").withRel(p),
  1348. },
  1349. }
  1350. }
  1351. func (p ModuleGenPath) genPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleGenPath {
  1352. // TODO: make a different path for local vs remote generated files?
  1353. return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
  1354. }
  1355. func (p ModuleGenPath) objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath {
  1356. return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
  1357. }
  1358. // ModuleObjPath is a Path representing the 'obj' directory in a module's output
  1359. // directory. Used for compiled objects.
  1360. type ModuleObjPath struct {
  1361. ModuleOutPath
  1362. }
  1363. func (p ModuleObjPath) RelativeToTop() Path {
  1364. p.OutputPath = p.outputPathRelativeToTop()
  1365. return p
  1366. }
  1367. var _ Path = ModuleObjPath{}
  1368. var _ WritablePath = ModuleObjPath{}
  1369. // PathForModuleObj returns a Path representing the paths... under the module's
  1370. // 'obj' directory.
  1371. func PathForModuleObj(ctx ModuleOutPathContext, pathComponents ...string) ModuleObjPath {
  1372. p, err := validatePath(pathComponents...)
  1373. if err != nil {
  1374. reportPathError(ctx, err)
  1375. }
  1376. return ModuleObjPath{PathForModuleOut(ctx, "obj", p)}
  1377. }
  1378. // ModuleResPath is a a Path representing the 'res' directory in a module's
  1379. // output directory.
  1380. type ModuleResPath struct {
  1381. ModuleOutPath
  1382. }
  1383. func (p ModuleResPath) RelativeToTop() Path {
  1384. p.OutputPath = p.outputPathRelativeToTop()
  1385. return p
  1386. }
  1387. var _ Path = ModuleResPath{}
  1388. var _ WritablePath = ModuleResPath{}
  1389. // PathForModuleRes returns a Path representing the paths... under the module's
  1390. // 'res' directory.
  1391. func PathForModuleRes(ctx ModuleOutPathContext, pathComponents ...string) ModuleResPath {
  1392. p, err := validatePath(pathComponents...)
  1393. if err != nil {
  1394. reportPathError(ctx, err)
  1395. }
  1396. return ModuleResPath{PathForModuleOut(ctx, "res", p)}
  1397. }
  1398. // InstallPath is a Path representing a installed file path rooted from the build directory
  1399. type InstallPath struct {
  1400. basePath
  1401. // The soong build directory, i.e. Config.SoongOutDir()
  1402. soongOutDir string
  1403. // partitionDir is the part of the InstallPath that is automatically determined according to the context.
  1404. // For example, it is host/<os>-<arch> for host modules, and target/product/<device>/<partition> for device modules.
  1405. partitionDir string
  1406. partition string
  1407. // makePath indicates whether this path is for Soong (false) or Make (true).
  1408. makePath bool
  1409. }
  1410. // Will panic if called from outside a test environment.
  1411. func ensureTestOnly() {
  1412. if PrefixInList(os.Args, "-test.") {
  1413. return
  1414. }
  1415. panic(fmt.Errorf("Not in test. Command line:\n %s", strings.Join(os.Args, "\n ")))
  1416. }
  1417. func (p InstallPath) RelativeToTop() Path {
  1418. ensureTestOnly()
  1419. p.soongOutDir = OutSoongDir
  1420. return p
  1421. }
  1422. func (p InstallPath) getSoongOutDir() string {
  1423. return p.soongOutDir
  1424. }
  1425. func (p InstallPath) ReplaceExtension(ctx PathContext, ext string) OutputPath {
  1426. panic("Not implemented")
  1427. }
  1428. var _ Path = InstallPath{}
  1429. var _ WritablePath = InstallPath{}
  1430. func (p InstallPath) writablePath() {}
  1431. func (p InstallPath) String() string {
  1432. if p.makePath {
  1433. // Make path starts with out/ instead of out/soong.
  1434. return filepath.Join(p.soongOutDir, "../", p.path)
  1435. } else {
  1436. return filepath.Join(p.soongOutDir, p.path)
  1437. }
  1438. }
  1439. // PartitionDir returns the path to the partition where the install path is rooted at. It is
  1440. // out/soong/target/product/<device>/<partition> for device modules, and out/soong/host/<os>-<arch> for host modules.
  1441. // The ./soong is dropped if the install path is for Make.
  1442. func (p InstallPath) PartitionDir() string {
  1443. if p.makePath {
  1444. return filepath.Join(p.soongOutDir, "../", p.partitionDir)
  1445. } else {
  1446. return filepath.Join(p.soongOutDir, p.partitionDir)
  1447. }
  1448. }
  1449. // Join creates a new InstallPath with paths... joined with the current path. The
  1450. // provided paths... may not use '..' to escape from the current path.
  1451. func (p InstallPath) Join(ctx PathContext, paths ...string) InstallPath {
  1452. path, err := validatePath(paths...)
  1453. if err != nil {
  1454. reportPathError(ctx, err)
  1455. }
  1456. return p.withRel(path)
  1457. }
  1458. func (p InstallPath) withRel(rel string) InstallPath {
  1459. p.basePath = p.basePath.withRel(rel)
  1460. return p
  1461. }
  1462. // Deprecated: ToMakePath is a noop, PathForModuleInstall always returns Make paths when building
  1463. // embedded in Make.
  1464. func (p InstallPath) ToMakePath() InstallPath {
  1465. p.makePath = true
  1466. return p
  1467. }
  1468. // PathForModuleInstall returns a Path representing the install path for the
  1469. // module appended with paths...
  1470. func PathForModuleInstall(ctx ModuleInstallPathContext, pathComponents ...string) InstallPath {
  1471. os, arch := osAndArch(ctx)
  1472. partition := modulePartition(ctx, os)
  1473. return makePathForInstall(ctx, os, arch, partition, ctx.Debug(), pathComponents...)
  1474. }
  1475. // PathForHostDexInstall returns an InstallPath representing the install path for the
  1476. // module appended with paths...
  1477. func PathForHostDexInstall(ctx ModuleInstallPathContext, pathComponents ...string) InstallPath {
  1478. return makePathForInstall(ctx, ctx.Config().BuildOS, ctx.Config().BuildArch, "", ctx.Debug(), pathComponents...)
  1479. }
  1480. // PathForModuleInPartitionInstall is similar to PathForModuleInstall but partition is provided by the caller
  1481. func PathForModuleInPartitionInstall(ctx ModuleInstallPathContext, partition string, pathComponents ...string) InstallPath {
  1482. os, arch := osAndArch(ctx)
  1483. return makePathForInstall(ctx, os, arch, partition, ctx.Debug(), pathComponents...)
  1484. }
  1485. func osAndArch(ctx ModuleInstallPathContext) (OsType, ArchType) {
  1486. os := ctx.Os()
  1487. arch := ctx.Arch().ArchType
  1488. forceOS, forceArch := ctx.InstallForceOS()
  1489. if forceOS != nil {
  1490. os = *forceOS
  1491. }
  1492. if forceArch != nil {
  1493. arch = *forceArch
  1494. }
  1495. return os, arch
  1496. }
  1497. func makePathForInstall(ctx ModuleInstallPathContext, os OsType, arch ArchType, partition string, debug bool, pathComponents ...string) InstallPath {
  1498. ret := pathForInstall(ctx, os, arch, partition, debug, pathComponents...)
  1499. return ret
  1500. }
  1501. func pathForInstall(ctx PathContext, os OsType, arch ArchType, partition string, debug bool,
  1502. pathComponents ...string) InstallPath {
  1503. var partionPaths []string
  1504. if os.Class == Device {
  1505. partionPaths = []string{"target", "product", ctx.Config().DeviceName(), partition}
  1506. } else {
  1507. osName := os.String()
  1508. if os == Linux || os == LinuxMusl {
  1509. // instead of linux_glibc
  1510. osName = "linux"
  1511. }
  1512. // SOONG_HOST_OUT is set to out/host/$(HOST_OS)-$(HOST_PREBUILT_ARCH)
  1513. // and HOST_PREBUILT_ARCH is forcibly set to x86 even on x86_64 hosts. We don't seem
  1514. // to have a plan to fix it (see the comment in build/make/core/envsetup.mk).
  1515. // Let's keep using x86 for the existing cases until we have a need to support
  1516. // other architectures.
  1517. archName := arch.String()
  1518. if os.Class == Host && (arch == X86_64 || arch == Common) {
  1519. archName = "x86"
  1520. }
  1521. partionPaths = []string{"host", osName + "-" + archName, partition}
  1522. }
  1523. if debug {
  1524. partionPaths = append([]string{"debug"}, partionPaths...)
  1525. }
  1526. partionPath, err := validatePath(partionPaths...)
  1527. if err != nil {
  1528. reportPathError(ctx, err)
  1529. }
  1530. base := InstallPath{
  1531. basePath: basePath{partionPath, ""},
  1532. soongOutDir: ctx.Config().soongOutDir,
  1533. partitionDir: partionPath,
  1534. partition: partition,
  1535. }
  1536. if ctx.Config().KatiEnabled() {
  1537. base.makePath = true
  1538. }
  1539. return base.Join(ctx, pathComponents...)
  1540. }
  1541. func pathForNdkOrSdkInstall(ctx PathContext, prefix string, paths []string) InstallPath {
  1542. base := InstallPath{
  1543. basePath: basePath{prefix, ""},
  1544. soongOutDir: ctx.Config().soongOutDir,
  1545. partitionDir: prefix,
  1546. makePath: false,
  1547. }
  1548. return base.Join(ctx, paths...)
  1549. }
  1550. func PathForNdkInstall(ctx PathContext, paths ...string) InstallPath {
  1551. return pathForNdkOrSdkInstall(ctx, "ndk", paths)
  1552. }
  1553. func PathForMainlineSdksInstall(ctx PathContext, paths ...string) InstallPath {
  1554. return pathForNdkOrSdkInstall(ctx, "mainline-sdks", paths)
  1555. }
  1556. func InstallPathToOnDevicePath(ctx PathContext, path InstallPath) string {
  1557. rel := Rel(ctx, strings.TrimSuffix(path.PartitionDir(), path.partition), path.String())
  1558. return "/" + rel
  1559. }
  1560. func modulePartition(ctx ModuleInstallPathContext, os OsType) string {
  1561. var partition string
  1562. if ctx.InstallInTestcases() {
  1563. // "testcases" install directory can be used for host or device modules.
  1564. partition = "testcases"
  1565. } else if os.Class == Device {
  1566. if ctx.InstallInData() {
  1567. partition = "data"
  1568. } else if ctx.InstallInRamdisk() {
  1569. if ctx.DeviceConfig().BoardUsesRecoveryAsBoot() {
  1570. partition = "recovery/root/first_stage_ramdisk"
  1571. } else {
  1572. partition = "ramdisk"
  1573. }
  1574. if !ctx.InstallInRoot() {
  1575. partition += "/system"
  1576. }
  1577. } else if ctx.InstallInVendorRamdisk() {
  1578. // The module is only available after switching root into
  1579. // /first_stage_ramdisk. To expose the module before switching root
  1580. // on a device without a dedicated recovery partition, install the
  1581. // recovery variant.
  1582. if ctx.DeviceConfig().BoardMoveRecoveryResourcesToVendorBoot() {
  1583. partition = "vendor_ramdisk/first_stage_ramdisk"
  1584. } else {
  1585. partition = "vendor_ramdisk"
  1586. }
  1587. if !ctx.InstallInRoot() {
  1588. partition += "/system"
  1589. }
  1590. } else if ctx.InstallInDebugRamdisk() {
  1591. partition = "debug_ramdisk"
  1592. } else if ctx.InstallInRecovery() {
  1593. if ctx.InstallInRoot() {
  1594. partition = "recovery/root"
  1595. } else {
  1596. // the layout of recovery partion is the same as that of system partition
  1597. partition = "recovery/root/system"
  1598. }
  1599. } else if ctx.SocSpecific() {
  1600. partition = ctx.DeviceConfig().VendorPath()
  1601. } else if ctx.DeviceSpecific() {
  1602. partition = ctx.DeviceConfig().OdmPath()
  1603. } else if ctx.ProductSpecific() {
  1604. partition = ctx.DeviceConfig().ProductPath()
  1605. } else if ctx.SystemExtSpecific() {
  1606. partition = ctx.DeviceConfig().SystemExtPath()
  1607. } else if ctx.InstallInRoot() {
  1608. partition = "root"
  1609. } else {
  1610. partition = "system"
  1611. }
  1612. if ctx.InstallInSanitizerDir() {
  1613. partition = "data/asan/" + partition
  1614. }
  1615. }
  1616. return partition
  1617. }
  1618. type InstallPaths []InstallPath
  1619. // Paths returns the InstallPaths as a Paths
  1620. func (p InstallPaths) Paths() Paths {
  1621. if p == nil {
  1622. return nil
  1623. }
  1624. ret := make(Paths, len(p))
  1625. for i, path := range p {
  1626. ret[i] = path
  1627. }
  1628. return ret
  1629. }
  1630. // Strings returns the string forms of the install paths.
  1631. func (p InstallPaths) Strings() []string {
  1632. if p == nil {
  1633. return nil
  1634. }
  1635. ret := make([]string, len(p))
  1636. for i, path := range p {
  1637. ret[i] = path.String()
  1638. }
  1639. return ret
  1640. }
  1641. // validateSafePath validates a path that we trust (may contain ninja variables).
  1642. // Ensures that each path component does not attempt to leave its component.
  1643. func validateSafePath(pathComponents ...string) (string, error) {
  1644. for _, path := range pathComponents {
  1645. path := filepath.Clean(path)
  1646. if path == ".." || strings.HasPrefix(path, "../") || strings.HasPrefix(path, "/") {
  1647. return "", fmt.Errorf("Path is outside directory: %s", path)
  1648. }
  1649. }
  1650. // TODO: filepath.Join isn't necessarily correct with embedded ninja
  1651. // variables. '..' may remove the entire ninja variable, even if it
  1652. // will be expanded to multiple nested directories.
  1653. return filepath.Join(pathComponents...), nil
  1654. }
  1655. // validatePath validates that a path does not include ninja variables, and that
  1656. // each path component does not attempt to leave its component. Returns a joined
  1657. // version of each path component.
  1658. func validatePath(pathComponents ...string) (string, error) {
  1659. for _, path := range pathComponents {
  1660. if strings.Contains(path, "$") {
  1661. return "", fmt.Errorf("Path contains invalid character($): %s", path)
  1662. }
  1663. }
  1664. return validateSafePath(pathComponents...)
  1665. }
  1666. func PathForPhony(ctx PathContext, phony string) WritablePath {
  1667. if strings.ContainsAny(phony, "$/") {
  1668. ReportPathErrorf(ctx, "Phony target contains invalid character ($ or /): %s", phony)
  1669. }
  1670. return PhonyPath{basePath{phony, ""}}
  1671. }
  1672. type PhonyPath struct {
  1673. basePath
  1674. }
  1675. func (p PhonyPath) writablePath() {}
  1676. func (p PhonyPath) getSoongOutDir() string {
  1677. // A phone path cannot contain any / so cannot be relative to the build directory.
  1678. return ""
  1679. }
  1680. func (p PhonyPath) RelativeToTop() Path {
  1681. ensureTestOnly()
  1682. // A phony path cannot contain any / so does not have a build directory so switching to a new
  1683. // build directory has no effect so just return this path.
  1684. return p
  1685. }
  1686. func (p PhonyPath) ReplaceExtension(ctx PathContext, ext string) OutputPath {
  1687. panic("Not implemented")
  1688. }
  1689. var _ Path = PhonyPath{}
  1690. var _ WritablePath = PhonyPath{}
  1691. type testPath struct {
  1692. basePath
  1693. }
  1694. func (p testPath) RelativeToTop() Path {
  1695. ensureTestOnly()
  1696. return p
  1697. }
  1698. func (p testPath) String() string {
  1699. return p.path
  1700. }
  1701. var _ Path = testPath{}
  1702. // PathForTesting returns a Path constructed from joining the elements of paths with '/'. It should only be used from
  1703. // within tests.
  1704. func PathForTesting(paths ...string) Path {
  1705. p, err := validateSafePath(paths...)
  1706. if err != nil {
  1707. panic(err)
  1708. }
  1709. return testPath{basePath{path: p, rel: p}}
  1710. }
  1711. // PathsForTesting returns a Path constructed from each element in strs. It should only be used from within tests.
  1712. func PathsForTesting(strs ...string) Paths {
  1713. p := make(Paths, len(strs))
  1714. for i, s := range strs {
  1715. p[i] = PathForTesting(s)
  1716. }
  1717. return p
  1718. }
  1719. type testPathContext struct {
  1720. config Config
  1721. }
  1722. func (x *testPathContext) Config() Config { return x.config }
  1723. func (x *testPathContext) AddNinjaFileDeps(...string) {}
  1724. // PathContextForTesting returns a PathContext that can be used in tests, for example to create an OutputPath with
  1725. // PathForOutput.
  1726. func PathContextForTesting(config Config) PathContext {
  1727. return &testPathContext{
  1728. config: config,
  1729. }
  1730. }
  1731. type testModuleInstallPathContext struct {
  1732. baseModuleContext
  1733. inData bool
  1734. inTestcases bool
  1735. inSanitizerDir bool
  1736. inRamdisk bool
  1737. inVendorRamdisk bool
  1738. inDebugRamdisk bool
  1739. inRecovery bool
  1740. inRoot bool
  1741. forceOS *OsType
  1742. forceArch *ArchType
  1743. }
  1744. func (m testModuleInstallPathContext) Config() Config {
  1745. return m.baseModuleContext.config
  1746. }
  1747. func (testModuleInstallPathContext) AddNinjaFileDeps(deps ...string) {}
  1748. func (m testModuleInstallPathContext) InstallInData() bool {
  1749. return m.inData
  1750. }
  1751. func (m testModuleInstallPathContext) InstallInTestcases() bool {
  1752. return m.inTestcases
  1753. }
  1754. func (m testModuleInstallPathContext) InstallInSanitizerDir() bool {
  1755. return m.inSanitizerDir
  1756. }
  1757. func (m testModuleInstallPathContext) InstallInRamdisk() bool {
  1758. return m.inRamdisk
  1759. }
  1760. func (m testModuleInstallPathContext) InstallInVendorRamdisk() bool {
  1761. return m.inVendorRamdisk
  1762. }
  1763. func (m testModuleInstallPathContext) InstallInDebugRamdisk() bool {
  1764. return m.inDebugRamdisk
  1765. }
  1766. func (m testModuleInstallPathContext) InstallInRecovery() bool {
  1767. return m.inRecovery
  1768. }
  1769. func (m testModuleInstallPathContext) InstallInRoot() bool {
  1770. return m.inRoot
  1771. }
  1772. func (m testModuleInstallPathContext) InstallForceOS() (*OsType, *ArchType) {
  1773. return m.forceOS, m.forceArch
  1774. }
  1775. // Construct a minimal ModuleInstallPathContext for testing. Note that baseModuleContext is
  1776. // default-initialized, which leaves blueprint.baseModuleContext set to nil, so methods that are
  1777. // delegated to it will panic.
  1778. func ModuleInstallPathContextForTesting(config Config) ModuleInstallPathContext {
  1779. ctx := &testModuleInstallPathContext{}
  1780. ctx.config = config
  1781. ctx.os = Android
  1782. return ctx
  1783. }
  1784. // Rel performs the same function as filepath.Rel, but reports errors to a PathContext, and reports an error if
  1785. // targetPath is not inside basePath.
  1786. func Rel(ctx PathContext, basePath string, targetPath string) string {
  1787. rel, isRel := MaybeRel(ctx, basePath, targetPath)
  1788. if !isRel {
  1789. ReportPathErrorf(ctx, "path %q is not under path %q", targetPath, basePath)
  1790. return ""
  1791. }
  1792. return rel
  1793. }
  1794. // MaybeRel performs the same function as filepath.Rel, but reports errors to a PathContext, and returns false if
  1795. // targetPath is not inside basePath.
  1796. func MaybeRel(ctx PathContext, basePath string, targetPath string) (string, bool) {
  1797. rel, isRel, err := maybeRelErr(basePath, targetPath)
  1798. if err != nil {
  1799. reportPathError(ctx, err)
  1800. }
  1801. return rel, isRel
  1802. }
  1803. func maybeRelErr(basePath string, targetPath string) (string, bool, error) {
  1804. // filepath.Rel returns an error if one path is absolute and the other is not, handle that case first.
  1805. if filepath.IsAbs(basePath) != filepath.IsAbs(targetPath) {
  1806. return "", false, nil
  1807. }
  1808. rel, err := filepath.Rel(basePath, targetPath)
  1809. if err != nil {
  1810. return "", false, err
  1811. } else if rel == ".." || strings.HasPrefix(rel, "../") || strings.HasPrefix(rel, "/") {
  1812. return "", false, nil
  1813. }
  1814. return rel, true, nil
  1815. }
  1816. // Writes a file to the output directory. Attempting to write directly to the output directory
  1817. // will fail due to the sandbox of the soong_build process.
  1818. func WriteFileToOutputDir(path WritablePath, data []byte, perm os.FileMode) error {
  1819. absPath := absolutePath(path.String())
  1820. err := os.MkdirAll(filepath.Dir(absPath), 0777)
  1821. if err != nil {
  1822. return err
  1823. }
  1824. return ioutil.WriteFile(absPath, data, perm)
  1825. }
  1826. func RemoveAllOutputDir(path WritablePath) error {
  1827. return os.RemoveAll(absolutePath(path.String()))
  1828. }
  1829. func CreateOutputDirIfNonexistent(path WritablePath, perm os.FileMode) error {
  1830. dir := absolutePath(path.String())
  1831. return createDirIfNonexistent(dir, perm)
  1832. }
  1833. func createDirIfNonexistent(dir string, perm os.FileMode) error {
  1834. if _, err := os.Stat(dir); os.IsNotExist(err) {
  1835. return os.MkdirAll(dir, os.ModePerm)
  1836. } else {
  1837. return err
  1838. }
  1839. }
  1840. // absolutePath is deliberately private so that Soong's Go plugins can't use it to find and
  1841. // read arbitrary files without going through the methods in the current package that track
  1842. // dependencies.
  1843. func absolutePath(path string) string {
  1844. if filepath.IsAbs(path) {
  1845. return path
  1846. }
  1847. return filepath.Join(absSrcDir, path)
  1848. }
  1849. // A DataPath represents the path of a file to be used as data, for example
  1850. // a test library to be installed alongside a test.
  1851. // The data file should be installed (copied from `<SrcPath>`) to
  1852. // `<install_root>/<RelativeInstallPath>/<filename>`, or
  1853. // `<install_root>/<filename>` if RelativeInstallPath is empty.
  1854. type DataPath struct {
  1855. // The path of the data file that should be copied into the data directory
  1856. SrcPath Path
  1857. // The install path of the data file, relative to the install root.
  1858. RelativeInstallPath string
  1859. }
  1860. // PathsIfNonNil returns a Paths containing only the non-nil input arguments.
  1861. func PathsIfNonNil(paths ...Path) Paths {
  1862. if len(paths) == 0 {
  1863. // Fast path for empty argument list
  1864. return nil
  1865. } else if len(paths) == 1 {
  1866. // Fast path for a single argument
  1867. if paths[0] != nil {
  1868. return paths
  1869. } else {
  1870. return nil
  1871. }
  1872. }
  1873. ret := make(Paths, 0, len(paths))
  1874. for _, path := range paths {
  1875. if path != nil {
  1876. ret = append(ret, path)
  1877. }
  1878. }
  1879. if len(ret) == 0 {
  1880. return nil
  1881. }
  1882. return ret
  1883. }
  1884. var thirdPartyDirPrefixExceptions = []*regexp.Regexp{
  1885. regexp.MustCompile("^vendor/[^/]*google[^/]*/"),
  1886. regexp.MustCompile("^hardware/google/"),
  1887. regexp.MustCompile("^hardware/interfaces/"),
  1888. regexp.MustCompile("^hardware/libhardware[^/]*/"),
  1889. regexp.MustCompile("^hardware/ril/"),
  1890. }
  1891. func IsThirdPartyPath(path string) bool {
  1892. thirdPartyDirPrefixes := []string{"external/", "vendor/", "hardware/"}
  1893. if HasAnyPrefix(path, thirdPartyDirPrefixes) {
  1894. for _, prefix := range thirdPartyDirPrefixExceptions {
  1895. if prefix.MatchString(path) {
  1896. return false
  1897. }
  1898. }
  1899. return true
  1900. }
  1901. return false
  1902. }
  1903. // PathsDepSet is a thin type-safe wrapper around the generic depSet. It always uses
  1904. // topological order.
  1905. type PathsDepSet struct {
  1906. depSet
  1907. }
  1908. // newPathsDepSet returns an immutable PathsDepSet with the given direct and
  1909. // transitive contents.
  1910. func newPathsDepSet(direct Paths, transitive []*PathsDepSet) *PathsDepSet {
  1911. return &PathsDepSet{*newDepSet(TOPOLOGICAL, direct, transitive)}
  1912. }
  1913. // ToList returns the PathsDepSet flattened to a list in topological order.
  1914. func (d *PathsDepSet) ToList() Paths {
  1915. if d == nil {
  1916. return nil
  1917. }
  1918. return d.depSet.ToList().(Paths)
  1919. }