/
JavaBuild.java
1327 lines (1113 loc) · 49.1 KB
/
JavaBuild.java
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-23 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.*;
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.*;
import processing.app.exec.ProcessHelper;
import processing.app.platform.MacPlatform;
import processing.app.ui.ExportPrompt;
import processing.core.PApplet;
import processing.core.PConstants;
import processing.data.StringList;
import processing.data.XML;
import processing.mode.java.preproc.PdePreprocessor;
import processing.mode.java.preproc.PreprocessorResult;
public class JavaBuild {
static public final String PACKAGE_REGEX =
"(?:^|\\s|;)package\\s+(\\S+)\\;";
static public final String MIN_JAVA_VERSION = "17";
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 SketchException details of where the build choked
*/
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
PreprocessorResult result = preprocess(srcFolder, sizeWarning);
String classNameFound = result.getClassName();
// 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 srcFolder Location to copy all the .java files
* @return null if compilation failed, main class name if not
*/
public PreprocessorResult preprocess(File srcFolder, boolean sizeWarning) throws SketchException {
PdePreprocessor preprocessor = PdePreprocessor.builderFor(sketch.getMainName()).build();
return preprocess(srcFolder, null, preprocessor, 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 PreprocessorResult object containing the preprocessing results
* @throws SketchException details of where the preprocessing failed
*/
public PreprocessorResult 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;
List<Integer> linesPerTab = new ArrayList<>();
for (SketchCode sc : sketch.getCode()) {
if (sc.isExtension("pde")) {
sc.setPreprocOffset(bigCount);
bigCode.append(sc.getProgram());
bigCode.append('\n');
linesPerTab.add(bigCount);
bigCount += sc.getLineCount();
}
}
linesPerTab.add(bigCount);
// // initSketchSize() sets the internal sketchWidth/Height/Renderer vars
// // in the preprocessor. Those are used in preproc.write() so that they
// // can be turned into sketchXxxx() methods.
// // This also returns the size info as an array so that we can figure out
// // if this fella is OpenGL, and if so, to add the import. It's messy and
// // gross and someday we'll just always include OpenGL.
// String[] sizeInfo =
/* next line commented out for ANTLR 4 - PdePreprocessor now does this when
* walking the tree
*/
// preprocessor.initSketchSize(sketch.getMainProgram(), sizeWarning);
// //PdePreprocessor.parseSketchSize(sketch.getMainProgram(), false);
// if (sizeInfo != null) {
// String sketchRenderer = sizeInfo[3];
// if (sketchRenderer != null) {
// if (sketchRenderer.equals("P2D") ||
// sketchRenderer.equals("P3D") ||
// sketchRenderer.equals("OPENGL")) {
// bigCode.insert(0, "import processing.opengl.*; ");
// }
// }
// }
PreprocessorResult result;
try {
File outputFolder = (packageName == null) ?
srcFolder : new File(srcFolder, packageName.replace('.', '/'));
outputFolder.mkdirs();
// Base.openFolder(outputFolder);
final File java = new File(outputFolder, sketch.getMainName() + ".java");
try (PrintWriter stream = PApplet.createWriter(java)) {
result = preprocessor.write(stream, bigCode.toString(), codeFolderPackages);
}
} 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());
}
if (result.getPreprocessIssues().size() > 0) {
Problem problem = ProblemFactory.build(
result.getPreprocessIssues().get(0),
linesPerTab
);
throw new SketchException(
problem.getMessage(),
problem.getTabIndex(),
problem.getLineNumber() - 1,
0
);
}
// 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 (ImportStatement item : result.getImportStatements()) {
String entry = item.getPackageName();
Library library = mode.getLibrary(entry);
if (library != null) {
if (!importedLibraries.contains(library)) {
importedLibraries.add(library);
// don't add the JavaFX libraries to the classpath
// https://github.com/processing/processing4/issues/212
// Disabling this after all, because we need our javafx.jar (PGraphicsJavaFX)
// and the JavaFX .jars are safely tucked into a "modules" subfolder
//if (!library.getName().equals("JavaFX")) {
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) {
for (String pkg : codeFolderPackages) {
if (pkg.equals(entry)) {
found = true;
break;
}
}
}
if (ignorableImport(entry + '.')) {
found = true;
}
if (!found) {
System.err.println("No library found for " + entry);
}
}
}
// Turning this off after 4.0 alpha 5, to see if everything still works.
// Including this classpath is really problematic (many possible conflicts,
// these are classes that won't be available on export, etc etc...)
// Also avoids accidentally hiding other potential classpath bugs.
/*
// 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.getHeaderOffset());
}
}
foundMain = preprocessor.hasMain();
return result;
}
/**
* 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...
*/
@SuppressWarnings("RedundantIfStatement")
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;
if (pkg.startsWith("org.omg")) return true;
if (pkg.startsWith("org.w3c.dom")) return true;
if (pkg.startsWith("org.xml.sax")) 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;
}
// /** Returns the dummy "module" path so that JavaFX doesn't complain. */
// public String getModulePath() {
// // Just set this to the main core/library directory to pick up JavaFX
// //return mode.getCoreLibrary().getLibraryPath();
// File folder = new File(mode.getFolder(), "libraries/javafx/library");
// return folder.getAbsolutePath();
// }
/** 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 dotJavaFilename The .java file where the exception was found.
* @param dotJavaLine 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(message + " 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.getMainName() + ".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.getMainName().equals(foundName)) {
Messages.showWarning("Error during export",
"Main tab is named " + sketch.getMainName() + " but the\n" +
"sketch name in the code was " + foundName, null);
return false;
}
/*
for (StringDict.Entry entry : Platform.getSupportedVariants().entries()) {
String variant = entry.key;
String name = entry.value;
}
*/
final String hostVariant = Platform.getVariant();
for (String variant : Preferences.get(ExportPrompt.EXPORT_VARIANTS).split(",")) {
// Can only embed Java on the native platform
boolean embedJava = variant.equals(hostVariant) &&
Preferences.getBoolean("export.application.embed_java");
File folder = new File(sketch.getFolder(), variant);
if (!exportApplication(folder, variant, embedJava)) {
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)) {
// // removing 32-bit export for 4.0 alpha 3
// /*
// // Don't try to export 32-bit on macOS, because it doesn't exist.
// if (platform != PConstants.MACOS) {
// // 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");
// // No longer including the 64 suffix in 4.0a3 because it's all 64-bit
// folder = new File(sketch.getFolder(), "application." + platformName);
// 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,
String exportVariant,
boolean embedJava) throws IOException, SketchException {
for (Library library : importedLibraries) {
if (library.isUnsupported(exportVariant)) {
Messages.showWarning("Quibbles 'n Bits",
"The application will not be exported for\n" +
Platform.getSupportedVariants().get(exportVariant) +
" because " + library.getName() + "\n" +
"does not support " + exportVariant + ".", null);
return true; // don't cancel all exports for this, just move along
}
}
/// getting started
int exportPlatform = Platform.getIndex(exportVariant);
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.MACOS) {
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)
File stubFile =
mode.getContentFile("application/stub-" + exportVariant);
File execFile =
new File(contentsFolder, "MacOS/" + sketch.getMainName());
Util.copyFile(stubFile, execFile);
if (!execFile.setExecutable(true)) {
throw new IOException("Could not make " + execFile + " executable.");
}
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/application.icns"),
new File(resourcesFolder, "application.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
FileOutputStream zipOutputFile =
new FileOutputStream(new File(jarFolder, sketch.getMainName() + ".jar"));
ZipOutputStream zos = new ZipOutputStream(zipOutputFile);
// add the manifest file so that the .jar can be double-clickable
// (not likely to work anymore these days [fry 200925])
addManifest(zos);
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 macOS, 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.MACOS) {
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);
}
}
}
zos.flush();
zos.close();
jarList.append(sketch.getMainName() + ".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(exportVariant)) {
String exportName = exportFile.getName();
if (!exportFile.exists()) {
System.err.println(exportFile.getName() +
" is mentioned in export.txt," +
" but it 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.MACOS) {
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.contains("arm")) {
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/2559
if (exportPlatform == PConstants.WINDOWS) {
runOptions.append("-Djava.library.path=\"%EXEDIR%\\lib\"");
}
Library javafx = findJavaFX();
if (javafx != null) {
String modulePath = exportPlatform == PConstants.MACOS ?
"$APP_ROOT/Contents/Java/modules" : "lib/modules";
for (String opt : getArgsJavaFX(modulePath)) {
runOptions.append(opt);
}
}
/// macOS: write out Info.plist (template for classpath, etc)
if (exportPlatform == PConstants.MACOS) {
StringBuilder runOptionsXML = new StringBuilder();
for (String opt : runOptions) {
runOptionsXML.append(" <string>");
runOptionsXML.append(opt);
runOptionsXML.append("</string>");
runOptionsXML.append('\n');
}
// read the template, first checking whether the sketch has its own copy
String PLIST_TEMPLATE = "Info.plist.tmpl";
File plistTemplate = new File(sketch.getFolder(), PLIST_TEMPLATE);
if (!plistTemplate.exists()) {
plistTemplate = mode.getContentFile("application/" + PLIST_TEMPLATE);
}
// substitute variables from the template with their values
StringBuilder sb = new StringBuilder(Util.loadFile(plistTemplate));
replaceTags(sb, "jvm_runtime", jvmRuntime);
replaceTags(sb, "jvm_version", MIN_JAVA_VERSION);
replaceTags(sb, "jvm_options_list", runOptionsXML.toString());
replaceTags(sb, "sketch", sketch.getMainName());
replaceTags(sb, "lsuipresentationmode",
Preferences.getBoolean("export.application.present") ? "4" : "0");
replaceTags(sb, "lsarchitecturepriority",
exportVariant.substring("macos-".length()));
// write the plist file
File plistFile = new File(dotAppFolder, "Contents/Info.plist");
PrintWriter pw = PApplet.createWriter(plistFile);
pw.print(sb);
pw.flush();
pw.close();
// attempt to code sign if the Xcode tools appear to be installed
String appPath = dotAppFolder.getAbsolutePath();
if (Platform.isMacOS()) {
if (MacPlatform.isXcodeInstalled()) {
// if (embedJava) {
// ProcessHelper.ffs("codesign", "--force", "--sign", "--deep", "-", jdkPath);
// }
ProcessHelper.ffs("codesign", "--force", "--deep", "--sign", "-", appPath);
} else {
System.err.println("Xcode not installed, install it and manually sign this app:");
System.err.println("codesign --force --deep --sign - " + appPath);
}
}
} else if (exportPlatform == PConstants.WINDOWS) {
File buildFile = new File(destFolder, "launch4j-build.xml");
System.out.println(buildFile);
File configFile = new File(destFolder, "launch4j-config.xml");
XML project = new XML("project");
XML target = project.addChild("target");
target.setString("name", "windows");
XML taskdef = target.addChild("taskdef");
taskdef.setString("name", "launch4j");
taskdef.setString("classname", "net.sf.launch4j.ant.Launch4jTask");
String launchPath = mode.getContentFile("application/launch4j").getAbsolutePath();
taskdef.setString("classpath", launchPath + "/launch4j.jar:" + launchPath + "/lib/xstream.jar");
XML launch4j = target.addChild("launch4j");
// not all launch4j options are available when embedded inside the ant
// build file (i.e. the icon param doesn't work), so use a config file
//<launch4j configFile="windows/work/config.xml" />
launch4j.setString("configFile", configFile.getAbsolutePath());
XML config = new XML("launch4jConfig");
config.addChild("headerType").setContent("gui");
//config.addChild("headerType").setContent("console");
config.addChild("dontWrapJar").setContent("true");
config.addChild("downloadUrl").setContent(ExportPrompt.JAVA_DOWNLOAD_URL);
File exeFile = new File(destFolder, sketch.getName() + ".exe");
config.addChild("outfile").setContent(exeFile.getAbsolutePath());
File iconFile = mode.getContentFile("application/application.ico");
config.addChild("icon").setContent(iconFile.getAbsolutePath());
XML clazzPath = config.addChild("classPath");
clazzPath.addChild("mainClass").setContent(sketch.getMainName());
for (String jarName : jarList) {
clazzPath.addChild("cp").setContent("lib/" + jarName);
}
XML jre = config.addChild("jre");
if (embedJava) {
// also falling back to PATH if the "java" folder is later removed
jre.addChild("path").setContent("java;%PATH%");
} else {
// needed to make OpenJDK work properly
// https://sourceforge.net/p/launch4j/bugs/197/
// https://github.com/processing/processing4/issues/667
jre.addChild("path").setContent("%PATH%");
}
jre.addChild("minVersion").setContent(MIN_JAVA_VERSION);
for (String opt : runOptions) {
jre.addChild("opt").setContent(opt);
}
config.save(configFile);
project.save(buildFile);
if (!buildWindowsLauncher(buildFile, "windows")) {
// don't delete the build file, might be useful for debugging
return false;
}
configFile.delete();
buildFile.delete();
} else {
File shellScript = new File(destFolder, sketch.getName());
PrintWriter pw = PApplet.createWriter(shellScript);
// Do the newlines explicitly so that Windows CRLF
// isn't used when exporting for Unix.
pw.print("#!/bin/sh\n\n");
pw.print("APPDIR=$(readlink -f \"$0\")\n"); // allow symlinks