Changeset 7635 for trunk/projects/jar2lib
- Timestamp:
- 02/22/11 17:32:55 (9 years ago)
- Location:
- trunk/projects/jar2lib/src/main
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/projects/jar2lib/src/main/java/loci/jar2lib/ClassList.java
r6926 r7635 35 35 package loci.jar2lib; 36 36 37 import java.io.File; 37 38 import java.io.IOException; 38 39 import java.util.ArrayList; -
trunk/projects/jar2lib/src/main/java/loci/jar2lib/Jar2Lib.java
r7629 r7635 38 38 39 39 import java.io.File; 40 import java.io.FileFilter; 41 import java.io.FileInputStream; 40 42 import java.io.FileOutputStream; 41 43 import java.io.IOException; … … 74 76 private String conflictsPath; 75 77 private String headerPath; 78 private String sourcePath; 76 79 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; 77 86 78 87 // -- Constructor -- … … 120 129 public void setHeaderPath(String headerPath) { 121 130 this.headerPath = headerPath; 131 } 132 public String getSourcePath() { 133 return sourcePath; 134 } 135 public void setSourcePath(String sourcePath) { 136 this.sourcePath = sourcePath; 122 137 } 123 138 public String getOutputPath() { … … 147 162 headerPath = args[++i]; 148 163 } 164 else if (arg.equals("-source")) { 165 if (i == args.length - 1) die("Error: no source path given."); 166 sourcePath = args[++i]; 167 } 149 168 else if (arg.equals("-output")) { 150 169 if (i == args.length - 1) die("Error: no output path given."); … … 165 184 /** Generates a C++ wrapper project based on the current settings. */ 166 185 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(); 172 192 173 193 // TODO - print instructions on how to proceed with CMake … … 179 199 /** 180 200 * 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. 182 202 * 183 203 * @throws IllegalStateException if the settings are invalid. 184 204 */ 185 public void checkInputs() {205 public void validateInputs() { 186 206 // check project ID 187 207 if (projectId == null || !projectId.matches("^(\\w)([\\w\\-])*$")) { … … 205 225 } 206 226 227 // generate list of source files 228 sourceFiles = listSourceFiles(sourcePath); 229 207 230 // create output directory 208 final FileoutputDir = new File(outputPath);231 outputDir = new File(outputPath); 209 232 if (!outputDir.exists()) outputDir.mkdirs(); 210 233 if (!outputDir.isDirectory()) { 211 234 throw new IllegalStateException("Not a valid directory: " + outputPath); 212 235 } 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(); 213 248 } 214 249 … … 216 251 * Generates one header per input Java library. 217 252 * 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 { 224 255 final VelocityAutogen generator = new VelocityAutogen(headerPath); 225 final File includeDir = new File(outputPath, "include");226 if (!includeDir.exists()) includeDir.mkdirs();227 256 for (String jarPath : libraryJars) { 228 257 final File jarFile = new File(jarPath); 229 258 log("--> Generating header for " + jarFile.getName()); 230 generator.createJaceHeader(jarPath, path( includeDir));259 generator.createJaceHeader(jarPath, path(outputIncludeDir)); 231 260 } 232 261 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)); 236 264 } 237 265 … … 242 270 * @param includeDir Folder containing the C++ headers. 243 271 */ 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"); 247 274 final List<String> projectResources = findResources(RESOURCE_PREFIX); 248 275 for (String resource : projectResources) { 249 276 final String outPath = resource.substring(RESOURCE_PREFIX.length()); 250 277 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 { 272 294 final String osName = System.getProperty("os.name"); 273 295 final boolean isWindows = osName.indexOf("Windows") >= 0; 274 296 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)); 277 299 autoProxyArgs.add(path(proxiesIncludeDir)); 278 300 autoProxyArgs.add(path(proxiesSourceDir)); … … 285 307 log("--> Generating proxies"); 286 308 AutoProxy.main(autoProxyArgs.toArray(new String[0])); 287 288 return proxiesDir;289 309 } 290 310 … … 296 316 * @throws IOException 297 317 */ 298 public void fixConflicts( File proxiesDir) throws IOException {318 public void fixConflicts() throws IOException { 299 319 if (conflictsPath == null) return; 300 320 log("--> Renaming conflicting constants"); … … 305 325 // -- Main method -- 306 326 307 public static void main( String[] args) throws Exception {327 public static void main(final String[] args) throws Exception { 308 328 final Jar2Lib jar2Lib = new Jar2Lib(); 309 329 try { … … 325 345 // -- Helper methods -- 326 346 327 protected void log(String message) { 328 // TODO - improving logging mechanism? 347 protected void log(final String message) { 329 348 System.out.println(message); 330 349 } 331 350 332 protected void die( String message) {351 protected void die(final String message) { 333 352 throw new IllegalArgumentException(message); 334 353 } 335 354 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 }); 341 366 } 342 367 343 368 /** 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 { 345 372 final List<String> resources = new ArrayList<String>(); 346 373 final String jarPath = findEnclosingJar(Jar2Lib.class); … … 359 386 360 387 /** 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) 362 389 throws IOException 363 390 { 364 391 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(); 368 395 if (resource.endsWith("/")) { 369 396 // resource is a directory … … 373 400 // resource is a file 374 401 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); 383 403 in.close(); 384 404 } 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(); 385 418 } 386 419 … … 391 424 * from which the given class was loaded. 392 425 */ 393 public static String findEnclosingJar( Class<?> c)426 public static String findEnclosingJar(final Class<?> c) 394 427 throws UnsupportedEncodingException 395 428 { … … 410 443 } 411 444 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 412 457 /** 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) 414 459 throws UnsupportedEncodingException 415 460 { … … 434 479 } 435 480 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 } 440 494 441 495 } -
trunk/projects/jar2lib/src/main/java/loci/jar2lib/VelocityAutogen.java
r6926 r7635 65 65 // -- Constructor -- 66 66 67 public VelocityAutogen( String headerPath) throws IOException {67 public VelocityAutogen(final String headerPath) throws IOException { 68 68 if (headerPath == null) javaHeader = scriptHeader = ""; 69 69 else { … … 96 96 // -- VelocityAutogen methods -- 97 97 98 public void createJaceHeader( String jarPath,String outputPath)98 public void createJaceHeader(final String jarPath, final String outputPath) 99 99 throws VelocityException, IOException 100 100 { … … 123 123 } 124 124 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 127 128 { 128 129 final File buildFile = new File(outputPath, "CMakeLists.txt"); … … 136 137 context.put("projectId", projectId); 137 138 context.put("projectName", projectName); 139 context.put("sourceFiles", sourceFiles); 140 context.put("q", this); 138 141 139 142 // generate CMakeLists.txt file … … 141 144 } 142 145 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 143 156 } -
trunk/projects/jar2lib/src/main/resources/CMakeLists.vm
r6809 r7635 12 12 13 13 add_subdirectory(jace) 14 15 # -- build $projectName --16 14 17 15 include_directories(include "proxies/include" "jace/include" … … 33 31 34 32 target_link_libraries($projectId jace) 33 #foreach ($sourceFile in $sourceFiles) 35 34 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()) 37 add_executable($execName source/$sourceName) 38 target_link_libraries($execName $projectId jace "${JAVA_JVM_LIBRARY}") 39 #end##foreach $sourceFile
Note: See TracChangeset
for help on using the changeset viewer.