SF.net SVN: ant-contrib: [148] cpptasks/trunk/src/main/java/net/sf/ antcontrib/cpptasks

[email protected] Sun, 03 Feb 2008 13:50:19 -0800
Newsgroups gmane.comp.java.ant-contrib.devel
Message-ID <[email protected]>
Revision: 148
          http://ant-contrib.svn.sourceforge.net/ant-contrib/?rev=148&view=rev
Author:   carnold
Date:     2008-02-03 13:50:19 -0800 (Sun, 03 Feb 2008)

Log Message:
-----------
Bug 980130: Add dependency support to project generation (VS 6 initially)

Modified Paths:
--------------
    cpptasks/trunk/src/main/java/net/sf/antcontrib/cpptasks/devstudio/DevStudioProjectWriter.java
    cpptasks/trunk/src/main/java/net/sf/antcontrib/cpptasks/ide/ProjectDef.java

Added Paths:
-----------
    cpptasks/trunk/src/main/java/net/sf/antcontrib/cpptasks/ide/DependencyDef.java

Modified: cpptasks/trunk/src/main/java/net/sf/antcontrib/cpptasks/devstudio/DevStudioProjectWriter.java
===================================================================
--- cpptasks/trunk/src/main/java/net/sf/antcontrib/cpptasks/devstudio/DevStudioProjectWriter.java	2008-01-30 05:19:46 UTC (rev 147)
+++ cpptasks/trunk/src/main/java/net/sf/antcontrib/cpptasks/devstudio/DevStudioProjectWriter.java	2008-02-03 21:50:19 UTC (rev 148)
@@ -16,27 +16,31 @@
  */
 package net.sf.antcontrib.cpptasks.devstudio;
 
+import net.sf.antcontrib.cpptasks.CCTask;
+import net.sf.antcontrib.cpptasks.CUtil;
+import net.sf.antcontrib.cpptasks.TargetInfo;
+import net.sf.antcontrib.cpptasks.compiler.CommandLineCompilerConfiguration;
+import net.sf.antcontrib.cpptasks.compiler.CommandLineLinkerConfiguration;
+import net.sf.antcontrib.cpptasks.compiler.ProcessorConfiguration;
+import net.sf.antcontrib.cpptasks.ide.DependencyDef;
+import net.sf.antcontrib.cpptasks.ide.ProjectDef;
+import net.sf.antcontrib.cpptasks.ide.ProjectWriter;
+import org.apache.tools.ant.BuildException;
+import org.apache.tools.ant.util.StringUtils;
+
 import java.io.BufferedWriter;
 import java.io.File;
 import java.io.FileWriter;
 import java.io.IOException;
 import java.io.Writer;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Comparator;
 import java.util.Hashtable;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Vector;
 
-import net.sf.antcontrib.cpptasks.CCTask;
-import net.sf.antcontrib.cpptasks.CUtil;
-import net.sf.antcontrib.cpptasks.TargetInfo;
-import net.sf.antcontrib.cpptasks.compiler.CommandLineCompilerConfiguration;
-import net.sf.antcontrib.cpptasks.compiler.CommandLineLinkerConfiguration;
-import net.sf.antcontrib.cpptasks.compiler.ProcessorConfiguration;
-import net.sf.antcontrib.cpptasks.ide.ProjectDef;
-import net.sf.antcontrib.cpptasks.ide.ProjectWriter;
-import org.apache.tools.ant.BuildException;
-
 /**
  * Writes a Microsoft Visual Studio 97 or Visual Studio 6 project file.
  *
@@ -60,6 +64,24 @@
     this.version = versionArg;
   }
 
+  private static String toProjectName(final String name) {
+      //
+      //    some characters are apparently not allowed in VS project names
+      //       but have not been able to find them documented
+      //       limiting characters to alphas, numerics and hyphens
+      StringBuffer projectNameBuf = new StringBuffer(name);
+      for (int i = 0; i < projectNameBuf.length(); i++) {
+        final char ch = projectNameBuf.charAt(i);
+        if (!((ch >= 'a' && ch <= 'z')
+               || (ch >= 'A' && ch <= 'Z')
+               || (ch >= '0' && ch <= '9'))) {
+          projectNameBuf.setCharAt(i, '_');
+        }
+      }
+      return projectNameBuf.toString();
+
+  }
+
   /**
    *  Writes a project definition file.
    * @param fileName File name base, writer may append appropriate extension
@@ -81,22 +103,12 @@
     //    some characters are apparently not allowed in VS project names
     //       but have not been able to find them documented
     //       limiting characters to alphas, numerics and hyphens
-    StringBuffer projectNameBuf;
     String projectName = projectDef.getName();
     if (projectName != null) {
-      projectNameBuf = new StringBuffer(projectName);
+      projectName = toProjectName(projectName);
     } else {
-      projectNameBuf = new StringBuffer(fileName.getName());
+      projectName = toProjectName(fileName.getName());
     }
-    for (int i = 0; i < projectNameBuf.length(); i++) {
-      final char ch = projectNameBuf.charAt(i);
-      if (!((ch >= 'a' && ch <= 'z')
-             || (ch >= 'A' && ch <= 'Z')
-             || (ch >= '0' && ch <= '9'))) {
-        projectNameBuf.setCharAt(i, '_');
-      }
-    }
-    projectName = projectNameBuf.toString();
 
     final String basePath = fileName.getAbsoluteFile().getParent();
 
@@ -187,10 +199,10 @@
     String buildDirPath = CUtil.getRelativePath(basePath, buildDir);
 
     writer.write("# PROP BASE Output_Dir \"");
-    writer.write(buildDirPath);
+    writer.write(toWindowsPath(buildDirPath));
     writer.write("\"\r\n");
     writer.write("# PROP BASE Intermediate_Dir \"");
-    writer.write(objDirPath);
+    writer.write(toWindowsPath(objDirPath));
     writer.write("\"\r\n");
     writer.write("# PROP BASE Target_Dir \"\"\r\n");
     writer.write("# PROP Use_MFC 0\r\n");
@@ -201,10 +213,10 @@
       writer.write("0\r\n");
     }
     writer.write("# PROP Output_Dir \"");
-    writer.write(buildDirPath);
+    writer.write(toWindowsPath(buildDirPath));
     writer.write("\"\r\n");
     writer.write("# PROP Intermediate_Dir \"");
-    writer.write(objDirPath);
+    writer.write(toWindowsPath(objDirPath));
     writer.write("\"\r\n");
     writer.write("# PROP Target_Dir \"\"\r\n");
     writeCompileOptions(writer, basePath, compilerConfig);
@@ -277,31 +289,72 @@
     //    write workspace file
     //
     writer = new BufferedWriter(new FileWriter(dswFile));
+    writeWorkspace(writer, projectDef, projectName, dspFile);
+    writer.close();
 
-    writer.write("Microsoft Developer Studio Workspace File, Format Version ");
-    writer.write(version);
-    writer.write("\r\n");
-    writer.write("# WARNING: DO NOT EDIT OR DELETE");
-    writer.write(" THIS WORKSPACE FILE!\r\n\r\n");
+  }
 
-    writer.write("############################################");
-    writer.write("###################################\r\n\r\n");
-    writer.write("Project: \"" + projectName + "\"=.\\"
-                 + dspFile.getName()
-                 + " - Package Owner=<4>\r\n\r\n");
+  private static void writeWorkspaceProject(final Writer writer,
+                                     final String projectName,
+                                     final String projectFile,
+                                     final List dependsOn) throws IOException {
+      writer.write("############################################");
+      writer.write("###################################\r\n\r\n");
+      writer.write("Project: \"" + projectName + "\"="
+                   + projectFile
+                   + " - Package Owner=<4>\r\n\r\n");
 
-    writer.write("Package=<5>\r\n{{{\r\n}}}\r\n\r\n");
-    writer.write("Package=<4>\r\n{{{\r\n}}}\r\n\r\n");
-    writer.write("######################################");
-    writer.write("#########################################\r\n\r\n");
+      writer.write("Package=<5>\r\n{{{\r\n}}}\r\n\r\n");
+      writer.write("Package=<4>\r\n{{{\r\n");
+      if (dependsOn != null) {
+        for(Iterator iter = dependsOn.iterator(); iter.hasNext();) {
+            writer.write("    Begin Project Dependency\r\n");
+            writer.write("    Project_Dep_name " + toProjectName(String.valueOf(iter.next())) + "\r\n");
+            writer.write("    End Project Dependency\r\n");
+        }
+      }
+      writer.write("}}}\r\n\r\n");
+      writer.write("######################################");
+      writer.write("#########################################\r\n\r\n");
 
-    writer.write("Global:\r\n\r\nPackage=<5>\r\n{{{\r\n}}}");
-    writer.write("\r\n\r\nPackage=<3>\r\n{{{\r\n}}}\r\n\r\n");
+  }
 
-    writer.write("########################################");
-    writer.write("#######################################\r\n\r\n");
-    writer.close();
+  private void writeWorkspace(final Writer writer,
+                              final ProjectDef project,
+                              final String projectName,
+                              final File dspFile) throws IOException {
 
+      writer.write("Microsoft Developer Studio Workspace File, Format Version ");
+      writer.write(version);
+      writer.write("\r\n");
+      writer.write("# WARNING: DO NOT EDIT OR DELETE");
+      writer.write(" THIS WORKSPACE FILE!\r\n\r\n");
+
+      List dependencies = project.getDependencies();
+      List projectDeps = new ArrayList();
+      String basePath = dspFile.getParent();
+      for(Iterator iter = dependencies.iterator(); iter.hasNext();) {
+          DependencyDef dep = (DependencyDef) iter.next();
+          if (dep.getFile() != null) {
+            String projName = toProjectName(dep.getName());
+            projectDeps.add(projName);
+            String depProject = toWindowsPath(
+                      CUtil.getRelativePath(basePath,
+                              new File(dep.getFile() + ".dsp")));
+            writeWorkspaceProject(writer, projName, depProject, dep.getDependsList());
+          }
+      }
+
+
+
+      writeWorkspaceProject(writer, projectName, ".\\" + dspFile.getName(), projectDeps);
+
+      writer.write("Global:\r\n\r\nPackage=<5>\r\n{{{\r\n}}}");
+      writer.write("\r\n\r\nPackage=<3>\r\n{{{\r\n}}}\r\n\r\n");
+
+      writer.write("########################################");
+      writer.write("#######################################\r\n\r\n");
+
   }
 
   /**
@@ -344,7 +397,7 @@
         && !relativePath.startsWith("\\")) {
       relativePath = ".\\" + relativePath;
     }
-    writer.write(relativePath);
+    writer.write(toWindowsPath(relativePath));
     writer.write("\r\n# End Source File\r\n");
   }
 
@@ -462,7 +515,7 @@
     for (int i = 0; i < includePath.length; i++) {
       options.append(" /I \"");
       String relPath = CUtil.getRelativePath(baseDir, includePath[i]);
-      options.append(relPath);
+      options.append(toWindowsPath(relPath));
       options.append('"');
     }
 
@@ -494,6 +547,29 @@
 
   }
 
+
+
+
+ /**
+  *  Determines if source file has a system path,
+  *    that is part of the compiler or platform.
+  *   @param source source, may not be null.
+  *   @return true is source file appears to be system library
+  *         and its path should be discarded.
+  */
+  private static boolean isSystemPath(final File source) {
+	  String lcPath = source.toString().toLowerCase(java.util.Locale.US);
+	  return lcPath.indexOf("platformsdk") != -1
+	      || lcPath.indexOf("microsoft") != -1;
+  }
+
+  private static String toWindowsPath(final String path) {
+      if (File.separatorChar != '\\' && path.indexOf(File.separatorChar) != -1) {
+          return StringUtils.replace(path, File.separator, "\\");
+      }
+      return path;
+  }
+
   /**
    * Writes link options.
    * @param writer Writer writer
@@ -523,17 +599,25 @@
         //   if file was not compiled or otherwise generated
         //
         if (targets.get(linkSources[i].getName()) == null) {
-          String relPath = CUtil.getRelativePath(basePath, linkSources[i]);
           //
+          //   if source appears to be a system library or object file
+          //      just output the name of the file (advapi.lib for example)
+          //      otherwise construct a relative path.
+          //
+          String relPath = linkSources[i].getName();
+          if (!isSystemPath(linkSources[i])) {
+              relPath = CUtil.getRelativePath(basePath, linkSources[i]);
+          }
+          //
           //   if path has an embedded space then
           //      must quote
           if (relPath.indexOf(' ') > 0) {
             options.append(" \"");
-            options.append(relPath);
+            options.append(toWindowsPath(relPath));
             options.append("\"");
           } else {
             options.append(' ');
-            options.append(relPath);
+            options.append(toWindowsPath(relPath));
           }
         }
       }

Added: cpptasks/trunk/src/main/java/net/sf/antcontrib/cpptasks/ide/DependencyDef.java
===================================================================
--- cpptasks/trunk/src/main/java/net/sf/antcontrib/cpptasks/ide/DependencyDef.java	                        (rev 0)
+++ cpptasks/trunk/src/main/java/net/sf/antcontrib/cpptasks/ide/DependencyDef.java	2008-02-03 21:50:19 UTC (rev 148)
@@ -0,0 +1,77 @@
+/*
+ *
+ * Copyright 2004-2006 The Ant-Contrib project
+ *
+ *  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.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package net.sf.antcontrib.cpptasks.ide;
+
+import org.apache.tools.ant.util.StringUtils;
+
+import java.io.File;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * Defines a dependency
+ *
+ */
+public final class DependencyDef {
+    private String id;
+    private File file;
+    private String name;
+    private String depends;
+
+    public DependencyDef() {
+    }
+
+
+    public void setID(final String val) {
+        id = val;
+    }
+    public File getFile() {
+        return file;
+    }
+    public void setFile(final File val) {
+        file = val;
+    }
+    public void setName(final String val) {
+        name = val;
+    }
+    public String getName() {
+        if (name != null) {
+            return name;
+        } else if(file != null) {
+            return file.getName();
+        }
+        return "null";
+    }
+    public String getID() {
+        if (id != null) {
+            return id;
+        }
+        return getName();
+    }
+    public String getDepends() {
+        return depends;
+    }
+    public void setDepends(final String val) {
+        depends = val;
+    }
+    public List getDependsList() {
+        if (depends != null) {
+            return StringUtils.split(depends, ',');
+        }
+        return Collections.EMPTY_LIST;    
+    }
+}

Modified: cpptasks/trunk/src/main/java/net/sf/antcontrib/cpptasks/ide/ProjectDef.java
===================================================================
--- cpptasks/trunk/src/main/java/net/sf/antcontrib/cpptasks/ide/ProjectDef.java	2008-01-30 05:19:46 UTC (rev 147)
+++ cpptasks/trunk/src/main/java/net/sf/antcontrib/cpptasks/ide/ProjectDef.java	2008-02-03 21:50:19 UTC (rev 148)
@@ -25,6 +25,7 @@
 
 import java.io.File;
 import java.lang.reflect.Method;
+import java.util.ArrayList;
 import java.util.Hashtable;
 import java.util.List;
 
@@ -83,6 +84,11 @@
    */
   private File objDir;
 
+    /**
+     * List of dependency definitions.
+     */
+  private List dependencies = new ArrayList();
+
   /**
    * Constructor.
    *
@@ -319,6 +325,21 @@
     this.objDir = oDir;
   }
 
+    /**
+     * FileSet containing project files that should be imported
+     *   as dependencies
+      * @param dependency dependency.
+     */
+  public void addDependency(final DependencyDef dependency) {
+        dependencies.add(dependency);
+
+  }
+
+  public List getDependencies() {
+      return new ArrayList(dependencies);
+  }
+
+
   /**
    * Required by documentation generator.
    */


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/