Skip to content

Commit

Permalink
add possibility to preserve source when using deterministic builds
Browse files Browse the repository at this point in the history
configure: add --enable/disable-deterministic-keep-source build option
compiler: add +deterministic_keep_source option
compiler: make some minor additions for +deterministic option description
  • Loading branch information
delitrem committed Jun 23, 2024
1 parent ac7435b commit daf7c22
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 5 deletions.
4 changes: 4 additions & 0 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ while test $# != 0; do
config_arguments="$config_arguments --enable-deterministic-build";;
--disable-deterministic-build)
config_arguments="$config_arguments --disable-deterministic-build";;
--enable-deterministic-keep-source)
config_arguments="$config_arguments --enable-deterministic-keep-source";;
--disable-deterministic-keep-source)
config_arguments="$config_arguments --disable-deterministic-keep-source";;
CFLAGS=* | LDFLAGS=*)
flgs_var=`echo "$1" | sed 's/=.*$//'`
flgs_val=`echo "$1" | sed 's/^[^=]*=//'`
Expand Down
4 changes: 4 additions & 0 deletions configure.src
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ while test $# != 0; do
config_arguments="$config_arguments --enable-deterministic-build";;
--disable-deterministic-build)
config_arguments="$config_arguments --disable-deterministic-build";;
--enable-deterministic-keep-source)
config_arguments="$config_arguments --enable-deterministic-keep-source";;
--disable-deterministic-keep-source)
config_arguments="$config_arguments --disable-deterministic-keep-source";;
CFLAGS=* | LDFLAGS=*)
flgs_var=`echo "$1" | sed 's/=.*$//'`
flgs_val=`echo "$1" | sed 's/^[^=]*=//'`
Expand Down
20 changes: 20 additions & 0 deletions erts/configure
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,7 @@ enable_year2038=yes
ac_subst_vars='LTLIBOBJS
LIBOBJS
DEBUG_CFLAGS
ERL_DETERMINISTIC_KEEP_SOURCE
ERL_DETERMINISTIC
CFLAGS32
CC32
Expand Down Expand Up @@ -863,6 +864,7 @@ enable_prefer_elapsed_monotonic_time_during_suspend
enable_gettimeofday_as_os_system_time
with_javac
enable_deterministic_build
enable_deterministic_keep_source
enable_year2038
'
ac_precious_vars='build_alias
Expand Down Expand Up @@ -1621,6 +1623,9 @@ Optional Features:
--enable-deterministic-build
enable build determinism, stripping absolute paths
from build output
--enable-deterministic-keep-source
enable keeping source when building deterministic
builds
--disable-year2038 don't support timestamps after 2038

Optional Packages:
Expand Down Expand Up @@ -26882,6 +26887,21 @@ fi




# Check whether --enable-deterministic-keep-source was given.
if test ${enable_deterministic_keep_source+y}
then :
enableval=$enable_deterministic_keep_source; case "$enableval" in
no) ERL_DETERMINISTIC_KEEP_SOURCE=no ;;
*) ERL_DETERMINISTIC_KEEP_SOURCE=yes ;;
esac
else case e in #(
e) ERL_DETERMINISTIC_KEEP_SOURCE=no ;;
esac
fi



{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking CFLAGS for -O switch" >&5
printf %s "checking CFLAGS for -O switch... " >&6; }
no_opt_CFLAGS=$(echo " $CFLAGS" | sed 's/ -O[^ ]*/ /g')
Expand Down
13 changes: 13 additions & 0 deletions erts/configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -3646,6 +3646,19 @@ AS_HELP_STRING([--enable-deterministic-build], [enable build determinism, stripp
ERL_DETERMINISTIC=no)
AC_SUBST(ERL_DETERMINISTIC)

dnl ----------------------------------------------------------------------
dnl Enable keep sources flag for using with determinism flag
dnl ----------------------------------------------------------------------

AC_ARG_ENABLE(deterministic-keep-source,
AS_HELP_STRING([--enable-deterministic-keep-source], [enable keeping source when building deterministic builds]),
[ case "$enableval" in
no) ERL_DETERMINISTIC_KEEP_SOURCE=no ;;
*) ERL_DETERMINISTIC_KEEP_SOURCE=yes ;;
esac ],
ERL_DETERMINISTIC_KEEP_SOURCE=no)
AC_SUBST(ERL_DETERMINISTIC_KEEP_SOURCE)

AC_MSG_CHECKING([CFLAGS for -O switch])
dnl Remove all "-O*" options
no_opt_CFLAGS=$(echo " $CFLAGS" | sed 's/ -O[[^ ]]*/ /g')
Expand Down
13 changes: 10 additions & 3 deletions lib/compiler/src/compile.erl
Original file line number Diff line number Diff line change
Expand Up @@ -333,9 +333,12 @@ Available options:
For details, see [beam_lib(3)](`m:beam_lib#debug_info`).
- **`deterministic`** - Omit the `options` and `source` tuples in the list
returned by `Module:module_info(compile)`, and reduce the paths in stack
traces to the module name alone. This option will make it easier to achieve
reproducible builds.
returned by `Module:module_info(compile)`, reduce the paths in stack traces
to the module name alone, and make embedded documentation ordered. This
option will make it easier to achieve reproducible builds.
- **`deterministic_keep_source`** - When used with `deterministic`, preserves
the `source` tuple in the list returned by `Module:module_info(compile)`.
- **`{feature, Feature, enable | disable}`** - [](){: #feature-option } Enable
(disable) the [feature](`e:system:features.md#features`) `Feature` during
Expand Down Expand Up @@ -2583,11 +2586,15 @@ beam_strip_types(Beam0, #compile{}=St) ->
compile_info(File, CompilerOpts, Opts) ->
IsSlim = member(slim, CompilerOpts),
IsDeterministic = member(deterministic, CompilerOpts),
IsDeterministicKeepSource =
member(deterministic_keep_source, CompilerOpts),
Info0 = proplists:get_value(compile_info, Opts, []),
Info1 =
case paranoid_absname(File) of
[_|_] = Source when not IsSlim, not IsDeterministic ->
[{source,Source} | Info0];
[_|_] = Source when IsDeterministicKeepSource ->
[{source,Source} | Info0];
_ ->
Info0
end,
Expand Down
37 changes: 35 additions & 2 deletions lib/compiler/test/compile_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
beam_ssa_pp_smoke_test/1,
warnings/1, pre_load_check/1, env_compiler_options/1,
bc_options/1, deterministic_include/1, deterministic_paths/1,
deterministic_docs/1,
deterministic_docs/1, deterministic_keep_source/1,
compile_attribute/1, message_printing/1, other_options/1,
transforms/1, erl_compile_api/1, types_pp/1, bs_init_writable/1,
annotations_pp/1, option_order/1
Expand All @@ -61,7 +61,7 @@ all() ->
warnings, pre_load_check,
env_compiler_options, custom_debug_info, bc_options,
custom_compile_info, deterministic_include, deterministic_paths,
deterministic_docs,
deterministic_docs, deterministic_keep_source,
compile_attribute, message_printing, other_options, transforms,
erl_compile_api, types_pp, bs_init_writable, annotations_pp,
option_order].
Expand Down Expand Up @@ -1779,6 +1779,15 @@ deterministic_include(Config) when is_list(Config) ->
{ok,_,DetD} = compile:file(Simple, [binary, deterministic, {i,"gaffel"}]),
true = DetC =:= DetD,

%% ... and files with +deterministic_keep_source shouldn't.
{ok,_,DetC} = compile:file(
Simple, [binary, deterministic,
deterministic_keep_source, {i,"gurka"}]),
{ok,_,DetD} = compile:file(
Simple, [binary, deterministic,
deterministic_keep_source, {i,"gaffel"}]),
true = DetC =:= DetD,

ok.

deterministic_paths(Config) when is_list(Config) ->
Expand All @@ -1791,6 +1800,10 @@ deterministic_paths(Config) when is_list(Config) ->
%% ... but files with +deterministic shouldn't.
false = deterministic_paths_1(DataDir, "simple", [deterministic]),

%% ... but files with +deterministic_keep_source should.
true = deterministic_paths_1(DataDir, "simple",
[deterministic, deterministic_keep_source]),

ok.

deterministic_paths_1(DataDir, Name, Opts) ->
Expand Down Expand Up @@ -1828,6 +1841,26 @@ deterministic_docs_1(Filepath, Opts, Checks) ->
Testing =:= Reference
end, lists:seq(1, Checks)).

deterministic_keep_source(Config) when is_list(Config) ->
DataDir = proplists:get_value(data_dir, Config),
Simple = filename:join(DataDir, "simple"),
ModuleInfo1 = deterministic_keep_source_1(Simple, []),
true = lists:keymember(source, 1, ModuleInfo1),
ModuleInfo2 = deterministic_keep_source_1(Simple, [deterministic]),
false = lists:keymember(source, 1, ModuleInfo2),
ModuleInfo3 = deterministic_keep_source_1(
Simple, [deterministic, deterministic_keep_source]),
true = lists:keymember(source, 1, ModuleInfo3),
ok.

deterministic_keep_source_1(Simple, Opts) ->
{ok, simple} = compile:file(Simple, Opts),
{module, simple} = c:l(simple),
ModuleInfo = simple:module_info(compile),
true = code:delete(simple),
false = code:purge(simple),
ModuleInfo.

%% ERL-1058: -compile(debug_info) had no effect
compile_attribute(Config) when is_list(Config) ->
DataDir = proplists:get_value(data_dir, Config),
Expand Down
4 changes: 4 additions & 0 deletions make/otp.mk.in
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ BITS64 = @BITS64@
OTP_RELEASE = @OTP_RELEASE@

ERL_DETERMINISTIC = @ERL_DETERMINISTIC@
ERL_DETERMINISTIC_KEEP_SOURCE = @ERL_DETERMINISTIC_KEEP_SOURCE@

# ----------------------------------------------------
# Erlang language section
Expand All @@ -110,6 +111,9 @@ ifeq ($(ERL_DETERMINISTIC),yes)
YRL_FLAGS += +deterministic
XRL_FLAGS += +deterministic
endif
ifeq ($(ERL_DETERMINISTIC_KEEP_SOURCE),yes)
ERL_COMPILE_FLAGS += +deterministic_keep_source
endif

ERLC_WFLAGS = -W
ERLC = erlc $(ERLC_WFLAGS) $(ERLC_FLAGS)
Expand Down

0 comments on commit daf7c22

Please sign in to comment.