1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
34
35
36
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
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
70
71
72
73
74
75
76 @Parameter(property = "nar.gnu.configure.in-place")
77 private boolean gnuConfigureInPlace;
78
79
80
81
82 @Parameter(property = "nar.gnu.autogen.skip")
83 private boolean gnuAutogenSkip;
84
85
86
87
88 @Parameter(property = "nar.gnu.configure.skip")
89 private boolean gnuConfigureSkip;
90
91
92
93
94 @Parameter(property = "nar.gnu.configure.args", defaultValue = "")
95 private String gnuConfigureArgs;
96
97
98
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
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
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
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 }