satayev | be3f8ea | 2021-03-19 11:08:49 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Anton Hansson | 45012df | 2022-05-25 15:13:07 +0000 | [diff] [blame] | 17 | #define LOG_TAG "derive_classpath" |
| 18 | |
satayev | be3f8ea | 2021-03-19 11:08:49 +0000 | [diff] [blame] | 19 | #include "derive_classpath.h" |
Jiakai Zhang | cbe3e0e | 2025-01-15 06:19:03 -0800 | [diff] [blame] | 20 | |
| 21 | #include <ctype.h> |
Artur Satayev | 80516af | 2021-03-30 11:36:03 +0100 | [diff] [blame] | 22 | #include <glob.h> |
Jiakai Zhang | cbe3e0e | 2025-01-15 06:19:03 -0800 | [diff] [blame] | 23 | |
Artur Satayev | 80516af | 2021-03-30 11:36:03 +0100 | [diff] [blame] | 24 | #include <regex> |
| 25 | #include <sstream> |
Krzysztof KosiĆski | f4898d1 | 2024-03-15 17:08:30 +0000 | [diff] [blame] | 26 | #include <unordered_map> |
Artur Satayev | 80516af | 2021-03-30 11:36:03 +0100 | [diff] [blame] | 27 | |
Jiakai Zhang | cbe3e0e | 2025-01-15 06:19:03 -0800 | [diff] [blame] | 28 | #include <android-base/file.h> |
| 29 | #include <android-base/logging.h> |
Jiakai Zhang | cbe3e0e | 2025-01-15 06:19:03 -0800 | [diff] [blame] | 30 | #include <android-base/parseint.h> |
Jiakai Zhang | 12a3041 | 2025-02-19 14:57:42 +0000 | [diff] [blame] | 31 | #include <android-base/strings.h> |
Jiakai Zhang | cbe3e0e | 2025-01-15 06:19:03 -0800 | [diff] [blame] | 32 | |
satayev | 6dd0d70 | 2021-05-10 12:33:24 +0100 | [diff] [blame] | 33 | #include "packages/modules/common/proto/classpaths.pb.h" |
satayev | be3f8ea | 2021-03-19 11:08:49 +0000 | [diff] [blame] | 34 | |
Jiakai Zhang | 12a3041 | 2025-02-19 14:57:42 +0000 | [diff] [blame] | 35 | #ifdef SDKEXT_ANDROID |
| 36 | #include <android-modules-utils/sdk_level.h> |
| 37 | #include <android-modules-utils/unbounded_sdk_level.h> |
| 38 | #endif |
| 39 | |
satayev | be3f8ea | 2021-03-19 11:08:49 +0000 | [diff] [blame] | 40 | namespace android { |
| 41 | namespace derive_classpath { |
| 42 | |
Artur Satayev | 80516af | 2021-03-30 11:36:03 +0100 | [diff] [blame] | 43 | using Filepaths = std::vector<std::string>; |
| 44 | using Classpaths = std::unordered_map<Classpath, Filepaths>; |
satayev | be3f8ea | 2021-03-19 11:08:49 +0000 | [diff] [blame] | 45 | |
Samiul Islam | 5c7cfec | 2021-10-09 00:23:57 +0100 | [diff] [blame] | 46 | // Matches path of format: /apex/<module-name>@<version-digits-only>/* |
| 47 | static const std::regex kBindMountedApex("/apex/[^/]+@[0-9]+/"); |
| 48 | // Capture module name in following formats: |
| 49 | // - /apex/<module-name>/* |
| 50 | // - /apex/<module-name>@*/* |
| 51 | static const std::regex kApexPathRegex("(/apex/[^@/]+)(?:@[^@/]+)?/"); |
| 52 | |
| 53 | static const std::string kBootclasspathFragmentLocation = "/etc/classpaths/bootclasspath.pb"; |
| 54 | static const std::string kSystemserverclasspathFragmentLocation = |
| 55 | "/etc/classpaths/systemserverclasspath.pb"; |
Artur Satayev | 80516af | 2021-03-30 11:36:03 +0100 | [diff] [blame] | 56 | |
Jiakai Zhang | cbe3e0e | 2025-01-15 06:19:03 -0800 | [diff] [blame] | 57 | static int GetVersionInt(const std::string& version) { |
| 58 | int version_int = 0; |
| 59 | if (!android::base::ParseInt(version, &version_int, /*min=*/1, /*max=*/INT_MAX)) { |
| 60 | PLOG(FATAL) << "Failed to convert version \"" << version << "\" to int"; |
| 61 | } |
| 62 | return version_int; |
| 63 | } |
| 64 | |
| 65 | static bool IsCodename(const std::string& version) { |
| 66 | LOG_IF(FATAL, version.empty()) << "Empty version"; |
| 67 | return isupper(version[0]); |
| 68 | } |
| 69 | |
| 70 | static bool SdkLevelIsAtLeast(const Args& args, const std::string& version) { |
Jiakai Zhang | 12a3041 | 2025-02-19 14:57:42 +0000 | [diff] [blame] | 71 | #ifdef SDKEXT_ANDROID |
Jiakai Zhang | cbe3e0e | 2025-01-15 06:19:03 -0800 | [diff] [blame] | 72 | if (args.override_device_sdk_version == 0) { |
| 73 | // Most common case: no override. |
| 74 | return android::modules::sdklevel::unbounded::IsAtLeast(version.c_str()); |
| 75 | } |
Jiakai Zhang | 12a3041 | 2025-02-19 14:57:42 +0000 | [diff] [blame] | 76 | #endif |
Jiakai Zhang | cbe3e0e | 2025-01-15 06:19:03 -0800 | [diff] [blame] | 77 | |
| 78 | // Mirrors the logic in unbounded_sdk_level.h. |
| 79 | if (args.override_device_codename == "REL") { |
| 80 | if (IsCodename(version)) { |
| 81 | return false; |
| 82 | } |
| 83 | return args.override_device_sdk_version >= GetVersionInt(version); |
| 84 | } |
| 85 | if (IsCodename(version)) { |
| 86 | return args.override_device_known_codenames.contains(version); |
| 87 | } |
| 88 | return args.override_device_sdk_version >= GetVersionInt(version); |
| 89 | } |
| 90 | |
| 91 | static bool SdkLevelIsAtMost(const Args& args, const std::string& version) { |
Jiakai Zhang | 12a3041 | 2025-02-19 14:57:42 +0000 | [diff] [blame] | 92 | #ifdef SDKEXT_ANDROID |
Jiakai Zhang | cbe3e0e | 2025-01-15 06:19:03 -0800 | [diff] [blame] | 93 | if (args.override_device_sdk_version == 0) { |
| 94 | // Most common case: no override. |
| 95 | return android::modules::sdklevel::unbounded::IsAtMost(version.c_str()); |
| 96 | } |
Jiakai Zhang | 12a3041 | 2025-02-19 14:57:42 +0000 | [diff] [blame] | 97 | #endif |
Jiakai Zhang | cbe3e0e | 2025-01-15 06:19:03 -0800 | [diff] [blame] | 98 | |
| 99 | // Mirrors the logic in unbounded_sdk_level.h. |
| 100 | if (args.override_device_codename == "REL") { |
| 101 | if (IsCodename(version)) { |
| 102 | return true; |
| 103 | } |
| 104 | return args.override_device_sdk_version <= GetVersionInt(version); |
| 105 | } |
| 106 | if (IsCodename(version)) { |
| 107 | return !args.override_device_known_codenames.contains(version) || |
| 108 | args.override_device_codename == version; |
| 109 | } |
| 110 | return args.override_device_sdk_version < GetVersionInt(version); |
| 111 | } |
| 112 | |
Victor Hsieh | e7d34bf | 2021-10-06 08:38:52 -0700 | [diff] [blame] | 113 | std::vector<std::string> getBootclasspathFragmentGlobPatterns(const Args& args) { |
Samiul Islam | 5c7cfec | 2021-10-09 00:23:57 +0100 | [diff] [blame] | 114 | // Scan only specific directory for fragments if scan_dir is specified |
| 115 | if (!args.scan_dirs.empty()) { |
| 116 | std::vector<std::string> patterns; |
| 117 | for (const auto& scan_dir : args.scan_dirs) { |
| 118 | patterns.push_back(scan_dir + kBootclasspathFragmentLocation); |
| 119 | } |
| 120 | return patterns; |
| 121 | } |
| 122 | |
Victor Hsieh | e7d34bf | 2021-10-06 08:38:52 -0700 | [diff] [blame] | 123 | // Defines the order of individual fragments to be merged for BOOTCLASSPATH: |
| 124 | // 1. Jars in ART module always come first; |
| 125 | // 2. Jars defined as part of /system/etc/classpaths; |
| 126 | // 3. Jars defined in all non-ART apexes that expose /apex/*/etc/classpaths fragments. |
| 127 | // |
| 128 | // Notes: |
| 129 | // - Relative order in the individual fragment files is not changed when merging. |
| 130 | // - If a fragment file is matched by multiple globs, the first one is used; i.e. ART module |
| 131 | // fragment is only parsed once, even if there is a "/apex/*/" pattern later. |
| 132 | // - If there are multiple files matched for a glob pattern with wildcards, the results are sorted |
| 133 | // by pathname (default glob behaviour); i.e. all fragment files are sorted within a single |
| 134 | // "pattern block". |
| 135 | std::vector<std::string> patterns = { |
| 136 | // ART module is a special case and must come first before any other classpath entries. |
Samiul Islam | 5c7cfec | 2021-10-09 00:23:57 +0100 | [diff] [blame] | 137 | "/apex/com.android.art" + kBootclasspathFragmentLocation, |
Victor Hsieh | e7d34bf | 2021-10-06 08:38:52 -0700 | [diff] [blame] | 138 | }; |
| 139 | if (args.system_bootclasspath_fragment.empty()) { |
Samiul Islam | 5c7cfec | 2021-10-09 00:23:57 +0100 | [diff] [blame] | 140 | patterns.emplace_back("/system" + kBootclasspathFragmentLocation); |
Victor Hsieh | e7d34bf | 2021-10-06 08:38:52 -0700 | [diff] [blame] | 141 | } else { |
| 142 | // TODO: Avoid applying glob(3) expansion later to this path. Although the caller should not |
| 143 | // provide a path that contains '*', it can technically happen. Instead of checking the string |
| 144 | // format, we should just avoid the glob(3) for this string. |
| 145 | patterns.emplace_back(args.system_bootclasspath_fragment); |
| 146 | } |
Samiul Islam | 5c7cfec | 2021-10-09 00:23:57 +0100 | [diff] [blame] | 147 | patterns.emplace_back("/apex/*" + kBootclasspathFragmentLocation); |
Victor Hsieh | e7d34bf | 2021-10-06 08:38:52 -0700 | [diff] [blame] | 148 | return patterns; |
| 149 | } |
satayev | f1b3641 | 2021-05-20 21:31:21 +0100 | [diff] [blame] | 150 | |
Victor Hsieh | e7d34bf | 2021-10-06 08:38:52 -0700 | [diff] [blame] | 151 | std::vector<std::string> getSystemserverclasspathFragmentGlobPatterns(const Args& args) { |
Samiul Islam | 5c7cfec | 2021-10-09 00:23:57 +0100 | [diff] [blame] | 152 | // Scan only specific directory for fragments if scan_dir is specified |
| 153 | if (!args.scan_dirs.empty()) { |
| 154 | std::vector<std::string> patterns; |
| 155 | for (const auto& scan_dir : args.scan_dirs) { |
| 156 | patterns.push_back(scan_dir + kSystemserverclasspathFragmentLocation); |
| 157 | } |
| 158 | return patterns; |
| 159 | } |
| 160 | |
Victor Hsieh | e7d34bf | 2021-10-06 08:38:52 -0700 | [diff] [blame] | 161 | // Defines the order of individual fragments to be merged for SYSTEMSERVERCLASSPATH. |
| 162 | // |
| 163 | // ART system server jars are not special in this case, and are considered to be part of all the |
| 164 | // other apexes that may expose system server jars. |
| 165 | // |
| 166 | // All notes from getBootclasspathFragmentGlobPatterns apply here. |
| 167 | std::vector<std::string> patterns; |
| 168 | if (args.system_systemserverclasspath_fragment.empty()) { |
Samiul Islam | 5c7cfec | 2021-10-09 00:23:57 +0100 | [diff] [blame] | 169 | patterns.emplace_back("/system" + kSystemserverclasspathFragmentLocation); |
Victor Hsieh | e7d34bf | 2021-10-06 08:38:52 -0700 | [diff] [blame] | 170 | } else { |
| 171 | // TODO: Avoid applying glob(3) expansion later to this path. See above. |
| 172 | patterns.emplace_back(args.system_systemserverclasspath_fragment); |
| 173 | } |
Samiul Islam | 5c7cfec | 2021-10-09 00:23:57 +0100 | [diff] [blame] | 174 | patterns.emplace_back("/apex/*" + kSystemserverclasspathFragmentLocation); |
Victor Hsieh | e7d34bf | 2021-10-06 08:38:52 -0700 | [diff] [blame] | 175 | return patterns; |
Artur Satayev | 80516af | 2021-03-30 11:36:03 +0100 | [diff] [blame] | 176 | }; |
| 177 | |
| 178 | // Finds all classpath fragment files that match the glob pattern and appends them to `fragments`. |
| 179 | // |
| 180 | // If a newly found fragment is already present in `fragments`, it is skipped to avoid duplicates. |
| 181 | // Note that appended fragment files are sorted by pathnames, which is a default behaviour for |
| 182 | // glob(). |
Samiul Islam | 5c7cfec | 2021-10-09 00:23:57 +0100 | [diff] [blame] | 183 | // |
| 184 | // glob_pattern_prefix is only populated for unit tests so that we can search for pattern in a test |
| 185 | // directory instead of from root. |
| 186 | bool GlobClasspathFragments(Filepaths* fragments, const std::string& glob_pattern_prefix, |
| 187 | const std::string& pattern) { |
Artur Satayev | 80516af | 2021-03-30 11:36:03 +0100 | [diff] [blame] | 188 | glob_t glob_result; |
Samiul Islam | 5c7cfec | 2021-10-09 00:23:57 +0100 | [diff] [blame] | 189 | const int ret = glob((glob_pattern_prefix + pattern).c_str(), GLOB_MARK, nullptr, &glob_result); |
Artur Satayev | 80516af | 2021-03-30 11:36:03 +0100 | [diff] [blame] | 190 | if (ret != 0 && ret != GLOB_NOMATCH) { |
| 191 | globfree(&glob_result); |
Samiul Islam | 5c7cfec | 2021-10-09 00:23:57 +0100 | [diff] [blame] | 192 | LOG(ERROR) << "Failed to glob " << glob_pattern_prefix + pattern; |
satayev | be3f8ea | 2021-03-19 11:08:49 +0000 | [diff] [blame] | 193 | return false; |
| 194 | } |
| 195 | |
Artur Satayev | 80516af | 2021-03-30 11:36:03 +0100 | [diff] [blame] | 196 | for (size_t i = 0; i < glob_result.gl_pathc; i++) { |
| 197 | std::string path = glob_result.gl_pathv[i]; |
| 198 | // Skip <name>@<ver> dirs, as they are bind-mounted to <name> |
Samiul Islam | 5c7cfec | 2021-10-09 00:23:57 +0100 | [diff] [blame] | 199 | // Remove glob_pattern_prefix first since kBindMountedAPex has prefix requirement |
| 200 | if (std::regex_search(path.substr(glob_pattern_prefix.size()), kBindMountedApex)) { |
Artur Satayev | 80516af | 2021-03-30 11:36:03 +0100 | [diff] [blame] | 201 | continue; |
| 202 | } |
| 203 | // Make sure we don't push duplicate fragments from previously processed patterns |
| 204 | if (std::find(fragments->begin(), fragments->end(), path) == fragments->end()) { |
| 205 | fragments->push_back(path); |
| 206 | } |
| 207 | } |
| 208 | globfree(&glob_result); |
| 209 | return true; |
| 210 | } |
| 211 | |
| 212 | // Writes the contents of *CLASSPATH variables to /data in the format expected by `load_exports` |
| 213 | // action from init.rc. See platform/system/core/init/README.md. |
Alex Light | d7937d1 | 2021-04-26 16:49:57 -0700 | [diff] [blame] | 214 | bool WriteClasspathExports(Classpaths classpaths, std::string_view output_path) { |
satayev | acb41b9 | 2021-06-30 16:49:53 +0100 | [diff] [blame] | 215 | LOG(INFO) << "WriteClasspathExports " << output_path; |
Artur Satayev | 80516af | 2021-03-30 11:36:03 +0100 | [diff] [blame] | 216 | |
satayev | acb41b9 | 2021-06-30 16:49:53 +0100 | [diff] [blame] | 217 | std::stringstream out; |
Artur Satayev | 80516af | 2021-03-30 11:36:03 +0100 | [diff] [blame] | 218 | out << "export BOOTCLASSPATH " << android::base::Join(classpaths[BOOTCLASSPATH], ':') << '\n'; |
| 219 | out << "export DEX2OATBOOTCLASSPATH " |
| 220 | << android::base::Join(classpaths[DEX2OATBOOTCLASSPATH], ':') << '\n'; |
| 221 | out << "export SYSTEMSERVERCLASSPATH " |
| 222 | << android::base::Join(classpaths[SYSTEMSERVERCLASSPATH], ':') << '\n'; |
Jiakai Zhang | 535bd7f | 2021-10-29 19:59:18 +0000 | [diff] [blame] | 223 | out << "export STANDALONE_SYSTEMSERVER_JARS " |
| 224 | << android::base::Join(classpaths[STANDALONE_SYSTEMSERVER_JARS], ':') << '\n'; |
Artur Satayev | 80516af | 2021-03-30 11:36:03 +0100 | [diff] [blame] | 225 | |
satayev | acb41b9 | 2021-06-30 16:49:53 +0100 | [diff] [blame] | 226 | const std::string& content = out.str(); |
| 227 | LOG(INFO) << "WriteClasspathExports content\n" << content; |
| 228 | |
Alex Light | d7937d1 | 2021-04-26 16:49:57 -0700 | [diff] [blame] | 229 | const std::string path_str(output_path); |
satayev | 43e7dff | 2021-08-02 16:18:21 +0100 | [diff] [blame] | 230 | if (android::base::StartsWith(path_str, "/data/")) { |
| 231 | // When writing to /data, write to a temp file first to make sure the partition is not full. |
| 232 | const std::string temp_str(path_str + ".tmp"); |
| 233 | if (!android::base::WriteStringToFile(content, temp_str, /*follow_symlinks=*/true)) { |
| 234 | return false; |
| 235 | } |
| 236 | return rename(temp_str.c_str(), path_str.c_str()) == 0; |
| 237 | } else { |
| 238 | return android::base::WriteStringToFile(content, path_str, /*follow_symlinks=*/true); |
| 239 | } |
Artur Satayev | 80516af | 2021-03-30 11:36:03 +0100 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | bool ReadClasspathFragment(ExportedClasspathsJars* fragment, const std::string& filepath) { |
satayev | acb41b9 | 2021-06-30 16:49:53 +0100 | [diff] [blame] | 243 | LOG(INFO) << "ReadClasspathFragment " << filepath; |
Artur Satayev | 80516af | 2021-03-30 11:36:03 +0100 | [diff] [blame] | 244 | std::string contents; |
| 245 | if (!android::base::ReadFileToString(filepath, &contents)) { |
| 246 | PLOG(ERROR) << "Failed to read " << filepath; |
| 247 | return false; |
| 248 | } |
| 249 | if (!fragment->ParseFromString(contents)) { |
| 250 | LOG(ERROR) << "Failed to parse " << filepath; |
| 251 | return false; |
| 252 | } |
| 253 | return true; |
| 254 | } |
| 255 | |
satayev | d268742 | 2021-05-19 19:39:39 +0100 | [diff] [blame] | 256 | // Returns an allowed prefix for a jar filepaths declared in a given fragment. |
| 257 | // For a given apex fragment, it returns the apex path - "/apex/com.android.foo" - as an allowed |
| 258 | // prefix for jars. This can be used to enforce that an apex fragment only exports jars located in |
| 259 | // that apex. For system fragment, it returns an empty string to allow any jars to be exported by |
| 260 | // the platform. |
| 261 | std::string GetAllowedJarPathPrefix(const std::string& fragment_path) { |
| 262 | std::smatch match; |
| 263 | if (std::regex_search(fragment_path, match, kApexPathRegex)) { |
| 264 | return match[1]; |
| 265 | } |
| 266 | return ""; |
| 267 | } |
| 268 | |
satayev | f1b3641 | 2021-05-20 21:31:21 +0100 | [diff] [blame] | 269 | // Finds and parses all classpath fragments on device matching given glob patterns. |
Victor Hsieh | e7d34bf | 2021-10-06 08:38:52 -0700 | [diff] [blame] | 270 | bool ParseFragments(const Args& args, Classpaths& classpaths, bool boot_jars) { |
satayev | acb41b9 | 2021-06-30 16:49:53 +0100 | [diff] [blame] | 271 | LOG(INFO) << "ParseFragments for " << (boot_jars ? "bootclasspath" : "systemserverclasspath"); |
satayev | f1b3641 | 2021-05-20 21:31:21 +0100 | [diff] [blame] | 272 | |
Victor Hsieh | e7d34bf | 2021-10-06 08:38:52 -0700 | [diff] [blame] | 273 | auto glob_patterns = boot_jars ? getBootclasspathFragmentGlobPatterns(args) |
| 274 | : getSystemserverclasspathFragmentGlobPatterns(args); |
satayev | acb41b9 | 2021-06-30 16:49:53 +0100 | [diff] [blame] | 275 | |
| 276 | Filepaths fragments; |
satayev | f1b3641 | 2021-05-20 21:31:21 +0100 | [diff] [blame] | 277 | for (const auto& pattern : glob_patterns) { |
Samiul Islam | 5c7cfec | 2021-10-09 00:23:57 +0100 | [diff] [blame] | 278 | if (!GlobClasspathFragments(&fragments, args.glob_pattern_prefix, pattern)) { |
Artur Satayev | 80516af | 2021-03-30 11:36:03 +0100 | [diff] [blame] | 279 | return false; |
| 280 | } |
satayev | be3f8ea | 2021-03-19 11:08:49 +0000 | [diff] [blame] | 281 | } |
| 282 | |
satayev | d268742 | 2021-05-19 19:39:39 +0100 | [diff] [blame] | 283 | for (const auto& fragment_path : fragments) { |
Artur Satayev | 80516af | 2021-03-30 11:36:03 +0100 | [diff] [blame] | 284 | ExportedClasspathsJars exportedJars; |
satayev | d268742 | 2021-05-19 19:39:39 +0100 | [diff] [blame] | 285 | if (!ReadClasspathFragment(&exportedJars, fragment_path)) { |
Artur Satayev | 80516af | 2021-03-30 11:36:03 +0100 | [diff] [blame] | 286 | return false; |
| 287 | } |
satayev | d268742 | 2021-05-19 19:39:39 +0100 | [diff] [blame] | 288 | |
| 289 | // Either a path to the apex, or an empty string |
| 290 | const std::string& allowed_jar_prefix = GetAllowedJarPathPrefix(fragment_path); |
| 291 | |
Artur Satayev | 80516af | 2021-03-30 11:36:03 +0100 | [diff] [blame] | 292 | for (const Jar& jar : exportedJars.jars()) { |
satayev | d268742 | 2021-05-19 19:39:39 +0100 | [diff] [blame] | 293 | const std::string& jar_path = jar.path(); |
| 294 | CHECK(android::base::StartsWith(jar_path, allowed_jar_prefix)) |
| 295 | << fragment_path << " must not export a jar from outside of the apex: " << jar_path; |
satayev | 43eba8f | 2021-06-11 15:07:48 +0100 | [diff] [blame] | 296 | |
satayev | f1b3641 | 2021-05-20 21:31:21 +0100 | [diff] [blame] | 297 | const Classpath classpath = jar.classpath(); |
Jiakai Zhang | 535bd7f | 2021-10-29 19:59:18 +0000 | [diff] [blame] | 298 | CHECK(boot_jars ^ |
| 299 | (classpath == SYSTEMSERVERCLASSPATH || classpath == STANDALONE_SYSTEMSERVER_JARS)) |
satayev | f1b3641 | 2021-05-20 21:31:21 +0100 | [diff] [blame] | 300 | << fragment_path << " must not export a jar for " << Classpath_Name(classpath); |
satayev | 43eba8f | 2021-06-11 15:07:48 +0100 | [diff] [blame] | 301 | |
| 302 | if (!jar.min_sdk_version().empty()) { |
| 303 | const auto& min_sdk_version = jar.min_sdk_version(); |
Jiakai Zhang | cbe3e0e | 2025-01-15 06:19:03 -0800 | [diff] [blame] | 304 | if (!SdkLevelIsAtLeast(args, min_sdk_version)) { |
satayev | 43eba8f | 2021-06-11 15:07:48 +0100 | [diff] [blame] | 305 | LOG(INFO) << "not installing " << jar_path << " with min_sdk_version " << min_sdk_version; |
| 306 | continue; |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | if (!jar.max_sdk_version().empty()) { |
| 311 | const auto& max_sdk_version = jar.max_sdk_version(); |
Jiakai Zhang | cbe3e0e | 2025-01-15 06:19:03 -0800 | [diff] [blame] | 312 | if (!SdkLevelIsAtMost(args, max_sdk_version)) { |
satayev | 43eba8f | 2021-06-11 15:07:48 +0100 | [diff] [blame] | 313 | LOG(INFO) << "not installing " << jar_path << " with max_sdk_version " << max_sdk_version; |
| 314 | continue; |
| 315 | } |
| 316 | } |
| 317 | |
satayev | f1b3641 | 2021-05-20 21:31:21 +0100 | [diff] [blame] | 318 | classpaths[classpath].push_back(jar_path); |
Artur Satayev | 80516af | 2021-03-30 11:36:03 +0100 | [diff] [blame] | 319 | } |
| 320 | } |
satayev | f1b3641 | 2021-05-20 21:31:21 +0100 | [diff] [blame] | 321 | return true; |
| 322 | } |
Artur Satayev | 80516af | 2021-03-30 11:36:03 +0100 | [diff] [blame] | 323 | |
satayev | a10318b | 2021-06-14 12:40:31 +0100 | [diff] [blame] | 324 | // Generates /data/system/environ/classpath exports file by globing and merging individual |
| 325 | // classpaths.proto config fragments. The exports file is read by init.rc to setenv *CLASSPATH |
| 326 | // environ variables at runtime. |
Victor Hsieh | 86d87ad | 2021-10-01 16:33:17 -0700 | [diff] [blame] | 327 | bool GenerateClasspathExports(const Args& args) { |
Jiakai Zhang | 12a3041 | 2025-02-19 14:57:42 +0000 | [diff] [blame] | 328 | #ifdef SDKEXT_ANDROID |
satayev | f1b3641 | 2021-05-20 21:31:21 +0100 | [diff] [blame] | 329 | // Parse all known classpath fragments |
satayev | a10318b | 2021-06-14 12:40:31 +0100 | [diff] [blame] | 330 | CHECK(android::modules::sdklevel::IsAtLeastS()) |
| 331 | << "derive_classpath must only be run on Android 12 or above"; |
Jiakai Zhang | 12a3041 | 2025-02-19 14:57:42 +0000 | [diff] [blame] | 332 | #endif |
satayev | a10318b | 2021-06-14 12:40:31 +0100 | [diff] [blame] | 333 | |
satayev | f1b3641 | 2021-05-20 21:31:21 +0100 | [diff] [blame] | 334 | Classpaths classpaths; |
Victor Hsieh | e7d34bf | 2021-10-06 08:38:52 -0700 | [diff] [blame] | 335 | if (!ParseFragments(args, classpaths, /*boot_jars=*/true)) { |
satayev | f1b3641 | 2021-05-20 21:31:21 +0100 | [diff] [blame] | 336 | LOG(ERROR) << "Failed to parse BOOTCLASSPATH fragments"; |
| 337 | return false; |
| 338 | } |
Victor Hsieh | e7d34bf | 2021-10-06 08:38:52 -0700 | [diff] [blame] | 339 | if (!ParseFragments(args, classpaths, /*boot_jars=*/false)) { |
satayev | f1b3641 | 2021-05-20 21:31:21 +0100 | [diff] [blame] | 340 | LOG(ERROR) << "Failed to parse SYSTEMSERVERCLASSPATH fragments"; |
| 341 | return false; |
| 342 | } |
| 343 | |
| 344 | // Write export actions for init.rc |
Victor Hsieh | 86d87ad | 2021-10-01 16:33:17 -0700 | [diff] [blame] | 345 | if (!WriteClasspathExports(classpaths, args.output_path)) { |
| 346 | PLOG(ERROR) << "Failed to write " << args.output_path; |
Artur Satayev | 80516af | 2021-03-30 11:36:03 +0100 | [diff] [blame] | 347 | return false; |
| 348 | } |
satayev | be3f8ea | 2021-03-19 11:08:49 +0000 | [diff] [blame] | 349 | return true; |
| 350 | } |
| 351 | |
| 352 | } // namespace derive_classpath |
| 353 | } // namespace android |