Home | History | Annotate | Download | only in jnigen
      1 /*******************************************************************************
      2  * Copyright 2011 See AUTHORS file.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *   http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  ******************************************************************************/
     16 
     17 package com.badlogic.gdx.jnigen;
     18 
     19 import java.io.BufferedReader;
     20 import java.io.File;
     21 import java.io.IOException;
     22 import java.io.InputStreamReader;
     23 import java.util.regex.Matcher;
     24 import java.util.regex.Pattern;
     25 
     26 /** Executes an ant script and its targets or an Android NDK build. See {@link AntScriptGenerator}.
     27  * @author mzechner */
     28 public class BuildExecutor {
     29 	/** Execute the Ant script file with the given parameters.
     30 	 * @param buildFile
     31 	 * @param params
     32 	 * @return whether the Ant succeeded */
     33 	public static boolean executeAnt (String buildFile, String params) {
     34 		FileDescriptor build = new FileDescriptor(buildFile);
     35 		String ant = System.getProperty("os.name").contains("Windows") ? "ant.bat" : "ant";
     36 		String command = ant + " -f \"" + build.file().getAbsolutePath() + "\" " + params;
     37 		System.out.println("Executing '" + command + "'");
     38 		return startProcess(command, build.parent().file());
     39 	}
     40 
     41 	/** Execute ndk-build in the given directory
     42 	 * @param directory */
     43 	public static void executeNdk (String directory) {
     44 		FileDescriptor build = new FileDescriptor(directory);
     45 		String command = "ndk-build";
     46 		startProcess(command, build.file());
     47 	}
     48 
     49 	private static boolean startProcess (String command, File directory) {
     50 		try {
     51 			final Process process = new ProcessBuilder(command.split(" ")).redirectErrorStream(true).start();
     52 
     53 			Thread t = new Thread(new Runnable() {
     54 				@Override
     55 				public void run () {
     56 					BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
     57 					String line = null;
     58 					try {
     59 						while ((line = reader.readLine()) != null) {
     60 							// augment output with java file line references :D
     61 							printFileLineNumber(line);
     62 						}
     63 					} catch (IOException e) {
     64 						e.printStackTrace();
     65 					}
     66 				}
     67 
     68 				private void printFileLineNumber (String line) {
     69 					if (line.contains("warning") || line.contains("error")) {
     70 						try {
     71 							String fileName = getFileName(line);
     72 							String error = getError(line);
     73 							int lineNumber = getLineNumber(line) - 1;
     74 							if (fileName != null && lineNumber >= 0) {
     75 								FileDescriptor file = new FileDescriptor(fileName);
     76 								if (file.exists()) {
     77 									String[] content = file.readString().split("\n");
     78 									if (lineNumber < content.length) {
     79 										for (int i = lineNumber; i >= 0; i--) {
     80 											String contentLine = content[i];
     81 											if (contentLine.startsWith("//@line:")) {
     82 												int javaLineNumber = Integer.parseInt(contentLine.split(":")[1].trim());
     83 												System.out.flush();
     84 												if (line.contains("warning")) {
     85 													System.out.println("(" + file.nameWithoutExtension() + ".java:"
     86 														+ (javaLineNumber + (lineNumber - i) - 1) + "): " + error + ", original: " + line);
     87 													System.out.flush();
     88 												} else {
     89 													System.err.println("(" + file.nameWithoutExtension() + ".java:"
     90 														+ (javaLineNumber + (lineNumber - i) - 1) + "): " + error + ", original: " + line);
     91 													System.err.flush();
     92 												}
     93 												return;
     94 											}
     95 										}
     96 									}
     97 								} else {
     98 									System.out.println(line);
     99 								}
    100 							}
    101 						} catch (Throwable t) {
    102 							System.out.println(line);
    103 							// silent death...
    104 						}
    105 					} else {
    106 						System.out.println(line);
    107 					}
    108 				}
    109 
    110 				private String getFileName (String line) {
    111 					Pattern pattern = Pattern.compile("(.*):([0-9])+:[0-9]+:");
    112 					Matcher matcher = pattern.matcher(line);
    113 					matcher.find();
    114 					String fileName = matcher.groupCount() >= 2 ? matcher.group(1).trim() : null;
    115 					if (fileName == null) return null;
    116 					int index = fileName.indexOf(" ");
    117 					if (index != -1)
    118 						return fileName.substring(index).trim();
    119 					else
    120 						return fileName;
    121 				}
    122 
    123 				private String getError (String line) {
    124 					Pattern pattern = Pattern.compile(":[0-9]+:[0-9]+:(.+)");
    125 					Matcher matcher = pattern.matcher(line);
    126 					matcher.find();
    127 					return matcher.groupCount() >= 1 ? matcher.group(1).trim() : null;
    128 				}
    129 
    130 				private int getLineNumber (String line) {
    131 					Pattern pattern = Pattern.compile(":([0-9]+):[0-9]+:");
    132 					Matcher matcher = pattern.matcher(line);
    133 					matcher.find();
    134 					return matcher.groupCount() >= 1 ? Integer.parseInt(matcher.group(1)) : -1;
    135 				}
    136 			});
    137 			t.setDaemon(true);
    138 			t.start();
    139 			process.waitFor();
    140 			return process.exitValue() == 0;
    141 		} catch (Exception e) {
    142 			e.printStackTrace();
    143 			return false;
    144 		}
    145 	}
    146 }
    147