test_config.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. mixedBuildDisabledModules: make(map[string]struct{}),
  57. mixedBuildEnabledModules: make(map[string]struct{}),
  58. }
  59. config.deviceConfig = &deviceConfig{
  60. config: config,
  61. }
  62. config.TestProductVariables = &config.productVariables
  63. config.mockFileSystem(bp, fs)
  64. determineBuildOS(config)
  65. return Config{config}
  66. }
  67. func modifyTestConfigToSupportArchMutator(testConfig Config) {
  68. config := testConfig.config
  69. config.Targets = map[OsType][]Target{
  70. Android: []Target{
  71. {Android, Arch{ArchType: Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridgeDisabled, "", "", false},
  72. {Android, Arch{ArchType: Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridgeDisabled, "", "", false},
  73. },
  74. config.BuildOS: []Target{
  75. {config.BuildOS, Arch{ArchType: X86_64}, NativeBridgeDisabled, "", "", false},
  76. {config.BuildOS, Arch{ArchType: X86}, NativeBridgeDisabled, "", "", false},
  77. },
  78. }
  79. if runtime.GOOS == "darwin" {
  80. config.Targets[config.BuildOS] = config.Targets[config.BuildOS][:1]
  81. }
  82. config.BuildOSTarget = config.Targets[config.BuildOS][0]
  83. config.BuildOSCommonTarget = getCommonTargets(config.Targets[config.BuildOS])[0]
  84. config.AndroidCommonTarget = getCommonTargets(config.Targets[Android])[0]
  85. config.AndroidFirstDeviceTarget = FirstTarget(config.Targets[Android], "lib64", "lib32")[0]
  86. config.TestProductVariables.DeviceArch = proptools.StringPtr("arm64")
  87. config.TestProductVariables.DeviceArchVariant = proptools.StringPtr("armv8-a")
  88. config.TestProductVariables.DeviceSecondaryArch = proptools.StringPtr("arm")
  89. config.TestProductVariables.DeviceSecondaryArchVariant = proptools.StringPtr("armv7-a-neon")
  90. }
  91. func modifyTestConfigForMusl(config Config) {
  92. delete(config.Targets, config.BuildOS)
  93. config.productVariables.HostMusl = boolPtr(true)
  94. determineBuildOS(config.config)
  95. config.Targets[config.BuildOS] = []Target{
  96. {config.BuildOS, Arch{ArchType: X86_64}, NativeBridgeDisabled, "", "", false},
  97. {config.BuildOS, Arch{ArchType: X86}, NativeBridgeDisabled, "", "", false},
  98. }
  99. config.BuildOSTarget = config.Targets[config.BuildOS][0]
  100. config.BuildOSCommonTarget = getCommonTargets(config.Targets[config.BuildOS])[0]
  101. }
  102. // TestArchConfig returns a Config object suitable for using for tests that
  103. // need to run the arch mutator.
  104. func TestArchConfig(buildDir string, env map[string]string, bp string, fs map[string][]byte) Config {
  105. testConfig := TestConfig(buildDir, env, bp, fs)
  106. modifyTestConfigToSupportArchMutator(testConfig)
  107. return testConfig
  108. }
  109. // CreateTestConfiguredJarList is a function to create ConfiguredJarList for tests.
  110. func CreateTestConfiguredJarList(list []string) ConfiguredJarList {
  111. // Create the ConfiguredJarList in as similar way as it is created at runtime by marshalling to
  112. // a json list of strings and then unmarshalling into a ConfiguredJarList instance.
  113. b, err := json.Marshal(list)
  114. if err != nil {
  115. panic(err)
  116. }
  117. var jarList ConfiguredJarList
  118. err = json.Unmarshal(b, &jarList)
  119. if err != nil {
  120. panic(err)
  121. }
  122. return jarList
  123. }