Changeset 6809


Ignore:
Timestamp:
08/18/10 14:01:56 (9 years ago)
Author:
curtis
Message:

Working version of Jar2Lib.

Location:
branches/maven/projects/jar2lib/src/main
Files:
5 edited
1 copied

Legend:

Unmodified
Added
Removed
  • branches/maven/projects/jar2lib/src/main/java/loci/jar2lib/FixProxies.java

    r6806 r6809  
    2525 
    2626import java.io.BufferedReader; 
     27import java.io.FileReader; 
    2728import java.io.IOException; 
    28 import java.io.InputStreamReader; 
    2929import java.util.ArrayList; 
    3030 
     
    6666    // parse list of conflicting constants 
    6767    constants = new ArrayList<String>(); 
    68     BufferedReader in = new BufferedReader(new InputStreamReader( 
    69       FixProxies.class.getResourceAsStream(conflictsFile))); 
     68    BufferedReader in = new BufferedReader(new FileReader(conflictsFile)); 
    7069    while (true) { 
    7170      String line = in.readLine(); 
  • branches/maven/projects/jar2lib/src/main/java/loci/jar2lib/Jar2Lib.java

    r6806 r6809  
    5151public class Jar2Lib { 
    5252 
    53   // -- Constants -- 
    54  
    55   public static final String DEFAULT_OUTPUT_DIR = "jar2lib-output"; 
    56  
    5753  // -- Fields -- 
    5854 
     55  private String projectId; 
     56  private String projectName; 
    5957  private List<String> jarPaths; 
    6058  private String conflictsPath; 
     
    6664  public Jar2Lib() { 
    6765    jarPaths = new ArrayList<String>(); 
    68     outputPath = DEFAULT_OUTPUT_DIR; 
    6966  } 
    7067 
    7168  // -- Jar2Lib methods -- 
    7269 
     70  public String getProjectId() { 
     71    return projectId; 
     72  } 
     73  public void setProjectId(String projectId) { 
     74    this.projectId = projectId; 
     75  } 
     76  public String getProjectName() { 
     77    return projectName; 
     78  } 
     79  public void setProjectName(String projectName) { 
     80    this.projectName = projectName; 
     81  } 
     82  public List<String> getLibraryPaths() { 
     83    return jarPaths; 
     84  } 
     85  public void setLibraryPaths(List<String> jarPaths) { 
     86    this.jarPaths = jarPaths; 
     87  } 
     88  public String getConflictsPath() { 
     89    return conflictsPath; 
     90  } 
     91  public void setConflictsPath(String conflictsPath) { 
     92    this.conflictsPath = conflictsPath; 
     93  } 
     94  public String getHeaderPath() { 
     95    return headerPath; 
     96  } 
     97  public void setHeaderPath(String headerPath) { 
     98    this.headerPath = headerPath; 
     99  } 
     100  public String getOutputPath() { 
     101    return outputPath; 
     102  } 
     103  public void setOutputPath(String outputPath) { 
     104    this.outputPath = outputPath; 
     105  } 
     106 
     107  /** Parses the settings from the given command line arguments. */ 
    73108  public void parseArgs(String[] args) { 
    74109    jarPaths = new ArrayList<String>(); 
    75110    conflictsPath = null; 
    76111    headerPath = null; 
    77     outputPath = DEFAULT_OUTPUT_DIR; 
    78     for (int i = 0; i < args.length; i++) { 
     112    outputPath = null; 
     113    projectId = args.length >= 1 ? args[0] : null; 
     114    projectName = args.length >= 2 ? args[1] : null; 
     115    for (int i = 2; i < args.length; i++) { 
    79116      final String arg = args[i]; 
    80117      if (arg.equals("-conflicts")) { 
     
    93130      else jarPaths.add(arg); 
    94131    } 
    95     if (jarPaths.size() == 0) { 
    96       die("Usage: java " + getClass().getName() + 
    97         " library.jar [library2.jar ...]\n" + 
    98                                 "  [-conflicts conflicts.txt] [-header header.txt]\n" + 
    99                                 "  [-output /path/to/output-project]"); 
    100     } 
    101   } 
    102  
     132    if (projectId == null || projectName == null || jarPaths.size() == 0) { 
     133      die("Usage: java " + getClass().getName() + " projectId projectName\n" + 
     134        "  library.jar [library2.jar ...]\n" + 
     135        "  [-conflicts conflicts.txt] [-header header.txt]\n" + 
     136        "  [-output /path/to/output-project]"); 
     137    } 
     138    if (outputPath == null) outputPath = projectId; 
     139  } 
     140 
     141  /** Generates a C++ wrapper project based on the current settings. */ 
    103142  public void execute() throws IOException, VelocityException { 
     143    checkInputs(); 
     144    final File includeDir = generateFiles(); 
     145    copyResources(includeDir); 
     146    final File proxiesDir = generateProxies(includeDir); 
     147    fixConflicts(proxiesDir); 
     148 
     149    // TODO - print instructions on how to proceed with CMake 
     150    // TODO - copy "final product" files such as wrapped JARs to build dir 
     151 
     152    log("--> Done"); 
     153  } 
     154 
     155  /** 
     156   * Checks that the current settings for project production are valid. 
     157   * Creates the output directory if it doesn't already exist. 
     158   * 
     159   * @throws IllegalStateException if the settings are invalid. 
     160   */ 
     161  public void checkInputs() { 
     162    // check project ID 
     163    if (projectId == null || !projectId.matches("^(\\w)+$")) { 
     164      throw new IllegalStateException("Invalid project ID: " + projectId); 
     165    } 
     166 
     167    // check project name 
     168    if (projectName == null) { 
     169      throw new IllegalStateException("Invalid project name: " + projectName); 
     170    } 
     171 
    104172    // check conflicts file 
    105173    if (conflictsPath != null && !new File(conflictsPath).exists()) { 
     
    119187      throw new IllegalStateException("Not a valid directory: " + outputPath); 
    120188    } 
    121  
    122     // generate jace headers 
     189  } 
     190 
     191  /** 
     192   * Generates one header per input Java library. 
     193   * Also generates the CMake build file. 
     194   * 
     195   * @return The include path where headers were written. 
     196   */ 
     197  public File generateFiles() throws IOException, VelocityException { 
     198    final File outputDir = new File(outputPath); 
     199 
    123200    final VelocityAutogen generator = new VelocityAutogen(headerPath); 
    124201    final File includeDir = new File(outputPath, "include"); 
    125                 if (!includeDir.exists()) includeDir.mkdirs(); 
     202    if (!includeDir.exists()) includeDir.mkdirs(); 
    126203    for (String jarPath : jarPaths) { 
    127                         final File jarFile = new File(jarPath); 
     204      final File jarFile = new File(jarPath); 
    128205      log("--> Generating header for " + jarFile.getName()); 
    129206      generator.createJaceHeader(jarPath, path(includeDir)); 
    130207    } 
    131208    log("--> Generating CMake build file"); 
    132                 final String projectId = "bfcpp"; //TODO TEMP! 
    133                 final String projectName = "Bio-Formats C++ bindings"; //TODO TEMP! 
    134                 generator.createCMakeLists(projectId, projectName, path(outputDir)); 
    135  
    136     // copy resources to output directory 
     209    generator.createCMakeLists(projectId, projectName, path(outputDir)); 
     210 
     211    return includeDir; 
     212  } 
     213 
     214  /** 
     215   * Copies static project resources into the project directory. 
     216   * In particular, copies the Jace C++ distribution and related files. 
     217   * 
     218   * @param includeDir Folder containing the C++ headers. 
     219   */ 
     220  public void copyResources(File includeDir) throws IOException { 
     221    final File outputDir = new File(outputPath); 
    137222    log("--> Copying resources"); 
    138223    final List<String> jaceResources = findResources("jace/"); 
    139224    for (String resource : jaceResources) copyResource(resource, outputDir); 
    140  
    141     // generate proxies using jace 
     225    copyResource("jace.h", includeDir); 
     226  } 
     227 
     228  /** 
     229   * Generates the C++ proxy classes enumerated in the C++ headers. 
     230   * 
     231   * @param includeDir Folder containing the C++ headers. 
     232   * @return The path where proxies were written. 
     233   * @throws UnsupportedEncodingException 
     234   */ 
     235  public File generateProxies(File includeDir) 
     236    throws UnsupportedEncodingException 
     237  { 
    142238    final File sourceDir = new File(outputPath, "source"); 
    143                 if (!sourceDir.exists()) sourceDir.mkdirs(); 
     239    if (!sourceDir.exists()) sourceDir.mkdirs(); 
    144240    final File proxiesDir = new File(outputPath, "proxies"); 
    145241    final File proxiesIncludeDir = new File(proxiesDir, "include"); 
    146                 if (!proxiesIncludeDir.exists()) proxiesIncludeDir.mkdirs(); 
     242    if (!proxiesIncludeDir.exists()) proxiesIncludeDir.mkdirs(); 
    147243    final File proxiesSourceDir = new File(proxiesDir, "source"); 
    148                 if (!proxiesSourceDir.exists()) proxiesSourceDir.mkdirs(); 
     244    if (!proxiesSourceDir.exists()) proxiesSourceDir.mkdirs(); 
    149245    final String osName = System.getProperty("os.name"); 
    150246    final boolean isWindows = osName.indexOf("Windows") >= 0; 
     
    160256    AutoProxy.main(autoProxyArgs.toArray(new String[0])); 
    161257 
    162     // post-process proxies to resolve any conflicts 
    163     if (conflictsPath != null) { 
    164       log("--> Renaming conflicting constants"); 
    165       final FixProxies fixProxies = new FixProxies(conflictsPath); 
    166       fixProxies.fixProxies(path(proxiesDir)); 
    167     } 
    168  
    169     // TODO - print instructions on how to proceed with CMake 
    170     // TODO - copy "final product" files such as wrapped JARs to build dir 
    171  
    172     log("--> Done"); 
     258    return proxiesDir; 
     259  } 
     260 
     261  /** 
     262   * Post-processes the generated proxies to correct any 
     263   * conflicts identified in the specified conflicts file. 
     264   * 
     265   * @param proxiesDir Folder containing the generated C++ proxies. 
     266   * @throws IOException 
     267   */ 
     268  public void fixConflicts(File proxiesDir) throws IOException { 
     269    if (conflictsPath == null) return; 
     270    log("--> Renaming conflicting constants"); 
     271    final FixProxies fixProxies = new FixProxies(conflictsPath); 
     272    fixProxies.fixProxies(path(proxiesDir)); 
    173273  } 
    174274 
     
    221321      sb.append(":"); 
    222322      sb.append(jarPath); 
     323    } 
     324    final String classPath = System.getProperty("java.class.path"); 
     325    if (classPath != null && !classPath.equals("")) { 
     326      sb.append(":"); 
     327      sb.append(classPath); 
    223328    } 
    224329    return sb.toString(); 
     
    251356    throws IOException 
    252357  { 
    253                 log(resource); 
     358    log(resource); 
    254359    final File outputFile = new File(baseDir, resource); 
    255360    final File outputDir = outputFile.getParentFile(); 
    256361    if (!outputDir.exists()) outputDir.mkdirs(); 
    257                 if (resource.endsWith("/")) { 
    258                         // resource is a directory 
    259                         outputFile.mkdir(); 
    260                 } 
    261                 else { 
    262                         // resource is a file 
    263                         final InputStream in = getClass().getResourceAsStream("/" + resource); 
    264                         final OutputStream out = new FileOutputStream(outputFile); 
    265                         final byte[] buf = new byte[512 * 1024]; // 512K buffer 
    266                         while (true) { 
    267                                 int r = in.read(buf); 
    268                                 if (r <= 0) break; // EOF 
    269                                 out.write(buf, 0, r); 
    270                         } 
    271                         out.close(); 
    272                         in.close(); 
    273                 } 
    274   } 
    275  
    276         /** 
    277          * Finds the JAR file (or file system path) 
    278          * from which the given class was loaded. 
    279          */ 
     362    if (resource.endsWith("/")) { 
     363      // resource is a directory 
     364      outputFile.mkdir(); 
     365    } 
     366    else { 
     367      // resource is a file 
     368      final InputStream in = getClass().getResourceAsStream("/" + resource); 
     369      final OutputStream out = new FileOutputStream(outputFile); 
     370      final byte[] buf = new byte[512 * 1024]; // 512K buffer 
     371      while (true) { 
     372        int r = in.read(buf); 
     373        if (r <= 0) break; // EOF 
     374        out.write(buf, 0, r); 
     375      } 
     376      out.close(); 
     377      in.close(); 
     378    } 
     379  } 
     380 
     381  // -- Static utility methods -- 
     382 
     383  /** 
     384   * Finds the JAR file (or file system path) 
     385   * from which the given class was loaded. 
     386   */ 
    280387  public static String findEnclosingJar(Class<?> c) 
    281388    throws UnsupportedEncodingException 
  • branches/maven/projects/jar2lib/src/main/resources/CMakeLists.vm

    r6806 r6809  
    1313add_subdirectory(jace) 
    1414 
    15 # TODO - eliminate use of JACE_DIR since we are bundling the source directly 
    16 #message(STATUS "-- Jace:") 
    17 \#set(JACE_DIR NOTFOUND CACHE PATH 
    18 #  "The path to toplevel directory of your Jace checkout") 
    19 \#if(IS_DIRECTORY "${JACE_DIR}") 
    20 #  message(STATUS "jace root     : ${JACE_DIR}") 
    21 \#else(IS_DIRECTORY "${JACE_DIR}") 
    22 #  message(FATAL_ERROR "Cannot build without Jace. Please set JACE_DIR.") 
    23 #endif(IS_DIRECTORY "${JACE_DIR}") 
    24  
    2515# -- build $projectName -- 
    2616 
    27 include_directories(include 
    28   "proxies/include" "${JACE_CPP_DIR}/include" 
     17include_directories(include "proxies/include" "jace/include" 
    2918  "${JAVA_INCLUDE_PATH}" "${JAVA_INCLUDE_PATH2}" "${Boost_INCLUDE_DIR}") 
    3019 
  • branches/maven/projects/jar2lib/src/main/resources/jace.h

    r6670 r6809  
    22// jace.h 
    33// 
    4  
    5 /* 
    6 OME Bio-Formats C++ bindings for native access to Bio-Formats Java library. 
    7 Copyright (c) 2008-@year@, UW-Madison LOCI. 
    8 All rights reserved. 
    9  
    10 Redistribution and use in source and binary forms, with or without 
    11 modification, are permitted provided that the following conditions are met: 
    12     * Redistributions of source code must retain the above copyright 
    13       notice, this list of conditions and the following disclaimer. 
    14     * Redistributions in binary form must reproduce the above copyright 
    15       notice, this list of conditions and the following disclaimer in the 
    16       documentation and/or other materials provided with the distribution. 
    17     * Neither the name of the UW-Madison LOCI nor the 
    18       names of its contributors may be used to endorse or promote products 
    19       derived from this software without specific prior written permission. 
    20  
    21 THIS SOFTWARE IS PROVIDED BY UW-MADISON LOCI ''AS IS'' AND ANY 
    22 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
    23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
    24 DISCLAIMED. IN NO EVENT SHALL UW-MADISON LOCI BE LIABLE FOR ANY 
    25 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
    26 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
    27 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 
    28 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
    29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
    30 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
    31 */ 
    32  
    33 /* 
    34 IMPORTANT NOTE: Although this software is distributed according to a 
    35 "BSD-style" license, it requires the Bio-Formats Java library to do 
    36 anything useful, which is licensed under the GPL v2 or later. 
    37 As such, if you wish to distribute this software with Bio-Formats itself, 
    38 your combined work must be distributed under the terms of the GPL. 
    39 */ 
    404 
    415#ifndef JACE_H 
  • branches/maven/projects/jar2lib/src/main/resources/jace/CMakeLists.txt

    r6806 r6809  
    1313### build Jace C++ library ### 
    1414 
    15 #message(STATUS "-- Jace:") 
    16 #set(JACE_DIR NOTFOUND CACHE PATH 
    17 #  "The path to toplevel directory of your Jace checkout") 
    18 #if(IS_DIRECTORY "${JACE_DIR}") 
    19 #  message(STATUS "jace root     : ${JACE_DIR}") 
    20 #else(IS_DIRECTORY "${JACE_DIR}") 
    21 #  message(FATAL_ERROR "Cannot build without Jace. Please set JACE_DIR.") 
    22 #endif(IS_DIRECTORY "${JACE_DIR}") 
    23  
    24 include_directories("${JACE_CPP_DIR}/include" 
     15include_directories("include" 
    2516  "${JAVA_INCLUDE_PATH}" "${JAVA_INCLUDE_PATH2}" 
    2617  "${Boost_INCLUDE_DIR}") 
    2718 
    28 file(GLOB_RECURSE JACE_SRC "${JACE_CPP_DIR}/source/*.cpp") 
    29 #message("jace source   : ${JACE_SRC}") 
    30 message(STATUS "") 
     19file(GLOB_RECURSE JACE_SRC "source/*.cpp") 
    3120 
    3221add_library(jace SHARED ${JACE_SRC}) 
  • branches/maven/projects/jar2lib/src/main/resources/jace/Prerequisites.cmake

    r6806 r6809  
    7272  link_directories(${Boost_STRIPPED_LIB_DIR}) 
    7373endif(WIN32) 
    74  
    75 message(STATUS "-- Jace:") 
    76 set(JACE_DIR NOTFOUND CACHE PATH 
    77   "The path to toplevel directory of your Jace checkout") 
    78 if(IS_DIRECTORY "${JACE_DIR}") 
    79   message(STATUS "jace root     : ${JACE_DIR}") 
    80 else(IS_DIRECTORY "${JACE_DIR}") 
    81   message(FATAL_ERROR "Cannot build without Jace. Please set JACE_DIR.") 
    82 endif(IS_DIRECTORY "${JACE_DIR}") 
    83  
    84 set(JACE_CPP_DIR "${JACE_DIR}/source/c++") 
    85 # For Jace r46 and later, use instead (& see build.properties): 
    86 #set(JACE_CPP_DIR "${JACE_DIR}/core/cpp") 
Note: See TracChangeset for help on using the changeset viewer.