From 6148bfadb439d4bc302bfbf3f1d88147917343fa Mon Sep 17 00:00:00 2001 From: Rui Maranhao Date: Mon, 13 Oct 2014 13:05:21 -0700 Subject: [PATCH 1/2] allow compilation of files with same names --- .../maven_nar/cpptasks/TargetMatcher.java | 64 +++++++++---------- .../cpptasks/compiler/AbstractCompiler.java | 7 +- .../compiler/CommandLineCompiler.java | 43 +++++++------ .../CommandLineCompilerConfiguration.java | 17 +++-- 4 files changed, 68 insertions(+), 63 deletions(-) diff --git a/src/main/java/com/github/maven_nar/cpptasks/TargetMatcher.java b/src/main/java/com/github/maven_nar/cpptasks/TargetMatcher.java index a5e2d5643..8fde82d5b 100644 --- a/src/main/java/com/github/maven_nar/cpptasks/TargetMatcher.java +++ b/src/main/java/com/github/maven_nar/cpptasks/TargetMatcher.java @@ -7,9 +7,9 @@ * Licensed 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. @@ -55,6 +55,7 @@ public TargetMatcher(CCTask task, File outputDir, } public void visit(File parentDir, String filename) throws BuildException { File fullPath = new File(parentDir, filename); + int hash = Math.abs((parentDir + System.getProperty("file.separator") + filename).hashCode()); // // see if any processor wants to bid // on this one @@ -84,41 +85,36 @@ public void visit(File parentDir, String filename) throws BuildException { } } } else { - // - // get output file name - // - String[] outputFileNames = selectedCompiler - .getOutputFileNames(filename, versionInfo); - sourceFiles[0] = fullPath; - // - // if there is some output for this task - // (that is a source file and not an header file) - // - for (int i = 0; i < outputFileNames.length; i++) { // - // see if the same output file has already been registered + // get output file name + // + String[] outputFileNames = selectedCompiler + .getOutputFileNames(filename, versionInfo); + sourceFiles[0] = fullPath; + + // + // if there is some output for this task + // (that is a source file and not an header file) // - TargetInfo previousTarget = (TargetInfo) targets - .get(outputFileNames[i]); - if (previousTarget == null) { - targets.put(outputFileNames[i], new TargetInfo( + for (int i = 0; i < outputFileNames.length; i++) { + // + // see if the same output file has already been registered + // + TargetInfo previousTarget = (TargetInfo) targets + .get(outputFileNames[i]); + + String ext = outputFileNames[i].substring(outputFileNames[i].lastIndexOf('.'),outputFileNames[i].length()); + StringBuffer nf = new StringBuffer(outputFileNames[i].substring(0, outputFileNames[i].lastIndexOf('.'))); + nf.append(hash + ext); + + outputFileNames[i] = nf.toString(); + + targets.put(outputFileNames[i], new TargetInfo( selectedCompiler, sourceFiles, null, new File( - outputDir, outputFileNames[i]), - selectedCompiler.getRebuild())); - } else { - if (!previousTarget.getSources()[0].equals(sourceFiles[0])) { - StringBuffer builder = new StringBuffer( - "Output filename conflict: "); - builder.append(outputFileNames[i]); - builder.append(" would be produced from "); - builder.append(previousTarget.getSources()[0] - .toString()); - builder.append(" and "); - builder.append(filename); - throw new BuildException(builder.toString()); - } - } - } + outputDir, outputFileNames[i]), + selectedCompiler.getRebuild())); + + } } } } diff --git a/src/main/java/com/github/maven_nar/cpptasks/compiler/AbstractCompiler.java b/src/main/java/com/github/maven_nar/cpptasks/compiler/AbstractCompiler.java index c0578c950..385424660 100644 --- a/src/main/java/com/github/maven_nar/cpptasks/compiler/AbstractCompiler.java +++ b/src/main/java/com/github/maven_nar/cpptasks/compiler/AbstractCompiler.java @@ -7,9 +7,9 @@ * Licensed 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. @@ -208,4 +208,7 @@ protected boolean resolveInclude(String includeName, File[] includePath, } return false; } + public String getOutputSuffix() { + return outputSuffix; + } } diff --git a/src/main/java/com/github/maven_nar/cpptasks/compiler/CommandLineCompiler.java b/src/main/java/com/github/maven_nar/cpptasks/compiler/CommandLineCompiler.java index f0ce4fd46..f226d1340 100644 --- a/src/main/java/com/github/maven_nar/cpptasks/compiler/CommandLineCompiler.java +++ b/src/main/java/com/github/maven_nar/cpptasks/compiler/CommandLineCompiler.java @@ -7,9 +7,9 @@ * Licensed 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. @@ -22,6 +22,7 @@ import java.io.IOException; import java.util.Enumeration; import java.util.Vector; +import java.util.ArrayList; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.types.Environment; @@ -39,7 +40,7 @@ /** * An abstract Compiler implementation which uses an external program to * perform the compile. - * + * * @author Adam Murdoch */ public abstract class CommandLineCompiler extends AbstractCompiler { @@ -73,11 +74,11 @@ abstract protected void addImpliedArgs(Vector args, boolean debug, Boolean rtti, OptimizationEnum optimization); /** * Adds command-line arguments for include directories. - * + * * If relativeArgs is not null will add corresponding relative paths * include switches to that vector (for use in building a configuration * identifier that is consistent between machines). - * + * * @param baseDirPath Base directory path. * @param includeDirs * Array of include directory paths @@ -134,7 +135,7 @@ protected void buildDefineArguments(CompilerDef[] defs, Vector args) { } /** * Compiles a source file. - * + * */ public void compile(CCTask task, File outputDir, String[] sourceFiles, String[] args, String[] endArgs, boolean relentless, @@ -184,25 +185,27 @@ public void compile(CCTask task, File outputDir, String[] sourceFiles, if (libtool) { argCount++; } - String[] commandline = new String[argCount]; + ArrayList commandline = new ArrayList(); int index = 0; if (libtool) { - commandline[index++] = "libtool"; + commandline.add("libtool"); } - commandline[index++] = command; + commandline.add(command); for (int j = 0; j < args.length; j++) { - commandline[index++] = args[j]; + commandline.add(args[j]); } for (int j = sourceIndex; j < firstFileNextExec; j++) { for (int k = 0; k < argumentCountPerInputFile; k++) { - commandline[index++] = getInputFileArgument(outputDir, - sourceFiles[j], k); + commandline.add(getInputFileArgument(outputDir, sourceFiles[j], k)); } } for (int j = 0; j < endArgs.length; j++) { - commandline[index++] = endArgs[j]; + commandline.add(endArgs[j]); } - int retval = runCommand(task, outputDir, commandline); + commandline.add("-o"); + File ff = new File(sourceFiles[sourceIndex]); + commandline.add(ff.getName().replaceFirst("[.][^.]+$", "") + Math.abs(sourceFiles[sourceIndex].hashCode()) + config.getOutputSuffix()); + int retval = runCommand(task, outputDir, commandline.toArray(new String[commandline.size()])); if (monitor != null) { String[] fileNames = new String[firstFileNextExec - sourceIndex]; @@ -240,8 +243,8 @@ public void compile(CCTask task, File outputDir, String[] sourceFiles, } } protected CompilerConfiguration createConfiguration(final CCTask task, - final LinkType linkType, - final ProcessorDef[] baseDefs, + final LinkType linkType, + final ProcessorDef[] baseDefs, final CompilerDef specificDef, final TargetDef targetPlatform, final VersionInfo versionInfo) { @@ -417,17 +420,17 @@ public String getIdentifier() { return identifier; } abstract protected String getIncludeDirSwitch(String source); - + /** * Added by Darren Sargent 22Oct2008 Returns the include dir switch value. * Default implementation doesn't treat system includes specially, for * compilers which don't care. - * + * * @param source * the given source value. * @param isSystem * "true" if this is a system include path - * + * * @return the include dir switch value. */ protected String getIncludeDirSwitch(String source, boolean isSystem) { @@ -451,7 +454,7 @@ protected final boolean getLibtool() { } /** * Obtains the same compiler, but with libtool set - * + * * Default behavior is to ignore libtool */ public final CommandLineCompiler getLibtoolCompiler() { diff --git a/src/main/java/com/github/maven_nar/cpptasks/compiler/CommandLineCompilerConfiguration.java b/src/main/java/com/github/maven_nar/cpptasks/compiler/CommandLineCompilerConfiguration.java index 5cd8c2b05..2f6927e76 100644 --- a/src/main/java/com/github/maven_nar/cpptasks/compiler/CommandLineCompilerConfiguration.java +++ b/src/main/java/com/github/maven_nar/cpptasks/compiler/CommandLineCompilerConfiguration.java @@ -7,9 +7,9 @@ * Licensed 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. @@ -30,7 +30,7 @@ import com.github.maven_nar.cpptasks.VersionInfo; /** * A configuration for a C++ compiler - * + * * @author Curt Arnold */ public final class CommandLineCompilerConfiguration @@ -158,23 +158,23 @@ public void compile(CCTask task, File outputDir, String[] sourceFiles, } } /** - * + * * This method may be used to get two distinct compiler configurations, one * for compiling the specified file and producing a precompiled header * file, and a second for compiling other files using the precompiled * header file. - * + * * The last (preferrably only) include directive in the prototype file will * be used to mark the boundary between pre-compiled and normally compiled * headers. - * + * * @param prototype * A source file (for example, stdafx.cpp) that is used to build * the precompiled header file. @returns null if precompiled * headers are not supported or a two element array containing * the precompiled header generation configuration and the * consuming configuration - * + * */ public CompilerConfiguration[] createPrecompileConfigurations( File prototype, String[] nonPrecompiledFiles) { @@ -244,4 +244,7 @@ public final void setCommandPath(String commandPath) { public final String getCommandPath() { return this.commandPath; } + public String getOutputSuffix() { + return compiler.getOutputSuffix(); + } } From c83125b0be134eaf6bd30460bee16af7422b3d07 Mon Sep 17 00:00:00 2001 From: Rui Maranhao Date: Mon, 13 Oct 2014 15:59:15 -0700 Subject: [PATCH 2/2] change how hash code is computed --- src/main/java/com/github/maven_nar/cpptasks/TargetMatcher.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/github/maven_nar/cpptasks/TargetMatcher.java b/src/main/java/com/github/maven_nar/cpptasks/TargetMatcher.java index 8fde82d5b..8bf152304 100644 --- a/src/main/java/com/github/maven_nar/cpptasks/TargetMatcher.java +++ b/src/main/java/com/github/maven_nar/cpptasks/TargetMatcher.java @@ -55,7 +55,7 @@ public TargetMatcher(CCTask task, File outputDir, } public void visit(File parentDir, String filename) throws BuildException { File fullPath = new File(parentDir, filename); - int hash = Math.abs((parentDir + System.getProperty("file.separator") + filename).hashCode()); + int hash = Math.abs(fullPath.getPath().hashCode()); // // see if any processor wants to bid // on this one