11load ("//:erlang_app_info.bzl" , "ErlangAppInfo" , "flat_deps" )
2- load (":erlang_bytecode.bzl" , "unique_dirnames" )
2+ load ("//:util.bzl" , "path_join" )
3+ load (":util.bzl" , "erl_libs_contents2" )
34load (
45 "//tools:erlang_toolchain.bzl" ,
56 "erlang_dirs" ,
67 "maybe_install_erlang" ,
78)
89
9- DEFAULT_PLT_APPS = ["erts" , "kernel" , "stdlib" ]
10-
1110def _impl (ctx ):
1211 logfile = ctx .actions .declare_file (ctx .outputs .plt .basename + ".log" )
1312 home_dir = ctx .actions .declare_directory (ctx .label .name + "_home" )
1413
14+ erl_libs_dir = ctx .label .name + "_deps"
15+
16+ erl_libs_files = erl_libs_contents2 (
17+ ctx ,
18+ deps = ctx .attr .libs ,
19+ dir = erl_libs_dir ,
20+ )
21+
22+ erl_libs_path = ""
23+ if len (erl_libs_files ) > 0 :
24+ erl_libs_path = path_join (
25+ ctx .bin_dir .path ,
26+ ctx .label .workspace_root ,
27+ ctx .label .package ,
28+ erl_libs_dir ,
29+ )
30+
31+ target_files_dir = ctx .label .name + "_files"
32+
33+ deps = []
34+ if ctx .attr .for_target != None :
35+ deps .extend (flat_deps (ctx .attr .for_target [ErlangAppInfo ].deps + ctx .attr .deps ))
36+ else :
37+ deps .extend (flat_deps (ctx .attr .deps ))
38+
39+ target_files = erl_libs_contents2 (
40+ ctx ,
41+ deps = deps ,
42+ dir = target_files_dir ,
43+ )
44+
45+ target_files_path = ""
46+ if len (target_files ) > 0 :
47+ target_files_path = path_join (
48+ ctx .bin_dir .path ,
49+ ctx .label .workspace_root ,
50+ ctx .label .package ,
51+ target_files_dir ,
52+ )
53+
54+ args = ctx .actions .args ()
55+
1556 if ctx .file .plt == None :
16- source_plt_arg = "--build_plt"
57+ args . add ( "--build_plt" )
1758 else :
18- source_plt_arg = "--plt " + ctx .file .plt .path + " --no_check_plt --add_to_plt"
59+ args .add ("--plt" )
60+ args .add (ctx .file .plt )
61+ args .add ("--no_check_plt" )
62+ args .add ("--add_to_plt" )
1963
2064 apps = []
65+ apps .extend (ctx .attr .apps )
2166 if ctx .attr .for_target != None :
2267 apps .extend (ctx .attr .for_target [ErlangAppInfo ].extra_apps )
23- if ctx .attr .for_target == None or ctx .attr .apps != DEFAULT_PLT_APPS :
24- apps .extend (ctx .attr .apps )
25- apps_args = ""
2668 if len (apps ) > 0 :
27- apps_args = "--apps " + " " .join (apps )
69+ args .add ("--apps" )
70+ args .add_all (apps )
2871
29- deps = []
30- if ctx .attr .for_target != None :
31- deps .extend (flat_deps (ctx .attr .for_target [ErlangAppInfo ].deps + ctx .attr .deps ))
32- else :
33- deps .extend (flat_deps (ctx .attr .deps ))
72+ if target_files_path != "" :
73+ args .add ("-r" )
74+ args .add (target_files_path )
3475
35- files = []
36- for dep in deps :
37- lib_info = dep [ErlangAppInfo ]
38- files .extend (lib_info .beam )
76+ args .add_all (ctx .attr .extra_args )
3977
40- dirs = unique_dirnames (files )
41- dirs_args = "" .join ([
42- "\n {} \\ " .format (d )
43- for d in dirs
44- ])
78+ args .add ("--output_plt" )
79+ args .add (ctx .outputs .plt )
4580
4681 (erlang_home , _ , runfiles ) = erlang_dirs (ctx )
4782
@@ -52,38 +87,38 @@ def _impl(ctx):
5287# without HOME being set, dialyzer will error regarding a default plt
5388export HOME={home}
5489
90+ if [ -n "{erl_libs_path}" ]; then
91+ export ERL_LIBS={erl_libs_path}
92+ fi
93+
5594set +e
5695set -x
57- "{erlang_home}"/bin/dialyzer {apps_args} \\
58- {source_plt_arg} \\ {dirs_args}
59- --output_plt {output} > {logfile}
96+ "{erlang_home}"/bin/dialyzer $@ > {logfile}
6097R=$?
6198set +x
6299set -e
63100if [ ! $R -eq 0 ]; then
64- cat {logfile}
101+ head {logfile}
65102fi
66103exit $R
67104""" .format (
68105 maybe_install_erlang = maybe_install_erlang (ctx ),
69106 erlang_home = erlang_home ,
70107 home = home_dir .path ,
71- apps_args = apps_args ,
72- source_plt_arg = source_plt_arg ,
73- dirs_args = dirs_args ,
74- output = ctx .outputs .plt .path ,
108+ erl_libs_path = erl_libs_path ,
75109 logfile = logfile .path ,
76110 )
77111
78112 inputs = depset (
79- direct = ctx .files .plt + files ,
113+ direct = ctx .files .plt + erl_libs_files + target_files ,
80114 transitive = [runfiles .files ],
81115 )
82116
83117 ctx .actions .run_shell (
84118 inputs = inputs ,
85119 outputs = [ctx .outputs .plt , logfile , home_dir ],
86120 command = script ,
121+ arguments = [args ],
87122 mnemonic = "DIALYZER" ,
88123 )
89124
@@ -93,8 +128,10 @@ plt = rule(
93128 "plt" : attr .label (
94129 allow_single_file = [".plt" ],
95130 ),
96- "apps" : attr .string_list (
97- default = DEFAULT_PLT_APPS ,
131+ "apps" : attr .string_list (),
132+ "extra_args" : attr .string_list (),
133+ "libs" : attr .label_list (
134+ providers = [ErlangAppInfo ],
98135 ),
99136 "deps" : attr .label_list (
100137 providers = [ErlangAppInfo ],
0 commit comments