diff options
Diffstat (limited to 'src/main/java/net/sf/antcontrib/cpptasks/devstudio/VisualStudioNETProjectWriter.java')
-rw-r--r-- | src/main/java/net/sf/antcontrib/cpptasks/devstudio/VisualStudioNETProjectWriter.java | 236 |
1 files changed, 123 insertions, 113 deletions
diff --git a/src/main/java/net/sf/antcontrib/cpptasks/devstudio/VisualStudioNETProjectWriter.java b/src/main/java/net/sf/antcontrib/cpptasks/devstudio/VisualStudioNETProjectWriter.java index 0afeebc..f554e09 100644 --- a/src/main/java/net/sf/antcontrib/cpptasks/devstudio/VisualStudioNETProjectWriter.java +++ b/src/main/java/net/sf/antcontrib/cpptasks/devstudio/VisualStudioNETProjectWriter.java @@ -25,8 +25,8 @@ import net.sf.antcontrib.cpptasks.compiler.CommandLineLinkerConfiguration; import net.sf.antcontrib.cpptasks.ide.ProjectDef; import net.sf.antcontrib.cpptasks.ide.CommentDef; import net.sf.antcontrib.cpptasks.ide.ProjectWriter; +import net.sf.antcontrib.cpptasks.ide.DependencyDef; import org.apache.tools.ant.BuildException; -import org.apache.tools.ant.util.StringUtils; import org.apache.xml.serialize.OutputFormat; import org.apache.xml.serialize.XMLSerializer; import org.xml.sax.ContentHandler; @@ -91,17 +91,6 @@ public final class VisualStudioNETProjectWriter } - /** - * Get configuration name. - * @param task cc task, may not be null. - * @return configuration name. - */ - private String getConfigurationName(final CCTask task) { - if (task.getDebug()) { - return "Debug|Win32"; - } - return "Release|Win32"; - } /** * Gets the configuration type. @@ -121,31 +110,6 @@ public final class VisualStudioNETProjectWriter } - /** - * Get output directory. - * @param basePath path to parent of project file. - * @param task cc task, may not be null. - * @return output directory relative path. - */ - private String getOutputDirectory(final String basePath, - final CCTask task) { - File outFile = task.getOutfile(); - File buildDir = outFile.getParentFile(); - return CUtil.toWindowsPath(CUtil.getRelativePath(basePath, buildDir)); - } - - /** - * Get object file directory. - * @param basePath path to parent of project file. - * @param task cc task, may not be null. - * @return object file directory relative path. - */ - private String getIntermediateDirectory(final String basePath, - final CCTask task) { - File objDir = task.getObjdir(); - return CUtil.toWindowsPath(CUtil.getRelativePath(basePath, objDir)); - } - /** * Get character set for Windows API. @@ -170,23 +134,27 @@ public final class VisualStudioNETProjectWriter /** * Write the start tag of the Configuration element. * @param content serialization content handler. - * @param basePath path of directory containing project file. + * @param isDebug if true, write a debug configuration. * @param task cc task. * @param compilerConfig compiler configuration. * @throws SAXException thrown if serialization error. */ private void writeConfigurationStartTag(final ContentHandler content, - final String basePath, + final boolean isDebug, final CCTask task, final CommandLineCompilerConfiguration compilerConfig) throws SAXException { AttributesImpl attributes = new AttributesImpl(); - addAttribute(attributes, "Name", - getConfigurationName(task)); - addAttribute(attributes, "OutputDirectory", - getOutputDirectory(basePath, task)); - addAttribute(attributes, "IntermediateDirectory", - getIntermediateDirectory(basePath, task)); + if (isDebug) { + addAttribute(attributes, "Name", "Debug|Win32"); + addAttribute(attributes, "OutputDirectory", "Debug"); + addAttribute(attributes, "IntermediateDirectory", "Debug"); + } else { + addAttribute(attributes, "Name", "Release|Win32"); + addAttribute(attributes, "OutputDirectory", "Release"); + addAttribute(attributes, "IntermediateDirectory", "Release"); + + } addAttribute(attributes, "ConfigurationType", getConfigurationType(task)); addAttribute(attributes, "CharacterSet", @@ -224,6 +192,7 @@ public final class VisualStudioNETProjectWriter /** * Get value of AdditionalIncludeDirectories property. * @param compilerConfig compiler configuration. + * @param baseDir base for relative paths. * @return value of AdditionalIncludeDirectories property. */ private String getAdditionalIncludeDirectories( @@ -247,15 +216,27 @@ public final class VisualStudioNETProjectWriter /** * Get value of PreprocessorDefinitions property. * @param compilerConfig compiler configuration. + * @param isDebug true if generating debug configuration. * @return value of PreprocessorDefinitions property. */ private String getPreprocessorDefinitions( - final CommandLineCompilerConfiguration compilerConfig) { + final CommandLineCompilerConfiguration compilerConfig, + final boolean isDebug) { StringBuffer defines = new StringBuffer(); String[] args = compilerConfig.getPreArguments(); for (int i = 0; i < args.length; i++) { if (args[i].startsWith("/D")) { - defines.append(args[i].substring(2)); + String macro = args[i].substring(2); + if (isDebug) { + if (macro.equals("NDEBUG")) { + macro = "_DEBUG"; + } + } else { + if (macro.equals("_DEBUG")) { + macro = "NDEBUG"; + } + } + defines.append(macro); defines.append(";"); } } @@ -269,24 +250,27 @@ public final class VisualStudioNETProjectWriter /** * Get value of RuntimeLibrary property. * @param compilerConfig compiler configuration. + * @param isDebug true if generating debug configuration. * @return value of RuntimeLibrary property. */ private String getRuntimeLibrary( - final CommandLineCompilerConfiguration compilerConfig) { + final CommandLineCompilerConfiguration compilerConfig, + final boolean isDebug) { String rtl = null; String[] args = compilerConfig.getPreArguments(); for (int i = 0; i < args.length; i++) { - if ("/MT".equals(args[i])) { - rtl = "0"; - } - if ("/MTd".equals(args[i])) { - rtl = "1"; - } - if ("/MD".equals(args[i])) { - rtl = "2"; - } - if ("/MDd".equals(args[i])) { - rtl = "3"; + if (args[i].startsWith("/MT")) { + if (isDebug) { + rtl = "1"; + } else { + rtl = "0"; + } + } else if (args[i].startsWith("/MD")) { + if (isDebug) { + rtl = "3"; + } else { + rtl = "2"; + } } } return rtl; @@ -330,15 +314,6 @@ public final class VisualStudioNETProjectWriter } - /** - * Get value of MinimalRebuild property. - * @param compilerConfig compiler configuration. - * @return value of MinimalRebuild property. - */ - private String getMinimalRebuild( - final CommandLineCompilerConfiguration compilerConfig) { - return trueLiteral; - } /** * Get value of BasicRuntimeChecks property. @@ -419,6 +394,9 @@ public final class VisualStudioNETProjectWriter if ("/Z7".equals(args[i])) { format = "1"; } + if ("/Zd".equals(args[i])) { + format = "2"; + } if ("/Zi".equals(args[i])) { format = "3"; } @@ -432,27 +410,41 @@ public final class VisualStudioNETProjectWriter /** * write the Compiler element. * @param content serialization content handler. + * @param isDebug true if generating debug configuration. + * @param basePath base for relative file paths. * @param compilerConfig compiler configuration. * @throws SAXException thrown if error during serialization. */ private void writeCompilerElement(final ContentHandler content, + final boolean isDebug, final String basePath, final CommandLineCompilerConfiguration compilerConfig) throws SAXException { AttributesImpl attributes = new AttributesImpl(); addAttribute(attributes, "Name", "VCCLCompilerTool"); - addAttribute(attributes, "Optimization", - getOptimization(compilerConfig)); + String optimization = getOptimization(compilerConfig); + String debugFormat = getDebugInformationFormat(compilerConfig); + if(isDebug) { + optimization = "0"; + if ("0".equals(debugFormat)) { + debugFormat = "4"; + } + } else { + if ("0".equals(optimization)) { + optimization = "2"; + } + debugFormat = "0"; + } + addAttribute(attributes, "Optimization", optimization); addAttribute(attributes, "AdditionalIncludeDirectories", getAdditionalIncludeDirectories(basePath, compilerConfig)); addAttribute(attributes, "PreprocessorDefinitions", - getPreprocessorDefinitions(compilerConfig)); - addAttribute(attributes, "MinimalRebuild", - getMinimalRebuild(compilerConfig)); + getPreprocessorDefinitions(compilerConfig, isDebug)); + addAttribute(attributes, "MinimalRebuild", trueLiteral); addAttribute(attributes, "BasicRuntimeChecks", getBasicRuntimeChecks(compilerConfig)); addAttribute(attributes, "RuntimeLibrary", - getRuntimeLibrary(compilerConfig)); + getRuntimeLibrary(compilerConfig, isDebug)); addAttribute(attributes, "UsePrecompiledHeader", getUsePrecompiledHeader(compilerConfig)); addAttribute(attributes, "PrecompiledHeaderFile", @@ -462,7 +454,7 @@ public final class VisualStudioNETProjectWriter addAttribute(attributes, "Detect64BitPortabilityProblems", getDetect64BitPortabilityProblems(compilerConfig)); addAttribute(attributes, "DebugInformationFormat", - getDebugInformationFormat(compilerConfig)); + debugFormat); content.startElement(null, "Tool", "Tool", attributes); content.endElement(null, "Tool", "Tool"); @@ -489,22 +481,6 @@ public final class VisualStudioNETProjectWriter return incremental; } - /** - * Get value of GenerateDebugInformation property. - * @param linkerConfig linker configuration. - * @return value of GenerateDebugInformation property - */ - private String getGenerateDebugInformation( - final CommandLineLinkerConfiguration linkerConfig) { - String debug = falseLiteral; - String[] args = linkerConfig.getPreArguments(); - for (int i = 0; i < args.length; i++) { - if ("/DEBUG".equals(args[i])) { - debug = trueLiteral; - } - } - return debug; - } /** * Get value of Subsystem property. @@ -549,11 +525,13 @@ public final class VisualStudioNETProjectWriter /** * Get value of AdditionalDependencies property. * @param linkTarget link target. + * @param projectDependencies dependencies declared in project. * @param targets all targets. * @param basePath path to directory containing project file. * @return value of AdditionalDependencies property. */ private String getAdditionalDependencies(final TargetInfo linkTarget, + final List projectDependencies, final Map targets, final String basePath) { String dependencies = null; @@ -570,21 +548,37 @@ public final class VisualStudioNETProjectWriter // otherwise construct a relative path. // String relPath = linkSources[i].getName(); - if (!CUtil.isSystemPath(linkSources[i])) { - relPath = CUtil.getRelativePath(basePath, linkSources[i]); + // + // check if file comes from a project dependency + // if it does it should not be explicitly linked + boolean fromDependency = false; + if (relPath.indexOf(".") > 0) { + String baseName = relPath.substring(0, relPath.indexOf(".")); + for(Iterator iter = projectDependencies.iterator(); iter.hasNext(); ) { + DependencyDef depend = (DependencyDef) iter.next(); + if (baseName.compareToIgnoreCase(depend.getName()) == 0) { + fromDependency = true; + } + } + } + + if (!fromDependency) { + if (!CUtil.isSystemPath(linkSources[i])) { + relPath = CUtil.getRelativePath(basePath, linkSources[i]); + } + // + // if path has an embedded space then + // must quote + if (relPath.indexOf(' ') > 0) { + buf.append('\"'); + buf.append(CUtil.toWindowsPath(relPath)); + buf.append('\"'); + } else { + buf.append(relPath); + } + buf.append(' '); + } } - // - // if path has an embedded space then - // must quote - if (relPath.indexOf(' ') > 0) { - buf.append('\"'); - buf.append(CUtil.toWindowsPath(relPath)); - buf.append('\"'); - } else { - buf.append(relPath); - } - buf.append(' '); - } } if (buf.length() > 0) { buf.setLength(buf.length() - 1); @@ -597,12 +591,16 @@ public final class VisualStudioNETProjectWriter /** * Write Tool element for linker. * @param content serialization content handler. + * @param isDebug true if generating debug configuration. + * @param dependencies project dependencies. * @param basePath path to directory containing project file. * @param linkTarget link target. * @param targets all targets. * @throws SAXException thrown if error during serialization. */ private void writeLinkerElement(final ContentHandler content, + final boolean isDebug, + final List dependencies, final String basePath, final TargetInfo linkTarget, final Map targets) throws SAXException { @@ -616,8 +614,11 @@ public final class VisualStudioNETProjectWriter if (linkerConfig.getLinker() instanceof DevStudioCompatibleLinker) { addAttribute(attributes, "LinkIncremental", getLinkIncremental(linkerConfig)); - addAttribute(attributes, "GenerateDebugInformation", - getGenerateDebugInformation(linkerConfig)); + if (isDebug) { + addAttribute(attributes, "GenerateDebugInformation", trueLiteral); + } else { + addAttribute(attributes, "GenerateDebugInformation", falseLiteral); + } addAttribute(attributes, "SubSystem", getSubsystem(linkerConfig)); addAttribute(attributes, "TargetMachine", @@ -625,7 +626,7 @@ public final class VisualStudioNETProjectWriter } } addAttribute(attributes, "AdditionalDependencies", - getAdditionalDependencies(linkTarget, targets, basePath)); + getAdditionalDependencies(linkTarget, dependencies, targets, basePath)); content.startElement(null, "Tool", "Tool", attributes); content.endElement(null, "Tool", "Tool"); } @@ -706,13 +707,22 @@ public final class VisualStudioNETProjectWriter content.startElement(null, "Configurations", "Configurations", emptyAttrs); - writeConfigurationStartTag(content, basePath, task, compilerConfig); - - writeCompilerElement(content, basePath, compilerConfig); - - writeLinkerElement(content, basePath, linkTarget, targets); + // + // write debug configuration + // + writeConfigurationStartTag(content, true, task, compilerConfig); + writeCompilerElement(content, true, basePath, compilerConfig); + writeLinkerElement(content, true, projectDef.getDependencies(), basePath, linkTarget, targets); + content.endElement(null, "Configuration", "Configuration"); + // + // write release configuration + // + writeConfigurationStartTag(content, false, task, compilerConfig); + writeCompilerElement(content, false, basePath, compilerConfig); + writeLinkerElement(content, false, projectDef.getDependencies(), basePath, linkTarget, targets); content.endElement(null, "Configuration", "Configuration"); + content.endElement(null, "Configurations", "Configurations"); content.startElement(null, "References", "References", emptyAttrs); content.endElement(null, "References", "References"); |