testing.go 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239
  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 android
  15. import (
  16. "bytes"
  17. "fmt"
  18. "path/filepath"
  19. "regexp"
  20. "sort"
  21. "strings"
  22. "sync"
  23. "testing"
  24. mkparser "android/soong/androidmk/parser"
  25. "github.com/google/blueprint"
  26. "github.com/google/blueprint/proptools"
  27. )
  28. func NewTestContext(config Config) *TestContext {
  29. namespaceExportFilter := func(namespace *Namespace) bool {
  30. return true
  31. }
  32. nameResolver := NewNameResolver(namespaceExportFilter)
  33. ctx := &TestContext{
  34. Context: &Context{blueprint.NewContext(), config},
  35. NameResolver: nameResolver,
  36. }
  37. ctx.SetNameInterface(nameResolver)
  38. ctx.postDeps = append(ctx.postDeps, registerPathDepsMutator)
  39. ctx.SetFs(ctx.config.fs)
  40. if ctx.config.mockBpList != "" {
  41. ctx.SetModuleListFile(ctx.config.mockBpList)
  42. }
  43. return ctx
  44. }
  45. var PrepareForTestWithArchMutator = GroupFixturePreparers(
  46. // Configure architecture targets in the fixture config.
  47. FixtureModifyConfig(modifyTestConfigToSupportArchMutator),
  48. // Add the arch mutator to the context.
  49. FixtureRegisterWithContext(func(ctx RegistrationContext) {
  50. ctx.PreDepsMutators(registerArchMutator)
  51. }),
  52. )
  53. var PrepareForTestWithDefaults = FixtureRegisterWithContext(func(ctx RegistrationContext) {
  54. ctx.PreArchMutators(RegisterDefaultsPreArchMutators)
  55. })
  56. var PrepareForTestWithComponentsMutator = FixtureRegisterWithContext(func(ctx RegistrationContext) {
  57. ctx.PreArchMutators(RegisterComponentsMutator)
  58. })
  59. var PrepareForTestWithPrebuilts = FixtureRegisterWithContext(RegisterPrebuiltMutators)
  60. var PrepareForTestWithOverrides = FixtureRegisterWithContext(func(ctx RegistrationContext) {
  61. ctx.PostDepsMutators(RegisterOverridePostDepsMutators)
  62. })
  63. var PrepareForTestWithLicenses = GroupFixturePreparers(
  64. FixtureRegisterWithContext(RegisterLicenseKindBuildComponents),
  65. FixtureRegisterWithContext(RegisterLicenseBuildComponents),
  66. FixtureRegisterWithContext(registerLicenseMutators),
  67. )
  68. var PrepareForTestWithGenNotice = FixtureRegisterWithContext(RegisterGenNoticeBuildComponents)
  69. func registerLicenseMutators(ctx RegistrationContext) {
  70. ctx.PreArchMutators(RegisterLicensesPackageMapper)
  71. ctx.PreArchMutators(RegisterLicensesPropertyGatherer)
  72. ctx.PostDepsMutators(RegisterLicensesDependencyChecker)
  73. }
  74. var PrepareForTestWithLicenseDefaultModules = GroupFixturePreparers(
  75. FixtureAddTextFile("build/soong/licenses/Android.bp", `
  76. license {
  77. name: "Android-Apache-2.0",
  78. package_name: "Android",
  79. license_kinds: ["SPDX-license-identifier-Apache-2.0"],
  80. copyright_notice: "Copyright (C) The Android Open Source Project",
  81. license_text: ["LICENSE"],
  82. }
  83. license_kind {
  84. name: "SPDX-license-identifier-Apache-2.0",
  85. conditions: ["notice"],
  86. url: "https://spdx.org/licenses/Apache-2.0.html",
  87. }
  88. license_kind {
  89. name: "legacy_unencumbered",
  90. conditions: ["unencumbered"],
  91. }
  92. `),
  93. FixtureAddFile("build/soong/licenses/LICENSE", nil),
  94. )
  95. var PrepareForTestWithNamespace = FixtureRegisterWithContext(func(ctx RegistrationContext) {
  96. registerNamespaceBuildComponents(ctx)
  97. ctx.PreArchMutators(RegisterNamespaceMutator)
  98. })
  99. var PrepareForTestWithMakevars = FixtureRegisterWithContext(func(ctx RegistrationContext) {
  100. ctx.RegisterSingletonType("makevars", makeVarsSingletonFunc)
  101. })
  102. // Test fixture preparer that will register most java build components.
  103. //
  104. // Singletons and mutators should only be added here if they are needed for a majority of java
  105. // module types, otherwise they should be added under a separate preparer to allow them to be
  106. // selected only when needed to reduce test execution time.
  107. //
  108. // Module types do not have much of an overhead unless they are used so this should include as many
  109. // module types as possible. The exceptions are those module types that require mutators and/or
  110. // singletons in order to function in which case they should be kept together in a separate
  111. // preparer.
  112. //
  113. // The mutators in this group were chosen because they are needed by the vast majority of tests.
  114. var PrepareForTestWithAndroidBuildComponents = GroupFixturePreparers(
  115. // Sorted alphabetically as the actual order does not matter as tests automatically enforce the
  116. // correct order.
  117. PrepareForTestWithArchMutator,
  118. PrepareForTestWithComponentsMutator,
  119. PrepareForTestWithDefaults,
  120. PrepareForTestWithFilegroup,
  121. PrepareForTestWithOverrides,
  122. PrepareForTestWithPackageModule,
  123. PrepareForTestWithPrebuilts,
  124. PrepareForTestWithVisibility,
  125. )
  126. // Prepares an integration test with all build components from the android package.
  127. //
  128. // This should only be used by tests that want to run with as much of the build enabled as possible.
  129. var PrepareForIntegrationTestWithAndroid = GroupFixturePreparers(
  130. PrepareForTestWithAndroidBuildComponents,
  131. )
  132. // Prepares a test that may be missing dependencies by setting allow_missing_dependencies to
  133. // true.
  134. var PrepareForTestWithAllowMissingDependencies = GroupFixturePreparers(
  135. FixtureModifyProductVariables(func(variables FixtureProductVariables) {
  136. variables.Allow_missing_dependencies = proptools.BoolPtr(true)
  137. }),
  138. FixtureModifyContext(func(ctx *TestContext) {
  139. ctx.SetAllowMissingDependencies(true)
  140. }),
  141. )
  142. // Prepares a test that disallows non-existent paths.
  143. var PrepareForTestDisallowNonExistentPaths = FixtureModifyConfig(func(config Config) {
  144. config.TestAllowNonExistentPaths = false
  145. })
  146. func NewTestArchContext(config Config) *TestContext {
  147. ctx := NewTestContext(config)
  148. ctx.preDeps = append(ctx.preDeps, registerArchMutator)
  149. return ctx
  150. }
  151. type TestContext struct {
  152. *Context
  153. preArch, preDeps, postDeps, finalDeps []RegisterMutatorFunc
  154. bp2buildPreArch, bp2buildMutators []RegisterMutatorFunc
  155. NameResolver *NameResolver
  156. // The list of pre-singletons and singletons registered for the test.
  157. preSingletons, singletons sortableComponents
  158. // The order in which the pre-singletons, mutators and singletons will be run in this test
  159. // context; for debugging.
  160. preSingletonOrder, mutatorOrder, singletonOrder []string
  161. }
  162. func (ctx *TestContext) PreArchMutators(f RegisterMutatorFunc) {
  163. ctx.preArch = append(ctx.preArch, f)
  164. }
  165. func (ctx *TestContext) HardCodedPreArchMutators(f RegisterMutatorFunc) {
  166. // Register mutator function as normal for testing.
  167. ctx.PreArchMutators(f)
  168. }
  169. func (ctx *TestContext) PreDepsMutators(f RegisterMutatorFunc) {
  170. ctx.preDeps = append(ctx.preDeps, f)
  171. }
  172. func (ctx *TestContext) PostDepsMutators(f RegisterMutatorFunc) {
  173. ctx.postDeps = append(ctx.postDeps, f)
  174. }
  175. func (ctx *TestContext) FinalDepsMutators(f RegisterMutatorFunc) {
  176. ctx.finalDeps = append(ctx.finalDeps, f)
  177. }
  178. func (ctx *TestContext) RegisterBp2BuildConfig(config bp2BuildConversionAllowlist) {
  179. ctx.config.bp2buildPackageConfig = config
  180. }
  181. // PreArchBp2BuildMutators adds mutators to be register for converting Android Blueprint modules
  182. // into Bazel BUILD targets that should run prior to deps and conversion.
  183. func (ctx *TestContext) PreArchBp2BuildMutators(f RegisterMutatorFunc) {
  184. ctx.bp2buildPreArch = append(ctx.bp2buildPreArch, f)
  185. }
  186. // registeredComponentOrder defines the order in which a sortableComponent type is registered at
  187. // runtime and provides support for reordering the components registered for a test in the same
  188. // way.
  189. type registeredComponentOrder struct {
  190. // The name of the component type, used for error messages.
  191. componentType string
  192. // The names of the registered components in the order in which they were registered.
  193. namesInOrder []string
  194. // Maps from the component name to its position in the runtime ordering.
  195. namesToIndex map[string]int
  196. // A function that defines the order between two named components that can be used to sort a slice
  197. // of component names into the same order as they appear in namesInOrder.
  198. less func(string, string) bool
  199. }
  200. // registeredComponentOrderFromExistingOrder takes an existing slice of sortableComponents and
  201. // creates a registeredComponentOrder that contains a less function that can be used to sort a
  202. // subset of that list of names so it is in the same order as the original sortableComponents.
  203. func registeredComponentOrderFromExistingOrder(componentType string, existingOrder sortableComponents) registeredComponentOrder {
  204. // Only the names from the existing order are needed for this so create a list of component names
  205. // in the correct order.
  206. namesInOrder := componentsToNames(existingOrder)
  207. // Populate the map from name to position in the list.
  208. nameToIndex := make(map[string]int)
  209. for i, n := range namesInOrder {
  210. nameToIndex[n] = i
  211. }
  212. // A function to use to map from a name to an index in the original order.
  213. indexOf := func(name string) int {
  214. index, ok := nameToIndex[name]
  215. if !ok {
  216. // Should never happen as tests that use components that are not known at runtime do not sort
  217. // so should never use this function.
  218. panic(fmt.Errorf("internal error: unknown %s %q should be one of %s", componentType, name, strings.Join(namesInOrder, ", ")))
  219. }
  220. return index
  221. }
  222. // The less function.
  223. less := func(n1, n2 string) bool {
  224. i1 := indexOf(n1)
  225. i2 := indexOf(n2)
  226. return i1 < i2
  227. }
  228. return registeredComponentOrder{
  229. componentType: componentType,
  230. namesInOrder: namesInOrder,
  231. namesToIndex: nameToIndex,
  232. less: less,
  233. }
  234. }
  235. // componentsToNames maps from the slice of components to a slice of their names.
  236. func componentsToNames(components sortableComponents) []string {
  237. names := make([]string, len(components))
  238. for i, c := range components {
  239. names[i] = c.componentName()
  240. }
  241. return names
  242. }
  243. // enforceOrdering enforces the supplied components are in the same order as is defined in this
  244. // object.
  245. //
  246. // If the supplied components contains any components that are not registered at runtime, i.e. test
  247. // specific components, then it is impossible to sort them into an order that both matches the
  248. // runtime and also preserves the implicit ordering defined in the test. In that case it will not
  249. // sort the components, instead it will just check that the components are in the correct order.
  250. //
  251. // Otherwise, this will sort the supplied components in place.
  252. func (o *registeredComponentOrder) enforceOrdering(components sortableComponents) {
  253. // Check to see if the list of components contains any components that are
  254. // not registered at runtime.
  255. var unknownComponents []string
  256. testOrder := componentsToNames(components)
  257. for _, name := range testOrder {
  258. if _, ok := o.namesToIndex[name]; !ok {
  259. unknownComponents = append(unknownComponents, name)
  260. break
  261. }
  262. }
  263. // If the slice contains some unknown components then it is not possible to
  264. // sort them into an order that matches the runtime while also preserving the
  265. // order expected from the test, so in that case don't sort just check that
  266. // the order of the known mutators does match.
  267. if len(unknownComponents) > 0 {
  268. // Check order.
  269. o.checkTestOrder(testOrder, unknownComponents)
  270. } else {
  271. // Sort the components.
  272. sort.Slice(components, func(i, j int) bool {
  273. n1 := components[i].componentName()
  274. n2 := components[j].componentName()
  275. return o.less(n1, n2)
  276. })
  277. }
  278. }
  279. // checkTestOrder checks that the supplied testOrder matches the one defined by this object,
  280. // panicking if it does not.
  281. func (o *registeredComponentOrder) checkTestOrder(testOrder []string, unknownComponents []string) {
  282. lastMatchingTest := -1
  283. matchCount := 0
  284. // Take a copy of the runtime order as it is modified during the comparison.
  285. runtimeOrder := append([]string(nil), o.namesInOrder...)
  286. componentType := o.componentType
  287. for i, j := 0, 0; i < len(testOrder) && j < len(runtimeOrder); {
  288. test := testOrder[i]
  289. runtime := runtimeOrder[j]
  290. if test == runtime {
  291. testOrder[i] = test + fmt.Sprintf(" <-- matched with runtime %s %d", componentType, j)
  292. runtimeOrder[j] = runtime + fmt.Sprintf(" <-- matched with test %s %d", componentType, i)
  293. lastMatchingTest = i
  294. i += 1
  295. j += 1
  296. matchCount += 1
  297. } else if _, ok := o.namesToIndex[test]; !ok {
  298. // The test component is not registered globally so assume it is the correct place, treat it
  299. // as having matched and skip it.
  300. i += 1
  301. matchCount += 1
  302. } else {
  303. // Assume that the test list is in the same order as the runtime list but the runtime list
  304. // contains some components that are not present in the tests. So, skip the runtime component
  305. // to try and find the next one that matches the current test component.
  306. j += 1
  307. }
  308. }
  309. // If every item in the test order was either test specific or matched one in the runtime then
  310. // it is in the correct order. Otherwise, it was not so fail.
  311. if matchCount != len(testOrder) {
  312. // The test component names were not all matched with a runtime component name so there must
  313. // either be a component present in the test that is not present in the runtime or they must be
  314. // in the wrong order.
  315. testOrder[lastMatchingTest+1] = testOrder[lastMatchingTest+1] + " <--- unmatched"
  316. panic(fmt.Errorf("the tests uses test specific components %q and so cannot be automatically sorted."+
  317. " Unfortunately it uses %s components in the wrong order.\n"+
  318. "test order:\n %s\n"+
  319. "runtime order\n %s\n",
  320. SortedUniqueStrings(unknownComponents),
  321. componentType,
  322. strings.Join(testOrder, "\n "),
  323. strings.Join(runtimeOrder, "\n ")))
  324. }
  325. }
  326. // registrationSorter encapsulates the information needed to ensure that the test mutators are
  327. // registered, and thereby executed, in the same order as they are at runtime.
  328. //
  329. // It MUST be populated lazily AFTER all package initialization has been done otherwise it will
  330. // only define the order for a subset of all the registered build components that are available for
  331. // the packages being tested.
  332. //
  333. // e.g if this is initialized during say the cc package initialization then any tests run in the
  334. // java package will not sort build components registered by the java package's init() functions.
  335. type registrationSorter struct {
  336. // Used to ensure that this is only created once.
  337. once sync.Once
  338. // The order of pre-singletons
  339. preSingletonOrder registeredComponentOrder
  340. // The order of mutators
  341. mutatorOrder registeredComponentOrder
  342. // The order of singletons
  343. singletonOrder registeredComponentOrder
  344. }
  345. // populate initializes this structure from globally registered build components.
  346. //
  347. // Only the first call has any effect.
  348. func (s *registrationSorter) populate() {
  349. s.once.Do(func() {
  350. // Create an ordering from the globally registered pre-singletons.
  351. s.preSingletonOrder = registeredComponentOrderFromExistingOrder("pre-singleton", preSingletons)
  352. // Created an ordering from the globally registered mutators.
  353. globallyRegisteredMutators := collateGloballyRegisteredMutators()
  354. s.mutatorOrder = registeredComponentOrderFromExistingOrder("mutator", globallyRegisteredMutators)
  355. // Create an ordering from the globally registered singletons.
  356. globallyRegisteredSingletons := collateGloballyRegisteredSingletons()
  357. s.singletonOrder = registeredComponentOrderFromExistingOrder("singleton", globallyRegisteredSingletons)
  358. })
  359. }
  360. // Provides support for enforcing the same order in which build components are registered globally
  361. // to the order in which they are registered during tests.
  362. //
  363. // MUST only be accessed via the globallyRegisteredComponentsOrder func.
  364. var globalRegistrationSorter registrationSorter
  365. // globallyRegisteredComponentsOrder returns the globalRegistrationSorter after ensuring it is
  366. // correctly populated.
  367. func globallyRegisteredComponentsOrder() *registrationSorter {
  368. globalRegistrationSorter.populate()
  369. return &globalRegistrationSorter
  370. }
  371. func (ctx *TestContext) Register() {
  372. globalOrder := globallyRegisteredComponentsOrder()
  373. // Ensure that the pre-singletons used in the test are in the same order as they are used at
  374. // runtime.
  375. globalOrder.preSingletonOrder.enforceOrdering(ctx.preSingletons)
  376. ctx.preSingletons.registerAll(ctx.Context)
  377. mutators := collateRegisteredMutators(ctx.preArch, ctx.preDeps, ctx.postDeps, ctx.finalDeps)
  378. // Ensure that the mutators used in the test are in the same order as they are used at runtime.
  379. globalOrder.mutatorOrder.enforceOrdering(mutators)
  380. mutators.registerAll(ctx.Context)
  381. // Ensure that the singletons used in the test are in the same order as they are used at runtime.
  382. globalOrder.singletonOrder.enforceOrdering(ctx.singletons)
  383. ctx.singletons.registerAll(ctx.Context)
  384. // Save the sorted components order away to make them easy to access while debugging.
  385. ctx.preSingletonOrder = componentsToNames(preSingletons)
  386. ctx.mutatorOrder = componentsToNames(mutators)
  387. ctx.singletonOrder = componentsToNames(singletons)
  388. }
  389. // RegisterForBazelConversion prepares a test context for bp2build conversion.
  390. func (ctx *TestContext) RegisterForBazelConversion() {
  391. ctx.SetRunningAsBp2build()
  392. RegisterMutatorsForBazelConversion(ctx.Context, ctx.bp2buildPreArch)
  393. }
  394. func (ctx *TestContext) ParseFileList(rootDir string, filePaths []string) (deps []string, errs []error) {
  395. // This function adapts the old style ParseFileList calls that are spread throughout the tests
  396. // to the new style that takes a config.
  397. return ctx.Context.ParseFileList(rootDir, filePaths, ctx.config)
  398. }
  399. func (ctx *TestContext) ParseBlueprintsFiles(rootDir string) (deps []string, errs []error) {
  400. // This function adapts the old style ParseBlueprintsFiles calls that are spread throughout the
  401. // tests to the new style that takes a config.
  402. return ctx.Context.ParseBlueprintsFiles(rootDir, ctx.config)
  403. }
  404. func (ctx *TestContext) RegisterModuleType(name string, factory ModuleFactory) {
  405. ctx.Context.RegisterModuleType(name, ModuleFactoryAdaptor(factory))
  406. }
  407. func (ctx *TestContext) RegisterSingletonModuleType(name string, factory SingletonModuleFactory) {
  408. s, m := SingletonModuleFactoryAdaptor(name, factory)
  409. ctx.RegisterSingletonType(name, s)
  410. ctx.RegisterModuleType(name, m)
  411. }
  412. func (ctx *TestContext) RegisterSingletonType(name string, factory SingletonFactory) {
  413. ctx.singletons = append(ctx.singletons, newSingleton(name, factory))
  414. }
  415. func (ctx *TestContext) RegisterPreSingletonType(name string, factory SingletonFactory) {
  416. ctx.preSingletons = append(ctx.preSingletons, newPreSingleton(name, factory))
  417. }
  418. // ModuleVariantForTests selects a specific variant of the module with the given
  419. // name by matching the variations map against the variations of each module
  420. // variant. A module variant matches the map if every variation that exists in
  421. // both have the same value. Both the module and the map are allowed to have
  422. // extra variations that the other doesn't have. Panics if not exactly one
  423. // module variant matches.
  424. func (ctx *TestContext) ModuleVariantForTests(name string, matchVariations map[string]string) TestingModule {
  425. modules := []Module{}
  426. ctx.VisitAllModules(func(m blueprint.Module) {
  427. if ctx.ModuleName(m) == name {
  428. am := m.(Module)
  429. amMut := am.base().commonProperties.DebugMutators
  430. amVar := am.base().commonProperties.DebugVariations
  431. matched := true
  432. for i, mut := range amMut {
  433. if wantedVar, found := matchVariations[mut]; found && amVar[i] != wantedVar {
  434. matched = false
  435. break
  436. }
  437. }
  438. if matched {
  439. modules = append(modules, am)
  440. }
  441. }
  442. })
  443. if len(modules) == 0 {
  444. // Show all the modules or module variants that do exist.
  445. var allModuleNames []string
  446. var allVariants []string
  447. ctx.VisitAllModules(func(m blueprint.Module) {
  448. allModuleNames = append(allModuleNames, ctx.ModuleName(m))
  449. if ctx.ModuleName(m) == name {
  450. allVariants = append(allVariants, m.(Module).String())
  451. }
  452. })
  453. if len(allVariants) == 0 {
  454. panic(fmt.Errorf("failed to find module %q. All modules:\n %s",
  455. name, strings.Join(SortedUniqueStrings(allModuleNames), "\n ")))
  456. } else {
  457. sort.Strings(allVariants)
  458. panic(fmt.Errorf("failed to find module %q matching %v. All variants:\n %s",
  459. name, matchVariations, strings.Join(allVariants, "\n ")))
  460. }
  461. }
  462. if len(modules) > 1 {
  463. moduleStrings := []string{}
  464. for _, m := range modules {
  465. moduleStrings = append(moduleStrings, m.String())
  466. }
  467. sort.Strings(moduleStrings)
  468. panic(fmt.Errorf("module %q has more than one variant that match %v:\n %s",
  469. name, matchVariations, strings.Join(moduleStrings, "\n ")))
  470. }
  471. return newTestingModule(ctx.config, modules[0])
  472. }
  473. func (ctx *TestContext) ModuleForTests(name, variant string) TestingModule {
  474. var module Module
  475. ctx.VisitAllModules(func(m blueprint.Module) {
  476. if ctx.ModuleName(m) == name && ctx.ModuleSubDir(m) == variant {
  477. module = m.(Module)
  478. }
  479. })
  480. if module == nil {
  481. // find all the modules that do exist
  482. var allModuleNames []string
  483. var allVariants []string
  484. ctx.VisitAllModules(func(m blueprint.Module) {
  485. allModuleNames = append(allModuleNames, ctx.ModuleName(m))
  486. if ctx.ModuleName(m) == name {
  487. allVariants = append(allVariants, ctx.ModuleSubDir(m))
  488. }
  489. })
  490. sort.Strings(allVariants)
  491. if len(allVariants) == 0 {
  492. panic(fmt.Errorf("failed to find module %q. All modules:\n %s",
  493. name, strings.Join(SortedUniqueStrings(allModuleNames), "\n ")))
  494. } else {
  495. panic(fmt.Errorf("failed to find module %q variant %q. All variants:\n %s",
  496. name, variant, strings.Join(allVariants, "\n ")))
  497. }
  498. }
  499. return newTestingModule(ctx.config, module)
  500. }
  501. func (ctx *TestContext) ModuleVariantsForTests(name string) []string {
  502. var variants []string
  503. ctx.VisitAllModules(func(m blueprint.Module) {
  504. if ctx.ModuleName(m) == name {
  505. variants = append(variants, ctx.ModuleSubDir(m))
  506. }
  507. })
  508. return variants
  509. }
  510. // SingletonForTests returns a TestingSingleton for the singleton registered with the given name.
  511. func (ctx *TestContext) SingletonForTests(name string) TestingSingleton {
  512. allSingletonNames := []string{}
  513. for _, s := range ctx.Singletons() {
  514. n := ctx.SingletonName(s)
  515. if n == name {
  516. return TestingSingleton{
  517. baseTestingComponent: newBaseTestingComponent(ctx.config, s.(testBuildProvider)),
  518. singleton: s.(*singletonAdaptor).Singleton,
  519. }
  520. }
  521. allSingletonNames = append(allSingletonNames, n)
  522. }
  523. panic(fmt.Errorf("failed to find singleton %q."+
  524. "\nall singletons: %v", name, allSingletonNames))
  525. }
  526. type InstallMakeRule struct {
  527. Target string
  528. Deps []string
  529. OrderOnlyDeps []string
  530. }
  531. func parseMkRules(t *testing.T, config Config, nodes []mkparser.Node) []InstallMakeRule {
  532. var rules []InstallMakeRule
  533. for _, node := range nodes {
  534. if mkParserRule, ok := node.(*mkparser.Rule); ok {
  535. var rule InstallMakeRule
  536. if targets := mkParserRule.Target.Words(); len(targets) == 0 {
  537. t.Fatalf("no targets for rule %s", mkParserRule.Dump())
  538. } else if len(targets) > 1 {
  539. t.Fatalf("unsupported multiple targets for rule %s", mkParserRule.Dump())
  540. } else if !targets[0].Const() {
  541. t.Fatalf("unsupported non-const target for rule %s", mkParserRule.Dump())
  542. } else {
  543. rule.Target = normalizeStringRelativeToTop(config, targets[0].Value(nil))
  544. }
  545. prereqList := &rule.Deps
  546. for _, prereq := range mkParserRule.Prerequisites.Words() {
  547. if !prereq.Const() {
  548. t.Fatalf("unsupported non-const prerequisite for rule %s", mkParserRule.Dump())
  549. }
  550. if prereq.Value(nil) == "|" {
  551. prereqList = &rule.OrderOnlyDeps
  552. continue
  553. }
  554. *prereqList = append(*prereqList, normalizeStringRelativeToTop(config, prereq.Value(nil)))
  555. }
  556. rules = append(rules, rule)
  557. }
  558. }
  559. return rules
  560. }
  561. func (ctx *TestContext) InstallMakeRulesForTesting(t *testing.T) []InstallMakeRule {
  562. installs := ctx.SingletonForTests("makevars").Singleton().(*makeVarsSingleton).installsForTesting
  563. buf := bytes.NewBuffer(append([]byte(nil), installs...))
  564. parser := mkparser.NewParser("makevars", buf)
  565. nodes, errs := parser.Parse()
  566. if len(errs) > 0 {
  567. t.Fatalf("error parsing install rules: %s", errs[0])
  568. }
  569. return parseMkRules(t, ctx.config, nodes)
  570. }
  571. func (ctx *TestContext) Config() Config {
  572. return ctx.config
  573. }
  574. type testBuildProvider interface {
  575. BuildParamsForTests() []BuildParams
  576. RuleParamsForTests() map[blueprint.Rule]blueprint.RuleParams
  577. }
  578. type TestingBuildParams struct {
  579. BuildParams
  580. RuleParams blueprint.RuleParams
  581. config Config
  582. }
  583. // RelativeToTop creates a new instance of this which has had any usages of the current test's
  584. // temporary and test specific build directory replaced with a path relative to the notional top.
  585. //
  586. // The parts of this structure which are changed are:
  587. // * BuildParams
  588. // - Args
  589. // - All Path, Paths, WritablePath and WritablePaths fields.
  590. //
  591. // * RuleParams
  592. // - Command
  593. // - Depfile
  594. // - Rspfile
  595. // - RspfileContent
  596. // - SymlinkOutputs
  597. // - CommandDeps
  598. // - CommandOrderOnly
  599. //
  600. // See PathRelativeToTop for more details.
  601. //
  602. // deprecated: this is no longer needed as TestingBuildParams are created in this form.
  603. func (p TestingBuildParams) RelativeToTop() TestingBuildParams {
  604. // If this is not a valid params then just return it back. That will make it easy to use with the
  605. // Maybe...() methods.
  606. if p.Rule == nil {
  607. return p
  608. }
  609. if p.config.config == nil {
  610. return p
  611. }
  612. // Take a copy of the build params and replace any args that contains test specific temporary
  613. // paths with paths relative to the top.
  614. bparams := p.BuildParams
  615. bparams.Depfile = normalizeWritablePathRelativeToTop(bparams.Depfile)
  616. bparams.Output = normalizeWritablePathRelativeToTop(bparams.Output)
  617. bparams.Outputs = bparams.Outputs.RelativeToTop()
  618. bparams.SymlinkOutput = normalizeWritablePathRelativeToTop(bparams.SymlinkOutput)
  619. bparams.SymlinkOutputs = bparams.SymlinkOutputs.RelativeToTop()
  620. bparams.ImplicitOutput = normalizeWritablePathRelativeToTop(bparams.ImplicitOutput)
  621. bparams.ImplicitOutputs = bparams.ImplicitOutputs.RelativeToTop()
  622. bparams.Input = normalizePathRelativeToTop(bparams.Input)
  623. bparams.Inputs = bparams.Inputs.RelativeToTop()
  624. bparams.Implicit = normalizePathRelativeToTop(bparams.Implicit)
  625. bparams.Implicits = bparams.Implicits.RelativeToTop()
  626. bparams.OrderOnly = bparams.OrderOnly.RelativeToTop()
  627. bparams.Validation = normalizePathRelativeToTop(bparams.Validation)
  628. bparams.Validations = bparams.Validations.RelativeToTop()
  629. bparams.Args = normalizeStringMapRelativeToTop(p.config, bparams.Args)
  630. // Ditto for any fields in the RuleParams.
  631. rparams := p.RuleParams
  632. rparams.Command = normalizeStringRelativeToTop(p.config, rparams.Command)
  633. rparams.Depfile = normalizeStringRelativeToTop(p.config, rparams.Depfile)
  634. rparams.Rspfile = normalizeStringRelativeToTop(p.config, rparams.Rspfile)
  635. rparams.RspfileContent = normalizeStringRelativeToTop(p.config, rparams.RspfileContent)
  636. rparams.SymlinkOutputs = normalizeStringArrayRelativeToTop(p.config, rparams.SymlinkOutputs)
  637. rparams.CommandDeps = normalizeStringArrayRelativeToTop(p.config, rparams.CommandDeps)
  638. rparams.CommandOrderOnly = normalizeStringArrayRelativeToTop(p.config, rparams.CommandOrderOnly)
  639. return TestingBuildParams{
  640. BuildParams: bparams,
  641. RuleParams: rparams,
  642. }
  643. }
  644. func normalizeWritablePathRelativeToTop(path WritablePath) WritablePath {
  645. if path == nil {
  646. return nil
  647. }
  648. return path.RelativeToTop().(WritablePath)
  649. }
  650. func normalizePathRelativeToTop(path Path) Path {
  651. if path == nil {
  652. return nil
  653. }
  654. return path.RelativeToTop()
  655. }
  656. // baseTestingComponent provides functionality common to both TestingModule and TestingSingleton.
  657. type baseTestingComponent struct {
  658. config Config
  659. provider testBuildProvider
  660. }
  661. func newBaseTestingComponent(config Config, provider testBuildProvider) baseTestingComponent {
  662. return baseTestingComponent{config, provider}
  663. }
  664. // A function that will normalize a string containing paths, e.g. ninja command, by replacing
  665. // any references to the test specific temporary build directory that changes with each run to a
  666. // fixed path relative to a notional top directory.
  667. //
  668. // This is similar to StringPathRelativeToTop except that assumes the string is a single path
  669. // containing at most one instance of the temporary build directory at the start of the path while
  670. // this assumes that there can be any number at any position.
  671. func normalizeStringRelativeToTop(config Config, s string) string {
  672. // The soongOutDir usually looks something like: /tmp/testFoo2345/001
  673. //
  674. // Replace any usage of the soongOutDir with out/soong, e.g. replace "/tmp/testFoo2345/001" with
  675. // "out/soong".
  676. outSoongDir := filepath.Clean(config.soongOutDir)
  677. re := regexp.MustCompile(`\Q` + outSoongDir + `\E\b`)
  678. s = re.ReplaceAllString(s, "out/soong")
  679. // Replace any usage of the soongOutDir/.. with out, e.g. replace "/tmp/testFoo2345" with
  680. // "out". This must come after the previous replacement otherwise this would replace
  681. // "/tmp/testFoo2345/001" with "out/001" instead of "out/soong".
  682. outDir := filepath.Dir(outSoongDir)
  683. re = regexp.MustCompile(`\Q` + outDir + `\E\b`)
  684. s = re.ReplaceAllString(s, "out")
  685. return s
  686. }
  687. // normalizeStringArrayRelativeToTop creates a new slice constructed by applying
  688. // normalizeStringRelativeToTop to each item in the slice.
  689. func normalizeStringArrayRelativeToTop(config Config, slice []string) []string {
  690. newSlice := make([]string, len(slice))
  691. for i, s := range slice {
  692. newSlice[i] = normalizeStringRelativeToTop(config, s)
  693. }
  694. return newSlice
  695. }
  696. // normalizeStringMapRelativeToTop creates a new map constructed by applying
  697. // normalizeStringRelativeToTop to each value in the map.
  698. func normalizeStringMapRelativeToTop(config Config, m map[string]string) map[string]string {
  699. newMap := map[string]string{}
  700. for k, v := range m {
  701. newMap[k] = normalizeStringRelativeToTop(config, v)
  702. }
  703. return newMap
  704. }
  705. func (b baseTestingComponent) newTestingBuildParams(bparams BuildParams) TestingBuildParams {
  706. return TestingBuildParams{
  707. config: b.config,
  708. BuildParams: bparams,
  709. RuleParams: b.provider.RuleParamsForTests()[bparams.Rule],
  710. }.RelativeToTop()
  711. }
  712. func (b baseTestingComponent) maybeBuildParamsFromRule(rule string) (TestingBuildParams, []string) {
  713. var searchedRules []string
  714. buildParams := b.provider.BuildParamsForTests()
  715. for _, p := range buildParams {
  716. ruleAsString := p.Rule.String()
  717. searchedRules = append(searchedRules, ruleAsString)
  718. if strings.Contains(ruleAsString, rule) {
  719. return b.newTestingBuildParams(p), searchedRules
  720. }
  721. }
  722. return TestingBuildParams{}, searchedRules
  723. }
  724. func (b baseTestingComponent) buildParamsFromRule(rule string) TestingBuildParams {
  725. p, searchRules := b.maybeBuildParamsFromRule(rule)
  726. if p.Rule == nil {
  727. panic(fmt.Errorf("couldn't find rule %q.\nall rules:\n%s", rule, strings.Join(searchRules, "\n")))
  728. }
  729. return p
  730. }
  731. func (b baseTestingComponent) maybeBuildParamsFromDescription(desc string) (TestingBuildParams, []string) {
  732. var searchedDescriptions []string
  733. for _, p := range b.provider.BuildParamsForTests() {
  734. searchedDescriptions = append(searchedDescriptions, p.Description)
  735. if strings.Contains(p.Description, desc) {
  736. return b.newTestingBuildParams(p), searchedDescriptions
  737. }
  738. }
  739. return TestingBuildParams{}, searchedDescriptions
  740. }
  741. func (b baseTestingComponent) buildParamsFromDescription(desc string) TestingBuildParams {
  742. p, searchedDescriptions := b.maybeBuildParamsFromDescription(desc)
  743. if p.Rule == nil {
  744. panic(fmt.Errorf("couldn't find description %q\nall descriptions:\n%s", desc, strings.Join(searchedDescriptions, "\n")))
  745. }
  746. return p
  747. }
  748. func (b baseTestingComponent) maybeBuildParamsFromOutput(file string) (TestingBuildParams, []string) {
  749. searchedOutputs := WritablePaths(nil)
  750. for _, p := range b.provider.BuildParamsForTests() {
  751. outputs := append(WritablePaths(nil), p.Outputs...)
  752. outputs = append(outputs, p.ImplicitOutputs...)
  753. if p.Output != nil {
  754. outputs = append(outputs, p.Output)
  755. }
  756. for _, f := range outputs {
  757. if f.String() == file || f.Rel() == file || PathRelativeToTop(f) == file {
  758. return b.newTestingBuildParams(p), nil
  759. }
  760. searchedOutputs = append(searchedOutputs, f)
  761. }
  762. }
  763. formattedOutputs := []string{}
  764. for _, f := range searchedOutputs {
  765. formattedOutputs = append(formattedOutputs,
  766. fmt.Sprintf("%s (rel=%s)", PathRelativeToTop(f), f.Rel()))
  767. }
  768. return TestingBuildParams{}, formattedOutputs
  769. }
  770. func (b baseTestingComponent) buildParamsFromOutput(file string) TestingBuildParams {
  771. p, searchedOutputs := b.maybeBuildParamsFromOutput(file)
  772. if p.Rule == nil {
  773. panic(fmt.Errorf("couldn't find output %q.\nall outputs:\n %s\n",
  774. file, strings.Join(searchedOutputs, "\n ")))
  775. }
  776. return p
  777. }
  778. func (b baseTestingComponent) allOutputs() []string {
  779. var outputFullPaths []string
  780. for _, p := range b.provider.BuildParamsForTests() {
  781. outputs := append(WritablePaths(nil), p.Outputs...)
  782. outputs = append(outputs, p.ImplicitOutputs...)
  783. if p.Output != nil {
  784. outputs = append(outputs, p.Output)
  785. }
  786. outputFullPaths = append(outputFullPaths, outputs.Strings()...)
  787. }
  788. return outputFullPaths
  789. }
  790. // MaybeRule finds a call to ctx.Build with BuildParams.Rule set to a rule with the given name. Returns an empty
  791. // BuildParams if no rule is found.
  792. func (b baseTestingComponent) MaybeRule(rule string) TestingBuildParams {
  793. r, _ := b.maybeBuildParamsFromRule(rule)
  794. return r
  795. }
  796. // Rule finds a call to ctx.Build with BuildParams.Rule set to a rule with the given name. Panics if no rule is found.
  797. func (b baseTestingComponent) Rule(rule string) TestingBuildParams {
  798. return b.buildParamsFromRule(rule)
  799. }
  800. // MaybeDescription finds a call to ctx.Build with BuildParams.Description set to a the given string. Returns an empty
  801. // BuildParams if no rule is found.
  802. func (b baseTestingComponent) MaybeDescription(desc string) TestingBuildParams {
  803. p, _ := b.maybeBuildParamsFromDescription(desc)
  804. return p
  805. }
  806. // Description finds a call to ctx.Build with BuildParams.Description set to a the given string. Panics if no rule is
  807. // found.
  808. func (b baseTestingComponent) Description(desc string) TestingBuildParams {
  809. return b.buildParamsFromDescription(desc)
  810. }
  811. // MaybeOutput finds a call to ctx.Build with a BuildParams.Output or BuildParams.Outputs whose String() or Rel()
  812. // value matches the provided string. Returns an empty BuildParams if no rule is found.
  813. func (b baseTestingComponent) MaybeOutput(file string) TestingBuildParams {
  814. p, _ := b.maybeBuildParamsFromOutput(file)
  815. return p
  816. }
  817. // Output finds a call to ctx.Build with a BuildParams.Output or BuildParams.Outputs whose String() or Rel()
  818. // value matches the provided string. Panics if no rule is found.
  819. func (b baseTestingComponent) Output(file string) TestingBuildParams {
  820. return b.buildParamsFromOutput(file)
  821. }
  822. // AllOutputs returns all 'BuildParams.Output's and 'BuildParams.Outputs's in their full path string forms.
  823. func (b baseTestingComponent) AllOutputs() []string {
  824. return b.allOutputs()
  825. }
  826. // TestingModule is wrapper around an android.Module that provides methods to find information about individual
  827. // ctx.Build parameters for verification in tests.
  828. type TestingModule struct {
  829. baseTestingComponent
  830. module Module
  831. }
  832. func newTestingModule(config Config, module Module) TestingModule {
  833. return TestingModule{
  834. newBaseTestingComponent(config, module),
  835. module,
  836. }
  837. }
  838. // Module returns the Module wrapped by the TestingModule.
  839. func (m TestingModule) Module() Module {
  840. return m.module
  841. }
  842. // VariablesForTestsRelativeToTop returns a copy of the Module.VariablesForTests() with every value
  843. // having any temporary build dir usages replaced with paths relative to a notional top.
  844. func (m TestingModule) VariablesForTestsRelativeToTop() map[string]string {
  845. return normalizeStringMapRelativeToTop(m.config, m.module.VariablesForTests())
  846. }
  847. // OutputFiles calls OutputFileProducer.OutputFiles on the encapsulated module, exits the test
  848. // immediately if there is an error and otherwise returns the result of calling Paths.RelativeToTop
  849. // on the returned Paths.
  850. func (m TestingModule) OutputFiles(t *testing.T, tag string) Paths {
  851. producer, ok := m.module.(OutputFileProducer)
  852. if !ok {
  853. t.Fatalf("%q must implement OutputFileProducer\n", m.module.Name())
  854. }
  855. paths, err := producer.OutputFiles(tag)
  856. if err != nil {
  857. t.Fatal(err)
  858. }
  859. return paths.RelativeToTop()
  860. }
  861. // TestingSingleton is wrapper around an android.Singleton that provides methods to find information about individual
  862. // ctx.Build parameters for verification in tests.
  863. type TestingSingleton struct {
  864. baseTestingComponent
  865. singleton Singleton
  866. }
  867. // Singleton returns the Singleton wrapped by the TestingSingleton.
  868. func (s TestingSingleton) Singleton() Singleton {
  869. return s.singleton
  870. }
  871. func FailIfErrored(t *testing.T, errs []error) {
  872. t.Helper()
  873. if len(errs) > 0 {
  874. for _, err := range errs {
  875. t.Error(err)
  876. }
  877. t.FailNow()
  878. }
  879. }
  880. // Fail if no errors that matched the regular expression were found.
  881. //
  882. // Returns true if a matching error was found, false otherwise.
  883. func FailIfNoMatchingErrors(t *testing.T, pattern string, errs []error) bool {
  884. t.Helper()
  885. matcher, err := regexp.Compile(pattern)
  886. if err != nil {
  887. t.Fatalf("failed to compile regular expression %q because %s", pattern, err)
  888. }
  889. found := false
  890. for _, err := range errs {
  891. if matcher.FindStringIndex(err.Error()) != nil {
  892. found = true
  893. break
  894. }
  895. }
  896. if !found {
  897. t.Errorf("missing the expected error %q (checked %d error(s))", pattern, len(errs))
  898. for i, err := range errs {
  899. t.Errorf("errs[%d] = %q", i, err)
  900. }
  901. }
  902. return found
  903. }
  904. func CheckErrorsAgainstExpectations(t *testing.T, errs []error, expectedErrorPatterns []string) {
  905. t.Helper()
  906. if expectedErrorPatterns == nil {
  907. FailIfErrored(t, errs)
  908. } else {
  909. for _, expectedError := range expectedErrorPatterns {
  910. FailIfNoMatchingErrors(t, expectedError, errs)
  911. }
  912. if len(errs) > len(expectedErrorPatterns) {
  913. t.Errorf("additional errors found, expected %d, found %d",
  914. len(expectedErrorPatterns), len(errs))
  915. for i, expectedError := range expectedErrorPatterns {
  916. t.Errorf("expectedErrors[%d] = %s", i, expectedError)
  917. }
  918. for i, err := range errs {
  919. t.Errorf("errs[%d] = %s", i, err)
  920. }
  921. t.FailNow()
  922. }
  923. }
  924. }
  925. func SetKatiEnabledForTests(config Config) {
  926. config.katiEnabled = true
  927. }
  928. func AndroidMkEntriesForTest(t *testing.T, ctx *TestContext, mod blueprint.Module) []AndroidMkEntries {
  929. var p AndroidMkEntriesProvider
  930. var ok bool
  931. if p, ok = mod.(AndroidMkEntriesProvider); !ok {
  932. t.Errorf("module does not implement AndroidMkEntriesProvider: " + mod.Name())
  933. }
  934. entriesList := p.AndroidMkEntries()
  935. for i, _ := range entriesList {
  936. entriesList[i].fillInEntries(ctx, mod)
  937. }
  938. return entriesList
  939. }
  940. func AndroidMkDataForTest(t *testing.T, ctx *TestContext, mod blueprint.Module) AndroidMkData {
  941. var p AndroidMkDataProvider
  942. var ok bool
  943. if p, ok = mod.(AndroidMkDataProvider); !ok {
  944. t.Errorf("module does not implement AndroidMkDataProvider: " + mod.Name())
  945. }
  946. data := p.AndroidMk()
  947. data.fillInData(ctx, mod)
  948. return data
  949. }
  950. // Normalize the path for testing.
  951. //
  952. // If the path is relative to the build directory then return the relative path
  953. // to avoid tests having to deal with the dynamically generated build directory.
  954. //
  955. // Otherwise, return the supplied path as it is almost certainly a source path
  956. // that is relative to the root of the source tree.
  957. //
  958. // The build and source paths should be distinguishable based on their contents.
  959. //
  960. // deprecated: use PathRelativeToTop instead as it handles make install paths and differentiates
  961. // between output and source properly.
  962. func NormalizePathForTesting(path Path) string {
  963. if path == nil {
  964. return "<nil path>"
  965. }
  966. p := path.String()
  967. if w, ok := path.(WritablePath); ok {
  968. rel, err := filepath.Rel(w.getSoongOutDir(), p)
  969. if err != nil {
  970. panic(err)
  971. }
  972. return rel
  973. }
  974. return p
  975. }
  976. // NormalizePathsForTesting creates a slice of strings where each string is the result of applying
  977. // NormalizePathForTesting to the corresponding Path in the input slice.
  978. //
  979. // deprecated: use PathsRelativeToTop instead as it handles make install paths and differentiates
  980. // between output and source properly.
  981. func NormalizePathsForTesting(paths Paths) []string {
  982. var result []string
  983. for _, path := range paths {
  984. relative := NormalizePathForTesting(path)
  985. result = append(result, relative)
  986. }
  987. return result
  988. }
  989. // PathRelativeToTop returns a string representation of the path relative to a notional top
  990. // directory.
  991. //
  992. // It return "<nil path>" if the supplied path is nil, otherwise it returns the result of calling
  993. // Path.RelativeToTop to obtain a relative Path and then calling Path.String on that to get the
  994. // string representation.
  995. func PathRelativeToTop(path Path) string {
  996. if path == nil {
  997. return "<nil path>"
  998. }
  999. return path.RelativeToTop().String()
  1000. }
  1001. // PathsRelativeToTop creates a slice of strings where each string is the result of applying
  1002. // PathRelativeToTop to the corresponding Path in the input slice.
  1003. func PathsRelativeToTop(paths Paths) []string {
  1004. var result []string
  1005. for _, path := range paths {
  1006. relative := PathRelativeToTop(path)
  1007. result = append(result, relative)
  1008. }
  1009. return result
  1010. }
  1011. // StringPathRelativeToTop returns a string representation of the path relative to a notional top
  1012. // directory.
  1013. //
  1014. // See Path.RelativeToTop for more details as to what `relative to top` means.
  1015. //
  1016. // This is provided for processing paths that have already been converted into a string, e.g. paths
  1017. // in AndroidMkEntries structures. As a result it needs to be supplied the soong output dir against
  1018. // which it can try and relativize paths. PathRelativeToTop must be used for process Path objects.
  1019. func StringPathRelativeToTop(soongOutDir string, path string) string {
  1020. ensureTestOnly()
  1021. // A relative path must be a source path so leave it as it is.
  1022. if !filepath.IsAbs(path) {
  1023. return path
  1024. }
  1025. // Check to see if the path is relative to the soong out dir.
  1026. rel, isRel, err := maybeRelErr(soongOutDir, path)
  1027. if err != nil {
  1028. panic(err)
  1029. }
  1030. if isRel {
  1031. // The path is in the soong out dir so indicate that in the relative path.
  1032. return filepath.Join("out/soong", rel)
  1033. }
  1034. // Check to see if the path is relative to the top level out dir.
  1035. outDir := filepath.Dir(soongOutDir)
  1036. rel, isRel, err = maybeRelErr(outDir, path)
  1037. if err != nil {
  1038. panic(err)
  1039. }
  1040. if isRel {
  1041. // The path is in the out dir so indicate that in the relative path.
  1042. return filepath.Join("out", rel)
  1043. }
  1044. // This should never happen.
  1045. panic(fmt.Errorf("internal error: absolute path %s is not relative to the out dir %s", path, outDir))
  1046. }
  1047. // StringPathsRelativeToTop creates a slice of strings where each string is the result of applying
  1048. // StringPathRelativeToTop to the corresponding string path in the input slice.
  1049. //
  1050. // This is provided for processing paths that have already been converted into a string, e.g. paths
  1051. // in AndroidMkEntries structures. As a result it needs to be supplied the soong output dir against
  1052. // which it can try and relativize paths. PathsRelativeToTop must be used for process Paths objects.
  1053. func StringPathsRelativeToTop(soongOutDir string, paths []string) []string {
  1054. var result []string
  1055. for _, path := range paths {
  1056. relative := StringPathRelativeToTop(soongOutDir, path)
  1057. result = append(result, relative)
  1058. }
  1059. return result
  1060. }
  1061. // StringRelativeToTop will normalize a string containing paths, e.g. ninja command, by replacing
  1062. // any references to the test specific temporary build directory that changes with each run to a
  1063. // fixed path relative to a notional top directory.
  1064. //
  1065. // This is similar to StringPathRelativeToTop except that assumes the string is a single path
  1066. // containing at most one instance of the temporary build directory at the start of the path while
  1067. // this assumes that there can be any number at any position.
  1068. func StringRelativeToTop(config Config, command string) string {
  1069. return normalizeStringRelativeToTop(config, command)
  1070. }
  1071. // StringsRelativeToTop will return a new slice such that each item in the new slice is the result
  1072. // of calling StringRelativeToTop on the corresponding item in the input slice.
  1073. func StringsRelativeToTop(config Config, command []string) []string {
  1074. return normalizeStringArrayRelativeToTop(config, command)
  1075. }