Skip to content

Commit

Permalink
compiler: Forward +source flag to epp and fix bug in +deterministic
Browse files Browse the repository at this point in the history
The source file path as given to `erlc` was included in an implicit
file attribute inserted by epp, even when the +source flag was
set to something else which was a bit surprising. It was also
included when +deterministic was specified, breaking the flag's
promise.

This commit forwards the +source flag to epp so it inserts the
right information, and if +deterministic is given it will be shaved
to just the base name of the file, guaranteeing the same result
regardless of how the input is reached.
  • Loading branch information
jhogberg committed Oct 5, 2018
1 parent 16b93b9 commit 3a34f37
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
7 changes: 4 additions & 3 deletions lib/compiler/doc/src/compile.xml
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@
<tag><c>deterministic</c></tag>
<item>
<p>Omit the <c>options</c> and <c>source</c> tuples in
the list returned by <c>Module:module_info(compile)</c>.
the list returned by <c>Module:module_info(compile)</c>, and
reduce the paths in stack traces to the module name alone.
This option will make it easier to achieve reproducible builds.
</p>
</item>
Expand Down Expand Up @@ -347,8 +348,8 @@ module.beam: module.erl \

<tag><c>{source,FileName}</c></tag>
<item>
<p>Sets the value of the source, as returned by
<c>module_info(compile)</c>.</p>
<p>Overrides the source file name as presented in
<c>module_info(compile)</c> and stack traces.</p>
</item>

<tag><c>{outdir,Dir}</c></tag>
Expand Down
14 changes: 10 additions & 4 deletions lib/compiler/src/compile.erl
Original file line number Diff line number Diff line change
Expand Up @@ -931,11 +931,17 @@ parse_module(_Code, St0) ->
end.

do_parse_module(DefEncoding, #compile{ifile=File,options=Opts,dir=Dir}=St) ->
SourceName0 = proplists:get_value(source, Opts, File),
SourceName = case member(deterministic, Opts) of
true -> filename:basename(SourceName0);
false -> SourceName0
end,
R = epp:parse_file(File,
[{includes,[".",Dir|inc_paths(Opts)]},
{macros,pre_defs(Opts)},
{default_encoding,DefEncoding},
extra]),
[{includes,[".",Dir|inc_paths(Opts)]},
{source_name, SourceName},
{macros,pre_defs(Opts)},
{default_encoding,DefEncoding},
extra]),
case R of
{ok,Forms,Extra} ->
Encoding = proplists:get_value(encoding, Extra),
Expand Down
28 changes: 26 additions & 2 deletions lib/compiler/test/compile_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
core_roundtrip/1, asm/1, optimized_guards/1,
sys_pre_attributes/1, dialyzer/1,
warnings/1, pre_load_check/1, env_compiler_options/1,
bc_options/1, deterministic_include/1
bc_options/1, deterministic_include/1, deterministic_paths/1
]).

suite() -> [{ct_hooks,[ts_install_cth]}].
Expand All @@ -53,7 +53,7 @@ all() ->
cover, env, core_pp, core_roundtrip, asm, optimized_guards,
sys_pre_attributes, dialyzer, warnings, pre_load_check,
env_compiler_options, custom_debug_info, bc_options,
custom_compile_info, deterministic_include].
custom_compile_info, deterministic_include, deterministic_paths].

groups() ->
[].
Expand Down Expand Up @@ -1531,6 +1531,30 @@ deterministic_include(Config) when is_list(Config) ->

ok.

deterministic_paths(Config) when is_list(Config) ->
DataDir = proplists:get_value(data_dir, Config),

%% Files without +deterministic should differ if they were compiled from a
%% different directory.
true = deterministic_paths_1(DataDir, "simple", []),

%% ... but files with +deterministic shouldn't.
false = deterministic_paths_1(DataDir, "simple", [deterministic]),

ok.

deterministic_paths_1(DataDir, Name, Opts) ->
Simple = filename:join(DataDir, "simple"),
{ok, Cwd} = file:get_cwd(),
try
{ok,_,A} = compile:file(Simple, [binary | Opts]),
ok = file:set_cwd(DataDir),
{ok,_,B} = compile:file(Name, [binary | Opts]),
A =/= B
after
file:set_cwd(Cwd)
end.

%%%
%%% Utilities.
%%%
Expand Down

0 comments on commit 3a34f37

Please sign in to comment.