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