GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — processing/java/src/processing/mode/java/JavaBuild.java at master · ErikssonM/processing Expand file tree/ Copy pathJavaBuild.javaMore file actions 1427 lines (1203 loc) · 53.2 KB / Copy pathJavaBuild.javaFile metadata and controls1427 lines (1203 loc) · 53.2 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 /* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* Part of the Processing project - http://processing.org Copyright (c) 2012-19 The Processing Foundation Copyright (c) 2004-12 Ben Fry and Casey Reas Copyright (c) 2001-04 Massachusetts Institute of Technology This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License version 2 as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ package processing.mode.java; import java.io.*; import java.util.ArrayList; import java.util.Enumeration; import java.util.HashMap; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import java.util.zip.ZipOutputStream; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.DefaultLogger; import org.apache.tools.ant.Project; import org.apache.tools.ant.ProjectHelper; import processing.app.Base; import processing.app.Language; import processing.app.Library; import processing.app.Messages; import processing.app.Mode; import processing.app.Platform; import processing.app.Preferences; import processing.app.Sketch; import processing.app.SketchCode; import processing.app.SketchException; import processing.app.Util; import processing.app.exec.ProcessHelper; import processing.core.PApplet; import processing.core.PConstants; import processing.data.StringList; import processing.data.XML; import processing.mode.java.pdex.SourceUtils; import processing.mode.java.preproc.PdePreprocessor; import processing.mode.java.preproc.PreprocessorResult; import processing.mode.java.preproc.SurfaceInfo; public class JavaBuild { public static final String PACKAGE_REGEX = "(?:^|\\s|;)package\\s+(\\S+)\\;"; protected Sketch sketch; protected Mode mode; // what happens in the build, stays in the build. // (which is to say that everything below this line, stays within this class) protected File srcFolder; protected File binFolder; private boolean foundMain = false; private String classPath; protected String sketchClassName; /** * This will include the code folder, any library folders, etc. that might * contain native libraries that need to be picked up with java.library.path. * This is *not* the "Processing" libraries path, this is the Java libraries * path, as in java.library.path=BlahBlah, which identifies search paths for * DLLs or JNILIBs. (It's Java's LD_LIBRARY_PATH, for you UNIX fans.) * This is set by the preprocessor as it figures out where everything is. */ private String javaLibraryPath; /** List of library folders, as figured out during preprocessing. */ private List<Library> importedLibraries; public JavaBuild(Sketch sketch) { this.sketch = sketch; this.mode = sketch.getMode(); } /** * Run the build inside a temporary build folder. Used for run/present. * @return null if compilation failed, main class name if not * @throws RunnerException */ public String build(boolean sizeWarning) throws SketchException { return build(sketch.makeTempFolder(), sketch.makeTempFolder(), sizeWarning); } /** * Preprocess and compile all the code for this sketch. * * In an advanced program, the returned class name could be different, * which is why the className is set based on the return value. * A compilation error will burp up a RunnerException. * * @return null if compilation failed, main class name if not */ public String build(File srcFolder, File binFolder, boolean sizeWarning) throws SketchException { this.srcFolder = srcFolder; this.binFolder = binFolder; // run the preprocessor String classNameFound = preprocess(srcFolder, sizeWarning); // compile the program. errors will happen as a RunnerException // that will bubble up to whomever called build(). if (Compiler.compile(this)) { sketchClassName = classNameFound; return classNameFound; } return null; } public String getSketchClassName() { return sketchClassName; } /** * Build all the code for this sketch. * * In an advanced program, the returned class name could be different, * which is why the className is set based on the return value. * A compilation error will burp up a RunnerException. * * Setting purty to 'true' will cause exception line numbers to be incorrect. * Unless you know the code compiles, you should first run the preprocessor * with purty set to false to make sure there are no errors, then once * successful, re-export with purty set to true. * * @param buildPath Location to copy all the .java files * @return null if compilation failed, main class name if not */ public String preprocess(File srcFolder, boolean sizeWarning) throws SketchException { return preprocess(srcFolder, null, new PdePreprocessor(sketch.getName()), sizeWarning); } /** * @param srcFolder location where the .java source files will be placed * @param packageName null, or the package name that should be used as default * @param preprocessor the preprocessor object ready to do the work * @return main PApplet class name found during preprocess, or null if error * @throws SketchException */ public String preprocess(File srcFolder, String packageName, PdePreprocessor preprocessor, boolean sizeWarning) throws SketchException { // make sure the user isn't playing "hide the sketch folder" sketch.ensureExistence(); // System.out.println("srcFolder is " + srcFolder); classPath = binFolder.getAbsolutePath(); // figure out the contents of the code folder to see if there // are files that need to be added to the imports StringList codeFolderPackages = null; if (sketch.hasCodeFolder()) { File codeFolder = sketch.getCodeFolder(); javaLibraryPath = codeFolder.getAbsolutePath(); // get a list of .jar files in the "code" folder // (class files in subfolders should also be picked up) String codeFolderClassPath = Util.contentsToClassPath(codeFolder); // append the jar files in the code folder to the class path classPath += File.pathSeparator + codeFolderClassPath; // get list of packages found in those jars codeFolderPackages = Util.packageListFromClassPath(codeFolderClassPath); } else { javaLibraryPath = ""; } // 1. concatenate all .pde files to the 'main' pde // store line number for starting point of each code bit StringBuilder bigCode = new StringBuilder(); int bigCount = 0; for (SketchCode sc : sketch.getCode()) { if (sc.isExtension("pde")) { sc.setPreprocOffset(bigCount); bigCode.append(sc.getProgram()); bigCode.append('\n'); bigCount += sc.getLineCount(); } } // initSketchSize() sets the internal sketchWidth/Height/Renderer vars // in the preprocessor. Those are used in preproc.write() so that they // can be used to add methods (settings() or sketchXxxx()) //String[] sizeParts = SurfaceInfo sizeInfo = preprocessor.initSketchSize(sketch.getMainProgram(), sizeWarning); if (sizeInfo == null) { // An error occurred while trying to pull out the size, so exit here return null; } //System.out.format("size() is '%s'%n", info[0]); // Remove the entries being moved to settings(). They will be re-inserted // by writeFooter() when it emits the settings() method. // If the user already has a settings() method, don't mess with anything. // https://github.com/processing/processing/issues/4703 if (!PdePreprocessor.hasSettingsMethod(bigCode.toString()) && sizeInfo != null && sizeInfo.hasSettings()) { for (String stmt : sizeInfo.getStatements()) { // Don't remove newlines (and while you're at it, just keep spaces) // https://github.com/processing/processing/issues/3654 stmt = stmt.trim(); int index = bigCode.indexOf(stmt); if (index != -1) { bigCode.delete(index, index + stmt.length()); } else { // TODO remove once we hit final; but prevent an exception like in // https://github.com/processing/processing/issues/3531 System.err.format("Error removing '%s' from the code.", stmt); } } } PreprocessorResult result; try { File outputFolder = (packageName == null) ? srcFolder : new File(srcFolder, packageName.replace('.', '/')); outputFolder.mkdirs(); final File java = new File(outputFolder, sketch.getName() + ".java"); try { final PrintWriter writer = PApplet.createWriter(java); try { result = preprocessor.write(writer, bigCode.toString(), codeFolderPackages); } finally { writer.close(); } } catch (RuntimeException re) { re.printStackTrace(); throw new SketchException("Could not write " + java.getAbsolutePath()); } } catch (antlr.RecognitionException re) { // re also returns a column that we're not bothering with for now // first assume that it's the main file // int errorFile = 0; int errorLine = re.getLine() - 1; // then search through for anyone else whose preprocName is null, // since they've also been combined into the main pde. int errorFile = findErrorFile(errorLine); errorLine -= sketch.getCode(errorFile).getPreprocOffset(); String msg = re.getMessage(); if (msg.contains("expecting RCURLY") || msg.contains("expecting LCURLY")) { for (int i = 0; i < sketch.getCodeCount(); i++) { SketchCode sc = sketch.getCode(i); if (sc.isExtension("pde")) { String s = sc.getProgram(); int[] braceTest = SourceUtils.checkForMissingBraces( SourceUtils.scrubCommentsAndStrings(s) + "\n", 0, s.length()+1); if (braceTest[0] == 0) continue; // Completely ignoring the errorFile/errorLine given since it's // likely to be the wrong tab. For the same reason, I'm not showing // the result of PApplet.match(msg, "found ('.*')") on missing // LCURLY. throw new SketchException(braceTest[0] > 0 ? "Found an extra { character without a } to match it." : "Found an extra } character without a { to match it.", i, braceTest[1], braceTest[2], false); } } // If we're still here, there's the right brackets, just not in the // right place. Passing on the original error. throw new SketchException( msg.replace("LCURLY", "{").replace("RCURLY", "}"), errorFile, errorLine, re.getColumn(), false); } if (msg.indexOf("expecting RBRACK") != -1) { System.err.println(msg); throw new SketchException("Syntax error, " + "maybe a missing ] character?", errorFile, errorLine, re.getColumn(), false); } if (msg.indexOf("expecting SEMI") != -1) { System.err.println(msg); throw new SketchException("Syntax error, " + "maybe a missing semicolon?", errorFile, errorLine, re.getColumn(), false); } if (msg.indexOf("expecting RPAREN") != -1) { System.err.println(msg); throw new SketchException("Syntax error, " + "maybe a missing right parenthesis?", errorFile, errorLine, re.getColumn(), false); } if (msg.indexOf("preproc.web_colors") != -1) { throw new SketchException("A web color (such as #ffcc00) " + "must be six digits.", errorFile, errorLine, re.getColumn(), false); } //System.out.println("msg is " + msg); throw new SketchException(msg, errorFile, errorLine, re.getColumn(), false); } catch (antlr.TokenStreamRecognitionException tsre) { // while this seems to store line and column internally, // there doesn't seem to be a method to grab it.. // so instead it's done using a regexp // System.err.println("and then she tells me " + tsre.toString()); // TODO not tested since removing ORO matcher.. ^ could be a problem String locationRegex = "^line (\\d+):(\\d+):\\s"; String message = tsre.getMessage(); String[] m; if (null != (m = PApplet.match(tsre.toString(), "unexpected char: (.*)"))) { char c = 0; if (m[1].startsWith("0x")) { // Hex c = (char) PApplet.unhex(m[1].substring(2)); } else if (m[1].length() == 3) { // Quoted c = m[1].charAt(1); } else if (m[1].length() == 1) { // Alone c = m[1].charAt(0); } if (c == '\u201C' || c == '\u201D' || // “” c == '\u2018' || c == '\u2019') { // ‘’ message = Language.interpolate("editor.status.bad_curly_quote", c); } else if (c != 0) { message = "Not expecting symbol " + m[1] + ", which is " + Character.getName(c) + "."; } } String[] matches = PApplet.match(tsre.toString(), locationRegex); if (matches != null) { int errorLine = Integer.parseInt(matches[1]) - 1; int errorColumn = Integer.parseInt(matches[2]); int errorFile = 0; for (int i = 1; i < sketch.getCodeCount(); i++) { SketchCode sc = sketch.getCode(i); if (sc.isExtension("pde") && (sc.getPreprocOffset() < errorLine)) { errorFile = i; } } errorLine -= sketch.getCode(errorFile).getPreprocOffset(); throw new SketchException(message, errorFile, errorLine, errorColumn); } else { // this is bad, defaults to the main class.. hrm. String msg = tsre.toString(); throw new SketchException(msg, 0, -1, -1); } } catch (SketchException pe) { // RunnerExceptions are caught here and re-thrown, so that they don't // get lost in the more general "Exception" handler below. throw pe; } catch (Exception ex) { // TODO better method for handling this? System.err.println("Uncaught exception type:" + ex.getClass()); ex.printStackTrace(); throw new SketchException(ex.toString()); } // grab the imports from the code just preprocessed importedLibraries = new ArrayList<>(); Library core = mode.getCoreLibrary(); if (core != null) { importedLibraries.add(core); classPath += core.getClassPath(); javaLibraryPath += File.pathSeparator + core.getNativePath(); } for (String item : result.extraImports) { // remove things up to the last dot int dot = item.lastIndexOf('.'); // http://dev.processing.org/bugs/show_bug.cgi?id=1145 String entry = (dot == -1) ? item : item.substring(0, dot); if (item.startsWith("static ")) { // import static - https://github.com/processing/processing/issues/8 int dot2 = item.lastIndexOf('.'); entry = entry.substring(7, (dot2 == -1) ? entry.length() : dot2); } Library library = mode.getLibrary(entry); if (library != null) { if (!importedLibraries.contains(library)) { importedLibraries.add(library); classPath += library.getClassPath(); javaLibraryPath += File.pathSeparator + library.getNativePath(); } } else { boolean found = false; // If someone insists on unnecessarily repeating the code folder // import, don't show an error for it. if (codeFolderPackages != null) { String itemPkg = entry; for (String pkg : codeFolderPackages) { if (pkg.equals(itemPkg)) { found = true; break; } } } if (ignorableImport(entry + '.')) { found = true; } if (!found) { System.err.println("No library found for " + entry); } } } // Finally, add the regular Java CLASSPATH. This contains everything // imported by the PDE itself (core.jar, pde.jar, quaqua.jar) which may // in fact be more of a problem. String javaClassPath = System.getProperty("java.class.path"); // Remove quotes if any.. A messy (and frequent) Windows problem if (javaClassPath.startsWith("\"") && javaClassPath.endsWith("\"")) { javaClassPath = javaClassPath.substring(1, javaClassPath.length() - 1); } classPath += File.pathSeparator + javaClassPath; // But make sure that there isn't anything in there that's missing, // otherwise ECJ will complain and die. For instance, Java 1.7 (or maybe // it's appbundler?) adds Java/Classes to the path, which kills us. //String[] classPieces = PApplet.split(classPath, File.pathSeparator); // Nah, nevermind... we'll just create the @!#$! folder until they fix it. // 3. then loop over the code[] and save each .java file for (SketchCode sc : sketch.getCode()) { if (sc.isExtension("java")) { // In most cases, no pre-processing services necessary for Java files. // Just write the the contents of 'program' to a .java file // into the build directory. However, if a default package is being // used (as in Android), and no package is specified in the source, // then we need to move this code to the same package as the sketch. // Otherwise, the class may not be found, or at a minimum, the default // access across the packages will mean that things behave incorrectly. // For instance, desktop code that uses a .java file with no packages, // will be fine with the default access, but since Android's PApplet // requires a package, code from that (default) package (such as the // PApplet itself) won't have access to methods/variables from the // package-less .java file (unless they're all marked public). String filename = sc.getFileName(); try { String javaCode = sc.getProgram(); String[] packageMatch = PApplet.match(javaCode, PACKAGE_REGEX); // if no package, and a default package is being used // (i.e. on Android) we'll have to add one if (packageMatch == null && packageName == null) { sc.copyTo(new File(srcFolder, filename)); } else { if (packageMatch == null) { // use the default package name, since mixing with package-less code will break packageMatch = new String[] { "", packageName }; // add the package name to the source before writing it javaCode = "package " + packageName + ";" + javaCode; } File packageFolder = new File(srcFolder, packageMatch[1].replace('.', File.separatorChar)); packageFolder.mkdirs(); Util.saveFile(javaCode, new File(packageFolder, filename)); } } catch (IOException e) { e.printStackTrace(); String msg = "Problem moving " + filename + " to the build folder"; throw new SketchException(msg); } } else if (sc.isExtension("pde")) { // The compiler and runner will need this to have a proper offset sc.addPreprocOffset(result.headerOffset); } } foundMain = preprocessor.hasMethod("main"); return result.className; } /** * Returns true if this package isn't part of a library (it's a system import * or something like that). Don't bother complaining about java.* or javax.* * because it's probably in boot.class.path. But we're not checking against * that path since it's enormous. Unfortunately we do still have to check * for libraries that begin with a prefix like javax, since that includes * the OpenGL library, even though we're just returning true here, hrm... */ protected boolean ignorableImport(String pkg) { if (pkg.startsWith("java.")) return true; if (pkg.startsWith("javax.")) return true; if (pkg.startsWith("javafx.")) return true; if (pkg.startsWith("processing.core.")) return true; if (pkg.startsWith("processing.data.")) return true; if (pkg.startsWith("processing.event.")) return true; if (pkg.startsWith("processing.opengl.")) return true; return false; } protected int findErrorFile(int errorLine) { for (int i = sketch.getCodeCount() - 1; i > 0; i--) { SketchCode sc = sketch.getCode(i); if (sc.isExtension("pde") && (sc.getPreprocOffset() <= errorLine)) { // keep looping until the errorLine is past the offset return i; } } return 0; // i give up } /** * Path to the folder that will contain processed .java source files. Not * the location for .pde files, since that can be obtained from the sketch. */ public File getSrcFolder() { return srcFolder; } public File getBinFolder() { return binFolder; } /** * Absolute path to the sketch folder. Used to set the working directry of * the sketch when running, i.e. so that saveFrame() goes to the right * location when running from the PDE, instead of the same folder as the * Processing.exe or the root of the user's home dir. */ public String getSketchPath() { return sketch.getFolder().getAbsolutePath(); } /** Class path determined during build. */ public String getClassPath() { return classPath; } /** Return the java.library.path for this sketch (for all the native DLLs etc). */ public String getJavaLibraryPath() { return javaLibraryPath; } /** * Whether the preprocessor found a main() method. If main() is found, then * it will be used to launch the sketch instead of PApplet.main(). */ public boolean getFoundMain() { return foundMain; } /** * Get the list of imported libraries. Used by external tools like Android mode. * @return list of library folders connected to this sketch. */ public List<Library> getImportedLibraries() { return importedLibraries; } /** * Map an error from a set of processed .java files back to its location * in the actual sketch. * @param message The error message. * @param filename The .java file where the exception was found. * @param line Line number of the .java file for the exception (0-indexed!) * @return A RunnerException to be sent to the editor, or null if it wasn't * possible to place the exception to the sketch code. */ public SketchException placeException(String message, String dotJavaFilename, int dotJavaLine) { int codeIndex = 0; //-1; int codeLine = -1; // System.out.println("placing " + dotJavaFilename + " " + dotJavaLine); // System.out.println("code count is " + getCodeCount()); // first check to see if it's a .java file for (int i = 0; i < sketch.getCodeCount(); i++) { SketchCode code = sketch.getCode(i); if (code.isExtension("java")) { if (dotJavaFilename.equals(code.getFileName())) { codeIndex = i; codeLine = dotJavaLine; return new SketchException(message, codeIndex, codeLine); } } } // If not the preprocessed file at this point, then need to get out if (!dotJavaFilename.equals(sketch.getName() + ".java")) { return null; } // if it's not a .java file, codeIndex will still be 0 // this section searches through the list of .pde files codeIndex = 0; for (int i = 0; i < sketch.getCodeCount(); i++) { SketchCode code = sketch.getCode(i); if (code.isExtension("pde")) { // System.out.println("preproc offset is " + code.getPreprocOffset()); // System.out.println("looking for line " + dotJavaLine); if (code.getPreprocOffset() <= dotJavaLine) { codeIndex = i; // System.out.println("i'm thinkin file " + i); codeLine = dotJavaLine - code.getPreprocOffset(); } } } // could not find a proper line number, so deal with this differently. // but if it was in fact the .java file we're looking for, though, // send the error message through. // this is necessary because 'import' statements will be at a line // that has a lower number than the preproc offset, for instance. // if (codeLine == -1 && !dotJavaFilename.equals(name + ".java")) { // return null; // } // return new SketchException(message, codeIndex, codeLine); return new SketchException(message, codeIndex, codeLine, -1, false); // changed for 0194 for compile errors, but... } /** * Export to application via GUI. */ protected boolean exportApplication() throws IOException, SketchException { // Do the build once, so that we know what libraries are in use (and what // the situation is with their native libs), and also for efficiency of // not redoing the compilation for each platform. In particular, though, // importedLibraries won't be set until the preprocessing has finished, // so we have to do that before the stuff below. String foundName = build(true); // (already reported) error during export, exit this function if (foundName == null) return false; // if name != exportSketchName, then that's weirdness // BUG unfortunately, that can also be a bug in the preproc :( if (!sketch.getName().equals(foundName)) { Messages.showWarning("Error during export", "Sketch name is " + sketch.getName() + " but the sketch\n" + "name in the code was " + foundName, null); return false; } File folder = null; for (String platformName : PConstants.platformNames) { int platform = Platform.getIndex(platformName); // Can only embed Java on the native platform boolean embedJava = (platform == PApplet.platform) && Preferences.getBoolean("export.application.embed_java"); if (Preferences.getBoolean(JavaEditor.EXPORT_PREFIX + platformName)) { final int bits = Platform.getNativeBits(); final String arch = Platform.getNativeArch(); if (Library.hasMultipleArch(platform, importedLibraries)) { // Don't try to export 32-bit on macOS, because it doesn't exist. if (platform != PConstants.MACOSX) { // export the 32-bit version folder = new File(sketch.getFolder(), "application." + platformName + "32"); if (!exportApplication(folder, platform, "32", embedJava && (bits == 32) && ("x86".equals(arch) || "i386".equals(arch)))) { return false; } } // export the 64-bit version folder = new File(sketch.getFolder(), "application." + platformName + "64"); if (!exportApplication(folder, platform, "64", embedJava && (bits == 64) && "amd64".equals(arch))) { return false; } if (platform == PConstants.LINUX) { // export the arm versions as well folder = new File(sketch.getFolder(), "application.linux-armv6hf"); if (!exportApplication(folder, platform, "armv6hf", embedJava && (bits == 32) && "arm".equals(arch))) { return false; } folder = new File(sketch.getFolder(), "application.linux-arm64"); if (!exportApplication(folder, platform, "arm64", embedJava && (bits == 64) && "aarch64".equals(arch))) { return false; } } } else { // just make a single one for this platform folder = new File(sketch.getFolder(), "application." + platformName); if (!exportApplication(folder, platform, "", embedJava)) { return false; } } } } return true; // all good } /** * Export to application without GUI. Also called by the Commander. */ protected boolean exportApplication(File destFolder, int exportPlatform, String exportVariant, boolean embedJava) throws IOException, SketchException { // TODO this should probably be a dialog box instead of a warning // on the terminal. And the message should be written better than this. // http://code.google.com/p/processing/issues/detail?id=884 for (Library library : importedLibraries) { if (!library.supportsArch(exportPlatform, exportVariant)) { String pn = PConstants.platformNames[exportPlatform]; Messages.showWarning("Quibbles 'n Bits", "The application." + pn + exportVariant + " folder will not be created\n" + "because no " + exportVariant + " version of " + library.getName() + " is available for " + pn, null); return true; // don't cancel all exports for this, just move along } } /// prep the output directory mode.prepareExportFolder(destFolder); /// figure out where the jar files will be placed File jarFolder = new File(destFolder, "lib"); /// where all the skeleton info lives /// on macosx, need to copy .app skeleton since that's /// also where the jar files will be placed File dotAppFolder = null; String jvmRuntime = ""; String jdkPath = null; if (exportPlatform == PConstants.MACOSX) { dotAppFolder = new File(destFolder, sketch.getName() + ".app"); File contentsOrig = new File(Platform.getJavaHome(), "../../../../.."); if (embedJava) { File jdkFolder = new File(Platform.getJavaHome(), "../../.."); String jdkFolderName = jdkFolder.getCanonicalFile().getName(); jvmRuntime = "<key>JVMRuntime</key>\n <string>" + jdkFolderName + "</string>"; jdkPath = new File(dotAppFolder, "Contents/PlugIns/" + jdkFolderName).getAbsolutePath(); } File contentsFolder = new File(dotAppFolder, "Contents"); contentsFolder.mkdirs(); // Info.plist will be written later // set the jar folder to a different location than windows/linux //jarFolder = new File(dotAppFolder, "Contents/Resources/Java"); jarFolder = new File(contentsFolder, "Java"); File macosFolder = new File(contentsFolder, "MacOS"); macosFolder.mkdirs(); // This is an unsigned copy of the app binary (see build/build.xml) Util.copyFile(mode.getContentFile("application/mac-app-stub"), new File(contentsFolder, "MacOS/" + sketch.getName())); File pkgInfo = new File(contentsFolder, "PkgInfo"); PrintWriter writer = PApplet.createWriter(pkgInfo); writer.println("APPL????"); writer.flush(); writer.close(); // Use faster(?) native copy here (also to do sym links) if (embedJava) { Util.copyDirNative(new File(contentsOrig, "PlugIns"), new File(contentsFolder, "PlugIns")); } File resourcesFolder = new File(contentsFolder, "Resources"); Util.copyDir(new File(contentsOrig, "Resources/en.lproj"), new File(resourcesFolder, "en.lproj")); Util.copyFile(mode.getContentFile("application/sketch.icns"), new File(resourcesFolder, "sketch.icns")); } else if (exportPlatform == PConstants.LINUX) { if (embedJava) { Util.copyDirNative(Platform.getJavaHome(), new File(destFolder, "java")); } } else if (exportPlatform == PConstants.WINDOWS) { if (embedJava) { Util.copyDir(Platform.getJavaHome(), new File(destFolder, "java")); } } /// make the jar folder (all platforms) if (!jarFolder.exists()) jarFolder.mkdirs(); /// start copying all jar files StringList jarList = new StringList(); /// create the main .jar file // HashMap<String,Object> zipFileContents = new HashMap<String,Object>(); FileOutputStream zipOutputFile = new FileOutputStream(new File(jarFolder, sketch.getName() + ".jar")); ZipOutputStream zos = new ZipOutputStream(zipOutputFile); // ZipEntry entry; // add the manifest file so that the .jar can be double clickable addManifest(zos); // add the project's .class files to the jar // (just grabs everything from the build directory, // since there may be some inner classes) // TODO this needs to be recursive (for packages) // File classFiles[] = tempClassesFolder.listFiles(new FilenameFilter() { // public boolean accept(File dir, String name) { // return name.endsWith(".class"); // } // }); // for (File file : classFiles) { // entry = new ZipEntry(file.getName()); // zos.putNextEntry(entry); // zos.write(Base.loadBytesRaw(file)); // zos.closeEntry(); // } addClasses(zos, binFolder); // add the data folder to the main jar file // addDataFolder(zos); // For 2.0a2, make the data folder a separate directory, rather than // packaging potentially large files into the JAR. On OS X, we have to hide // the folder inside the .app package, while Linux and Windows will have a // 'data' folder next to 'lib'. if (sketch.hasDataFolder()) { if (exportPlatform == PConstants.MACOSX) { Util.copyDir(sketch.getDataFolder(), new File(jarFolder, "data")); } else { Util.copyDir(sketch.getDataFolder(), new File(destFolder, "data")); } } // add the contents of the code folder to the jar if (sketch.hasCodeFolder()) { String includes = Util.contentsToClassPath(sketch.getCodeFolder()); // Use tokens to get rid of extra blanks, which causes huge exports String[] codeList = PApplet.splitTokens(includes, File.pathSeparator); for (int i = 0; i < codeList.length; i++) { if (codeList[i].toLowerCase().endsWith(".jar") || codeList[i].toLowerCase().endsWith(".zip")) { File exportFile = new File(codeList[i]); String exportFilename = exportFile.getName(); Util.copyFile(exportFile, new File(jarFolder, exportFilename)); jarList.append(exportFilename); } else { // cp += codeList[i] + File.pathSeparator; } } } zos.flush(); zos.close(); jarList.append(sketch.getName() + ".jar"); /// add contents of 'library' folders to the export for (Library library : importedLibraries) { // add each item from the library folder / export list to the output for (File exportFile : library.getApplicationExports(exportPlatform, exportVariant)) { // System.out.println("export: " + exportFile); String exportName = exportFile.getName(); if (!exportFile.exists()) { System.err.println(exportFile.getName() + " is mentioned in export.txt, but it's " + "a big fat lie and does not exist."); } else if (exportFile.isDirectory()) { Util.copyDir(exportFile, new File(jarFolder, exportName)); } else if (exportName.toLowerCase().endsWith(".zip") || exportName.toLowerCase().endsWith(".jar")) { Util.copyFile(exportFile, new File(jarFolder, exportName)); jarList.append(exportName); } else { // Starting with 2.0a2 put extra export files (DLLs, plugins folder, // anything else for libraries) inside lib or Contents/Resources/Java Util.copyFile(exportFile, new File(jarFolder, exportName)); } } } /// create platform-specific CLASSPATH based on included jars String exportClassPath = null; if (exportPlatform == PConstants.MACOSX) { exportClassPath = "$JAVAROOT/" + jarList.join(":$JAVAROOT/"); } else if (exportPlatform == PConstants.WINDOWS) { exportClassPath = jarList.join(","); } else if (exportPlatform == PConstants.LINUX) { // why is $APPDIR at the front of this list? exportClassPath = "$APPDIR" + ":$APPDIR/lib/" + jarList.join(":$APPDIR/lib/"); } /// figure out run options for the VM StringList runOptions = new StringList(); // Set memory options, except for ARM where we're more memory-constrained // compared to the machine being used to build/export the sketch // https://github.com/processing/processing/pull/4406 if (Preferences.getBoolean("run.options.memory") && !exportVariant.equals("armv6hf")) { runOptions.append("-Xms" + Preferences.get("run.options.memory.initial") + "m"); runOptions.append("-Xmx" + Preferences.get("run.options.memory.maximum") + "m"); } // https://github.com/processing/processing/issues/2239 runOptions.append("-Djna.nosys=true"); // https://github.com/processing/processing/issues/4608 if (embedJava) { // if people don't embed Java, it might be a mess, but what can we do? if (exportPlatform == PConstants.MACOSX) { runOptions.append("-Djava.ext.dirs=$APP_ROOT/Contents/PlugIns/jdk" + PApplet.javaVersionName + ".jdk/Contents/Home/jre/lib/ext"); } else if (exportPlatform == PConstants.WINDOWS) { runOptions.append("-Djava.ext.dirs=\"%EXEDIR%\\java\\lib\\ext\""); } else if (exportPlatform == PConstants.LINUX) { runOptions.append("-Djava.ext.dirs=\"$APPDIR/java/lib/ext\""); } } // https://github.com/processing/processing/issues/2559 if (exportPlatform == PConstants.WINDOWS) { runOptions.append("-Djava.library.path=\"%EXEDIR%\\lib\""); } /// macosx: write out Info.plist (template for classpath, etc) if (exportPlatform == PConstants.MACOSX) { StringBuilder runOptionsXML = new StringBuilder(); for (String opt : runOptions) { runOptionsXML.append(" <string>"); |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |