-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpackbeam_api.erl
More file actions
958 lines (887 loc) · 32.4 KB
/
packbeam_api.erl
File metadata and controls
958 lines (887 loc) · 32.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
%%
%% Copyright (c) dushin.net
%% All rights reserved.
%%
%% 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.
%%
%% SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
%%-----------------------------------------------------------------------------
%% @doc A library used to generate an
%% <a href="http://github.com/bettio/AtomVM">AtomVM</a> AVM file from a set of
%% files (beam files, previously built AVM files, or even arbitrary data files).
%% @end
%%-----------------------------------------------------------------------------
-module(packbeam_api).
%% API exports
-export([create/2, create/3, create/4, create/5, list/1, extract/3, delete/3]).
%% AVM Entry functions
-export([is_beam/1, is_entrypoint/1, get_element_name/1, get_element_data/1, get_element_module/1]).
% erlfmt:ignore We want to keep the block format
-define(AVM_HEADER,
16#23, 16#21, 16#2f, 16#75,
16#73, 16#72, 16#2f, 16#62,
16#69, 16#6e, 16#2f, 16#65,
16#6e, 16#76, 16#20, 16#41,
16#74, 16#6f, 16#6d, 16#56,
16#4d, 16#0a, 16#00, 16#00
).
-define(ALLOWED_CHUNKS, [
"AtU8", "Code", "ExpT", "LocT", "ImpT", "LitU", "FunT", "StrT", "LitT", "avmN"
]).
-define(BEAM_START_FLAG, 1).
-define(BEAM_CODE_FLAG, 2).
-define(NORMAL_FILE_FLAG, 4).
-opaque avm_element() :: [atom() | {atom(), term()}].
-type path() :: string().
-type avm_element_name() :: string().
-type options() :: #{
prune => boolean(),
start_module => module() | undefined,
application_module => module() | undefined,
include_lines => boolean(),
arch => atom()
}.
-export_type([
path/0,
avm_element/0,
avm_element_name/0,
options/0
]).
-define(DEFAULT_OPTIONS, #{
prune => false,
start_module => undefined,
application_module => undefined,
include_lines => true,
arch => undefined,
platform => generic_unix
}).
%%
%% Public API
%%
%%-----------------------------------------------------------------------------
%% @param OutputPath the path to write the AVM file
%% @param InputPaths a list of paths of beam files, AVM files, or normal data files
%% @returns ok if the file was created.
%% @throws Reason::string()
%% @doc Create an AVM file.
%%
%% Equivalent to `create(OutputPath, InputPaths, DefaultOptions)'
%%
%% where `DefaultOptions' is `#{
%% prune => false,
%% start_module => undefined,
%% application_module => undefined,
%% include_lines => false,
%% arch => undefined,
%% platform => generic_unix
%% }'
%%
%% @end
%%-----------------------------------------------------------------------------
-spec create(
OutputPath :: path(),
InputPaths :: [path()]
) -> ok | {error, Reason :: term()}.
create(OutputPath, InputPaths) ->
create(OutputPath, InputPaths, ?DEFAULT_OPTIONS).
%%-----------------------------------------------------------------------------
%% @param OutputPath the path to write the AVM file
%% @param InputPaths a list of paths of beam files, AVM files, or normal data files
%% @param Options creation options
%% @returns ok if the file was created.
%% @throws Reason::string()
%% @doc Create an AVM file.
%%
%% This function will create an AVM file at the location specified in
%% OutputPath, using the input files specified in InputPaths.
%% @end
%%-----------------------------------------------------------------------------
-spec create(
OutputPath :: path(),
InputPaths :: [path()],
Options :: options()
) -> ok | {error, Reason :: term()}.
create(OutputPath, InputPaths, Options) ->
#{
prune := Prune,
start_module := StartModule,
application_module := ApplicationModule,
include_lines := IncludeLines,
arch := Arch,
platform := Platform
} = maps:merge(?DEFAULT_OPTIONS, Options),
Files0 = parse_files(InputPaths, StartModule, IncludeLines),
Files1 = filter_arch_modules(Files0, Arch),
Files2 = filter_platform_modules(Files1, Platform),
Files3 =
if
Prune -> prune(Files2, ApplicationModule);
true -> Files2
end,
write_packbeam(
OutputPath,
Files3
).
%%-----------------------------------------------------------------------------
%% @param OutputPath the path to write the AVM file
%% @param InputPaths a list of paths of beam files, AVM files, or normal data files
%% @param Prune whether to prune the archive. Without pruning, all found BEAM files are included
%% in the output AVM file. With pruning, then packbeam will attempt to
%% determine which BEAM files are needed to run the application, depending
%% on which modules are (transitively) referenced from the AVM entrypoint.
%% @param StartModule if `undefined', then this parameter is a module that
%% is intended to the the start module for the application. This module
%% will occur first in the generated AVM file.
%% @returns ok if the file was created.
%% @throws Reason::string()
%% @deprecated This function is deprecated. Use `create/3' instead.
%% @doc Create an AVM file.
%%
%% Equivalent to create(OutputPath, InputPaths, undefined, Prune, StartModule).
%% @end
%% @hidden
%%-----------------------------------------------------------------------------
-spec create(
OutputPath :: path(),
InputPaths :: [path()],
Prune :: boolean(),
StartModule :: module() | undefined
) ->
ok | {error, Reason :: term()}.
create(OutputPath, InputPaths, Prune, StartModule) ->
io:format("WARNING: Deprecated function: ~p:create/4~n", [?MODULE]),
Options = #{prune => Prune, start_module => StartModule},
create(OutputPath, InputPaths, maps:merge(?DEFAULT_OPTIONS, Options)).
%%-----------------------------------------------------------------------------
%% @param OutputPath the path to write the AVM file
%% @param InputPaths a list of paths of beam files, AVM files, or normal data files
%% @param ApplicationModule If not `undefined', then this parameter designates
%% the name of an OTP application, from which additional dependencies
%% will be computed.
%% @param Prune whether to prune the archive. Without pruning, all found BEAM files are included
%% in the output AVM file. With pruning, then packbeam will attempt to
%% determine which BEAM files are needed to run the application, depending
%% on which modules are (transitively) referenced from the AVM entrypoint.
%% @param StartModule if `undefined', then this parameter is a module that
%% is intended to the the start module for the application. This module
%% will occur first in the generated AVM file.
%% @returns ok if the file was created.
%% @throws Reason::string()
%% @deprecated This function is deprecated. Use `create/3' instead.
%% @doc Create an AVM file.
%%
%% This function will create an AVM file at the location specified in
%% OutputPath, using the input files specified in InputPaths.
%% @end
%% @hidden
%%-----------------------------------------------------------------------------
-spec create(
OutputPath :: path(),
InputPaths :: [path()],
ApplicationModule :: module() | undefined,
Prune :: boolean(),
StartModule :: module() | undefined
) ->
ok | {error, Reason :: term()}.
create(OutputPath, InputPaths, ApplicationModule, Prune, StartModule) ->
io:format("WARNING: Deprecated function: ~p:create/5~n", [?MODULE]),
Options = #{
prune => Prune, start_module => StartModule, application_module => ApplicationModule
},
create(OutputPath, InputPaths, maps:merge(?DEFAULT_OPTIONS, Options)).
%%-----------------------------------------------------------------------------
%% @param InputPath the AVM file from which to list elements
%% @returns list of element data
%% @throws Reason::string()
%% @doc List the contents of an AVM file.
%%
%% This function will list the contents of an AVM file at the
%% location specified in InputPath.
%% @end
%%-----------------------------------------------------------------------------
-spec list(InputPath :: path()) -> [avm_element()].
list(InputPath) ->
case file_type(InputPath) of
avm ->
parse_file(InputPath, false);
_ ->
throw(io_lib:format("Expected AVM file: ~p", [InputPath]))
end.
%%-----------------------------------------------------------------------------
%% @param InputPath the AVM file from which to extract elements
%% @param AVMElementNames a list of elements from the source AVM file to extract. If
%% empty, then extract all elements.
%% @param OutputDir the directory to write the contents
%% @returns ok if the file was created.
%% @throws Reason::string()
%% @doc Extract all or selected elements from an AVM file.
%%
%% This function will extract elements of an AVM file at the location specified in
%% InputPath, specified by the supplied list of names. The elements
%% from the input AVM file will be written into the specified output directory,
%% creating any subdirectories if the AVM file elements contain path information.
%% @end
%%-----------------------------------------------------------------------------
-spec extract(
InputPath :: path(),
AVMElementNames :: [avm_element_name()],
OutputDir :: path()
) -> ok | {error, Reason :: term()}.
extract(InputPath, AVMElementNames, OutputDir) ->
case file_type(InputPath) of
avm ->
ParsedFiles = parse_file(InputPath, false),
write_files(filter_names(AVMElementNames, ParsedFiles), OutputDir);
_ ->
throw(io_lib:format("Expected AVM file: ~p", [InputPath]))
end.
%%-----------------------------------------------------------------------------
%% @param InputPath the AVM file from which to delete elements
%% @param OutputPath the path to write the AVM file
%% @param AVMElementNames a list of elements from the source AVM file to delete
%% @returns ok if the file was created.
%% @throws Reason::string()
%% @doc Delete selected elements of an AVM file.
%%
%% This function will delete elements of an AVM file at the location specified in
%% InputPath, specified by the supplied list of names. The output AVM file
%% is written to OutputPath, which may be the same as InputPath.
%% @end
%%-----------------------------------------------------------------------------
-spec delete(
OutputPath :: path(),
InputPath :: path(),
AVMElementNames :: [avm_element_name()]
) -> ok | {error, Reason :: term()}.
delete(OutputPath, InputPath, AVMElementNames) ->
case file_type(InputPath) of
avm ->
ParsedFiles = parse_file(InputPath, false),
write_packbeam(OutputPath, remove_names(AVMElementNames, ParsedFiles));
_ ->
throw(io_lib:format("Expected AVM file: ~p", [InputPath]))
end.
%%-----------------------------------------------------------------------------
%% @param AVMElement An AVM file element
%% @returns the name of the element.
%% @doc Return the name of the element.
%% @end
%%-----------------------------------------------------------------------------
-spec get_element_name(AVMElement :: avm_element()) -> avm_element_name().
get_element_name(AVMElement) ->
proplists:get_value(element_name, AVMElement).
%%-----------------------------------------------------------------------------
%% @param AVMElement An AVM file element
%% @returns the AVM element data.
%% @doc Return AVM element data.
%% @end
%%-----------------------------------------------------------------------------
-spec get_element_data(AVMElement :: avm_element()) -> binary().
get_element_data(AVMElement) ->
proplists:get_value(data, AVMElement).
%%-----------------------------------------------------------------------------
%% @param AVMElement An AVM file element
%% @returns the AVM element module, if the element is a BEAM file; `undefined',
%% otherwise.
%% @doc Return AVM element module, if the element is a BEAM file.
%% @end
%%-----------------------------------------------------------------------------
-spec get_element_module(AVMElement :: avm_element()) -> module() | undefined.
get_element_module(AVMElement) ->
proplists:get_value(module, AVMElement).
%%-----------------------------------------------------------------------------
%% @param AVMElement An AVM file element
%% @returns `true' if the AVM element is an entrypoint (i.e., exports a `start/0'
%% function); `false' otherwise.
%% @doc Indicates whether the AVM file element is an entrypoint.
%% @end
%%-----------------------------------------------------------------------------
-spec is_entrypoint(AVMElement :: avm_element()) -> boolean().
is_entrypoint(AVMElement) ->
(get_flags(AVMElement) band ?BEAM_START_FLAG) =:= ?BEAM_START_FLAG.
%%-----------------------------------------------------------------------------
%% @param AVMElement An AVM file element
%% @returns `true' if the AVM element is a BEAM file; `false' otherwise.
%% @doc Indicates whether the AVM file element is a BEAM file.
%% @end
%%-----------------------------------------------------------------------------
-spec is_beam(AVMElement :: avm_element()) -> boolean().
is_beam(AVMElement) ->
(get_flags(AVMElement) band ?BEAM_CODE_FLAG) =:= ?BEAM_CODE_FLAG.
%%
%% Internal API functions
%%
%% @private
get_flags(AVMElement) ->
proplists:get_value(flags, AVMElement).
%% @private
parse_files(InputPaths, StartModule, IncludeLines) ->
Files = lists:foldl(
fun(InputPath, Accum) ->
Accum ++ parse_file(InputPath, IncludeLines)
end,
[],
InputPaths
),
case StartModule of
undefined ->
Files;
_ ->
reorder_start_module(StartModule, Files)
end.
%% @private
parse_file({InputPath, ModuleName}, IncludeLines) ->
parse_file(file_type(InputPath), ModuleName, load_file(InputPath), IncludeLines);
parse_file(InputPath, IncludeLines) ->
parse_file(file_type(InputPath), InputPath, load_file(InputPath), IncludeLines).
%% @private
file_type(InputPath) ->
case ends_with(InputPath, ".beam") of
true ->
beam;
_ ->
case ends_with(InputPath, ".avm") of
true -> avm;
_ -> normal
end
end.
%% @private
load_file(Path) ->
case file:read_file(Path) of
{ok, Data} ->
Data;
{error, Reason} ->
throw(io_lib:format("Unable to load file ~s. Reason: ~p", [Path, Reason]))
end.
%% @private
prune(ParsedFiles, RootApplicationModule) ->
case find_entrypoint(ParsedFiles) of
false ->
throw("No input beam files contain start/0 entrypoint");
{value, Entrypoint} ->
BeamFiles = lists:filter(fun is_beam/1, ParsedFiles),
Modules = closure(Entrypoint, BeamFiles, [get_element_module(Entrypoint)]),
ApplicationStartModules = find_application_modules(ParsedFiles, RootApplicationModule),
ApplicationModules = find_dependencies(ApplicationStartModules, BeamFiles),
filter_modules(Modules ++ ApplicationModules, ParsedFiles)
end.
find_application_modules(_ParsedFiles, undefined) ->
[];
find_application_modules(ParsedFiles, ApplicationModule) ->
ApplicationSpecs = find_all_application_specs(ParsedFiles),
find_application_start_modules(ParsedFiles, ApplicationSpecs, ApplicationModule).
find_all_application_specs(ParsedFiles) ->
ApplicationFiles = lists:filter(
fun is_application_file/1,
ParsedFiles
),
[get_application_spec(ApplicationFile) || ApplicationFile <- ApplicationFiles].
find_application_start_modules(ParsedFiles, ApplicationSpecs, ApplicationModule) ->
case find_application_spec(ApplicationSpecs, ApplicationModule) of
false ->
[];
{value, {application, _ApplicationModule, Properties}} ->
ChildApplicationStartModules =
case proplists:get_value(applications, Properties) of
Applications when is_list(Applications) ->
lists:foldl(
fun(Application, InnerAccum) ->
find_application_start_modules(
ParsedFiles, ApplicationSpecs, Application
) ++ InnerAccum
end,
[],
Applications
);
_ ->
[]
end,
StartModules =
case proplists:get_value(mod, Properties) of
{StartModule, _Args} when is_atom(StartModule) ->
[StartModule];
_ ->
[]
end,
ChildApplicationStartModules ++ StartModules
end.
find_dependencies(Entrypoints, BeamFiles) ->
deduplicate(
lists:flatten(
[
closure(
get_parsed_file(Entrypoint, BeamFiles),
BeamFiles,
[Entrypoint]
)
|| Entrypoint <- Entrypoints
]
)
).
find_application_spec(ApplicationSpecs, ApplicationModule) ->
lists:search(
fun({application, Module, _Properties}) ->
Module =:= ApplicationModule
end,
ApplicationSpecs
).
get_application_spec(ApplicationFile) ->
ApplicationData = get_element_data(ApplicationFile),
<<_Size:4/binary, SerializedTerm/binary>> = ApplicationData,
binary_to_term(SerializedTerm).
is_application_file(ParsedFile) ->
case not is_beam(ParsedFile) of
true ->
ModuleName = get_element_name(ParsedFile),
Components = string:split(ModuleName, "/", all),
case Components of
[_ModuleName, "priv", "application.bin"] ->
true;
_ ->
false
end;
false ->
false
end.
%% @private
find_entrypoint(ParsedFiles) ->
lists:search(fun is_entrypoint/1, ParsedFiles).
%% @private
closure(_Current, [], Accum) ->
lists:reverse(Accum);
closure(Current, Candidates, Accum) ->
CandidateModules = get_element_modules(Candidates),
CurrentsImports = get_imports(Current),
CurrentsAtoms = get_atoms(Current),
DepModules = intersection(CurrentsImports ++ CurrentsAtoms, CandidateModules) -- Accum,
lists:foldl(
fun(DepModule, A) ->
case lists:member(DepModule, A) of
true ->
A;
_ ->
NewCandidates = remove(DepModule, Candidates),
closure(get_parsed_file(DepModule, Candidates), NewCandidates, [DepModule | A])
end
end,
Accum,
DepModules
).
%% @private
remove(Module, ParsedFiles) ->
[P || P <- ParsedFiles, Module =/= get_element_module(P)].
%% @private
get_imports(ParsedFile) ->
Imports = proplists:get_value(imports, proplists:get_value(chunk_refs, ParsedFile)),
lists:usort([M || {M, _F, _A} <- Imports]).
%% @private
get_atoms(ParsedFile) ->
AtomsT = [
Atom
|| {_Index, Atom} <- proplists:get_value(atoms, proplists:get_value(chunk_refs, ParsedFile))
],
AtomsFromLiterals = get_atom_literals(proplists:get_value(uncompressed_literals, ParsedFile)),
lists:usort(AtomsT ++ AtomsFromLiterals).
%% @private
get_atom_literals(undefined) ->
[];
get_atom_literals(UncompressedLiterals) ->
<<NumLiterals:32, Rest/binary>> = UncompressedLiterals,
get_atom_literals(NumLiterals, Rest, []).
%% @private
get_atom_literals(0, <<"">>, Accum) ->
Accum;
get_atom_literals(I, Data, Accum) ->
<<Length:32, StartData/binary>> = Data,
<<EncodedLiteral:Length/binary, Rest/binary>> = StartData,
Literal = binary_to_term(EncodedLiteral),
ExtractedAtoms = extract_atoms(Literal, []),
get_atom_literals(I - 1, Rest, ExtractedAtoms ++ Accum).
%% @private
extract_atoms(Term, Accum) when is_atom(Term) ->
[Term | Accum];
extract_atoms(Term, Accum) when is_tuple(Term) ->
extract_atoms(tuple_to_list(Term), Accum);
extract_atoms(Term, Accum) when is_map(Term) ->
extract_atoms(maps:to_list(Term), Accum);
extract_atoms([H | T], Accum) ->
HeadAtoms = extract_atoms(H, []),
extract_atoms(T, HeadAtoms ++ Accum);
extract_atoms(_Term, Accum) ->
Accum.
%% @private
get_element_modules(ParsedFiles) ->
[get_element_module(ParsedFile) || ParsedFile <- ParsedFiles].
%% @private
get_parsed_file(Module, ParsedFiles) ->
SearchResult = lists:search(
fun(ParsedFile) ->
get_element_module(ParsedFile) =:= Module
end,
ParsedFiles
),
case SearchResult of
{value, V} ->
V;
false ->
exit({module_not_found, Module, ParsedFiles})
end.
%% @private
intersection(A, B) ->
lists:filter(
fun(E) ->
lists:member(E, B)
end,
A
).
%% @private
filter_modules(Modules, ParsedFiles) ->
lists:filter(
fun(ParsedFile) ->
case is_beam(ParsedFile) of
true ->
lists:member(get_element_module(ParsedFile), Modules);
_ ->
true
end
end,
ParsedFiles
).
%% @private
parse_file(beam, _ModuleName, Data, IncludeLines) ->
{ok, Module, Chunks} = beam_lib:all_chunks(Data),
{UncompressedChunks, UncompressedLiterals} = maybe_uncompress_literals(Chunks),
FilteredChunks = filter_chunks(UncompressedChunks, IncludeLines),
{ok, Binary} = beam_lib:build_module(FilteredChunks),
{ok, {Module, ChunkRefs}} = beam_lib:chunks(Data, [imports, exports, atoms]),
Exports = proplists:get_value(exports, ChunkRefs),
Flags =
case lists:member({start, 0}, Exports) of
true ->
?BEAM_CODE_FLAG bor ?BEAM_START_FLAG;
_ ->
?BEAM_CODE_FLAG
end,
[
[
{module, Module},
{element_name, io_lib:format("~s.beam", [atom_to_list(Module)])},
{flags, Flags},
{data, Binary},
{chunk_refs, ChunkRefs},
{uncompressed_literals, UncompressedLiterals}
]
];
parse_file(avm, ModuleName, Data, _IncludeLines) ->
case Data of
<<?AVM_HEADER, AVMData/binary>> ->
parse_avm_data(AVMData);
_ ->
throw(io_lib:format("Invalid AVM header: ~p", [ModuleName]))
end;
parse_file(normal, ModuleName, Data, _IncludeLines) ->
DataSize = byte_size(Data),
Blob = <<DataSize:32, Data/binary>>,
[[{element_name, ModuleName}, {flags, ?NORMAL_FILE_FLAG}, {data, Blob}]].
%% @private
reorder_start_module(StartModule, Files) ->
{StartProps, Other} =
lists:partition(
fun(Props) ->
% io:format("Props: ~w~n", [Props]),
case get_element_module(Props) of
StartModule ->
case is_entrypoint(Props) of
true -> true;
_ -> throw({start_module_not_start_beam, StartModule})
end;
_ ->
false
end
end,
Files
),
case StartProps of
[] ->
throw({start_module_not_found, StartModule});
_ ->
StartProps ++ Other
end.
%% @private
parse_avm_data(AVMData) ->
parse_avm_data(AVMData, []).
%% @private
parse_avm_data(<<"">>, Accum) ->
lists:reverse(Accum);
parse_avm_data(<<0:32/integer, _AVMRest/binary>>, Accum) ->
lists:reverse(Accum);
parse_avm_data(<<Size:32/integer, AVMRest/binary>>, Accum) ->
SizeWithoutSizeField = Size - 4,
case SizeWithoutSizeField =< erlang:byte_size(AVMRest) of
true ->
<<AVMBeamData:SizeWithoutSizeField/binary, AVMNext/binary>> = AVMRest,
Beam = parse_beam(AVMBeamData, [], in_header, 0, []),
parse_avm_data(AVMNext, [Beam | Accum]);
_ ->
throw(
io_lib:format("Invalid AVM data size: ~p (AVMRest=~p)", [
Size, erlang:byte_size(AVMRest)
])
)
end.
%% @private
parse_beam(<<Flags:32, _Reserved:32, Rest/binary>>, [], in_header, Pos, Accum) ->
parse_beam(Rest, [], in_element_name, Pos + 8, [{flags, Flags} | Accum]);
parse_beam(<<0:8, Rest/binary>>, Tmp, in_element_name, Pos, Accum) ->
ModuleName = lists:reverse(Tmp),
case (Pos + 1) rem 4 of
0 ->
parse_beam(Rest, Tmp, in_data, Pos, [{element_name, ModuleName} | Accum]);
_ ->
parse_beam(Rest, [], eat_padding, Pos + 1, [{element_name, ModuleName} | Accum])
end;
parse_beam(<<C:8, Rest/binary>>, Tmp, in_element_name, Pos, Accum) ->
parse_beam(Rest, [C | Tmp], in_element_name, Pos + 1, Accum);
parse_beam(<<0:8, Rest/binary>>, Tmp, eat_padding, Pos, Accum) ->
case (Pos + 1) rem 4 of
0 ->
parse_beam(Rest, Tmp, in_data, Pos, Accum);
_ ->
parse_beam(Rest, Tmp, eat_padding, Pos + 1, Accum)
end;
parse_beam(Bin, Tmp, eat_padding, Pos, Accum) ->
parse_beam(Bin, Tmp, in_data, Pos, Accum);
parse_beam(Data, _Tmp, in_data, _Pos, Accum) ->
case is_beam(Accum) orelse is_entrypoint(Accum) of
true ->
StrippedData = strip_padding(Data),
{ok, {Module, ChunkRefs}} = beam_lib:chunks(StrippedData, [imports, exports, atoms]),
[{module, Module}, {chunk_refs, ChunkRefs}, {data, StrippedData} | Accum];
_ ->
[{data, Data} | Accum]
end.
strip_padding(<<0:8, Rest/binary>>) ->
strip_padding(Rest);
strip_padding(Data) ->
Data.
%% @private
maybe_uncompress_literals(Chunks) ->
case proplists:get_value("LitT", Chunks) of
undefined ->
{Chunks, undefined};
<<0:32, Data/binary>> ->
{Chunks, Data};
<<_Size:4/binary, Data/binary>> ->
do_uncompress_literals(Chunks, Data)
end.
%% @private
do_uncompress_literals(Chunks, Data) ->
UncompressedData = zlib:uncompress(Data),
{
lists:keyreplace(
"LitT",
1,
Chunks,
{"LitU", UncompressedData}
),
UncompressedData
}.
%% @private
write_packbeam(OutputFilePath, ParsedFiles) ->
PackedData =
[<<?AVM_HEADER>> | [pack_data(ParsedFile) || ParsedFile <- ParsedFiles]] ++
[create_header(0, 0, <<"end">>)],
file:write_file(OutputFilePath, PackedData).
%% @private
pack_data(ParsedFile) ->
ModuleName = list_to_binary(get_element_name(ParsedFile)),
Flags = get_flags(ParsedFile),
Data = get_element_data(ParsedFile),
HeaderSize = header_size(ModuleName),
HeaderPadding = create_padding(HeaderSize),
DataSize = byte_size(Data),
DataPadding = create_padding(DataSize),
Size = HeaderSize + byte_size(HeaderPadding) + DataSize + byte_size(DataPadding),
Header = create_header(Size, Flags, ModuleName),
<<Header/binary, HeaderPadding/binary, Data/binary, DataPadding/binary>>.
%% @private
header_size(ModuleName) ->
4 + 4 + 4 + byte_size(ModuleName) + 1.
%% @private
create_header(Size, Flags, ModuleName) ->
<<Size:32, Flags:32, 0:32, ModuleName/binary, 0:8>>.
%% @private
create_padding(Size) ->
case Size rem 4 of
0 -> <<"">>;
K -> list_to_binary(lists:duplicate(4 - K, 0))
end.
%% @private
ends_with(String, Suffix) ->
string:find(String, Suffix, trailing) =:= Suffix.
%% @private
filter_chunks(Chunks, IncludeLines) ->
AllowedChunks0 = allowed_chunks(IncludeLines),
AllowedChunks1 =
case lists:keymember("avmN", 1, Chunks) of
true -> AllowedChunks0 -- ["Code", "Type"];
false -> AllowedChunks0 -- ["avmN"]
end,
lists:filter(
fun({Tag, _Data}) -> lists:member(Tag, AllowedChunks1) end,
Chunks
).
allowed_chunks(false) ->
?ALLOWED_CHUNKS;
allowed_chunks(true) ->
["Line" | ?ALLOWED_CHUNKS].
%% @private
remove_names(Names, ParsedFiles) ->
lists:filter(
fun(ParsedFile) ->
ModuleName = get_element_name(ParsedFile),
not lists:member(ModuleName, Names)
end,
ParsedFiles
).
%% @private
write_files(ParsedFiles, OutputDir) ->
case filelib:is_dir(OutputDir) of
true ->
io:format("Writing to ~s ...~n", [OutputDir]),
lists:foreach(
fun(ParsedFile) ->
ModuleName = get_element_name(ParsedFile),
Path = OutputDir ++ "/" ++ ModuleName,
case filelib:ensure_dir(Path) of
ok ->
io:format("x ~s~n", [ModuleName]),
RawData = get_element_data(ParsedFile),
Data =
case file_type(ModuleName) of
normal ->
<<Size:32, Rest/binary>> = RawData,
<<Contents:Size/binary, _/binary>> = Rest,
Contents;
_ ->
RawData
end,
file:write_file(Path, Data);
Error ->
throw(Error)
end
end,
ParsedFiles
);
_ ->
throw(enoent)
end.
%% @private
filter_names([], ParsedFiles) ->
ParsedFiles;
filter_names(Names, ParsedFiles) ->
lists:filter(
fun(ParsedFile) ->
ModuleName = get_element_name(ParsedFile),
lists:member(ModuleName, Names)
end,
ParsedFiles
).
%% @private
deduplicate([]) ->
[];
deduplicate([H | T]) ->
[H | [X || X <- deduplicate(T), X /= H]].
% Some modules may be for several platforms, so keep list of modules we can
% filter out.
-define(PLATFORM_MODULES_FILTER, #{
generic_unix => [
"pico.beam",
"i2c.beam",
"gpio.beam",
"esp_adc.beam",
"esp.beam",
"emscripten.beam",
"jit_stream_flash.beam"
],
esp32 => [
"pico.beam",
"emscripten.beam",
"jit_stream_mmap.beam"
],
stm32 => [
"pico.beam",
"esp_adc.beam",
"esp.beam",
"emscripten.beam",
"jit_stream_mmap.beam"
],
rp2 => [
"esp_adc.beam",
"esp.beam",
"emscripten.beam",
"jit_stream_mmap.beam"
],
emscripten => [
"pico.beam",
"i2c.beam",
"gpio.beam",
"esp_adc.beam",
"esp.beam",
"emscripten.beam",
"jit_stream_flash.beam",
"jit_stream_mmap.beam"
]
}).
filter_platform_modules(Files, Platform) when is_map_key(Platform, ?PLATFORM_MODULES_FILTER) ->
lists:subtract(Files, map_get(Platform, ?PLATFORM_MODULES_FILTER));
filter_platform_modules(Files, _) ->
Files.
-define(ARCH_MODULES_FILTER, #{
aarch64 => [
"jit_armv6m.beam",
"jit_armv6m_asm.beam",
"jit_riscv32.beam",
"jit_riscv32_asm.beam",
"jit_x86_64.beam",
"jit_x86_64_asm.beam"
],
armv6m => [
"jit_aarch64.beam",
"jit_aarch64_asm.beam",
"jit_riscv32.beam",
"jit_riscv32_asm.beam",
"jit_x86_64.beam",
"jit_x86_64_asm.beam"
],
riscv32 => [
"jit_aarch64.beam",
"jit_aarch64_asm.beam",
"jit_armv6m.beam",
"jit_armv6m_asm.beam",
"jit_x86_64.beam",
"jit_x86_64_asm.beam"
],
x86_64 => [
"jit_aarch64.beam",
"jit_aarch64_asm.beam",
"jit_armv6m.beam",
"jit_armv6m_asm.beam",
"jit_riscv32.beam",
"jit_riscv32_asm.beam"
]
}).
filter_arch_modules(Files, Platform) when is_map_key(Platform, ?ARCH_MODULES_FILTER) ->
lists:subtract(Files, map_get(Platform, ?ARCH_MODULES_FILTER));
filter_arch_modules(Files, _) ->
Files.