From 2adbe5725a399ac1df275d299e5b352e9e589eaf Mon Sep 17 00:00:00 2001 From: Mark Donszelmann Date: Thu, 29 Oct 2009 15:50:37 +0100 Subject: Progress on NAR-1 --- .../java/org/apache/maven/plugin/nar/Linker.java | 65 +++++++++++++++++---- .../java/org/apache/maven/plugin/nar/NarUtil.java | 67 ++++++++++++++-------- .../apache/maven/plugin/nar/StringTextStream.java | 56 ++++++++++++++++++ .../org/apache/maven/plugin/nar/TextStream.java | 31 ++++++++++ 4 files changed, 185 insertions(+), 34 deletions(-) create mode 100644 src/main/java/org/apache/maven/plugin/nar/StringTextStream.java create mode 100644 src/main/java/org/apache/maven/plugin/nar/TextStream.java (limited to 'src/main') diff --git a/src/main/java/org/apache/maven/plugin/nar/Linker.java b/src/main/java/org/apache/maven/plugin/nar/Linker.java index febb70e..f09458a 100644 --- a/src/main/java/org/apache/maven/plugin/nar/Linker.java +++ b/src/main/java/org/apache/maven/plugin/nar/Linker.java @@ -23,10 +23,12 @@ import java.io.File; import java.io.IOException; import java.util.HashSet; import java.util.Iterator; +import java.util.LinkedList; import java.util.List; import java.util.Properties; import java.util.Set; -import java.util.LinkedList; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import net.sf.antcontrib.cpptasks.CUtil; import net.sf.antcontrib.cpptasks.LinkerDef; @@ -163,13 +165,53 @@ public class Linker } if ( name == null ) { - throw new MojoFailureException( "NAR: One of two things may be wrong here:\n\n"+ - "1. tag is missing inside the tag of your NAR configuration\n\n"+ - "2. no linker is defined in the aol.properties file for '"+prefix+"linker'\n"); + throw new MojoFailureException( "NAR: One of two things may be wrong here:\n\n" + + "1. tag is missing inside the tag of your NAR configuration\n\n" + + "2. no linker is defined in the aol.properties file for '" + prefix + "linker'\n" ); } return name; } + public String getVersion() + throws MojoFailureException, MojoExecutionException + { + if ( name == null ) + throw new MojoFailureException( "Cannot deduce linker version if name is null" ); + + String version = null; + + TextStream out = new StringTextStream(); + TextStream err = new StringTextStream(); + TextStream dbg = new StringTextStream(); + + if ( name.equals( "g++" ) || name.equals( "gcc" ) ) + { + NarUtil.runCommand( "gcc", new String[] { "--version" }, null, null, out, err, dbg ); + Pattern p = Pattern.compile( "\\d+\\.\\d+\\.\\d+" ); + Matcher m = p.matcher( out.toString() ); + if (m.find()) { + version = m.group( 0 ); + } + } + else if ( name.equals( "msvc" ) ) + { + NarUtil.runCommand( "link", new String[] { "/version" }, null, null, out, err, dbg ); + Pattern p = Pattern.compile( "\\d+\\.\\d+\\.\\d+" ); + Matcher m = p.matcher( out.toString() ); + if (m.find()) { + version = m.group( 0 ); + } + } + else + { + throw new MojoFailureException( "Cannot find version number for linker '" + name + "'" ); + } + System.err.println( "O " + out ); + System.err.println( "E " + err ); + System.err.println( "D " + dbg ); + return version; + } + public LinkerDef getLinker( AbstractCompileMojo mojo, Project antProject, String os, String prefix, String type ) throws MojoFailureException, MojoExecutionException { @@ -195,8 +237,9 @@ public class Linker try { List cSrcDirs = mojo.getC().getSourceDirectories(); - for (Iterator i = cSrcDirs.iterator(); i.hasNext(); ) { - File dir = (File)i.next(); + for ( Iterator i = cSrcDirs.iterator(); i.hasNext(); ) + { + File dir = (File) i.next(); if ( dir.exists() ) defs.addAll( FileUtils.getFiles( dir, "**/*.def", null ) ); } @@ -207,8 +250,9 @@ public class Linker try { List cppSrcDirs = mojo.getCpp().getSourceDirectories(); - for (Iterator i = cppSrcDirs.iterator(); i.hasNext(); ) { - File dir = (File)i.next(); + for ( Iterator i = cppSrcDirs.iterator(); i.hasNext(); ) + { + File dir = (File) i.next(); if ( dir.exists() ) defs.addAll( FileUtils.getFiles( dir, "**/*.def", null ) ); } @@ -219,8 +263,9 @@ public class Linker try { List fortranSrcDirs = mojo.getFortran().getSourceDirectories(); - for (Iterator i = fortranSrcDirs.iterator(); i.hasNext(); ) { - File dir = (File)i.next(); + for ( Iterator i = fortranSrcDirs.iterator(); i.hasNext(); ) + { + File dir = (File) i.next(); if ( dir.exists() ) defs.addAll( FileUtils.getFiles( dir, "**/*.def", null ) ); } diff --git a/src/main/java/org/apache/maven/plugin/nar/NarUtil.java b/src/main/java/org/apache/maven/plugin/nar/NarUtil.java index 07e8212..b663d95 100644 --- a/src/main/java/org/apache/maven/plugin/nar/NarUtil.java +++ b/src/main/java/org/apache/maven/plugin/nar/NarUtil.java @@ -20,10 +20,13 @@ package org.apache.maven.plugin.nar; */ import java.io.BufferedReader; +import java.io.BufferedWriter; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.io.PrintStream; +import java.io.Writer; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -406,49 +409,76 @@ public class NarUtil return pathName + "=" + value; } - public static int runCommand( String cmd, String[] args, File workingDirectory, String[] env, Log log ) + public static int runCommand( String cmd, String[] args, File workingDirectory, String[] env, final Log log ) + throws MojoExecutionException, MojoFailureException + { + return runCommand( cmd, args, workingDirectory, env, new TextStream() + { + public void println( String text ) + { + log.info( text ); + } + }, new TextStream() + { + public void println( String text ) + { + log.error( text ); + } + + }, new TextStream() + { + public void println( String text ) + { + log.debug( text ); + } + } ); + } + + public static int runCommand( String cmd, String[] args, File workingDirectory, String[] env, TextStream out, + TextStream err, TextStream dbg ) throws MojoExecutionException, MojoFailureException { Commandline cmdLine = new Commandline(); - + try { - log.debug( "RunCommand: " + cmd ); + dbg.println( "RunCommand: " + cmd ); cmdLine.setExecutable( cmd ); if ( args != null ) { for ( int i = 0; i < args.length; i++ ) { - log.debug( " '" + args[i] + "'" ); + dbg.println( " '" + args[i] + "'" ); } cmdLine.addArguments( args ); } if ( workingDirectory != null ) { - log.debug( "in: " + workingDirectory.getPath() ); + dbg.println( "in: " + workingDirectory.getPath() ); cmdLine.setWorkingDirectory( workingDirectory ); } if ( env != null ) { - log.debug( "with Env:" ); + dbg.println( "with Env:" ); for ( int i = 0; i < env.length; i++ ) { String[] nameValue = env[i].split( "=", 2 ); if ( nameValue.length < 2 ) throw new MojoFailureException( " Misformed env: '" + env[i] + "'" ); - log.debug( " '" + nameValue[0] + "=" + nameValue[1] + "'" ); + dbg.println( " '" + nameValue[0] + "=" + nameValue[1] + "'" ); cmdLine.addEnvironment( nameValue[0], nameValue[1] ); } } Process process = cmdLine.execute(); - StreamGobbler errorGobbler = new StreamGobbler( process.getErrorStream(), true, log ); - StreamGobbler outputGobbler = new StreamGobbler( process.getInputStream(), false, log ); + StreamGobbler errorGobbler = new StreamGobbler( process.getErrorStream(), err ); + StreamGobbler outputGobbler = new StreamGobbler( process.getInputStream(), out ); errorGobbler.start(); outputGobbler.start(); process.waitFor(); + dbg.println( "ExitValue: " + process.exitValue() ); return process.exitValue(); } catch ( Throwable e ) @@ -457,21 +487,17 @@ public class NarUtil } } - static class StreamGobbler extends Thread { InputStream is; - boolean error; - - Log log; + TextStream ts; - StreamGobbler( InputStream is, boolean error, Log log ) + StreamGobbler( InputStream is, TextStream ts ) { this.is = is; - this.error = error; - this.log = log; + this.ts = ts; } public void run() @@ -482,14 +508,7 @@ public class NarUtil String line = null; while ( ( line = reader.readLine() ) != null ) { - if ( error ) - { - log.error( line ); - } - else - { - log.debug( line ); - } + ts.println( line ); } reader.close(); } diff --git a/src/main/java/org/apache/maven/plugin/nar/StringTextStream.java b/src/main/java/org/apache/maven/plugin/nar/StringTextStream.java new file mode 100644 index 0000000..5bc3b15 --- /dev/null +++ b/src/main/java/org/apache/maven/plugin/nar/StringTextStream.java @@ -0,0 +1,56 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.maven.plugin.nar; + +/** + * Stream to write to a string. + * + * @author Mark Donszelmann (Mark.Donszelmann@gmail.com) + * @version $Id$ + */ +public class StringTextStream + implements TextStream +{ + private StringBuffer sb; + + private String lineSeparator; + + public StringTextStream() + { + sb = new StringBuffer(); + lineSeparator = System.getProperty( "line.separator" ); + assert ( lineSeparator != null ); + } + + /* + * (non-Javadoc) + * @see org.apache.maven.plugin.nar.TextStream#println(java.lang.String) + */ + public void println( String text ) + { + sb.append( text ); + sb.append( lineSeparator ); + } + + public String toString() + { + return sb.toString(); + } +} diff --git a/src/main/java/org/apache/maven/plugin/nar/TextStream.java b/src/main/java/org/apache/maven/plugin/nar/TextStream.java new file mode 100644 index 0000000..4f3950c --- /dev/null +++ b/src/main/java/org/apache/maven/plugin/nar/TextStream.java @@ -0,0 +1,31 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.maven.plugin.nar; + +/** + * Interface to write text into a stream + * + * @author Mark Donszelmann (Mark.Donszelmann@gmail.com) + * @version $Id$ + */ +public interface TextStream +{ + public void println(String text); +} -- cgit v1.2.3