Search in sources :

Example 1 with LaunchParameters

use of org.robovm.compiler.target.LaunchParameters in project robovm by robovm.

the class AppCompiler method main.

public static void main(String[] args) throws IOException {
    AppCompiler compiler = null;
    Config.Builder builder = null;
    boolean verbose = false;
    boolean run = false;
    boolean archive = false;
    List<Arch> archs = new ArrayList<>();
    String dumpConfigFile = null;
    List<String> runArgs = new ArrayList<String>();
    try {
        builder = new Config.Builder();
        Map<String, PluginArgument> pluginArguments = builder.fetchPluginArguments();
        int i = 0;
        while (i < args.length) {
            if ("-cp".equals(args[i]) || "-classpath".equals(args[i])) {
                for (String p : args[++i].split(File.pathSeparator)) {
                    builder.addClasspathEntry(new File(p));
                }
            } else if ("-bcp".equals(args[i]) || "-bootcp".equals(args[i]) || "-bootclasspath".equals(args[i])) {
                for (String p : args[++i].split(File.pathSeparator)) {
                    builder.addBootClasspathEntry(new File(p));
                }
            } else if ("-jar".equals(args[i])) {
                builder.mainJar(new File(args[++i]));
            } else if ("-o".equals(args[i])) {
                builder.executableName(args[++i]);
            } else if ("-d".equals(args[i])) {
                builder.installDir(new File(args[++i]));
            } else if ("-cache".equals(args[i])) {
                builder.cacheDir(new File(args[++i]));
            } else if ("-home".equals(args[i])) {
                builder.home(new Config.Home(new File(args[++i])));
            } else if ("-tmp".equals(args[i])) {
                builder.tmpDir(new File(args[++i]));
            } else if ("-threads".equals(args[i])) {
                String s = args[++i];
                try {
                    int n = Integer.parseInt(s);
                    // Make sure n > 0 and cap at 128 threads.
                    n = Math.max(n, 1);
                    n = Math.min(n, 128);
                    builder.threads(n);
                } catch (NumberFormatException e) {
                    throw new IllegalArgumentException("Unparsable thread count: " + s);
                }
            } else if ("-run".equals(args[i])) {
                run = true;
            } else if ("-verbose".equals(args[i])) {
                verbose = true;
            } else if ("-config".equals(args[i])) {
                builder.read(new File(args[++i]));
            } else if ("-dumpconfig".equals(args[i])) {
                dumpConfigFile = args[++i];
            } else if ("-properties".equals(args[i])) {
                builder.addProperties(new File(args[++i]));
            } else if (args[i].startsWith("-P")) {
                int index = args[i].indexOf('=');
                if (index <= 0) {
                    throw new IllegalArgumentException("Malformed property: " + args[i]);
                }
                String name = args[i].substring(2, index);
                String value = args[i].substring(index + 1);
                builder.addProperty(name, value);
            } else if ("-debug".equals(args[i])) {
                builder.debug(true);
            } else if ("-use-debug-libs".equals(args[i])) {
                builder.useDebugLibs(true);
            } else if ("-dump-intermediates".equals(args[i])) {
                builder.dumpIntermediates(true);
            } else if ("-dynamic-jni".equals(args[i])) {
            // TODO: Old option not used any longer. We still accept it
            // for now. Delete it in a future release.
            } else if ("-skiprt".equals(args[i])) {
                builder.skipRuntimeLib(true);
            } else if ("-skipsign".equals(args[i])) {
                builder.iosSkipSigning(true);
            } else if ("-clean".equals(args[i])) {
                builder.clean(true);
            } else if ("-help".equals(args[i]) || "-?".equals(args[i])) {
                printUsageAndExit(null, builder.getPlugins());
            } else if ("-version".equals(args[i])) {
                printVersionAndExit();
            } else if ("-cc".equals(args[i])) {
                builder.ccBinPath(new File(args[++i]));
            } else if ("-os".equals(args[i])) {
                String s = args[++i];
                builder.os("auto".equals(s) ? null : OS.valueOf(s));
            } else if ("-arch".equals(args[i])) {
                String s = args[++i];
                if (!"auto".equals(s)) {
                    archs.add(Arch.valueOf(s));
                }
            } else if ("-archs".equals(args[i])) {
                for (String s : args[++i].split(":")) {
                    if (!"auto".equals(s)) {
                        archs.add(Arch.valueOf(s));
                    }
                }
            //                } else if ("-cpu".equals(args[i])) {
            //                    builder.cpu(args[++i]);
            } else if ("-target".equals(args[i])) {
                String s = args[++i];
                builder.targetType("auto".equals(s) ? null : s);
            } else if ("-treeshaker".equals(args[i])) {
                String s = args[++i];
                builder.treeShakerMode(TreeShakerMode.valueOf(s));
            } else if ("-forcelinkclasses".equals(args[i])) {
                for (String p : args[++i].split(":")) {
                    p = p.replace('#', '*');
                    builder.addForceLinkClass(p);
                }
            } else if ("-libs".equals(args[i])) {
                for (String p : args[++i].split(":")) {
                    builder.addLib(new Config.Lib(p, true));
                }
            } else if ("-exportedsymbols".equals(args[i])) {
                for (String p : args[++i].split(":")) {
                    builder.addExportedSymbol(p);
                }
            } else if ("-unhidesymbols".equals(args[i])) {
                for (String p : args[++i].split(":")) {
                    builder.addUnhideSymbol(p);
                }
            } else if ("-frameworks".equals(args[i])) {
                for (String p : args[++i].split(":")) {
                    builder.addFramework(p);
                }
            } else if ("-weakframeworks".equals(args[i])) {
                for (String p : args[++i].split(":")) {
                    builder.addWeakFramework(p);
                }
            } else if ("-resources".equals(args[i])) {
                for (String p : args[++i].split(":")) {
                    if (AntPathMatcher.isPattern(p)) {
                        File dir = new File(AntPathMatcher.rtrimWildcardTokens(p));
                        String pattern = AntPathMatcher.extractPattern(p);
                        builder.addResource(new Resource(dir, null).include(pattern));
                    } else {
                        builder.addResource(new Resource(new File(p)));
                    }
                }
            } else if ("-cacerts".equals(args[i])) {
                String name = args[++i];
                Config.Cacerts cacerts = null;
                if (!"none".equals(name)) {
                    try {
                        cacerts = Config.Cacerts.valueOf(name);
                    } catch (IllegalArgumentException e) {
                        throw new IllegalArgumentException("Illegal -cacerts value: " + name);
                    }
                }
                builder.cacerts(cacerts);
            } else if ("-plist".equals(args[i])) {
                builder.infoPList(new File(args[++i]));
            } else if ("-entitlements".equals(args[i])) {
                builder.iosEntitlementsPList(new File(args[++i]));
            } else if ("-signidentity".equals(args[i])) {
                builder.iosSignIdentity(SigningIdentity.find(SigningIdentity.list(), args[++i]));
            } else if ("-provisioningprofile".equals(args[i])) {
                builder.iosProvisioningProfile(ProvisioningProfile.find(ProvisioningProfile.list(), args[++i]));
            } else if ("-sdk".equals(args[i])) {
                builder.iosSdkVersion(args[++i]);
            } else if ("-printdevicetypes".equals(args[i])) {
                printDeviceTypesAndExit();
            } else if ("-devicetype".equals(args[i])) {
                builder.iosDeviceType(args[++i]);
            } else if ("-archive".equals(args[i])) {
                archive = true;
            } else if ("-createipa".equals(args[i])) {
                archive = true;
            } else if ("-ipaarchs".equals(args[i])) {
                for (String s : args[++i].split(":")) {
                    if (!"auto".equals(s)) {
                        archs.add(Arch.valueOf(s));
                    }
                }
            } else if (args[i].startsWith("-D")) {
            } else if (args[i].startsWith("-X")) {
            } else if (args[i].startsWith("-rvm:")) {
                runArgs.add(args[i]);
            } else if (args[i].startsWith("-")) {
                String argName = args[i].substring(1, args[i].length());
                if (argName.contains("=")) {
                    argName = argName.substring(0, argName.indexOf('='));
                }
                PluginArgument arg = pluginArguments.get(argName);
                if (arg != null) {
                    builder.addPluginArgument(args[i].substring(1));
                } else {
                    throw new IllegalArgumentException("Unrecognized option: " + args[i]);
                }
            } else {
                builder.mainClass(args[i++]);
                break;
            }
            i++;
        }
        builder.archs(archs.toArray(new Arch[archs.size()]));
        while (i < args.length) {
            runArgs.add(args[i++]);
        }
        if (archive && run) {
            throw new IllegalArgumentException("Specify either -run or -createipa/-archive, not both");
        }
        builder.logger(new ConsoleLogger(verbose));
        builder.skipInstall(run);
        if (dumpConfigFile != null) {
            if (dumpConfigFile.equals("-")) {
                builder.write(new OutputStreamWriter(System.out), new File("."));
            } else {
                File file = new File(dumpConfigFile);
                if (file.exists()) {
                    throw new IllegalArgumentException("Cannot dump config to " + file.getAbsolutePath() + ". The file already exists.");
                }
                builder.write(file);
            }
            return;
        }
        compiler = new AppCompiler(builder.build());
    } catch (Throwable t) {
        String message = t.getMessage();
        if (t instanceof ArrayIndexOutOfBoundsException) {
            message = "Missing argument";
        }
        if (t instanceof IndexOutOfBoundsException) {
            message = "Missing argument";
        }
        if (verbose && !(t instanceof StringIndexOutOfBoundsException) && !(t instanceof IllegalArgumentException)) {
            t.printStackTrace();
        }
        printUsageAndExit(message, builder != null ? builder.getPlugins() : null);
    }
    try {
        if (archive) {
            compiler.build();
            compiler.archive();
        } else {
            if (run && !compiler.config.getTarget().canLaunch()) {
                throw new IllegalArgumentException("Cannot launch when building " + compiler.config.getTarget().getType() + " binaries");
            }
            if (run) {
                // Just compile the first slice if multiple archs have been specified
                compiler.compile();
                LaunchParameters launchParameters = compiler.config.getTarget().createLaunchParameters();
                if (launchParameters instanceof IOSSimulatorLaunchParameters) {
                    IOSSimulatorLaunchParameters simParams = (IOSSimulatorLaunchParameters) launchParameters;
                    String deviceName = null;
                    String sdkVersion = null;
                    if (compiler.config.getIosDeviceType() != null) {
                        String[] parts = compiler.config.getIosDeviceType().split("[:;, ]+");
                        deviceName = parts[0].trim();
                        sdkVersion = parts.length > 1 ? parts[1].trim() : null;
                    }
                    DeviceType type = DeviceType.getBestDeviceType(compiler.config.getArch(), null, deviceName, sdkVersion);
                    simParams.setDeviceType(type);
                }
                launchParameters.setArguments(runArgs);
                compiler.launch(launchParameters);
            } else {
                compiler.build();
                compiler.config.getTarget().install();
            }
        }
    } catch (Throwable t) {
        String message = t.getMessage();
        if (verbose && !(t instanceof ExecuteException)) {
            t.printStackTrace();
        }
        printUsageAndExit(message, builder.getPlugins());
    }
}
Also used : Config(org.robovm.compiler.config.Config) IOSSimulatorLaunchParameters(org.robovm.compiler.target.ios.IOSSimulatorLaunchParameters) ArrayList(java.util.ArrayList) ExecuteException(org.apache.commons.exec.ExecuteException) PluginArgument(org.robovm.compiler.plugin.PluginArgument) Resource(org.robovm.compiler.config.Resource) Arch(org.robovm.compiler.config.Arch) DeviceType(org.robovm.compiler.target.ios.DeviceType) ConsoleLogger(org.robovm.compiler.log.ConsoleLogger) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) LaunchParameters(org.robovm.compiler.target.LaunchParameters) IOSSimulatorLaunchParameters(org.robovm.compiler.target.ios.IOSSimulatorLaunchParameters)

Aggregations

File (java.io.File)1 OutputStreamWriter (java.io.OutputStreamWriter)1 ArrayList (java.util.ArrayList)1 ExecuteException (org.apache.commons.exec.ExecuteException)1 Arch (org.robovm.compiler.config.Arch)1 Config (org.robovm.compiler.config.Config)1 Resource (org.robovm.compiler.config.Resource)1 ConsoleLogger (org.robovm.compiler.log.ConsoleLogger)1 PluginArgument (org.robovm.compiler.plugin.PluginArgument)1 LaunchParameters (org.robovm.compiler.target.LaunchParameters)1 DeviceType (org.robovm.compiler.target.ios.DeviceType)1 IOSSimulatorLaunchParameters (org.robovm.compiler.target.ios.IOSSimulatorLaunchParameters)1