Skip to content

Commit 554019b

Browse files
committed
JIT: introduce filtering of modules with --arch and --platform arguments
Signed-off-by: Paul Guyot <[email protected]>
1 parent bf1c4db commit 554019b

File tree

2 files changed

+89
-12
lines changed

2 files changed

+89
-12
lines changed

src/packbeam.erl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,8 @@ do_create(Opts, Args) ->
187187
#{
188188
prune => maps:get(prune, Opts, false),
189189
start_module => maps:get(start_module, Opts, undefined),
190-
include_lines => not maps:get(remove_lines, Opts, false)
190+
include_lines => not maps:get(remove_lines, Opts, false),
191+
arch => maps:get(arch, Opts, undefined)
191192
}
192193
),
193194
0.
@@ -346,5 +347,9 @@ parse_args(["-f", Format | T], {Opts, Args}) ->
346347
parse_args(["--format", Format | T], {Opts, Args});
347348
parse_args(["--format", Format | T], {Opts, Args}) ->
348349
parse_args(T, {Opts#{format => Format}, Args});
350+
parse_args(["--arch", Arch | T], {Opts, Args}) ->
351+
parse_args(T, {Opts#{arch => list_to_atom(Arch)}, Args});
352+
parse_args(["--platform", Platform | T], {Opts, Args}) ->
353+
parse_args(T, {Opts#{platform => list_to_atom(Platform)}, Args});
349354
parse_args([H | T], {Opts, Args}) ->
350355
parse_args(T, {Opts, [H | Args]}).

src/packbeam_api.erl

Lines changed: 83 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@
5454
prune => boolean(),
5555
start_module => module() | undefined,
5656
application_module => module() | undefined,
57-
include_lines => boolean()
57+
include_lines => boolean(),
58+
arch => atom()
5859
}.
5960

6061
-export_type([
@@ -68,7 +69,9 @@
6869
prune => false,
6970
start_module => undefined,
7071
application_module => undefined,
71-
include_lines => true
72+
include_lines => true,
73+
arch => undefined,
74+
platform => generic_unix
7275
}).
7376

7477
%%
@@ -88,7 +91,9 @@
8891
%% prune => false,
8992
%% start_module => undefined,
9093
%% application_module => undefined,
91-
%% include_lines => false
94+
%% include_lines => false,
95+
%% arch => undefined,
96+
%% platform => generic_unix
9297
%% }'
9398
%%
9499
%% @end
@@ -122,15 +127,20 @@ create(OutputPath, InputPaths, Options) ->
122127
prune := Prune,
123128
start_module := StartModule,
124129
application_module := ApplicationModule,
125-
include_lines := IncludeLines
130+
include_lines := IncludeLines,
131+
arch := Arch,
132+
platform := Platform
126133
} = maps:merge(?DEFAULT_OPTIONS, Options),
127-
ParsedFiles = parse_files(InputPaths, StartModule, IncludeLines),
134+
Files0 = parse_files(InputPaths, StartModule, IncludeLines),
135+
Files1 = filter_jit(Files0, Arch, Platform),
136+
Files2 =
137+
if
138+
Prune -> prune(Files1, ApplicationModule);
139+
true -> Files1
140+
end,
128141
write_packbeam(
129142
OutputPath,
130-
case Prune of
131-
true -> prune(ParsedFiles, ApplicationModule);
132-
_ -> ParsedFiles
133-
end
143+
Files2
134144
).
135145

136146
%%-----------------------------------------------------------------------------
@@ -782,9 +792,14 @@ ends_with(String, Suffix) ->
782792

783793
%% @private
784794
filter_chunks(Chunks, IncludeLines) ->
785-
AllowedChunks = allowed_chunks(IncludeLines),
795+
AllowedChunks0 = allowed_chunks(IncludeLines),
796+
AllowedChunks1 =
797+
case lists:keymember("avmN", 1, Chunks) of
798+
true -> AllowedChunks0 -- ["Code", "Type"];
799+
false -> AllowedChunks0 -- ["avmN"]
800+
end,
786801
lists:filter(
787-
fun({Tag, _Data}) -> lists:member(Tag, AllowedChunks) end,
802+
fun({Tag, _Data}) -> lists:member(Tag, AllowedChunks1) end,
788803
Chunks
789804
).
790805

@@ -853,3 +868,60 @@ deduplicate([]) ->
853868
[];
854869
deduplicate([H | T]) ->
855870
[H | [X || X <- deduplicate(T), X /= H]].
871+
872+
filter_jit(Files0, undefined, generic_unix) ->
873+
Files0;
874+
filter_jit(Files0, aarch64, generic_unix) ->
875+
lists:filter(
876+
fun(File) ->
877+
filter_jit0(File, [
878+
"jit.beam", "jit_stream_mmap.beam", "jit_aarch64.beam", "jit_aarch64_asm.beam"
879+
])
880+
end,
881+
Files0
882+
);
883+
filter_jit(Files0, x86_64, generic_unix) ->
884+
lists:filter(
885+
fun(File) ->
886+
filter_jit0(File, [
887+
"jit.beam", "jit_stream_mmap.beam", "jit_x86_64.beam", "jit_x86_64_asm.beam"
888+
])
889+
end,
890+
Files0
891+
);
892+
filter_jit(Files0, armv6m, generic_unix) ->
893+
lists:filter(
894+
fun(File) ->
895+
filter_jit0(File, [
896+
"jit.beam", "jit_stream_mmap.beam", "jit_armv6m.beam", "jit_armv6m_asm.beam"
897+
])
898+
end,
899+
Files0
900+
);
901+
filter_jit(Files0, armv6m, rp2) ->
902+
lists:filter(
903+
fun(File) ->
904+
filter_jit0(File, [
905+
"jit.beam", "jit_stream_flash.beam", "jit_armv6m.beam", "jit_armv6m_asm.beam"
906+
])
907+
end,
908+
Files0
909+
);
910+
filter_jit(Files0, armv6m, stm32) ->
911+
lists:filter(
912+
fun(File) ->
913+
filter_jit0(File, [
914+
"jit.beam", "jit_stream_flash.beam", "jit_armv6m.beam", "jit_armv6m_asm.beam"
915+
])
916+
end,
917+
Files0
918+
).
919+
920+
filter_jit0(File, JITModules) ->
921+
ModuleName = lists:flatten(get_element_name(File)),
922+
case ModuleName of
923+
"jit" ++ _ ->
924+
lists:member(ModuleName, JITModules);
925+
_ ->
926+
true
927+
end.

0 commit comments

Comments
 (0)