1 /* 2 * Copyright (C) 2009 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 vogar; 18 19 import java.util.ArrayList; 20 import java.util.Arrays; 21 import java.util.List; 22 23 public enum ModeId { 24 /** (Target) dalvikvm with "normal" jars */ 25 DEVICE, 26 /** (Host) dalvikvm with -hostdex jars */ 27 HOST, 28 /** (Host) java */ 29 JVM, 30 /** (Target), execution as an Android app with Zygote */ 31 ACTIVITY, 32 /** (Target) app_process */ 33 APP_PROCESS; 34 35 /** 36 * $BOOTCLASSPATH for art+libcore only. 37 * (Intended for use with dalvikvm only.) 38 * See TARGET_TEST_CORE_JARS in android/art/build/Android.common_path.mk 39 */ 40 private static final String[] DEVICE_JARS = new String[] { 41 "core-oj", 42 "core-libart", 43 "core-icu4j", 44 "conscrypt", 45 "okhttp", 46 "bouncycastle", 47 "apache-xml", 48 }; 49 50 /** 51 * $BOOTCLASSPATH for art+libcore only (host version). 52 * (Intended for use with dalvikvm only.) 53 * See HOST_TEST_CORE_JARS in android/art/build/Android.common_path.mk 54 */ 55 private static final String[] HOST_JARS = new String[] { 56 "core-oj-hostdex", 57 "core-libart-hostdex", 58 "core-icu4j-hostdex", 59 "conscrypt-hostdex", 60 "okhttp-hostdex", 61 "bouncycastle-hostdex", 62 "apache-xml-hostdex", 63 }; 64 65 /** 66 * $BOOTCLASSPATH defined by init.environ.rc on device. 67 * 68 * {@link #DEVICE_JARS} are prepended automatically in {@link #getJarNames()} so do not need to 69 * be listed below. 70 * 71 * (Intended for use with app_process and activities.) 72 * 73 * See also system/core/rootdir/init.environment.rc.in 74 * and PRODUCT_BOOT_JARS in build/make/target/product/base_system.mk for the build system 75 * generation. 76 */ 77 private static final String[] APP_JARS = new String[] { 78 "framework", 79 "ext", 80 "telephony-common", 81 "voip-common", 82 "ims-common", 83 "android.test.base", 84 // TODO: get this list programatically 85 }; 86 acceptsVmArgs()87 public boolean acceptsVmArgs() { 88 return this != ACTIVITY; 89 } 90 91 /** 92 * Returns {@code true} if execution happens on the local machine. e.g. host-mode android or a 93 * JVM. 94 */ isLocal()95 public boolean isLocal() { 96 return isHost() || this == ModeId.JVM; 97 } 98 99 /** Returns {@code true} if execution takes place with a host-mode Android runtime */ isHost()100 public boolean isHost() { 101 return this == HOST; 102 } 103 104 /** Returns {@code true} if execution takes place with a device-mode Android runtime */ isDevice()105 public boolean isDevice() { 106 return this == ModeId.DEVICE || this == ModeId.APP_PROCESS; 107 } 108 requiresAndroidSdk()109 public boolean requiresAndroidSdk() { 110 return this != JVM; 111 } 112 supportsVariant(Variant variant)113 public boolean supportsVariant(Variant variant) { 114 if (variant == Variant.DEFAULT) { 115 return true; 116 } else if (variant == Variant.X64 || variant == Variant.X32) { 117 return this == HOST || this == DEVICE || this == APP_PROCESS; 118 } 119 // Unknown variant. 120 return false; 121 } 122 123 /** Does this mode support chroot-based execution? */ supportsChroot()124 public boolean supportsChroot() { 125 // We only support execution from a chroot directory in device mode for now. 126 return this == ModeId.DEVICE; 127 } 128 supportsToolchain(Toolchain toolchain)129 public boolean supportsToolchain(Toolchain toolchain) { 130 return (this == JVM && toolchain == Toolchain.JAVAC) 131 || (this != JVM && toolchain != Toolchain.JAVAC); 132 } 133 134 /** The default command to use for the mode unless overridden by --vm-command */ defaultVmCommand(Variant variant)135 public String defaultVmCommand(Variant variant) { 136 if (!supportsVariant(variant)) { 137 throw new AssertionError("Unsupported variant: " + variant + " for " + this); 138 } 139 switch (this) { 140 case DEVICE: 141 case HOST: 142 if (variant == Variant.DEFAULT) { 143 return "dalvikvm"; 144 } else if (variant == Variant.X32) { 145 return "dalvikvm32"; 146 } else if (variant == Variant.X64) { 147 return "dalvikvm64"; 148 } 149 throw throwInvalidVariant(variant); 150 case JVM: 151 if (variant == Variant.DEFAULT) { 152 return "java"; 153 } 154 throw throwInvalidVariant(variant); 155 case APP_PROCESS: 156 if (variant == Variant.DEFAULT) { 157 return "app_process"; 158 } else if (variant == Variant.X32) { 159 return "app_process32"; 160 } else if (variant == Variant.X64) { 161 return "app_process64"; 162 } 163 throw throwInvalidVariant(variant); 164 case ACTIVITY: 165 if (variant == Variant.DEFAULT) { 166 return null; 167 } 168 throw throwInvalidVariant(variant); 169 default: 170 throw new IllegalArgumentException("Unknown mode: " + this); 171 } 172 } 173 throwInvalidVariant(Variant variant)174 private IllegalArgumentException throwInvalidVariant(Variant variant) { 175 throw new IllegalArgumentException( 176 "Unknown variant " + variant + " for mode " + this); 177 } 178 179 /** 180 * Return the names of jars required to compile in this mode when android.jar is not being used. 181 * Also used to generated the bootclasspath in HOST* and DEVICE* modes. 182 */ getJarNames()183 public String[] getJarNames() { 184 List<String> jarNames = new ArrayList<String>(); 185 switch (this) { 186 case ACTIVITY: 187 case APP_PROCESS: 188 // Order matters. Add device-jars before app-jars. 189 jarNames.addAll(Arrays.asList(DEVICE_JARS)); 190 jarNames.addAll(Arrays.asList(APP_JARS)); 191 break; 192 case DEVICE: 193 jarNames.addAll(Arrays.asList(DEVICE_JARS)); 194 break; 195 case HOST: 196 jarNames.addAll(Arrays.asList(HOST_JARS)); 197 break; 198 default: 199 throw new IllegalArgumentException("Unsupported mode: " + this); 200 } 201 return jarNames.toArray(new String[jarNames.size()]); 202 } 203 204 /** Returns the default toolchain to use with the mode if not overriden. */ defaultToolchain()205 public Toolchain defaultToolchain() { 206 switch (this) { 207 case JVM: 208 return Toolchain.JAVAC; 209 default: 210 return Toolchain.D8; 211 } 212 } 213 } 214