rt/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/AheadOfTime.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 11 Sep 2015 14:51:09 +0200
branchflow
changeset 1842 dd4dabfead82
parent 1789 2d81494f54b3
child 1826 511463a1733d
permissions -rw-r--r--
Giving the flow analyzer chance to generate the whole function body
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.bck2brwsr.mojo;
    19 
    20 import java.io.File;
    21 import java.io.FileOutputStream;
    22 import java.io.IOException;
    23 import java.io.InputStream;
    24 import java.io.OutputStreamWriter;
    25 import java.io.Writer;
    26 import java.net.MalformedURLException;
    27 import java.net.URL;
    28 import java.net.URLClassLoader;
    29 import java.nio.file.Files;
    30 import java.util.ArrayList;
    31 import java.util.Collection;
    32 import java.util.List;
    33 import java.util.Map;
    34 import java.util.jar.Attributes;
    35 import java.util.jar.JarFile;
    36 import java.util.jar.Manifest;
    37 import java.util.zip.ZipEntry;
    38 import org.apache.maven.artifact.Artifact;
    39 import org.apache.maven.plugin.AbstractMojo;
    40 import org.apache.maven.plugin.MojoExecutionException;
    41 import org.apache.maven.plugin.MojoFailureException;
    42 import org.apache.maven.plugins.annotations.LifecyclePhase;
    43 import org.apache.maven.plugins.annotations.Mojo;
    44 import org.apache.maven.plugins.annotations.Parameter;
    45 import org.apache.maven.plugins.annotations.ResolutionScope;
    46 import org.apache.maven.project.MavenProject;
    47 import org.apidesign.bck2brwsr.aot.Bck2BrwsrJars;
    48 import org.apidesign.vm4brwsr.Bck2Brwsr;
    49 import org.apidesign.vm4brwsr.ObfuscationLevel;
    50 
    51 /**
    52  *
    53  * @author Jaroslav Tulach
    54  * @since 0.9
    55  */
    56 @Mojo(name = "aot",
    57     requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME,
    58     defaultPhase = LifecyclePhase.PACKAGE
    59 )
    60 public class AheadOfTime extends AbstractMojo {
    61     @Parameter(defaultValue = "${project}")
    62     private MavenProject prj;
    63     
    64     @Parameter(defaultValue = "${project.build.directory}/${project.build.finalName}.jar")
    65     private File mainJar;
    66 
    67     @Parameter(defaultValue = "${project.build.directory}/${project.build.finalName}.js")
    68     private File mainJavaScript;
    69     
    70     @Parameter
    71     private String[] exports;
    72     
    73     /**
    74      * Directory where to generate ahead-of-time JavaScript files for
    75      * required libraries.
    76      */
    77     @Parameter(defaultValue = "lib")
    78     private String classPathPrefix;
    79 
    80     /** Root JavaScript file to generate */
    81     @Parameter(defaultValue="${project.build.directory}/bck2brwsr.js")
    82     private File vm;
    83     
    84     @Parameter(defaultValue = "true")
    85     private boolean generateAotLibraries;
    86     
    87     @Parameter(defaultValue = "true")
    88     private boolean ignoreBootClassPath;
    89     
    90     /**
    91      * The obfuscation level for the generated JavaScript file.
    92      *
    93      * @since 0.5
    94      */
    95     @Parameter(defaultValue = "NONE")
    96     private ObfuscationLevel obfuscation;
    97     
    98     @Override
    99     public void execute() throws MojoExecutionException, MojoFailureException {
   100         URLClassLoader loader;
   101         try {
   102             loader = buildClassLoader(mainJar, prj.getArtifacts());
   103         } catch (MalformedURLException ex) {
   104             throw new MojoFailureException("Can't initialize classloader");
   105         }
   106         for (Artifact a : prj.getArtifacts()) {
   107             if (a.getFile() == null) {
   108                 continue;
   109             }
   110             String n = a.getFile().getName();
   111             if (!n.endsWith(".jar")) {
   112                 continue;
   113             }
   114             if ("provided".equals(a.getScope())) {
   115                 continue;
   116             }
   117             File aot = new File(mainJavaScript.getParent(), classPathPrefix);
   118             aot.mkdirs();
   119             File js = new File(aot, n.substring(0, n.length() - 4) + ".js");
   120             if (js.exists()) {
   121                 getLog().info("Skipping " + js + " as it already exists.");
   122                 continue;
   123             }
   124             try {
   125                 aotLibrary(a, js , loader);
   126             } catch (IOException ex) {
   127                 throw new MojoFailureException("Can't compile " + a.getFile(), ex);
   128             }
   129         }
   130         
   131         try {
   132             if (mainJavaScript.exists()) {
   133                 getLog().info("Skipping " + mainJavaScript + " as it already exists.");
   134             } else {
   135                 getLog().info("Generating " + mainJavaScript);
   136                 Bck2Brwsr c = Bck2BrwsrJars.configureFrom(null, mainJar, loader, ignoreBootClassPath);
   137                 if (exports != null) {
   138                     for (String e : exports) {
   139                         c = c.addExported(e.replace('.', '/'));
   140                     }
   141                 }
   142                 Writer w = new OutputStreamWriter(new FileOutputStream(mainJavaScript), "UTF-8");
   143                 c.
   144                         obfuscation(obfuscation).
   145                         generate(w);
   146                 w.close();
   147             }
   148         } catch (IOException ex) {
   149             throw new MojoFailureException("Cannot generate script for " + mainJar, ex);
   150         }
   151             
   152         try {
   153             Writer w = new OutputStreamWriter(new FileOutputStream(vm), "UTF-8");
   154             Bck2Brwsr.newCompiler().
   155                     obfuscation(obfuscation).
   156                     standalone(false).
   157                     resources(new Bck2Brwsr.Resources() {
   158 
   159                 @Override
   160                 public InputStream get(String resource) throws IOException {
   161                     return null;
   162                 }
   163             }).
   164                     generate(w);
   165             w.close();
   166             
   167         } catch (IOException ex) {
   168             throw new MojoExecutionException("Can't compile", ex);
   169         }
   170     }
   171 
   172     private void aotLibrary(Artifact a, File js, URLClassLoader loader) throws IOException, MojoExecutionException {
   173         for (Artifact b : prj.getArtifacts()) {
   174             if ("bck2brwsr".equals(b.getClassifier())) { // NOI18N
   175                 getLog().debug("Inspecting " + b.getFile());
   176                 JarFile jf = new JarFile(b.getFile());
   177                 Manifest man = jf.getManifest();
   178                 for (Map.Entry<String, Attributes> entrySet : man.getEntries().entrySet()) {
   179                     String entryName = entrySet.getKey();
   180                     Attributes attr = entrySet.getValue();
   181                     
   182                     if (
   183                         a.getArtifactId().equals(attr.getValue("Bck2BrwsrArtifactId")) &&
   184                         a.getGroupId().equals(attr.getValue("Bck2BrwsrGroupId")) &&
   185                         a.getVersion().equals(attr.getValue("Bck2BrwsrVersion")) &&
   186                         (
   187                             obfuscation == ObfuscationLevel.FULL && "true".equals(attr.getValue("Bck2BrwsrMinified"))
   188                             ||
   189                             obfuscation != ObfuscationLevel.FULL && "true".equals(attr.getValue("Bck2BrwsrDebug"))
   190                         )
   191                     ) {
   192                         getLog().info("Extracting " + js + " from " + b.getFile());
   193                         InputStream is = jf.getInputStream(new ZipEntry(entryName));
   194                         Files.copy(is, js.toPath());
   195                         is.close();
   196                         return;
   197                     }
   198                 }
   199             }
   200         }
   201         if (!generateAotLibraries) {
   202             throw new MojoExecutionException("Not generating " + js + " and no precompiled version found!");
   203         }
   204         getLog().info("Generating " + js);
   205         Writer w = new OutputStreamWriter(new FileOutputStream(js), "UTF-8");
   206         Bck2Brwsr c = Bck2BrwsrJars.configureFrom(null, a.getFile(), loader, ignoreBootClassPath);
   207         if (exports != null) {
   208             c = c.addExported(exports);
   209         }
   210         c.
   211             obfuscation(obfuscation).
   212             generate(w);
   213         w.close();
   214     }
   215     private static URLClassLoader buildClassLoader(File root, Collection<Artifact> deps) throws MalformedURLException {
   216         List<URL> arr = new ArrayList<URL>();
   217         if (root != null) {
   218             arr.add(root.toURI().toURL());
   219         }
   220         for (Artifact a : deps) {
   221             if (a.getFile() != null) {
   222                 arr.add(a.getFile().toURI().toURL());
   223             }
   224         }
   225         return new URLClassLoader(arr.toArray(new URL[0]), Java2JavaScript.class.getClassLoader());
   226     }
   227 }