Skip to content

Commit 6e3bbe0

Browse files
committed
compiler-wrapper: set -frandom-seed= in GCC
To make builds deterministic with GCC, use `-frandom-seed` with the value set to the first positional argument from the command line (typically the source file, or the output file name) Signed-off-by: Harmen Stoppels <me@harmenstoppels.nl>
1 parent 520d89c commit 6e3bbe0

2 files changed

Lines changed: 25 additions & 12 deletions

File tree

repos/spack_repo/builtin/packages/compiler_wrapper/cc.sh

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -365,20 +365,21 @@ if [ -z "$mode" ] || [ "$mode" = ld ]; then
365365
done
366366
fi
367367

368-
# Finish setting up the mode.
368+
# Finish setting up the mode, and find the first positional arguments as a value
369+
# for -frandom-seed (to make GCC deterministic)
370+
_rseed=""
371+
eval _has_frandom_seed=\${SPACK_${comp}_HAS_FRANDOM_SEED:-}
369372
if [ -z "$mode" ]; then
370373
mode=ccld
371374
for arg in "$@"; do
372-
if [ "$arg" = "-E" ]; then
373-
mode=cpp
374-
break
375-
elif [ "$arg" = "-S" ]; then
376-
mode=as
377-
break
378-
elif [ "$arg" = "-c" ]; then
379-
mode=cc
380-
break
381-
fi
375+
case $arg in
376+
-E) mode=cpp ;;
377+
-S) mode=as ;;
378+
-c) mode=cc ;;
379+
-frandom-seed=*) [ -n "$_has_frandom_seed" ] && _rseed=_set ;;
380+
-*|*.h*) ;;
381+
*) if [ -n "$_has_frandom_seed" ] && [ -z "$_rseed" ]; then _rseed=$arg; fi ;;
382+
esac
382383
done
383384
fi
384385

@@ -951,6 +952,14 @@ extend args_list libs_list "-l"
951952
full_command_list="$command"
952953
extend full_command_list args_list
953954

955+
if [ "$mode" = cc ] && [ -n "$_has_frandom_seed" ]; then
956+
case "$_rseed" in
957+
_set|'') ;;
958+
*) append full_command_list "-frandom-seed=$_rseed" ;;
959+
esac
960+
fi
961+
unset _rseed _has_frandom_seed
962+
954963
# prepend the ccache binary if we're using ccache
955964
if [ -n "$SPACK_CCACHE_BINARY" ]; then
956965
case "$lang_flags" in

repos/spack_repo/builtin/packages/compiler_wrapper/package.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class CompilerWrapper(Package):
4040
if sys.platform != "win32":
4141
version(
4242
"1.0",
43-
sha256="c7b816479554fd32f677db15ceec6627b91c86074a5d65498688afcbe2796188",
43+
sha256="21afdc01e2d8d1f4ea3438ab81b2d3bc16b235c01fbbb9357ac71390ec563b67",
4444
expand=False,
4545
)
4646
else:
@@ -175,6 +175,10 @@ def setup_dependent_build_environment(
175175
compiler = getattr(compiler_pkg, attr_name)
176176
env.set(spack_var_name, compiler)
177177

178+
# -frandom-seed= is needed for deterministic builds with GCC
179+
if compiler_pkg.name == "gcc":
180+
env.set(f"SPACK_{wrapper_var_name}_HAS_FRANDOM_SEED", "1")
181+
178182
if language not in compiler_pkg.compiler_wrapper_link_paths:
179183
continue
180184

0 commit comments

Comments
 (0)