From 89a0ed230c1a967462ac03be09f6d5349c0bc9c1 Mon Sep 17 00:00:00 2001 From: Yohann Roussel Date: Thu, 18 Jun 2015 12:30:49 +0200 Subject: Declare CLI api and let Jack implement it This should allow compatibility with the next server. Change-Id: Ic57b3b68fa31caad24298be24bed3db6f533ab2d --- .../android/jack/api/v01/Cli01CompilationTask.java | 34 +++++++ .../src/com/android/jack/api/v01/Cli01Config.java | 59 ++++++++++++ jack/src/com/android/jack/Options.java | 10 ++ .../android/jack/api/impl/JackProviderImpl.java | 7 +- .../api/v01/impl/Cli01CompilationTaskImpl.java | 46 ++++++++++ .../android/jack/api/v01/impl/Cli01ConfigImpl.java | 102 +++++++++++++++++++++ 6 files changed, 257 insertions(+), 1 deletion(-) create mode 100644 jack-api/src/com/android/jack/api/v01/Cli01CompilationTask.java create mode 100644 jack-api/src/com/android/jack/api/v01/Cli01Config.java create mode 100644 jack/src/com/android/jack/api/v01/impl/Cli01CompilationTaskImpl.java create mode 100644 jack/src/com/android/jack/api/v01/impl/Cli01ConfigImpl.java diff --git a/jack-api/src/com/android/jack/api/v01/Cli01CompilationTask.java b/jack-api/src/com/android/jack/api/v01/Cli01CompilationTask.java new file mode 100644 index 0000000..c609705 --- /dev/null +++ b/jack-api/src/com/android/jack/api/v01/Cli01CompilationTask.java @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.jack.api.v01; + + +/** + * A task allowing to run the Jack compiler once. + */ +public interface Cli01CompilationTask { + + /** + * Runs the Jack compiler. May be called only once. + * @return command line status + * @throws UnrecoverableException If an error out of Jack's control occurred + * @throws ConfigurationException If there is an error in the configuration + * @throws IllegalStateException If Jack is run more than once + */ + int run() throws UnrecoverableException, ConfigurationException, + IllegalStateException; +} diff --git a/jack-api/src/com/android/jack/api/v01/Cli01Config.java b/jack-api/src/com/android/jack/api/v01/Cli01Config.java new file mode 100644 index 0000000..b661f1d --- /dev/null +++ b/jack-api/src/com/android/jack/api/v01/Cli01Config.java @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.jack.api.v01; + +import com.android.jack.api.JackConfig; + +import java.io.File; +import java.io.PrintStream; + +import javax.annotation.Nonnull; + +/** + * A configuration for CLI level 01 of the Jack compiler. + */ +public interface Cli01Config extends JackConfig { + + /** + * Creates an instance of the {@link Cli01CompilationTask} according to this configuration. + * @param args To be handled as command line arguments. + * @return The {@link Cli01CompilationTask} + * @throws ConfigurationException + */ + @Nonnull + Cli01CompilationTask getTask(@Nonnull String[] args) throws ConfigurationException; + + /** + * Redirect Jack's error output to the given stream. + * @param standardError The stream where to write errors. + */ + void setStandardError(@Nonnull PrintStream standardError); + + /** + * Redirect Jack's standards output to the given stream. + * @param standardOutput The stream where to write non error messages. + */ + void setStandardOutput(@Nonnull PrintStream standardOutput); + + /** + * Defines Jack's working directory. + * @param workingDirectory The base directory that will be used to evaluate non absolute file + * paths. + */ + void setWorkingDirectory(@Nonnull File workingDirectory); + +} diff --git a/jack/src/com/android/jack/Options.java b/jack/src/com/android/jack/Options.java index ed05863..dc713f4 100644 --- a/jack/src/com/android/jack/Options.java +++ b/jack/src/com/android/jack/Options.java @@ -1035,10 +1035,20 @@ public class Options { this.workingDirectory = workingDirectory; } + @CheckForNull + public File getWorkingDirectory() { + return workingDirectory; + } + public void setStandardError(@Nonnull PrintStream standardError) { this.standardError = standardError; } + @CheckForNull + public PrintStream getStandardError() { + return standardError; + } + public void setStandardOutput(@Nonnull PrintStream standardOutput) { this.standardOutput = standardOutput; } diff --git a/jack/src/com/android/jack/api/impl/JackProviderImpl.java b/jack/src/com/android/jack/api/impl/JackProviderImpl.java index 1ec577f..9f14edf 100644 --- a/jack/src/com/android/jack/api/impl/JackProviderImpl.java +++ b/jack/src/com/android/jack/api/impl/JackProviderImpl.java @@ -21,7 +21,9 @@ import com.android.jack.api.ConfigNotSupportedException; import com.android.jack.api.JackConfig; import com.android.jack.api.JackProvider; import com.android.jack.api.v01.Api01Config; +import com.android.jack.api.v01.Cli01Config; import com.android.jack.api.v01.impl.Api01ConfigImpl; +import com.android.jack.api.v01.impl.Cli01ConfigImpl; import java.util.ArrayList; import java.util.Collection; @@ -40,6 +42,8 @@ public class JackProviderImpl implements JackProvider { public T createConfig(Class cls) throws ConfigNotSupportedException { if (cls == Api01Config.class) { return (T) new Api01ConfigImpl(); + } else if (cls == Cli01Config.class) { + return (T) new Cli01ConfigImpl(); } throw new ConfigNotSupportedException(cls.getName() + " are not supported"); @@ -49,13 +53,14 @@ public class JackProviderImpl implements JackProvider { public Collection> getSupportedConfigs() { List> result = new ArrayList>(1); result.add(Api01Config.class); + result.add(Cli01Config.class); return result; } @Override @Nonnull public boolean isConfigSupported(@Nonnull Class cls) { - return cls == Api01Config.class; + return cls == Api01Config.class || cls == Cli01Config.class; } @Override diff --git a/jack/src/com/android/jack/api/v01/impl/Cli01CompilationTaskImpl.java b/jack/src/com/android/jack/api/v01/impl/Cli01CompilationTaskImpl.java new file mode 100644 index 0000000..a8d6ee8 --- /dev/null +++ b/jack/src/com/android/jack/api/v01/impl/Cli01CompilationTaskImpl.java @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.jack.api.v01.impl; + +import com.android.jack.CommandLine; +import com.android.jack.Options; +import com.android.jack.api.v01.Cli01CompilationTask; + +import java.io.PrintStream; + +import javax.annotation.Nonnull; + +class Cli01CompilationTaskImpl extends CommandLine implements Cli01CompilationTask { + + + @Nonnull + private final Options options; + + public Cli01CompilationTaskImpl(@Nonnull Options options) { + this.options = options; + } + + @Override + public int run() { + PrintStream err = options.getStandardError(); + if (err == null) { + err = System.err; + } + return runJack(err, options); + } + +} diff --git a/jack/src/com/android/jack/api/v01/impl/Cli01ConfigImpl.java b/jack/src/com/android/jack/api/v01/impl/Cli01ConfigImpl.java new file mode 100644 index 0000000..948d9a2 --- /dev/null +++ b/jack/src/com/android/jack/api/v01/impl/Cli01ConfigImpl.java @@ -0,0 +1,102 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.jack.api.v01.impl; + +import com.android.jack.IllegalOptionsException; +import com.android.jack.Jack; +import com.android.jack.Options; +import com.android.jack.api.v01.Cli01CompilationTask; +import com.android.jack.api.v01.Cli01Config; +import com.android.jack.api.v01.ConfigurationException; +import com.android.sched.util.RunnableHooks; +import com.android.sched.util.config.cli.TokenIterator; +import com.android.sched.util.location.NoLocation; + +import org.kohsuke.args4j.CmdLineException; +import org.kohsuke.args4j.CmdLineParser; +import org.kohsuke.args4j.ParserProperties; + +import java.io.File; +import java.io.IOException; +import java.io.PrintStream; +import java.util.ArrayList; +import java.util.List; + +import javax.annotation.Nonnull; + +/** + * A configuration implementation for CLI level 01 of the Jack compiler. + */ +public class Cli01ConfigImpl implements Cli01Config { + + @Nonnull + protected final Options options; + + public Cli01ConfigImpl() { + options = new Options(); + } + + @Override + @Nonnull + public Cli01CompilationTask getTask(@Nonnull String[] args) throws ConfigurationException { + RunnableHooks configHooks = new RunnableHooks(); + try { + TokenIterator iterator = new TokenIterator(new NoLocation(), args); + File workingDirectory = options.getWorkingDirectory(); + if (workingDirectory != null) { + iterator = iterator.withFileRelativeTo(workingDirectory); + } + List list = new ArrayList(); + while (iterator.hasNext()) { + list.add(iterator.next()); + } + CmdLineParser parser = + new CmdLineParser(options, ParserProperties.defaults().withUsageWidth(100)); + + parser.parseArgument(list); + parser.stopOptionParsing(); + + Jack.check(options, configHooks); + } catch (com.android.sched.util.config.ConfigurationException e) { + throw new ConfigurationException(e.getMessage(), e); + } catch (IllegalOptionsException e) { + throw new ConfigurationException(e.getMessage(), e); + } catch (CmdLineException e) { + throw new ConfigurationException(e.getMessage(), e); + } catch (IOException e) { + throw new ConfigurationException(e.getMessage(), e); + } + + return new Cli01CompilationTaskImpl(options); + } + + @Override + public void setStandardError(@Nonnull PrintStream standardError) { + options.setStandardError(standardError); + } + + @Override + public void setStandardOutput(@Nonnull PrintStream standardOutput) { + options.setStandardOutput(standardOutput); + } + + @Override + public void setWorkingDirectory(@Nonnull File workingDirectory) { + options.setWorkingDirectory(workingDirectory); + } + +} -- cgit v1.1