View Javadoc

1   /*
2    * #%L
3    * Native ARchive plugin for Maven
4    * %%
5    * Copyright (C) 2002 - 2014 NAR Maven Plugin developers.
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * 
11   * http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package com.github.maven_nar;
21  
22  import java.io.File;
23  import java.io.IOException;
24  
25  import org.apache.maven.plugin.MojoExecutionException;
26  import org.apache.maven.plugin.MojoFailureException;
27  import org.apache.maven.plugins.annotations.LifecyclePhase;
28  import org.apache.maven.plugins.annotations.Mojo;
29  import org.apache.maven.plugins.annotations.Parameter;
30  import org.codehaus.plexus.util.FileUtils;
31  
32  /**
33   * Copies the GNU style source files to a target area, autogens and configures
34   * them.
35   * 
36   * @author Mark Donszelmann
37   */
38  @Mojo(name = "nar-gnu-configure", requiresProject = true, defaultPhase = LifecyclePhase.PROCESS_SOURCES)
39  public class NarGnuConfigureMojo extends AbstractGnuMojo {
40  
41    private static final String AUTOGEN = "autogen.sh";
42  
43    private static final String BUILDCONF = "buildconf";
44  
45    private static final String CONFIGURE = "configure";
46  
47    // JDK 1.4 compatibility
48    private static String arraysToString(final Object[] a) {
49      if (a == null) {
50        return "null";
51      }
52      final int iMax = a.length - 1;
53      if (iMax == -1) {
54        return "[]";
55      }
56  
57      final StringBuilder b = new StringBuilder();
58      b.append('[');
59      for (int i = 0;; i++) {
60        b.append(String.valueOf(a[i]));
61        if (i == iMax) {
62          return b.append(']').toString();
63        }
64        b.append(", ");
65      }
66    }
67  
68    /**
69     * If true, we run <code>./configure</code> in the source directory instead of
70     * copying the
71     * source code to the <code>target/</code> directory first (this saves disk
72     * space but
73     * violates Maven's paradigm of keeping generated files inside the
74     * <code>target/</code> directory structure.
75     */
76    @Parameter(property = "nar.gnu.configure.in-place")
77    private boolean gnuConfigureInPlace;
78  
79    /**
80     * Skip running of autogen.sh (aka buildconf).
81     */
82    @Parameter(property = "nar.gnu.autogen.skip")
83    private boolean gnuAutogenSkip;
84  
85    /**
86     * Skip running of configure and therefore also autogen.sh
87     */
88    @Parameter(property = "nar.gnu.configure.skip")
89    private boolean gnuConfigureSkip;
90  
91    /**
92     * Arguments to pass to GNU configure.
93     */
94    @Parameter(property = "nar.gnu.configure.args", defaultValue = "")
95    private String gnuConfigureArgs;
96  
97    /**
98     * Arguments to pass to GNU buildconf.
99     */
100   @Parameter(property = "nar.gnu.buildconf.args", defaultValue = "")
101   private String gnuBuildconfArgs;
102 
103   public NarGnuConfigureMojo() {
104   }
105 
106   @Override
107   public final void narExecute() throws MojoExecutionException, MojoFailureException {
108 
109     if (!useGnu()) {
110       return;
111     }
112 
113     final File sourceDir = getGnuSourceDirectory();
114     if (sourceDir.exists()) {
115       File targetDir;
116 
117       if (!this.gnuConfigureInPlace) {
118         targetDir = getGnuAOLSourceDirectory();
119 
120         getLog().info("Copying GNU sources");
121 
122         try {
123           FileUtils.mkdir(targetDir.getPath());
124           NarUtil.copyDirectoryStructure(sourceDir, targetDir, null, null);
125         } catch (final IOException e) {
126           throw new MojoExecutionException("Failed to copy GNU sources", e);
127         }
128 
129         if (!this.gnuConfigureSkip && !this.gnuAutogenSkip) {
130           final File autogen = new File(targetDir, AUTOGEN);
131           final File buildconf = new File(targetDir, BUILDCONF);
132           if (autogen.exists()) {
133             getLog().info("Running GNU " + AUTOGEN);
134             runAutogen(autogen, targetDir, null);
135           } else if (buildconf.exists()) {
136             getLog().info("Running GNU " + BUILDCONF);
137             String gnuBuildconfArgsArray[] = null;
138             if (this.gnuBuildconfArgs != null) {
139               gnuBuildconfArgsArray = this.gnuBuildconfArgs.split("\\s");
140             }
141             runAutogen(buildconf, targetDir, gnuBuildconfArgsArray);
142           }
143         }
144       } else {
145         targetDir = sourceDir;
146       }
147 
148       final File configure = new File(targetDir, CONFIGURE);
149       if (!this.gnuConfigureSkip && configure.exists()) {
150         getLog().info("Running GNU " + CONFIGURE);
151 
152         NarUtil.makeExecutable(configure, getLog());
153         String[] args = null;
154 
155         // create the array to hold constant and additional args
156         if (this.gnuConfigureArgs != null) {
157           final String[] a = this.gnuConfigureArgs.split(" ");
158           args = new String[a.length + 2];
159 
160           System.arraycopy(a, 0, args, 2, a.length);
161         } else {
162           args = new String[2];
163         }
164 
165         // first 2 args are constant
166         args[0] = configure.getAbsolutePath();
167         args[1] = "--prefix=" + getGnuAOLTargetDirectory().getAbsolutePath();
168 
169         final File buildDir = getGnuAOLSourceDirectory();
170         FileUtils.mkdir(buildDir.getPath());
171 
172         getLog().info("args: " + arraysToString(args));
173         final int result = NarUtil.runCommand("sh", args, buildDir, null, getLog());
174         if (result != 0) {
175           throw new MojoExecutionException("'" + CONFIGURE + "' errorcode: " + result);
176         }
177       }
178     }
179   }
180 
181   private void runAutogen(final File autogen, final File targetDir, final String args[])
182       throws MojoExecutionException, MojoFailureException {
183     // fix missing config directory
184     final File configDir = new File(targetDir, "config");
185     if (!configDir.exists()) {
186       configDir.mkdirs();
187     }
188 
189     NarUtil.makeExecutable(autogen, getLog());
190     getLog().debug("running sh ./" + autogen.getName());
191 
192     String arguments[] = null;
193     if (args != null) {
194       arguments = new String[1 + args.length];
195       System.arraycopy(args, 0, arguments, 1, args.length);
196     } else {
197       arguments = new String[1];
198     }
199     arguments[0] = "./" + autogen.getName();
200 
201     getLog().info("args: " + arraysToString(arguments));
202 
203     final int result = NarUtil.runCommand("sh", arguments, targetDir, null, getLog());
204     if (result != 0) {
205       throw new MojoExecutionException("'" + autogen.getName() + "' errorcode: " + result);
206     }
207   }
208 
209 }