test_config.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Copyright 2022 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. "encoding/json"
  17. "os"
  18. "path/filepath"
  19. "runtime"
  20. "github.com/google/blueprint/proptools"
  21. )
  22. // TestConfig returns a Config object for testing.
  23. func TestConfig(buildDir string, env map[string]string, bp string, fs map[string][]byte) Config {
  24. envCopy := make(map[string]string)
  25. for k, v := range env {
  26. envCopy[k] = v
  27. }
  28. // Copy the real PATH value to the test environment, it's needed by
  29. // NonHermeticHostSystemTool() used in x86_darwin_host.go
  30. envCopy["PATH"] = os.Getenv("PATH")
  31. config := &config{
  32. productVariables: productVariables{
  33. DeviceName: stringPtr("test_device"),
  34. DeviceProduct: stringPtr("test_product"),
  35. Platform_sdk_version: intPtr(30),
  36. Platform_sdk_codename: stringPtr("S"),
  37. Platform_base_sdk_extension_version: intPtr(1),
  38. Platform_version_active_codenames: []string{"S", "Tiramisu"},
  39. DeviceSystemSdkVersions: []string{"14", "15"},
  40. Platform_systemsdk_versions: []string{"29", "30"},
  41. AAPTConfig: []string{"normal", "large", "xlarge", "hdpi", "xhdpi", "xxhdpi"},
  42. AAPTPreferredConfig: stringPtr("xhdpi"),
  43. AAPTCharacteristics: stringPtr("nosdcard"),
  44. AAPTPrebuiltDPI: []string{"xhdpi", "xxhdpi"},
  45. UncompressPrivAppDex: boolPtr(true),
  46. ShippingApiLevel: stringPtr("30"),
  47. },
  48. outDir: buildDir,
  49. soongOutDir: filepath.Join(buildDir, "soong"),
  50. captureBuild: true,
  51. env: envCopy,
  52. // Set testAllowNonExistentPaths so that test contexts don't need to specify every path
  53. // passed to PathForSource or PathForModuleSrc.
  54. TestAllowNonExistentPaths: true,
  55. BazelContext: noopBazelContext{},
  56. BuildMode: BazelProdMode,
  57. mixedBuildDisabledModules: make(map[string]struct{}),
  58. mixedBuildEnabledModules: make(map[string]struct{}),
  59. bazelForceEnabledModules: make(map[string]struct{}),
  60. }
  61. config.deviceConfig = &deviceConfig{
  62. config: config,
  63. }
  64. config.TestProductVariables = &config.productVariables
  65. config.mockFileSystem(bp, fs)
  66. determineBuildOS(config)
  67. return Config{config}
  68. }
  69. func modifyTestConfigToSupportArchMutator(testConfig Config) {
  70. config := testConfig.config
  71. config.Targets = map[OsType][]Target{
  72. Android: []Target{
  73. {Android, Arch{ArchType: Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridgeDisabled, "", "", false},
  74. {Android, Arch{ArchType: Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridgeDisabled, "", "", false},
  75. },
  76. config.BuildOS: []Target{
  77. {config.BuildOS, Arch{ArchType: X86_64}, NativeBridgeDisabled, "", "", false},
  78. {config.BuildOS, Arch{ArchType: X86}, NativeBridgeDisabled, "", "", false},
  79. },
  80. }
  81. // Make the CommonOS OsType available for all products.
  82. config.Targets[CommonOS] = []Target{commonTargetMap[CommonOS.Name]}
  83. if runtime.GOOS == "darwin" {
  84. config.Targets[config.BuildOS] = config.Targets[config.BuildOS][:1]
  85. }
  86. config.BuildOSTarget = config.Targets[config.BuildOS][0]
  87. config.BuildOSCommonTarget = getCommonTargets(config.Targets[config.BuildOS])[0]
  88. config.AndroidCommonTarget = getCommonTargets(config.Targets[Android])[0]
  89. config.AndroidFirstDeviceTarget = FirstTarget(config.Targets[Android], "lib64", "lib32")[0]
  90. config.TestProductVariables.DeviceArch = proptools.StringPtr("arm64")
  91. config.TestProductVariables.DeviceArchVariant = proptools.StringPtr("armv8-a")
  92. config.TestProductVariables.DeviceSecondaryArch = proptools.StringPtr("arm")
  93. config.TestProductVariables.DeviceSecondaryArchVariant = proptools.StringPtr("armv7-a-neon")
  94. }
  95. // ModifyTestConfigForMusl takes a Config returned by TestConfig and changes the host targets from glibc to musl.
  96. func ModifyTestConfigForMusl(config Config) {
  97. delete(config.Targets, config.BuildOS)
  98. config.productVariables.HostMusl = boolPtr(true)
  99. determineBuildOS(config.config)
  100. config.Targets[config.BuildOS] = []Target{
  101. {config.BuildOS, Arch{ArchType: X86_64}, NativeBridgeDisabled, "", "", false},
  102. {config.BuildOS, Arch{ArchType: X86}, NativeBridgeDisabled, "", "", false},
  103. }
  104. config.BuildOSTarget = config.Targets[config.BuildOS][0]
  105. config.BuildOSCommonTarget = getCommonTargets(config.Targets[config.BuildOS])[0]
  106. }
  107. func modifyTestConfigForMuslArm64HostCross(config Config) {
  108. config.Targets[LinuxMusl] = append(config.Targets[LinuxMusl],
  109. Target{config.BuildOS, Arch{ArchType: Arm64}, NativeBridgeDisabled, "", "", true})
  110. }
  111. // TestArchConfig returns a Config object suitable for using for tests that
  112. // need to run the arch mutator.
  113. func TestArchConfig(buildDir string, env map[string]string, bp string, fs map[string][]byte) Config {
  114. testConfig := TestConfig(buildDir, env, bp, fs)
  115. modifyTestConfigToSupportArchMutator(testConfig)
  116. return testConfig
  117. }
  118. // CreateTestConfiguredJarList is a function to create ConfiguredJarList for tests.
  119. func CreateTestConfiguredJarList(list []string) ConfiguredJarList {
  120. // Create the ConfiguredJarList in as similar way as it is created at runtime by marshalling to
  121. // a json list of strings and then unmarshalling into a ConfiguredJarList instance.
  122. b, err := json.Marshal(list)
  123. if err != nil {
  124. panic(err)
  125. }
  126. var jarList ConfiguredJarList
  127. err = json.Unmarshal(b, &jarList)
  128. if err != nil {
  129. panic(err)
  130. }
  131. return jarList
  132. }