-
Notifications
You must be signed in to change notification settings - Fork 43
/
J2objcConfig.groovy
986 lines (898 loc) · 38.2 KB
/
J2objcConfig.groovy
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
/*
* Copyright (c) 2015 the authors of j2objc-gradle (see AUTHORS file)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.j2objccontrib.j2objcgradle
import com.github.j2objccontrib.j2objcgradle.tasks.Utils
import com.google.common.annotations.VisibleForTesting
import groovy.transform.CompileStatic
import groovy.transform.TypeCheckingMode
import org.gradle.api.InvalidUserDataException
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.tasks.util.PatternSet
import org.gradle.util.ConfigureUtil
/**
* j2objcConfig is used to configure the plugin with the project's build.gradle.
*
* All paths are resolved using Gradle's <a href="https://docs.gradle.org/current/javadoc/org/gradle/api/Project.html#file(java.lang.Object)">project.file(...)</a>
*
*
* Basic Example:
*
* j2objcConfig {
* xcodeProjectDir '../ios'
* xcodeTarget 'IOS-APP'
* finalConfigure()
* }
*
*
* Complex Example:
*
* TODO...
*
*/
@CompileStatic
class J2objcConfig {
static J2objcConfig from(Project project) {
return project.extensions.findByType(J2objcConfig)
}
final protected Project project
J2objcConfig(Project project) {
assert project != null
this.project = project
// The NativeCompilation effectively provides further extensions
// to the Project, by configuring the 'Objective-C' native build plugin.
// We don't want to expose the instance to clients of the 'j2objc' plugin,
// but we also need to configure this object via methods on J2objcConfig.
nativeCompilation = new NativeCompilation(project)
// Provide defaults for assembly output locations.
destLibDir = new File(project.buildDir, 'j2objcOutputs/lib').absolutePath
// Can't be in subdirectory as podspec paths must be relative and not traverse parent ('..')
destPodspecDir = new File(project.buildDir, 'j2objcOutputs').absolutePath
destSrcMainDir = new File(project.buildDir, 'j2objcOutputs/src/main').absolutePath
destSrcTestDir = new File(project.buildDir, 'j2objcOutputs/src/test').absolutePath
}
/**
* Exact required version of j2objc.
*/
String j2objcVersion = '0.9.8.2.1'
/**
* Don't verify J2ObjC binaries. Useful for testing and power-users
* who know what they are doing and may wish to use a custom-build J2ObjC distribution.
*/
boolean skipJ2objcVerification = false;
/**
* Where to assemble generated main libraries.
* <p/>
* Defaults to $buildDir/j2objcOutputs
*/
String destPodspecDir = null
/**
* Where to assemble generated main libraries.
* <p/>
* Defaults to $buildDir/j2objcOutputs/lib
*/
String destLibDir = null
/**
* Where to assemble generated main source and resources files.
* <p/>
* Defaults to $buildDir/j2objcOutputs/src/main/objc
*/
String destSrcMainDir = null
/**
* Where to assemble generated test source and resources files.
* <p/>
* Can be the same directory as destDir
* Defaults to $buildDir/j2objcOutputs/src/test/objc
*/
String destSrcTestDir = null
// Private helper methods
// Should use instead of accessing client set 'dest' strings
File getDestLibDirFile() {
return project.file(destLibDir)
}
File getDestSrcDirFile(String sourceSetName, String fileType) {
assert sourceSetName in ['main', 'test']
assert fileType in ['objc', 'resources']
File destSrcDir = null
if (sourceSetName == 'main') {
destSrcDir = project.file(destSrcMainDir)
} else if (sourceSetName == 'test') {
destSrcDir = project.file(destSrcTestDir)
} else {
assert false, "Unsupported sourceSetName: $sourceSetName"
}
return project.file(new File(destSrcDir, fileType))
}
File getDestPodspecDirFile() {
return project.file(destPodspecDir)
}
/**
* Generated source files directories, e.g. from dagger annotations.
*/
// Default location for generated source files using annotation processor compilation,
// per sourceSets.main.output.classesDir.
// However, we cannot actually access sourceSets.main.output.classesDir here, because
// the Java plugin convention may not be applied at this time.
// TODO: Add a test counterpart for this.
List<String> generatedSourceDirs = ['build/classes/main']
/**
* Add generated source files directories, e.g. from dagger annotations.
*
* @param generatedSourceDirs adds generated source directories for j2objc translate
*/
void generatedSourceDirs(String... generatedSourceDirs) {
appendArgs(this.generatedSourceDirs, 'generatedSourceDirs', true, generatedSourceDirs)
}
// CYCLEFINDER
/**
* Command line arguments for j2objc cycle_finder.
* <p/>
* A list of all possible arguments can be found here:
* http://j2objc.org/docs/cycle_finder.html
*/
List<String> cycleFinderArgs = new ArrayList<>()
/**
* Add command line arguments for j2objc cycle_finder.
* <p/>
* A list of all possible arguments can be found here:
* http://j2objc.org/docs/cycle_finder.html
*
* @param cycleFinderArgs add args for 'cycle_finder' tool
*/
void cycleFinderArgs(String... cycleFinderArgs) {
appendArgs(this.cycleFinderArgs, 'cycleFinderArgs', true, cycleFinderArgs)
}
/**
* Expected number of cycles, defaults to all those found in JRE.
* <p/>
* This is an exact number rather than minimum as any change is significant.
*/
// TODO(bruno): convert to a default whitelist and change expected cyles to 0
int cycleFinderExpectedCycles = 40
// TRANSLATE
/**
* Command line arguments for j2objc translate.
* <p/>
* A list of all possible arguments can be found here:
* http://j2objc.org/docs/j2objc.html
*/
List<String> translateArgs = new ArrayList<>()
/**
* Add command line arguments for j2objc translate.
* <p/>
* A list of all possible arguments can be found here:
* http://j2objc.org/docs/j2objc.html
*
* @param translateArgs add args for the 'j2objc' tool
*/
void translateArgs(String... translateArgs) {
appendArgs(this.translateArgs, 'translateArgs', true, translateArgs)
}
/**
* Enables --build-closure, which translates classes referenced from the
* list of files passed for translation, using the
* {@link #translateSourcepaths}.
*/
void enableBuildClosure() {
if (!translateArgs.contains('--build-closure')) {
translateArgs('--build-closure')
}
}
/**
* Local jars for translation e.g.: "lib/json-20140107.jar", "lib/somelib.jar".
* This will be added to j2objc as a '-classpath' argument.
*/
List<String> translateClasspaths = new ArrayList<>()
/**
* Local jars for translation e.g.: "lib/json-20140107.jar", "lib/somelib.jar".
* This will be added to j2objc as a '-classpath' argument.
*
* @param translateClasspaths add libraries for -classpath argument
*/
void translateClasspaths(String... translateClasspaths) {
appendArgs(this.translateClasspaths, 'translateClasspaths', true, translateClasspaths)
}
/**
* Source jars for translation e.g.: "lib/json-20140107-sources.jar"
*/
List<String> translateSourcepaths = new ArrayList<>()
/**
* Source jars for translation e.g.: "lib/json-20140107-sources.jar"
*
* @param translateSourcepaths args add source jar for translation
*/
void translateSourcepaths(String... translateSourcepaths) {
appendArgs(this.translateSourcepaths, 'translateSourcepaths', true, translateSourcepaths)
}
/**
* True iff only translation (and cycle finding, if applicable) should be attempted,
* skipping all compilation, linking, and testing tasks.
*/
boolean translateOnlyMode = false
// Do not use groovydoc, this option should remain undocumented.
// WARNING: Do not use this unless you know what you are doing.
// If true, incremental builds will be supported even if --build-closure is included in
// translateArgs. This may break the build in unexpected ways if you change the dependencies
// (e.g. adding new files or changing translateClasspaths). When you change the dependencies and
// the build breaks, you need to do a clean build.
boolean UNSAFE_incrementalBuildClosure = false
/**
* Experimental functionality to automatically configure dependencies.
* Consider you have dependencies like:
* <pre>
* dependencies {
* compile project(':peer1') // type (1)
* compile 'com.google.code.gson:gson:2.3.1' // type (3)
* compile 'com.google.guava:guava:18.0' // type (2)
* testCompile 'junit:junit:4.11' // type (2)
* }
* </pre>
* Project dependencies (1) will be added as a `j2objcLink` dependency.
* Libraries already included in j2objc (2) will be ignored.
* External libraries in Maven (3) will be added in source JAR form to
* `j2objcTranslate`, and translated using `--build-closure`.
* Dependencies must be fully specified before you call finalConfigure().
* <p/>
* This will become the default when stable in future releases.
*/
boolean autoConfigureDeps = false
/**
* Additional Java libraries that are part of the j2objc distribution.
* <p/>
* For example:
* <pre>
* translateJ2objcLibs = ["j2objc_junit.jar", "jre_emul.jar"]
* </pre>
*/
// J2objc default libraries, from $J2OBJC_HOME/lib/...
// TODO: auto add libraries based on java dependencies, warn on version differences
List<String> translateJ2objcLibs = [
// Comments indicate difference compared to standard libraries...
// Memory annotations, e.g. @Weak, @AutoreleasePool
"j2objc_annotations.jar",
// Libraries that have CycleFinder fixes, e.g. @Weak and code removal
"j2objc_guava.jar", "j2objc_junit.jar", "jre_emul.jar",
// Libraries that don't need CycleFinder fixes
"javax.inject-1.jar", "jsr305-3.0.0.jar",
"mockito-core-1.9.5.jar", "hamcrest-core-1.3.jar", "protobuf_runtime.jar"]
/**
* Additional native libraries that are part of the j2objc distribution to link
* with the production code (and also the test code).
* <p/>
* For example:
* <pre>
* linkJ2objcLibs = ["guava", "jsr305"]
* </pre>
*/
// J2objc default libraries, from $J2OBJC_HOME/lib/..., without '.a' extension.
// TODO: auto add libraries based on java dependencies, warn on version differences
List<String> linkJ2objcLibs = ['guava', 'javax_inject', 'jsr305', 'protobuf_runtime']
/**
* Additional native libraries that are part of the j2objc distribution to link
* with the test code.
*/
// J2objc default libraries, from $J2OBJC_HOME/lib/..., without '.a' extension.
// TODO: auto add libraries based on java dependencies, warn on version differences
// Note: Hamcrest appears to be included within libjunit.a.
List<String> linkJ2objcTestLibs = ['junit', 'mockito', 'j2objc_main']
// TODO: warn if different versions than testCompile from Java plugin
/**
* Force filename collision check so prohibit two files with same name.
* <p/>
* This will automatically be set to true when translateArgs contains
* '--no-package-directories'. That flag flattens the directory structure
* and will overwrite files with the same name.
*/
boolean forceFilenameCollisionCheck = false
// All access to filenameCollisionCheck should be done through this function
boolean getFilenameCollisionCheck() {
if (translateArgs.contains('--no-package-directories'))
return true
return forceFilenameCollisionCheck
}
/**
* Sets the filter on files to translate.
* <p/>
* If no pattern is specified, all files within the sourceSets are translated.
* <p/>
* This filter is applied on top of all files within the 'main' and 'test'
* java sourceSets. Use {@link #translatePattern(groovy.lang.Closure)} to
* configure.
*/
PatternSet translatePattern = null
/**
* Configures the {@link #translatePattern}.
* <p/>
* Calling this method repeatedly further modifies the existing translatePattern,
* and will create an empty translatePattern if none exists.
* <p/>
* For example:
* <pre>
* translatePattern {
* exclude 'CannotTranslateFile.java'
* exclude '**/CannotTranslateDir/*.java'
* include '**/CannotTranslateDir/AnExceptionToInclude.java'
* }
* </pre>
* @see
* <a href="https://docs.gradle.org/current/userguide/working_with_files.html#sec:file_trees">Gradle User Guide</a>
*/
PatternSet translatePattern(@DelegatesTo(strategy = Closure.DELEGATE_FIRST, value = PatternSet) Closure cl) {
if (translatePattern == null) {
translatePattern = new PatternSet()
}
return ConfigureUtil.configure(cl, translatePattern)
}
/**
* A mapping from source file names (in the project Java sourcesets) to alternate
* source files.
* Both before and after names (keys and values) are evaluated using project.file(...).
* <p/>
* Mappings can be used to have completely different implementations in your Java
* jar vs. your Objective-C library. This can be especially useful when compiling
* a third-party library and you need to provide non-trivial OCNI implementations
* in Objective-C.
*/
Map<String, String> translateSourceMapping = [:]
/**
* Adds a new source mapping.
* @see #translateSourceMapping
*/
void translateSourceMapping(String before, String after) {
translateSourceMapping.put(before, after)
}
/**
* @see #dependsOnJ2objcLib(org.gradle.api.Project)
* @deprecated Use `dependencies { j2objcLinkage project(':beforeProjectName') }` or
* `autoConfigureDeps = true` instead.
*/
// TODO: Do this automatically based on project dependencies.
@Deprecated
void dependsOnJ2objcLib(String beforeProjectName) {
//noinspection GrDeprecatedAPIUsage
dependsOnJ2objcLib(project.project(beforeProjectName))
}
protected NativeCompilation nativeCompilation
/**
* Get J2ObjC project dependencies.
*
* Must not be modified by caller.
*/
// TODO: ideally use immutable wrapper, not enough to justify Guava dependency
List<Project> getBeforeProjects() {
return nativeCompilation.beforeProjects
}
/**
* Uses the generated headers and compiled j2objc libraries of the given project when
* compiling this project.
* <p/>
* Generally every cross-project 'compile' dependency should have a corresponding
* call to dependsOnJ2objcLib.
* <p/>
* It is safe to use this in conjunction with --build-closure.
* <p/>
* Do not also include beforeProject's java source or jar in the
* translateSourcepaths or translateClasspaths, respectively. Calling this method
* is preferable and sufficient.
*
* @deprecated Use `dependencies { j2objcLinkage project(':beforeProjectName') }` or
* `autoConfigureDeps=true` instead.
*/
// TODO: Phase this API out, and have J2ObjC-applied project dependencies controlled
// solely via `j2objcLinkage` configuration.
@CompileStatic(TypeCheckingMode.SKIP)
@Deprecated
void dependsOnJ2objcLib(Project beforeProject) {
project.dependencies {
j2objcLinkage beforeProject
}
}
/**
* Which architectures will be built and supported in packed ('fat') libraries.
* <p/>
* The three ios_arm* architectures are for iPhone and iPad devices, while
* ios_i386 and ios_x86_64 are for their simulators.
* <p/>
* By default, only common modern iOS architectures will be built:
* ios_arm64, ios_armv7, ios_x86_64. You may choose to add any of the remaining
* entries from NativeCompilation.ALL_IOS_ARCHS (ios_i386 and ios_armv7s)
* to support all possible iOS architectures. Listing any new architectures outside of
* ALL_IOS_ARCHS will fail the build.
* <p/>
* Removing an architecture here will cause that architecture not to be built
* and corresponding gradle tasks to not be created.
* <pre>
* supportedArchs = ['ios_arm64'] // Only build libraries for 64-bit iOS devices
* </pre>
* The options are:
* <ul>
* <li>'ios_arm64' => iPhone 5S, 6, 6 Plus
* <li>'ios_armv7' => iPhone 4, 4S, 5
* <li>'ios_i386' => iOS Simulator on 32-bit OS X
* <li>'ios_x86_64' => iOS Simulator on 64-bit OS X
* </ul>
* @see NativeCompilation#ALL_IOS_ARCHS
*/
// Public to allow assignment of array of targets as shown in example
List<String> supportedArchs = ['ios_arm64', 'ios_armv7', 'ios_x86_64']
/**
* An architecture is active if it is both supported ({@link #supportedArchs})
* and enabled in the current environment via the comma-separated j2objc.enabledArchs
* value in local.properties.
* <p/>
* If no j2objc.enabledArchs value is specified in local.properties, all supported
* architectures are also active, otherwise the intersection of supportedArchs
* and j2objc.enabledArchs is used.
*/
List<String> getActiveArchs() {
// null is the default value, since an explicit empty string means no architectures
// are enabled.
String archsCsv = Utils.getLocalProperty(project, 'enabledArchs', null)
if (archsCsv == null) {
return supportedArchs
}
List<String> enabledArchs = archsCsv.split(',').toList()
// Given `j2objc.enabledArchs=` we will have one architecture of empty string,
// instead we want no architectures at all in this case.
enabledArchs.remove('')
List<String> invalidArchs = enabledArchs.minus(
NativeCompilation.ALL_IOS_ARCHS.clone() as List<String>).toList()
if (!invalidArchs.isEmpty()) {
throw new InvalidUserDataException("Invalid 'enabledArchs' entry: " + invalidArchs.join(', '))
}
// Keep the return value sorted to prevent changes in intersection ordering
// from forcing a rebuild.
return supportedArchs.intersect(enabledArchs).toList().sort()
}
// TEST
/**
* Command line arguments for j2objcTest task.
*/
List<String> testArgs = new ArrayList<>()
/**
* Add command line arguments for j2objcTest task.
*
* @param testArgs add args for the 'j2objcTest' task
*/
void testArgs(String... testArgs) {
appendArgs(this.testArgs, 'testArgs', true, testArgs)
}
/**
* j2objcTest will fail if it runs less than the expected number of tests; set to 0 to disable.
* <p/>
* It is a minimum so adding a unit test doesn't break the j2objc build.
*/
int testMinExpectedTests = 1
/**
* Filter on files to test. Note this has no effect on which tests are
* translated, just which tests are executed by the j2objcTest task.
* <p/>
* If no pattern is specified, all files within the 'test' sourceSet are translated.
* <p/>
* This filter is applied on top of all files within the 'main' and 'test'
* java sourceSets. Use {@link #testPattern(groovy.lang.Closure)} to
* configure.
*/
PatternSet testPattern = null
/**
* Configures the {@link #testPattern}
* <p/>
* Calling this method repeatedly further modifies the existing testPattern,
* and will create an empty testPattern if none exists.
* <p/>
* For example:
* <pre>
* translatePattern {
* exclude 'CannotTranslateFileTest.java'
* exclude '**/CannotTranslateDir/*.java'
* include '**/CannotTranslateDir/AnExceptionToIncludeTest.java'
* }
* </pre>
* @see
* <a href="https://docs.gradle.org/current/userguide/working_with_files.html#sec:file_trees">Gradle User Guide</a>
*/
PatternSet testPattern(@DelegatesTo(strategy = Closure.DELEGATE_FIRST, value = PatternSet) Closure cl) {
if (testPattern == null) {
testPattern = new PatternSet()
}
return ConfigureUtil.configure(cl, testPattern)
}
// Native build customization.
/**
* Directories of Objective-C source to compile in addition to the
* translated source.
*/
// Native build accepts empty array but throws exception on empty List<String>
// "srcDirs j2objcConfig.extraObjcSrcDirs" line in NativeCompilation
String[] extraObjcSrcDirs = []
/**
* Add directories of Objective-C source to compile in addition to the
* translated source.
*
* @param extraObjcSrcDirs add directories for Objective-C source to be compiled
*/
void extraObjcSrcDirs(String... extraObjcSrcDirs) {
verifyArgs('extraObjcSrcDirs', true, extraObjcSrcDirs)
for (String arg in extraObjcSrcDirs) {
this.extraObjcSrcDirs += arg
}
}
/**
* Additional arguments to pass to the native compiler.
*/
// Native build accepts empty array but throws exception on empty List<String>
String[] extraObjcCompilerArgs = []
/**
* Add arguments to pass to the native compiler.
*
* @param extraObjcCompilerArgs add arguments to pass to the native compiler.
*/
void extraObjcCompilerArgs(String... extraObjcCompilerArgs) {
verifyArgs('extraObjcCompilerArgs', true, extraObjcCompilerArgs)
for (String arg in extraObjcCompilerArgs) {
this.extraObjcCompilerArgs += arg
}
}
/**
* Additional arguments to pass to the native linker.
*/
// Native build accepts empty array but throws exception on empty List<String>
String[] extraLinkerArgs = []
/**
* Add arguments to pass to the native linker.
*
* @param extraLinkerArgs add arguments to pass to the native linker.
*/
void extraLinkerArgs(String... extraLinkerArgs) {
verifyArgs('extraLinkerArgs', true, extraLinkerArgs)
for (String arg in extraLinkerArgs) {
this.extraLinkerArgs += arg
}
}
/**
* Additional native libraries to link to.
*/
List<Map> extraNativeLibs = []
/**
* Add additional native library to link to.
* <p/>
* For example, if you built native library 'utils' in project 'common':
* <pre>
* extraNativeLib project: 'common', library: 'utils', linkage: 'static'
* </pre>
*/
void extraNativeLib(Map spec) {
extraNativeLibs.add(spec)
}
/**
* The minimum iOS version to build against. You cannot use APIs that are not supported
* in this version.
* <p/>
* See https://developer.apple.com/library/ios/documentation/DeveloperTools/Conceptual/cross_development/Configuring/configuring.html#//apple_ref/doc/uid/10000163i-CH1-SW2
*/
// Chosen to broaden compatibility for initial use
// Maintain at one version behind current
String minVersionIos = '8.3'
/**
* The minimum OS X version to build against. You cannot use APIs that are not supported
* in this version.
* <p/>
* See https://developer.apple.com/library/ios/documentation/DeveloperTools/Conceptual/cross_development/Configuring/configuring.html#//apple_ref/doc/uid/10000163i-CH1-SW2
*/
// Oldest OS X version that supports automatic reference counting (2009 onwards)
// Prevents Xcode error: "-fobjc-arc is not supported on versions of OS X prior to 10.6"
String minVersionOsx = '10.6'
/**
* The minimum Watch OS version to build against. You cannot use APIs that are not supported
* in this version.
* <p/>
* See https://developer.apple.com/library/ios/documentation/DeveloperTools/Conceptual/cross_development/Configuring/configuring.html#//apple_ref/doc/uid/10000163i-CH1-SW2
*/
// Chosen to broaden compatibility for initial use
// Maintain at one version behind current
String minVersionWatchos = '2.0'
// XCODE
/**
* Directory of the target Xcode project.
*
* Suggested location is '../ios'
* See J2ObjC Plugin <a href="https://github.com/j2objc-contrib/j2objc-gradle/blob/master/FAQ.md#what-is-the-recommended-folder-structure-for-my-app">Folder Structure</a>
*
*/
String xcodeProjectDir = null
/**
* iOS app and test Xcode targets to link to the generated libraries.
*
* This will automatically add linkage for any target in the specified list
* to the generated shared libraries. This should include test targets also.
*/
List<String> xcodeTargetsIos = new ArrayList<>()
/**
* iOS app and test Xcode targets to link to the generated libraries.
*
* @param xcodeTargetsIos targets to link to the generated libraries.
*/
void xcodeTargetsIos(String... xcodeTargetsIos) {
appendArgs(this.xcodeTargetsIos, 'xcodeTargetsIos', false, xcodeTargetsIos)
}
/**
* OS X app and test Xcode targets that should be linked to the generated libraries.
*
* This will automatically add linkage for any target in the specified list
* to the generated shared libraries. This should include test targets also.
*/
List<String> xcodeTargetsOsx = new ArrayList<>()
/**
* OS X app and test Xcode targets to link to the generated libraries.
*
* @param xcodeTargetsOsx targets to link to the generated libraries.
*/
void xcodeTargetsOsx(String... xcodeTargetsOsx) {
appendArgs(this.xcodeTargetsOsx, 'xcodeTargetsOsx', false, xcodeTargetsOsx)
}
/**
* watchOS app and test Xcode targets that should be linked to the generated libraries.
*
* This will automatically add linkage for any target in the specified list
* to the generated shared libraries. This should include test targets also.
*/
List<String> xcodeTargetsWatchos = new ArrayList<>()
/**
* watchOS app and test Xcode targets to link to the generated libraries.
*
* @param xcodeTargetsWatchos targets to link to the generated libraries.
*/
void xcodeTargetsWatchos(String... xcodeTargetsWatchos) {
appendArgs(this.xcodeTargetsWatchos, 'xcodeTargetsWatchos', false, xcodeTargetsWatchos)
}
/**
* Allows manual config of Xcode targets in the Podfile (default is false).
*
* When set to true, this allows manual configuring of the Podfile targets.
* This is necessary when your Podfile is too complex to be automatically
* updated. It will still add the "Pod Method" (e.g. j2objc_shared) but it
* will not update the targets within the Podfile. When used, you must also
* set xcodeTargets{Ios|Osx|Watchos) to empty.
*/
boolean xcodeTargetsManualConfig = false
/**
* The Xcode build configurations which should link to the generated debug libraries.
* If set to an empty array, the Debug configuration will be omitted from the "pod method".
* <p/>
* For example:
* <pre>
* j2objcConfig {
* xcodeDebugConfigurations += ['Beta']
* ...
* }
* </pre>
*/
List<String> xcodeDebugConfigurations = ['Debug']
/**
* The Xcode build configurations which should link to the generated release libraries.
* If set to an empty array, the Release configuration will be omitted from the "pod method".
* <p/>
* For example:
* <pre>
* j2objcConfig {
* xcodeReleaseConfigurations += ['Preview']
* ...
* }
* </pre>
*/
List<String> xcodeReleaseConfigurations = ['Release']
protected boolean finalConfigured = false
/**
* Configures the j2objc build. Must be called at the very
* end of your j2objcConfig block.
*/
@VisibleForTesting
void finalConfigure() {
// Gradle 2.9 build will fail if it calls configureNativeCompilation:
// https://github.com/j2objc-contrib/j2objc-gradle/issues/568
// Return early without error to avoid deadlock:
// https://github.com/j2objc-contrib/j2objc-gradle/issues/585
// Exception is thrown when TranslateTask is run. Safest approach is to disable
// all setup logic even though only NativeCompilation appears to cause any issue.
if (Utils.checkGradleVersion(false)) {
configureNativeCompilationForUnsupported()
// Avoid misleading error message that finalConfigured() wasn't in build.gradle
finalConfigured = true
return
}
validateConfiguration()
// Conversion of compile and testCompile dependencies occurs optionally.
if (autoConfigureDeps) {
convertDeps()
}
// Resolution of j2objcTranslateSource dependencies occurs always.
// This lets users turn off autoConfigureDeps but manually set j2objcTranslateSource.
resolveDeps()
configureNativeCompilation()
configureTaskState()
finalConfigured = true
}
public static final String MIN_SUPPORTED_J2OBJC_VERSION = '0.9.8.2.1'
protected void verifyJ2objcRequirements() {
if (skipJ2objcVerification) {
return
}
if (!Utils.isAtLeastVersion(j2objcVersion, MIN_SUPPORTED_J2OBJC_VERSION)) {
String requestedVersion = j2objcVersion
// j2objcVersion is used for instructing the user how to install j2objc
// so we should use the version we need, not the bad one the user requested.
j2objcVersion = MIN_SUPPORTED_J2OBJC_VERSION
Utils.throwJ2objcConfigFailure(project,
"Must use at least J2ObjC version $MIN_SUPPORTED_J2OBJC_VERSION; you requested $requestedVersion.")
}
// Make sure we have *some* J2ObjC distribution identified.
// This will throw a proper out-of-box error if misconfigured.
String j2objcHome = Utils.j2objcHome(project)
// Verify that underlying J2ObjC binary exists at all.
File j2objcJar = Utils.j2objcJar(project)
if (!j2objcJar.exists()) {
Utils.throwJ2objcConfigFailure(project, "J2ObjC binary does not exist at ${j2objcJar.absolutePath}.")
}
// Now check the version of the binary against the version required.
String j2objcExecutable = "$j2objcHome/j2objc"
List<String> windowsOnlyArgs = new ArrayList<String>()
if (Utils.isWindows()) {
j2objcExecutable = 'java'
windowsOnlyArgs.add('-jar')
windowsOnlyArgs.add(j2objcJar.absolutePath)
}
ByteArrayOutputStream stdout = new ByteArrayOutputStream()
ByteArrayOutputStream stderr = new ByteArrayOutputStream()
project.logger.debug('VerifyJ2objcRequirements - projectExec:')
try {
Utils.projectExec(project, stdout, stderr, null, {
executable j2objcExecutable
windowsOnlyArgs.each { String windowsOnlyArg ->
args windowsOnlyArg
}
// Arguments
args "-version"
setStandardOutput stdout
setErrorOutput stderr
})
} catch (Exception exception) { // NOSONAR
// Likely too old to understand -version,
// but include the error since it could be something else.
Utils.throwJ2objcConfigFailure(project, exception.toString() + "\n\n" +
"J2ObjC binary at $j2objcHome too old, v$j2objcVersion required.")
}
// Yes, J2ObjC uses stderr to output the version.
String actualVersionString = stderr.toString().trim()
if (actualVersionString != "j2objc $j2objcVersion".toString()) {
// Note that actualVersionString will usually already have the word 'j2objc' in it.
Utils.throwJ2objcConfigFailure(project,
"Found $actualVersionString at $j2objcHome, J2ObjC v$j2objcVersion required.")
}
}
protected void validateConfiguration() {
// Validate minimally required parameters.
verifyJ2objcRequirements()
assert destLibDir != null
assert destPodspecDir != null
assert destSrcMainDir != null
assert destSrcTestDir != null
// TODO: watchOS build support
if (xcodeTargetsWatchos.size() > 0) {
project.logger.warn(
"watchOS isn't yet supported, please unset xcodeTargetsWatchos for now.\n" +
"Follow this issue for updates: https://github.com/j2objc-contrib/j2objc-gradle/issues/525")
}
}
protected void configureNativeCompilation() {
// TODO: When Gradle makes it possible to modify a native build config
// after initial creation, we can remove this, and have methods on this object
// mutate the existing native model { } block. See:
// https://discuss.gradle.org/t/problem-with-model-block-when-switching-from-2-2-1-to-2-4/9937
nativeCompilation.apply(project.file("${project.buildDir}/j2objcSrcGenMain"),
project.file("${project.buildDir}/j2objcSrcGenTest"))
}
protected void configureNativeCompilationForUnsupported() {
nativeCompilation.applyWhenUnsupported()
}
protected void convertDeps() {
new DependencyConverter(project, this).configureAll()
}
protected void resolveDeps() {
new DependencyResolver(project, this).configureAll()
}
protected void configureTaskState() {
// Disable only if explicitly present and not true.
boolean debugEnabled = Boolean.parseBoolean(Utils.getLocalProperty(project, 'debug.enabled', 'true'))
boolean releaseEnabled = Boolean.parseBoolean(Utils.getLocalProperty(project, 'release.enabled', 'true'))
// Enable only if explicitly present in either the project config OR the local config.
boolean translateOnlyMode = this.translateOnlyMode ||
Boolean.parseBoolean(Utils.getLocalProperty(project, 'translateOnlyMode', 'false'))
if (!translateOnlyMode) {
Utils.requireMacOSX('Native Compilation of translated code task')
}
project.logger.info("J2objcPlugin: translateOnlyMode will disable most j2objc tasks")
project.tasks.all { Task task ->
String name = task.name
// For convenience, disable all debug and/or release tasks if the user desires.
// Note all J2objcPlugin-created tasks are of the form `j2objc.*(Debug|Release)?`
// however Native plugin-created tasks (on our behalf) are of the form `.*((D|d)ebug|(R|r)elease).*(j|J)2objc.*'
// so these patterns find all such tasks.
if (name.contains('j2objc') || name.contains('J2objc')) {
if (!debugEnabled && (name.contains('debug') || name.contains('Debug'))) {
task.enabled = false
}
if (!releaseEnabled && (name.contains('release') || name.contains('Release'))) {
task.enabled = false
}
}
// Support translation-only mode.
if (translateOnlyMode) {
// First pattern matches all native-compilation tasks.
// Second pattern matches plugin-specific tasks beyond translation.
if ((name =~ /^.*((J|j)2objc(Executable|StaticLibrary|SharedLibrary|Objc))$/).matches() ||
(name =~ /^j2objc(Assemble|PackLibraries|Test)(Debug|Release)$/).matches() ||
(name =~ /^j2objc(Podspec|Xcode)$/).matches()) {
task.enabled = false
}
}
}
}
boolean isFinalConfigured() {
return finalConfigured
}
// Provides a subset of "args" interface from project.exec as implemented by ExecHandleBuilder:
// https://github.com/gradle/gradle/blob/master/subprojects/core/src/main/groovy/org/gradle/process/internal/ExecHandleBuilder.java
// Allows the following:
// j2objcConfig {
// translateArgs '--no-package-directories', '--prefixes', 'prefixes.properties'
// }
@VisibleForTesting
static void appendArgs(List<String> listArgs, String nameArgs, boolean rejectSpaces, String... args) {
verifyArgs(nameArgs, rejectSpaces, args)
listArgs.addAll(Arrays.asList(args))
}
// Verify that no argument contains a space
@VisibleForTesting
static void verifyArgs(String nameArgs, boolean rejectSpaces, String... args) {
if (args == null) {
throw new InvalidUserDataException("$nameArgs == null!")
}
for (String arg in args) {
if (arg.isAllWhitespace()) {
throw new InvalidUserDataException(
"$nameArgs is all whitespace: '$arg'")
}
if (rejectSpaces) {
if (arg.contains(' ')) {
String rewrittenArgs = "'" + arg.split(' ').join("', '") + "'"
throw new InvalidUserDataException(
"'$arg' argument should not contain spaces and be written out as distinct entries:\n" +
"$nameArgs $rewrittenArgs")
}
}
}
}
@VisibleForTesting
void testingOnlyPrepConfigurations() {
// When testing we don't always want to apply the entire plugin
// before calling finalConfigure.
project.configurations.create('j2objcTranslationClosure')
project.configurations.create('j2objcTranslation')
project.configurations.create('j2objcTestTranslation')
project.configurations.create('j2objcLinkage')
project.configurations.create('j2objcTestLinkage')
}
}