Created
December 10, 2012 04:25
-
-
Save karadaisy/4248372 to your computer and use it in GitHub Desktop.
First attempt at a Maven plugin that delegates to CMake
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.util.*; | |
import org.apache.maven.plugin.*; | |
import org.apache.maven.plugin.logging.Log; | |
/** | |
* | |
* @author kmahan | |
* | |
* @goal build | |
* @phase compile | |
*/ | |
public class BuildWithCMakeMojo extends AbstractMojo { | |
/** | |
* @parameter expression="${basedir}" | |
* @readonly | |
*/ | |
private File baseDir; | |
/** | |
* Directory for the out of source build | |
* @parameter | |
*/ | |
private File buildDirectory; | |
/** | |
* Location for the cmake executable, if it's not on PATH | |
* @parameter default-value="cmake" | |
*/ | |
private String cmakeExecutable; | |
/** | |
* Location of the C++ source files | |
* @parameter default-value="${basedir}/src" | |
*/ | |
private File sourceDirectory; | |
/** | |
* Architecture. One of { "x86", "x64" } | |
* @parameter default-value="x86" | |
* @required | |
*/ | |
private String archPrefix; | |
/** | |
* OS. One of { "win", "linux" } | |
* @parameter default-value="win" | |
* @required | |
*/ | |
private String osPrefix; | |
@Override | |
public void execute() throws MojoExecutionException, MojoFailureException { | |
if(buildDirectory == null) { | |
String defaultDirectory = "cmake-build-" + osPrefix + "_" + archPrefix; | |
buildDirectory = new File(baseDir, defaultDirectory); | |
} | |
String generator; | |
if ("win".equals(osPrefix)) { | |
generator = "Visual Studio 9 2008"; | |
if ("x64".equals(archPrefix)) { | |
generator += " Win64"; | |
} | |
} | |
else if ("linux".equals(osPrefix)) { | |
generator = "Unix Makefiles"; | |
} | |
else { | |
throw new MojoExecutionException("Unsupported OS: " + osPrefix); | |
} | |
cmakeGenerate("Release", generator); | |
cmakeBuild("Release", "install"); | |
} | |
private void cmakeGenerate(String buildType, String generator) | |
throws MojoExecutionException { | |
List<String> args = new ArrayList<String>(); | |
args.add(cmakeExecutable); | |
args.add("-DCMAKE_BUILD_TYPE=" + buildType); | |
args.add("-DARCH_PREFIX=" + archPrefix); | |
args.add("-G"); | |
args.add(generator); | |
args.add(sourceDirectory.getPath()); | |
run(buildDirectory, args); | |
} | |
private void cmakeBuild(String buildType, String target) | |
throws MojoExecutionException { | |
List<String> args = new ArrayList<String>(); | |
args.add(cmakeExecutable); | |
args.add("--build"); | |
args.add("."); | |
args.add("--target"); | |
args.add(target); | |
args.add("--config"); | |
args.add(buildType); | |
run(buildDirectory, args); | |
} | |
private void run(File workingDir, List<String> args) | |
throws MojoExecutionException { | |
try { | |
if (!workingDir.exists() && !workingDir.mkdirs()) { | |
throw new MojoExecutionException("Could not create directory: " | |
+ workingDir); | |
} | |
ProcessBuilder pb = new ProcessBuilder(args); | |
pb.directory(workingDir); | |
pb.redirectErrorStream(true); | |
getLog().info("Executing SubProcess: " + args + " in directory " | |
+ workingDir); | |
final Process process = pb.start(); | |
// Try to kill the subprocess if we're interrupted (e.g., by Ctrl-C) | |
Runtime.getRuntime().addShutdownHook(new Thread(){ | |
@Override | |
public void run() { | |
process.destroy(); | |
} | |
}); | |
BufferedReader reader = new BufferedReader(new InputStreamReader( | |
process.getInputStream())); | |
String line; | |
while ((line = reader.readLine()) != null) { | |
System.out.println(line); | |
} | |
int result = process.waitFor(); | |
if (result != 0) { | |
throw new MojoExecutionException( | |
"Sub-process returned with error code: " + result); | |
} | |
} | |
catch (IOException e) { | |
throw new MojoExecutionException("Sub-process Error", e); | |
} | |
catch (InterruptedException e) { | |
throw new MojoExecutionException( | |
"Interrupted waiting for sub-process to finish", e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment