-
Notifications
You must be signed in to change notification settings - Fork 18
/
CocoaPodsService.java
1351 lines (1219 loc) · 59.1 KB
/
CocoaPodsService.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
package com.defold.extender.services;
import com.defold.extender.ExtenderException;
import com.defold.extender.ExtenderUtil;
import com.defold.extender.ProcessExecutor;
import com.defold.extender.TemplateExecutor;
import com.defold.extender.PlatformConfig;
import com.defold.extender.metrics.MetricsWriter;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.stereotype.Service;
import org.apache.commons.text.StringEscapeUtils;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.Set;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Iterator;
import java.util.Collections;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.lang.StringBuilder;
@Service
public class CocoaPodsService {
public class ResolvedPods {
public List<PodSpec> pods = new ArrayList<>();
public File podsDir;
public File frameworksDir;
public File generatedDir;
public String platformMinVersion;
// In the functions below we also get the values from the parent spec
// if one exists. A parent spec inherits all of its subspecs (unless a
// default_spec is set). And the subspecs inherit the values of their
// parent
// https://guides.cocoapods.org/syntax/podspec.html#subspec
private void addPodLibs(String platform, PodSpec pod, Set<String> libs) {
libs.addAll(pod.libraries.get(platform));
if (pod.parentSpec != null) addPodLibs(platform, pod.parentSpec, libs);
}
public List<String> getAllPodLibs(String platform) {
Set<String> libs = new LinkedHashSet<>();
for (PodSpec pod : pods) {
addPodLibs(platform, pod, libs);
}
return new ArrayList<String>(libs);
}
private void addPodLinkFlags(String platform, PodSpec pod, Set<String> flags) {
flags.addAll(pod.linkflags.get(platform));
if (pod.parentSpec != null) addPodLinkFlags(platform, pod.parentSpec, flags);
}
public List<String> getAllPodLinkFlags(String platform) {
Set<String> flags = new LinkedHashSet<>();
for (PodSpec pod : pods) {
addPodLinkFlags(platform, pod, flags);
}
return new ArrayList<String>(flags);
}
private void addPodResources(String platform, PodSpec pod, Set<String> resources) {
String podDir = pod.dir.getAbsolutePath();
for (String resource : pod.resources.get(platform)) {
resources.add(podDir + "/" + resource);
}
if (pod.parentSpec != null) addPodResources(platform, pod.parentSpec, resources);
}
public List<String> getAllPodResources(String platform) {
Set<String> resources = new LinkedHashSet<>();
for (PodSpec pod : pods) {
addPodResources(platform, pod, resources);
}
return new ArrayList<String>(resources);
}
private void addPodFrameworks(String platform, PodSpec pod, Set<String> frameworks) {
frameworks.addAll(pod.frameworks.get(platform));
if (pod.parentSpec != null) addPodFrameworks(platform, pod.parentSpec, frameworks);
}
public List<String> getAllPodFrameworks(String platform) {
Set<String> frameworks = new LinkedHashSet<>();
for (PodSpec pod : pods) {
addPodFrameworks(platform, pod, frameworks);
}
return new ArrayList<String>(frameworks);
}
private void addPodWeakFrameworks(String platform, PodSpec pod, Set<String> weakFrameworks) {
weakFrameworks.addAll(pod.weakFrameworks.get(platform));
if (pod.parentSpec != null) addPodWeakFrameworks(platform, pod.parentSpec, weakFrameworks);
}
public List<String> getAllPodWeakFrameworks(String platform) {
Set<String> weakFrameworks = new LinkedHashSet<>();
for (PodSpec pod : pods) {
addPodWeakFrameworks(platform, pod, weakFrameworks);
}
return new ArrayList<String>(weakFrameworks);
}
}
public class LanguageSet {
public Set<String> c = new LinkedHashSet<>();
public Set<String> cpp = new LinkedHashSet<>();
public Set<String> objc = new LinkedHashSet<>();
public Set<String> objcpp = new LinkedHashSet<>();
public void add(String value) {
c.add(value);
cpp.add(value);
objc.add(value);
objcpp.add(value);
}
public void addAll(List<String> values) {
for (String v : values) {
add(v);
}
}
public void addAll(LanguageSet set) {
c.addAll(set.c);
cpp.addAll(set.cpp);
objc.addAll(set.objc);
objcpp.addAll(set.objcpp);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("c: " + c);
sb.append(" cpp: " + cpp);
sb.append(" objc: " + objc);
sb.append(" objcpp: " + objcpp);
return sb.toString();
}
}
public class PlatformAndLanguageSet {
public LanguageSet ios = new LanguageSet();
public LanguageSet osx = new LanguageSet();
public void addAll(PlatformAndLanguageSet v) {
ios.addAll(v.ios);
osx.addAll(v.osx);
}
public void addAll(List<String> values) {
for (String v : values) {
ios.add(v);
osx.add(v);
}
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("ios: " + ios.toString());
sb.append(" osx: " + osx.toString());
return sb.toString();
}
}
public class PlatformSet {
public Set<String> ios = new LinkedHashSet<>();
public Set<String> osx = new LinkedHashSet<>();
public void addAll(PlatformSet v) {
ios.addAll(v.ios);
osx.addAll(v.osx);
}
public void addAll(List<String> values) {
for (String v : values) {
ios.add(v);
osx.add(v);
}
}
public void add(String value) {
ios.add(value);
osx.add(value);
}
public Set<String> get(String platform) {
if (platform.contains("ios")) {
return ios;
}
else if (platform.contains("osx")) {
return osx;
}
return new LinkedHashSet<String>();
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("ios: " + ios.toString());
sb.append(" osx: " + osx.toString());
return sb.toString();
}
}
public class PodSpec {
public String name = "";
public String moduleName = "";
public String version = "";
public String originalJSON = "";
public String iosversion = "";
public String osxversion = "";
public String iosModuleMap = null;
public String osxModuleMap = null;
public String iosUmbrellaHeader = null;
public String osxUmbrellaHeader = null;
// Set to true if this Pod contains at least one .xcframework
public Boolean containsFramework = false;
// The Swift source file header (ModuleName-Swift.h)
// This file is referenced from the modulemap and generated in
// Extender.java as part of the process when building .swift files
public File swiftModuleHeader = null;
public Set<String> swiftSourceFilePaths = new LinkedHashSet<>();
public Set<File> swiftSourceFiles = new LinkedHashSet<>();
public Set<File> sourceFiles = new LinkedHashSet<>();
public Set<File> includePaths = new LinkedHashSet<>();
public PodSpec parentSpec = null;
public List<String> defaultSubspecs = new ArrayList<>();
public List<PodSpec> subspecs = new ArrayList<>();
public List<String> dependencies = new ArrayList<>();
public PlatformAndLanguageSet flags = new PlatformAndLanguageSet();
public PlatformSet defines = new PlatformSet();
public PlatformSet linkflags = new PlatformSet();
public Set<String> vendoredFrameworks = new LinkedHashSet<>();
public PlatformSet weakFrameworks = new PlatformSet();
public PlatformSet resources = new PlatformSet();
public PlatformSet frameworks = new PlatformSet();
public PlatformSet libraries = new PlatformSet();
public PlatformSet publicHeaders = new PlatformSet();
public File dir;
public File generatedDir;
// true if this podspec was installed by Cocoapods
public boolean installed;
public PodSpec getSubspec(String name) {
for (PodSpec spec : subspecs) {
if (spec.name.equals(name)) {
return spec;
}
}
return null;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(name + ":" + version + "\n");
sb.append(" dir: " + dir + "\n");
sb.append(" moduleName: " + moduleName + "\n");
sb.append(" generated dir: " + generatedDir + "\n");
sb.append(" installed: " + installed + "\n");
sb.append(" src: " + sourceFiles + "\n");
sb.append(" swift src: " + swiftSourceFiles + "\n");
sb.append(" swift module header: " + swiftModuleHeader + "\n");
sb.append(" includes: " + includePaths + "\n");
sb.append(" defines: " + defines + "\n");
sb.append(" flags: " + flags + "\n");
sb.append(" linkflags: " + linkflags + "\n");
sb.append(" weak_frameworks: " + weakFrameworks + "\n");
sb.append(" resources: " + resources + "\n");
sb.append(" frameworks: " + frameworks + "\n");
sb.append(" vendored_frameworks: " + vendoredFrameworks + "\n");
sb.append(" libraries: " + libraries + "\n");
sb.append(" dependencies: " + dependencies + "\n");
sb.append(" parentSpec: " + ((parentSpec != null) ? parentSpec.name : "null") + "\n");
sb.append(" default_subspecs: " + defaultSubspecs + "\n");
for (PodSpec sub : subspecs) {
sb.append(" subspec: " + sub.name + "\n");
}
return sb.toString();
}
}
private static final String PODFILE_TEMPLATE_PATH = System.getenv("EXTENSION_PODFILE_TEMPLATE");
private static final String MODULEMAP_TEMPLATE_PATH = System.getenv("EXTENSION_MODULEMAP_TEMPLATE");
private static final String UMBRELLAHEADER_TEMPLATE_PATH = System.getenv("EXTENSION_UMBRELLAHEADER_TEMPLATE");
private static final Logger LOGGER = LoggerFactory.getLogger(CocoaPodsService.class);
private final TemplateExecutor templateExecutor = new TemplateExecutor();
private final String podfileTemplateContents;
private final String modulemapTemplateContents;
private final String umbrellaHeaderTemplateContents;
private final MeterRegistry meterRegistry;
private String readFile(String filePath) throws IOException {
if (filePath == null) {
return "";
}
return new String( Files.readAllBytes( Paths.get(filePath) ) );
}
@Autowired
CocoaPodsService(@Value("${extender.gradle.cache-size}") int cacheSize,
MeterRegistry meterRegistry) throws IOException {
this.meterRegistry = meterRegistry;
this.podfileTemplateContents = readFile(PODFILE_TEMPLATE_PATH);
this.modulemapTemplateContents = readFile(MODULEMAP_TEMPLATE_PATH);
this.umbrellaHeaderTemplateContents = readFile(UMBRELLAHEADER_TEMPLATE_PATH);
LOGGER.info("CocoaPodsService service");
}
// debugging function for printing a directory structure with files and folders
private static void dumpDir(File file, int indent) throws IOException {
String indentString = "";
for (int i = 0; i < indent; i++) {
indentString += "-";
}
LOGGER.info(indentString + file.getName());
if (file.isDirectory()) {
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
dumpDir(files[i], indent + 3);
}
}
}
// https://www.baeldung.com/java-comparing-versions#customSolution
private int compareVersions(String version1, String version2) {
int result = 0;
String[] parts1 = version1.split("\\.");
String[] parts2 = version2.split("\\.");
int length = Math.max(parts1.length, parts2.length);
for (int i = 0; i < length; i++){
Integer v1 = i < parts1.length ? Integer.parseInt(parts1[i]) : 0;
Integer v2 = i < parts2.length ? Integer.parseInt(parts2[i]) : 0;
int compare = v1.compareTo(v2);
if (compare != 0) {
result = compare;
break;
}
}
return result;
}
/**
* Create the main Podfile with a list of all dependencies for all uploaded extensions
* @param podFiles List of podfiles to merge into the main Pofile
* @param jobDirectory The job directory from where to search for Podfiles
* @param workingDir The working directory where pods should be resolved
* @param platform For which platform to resolve pods
* @return Minimum platform version
*/
private String createMainPodFile(List<File> podFiles, File jobDirectory, File workingDir, String platform, Map<String, Object> jobEnvContext) throws IOException {
File mainPodFile = new File(workingDir, "Podfile");
// This file might exist when testing and debugging the extender using a debug job folder
podFiles.remove(mainPodFile);
String mainPodfilePlatformVersion = (platform.contains("ios") ?
jobEnvContext.get("env.IOS_VERSION_MIN").toString():
jobEnvContext.get("env.MACOS_VERSION_MIN").toString());
String mainPodfilePlatform = (platform.contains("ios") ? "ios" : "osx");
// Load all Podfiles
List<String> pods = new ArrayList<>();
for (File podFile : podFiles) {
// Split each file into lines and go through them one by one
// Search for a Podfile platform and version configuration, examples:
// platform :ios, '9.0'
// platform :osx, '10.2'
// Get the version and figure out which is the highest version defined. This
// version will be used in the combined Podfile created by this function.
// Treat everything else as pods
List<String> lines = Files.readAllLines(podFile.getAbsoluteFile().toPath());
for (String line : lines) {
if (line.startsWith("platform :")) {
String version = line.replaceFirst("platform :ios|platform :osx", "").replace(",", "").replace("'", "").trim();
if (!version.isEmpty() && (compareVersions(version, mainPodfilePlatformVersion) > 0)) {
mainPodfilePlatformVersion = version;
}
}
else {
pods.add(line);
}
}
}
// Create main Podfile contents
HashMap<String, Object> envContext = new HashMap<>();
envContext.put("PLATFORM", mainPodfilePlatform);
envContext.put("PLATFORM_VERSION", mainPodfilePlatformVersion);
envContext.put("PODS", pods);
String mainPodFileContents = templateExecutor.execute(podfileTemplateContents, envContext);
LOGGER.info("Created main Podfile:\n{}", mainPodFileContents);
Files.write(mainPodFile.toPath(), mainPodFileContents.getBytes());
return mainPodfilePlatformVersion;
}
/**
* Add source files matching a pattern to a pod
* @param pod
* @param pattern Source file pattern (glob format)
*/
private void addPodSourceFiles(PodSpec pod, String pattern) {
String absolutePatternPath = pod.dir.getAbsolutePath() + File.separator + pattern;
List<File> podSrcFiles = listFilesGlob(pod.dir, absolutePatternPath);
for (File podSrcFile : podSrcFiles) {
final String filename = podSrcFile.getName();
if (filename.endsWith(".swift")) {
pod.swiftSourceFiles.add(podSrcFile);
pod.swiftSourceFilePaths.add(podSrcFile.getAbsolutePath());
}
else if (!filename.endsWith(".h")) {
pod.sourceFiles.add(podSrcFile);
}
}
}
/**
* Add a list of include paths matching a pattern to a pod
* @param pod
* @param pattern Source file pattern (glob format)
*/
private void addPodIncludePaths(PodSpec pod, String pattern) {
String absolutePatternPath = pod.dir.getAbsolutePath() + File.separator + pattern;
List<File> podSrcFiles = listFilesGlob(pod.dir, absolutePatternPath);
for (File podSrcFile : podSrcFiles) {
if (podSrcFile.getName().endsWith(".h")) {
File podIncludeDir = podSrcFile.getParentFile();
if (podIncludeDir != null) {
pod.includePaths.add(podIncludeDir);
File podIncludeParentDir = podIncludeDir.getParentFile();
if (podIncludeParentDir != null) {
pod.includePaths.add(podIncludeParentDir);
}
}
}
}
}
// copy the files and folders of a directory recursively
// the function will also resolve symlinks while copying files and folders
private void copyDirectoryRecursively(File fromDir, File toDir) throws IOException {
toDir.mkdirs();
File resolved = fromDir.toPath().toRealPath().toFile();
File[] files = fromDir.toPath().toRealPath().toFile().listFiles();
for (File file : files) {
if (file.isDirectory()) {
File sourceDir = file;
File destDir = new File(toDir, file.getName());
copyDirectoryRecursively(sourceDir, destDir);
}
else {
// follow symlink (if one exists) and then copy file
File sourceFile = file.toPath().toRealPath().toFile();
File destFile = new File(toDir, sourceFile.getName());
Files.copy(sourceFile.toPath(), destFile.toPath());
}
}
}
private void stripBitcode(File file, PlatformConfig config) throws ExtenderException {
String command = null;
// bitcodeStripCmd added in 1.8.1
if (config.bitcodeStripCmd != null) {
Map<String, Object> context = new HashMap<>(config.context);
context.put("source", file.getAbsolutePath());
context.put("target", file.getAbsolutePath());
command = templateExecutor.execute(config.bitcodeStripCmd, context);
}
else {
command = String.format("bitcode_strip %s -r -o %s", file.getAbsolutePath(), file.getAbsolutePath());
}
String log = execCommand(command);
LOGGER.info("\n" + log);
}
// helper function to copy the frameworks, static libs and headers from a
// platform architecture folder (inside an .xcframework)
private void copyPodFrameworksFromArchitectureDir(File architectureDir, File destFrameworkDir, File destHeaderDir, PlatformConfig config) throws IOException, ExtenderException {
File[] files = architectureDir.listFiles();
for (File file : files) {
String filename = file.getName();
if (filename.endsWith(".framework")) {
String frameworkName = filename.replace(".framework", "");
File lib = new File(file, frameworkName);
if (lib.exists()) {
stripBitcode(lib, config);
}
File from = file;
File to = new File(destFrameworkDir, filename);
copyDirectoryRecursively(from, to);
}
// static libs
else if (filename.endsWith(".a")) {
Path from = file.toPath().toRealPath();
Path to = new File(destFrameworkDir, filename).toPath();
Files.copy(from, to);
}
// headers (for the static libs)
else if (filename.equals("Headers")) {
File from = file;
File to = destHeaderDir;
copyDirectoryRecursively(from, to);
}
}
}
/**
* Find all .xcframeworks (vendored frameworks) in a list of pods and copy any arm
* and x86 .framworks for use later when building extensions using the pods
* @param pods The pods to process
* @param frameworksDir Directory where to copy frameworks and framework libs
* @param config Platform config
*/
private void copyPodFrameworks(List<PodSpec> pods, File frameworksDir, PlatformConfig config) throws IOException, ExtenderException {
LOGGER.info("Copying pod frameworks");
File libDir = new File(frameworksDir, "lib");
File armLibDir = new File(libDir, "arm64-ios");
File x86LibDir = new File(libDir, "x86_64-ios");
armLibDir.mkdirs();
x86LibDir.mkdirs();
File headersDir = new File(frameworksDir, "headers");
File armHeaderDir = new File(headersDir, "arm64-ios");
File x86HeaderDir = new File(headersDir, "x86_64-ios");
armHeaderDir.mkdirs();
x86HeaderDir.mkdirs();
for (PodSpec pod : pods) {
for (String framework : pod.vendoredFrameworks) {
File frameworkDir = new File(pod.dir, framework);
String frameworkName = frameworkDir.getName().replace(".xcframework", "");
File arm64_armv7FrameworkDir = new File(frameworkDir, "ios-arm64_armv7");
File arm64FrameworkDir = new File(frameworkDir, "ios-arm64");
if (arm64_armv7FrameworkDir.exists()) {
copyPodFrameworksFromArchitectureDir(arm64_armv7FrameworkDir, armLibDir, armHeaderDir, config);
}
else if (arm64FrameworkDir.exists()) {
copyPodFrameworksFromArchitectureDir(arm64FrameworkDir, armLibDir, armHeaderDir, config);
}
File arm64_i386_x86Framework = new File(frameworkDir, "ios-arm64_i386_x86_64-simulator");
File arm64_x86Framework = new File(frameworkDir, "ios-arm64_x86_64-simulator");
if (arm64_i386_x86Framework.exists()) {
copyPodFrameworksFromArchitectureDir(arm64_i386_x86Framework, x86LibDir, x86HeaderDir, config);
}
else if (arm64_x86Framework.exists()) {
copyPodFrameworksFromArchitectureDir(arm64_x86Framework, x86LibDir, x86HeaderDir, config);
}
}
}
}
private JSONObject parseJson(String json) throws ExtenderException {
try {
JSONParser parser = new JSONParser();
return (JSONObject)parser.parse(json);
}
catch (ParseException e) {
e.printStackTrace();
throw new ExtenderException(e, "Failed to parse json. " + e);
}
}
// get the value of a key as a JSONArray even it is a single value
private JSONArray getAsJSONArray(JSONObject o, String key) {
Object value = o.get(key);
if (value instanceof JSONArray) {
return (JSONArray)value;
}
JSONArray a = new JSONArray();
if (value != null) {
a.add(value.toString());
}
return a;
}
private List<String> unescapeStrings(List<String> strings) {
List<String> unescapedStrings = new ArrayList<>();
for (String s : strings) {
unescapedStrings.add(StringEscapeUtils.unescapeJava(s));
}
return unescapedStrings;
}
// get a string value from a JSON object and split it into a list using space character as delimiter
// will return an empty list if the value does not exist
private List<String> getAsSplitString(JSONObject o, String key) {
String value = (String)o.get(key);
if (value == null || value.trim().isEmpty()) {
return new ArrayList<>();
}
return Arrays.asList(value.split(" "));
}
// check if a string value on a JSON object exists
// will return false if the value doesn't exist or is an empty string
private boolean hasString(JSONObject o, String key) {
String value = (String)o.get(key);
if (value == null || value.trim().isEmpty()) {
return false;
}
return true;
}
// get a string value from a JSON object
// will return a default value if the value doesn't exist or is an empty string
private String getAsString(JSONObject o, String key, String defaultValue) {
String value = (String)o.get(key);
if (value == null || value.trim().isEmpty()) {
return defaultValue;
}
return value;
}
// get a string value from a JSON object
// will return null if the value doesn't exist or is an empty string
private String getAsString(JSONObject o, String key) {
return getAsString(o, key, null);
}
// check if the value for a specific key on a json object matches an expected value
private boolean compareString(JSONObject o, String key, String expected) {
String value = (String)o.get(key);
if (value == null || value.trim().isEmpty()) {
return false;
}
return value.equals(expected);
}
private void parseConfig(JSONObject config, LanguageSet flags, Set<String> linkflags, Set<String> defines) {
// https://pewpewthespells.com/blog/buildsettings.html
// defines
if (hasString(config, "GCC_PREPROCESSOR_DEFINITIONS")) {
defines.addAll(unescapeStrings(getAsSplitString(config, "GCC_PREPROCESSOR_DEFINITIONS")));
}
// linker flags
// https://xcodebuildsettings.com/#other_ldflags
if (hasString(config, "OTHER_LDFLAGS")) {
linkflags.addAll(getAsSplitString(config, "OTHER_LDFLAGS"));
}
// compiler flags for c and objc files
// https://xcodebuildsettings.com/#other_cflags
if (hasString(config, "OTHER_CFLAGS")) {
flags.c.addAll(getAsSplitString(config, "OTHER_CFLAGS"));
flags.objc.addAll(getAsSplitString(config, "OTHER_CFLAGS"));
}
// compiler flags
if (hasString(config, "CLANG_CXX_LANGUAGE_STANDARD")) {
String cppStandard = getAsString(config, "CLANG_CXX_LANGUAGE_STANDARD", "compiler-default");
String compilerFlag = "";
switch (cppStandard) {
case "c++98": compilerFlag = "-std=c++98"; break;
case "c++0x": compilerFlag = "-std=c++11"; break;
case "gnu++0x": compilerFlag = "-std=gnu++11"; break;
case "c++14": compilerFlag = "-std=c++1y"; break;
case "gnu++14": compilerFlag = "-std=gnu++1y"; break;
case "gnu++98":
case "compiler-default":
default: compilerFlag = "-std=gnu++98"; break;
}
flags.cpp.add(compilerFlag);
flags.objcpp.add(compilerFlag);
}
if (hasString(config, "GCC_C_LANGUAGE_STANDARD")) {
String cStandard = getAsString(config, "GCC_C_LANGUAGE_STANDARD", "compiler-default");
String compilerFlag = "";
switch (cStandard) {
case "ansi": compilerFlag = "-ansi"; break;
case "c89": compilerFlag = "-std=c89"; break;
case "gnu89": compilerFlag = "-std=gnu89"; break;
case "c99": compilerFlag = "-std=c99"; break;
case "c11": compilerFlag = "-std=c11"; break;
case "gnu11": compilerFlag = "-std=gnu11"; break;
case "gnu99":
case "compiler-default":
default: compilerFlag = "-std=gnu99"; break;
}
flags.c.add(compilerFlag);
}
if (hasString(config, "CLANG_CXX_LIBRARY")) {
String stdLib = getAsString(config, "CLANG_CXX_LIBRARY", "compiler-default");
String stdLibFlag = "";
switch (stdLib) {
case "libc++": stdLibFlag = "-stdlib=libc++"; break;
case "libstdc++":
case "compiler-default":
default: stdLibFlag = "-stdlib=libstdlibc++"; break;
}
flags.cpp.add(stdLibFlag);
flags.objcpp.add(stdLibFlag);
}
if (compareString(config, "GCC_ENABLE_CPP_EXCEPTIONS", "YES")) {
flags.cpp.add("-fcxx-exceptions");
}
if (compareString(config, "GCC_ENABLE_CPP_EXCEPTIONS", "NO")) {
flags.cpp.add("-fno-cxx-exceptions");
}
if (compareString(config, "GCC_ENABLE_EXCEPTIONS", "YES")) {
flags.add("-fexceptions");
}
if (compareString(config, "GCC_ENABLE_EXCEPTIONS", "NO")) {
flags.add("-fno-exceptions");
}
if (compareString(config, "GCC_ENABLE_OBJC_EXCEPTIONS", "YES")) {
flags.objc.add("-fobjc-exceptions");
flags.objcpp.add("-fobjc-exceptions");
}
if (compareString(config, "GCC_ENABLE_OBJC_EXCEPTIONS", "NO")) {
flags.objc.add("-fno-objc-exceptions");
flags.objcpp.add("-fno-objc-exceptions");
}
if (compareString(config, "GCC_ENABLE_CPP_RTTI", "YES")) {
flags.cpp.add("-frtti");
}
if (compareString(config, "GCC_ENABLE_CPP_RTTI", "NO")) {
flags.cpp.add("-fno-rtti");
}
if (compareString(config, "GCC_ENABLE_OBJC_GC", "supported")) {
flags.objc.add("-fobjc-gc");
flags.objcpp.add("-fobjc-gc");
}
if (compareString(config, "GCC_ENABLE_OBJC_GC", "required")) {
flags.objc.add("-fobjc-gc-only");
flags.objcpp.add("-fobjc-gc-only");
}
if (compareString(config, "GCC_ENABLE_ASM_KEYWORD", "YES")) {
flags.add("-fasm");
}
if (compareString(config, "GCC_ENABLE_ASM_KEYWORD", "NO")) {
flags.add("-fno-asm");
}
}
private void parseMultiPlatformConfig(PodSpec spec, JSONObject config) {
if (config != null) {
parseConfig(config, spec.flags.ios, spec.linkflags.ios, spec.defines.ios);
parseConfig(config, spec.flags.osx, spec.linkflags.osx, spec.defines.osx);
JSONObject iosConfig = (JSONObject)config.get("ios");
JSONObject osxConfig = (JSONObject)config.get("ios");
if (iosConfig != null) parseConfig(iosConfig, spec.flags.ios, spec.linkflags.ios, spec.defines.ios);
if (osxConfig != null) parseConfig(osxConfig, spec.flags.osx, spec.linkflags.osx, spec.defines.osx);
}
}
private List<File> listFilesGlob(File dir, String pattern) {
List<File> files = ExtenderUtil.listFilesGlob(dir, pattern);
// Cocoapods uses Ruby where glob patterns are treated slightly differently:
// Ruby: foo/**/*.h will find .h files in any subdirectory of foo AND in foo/
// Java: foo/**/*.h will find .h files in any subdirectory of foo but NOT in foo/
if (pattern.contains("/**/")) {
pattern = pattern.replaceFirst("\\/\\*\\*\\/", "/");
files.addAll(ExtenderUtil.listFilesGlob(dir, pattern));
}
return files;
}
private void createUmbrellaHeader(PodSpec pod, Set<String> headerPatterns, File umbrellaHeaderFile, File jobDir) throws ExtenderException {
LOGGER.info("createUmbrellaHeader() " + pod.moduleName + " from header patterns: " + headerPatterns + " " + umbrellaHeaderFile);
List<String> headers = new ArrayList<>();
for (String headerPattern : headerPatterns) {
String absoluteHeaderPatternPath = pod.dir.getAbsolutePath() + File.separator + headerPattern;
List<File> headerFiles = listFilesGlob(pod.dir, absoluteHeaderPatternPath);
for (File headerFile : headerFiles) {
headers.add(headerFile.getAbsolutePath());
}
}
HashMap<String, Object> envContext = new HashMap<>();
envContext.put("MODULE_ID", pod.moduleName);
envContext.put("HEADERS", headers);
String umbrellaHeader = templateExecutor.execute(umbrellaHeaderTemplateContents, envContext);
try {
Files.writeString(umbrellaHeaderFile.toPath(), umbrellaHeader);
}
catch (IOException e) {
throw new ExtenderException(e, "Unable to create umbrella header for " + pod.moduleName);
}
}
private String createIosUmbrellaHeader(PodSpec pod, File jobDir) throws ExtenderException {
File umbrellaHeaderFile = new File(pod.generatedDir, pod.moduleName + "-umbrella.h");
createUmbrellaHeader(pod, pod.publicHeaders.ios, umbrellaHeaderFile, jobDir);
return umbrellaHeaderFile.getAbsolutePath();
}
private String createOsxUmbrellaHeader(PodSpec pod, File jobDir) throws ExtenderException {
File umbrellaHeaderFile = new File(pod.generatedDir, pod.moduleName + "-umbrella.h");
createUmbrellaHeader(pod, pod.publicHeaders.osx, umbrellaHeaderFile, jobDir);
return umbrellaHeaderFile.getAbsolutePath();
}
// https://clang.llvm.org/docs/Modules.html#module-declaration
private void createModuleMap(PodSpec pod, Set<String> headerPatterns, String umbrellaHeader, File moduleMapFile, File jobDir) throws ExtenderException {
LOGGER.info("createModuleMap() " + pod.moduleName + " from header patterns: " + headerPatterns + " " + moduleMapFile);
List<String> headers = new ArrayList<>();
// swift source file header
// generated by the swift compiler if -emit-objc-header flag is set
if (!pod.swiftSourceFiles.isEmpty()) {
File swiftModuleHeader = new File(pod.generatedDir, pod.moduleName + "-Swift.h");
pod.swiftModuleHeader = swiftModuleHeader;
headers.add(swiftModuleHeader.getAbsolutePath());
}
// module name, including parent spec module name
String fullModuleName = ((pod.parentSpec != null) ? (pod.parentSpec.moduleName + "_") : "") + pod.moduleName;
// generate final modulemap
HashMap<String, Object> envContext = new HashMap<>();
envContext.put("FRAMEWORKOPT", pod.containsFramework ? "framework" : "");
envContext.put("MODULE_ID", fullModuleName);
envContext.put("HEADERS", headers);
envContext.put("UMBRELLA_HEADER", umbrellaHeader);
envContext.put("SUBMODULE", pod.containsFramework ? "module * { export * }" : "");
String moduleMap = templateExecutor.execute(modulemapTemplateContents, envContext);
try {
Files.writeString(moduleMapFile.toPath(), moduleMap);
}
catch (IOException e) {
throw new ExtenderException(e, "Unable to create modulemap for " + pod.moduleName);
}
}
private String createIosModuleMap(PodSpec pod, File jobDir) throws ExtenderException {
File moduleMapFile = new File(pod.generatedDir, "module.modulemap");
createModuleMap(pod, pod.publicHeaders.ios, pod.iosUmbrellaHeader, moduleMapFile, jobDir);
return moduleMapFile.getAbsolutePath();
}
private String createOsxModuleMap(PodSpec pod, File jobDir) throws ExtenderException {
File moduleMapFile = new File(pod.generatedDir, "module.modulemap");
createModuleMap(pod, pod.publicHeaders.osx, pod.osxUmbrellaHeader, moduleMapFile, jobDir);
return moduleMapFile.getAbsolutePath();
}
// https://guides.cocoapods.org/syntax/podspec.html
private PodSpec createPodSpec(JSONObject specJson, PodSpec parent, File podsDir, File generatedDir, File jobDir, Map<String, Object> jobEnvContext) throws ExtenderException {
PodSpec spec = new PodSpec();
spec.name = (String)specJson.get("name");
if (specJson.containsKey("module_name")) {
spec.moduleName = (String)specJson.get("module_name");
}
else {
// NSData+zlib -> NSData
spec.moduleName = ((String)specJson.get("name")).replaceAll("[\\+].*", "");
}
spec.version = (parent == null) ? (String)specJson.get("version") : parent.version;
spec.dir = (parent == null) ? new File(podsDir, spec.name) : parent.dir;
spec.parentSpec = parent;
if (parent == null) {
spec.installed = spec.dir.exists();
}
else {
spec.installed = (new File(new File(spec.dir, spec.parentSpec.name), spec.name)).exists();
}
// generated files relating to the pod
// modulemap, swift header etc
spec.generatedDir = new File(generatedDir, spec.name);
spec.generatedDir.mkdirs();
// inherit flags and defines from the parent
if (parent != null) {
spec.flags.ios.addAll(parent.flags.ios);
spec.flags.osx.addAll(parent.flags.osx);
spec.defines.ios.addAll(parent.defines.ios);
spec.defines.osx.addAll(parent.defines.osx);
spec.linkflags.ios.addAll(parent.linkflags.ios);
spec.linkflags.ios.addAll(parent.linkflags.ios);
}
// platform versions
JSONObject platforms = (JSONObject)specJson.get("platforms");
if (platforms != null) {
spec.iosversion = (String)platforms.getOrDefault("ios", jobEnvContext.get("env.IOS_VERSION_MIN"));
}
if (platforms != null) {
spec.osxversion = (String)platforms.getOrDefault("osx", jobEnvContext.get("env.MACOS_VERSION_MIN"));
}
// for multi platform settings
JSONObject ios = (JSONObject)specJson.get("ios");
JSONObject osx = (JSONObject)specJson.get("osx");
// flags and defines
// https://guides.cocoapods.org/syntax/podspec.html#pod_target_xcconfig
// https://guides.cocoapods.org/syntax/podspec.html#user_target_xcconfig
parseMultiPlatformConfig(spec, (JSONObject)specJson.get("pod_target_xcconfig"));
parseMultiPlatformConfig(spec, (JSONObject)specJson.get("user_target_xcconfig")); // not recommended for use but we need to handle it
parseMultiPlatformConfig(spec, (JSONObject)specJson.get("xcconfig")); // undocumented but used by some pods
// requires_arc flag
// The 'requires_arc' option can also be a file pattern string or array
// of files where arc should be enabled. See:
// https://guides.cocoapods.org/syntax/podspec.html#requires_arc
//
// This is currently not supported and the presence of a string or array
// will be treated as the default value (ie true)
Boolean requiresArc = true;
Object requiresArcObject = specJson.get("requires_arc");
if (requiresArcObject instanceof Boolean) requiresArc = (Boolean)requiresArcObject;
spec.flags.ios.objc.add((requiresArc == null || requiresArc == true) ? "-fobjc-arc" : "-fno-objc-arc");
spec.flags.ios.objcpp.add((requiresArc == null || requiresArc == true) ? "-fobjc-arc" : "-fno-objc-arc");
spec.flags.osx.objc.add((requiresArc == null || requiresArc == true) ? "-fobjc-arc" : "-fno-objc-arc");
spec.flags.osx.objcpp.add((requiresArc == null || requiresArc == true) ? "-fobjc-arc" : "-fno-objc-arc");
// compiler flags
// https://guides.cocoapods.org/syntax/podspec.html#compiler_flags
spec.flags.ios.addAll(getAsSplitString(specJson, "compiler_flags"));
spec.flags.osx.addAll(getAsSplitString(specJson, "compiler_flags"));
if (ios != null) spec.flags.ios.addAll(getAsJSONArray(ios, "compiler_flags"));
if (osx != null) spec.flags.osx.addAll(getAsJSONArray(osx, "compiler_flags"));
spec.flags.ios.c.add("--language=c");
spec.flags.osx.c.add("--language=c");
spec.flags.ios.cpp.add("--language=c++");
spec.flags.osx.cpp.add("--language=c++");
spec.flags.ios.objc.add("--language=objective-c");
spec.flags.osx.objc.add("--language=objective-c");
spec.flags.ios.objcpp.add("--language=objective-c++");
spec.flags.osx.objcpp.add("--language=objective-c++");
// CocoaPods sets CLANG_ENABLE_MODULES when creating an XCode project
// https://xcodebuildsettings.com/#clang_enable_modules
spec.flags.ios.objc.add("-fmodules");
spec.flags.osx.objc.add("-fmodules");
spec.flags.ios.objcpp.add("-fcxx-modules");
spec.flags.osx.objcpp.add("-fcxx-modules");
if (spec.parentSpec == null) {
spec.flags.ios.objc.add("-fmodule-name=" + spec.moduleName);
spec.flags.osx.objc.add("-fmodule-name=" + spec.moduleName);
spec.flags.ios.objcpp.add("-fmodule-name=" + spec.moduleName);
}
// resources
// https://guides.cocoapods.org/syntax/podspec.html#resources
spec.resources.addAll(getAsJSONArray(specJson, "resource"));
spec.resources.addAll(getAsJSONArray(specJson, "resources"));
if (ios != null) spec.resources.ios.addAll(getAsJSONArray(ios, "resource"));
if (osx != null) spec.resources.osx.addAll(getAsJSONArray(osx, "resource"));
if (ios != null) spec.resources.ios.addAll(getAsJSONArray(ios, "resources"));
if (osx != null) spec.resources.osx.addAll(getAsJSONArray(osx, "resources"));
// frameworks
// https://guides.cocoapods.org/syntax/podspec.html#frameworks
spec.frameworks.addAll(getAsJSONArray(specJson, "frameworks"));
if (ios != null) spec.frameworks.ios.addAll(getAsJSONArray(ios, "frameworks"));
if (osx != null) spec.frameworks.osx.addAll(getAsJSONArray(osx, "frameworks"));
// weak frameworks
// https://guides.cocoapods.org/syntax/podspec.html#weak_frameworks
spec.weakFrameworks.addAll(getAsJSONArray(specJson, "weak_frameworks"));
if (ios != null) spec.weakFrameworks.ios.addAll(getAsJSONArray(ios, "weak_frameworks"));
if (osx != null) spec.weakFrameworks.osx.addAll(getAsJSONArray(osx, "weak_frameworks"));
// vendored frameworks
// https://guides.cocoapods.org/syntax/podspec.html#vendored_frameworks
JSONArray vendored = getAsJSONArray(specJson, "vendored_frameworks");
if (vendored != null) {
spec.vendoredFrameworks.addAll(vendored);
}
if (ios != null) {
JSONArray ios_vendored = getAsJSONArray(ios, "vendored_frameworks");
if (ios_vendored != null) {
spec.vendoredFrameworks.addAll(ios_vendored);
}
}
if (osx != null) {