Skip to content

Commit 7ac6948

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

File tree

2 files changed

+124
-12
lines changed

2 files changed

+124
-12
lines changed

src/packbeam.erl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ print_help() ->
131131
" [--prune|-p] Prune dependencies~n"
132132
" [--start|-s <module>] Start module~n"
133133
" [--remove_lines|-r] Remove line number information from AVM files~n"
134+
" [--arch <arch>] Filter out modules not for architecture~n"
135+
" (aarch64, armv6m, riscv32, x86_64)~n"
136+
" [--platform <plt>] Filter out modules not for platform~n"
137+
" (generic_unix, rp2, esp32, stm32, emscripten)"
134138
"~n"
135139
" list <options> <avm-file>~n"
136140
" where:~n"
@@ -187,7 +191,8 @@ do_create(Opts, Args) ->
187191
#{
188192
prune => maps:get(prune, Opts, false),
189193
start_module => maps:get(start_module, Opts, undefined),
190-
include_lines => not maps:get(remove_lines, Opts, false)
194+
include_lines => not maps:get(remove_lines, Opts, false),
195+
arch => maps:get(arch, Opts, undefined)
191196
}
192197
),
193198
0.
@@ -346,5 +351,9 @@ parse_args(["-f", Format | T], {Opts, Args}) ->
346351
parse_args(["--format", Format | T], {Opts, Args});
347352
parse_args(["--format", Format | T], {Opts, Args}) ->
348353
parse_args(T, {Opts#{format => Format}, Args});
354+
parse_args(["--arch", Arch | T], {Opts, Args}) ->
355+
parse_args(T, {Opts#{arch => list_to_atom(Arch)}, Args});
356+
parse_args(["--platform", Platform | T], {Opts, Args}) ->
357+
parse_args(T, {Opts#{platform => list_to_atom(Platform)}, Args});
349358
parse_args([H | T], {Opts, Args}) ->
350359
parse_args(T, {Opts, [H | Args]}).

src/packbeam_api.erl

Lines changed: 114 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,21 @@ 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_arch_modules(Files0, Arch),
136+
Files2 = filter_platform_modules(Files1, Platform),
137+
Files3 =
138+
if
139+
Prune -> prune(Files2, ApplicationModule);
140+
true -> Files2
141+
end,
128142
write_packbeam(
129143
OutputPath,
130-
case Prune of
131-
true -> prune(ParsedFiles, ApplicationModule);
132-
_ -> ParsedFiles
133-
end
144+
Files3
134145
).
135146

136147
%%-----------------------------------------------------------------------------
@@ -782,9 +793,14 @@ ends_with(String, Suffix) ->
782793

783794
%% @private
784795
filter_chunks(Chunks, IncludeLines) ->
785-
AllowedChunks = allowed_chunks(IncludeLines),
796+
AllowedChunks0 = allowed_chunks(IncludeLines),
797+
AllowedChunks1 =
798+
case lists:keymember("avmN", 1, Chunks) of
799+
true -> AllowedChunks0 -- ["Code", "Type"];
800+
false -> AllowedChunks0 -- ["avmN"]
801+
end,
786802
lists:filter(
787-
fun({Tag, _Data}) -> lists:member(Tag, AllowedChunks) end,
803+
fun({Tag, _Data}) -> lists:member(Tag, AllowedChunks1) end,
788804
Chunks
789805
).
790806

@@ -853,3 +869,90 @@ deduplicate([]) ->
853869
[];
854870
deduplicate([H | T]) ->
855871
[H | [X || X <- deduplicate(T), X /= H]].
872+
873+
% Some modules may be for several platforms, so keep list of modules we can
874+
% filter out.
875+
-define(PLATFORM_MODULES_FILTER, #{
876+
generic_unix => [
877+
"pico.beam",
878+
"i2c.beam",
879+
"gpio.beam",
880+
"esp_adc.beam",
881+
"esp.beam",
882+
"emscripten.beam",
883+
"jit_stream_flash.beam"
884+
],
885+
esp32 => [
886+
"pico.beam",
887+
"emscripten.beam",
888+
"jit_stream_mmap.beam"
889+
],
890+
stm32 => [
891+
"pico.beam",
892+
"esp_adc.beam",
893+
"esp.beam",
894+
"emscripten.beam",
895+
"jit_stream_mmap.beam"
896+
],
897+
rp2 => [
898+
"esp_adc.beam",
899+
"esp.beam",
900+
"emscripten.beam",
901+
"jit_stream_mmap.beam"
902+
],
903+
emscripten => [
904+
"pico.beam",
905+
"i2c.beam",
906+
"gpio.beam",
907+
"esp_adc.beam",
908+
"esp.beam",
909+
"emscripten.beam",
910+
"jit_stream_flash.beam",
911+
"jit_stream_mmap.beam"
912+
]
913+
}).
914+
915+
filter_platform_modules(Files, Platform) when is_map_key(Platform, ?PLATFORM_MODULES_FILTER) ->
916+
lists:subtract(Files, map_get(Platform, ?PLATFORM_MODULES_FILTER));
917+
filter_platform_modules(Files, _) ->
918+
Files.
919+
920+
-define(ARCH_MODULES_FILTER, #{
921+
aarch64 => [
922+
"jit_armv6m.beam",
923+
"jit_armv6m_asm.beam",
924+
"jit_riscv32.beam",
925+
"jit_riscv32_asm.beam",
926+
"jit_x86_64.beam",
927+
"jit_x86_64_asm.beam"
928+
],
929+
armv6m => [
930+
"jit_aarch64.beam",
931+
"jit_aarch64_asm.beam",
932+
"jit_riscv32.beam",
933+
"jit_riscv32_asm.beam",
934+
"jit_x86_64.beam",
935+
"jit_x86_64_asm.beam"
936+
],
937+
riscv32 => [
938+
"jit_aarch64.beam",
939+
"jit_aarch64_asm.beam",
940+
"jit_armv6m.beam",
941+
"jit_armv6m_asm.beam",
942+
"jit_x86_64.beam",
943+
"jit_x86_64_asm.beam"
944+
],
945+
x86_64 => [
946+
"jit_aarch64.beam",
947+
"jit_aarch64_asm.beam",
948+
"jit_armv6m.beam",
949+
"jit_armv6m_asm.beam",
950+
"jit_riscv32.beam",
951+
"jit_riscv32_asm.beam"
952+
]
953+
}).
954+
955+
filter_arch_modules(Files, Platform) when is_map_key(Platform, ?ARCH_MODULES_FILTER) ->
956+
lists:subtract(Files, map_get(Platform, ?ARCH_MODULES_FILTER));
957+
filter_arch_modules(Files, _) ->
958+
Files.

0 commit comments

Comments
 (0)