Changeset 7635 for trunk/projects


Ignore:
Timestamp:
02/22/11 17:32:55 (9 years ago)
Author:
curtis
Message:

Add ability to specify additional source files.

Also clean up the Jar2Lib source code a bit.

Location:
trunk/projects
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/projects/cppwrap-maven-plugin/src/main/java/loci/maven/plugin/cppwrap/CppWrapMojo.java

    r7630 r7635  
    9898 
    9999        /** 
     100         * Path to folder containing additional C++ source code. 
     101         * 
     102         * Each .cpp file in the folder should contain a main method. 
     103         * These files will then be compiled as part of the build process, 
     104         * as individual executables. 
     105         * 
     106         * @parameter expression="${cppwrap.sourceDir}" 
     107         *   default-value="src/main/cppwrap" 
     108         */ 
     109        private File sourceDir; 
     110 
     111        /** 
    100112         * Path to output folder for C++ project. 
    101113         * 
     
    117129                final String headerPath = headerFile.exists() ? 
    118130                        headerFile.getPath() : null; 
     131                final String sourcePath = sourceDir.isDirectory() ? 
     132                        sourceDir.getPath() : null; 
    119133                final String outputPath = outputDir.getPath(); 
    120134 
     
    131145                jar2lib.setConflictsPath(conflictsPath); 
    132146                jar2lib.setHeaderPath(headerPath); 
     147                jar2lib.setSourcePath(sourcePath); 
    133148                jar2lib.setOutputPath(outputPath); 
    134149                try { 
  • trunk/projects/jar2lib/src/main/java/loci/jar2lib/ClassList.java

    r6926 r7635  
    3535package loci.jar2lib; 
    3636 
     37import java.io.File; 
    3738import java.io.IOException; 
    3839import java.util.ArrayList; 
  • trunk/projects/jar2lib/src/main/java/loci/jar2lib/Jar2Lib.java

    r7629 r7635  
    3838 
    3939import java.io.File; 
     40import java.io.FileFilter; 
     41import java.io.FileInputStream; 
    4042import java.io.FileOutputStream; 
    4143import java.io.IOException; 
     
    7476  private String conflictsPath; 
    7577  private String headerPath; 
     78  private String sourcePath; 
    7679  private String outputPath; 
     80 
     81  private File[] sourceFiles; 
     82  private File outputDir; 
     83  private File outputIncludeDir, outputSourceDir; 
     84  private File proxiesDir; 
     85  private File proxiesIncludeDir, proxiesSourceDir; 
    7786 
    7887  // -- Constructor -- 
     
    120129  public void setHeaderPath(String headerPath) { 
    121130    this.headerPath = headerPath; 
     131  } 
     132  public String getSourcePath() { 
     133    return sourcePath; 
     134  } 
     135  public void setSourcePath(String sourcePath) { 
     136    this.sourcePath = sourcePath; 
    122137  } 
    123138  public String getOutputPath() { 
     
    147162        headerPath = args[++i]; 
    148163      } 
     164      else if (arg.equals("-source")) { 
     165        if (i == args.length - 1) die("Error: no source path given."); 
     166        sourcePath = args[++i]; 
     167      } 
    149168      else if (arg.equals("-output")) { 
    150169        if (i == args.length - 1) die("Error: no output path given."); 
     
    165184  /** Generates a C++ wrapper project based on the current settings. */ 
    166185  public void execute() throws IOException, VelocityException { 
    167     checkInputs(); 
    168     final File includeDir = generateFiles(); 
    169     copyResources(includeDir); 
    170     final File proxiesDir = generateProxies(includeDir); 
    171     fixConflicts(proxiesDir); 
     186    validateInputs(); 
     187    generateSkeleton(); 
     188    copySourceFiles(); 
     189    generateHeaders(); 
     190    generateProxies(); 
     191    fixConflicts(); 
    172192 
    173193    // TODO - print instructions on how to proceed with CMake 
     
    179199  /** 
    180200   * Checks that the current settings for project production are valid. 
    181    * Creates the output directory if it doesn't already exist. 
     201   * Creates various needed output directories if needed. 
    182202   * 
    183203   * @throws IllegalStateException if the settings are invalid. 
    184204   */ 
    185   public void checkInputs() { 
     205  public void validateInputs() { 
    186206    // check project ID 
    187207    if (projectId == null || !projectId.matches("^(\\w)([\\w\\-])*$")) { 
     
    205225    } 
    206226 
     227    // generate list of source files 
     228    sourceFiles = listSourceFiles(sourcePath); 
     229 
    207230    // create output directory 
    208     final File outputDir = new File(outputPath); 
     231    outputDir = new File(outputPath); 
    209232    if (!outputDir.exists()) outputDir.mkdirs(); 
    210233    if (!outputDir.isDirectory()) { 
    211234      throw new IllegalStateException("Not a valid directory: " + outputPath); 
    212235    } 
     236 
     237    // create source and include directories 
     238    outputIncludeDir = new File(outputDir, "include"); 
     239    if (!outputIncludeDir.exists()) outputIncludeDir.mkdirs(); 
     240    outputSourceDir = new File(outputDir, "source"); 
     241    if (!outputSourceDir.exists()) outputSourceDir.mkdirs(); 
     242 
     243    proxiesDir = new File(outputPath, "proxies"); 
     244    proxiesIncludeDir = new File(proxiesDir, "include"); 
     245    if (!proxiesIncludeDir.exists()) proxiesIncludeDir.mkdirs(); 
     246    proxiesSourceDir = new File(proxiesDir, "source"); 
     247    if (!proxiesSourceDir.exists()) proxiesSourceDir.mkdirs(); 
    213248  } 
    214249 
     
    216251   * Generates one header per input Java library. 
    217252   * Also generates the CMake build file. 
    218    * 
    219    * @return The include path where headers were written. 
    220    */ 
    221   public File generateFiles() throws IOException, VelocityException { 
    222     final File outputDir = new File(outputPath); 
    223  
     253   */ 
     254  public void generateHeaders() throws IOException, VelocityException { 
    224255    final VelocityAutogen generator = new VelocityAutogen(headerPath); 
    225     final File includeDir = new File(outputPath, "include"); 
    226     if (!includeDir.exists()) includeDir.mkdirs(); 
    227256    for (String jarPath : libraryJars) { 
    228257      final File jarFile = new File(jarPath); 
    229258      log("--> Generating header for " + jarFile.getName()); 
    230       generator.createJaceHeader(jarPath, path(includeDir)); 
     259      generator.createJaceHeader(jarPath, path(outputIncludeDir)); 
    231260    } 
    232261    log("--> Generating CMake build file"); 
    233     generator.createCMakeLists(projectId, projectName, path(outputDir)); 
    234  
    235     return includeDir; 
     262    generator.createCMakeLists(projectId, projectName, 
     263        sourceFiles, path(outputDir)); 
    236264  } 
    237265 
     
    242270   * @param includeDir Folder containing the C++ headers. 
    243271   */ 
    244   public void copyResources(File includeDir) throws IOException { 
    245     final File outputDir = new File(outputPath); 
    246     log("--> Copying resources"); 
     272  public void generateSkeleton() throws IOException { 
     273    log("--> Generating project skeleton"); 
    247274    final List<String> projectResources = findResources(RESOURCE_PREFIX); 
    248275    for (String resource : projectResources) { 
    249276      final String outPath = resource.substring(RESOURCE_PREFIX.length()); 
    250277      if (outPath.equals("")) continue; // skip base folder 
    251       copyResource(resource, outPath, outputDir); 
    252     } 
    253   } 
    254  
    255   /** 
    256    * Generates the C++ proxy classes enumerated in the C++ headers. 
    257    * 
    258    * @param includeDir Folder containing the C++ headers. 
    259    * @return The path where proxies were written. 
    260    * @throws UnsupportedEncodingException 
    261    */ 
    262   public File generateProxies(File includeDir) 
    263     throws UnsupportedEncodingException 
    264   { 
    265     final File sourceDir = new File(outputPath, "source"); 
    266     if (!sourceDir.exists()) sourceDir.mkdirs(); 
    267     final File proxiesDir = new File(outputPath, "proxies"); 
    268     final File proxiesIncludeDir = new File(proxiesDir, "include"); 
    269     if (!proxiesIncludeDir.exists()) proxiesIncludeDir.mkdirs(); 
    270     final File proxiesSourceDir = new File(proxiesDir, "source"); 
    271     if (!proxiesSourceDir.exists()) proxiesSourceDir.mkdirs(); 
     278      copyResource(resource, outPath); 
     279    } 
     280  } 
     281 
     282  /** Copies any extra C++ source files into the project directory. */ 
     283  public void copySourceFiles() throws IOException { 
     284    if (sourceFiles.length == 0) return; // no sources 
     285    log("--> Copying sources"); 
     286    for (File sourceFile : sourceFiles) { 
     287      copySourceFile(sourceFile); 
     288    } 
     289  } 
     290 
     291 
     292  /** Generates the C++ proxy classes enumerated in the C++ headers. */ 
     293  public void generateProxies() throws UnsupportedEncodingException { 
    272294    final String osName = System.getProperty("os.name"); 
    273295    final boolean isWindows = osName.indexOf("Windows") >= 0; 
    274296    final List<String> autoProxyArgs = new ArrayList<String>(); 
    275     autoProxyArgs.add(path(includeDir)); 
    276     autoProxyArgs.add(path(sourceDir)); 
     297    autoProxyArgs.add(path(outputIncludeDir)); 
     298    autoProxyArgs.add(path(outputSourceDir)); 
    277299    autoProxyArgs.add(path(proxiesIncludeDir)); 
    278300    autoProxyArgs.add(path(proxiesSourceDir)); 
     
    285307    log("--> Generating proxies"); 
    286308    AutoProxy.main(autoProxyArgs.toArray(new String[0])); 
    287  
    288     return proxiesDir; 
    289309  } 
    290310 
     
    296316   * @throws IOException 
    297317   */ 
    298   public void fixConflicts(File proxiesDir) throws IOException { 
     318  public void fixConflicts() throws IOException { 
    299319    if (conflictsPath == null) return; 
    300320    log("--> Renaming conflicting constants"); 
     
    305325  // -- Main method -- 
    306326 
    307   public static void main(String[] args) throws Exception { 
     327  public static void main(final String[] args) throws Exception { 
    308328    final Jar2Lib jar2Lib = new Jar2Lib(); 
    309329    try { 
     
    325345  // -- Helper methods -- 
    326346 
    327   protected void log(String message) { 
    328     // TODO - improving logging mechanism? 
     347  protected void log(final String message) { 
    329348    System.out.println(message); 
    330349  } 
    331350 
    332   protected void die(String message) { 
     351  protected void die(final String message) { 
    333352    throw new IllegalArgumentException(message); 
    334353  } 
    335354 
    336   /** Gets the absolute path to the given file, with forward slashes. */ 
    337   private String path(File file) { 
    338     final String path = file.getAbsolutePath(); 
    339     // NB: Use forward slashes even on Windows. 
    340     return path.replaceAll("\\\\", "/"); 
     355  /** Gets a list of C++ source files in the given directory. */ 
     356  private File[] listSourceFiles(final String path) { 
     357    if (path == null) return new File[0]; // no sources 
     358    final File sourceDir = new File(path); 
     359    if (!sourceDir.exists()) return new File[0]; // no sources 
     360    return sourceDir.listFiles(new FileFilter() { 
     361      @Override 
     362      public boolean accept(final File pathname) { 
     363        return pathname.getName().toLowerCase().endsWith(".cpp"); 
     364      } 
     365    }); 
    341366  } 
    342367 
    343368  /** Scans the enclosing JAR file for all resources beneath the given path. */ 
    344   private List<String> findResources(String resourceDir) throws IOException { 
     369  private List<String> findResources(final String resourceDir) 
     370    throws IOException 
     371  { 
    345372    final List<String> resources = new ArrayList<String>(); 
    346373    final String jarPath = findEnclosingJar(Jar2Lib.class); 
     
    359386 
    360387  /** Copies the given resource to the specified output directory. */ 
    361   private void copyResource(String resource, String outPath, File baseDir) 
     388  private void copyResource(final String resource, final String outPath) 
    362389    throws IOException 
    363390  { 
    364391    log(outPath); 
    365     final File outputFile = new File(baseDir, outPath); 
    366     final File outputDir = outputFile.getParentFile(); 
    367     if (!outputDir.exists()) outputDir.mkdirs(); 
     392    final File outputFile = new File(outputDir, outPath); 
     393    final File parentDir = outputFile.getParentFile(); 
     394    if (!parentDir.exists()) parentDir.mkdirs(); 
    368395    if (resource.endsWith("/")) { 
    369396      // resource is a directory 
     
    373400      // resource is a file 
    374401      final InputStream in = Jar2Lib.class.getResourceAsStream("/" + resource); 
    375       final OutputStream out = new FileOutputStream(outputFile); 
    376       final byte[] buf = new byte[512 * 1024]; // 512K buffer 
    377       while (true) { 
    378         int r = in.read(buf); 
    379         if (r <= 0) break; // EOF 
    380         out.write(buf, 0, r); 
    381       } 
    382       out.close(); 
     402      writeStreamToFile(in, outputFile); 
    383403      in.close(); 
    384404    } 
     405  } 
     406 
     407        /** 
     408   * Copies the given file to to the source folder 
     409   * of the specified output directory. 
     410         * @throws IOException  
     411   */ 
     412  private void copySourceFile(final File sourceFile) throws IOException { 
     413        log(sourceFile.getPath()); 
     414        final File outputFile = new File(outputSourceDir, sourceFile.getName()); 
     415        final FileInputStream in = new FileInputStream(sourceFile); 
     416        writeStreamToFile(in, outputFile); 
     417        in.close(); 
    385418  } 
    386419 
     
    391424   * from which the given class was loaded. 
    392425   */ 
    393   public static String findEnclosingJar(Class<?> c) 
     426  public static String findEnclosingJar(final Class<?> c) 
    394427    throws UnsupportedEncodingException 
    395428  { 
     
    410443  } 
    411444 
     445  /** Locates the JAR file containing this JVM's classes. */ 
     446  private static String findRuntime() throws UnsupportedEncodingException { 
     447    return findEnclosingJar(Object.class); 
     448  } 
     449 
     450  /** Gets the absolute path to the given file, with forward slashes. */ 
     451  private static String path(final File file) { 
     452    final String path = file.getAbsolutePath(); 
     453    // NB: Use forward slashes even on Windows. 
     454    return path.replaceAll("\\\\", "/"); 
     455  } 
     456 
    412457  /** Builds a classpath corresponding to the given list of JAR files. */ 
    413   private static String classpath(List<String> libraryJars) 
     458  private static String classpath(final List<String> libraryJars) 
    414459    throws UnsupportedEncodingException 
    415460  { 
     
    434479  } 
    435480 
    436   /** Locates the JAR file containing this JVM's classes. */ 
    437   private static String findRuntime() throws UnsupportedEncodingException { 
    438     return findEnclosingJar(Object.class); 
    439   } 
     481  /** Writes the contents of the given input stream to the specified file. */ 
     482  private static void writeStreamToFile(final InputStream in, 
     483        final File outputFile) throws IOException 
     484        { 
     485          final OutputStream out = new FileOutputStream(outputFile); 
     486          final byte[] buf = new byte[512 * 1024]; // 512K buffer 
     487          while (true) { 
     488            int r = in.read(buf); 
     489            if (r <= 0) break; // EOF 
     490            out.write(buf, 0, r); 
     491          } 
     492          out.close(); 
     493        } 
    440494 
    441495} 
  • trunk/projects/jar2lib/src/main/java/loci/jar2lib/VelocityAutogen.java

    r6926 r7635  
    6565  // -- Constructor -- 
    6666 
    67   public VelocityAutogen(String headerPath) throws IOException { 
     67  public VelocityAutogen(final String headerPath) throws IOException { 
    6868    if (headerPath == null) javaHeader = scriptHeader = ""; 
    6969    else { 
     
    9696  // -- VelocityAutogen methods -- 
    9797 
    98   public void createJaceHeader(String jarPath, String outputPath) 
     98  public void createJaceHeader(final String jarPath, final String outputPath) 
    9999    throws VelocityException, IOException 
    100100  { 
     
    123123  } 
    124124 
    125   public void createCMakeLists(String projectId, String projectName, 
    126     String outputPath) throws VelocityException, IOException 
     125  public void createCMakeLists(final String projectId, 
     126        final String projectName, final File[] sourceFiles, 
     127        final String outputPath) throws VelocityException, IOException 
    127128  { 
    128129    final File buildFile = new File(outputPath, "CMakeLists.txt"); 
     
    136137    context.put("projectId", projectId); 
    137138    context.put("projectName", projectName); 
     139    context.put("sourceFiles", sourceFiles); 
     140    context.put("q", this); 
    138141 
    139142    // generate CMakeLists.txt file 
     
    141144  } 
    142145 
     146  // -- Utility methods -- 
     147 
     148  /** Gets a simple name prefix with only alphameric characters. */ 
     149  public static String simpleName(final File file) { 
     150        final String name = file.getName(); 
     151        final int dot = name.indexOf("."); 
     152        final String prefix = dot < 0 ? name : name.substring(0, dot); 
     153        return prefix.replaceAll("[^\\w\\-]", ""); 
     154  } 
     155 
    143156} 
  • trunk/projects/jar2lib/src/main/resources/CMakeLists.vm

    r6809 r7635  
    1212 
    1313add_subdirectory(jace) 
    14  
    15 # -- build $projectName -- 
    1614 
    1715include_directories(include "proxies/include" "jace/include" 
     
    3331 
    3432target_link_libraries($projectId jace) 
     33#foreach ($sourceFile in $sourceFiles) 
    3534 
    36 # -- build some examples for testing $projectId -- 
    37  
    38 # TODO - eliminate hard-coded sources below, in favor of user-specified option 
    39  
    40 # add_executable(showinf source/showinf.cpp) 
    41 # target_link_libraries(showinf $projectId jace "${JAVA_JVM_LIBRARY}") 
    42  
    43 # add_executable(minimum_writer source/minimum_writer.cpp) 
    44 # target_link_libraries(minimum_writer $projectId jace "${JAVA_JVM_LIBRARY}") 
     35#set ($execName = $q.simpleName($sourceFile)) 
     36#set ($sourceName = $sourceFile.getName()) 
     37add_executable($execName source/$sourceName) 
     38target_link_libraries($execName $projectId jace "${JAVA_JVM_LIBRARY}") 
     39#end##foreach $sourceFile 
Note: See TracChangeset for help on using the changeset viewer.