1 /*
2  * Copyright (C) 2023 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 
17 package libcore.tools.analyzer.openjdk;
18 
19 import java.nio.file.Path;
20 import java.nio.file.Paths;
21 
22 /**
23  * Util functions for the host and runtime environment in AOSP.
24  */
25 public class AndroidHostEnvUtil {
getAndroidBuildTop()26     public static Path getAndroidBuildTop() {
27         return pathFromEnvOrThrow("ANDROID_BUILD_TOP");
28     }
29 
pathFromEnvOrThrow(String name)30     private static Path pathFromEnvOrThrow(String name) {
31         String envValue = getEnvOrThrow(name);
32         Path result = Paths.get(envValue);
33         if (!result.toFile().exists()) {
34             throw new IllegalStateException("For " + name + ", path doesn't exist: " + result);
35         }
36         return result;
37     }
38 
getEnvOrThrow(String name)39     private static String getEnvOrThrow(String name) {
40         String result = System.getenv(name);
41         if (result == null) {
42             throw new IllegalStateException("Environment variable undefined: " + name);
43         }
44         return result;
45     }
46 
parseInputClasspath(String classpath)47     static Path parseInputClasspath(String classpath) {
48         switch (classpath) {
49             case "oj":
50                 return getAndroidBuildTop().resolve(
51                         "out/soong/.intermediates/libcore/core-oj/android_common_apex31/"
52                                 + "javac/core-oj.jar");
53             case "8":
54                 return getAndroidBuildTop().resolve(
55                         "prebuilts/jdk/jdk8/linux-x86/jre/lib/rt.jar");
56             case "9":
57             case "11":
58             case "17":
59             case "21":
60                 return getAndroidBuildTop().resolve(
61                         "prebuilts/jdk/jdk" + classpath + "/linux-x86/jmods/java.base.jmod");
62             default:
63                 return Path.of(classpath);
64         }
65     }
66 }
67