-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAndroidMakefileScanner.rb
executable file
·1315 lines (1141 loc) · 39.2 KB
/
AndroidMakefileScanner.rb
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
#!/usr/bin/env ruby
# Copyright 2022 hidenory
#
# 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.
require 'fileutils'
require 'optparse'
require 'shellwords'
require 'json'
require 'rexml/document'
require_relative 'FileUtil'
require_relative 'StrUtil'
require_relative 'TaskManager'
require_relative 'Reporter'
class ArrayUtil
def self.includes?(theArray, vals)
result = false
vals.to_a.each do | aVal |
result = theArray.include?( aVal )
break if result
end
return result
end
end
class RepoUtil
DEF_MANIFESTFILE = "manifest.xml"
DEF_MANIFESTFILE_DIRS = [
"/.repo/",
"/.repo/manifests/"
]
def self.getAvailableManifestPath(basePath, manifestFilename)
DEF_MANIFESTFILE_DIRS.each do |aDir|
path = basePath + aDir.to_s + manifestFilename
if FileTest.exist?(path) then
return path
end
end
return nil
end
def self.getPathesFromManifestSub(basePath, manifestFilename, pathes, pathFilter, groupFilter)
manifestPath = getAvailableManifestPath(basePath, manifestFilename)
if manifestPath && FileTest.exist?(manifestPath) then
doc = REXML::Document.new(open(manifestPath))
doc.elements.each("manifest/include[@name]") do |anElement|
getPathesFromManifestSub(basePath, anElement.attributes["name"], pathes, pathFilter, groupFilter)
end
doc.elements.each("manifest/project[@path]") do |anElement|
theGitPath = anElement.attributes["path"].to_s
if pathFilter.empty? || ( !pathFilter.to_s.empty? && theGitPath.match( pathFilter.to_s ) ) then
theGroups = anElement.attributes["groups"].to_s
if theGroups.empty? || groupFilter.empty? || ( !groupFilter.to_s.empty? && theGroups.match( groupFilter.to_s ) ) then
pathes << "#{basePath}/#{theGitPath}"
end
end
end
end
end
def self.getPathesFromManifest(basePath, pathFilter="", groupFilter="")
pathes = []
getPathesFromManifestSub(basePath, DEF_MANIFESTFILE, pathes, pathFilter, groupFilter)
return pathes
end
end
class AndroidUtil
DEF_ANDROID_MAKEFILES = [
"Android.mk",
"Android.bp",
]
def self.getListOfAndroidMakefile(imagePath)
gitPaths = RepoUtil.getPathesFromManifest(imagePath)
gitPaths = [imagePath] if gitPaths.empty?
result = FileUtil.getRegExpFilteredFilesMT(gitPaths, "Android\.(bp|mk)")
return result
end
DEF_INTERMEDIATE_BUILTOUTS=[
"/obj/PACKAGING/target_files_intermediates/",
"/obj/SHARED_LIBRARIES/",
"/obj/JAVA_LIBRARIES/",
"/obj/APPS/",
"/obj/ETC/",
"/symbols/"
]
def self.excludesKnownIntermediatesBuiltOuts(builtOuts)
results = []
builtOuts.each do | aBuiltOut |
isExclude = ArrayUtil.includes?(aBuiltOut, DEF_INTERMEDIATE_BUILTOUTS)
results << aBuiltOut if !isExclude
end
return results
end
def self.getListOfBuiltOuts(builtOutPath, isNativeLib = true, isApk = true, isJar = true, isApex = true)
searchTarget = []
searchTarget << "so|a" if isNativeLib
searchTarget << "apk" if isApk
searchTarget << "jar" if isJar
searchTarget << "apex" if isApex
searchTarget = searchTarget.join("|")
searchTarget = searchTarget.slice(0, searchTarget.length-1) if searchTarget.end_with?("|")
return searchTarget ? excludesKnownIntermediatesBuiltOuts( FileUtil.getRegExpFilteredFilesMT2(builtOutPath, "\.(#{searchTarget})$") ) : []
end
DEF_BUILTS_OUT_EXTS=[
".so",
".apk",
".jar",
".apex"
]
def self.getFilenameFromPathWithoutExt( path )
path = path.to_s
path = FileUtil.getFilenameFromPath(path)
DEF_BUILTS_OUT_EXTS.each do |anExt|
pos = path.to_s.rindex(anExt)
if pos then
path = path.slice(0, pos)
break
end
end
return path
end
DEF_ANDROID_ROOT=[
"/system/",
"/frameworks/",
"/device/",
"/vendor/",
"/packages/",
"/external/",
"/hardware/",
]
def self.getAndroidRootPath(path)
result = ""
DEF_ANDROID_ROOT.each do |aPath|
pos = path.index(aPath)
if pos then
result = path.slice(0, pos)
break
end
end
return result
end
end
class AndroidMakefileParser
class ParseResult
attr_accessor :builtOuts
attr_accessor :libName
attr_accessor :nativeIncludes
attr_accessor :cflags
attr_accessor :apkName
attr_accessor :jarName
attr_accessor :apexName
attr_accessor :certificate
attr_accessor :dexPreOpt
attr_accessor :optimizeEnabled
attr_accessor :optimizeShrink
def initialize
@builtOuts = []
@libName = ""
@nativeIncludes = []
@cflags = []
@apkName = ""
@jarName = ""
@apexName = ""
@certificate = ""
@dexPreOpt = "true"
@optimizeEnabled = "true"
@optimizeShrink = "true"
end
end
def initialize(makefilePath, envFlatten, compilerFilter, enableNativeScan = true, enableApkScan = true, enableJarScan = true, enableApexScan = true)
@makefilePath = makefilePath
@makefileDirectory = FileUtil.getDirectoryFromPath(makefilePath)
@androidRootPath = AndroidUtil.getAndroidRootPath(makefilePath)
@envFlatten = envFlatten
@isNativeLib = false
@isApk = false
@isJar = false
@isApex = false
@enableNativeScan = enableNativeScan
@enableApkScan = enableApkScan
@enableJarScan = enableJarScan
@enableApexScan = enableApexScan
@currentResult = ParseResult.new()
@results = [@currentResult]
@compilerFilter = compilerFilter
end
def isNativeLib
return @isNativeLib
end
def isApk
return @isApk
end
def isJar
return @isJar
end
def isApex
return @isApex
end
def getResults(defaultVersion)
results = []
@results.each do |aResult|
result = {}
aResult.nativeIncludes.uniq!
aResult.builtOuts.uniq!
aResult.cflags.uniq!
if @isNativeLib && (!aResult.nativeIncludes.empty? || !aResult.cflags.empty?) then
result["libName"] = aResult.libName #? aResult.libName : AndroidUtil.getFilenameFromPathWithoutExt(aResult.builtOuts.to_a[0])
result["version"] = defaultVersion.to_s #TODO: get version and use it if it's not specified
result["headers"] = aResult.nativeIncludes
result["builtOuts"] = aResult.builtOuts
result["gcc_options"] = aResult.cflags
end
if @isApk && aResult.apkName then
result["apkName"] = aResult.apkName
result["builtOuts"] = aResult.builtOuts
result["certificate"] = aResult.certificate
result["dexPreOpt"] = aResult.dexPreOpt
result["optimizeEnabled"] = aResult.optimizeEnabled
result["optimizeShrink"] = aResult.optimizeShrink
end
if @isJar && aResult.jarName then
result["jarName"] = aResult.jarName
result["jarPath"] = aResult.builtOuts
result["builtOuts"] = aResult.builtOuts
result["certificate"] = aResult.certificate
result["dexPreOpt"] = aResult.dexPreOpt
end
if @isApex && aResult.apexName then
result["apexName"] = aResult.apexName
result["apexPath"] = aResult.builtOuts
result["builtOuts"] = aResult.builtOuts
result["certificate"] = aResult.certificate
end
results << result if !result.empty?
end
return results
end
def self.replacePathWithBuiltOuts( original, builtOuts, enableOnlyFoundBuiltOuts = false )
result = []
builtOutCache = {}
builtOuts.each do |aPath|
builtOutCache[ AndroidUtil.getFilenameFromPathWithoutExt( aPath ) ] = aPath if !enableOnlyFoundBuiltOuts || enableOnlyFoundBuiltOuts && File.exist?(aPath) && File.size(aPath).to_i>0
end
original.each do |aResult|
found = false
targets = []
targets = targets | aResult["builtOuts"] if aResult.has_key?("builtOuts")
targets << aResult["libName"] if aResult.has_key?("libName")
targets << aResult["apkName"] if aResult.has_key?("apkName")
targets << aResult["jarName"] if aResult.has_key?("jarName")
targets << aResult["apexName"] if aResult.has_key?("apexName")
if !targets.empty? then
replacedResults = []
targets.to_a.each do |anTarget|
key = AndroidUtil.getFilenameFromPathWithoutExt(anTarget)
if builtOutCache.has_key?( key ) then
found = true
replacedResults << builtOutCache[key]
else
replacedResults << anTarget if !enableOnlyFoundBuiltOuts
end
end
builtOuts = []
apkName = ""
jarName = ""
apexName = ""
replacedResults.each do |aReplacedResult|
aReplacedResult = aReplacedResult.to_s
builtOuts << aReplacedResult if aReplacedResult.end_with?(".so") || aReplacedResult.end_with?(".a") || aReplacedResult.end_with?(".apk") || aReplacedResult.end_with?(".apex") || aReplacedResult.end_with?(".jar")
apkName = aReplacedResult if aReplacedResult.end_with?(".apk")
jarName = aReplacedResult if aReplacedResult.end_with?(".jar")
apexName = aReplacedResult if aReplacedResult.end_with?(".apex")
end
aResult["builtOuts"] = builtOuts if !builtOuts.empty? || enableOnlyFoundBuiltOuts
aResult["apkName"] = apkName if apkName || enableOnlyFoundBuiltOuts
aResult["jarName"] = jarName if jarName || enableOnlyFoundBuiltOuts
aResult["apexName"] = apexName if apexName || enableOnlyFoundBuiltOuts
end
aResult["libName"] = AndroidUtil.getFilenameFromPathWithoutExt(aResult["builtOuts"].to_a[0]) if !aResult["builtOuts"].to_a.empty?
aResult["apkName"] = AndroidUtil.getFilenameFromPathWithoutExt(aResult["apkName"]) if aResult["apkName"]
aResult["jarName"] = AndroidUtil.getFilenameFromPathWithoutExt(aResult["jarName"]) if aResult["jarName"]
aResult["apexName"] = AndroidUtil.getFilenameFromPathWithoutExt(aResult["apexName"]) if aResult["apexName"]
result << aResult if !enableOnlyFoundBuiltOuts || found
end
return result
end
def dump
return ""
end
def getRobustPath(basePath, thePath)
result = "#{basePath}/#{thePath}"
if !File.exist?(result) then
foundPaths = ""
thePaths = thePath.split("/")
thePaths.each do |aPath|
if basePath.include?(aPath) then
foundPaths = "#{foundPaths}/#{aPath}"
else
break
end
end
foundPaths = foundPaths.slice(1, foundPaths.length) if foundPaths.start_with?("/")
if !foundPaths.empty? then
remainingPath = thePath.slice( thePath.index(foundPaths)+foundPaths.length, thePath.length )
thePath = basePath.slice( 0, thePath.index(foundPaths) ) + remainingPath
end
result = "#{basePath}/#{thePath}"
end
return result
end
DEF_NATIVE_HEADER_EXTENSION="\.(h|hpp)$"
DEF_NATIVE_SOURCE_EXTENSION="\.c??$"
def _isNativeHeader(path)
return true if path.end_with?(".h") || path.end_with?(".hpp") || path.end_with?(".hh") || path.end_with?(".h++")
return true if path.end_with?(".c") || path.end_with?(".cc") || path.end_with?(".cxx") || path.end_with?(".cpp")
return false
end
def ensureNativeIncludes
@results.each do |aResult|
if aResult.nativeIncludes.empty? then
incPaths = FileUtil.getRegExpFilteredFiles(@makefileDirectory, DEF_NATIVE_HEADER_EXTENSION)
incPaths = FileUtil.getRegExpFilteredFiles(@makefileDirectory, DEF_NATIVE_SOURCE_EXTENSION) if incPaths.empty?
incPaths.each do |anInc|
if _isNativeHeader(anInc) then
theDir = FileUtil.getDirectoryFromPath(anInc)
theDir = theDir.slice(0, theDir.length-1) if theDir.end_with?(".")
theDir = theDir.slice(0, theDir.length-1) if theDir.end_with?("/")
aResult.nativeIncludes << theDir if !aResult.nativeIncludes.include?(theDir)
end
end
end
result = []
aResult.nativeIncludes.each do |anInc|
anInc = anInc.gsub("//", "/")
anInc = anInc.slice(0, anInc.length-1) if anInc.end_with?(".")
anInc = anInc.slice(0, anInc.length-1) if anInc.end_with?("/")
anInc.strip!
result << anInc if !anInc.empty?
end
aResult.nativeIncludes = result
aResult.nativeIncludes.uniq!
end
end
def ensureCompilerOption
@results.each do |aResult|
aResult.cflags.uniq!
aResult.cflags = @compilerFilter.filterOption( aResult.cflags )
aResult.cflags.uniq!
end
end
end
class AndroidMkParser < AndroidMakefileParser
def getKeyValueFromLine(aLine)
key = nil
value = nil
pos = aLine.index("=")
if pos then
value = aLine.slice(pos+1, aLine.length).strip
key = aLine.slice(0, pos).strip
key = key.slice(0, key.length-1).strip if key.end_with?(":")
if key.end_with?("+") then
key = key.slice(0, key.length-1).strip
value = @env.has_key?(key) ? "#{@env[key]} \\ #{value}" : value
end
end
return key, value
end
DEF_SUBST_INNER="subst "
DEF_SUBST="$(#{DEF_SUBST_INNER}"
def _subst(value)
pos = value.index(DEF_SUBST)
if pos then
substWords = StrUtil.getBlacket( value, "(", ")", pos)
posEnd = value.index(substWords) + substWords.length + 2
if substWords.index(DEF_SUBST) then
substWords = _subst( substWords )
end
pos1 = substWords.index(DEF_SUBST_INNER)
if pos1 then
substArgs = substWords.slice(pos1+DEF_SUBST_INNER.length, substWords.length).strip.split(",")
if substArgs.length == 3 then
target = substArgs[0].strip
replaceKey = substArgs[1].strip
replaceVal = substArgs[1].strip
value = value.slice(0, pos).to_s + target.gsub( replaceKey, replaceVal ).to_s + value.slice(posEnd, value.length).to_s
end
end
end
return value
end
DEF_INC_PATH_MAP={
"camera" => "system/media/camera/include",
"frameworks-base" => "frameworks/base/include",
"frameworks-native" => "frameworks/native/include",
"libhardware" => "hardware/libhardware/include",
"libhardware_legacy" => "hardware/libhardware_legacy/include",
"libril" => "hardware/ril/include",
"system-core" => "system/core/include",
"audio" => "system/media/audio/include",
"audio-effects" => "system/media/audio_effects/include",
"audio-utils" => "system/media/audio_utils/include",
"audio-route" => "system/media/audio_route/include",
"wilhelm" => "frameworks/wilhelm/include",
"wilhelm-ut" => "frameworks/wilhelm/src/ut",
"mediandk" => "frameworks/av/media/ndk/"
}
DEF_INC_PATH_INNER="include-path-for"
DEF_INC_PATH="$(call #{DEF_INC_PATH_INNER}"
def _include_path_for(value)
pos = value.index(DEF_INC_PATH)
if pos then
incValue = StrUtil.getBlacket( value, "(", ")", pos)
posEnd = value.index(incValue) + incValue.length + 1
pos1 = incValue.index(DEF_INC_PATH_INNER, pos)
if pos1 then
incPathArg = incValue.slice(pos1+DEF_INC_PATH_INNER.length+1, incValue.length).strip
if DEF_INC_PATH_MAP.has_key?(incPathArg) then
replaceVal = "#{@androidRootPath}/#{DEF_INC_PATH_MAP[incPathArg]}"
value = value.slice(0, pos).to_s + replaceVal + value.slice(posEnd, value.length).to_s
end
end
end
return value
end
def _envEnsure
@env.clone.each do |key, val|
@env[key] = envEnsure(val)
end
end
def envEnsure(value)
@env.each do |key, replaceValue|
replaceKey = "\$\(#{key}\)"
value = value.to_s.gsub(replaceKey, replaceValue.to_s )
end
value = _subst(value)
return value
end
DEF_OUTPUT_IDENTIFIER="LOCAL_MODULE" #Regexp.compile("LOCAL_MODULE *:=")
DEF_INCLUDE_IDENTIFIER="LOCAL_C_INCLUDES" #Regexp.compile("LOCAL_C_INCLUDES *(\\+|:)=")
DEF_CFLAGS_IDENTIFIER = [
"LOCAL_CPPFLAGS",
"LOCAL_CFLAGS",
"LOCAL_CONLYFLAGS"
]
DEF_PREBUILT_NAME_IDENTIFIER = "LOCAL_SRC_FILES"
DEF_PREBUILT_LIBS_IDENTIFIER = "LOCAL_PREBUILT_LIBS"
DEF_PREBUILT_JAR_IDENTIFIER = "LOCAL_PREBUILT_JAVA_LIBRARIES"
DEF_PREBUILT_STATIC_JAR_IDENTIFIER = "LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES"
DEF_APK_PACKAGE_NAME_IDENTIFIER = "LOCAL_PACKAGE_NAME"
DEF_APK_OPTIMIZE_IDENTIFIER = "LOCAL_PROGUARD_ENABLED"
DEF_CERTIFICATE_IDENTIFIER = "LOCAL_CERTIFICATE"
DEF_DEX_PREOPT_IDENTIFIER = "LOCAL_DEX_PREOPT"
DEF_NATIVE_LIB_IDENTIFIER=[
Regexp.compile("\(BUILD_(STATIC|SHARED)_LIBRARY\)"),
Regexp.compile("\(PREBUILT_SHARED_LIBRARY\)")
# Regexp.compile("LOCAL_MODULE_CLASS.*\=.*(STATIC|SHARED)_LIBRARIES")
]
DEF_PREBUILT_IDENTIFIER=[
Regexp.compile("\(BUILD_PREBUILT\)"),
Regexp.compile("\(BUILD_MULTI_PREBUILT\)")
]
DEF_APK_IDENTIFIER=[
Regexp.compile("\(BUILD_PACKAGE\)"),
#Regexp.compile("\(BUILD_CTS_PACKAGE\)"),
Regexp.compile("\(BUILD_RRO_PACKAGE\)"),
Regexp.compile("\(BUILD_PHONY_PACKAGE\)"),
# Regexp.compile("LOCAL_MODULE_CLASS.*\=.*APPS")
]
DEF_JAR_IDENTIFIER=[
Regexp.compile("\(BUILD_STATIC_JAVA_LIBRARY\)"),
Regexp.compile("\(BUILD_JAVA_LIBRARY\)"),
# Regexp.compile("LOCAL_MODULE_CLASS.*\=.*JAVA_LIBRARIES")
]
def parseMakefile(makefileBody)
theLine = ""
targetIdentifiers = DEF_PREBUILT_IDENTIFIER
targetIdentifiers = targetIdentifiers | DEF_NATIVE_LIB_IDENTIFIER if @enableNativeScan
targetIdentifiers = targetIdentifiers | DEF_APK_IDENTIFIER if @enableApkScan
targetIdentifiers = targetIdentifiers | DEF_JAR_IDENTIFIER if @enableJarScan
makefileBody.each do |aLine|
aLine.strip!
next if aLine.start_with?('''#''') #skip comment
targetIdentifiers.each do |aCondition|
if aLine.match(aCondition) then
@currentResult.builtOuts.each do |aBuiltOut|
theName = AndroidUtil.getFilenameFromPathWithoutExt(aBuiltOut)
@currentResult.libName = theName if DEF_NATIVE_LIB_IDENTIFIER.include?(aCondition)
@currentResult.apkName = theName if DEF_APK_IDENTIFIER.include?(aCondition)
@currentResult.jarName = theName if DEF_JAR_IDENTIFIER.include?(aCondition)
end
@currentResult = ParseResult.new()
@results << @currentResult
end
end
theLine = "#{theLine} #{aLine}"
if !aLine.end_with?("\\") then
key, value = getKeyValueFromLine( theLine )
if key and value then
value = envEnsure(value) if @envFlatten
@env[key] = value
end
if value then
case key
when DEF_INCLUDE_IDENTIFIER
if @enableNativeScan then
val = value.to_s.split("\\").map(&:strip!)
val.each do |aVal|
if aVal then
aVal = _include_path_for(aVal.to_s)
theLibIncludePath = getRobustPath(@makefileDirectory, aVal)
@currentResult.nativeIncludes << theLibIncludePath if File.exist?(theLibIncludePath)
theLibIncludePath = getRobustPath(@androidRootPath, aVal)
@currentResult.nativeIncludes << theLibIncludePath if File.exist?(theLibIncludePath)
end
end
end
when DEF_OUTPUT_IDENTIFIER
@currentResult.builtOuts << value if value
when DEF_DEX_PREOPT_IDENTIFIER
if @enableApkScan || @enableJarScan then
@currentResult.dexPreOpt = value if value
end
when DEF_APK_PACKAGE_NAME_IDENTIFIER
@currentResult.apkName = value if value && @enableApkScan
@isApk = true
when DEF_CERTIFICATE_IDENTIFIER
if @enableApkScan || @enableJarScan then
@currentResult.certificate = value if value
end
when DEF_PREBUILT_LIBS_IDENTIFIER, DEF_PREBUILT_JAR_IDENTIFIER, DEF_PREBUILT_STATIC_JAR_IDENTIFIER
values = value.split(" ")
@currentResult.builtOuts = (@currentResult.builtOuts | values).uniq
when DEF_PREBUILT_NAME_IDENTIFIER
values = value.split(" ")
values.each do | value |
@currentResult.builtOuts << value if ArrayUtil.includes?( value, AndroidUtil::DEF_BUILTS_OUT_EXTS )
if @enableApkScan && value.include?(".apk") then
@currentResult.apkName = value
@isApk = true
elsif @enableNativeScan && ArrayUtil.includes?( value, [".so", ".a"] ) then
@currentResult.libName = value
@isNativeLib = true
elsif @enableJarScan && value.include?(".jar") then
@currentResult.jarName = value
@isJar = true
elsif @enableApexScan && value.include?(".apex") then
@currentResult.apexName = value
@isApex = true
end
end
when DEF_APK_OPTIMIZE_IDENTIFIER
if @enableApkScan && value then
if value.to_s.downcase == "disabled" then
@currentResult.optimizeEnabled = false
else
@currentResult.optimizeEnabled = value
end
end
else
if @enableNativeScan then
DEF_CFLAGS_IDENTIFIER.each do | aCFlags |
if aCFlags == key then
val = value.to_s.split("\\").map(&:strip!)
val.each do |aVal|
if aVal then
@currentResult.cflags << aVal
end
end
break
end
end
end
if @enableJarScan then
DEF_JAR_IDENTIFIER.each do | anIdentifier |
if theLine.match(anIdentifier) then
@currentResult.jarName = @currentResult.builtOuts.last if @currentResult.builtOuts.last
@isJar = true
end
end
end
end
end
theLine = ""
end
end
ensureNativeIncludes()
_envEnsure()
ensureCompilerOption()
end
def initialize(makefilePath, envFlatten, compilerFilter, enableNativeScan = true, enableApkScan = true, enableJarScan = true, enableApexScan = true)
super(makefilePath, envFlatten, compilerFilter, enableNativeScan, enableApkScan, enableJarScan, enableApexScan)
@env = {}
@env["call my-dir"] = FileUtil.getDirectoryFromPath(@makefilePath)
makefileBody = FileUtil.readFileAsArray(makefilePath)
targetIdentifiers = DEF_NATIVE_LIB_IDENTIFIER | DEF_APK_IDENTIFIER | DEF_JAR_IDENTIFIER # TODO:APEX by Android.mk?
targetIdentifiers.each do | aCondition |
result = makefileBody.grep(aCondition)
if !result.empty? then
# found native lib
@isNativeLib = true if @enableNativeScan && DEF_NATIVE_LIB_IDENTIFIER.include?(aCondition)
@isApk = true if @enableApkScan && DEF_APK_IDENTIFIER.include?(aCondition)
@isJar = true if @enableJarScan && DEF_JAR_IDENTIFIER.include?(aCondition)
@isApex = false
#break
end
end
parseMakefile(makefileBody) if @isNativeLib || @isApk || @isJar
end
def dump
return "path:#{@makefilePath}, nativeLib:#{@isNativeLib ? "true" : "false"}, builtOuts:#{@builtOuts.to_s}, includes:#{@nativeIncludes.to_s}"
end
end
class AndroidBpParser < AndroidMakefileParser
DEF_DEFAULTS_IDENTIFIER = "defaults"
DEF_DEFAULTS_IDENTIFIERS = [
"cc_defaults",
"java_defaults",
"rust_defaults",
"apex_defaults",
"_defaults" # wild card...
]
DEF_NATIVE_LIB_IDENTIFIER=[
"cc_library",
"cc_library_shared",
"cc_library_static"
]
DEF_NAME_IDENTIFIER = "name"
DEF_INCLUDE_DIRS = [
"export_include_dirs",
"header_libs",
"export_header_lib_headers",
"include_dirs",
"local_include_dirs",
]
DEF_COMPILE_OPTION = "cflags"
DEF_APK_IDENTIFIER = [
"android_app",
"android_app_import",
"runtime_resource_overlay"
]
DEF_APK_DEPENDENCIES_IDENTIFIER = [
"static_libs" # []
]
DEF_CERTIFICATE_IDENTIFIER = "certificate"
DEF_APK_PRIVILEGED_IDENTIFIER = "privileged"
DEF_APK_PLATFORM_API_IDENTIFIER = "platform_apis"
DEF_APK_OPTIMIZE_IDENTIFIER = "optimize"
DEF_APK_OPTIMIZE_ENABLED_IDENTIFIER = "enabled"
DEF_APK_OPTIMIZE_SHRINK_IDENTIFIER = "shrink"
DEF_DEX_PREOPT_IDENTIFIER = "dex_preopt"
DEF_DEX_PREOPT_ENABLED_IDENTIFIER = "enabled"
DEF_JAR_IDENTIFIER = [
"java_library_static",
"java_library",
"java_sdk_library",
"android_library"
]
DEF_APEX_IDENTIFIER = ["module_apex"]
def ensureJson(body)
return "{ #{body} }".gsub(/(\w+)\s*:/, '"\1":').gsub(/,(?= *\])/, '').gsub(/,(?= *\})/, '')
end
def removeRemark(makefileBody)
result = []
makefileBody.each do | aLine |
pos = aLine.index("//")
if pos then
aLine = aLine.slice(0,pos)
end
result << aLine if !aLine.empty?
end
return result
end
def getCorrespondingDefaults(body, targetDefaults)
result = {}
DEF_DEFAULTS_IDENTIFIERS.each do |aCondition|
startPos = 0
loop do
pos = body.index(aCondition, startPos)
if pos then
theBody = StrUtil.getBlacket(body, "{", "}", pos)
ensuredJson = ensureJson(theBody)
# update the next pos
startPos = pos + aCondition.length + theBody.length
theBp = {}
begin
theBp = JSON.parse(ensuredJson)
rescue => ex
end
if theBp.has_key?(DEF_NAME_IDENTIFIER) then
if theBp[DEF_NAME_IDENTIFIER] == targetDefaults then
result = theBp
break
end
end
else
# Not found
break
end
end
break if !result.empty?
end
return result
end
def mergeBp(bp1, bp2)
result = bp1
bp2.each do |key,value|
if !result.has_key?(key) then
result[key] = value
else
if value.kind_of?(Array) && result[key].kind_of?(Array) then
result[key] = result[key] | value
elsif value.kind_of?(Hash) && result[key].kind_of?(Hash) then
result[key] = result[key].merge( value )
else
# Unexpect override case
result[key] = value
end
end
end
return result
end
def ensureDefaults(body, theBp)
result = theBp
if theBp.has_key?(DEF_DEFAULTS_IDENTIFIER) then
defaults = theBp[DEF_DEFAULTS_IDENTIFIER]
theBp.delete( DEF_DEFAULTS_IDENTIFIER )
defaults.each do |aDefault|
theDefault = getCorrespondingDefaults(body, aDefault)
theDefault.delete( DEF_NAME_IDENTIFIER )
if theDefault.has_key?(DEF_DEFAULTS_IDENTIFIER) then
theDefault = ensureDefaults(body, theDefault)
end
theBp = mergeBp( theBp, theDefault )
end
result = theBp
end
return result
end
def parseMakefile(makefileBody)
body = removeRemark(makefileBody).join(" ")
targetIdentifier = []
targetIdentifier = targetIdentifier | DEF_NATIVE_LIB_IDENTIFIER if @enableNativeScan
targetIdentifier = targetIdentifier | DEF_APK_IDENTIFIER if @enableApkScan
targetIdentifier = targetIdentifier | DEF_JAR_IDENTIFIER if @enableJarScan
targetIdentifier = targetIdentifier | DEF_APEX_IDENTIFIER if @enableApexScan
targetIdentifier.each do |aCondition|
pos = body.index(aCondition)
if pos then
theBody = StrUtil.getBlacket(body, "{", "}", pos)
ensuredJson = ensureJson(theBody)
theBp = {}
begin
theBp = JSON.parse(ensuredJson)
rescue => ex
end
if !theBp.empty? then
theBp = ensureDefaults(body, theBp)
moduleName = nil
moduleName = theBp[DEF_NAME_IDENTIFIER] if theBp.has_key?(DEF_NAME_IDENTIFIER)
@currentResult.builtOuts << moduleName if moduleName
if @enableNativeScan && DEF_NATIVE_LIB_IDENTIFIER.include?(aCondition) then
@isNativeLib = true
@currentResult.libName = moduleName if moduleName
DEF_INCLUDE_DIRS.each do |anIncludeIdentifier|
if theBp.has_key?(anIncludeIdentifier) then
theBp[anIncludeIdentifier].to_a.each do |anInclude|
anInclude.to_s.strip!
anInclude = anInclude.slice(0, anInclude.length-1) if anInclude.end_with?(".")
if !anInclude.empty? then
theLibIncludePath = getRobustPath(@makefileDirectory, anInclude)
@currentResult.nativeIncludes << theLibIncludePath if File.exist?(theLibIncludePath)
theLibIncludePath = getRobustPath(@androidRootPath, anInclude)
@currentResult.nativeIncludes << theLibIncludePath if File.exist?(theLibIncludePath)
end
end
end
end
if theBp.has_key?(DEF_COMPILE_OPTION) then
theBp[DEF_COMPILE_OPTION].to_a.each do |anOption|
anOption = anOption.to_s.strip
@currentResult.cflags << anOption if !anOption.empty?
end
end
elsif @enableApkScan && DEF_APK_IDENTIFIER.include?(aCondition) then
@isApk = true
@currentResult.apkName = moduleName if moduleName
if theBp.has_key?(DEF_DEX_PREOPT_IDENTIFIER) then
val = theBp[DEF_DEX_PREOPT_IDENTIFIER]
if val.has_key?(DEF_DEX_PREOPT_ENABLED_IDENTIFIER) then
enabled = val[DEF_DEX_PREOPT_ENABLED_IDENTIFIER].to_s
@currentResult.dexPreOpt = enabled if enabled
end
end
if theBp.has_key?(DEF_CERTIFICATE_IDENTIFIER) then
val = theBp[DEF_CERTIFICATE_IDENTIFIER].to_s
@currentResult.certificate = val if val
end
if theBp.has_key?(DEF_APK_OPTIMIZE_IDENTIFIER) then
val = theBp[DEF_APK_OPTIMIZE_IDENTIFIER]
if val.has_key?(DEF_APK_OPTIMIZE_ENABLED_IDENTIFIER) then
theVal = val[DEF_APK_OPTIMIZE_ENABLED_IDENTIFIER].to_s
@currentResult.optimizeEnabled = theVal if theVal
end
if val.has_key?(DEF_APK_OPTIMIZE_SHRINK_IDENTIFIER) then
theVal = val[DEF_APK_OPTIMIZE_SHRINK_IDENTIFIER].to_s
@currentResult.optimizeShrink = theVal if theVal
end
end
elsif @enableJarScan && DEF_JAR_IDENTIFIER.include?(aCondition) then
@isJar = true
@currentResult.jarName = moduleName if moduleName
if theBp.has_key?(DEF_CERTIFICATE_IDENTIFIER) then
val = theBp[DEF_CERTIFICATE_IDENTIFIER].to_s
@currentResult.certificate = val if val
end
if theBp.has_key?(DEF_DEX_PREOPT_IDENTIFIER) then
val = theBp[DEF_DEX_PREOPT_IDENTIFIER]
if val.has_key?(DEF_DEX_PREOPT_ENABLED_IDENTIFIER) then
enabled = val[DEF_DEX_PREOPT_ENABLED_IDENTIFIER].to_s
@currentResult.dexPreOpt = enabled if enabled
end
end
elsif @enableApexScan && DEF_APEX_IDENTIFIER.include?(aCondition) then
@isApex = true
@currentResult.apexName = moduleName if moduleName
if theBp.has_key?(DEF_CERTIFICATE_IDENTIFIER) then
val = theBp[DEF_CERTIFICATE_IDENTIFIER].to_s
@currentResult.certificate = val if val
end
end
end
@currentResult = ParseResult.new()
@results << @currentResult
end
end
ensureNativeIncludes()
ensureCompilerOption()
end
def initialize(makefilePath, envFlatten, compilerFilter, enableNativeScan = true, enableApkScan = true, enableJarScan = true, enableApexScan = true)
super(makefilePath, envFlatten, compilerFilter, enableNativeScan, enableApkScan, enableJarScan, enableApexScan)
makefileBody = FileUtil.readFileAsArray(makefilePath)
parseMakefile(makefileBody)
end
def dump
return "path:#{@makefilePath}, nativeLib:#{@isNativeLib ? "true" : "false"}, builtOuts:#{@builtOuts.to_s}, includes:#{@nativeIncludes.to_s}"
end
end
class XmlReporterPerLib < XmlReporter
def initialize(reportOutPath, enableAppend=false)
@reportOutPath = reportOutPath
@outStream = nil #not necessary to call super()
end
def report(data, outputSections=nil, options={})
outputSections = outputSections ? outputSections.split("|") : nil
mainKey = nil
if outputSections then
mainKey = outputSections[0]
end
data.each do |aData|
aData = _ensureFilteredHash(aData, outputSections) if aData.kind_of?(Hash)
reportPath = @reportOutPath
mainVal = nil
if mainKey then
mainVal = aData.has_key?(mainKey) ? aData[mainKey] : "library"
if mainVal.kind_of?(Array) then
mainVal = mainVal[0]