Skip to content

Commit 2b3dc10

Browse files
Introduce reproducible builds and always normalize temporary names [GH #426]
This commit also: - updates the copyright notice in the version command to credit contributors - and removes the Edison Design Group-specific DEMO_VERSION_ID configuration option.
1 parent bc7c97b commit 2b3dc10

54 files changed

Lines changed: 294 additions & 235 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎doc/source/build.rst‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,15 @@ and defaults that many configuration macros have). The
607607
``--dump_configuration`` command-line option provides a convenient display of
608608
all configuration macros and their settings.
609609

610+
By default, the ``--version`` and ``--dump_configuration`` output (and the
611+
string that identifies precompiled header files) includes the date and time the
612+
front end was built, so no two builds produce identical executables. Defining
613+
the configuration macro ``BUILD_IDENTIFIER`` as a string literal (with CMake,
614+
``-DEDG_BUILD_IDENTIFIER=``\ *string*) replaces the date and time with that
615+
string, making the build reproducible. Builds of different sources should use
616+
different identifiers so their precompiled headers aren't mistaken for each
617+
other.
618+
610619
A non-legacy target configuration is most easily generated as a derivative from
611620
an existing legacy target configuration using the ``--dump_legacy_as_target``
612621
command-line option. This option takes a target configuration name and

‎lib_src/CMakeLists.txt‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ endif()
1515
# set the project name
1616
project(lib_src NONE)
1717

18+
# Create deterministic archives (with zeroed timestamps, owners, and modes) on
19+
# Linux, where ar and ranlib are expected to be GNU binutils or compatible,
20+
# so that rebuilding the same sources produces the same libraries.
21+
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
22+
set(CMAKE_CXX_ARCHIVE_CREATE
23+
"<CMAKE_AR> qcD <TARGET> <LINK_FLAGS> <OBJECTS>")
24+
set(CMAKE_CXX_ARCHIVE_APPEND "<CMAKE_AR> qD <TARGET> <LINK_FLAGS> <OBJECTS>")
25+
set(CMAKE_CXX_ARCHIVE_FINISH "<CMAKE_RANLIB> -D <TARGET>")
26+
endif()
27+
1828
# Add the default value of EDG_RUNTIME_LIB to the cache.
1929
if(NOT DEFINED CACHE{EDG_RUNTIME_LIB})
2030
# Use the same environment variables ECCP will be using.

‎lib_src/cmake/lib_build_support.cmake‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,16 @@ function(add_edg_cpp_rt_obj_file src_file_name obj_file_name)
6060
list(APPEND obj_file_deps ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/cpfe)
6161
endif()
6262

63+
# Keep the generated C file in the object's directory (which is unique to
64+
# the target platform) rather than in a randomly named temporary directory,
65+
# so that the debug information, and thus the library, is reproducible.
66+
get_filename_component(obj_dir "${obj_file_name}" DIRECTORY)
67+
6368
add_custom_command(
6469
OUTPUT ${obj_file_name}
6570
COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/eccp
6671
# Set the base options
67-
--building_runtime --c++23 -g
72+
--building_runtime --c++23 -g --keep_gen_c
6873
# Set the compile definitons
6974
-DCHECKING=1 -DDEBUG=1
7075
${cmakedef_define_flags}
@@ -84,6 +89,7 @@ function(add_edg_cpp_rt_obj_file src_file_name obj_file_name)
8489
-o ${obj_file_name}
8590
-c ${src_file_name}
8691
DEPENDS ${obj_file_deps}
92+
WORKING_DIRECTORY ${obj_dir}
8793
)
8894
endfunction()
8995

‎src/CMakeLists.txt‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,23 @@ if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
146146
endif()
147147
endif()
148148

149+
# Build identifier
150+
151+
# Replace the build date and time shown by --version (and used to identify
152+
# precompiled headers) with a fixed string, so that rebuilding the same
153+
# sources produces the same executables.
154+
set(EDG_BUILD_IDENTIFIER "" CACHE STRING
155+
"Build identifier to use in place of the build date and time")
156+
if(NOT "$CACHE{EDG_BUILD_IDENTIFIER}" STREQUAL "")
157+
add_compile_definitions(
158+
"BUILD_IDENTIFIER=\"$CACHE{EDG_BUILD_IDENTIFIER}\"")
159+
# GCC otherwise seeds some generated names and the gcov stamps in objects
160+
# and .gcno files with the time of compilation.
161+
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
162+
add_compile_options(-frandom-seed=$CACHE{EDG_BUILD_IDENTIFIER})
163+
endif()
164+
endif()
165+
149166
# Enable colored output.
150167

151168
if (${FORCE_COLORED_OUTPUT})

‎src/cmd_line.c‎

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6472,8 +6472,8 @@ file.
64726472
/* Print a banner. */
64736473
fprintf(f_error,
64746474
"/* Configuration data for Edison Design Group C/C++ Front End */\n"
6475-
"/* version %s, built on %s at %s. */\n\n",
6476-
VERSION_NUMBER, build_date, build_time);
6475+
"/* version %s (%s). */\n\n",
6476+
VERSION_NUMBER, build_stamp);
64776477
/* Print the values of all configuration macros. */
64786478
#if defined(ABI_CHANGES_FOR_ARRAY_NEW_AND_DELETE)
64796479
define_numeric_valued_macro(ABI_CHANGES_FOR_ARRAY_NEW_AND_DELETE);
@@ -6718,6 +6718,11 @@ file.
67186718
#else /* !defined(BOOL_ENABLING_POSSIBLE) */
67196719
comment_undefined_macro_name(BOOL_ENABLING_POSSIBLE);
67206720
#endif /* defined(BOOL_ENABLING_POSSIBLE) */
6721+
#if defined(BUILD_IDENTIFIER)
6722+
define_string_valued_macro(BUILD_IDENTIFIER);
6723+
#else /* !defined(BUILD_IDENTIFIER) */
6724+
comment_undefined_macro_name(BUILD_IDENTIFIER);
6725+
#endif /* defined(BUILD_IDENTIFIER) */
67216726
#if defined(BUILTIN_FUNCTIONS_ENABLED)
67226727
define_string_valued_macro(BUILTIN_FUNCTIONS_ENABLED);
67236728
#else /* !defined(BUILTIN_FUNCTIONS_ENABLED) */
@@ -7819,11 +7824,6 @@ file.
78197824
#else /* !defined(DELETE_CAN_BE_FOLDED_INTO_DTOR) */
78207825
comment_undefined_macro_name(DELETE_CAN_BE_FOLDED_INTO_DTOR);
78217826
#endif /* defined(DELETE_CAN_BE_FOLDED_INTO_DTOR) */
7822-
#if defined(DEMO_VERSION_ID)
7823-
define_string_valued_macro(DEMO_VERSION_ID);
7824-
#else /* !defined(DEMO_VERSION_ID) */
7825-
comment_undefined_macro_name(DEMO_VERSION_ID);
7826-
#endif /* defined(DEMO_VERSION_ID) */
78277827
#if defined(DIRECTORY_SEPARATOR)
78287828
define_string_valued_macro(DIRECTORY_SEPARATOR);
78297829
#else /* !defined(DIRECTORY_SEPARATOR) */
@@ -10788,12 +10788,10 @@ Process the arguments on the command line that invoked the compiler.
1078810788
/* Print out compiler version. */
1078910789
check_assertion(opt_value == TRUE);
1079010790
fprintf(f_error,
10791-
"Edison Design Group C/C++ Front End, version %s (%s %s)\n",
10792-
VERSION_NUMBER, build_date, build_time);
10793-
fprintf(f_error, "Copyright 1988-2026 Edison Design Group, Inc.\n");
10794-
#ifdef DEMO_VERSION_ID
10795-
fprintf(f_error, "Demonstration version for %s\n", DEMO_VERSION_ID);
10796-
#endif /* ifdef DEMO_VERSION_ID */
10791+
"Edison Design Group C/C++ Front End, version %s (%s)\n",
10792+
VERSION_NUMBER, build_stamp);
10793+
fprintf(f_error, "Copyright 1988-2026 Edison Design Group, Inc. "
10794+
"and Contributors\n");
1079710795
fputc('\n', f_error);
1079810796
source_file_name_optional = TRUE;
1079910797
break;

‎src/host_envir.h‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2551,12 +2551,6 @@ If only one CPU id is needed, HOSTID should be set, and HOSTID2 should be
25512551
left undefined.
25522552
*/
25532553

2554-
/*
2555-
The flag DEMO_VERSION_ID can be defined with a string identifying a demo
2556-
version if this is a demo version. The string is printed on startup with
2557-
the -v option.
2558-
*/
2559-
25602554
/*
25612555
Flag that provides the default value for null_chars_allowed_in_source,
25622556
which controls whether null (zero) characters are allowed in source lines.

‎src/il_to_str.c‎

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8563,15 +8563,40 @@ is properly maintained. Do the output as indicated by octl.
85638563
#endif /* BACK_END_IS_C_GEN_BE */
85648564

85658565
#if BACK_END_IS_C_GEN_BE || BACK_END_IS_CP_GEN_BE
8566-
#if DEBUG
85678566

85688567
STATIC_THREAD uintptr_t
85698568
unique_id_counter;
85708569
/* A ticket counter used by
8571-
generate_unique_id_for_il_pointer to generate stable
8570+
generate_unique_id_for_temporary to generate stable
85728571
unique identifiers for a given pointer. */
85738572

8574-
using a_ptr_unique_id_map = Ptr_map<void*, uintptr_t, General_allocator>;
8573+
namespace {
8574+
8575+
enum class a_temporary_key : uintptr_t {};
8576+
/* The key type of ptr_to_unique_id: an IL pointer
8577+
converted to an integer. It is a distinct type
8578+
only so that it can have its own hash_ptr. */
8579+
8580+
inline uintptr_t hash_ptr(a_temporary_key key)
8581+
/*
8582+
Return a hash value for the given temporary key that is appropriate for
8583+
Ptr_map. IL entries of a given kind tend to be allocated at regular intervals,
8584+
so the default pointer hash (essentially the address itself) clusters badly in
8585+
the low-order bits that Ptr_map uses for indexing. Mix all the bits of the
8586+
address into the low-order bits (Fibonacci hashing).
8587+
*/
8588+
{
8589+
constexpr unsigned half_width = sizeof(uintptr_t) * 4;
8590+
uintptr_t hash = (uintptr_t)key *
8591+
(uintptr_t)0x9E3779B97F4A7C15ull;
8592+
8593+
return hash ^ (hash >> half_width);
8594+
} /* hash_ptr */
8595+
8596+
} /* namespace */
8597+
8598+
using a_ptr_unique_id_map = Ptr_map<a_temporary_key, uintptr_t,
8599+
General_allocator>;
85758600
/* The type of ptr_to_unique_id used to map an
85768601
arbitrary pointer to a unique identifier from the
85778602
unique_id_counter. */
@@ -8580,48 +8605,40 @@ STATIC_THREAD a_ptr_unique_id_map
85808605
*ptr_to_unique_id;
85818606
/* A map of a given pointer to its unique identifiers
85828607
from the unique_id_counter used by
8583-
generate_unique_id_for_il_pointer. */
8608+
generate_unique_id_for_temporary. */
85848609

85858610

85868611
static uintptr_t generate_unique_id_for_temporary(void *ptr)
85878612
/*
8588-
Return an identifier unique to the current translation unit for the given IL
8589-
pointer.
8613+
Return an identifier unique to the current generated file for the given IL
8614+
pointer. Identifiers are assigned in order of first use, so (unlike the IL
8615+
pointer itself) they do not depend on where the IL happens to be allocated,
8616+
and the generated output is reproducible from run to run.
85908617
*/
85918618
{
85928619
uintptr_t result;
85938620

85948621
if (ptr == NULL) {
85958622
result = 0;
8596-
} else if (ptr_to_unique_id->get(ptr) == 0) {
8597-
result = ++unique_id_counter;
8598-
ptr_to_unique_id->map(ptr, result);
85998623
} else {
8600-
result = ptr_to_unique_id->get(ptr);
8624+
a_temporary_key key = (a_temporary_key)(uintptr_t)ptr;
8625+
8626+
result = ptr_to_unique_id->get(key);
8627+
if (result == 0) {
8628+
result = ++unique_id_counter;
8629+
ptr_to_unique_id->map(key, result);
8630+
} /* if */
86018631
} /* if */
86028632
return result;
86038633
} /* generate_unique_id_for_temporary */
86048634

8605-
#endif /* DEBUG */
86068635

86078636
a_temp_var_name_buffer form_temporary_name_for_back_end(void *ptr)
86088637
/*
86098638
Return a temporary name generated for the given IL pointer.
86108639
*/
86118640
{
8612-
uintptr_t unique_id;
8613-
8614-
#if DEBUG
8615-
if (db_active) {
8616-
unique_id = generate_unique_id_for_temporary(ptr);
8617-
} else
8618-
#endif /* DEBUG */
8619-
/* Do not add code here. */
8620-
{
8621-
unique_id = unique_id_for_il_pointer(ptr);
8622-
} /* if */
8623-
8624-
a_temp_var_name_buffer result("__T", unique_id);
8641+
a_temp_var_name_buffer result("__T", generate_unique_id_for_temporary(ptr));
86258642
return result;
86268643
} /* form_temporary_name_for_back_end */
86278644

@@ -8632,11 +8649,9 @@ Initialize the common logic for a C-like language generating back end. These
86328649
are initializations that must be redone for each generated file.
86338650
*/
86348651
{
8635-
#if DEBUG
86368652
unique_id_counter = 0;
86378653
ptr_to_unique_id = alloc_general_of_type(a_ptr_unique_id_map);
86388654
construct(ptr_to_unique_id, /*mask_width=*/10u);
8639-
#endif /* DEBUG */
86408655
} /* il_to_str_back_end_file_init */
86418656

86428657
#endif /* BACK_END_IS_C_GEN_BE || BACK_END_IS_CP_GEN_BE */

‎src/pch.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ associated with this compiler version.
465465
{
466466
Small_string<PCH_ID_STRING_LENGTH>
467467
temp_pch_str("EDG C/C++ version ",VERSION_NUMBER,
468-
" (", build_date, " ", build_time, ")\n");
468+
" (", build_stamp, ")\n");
469469

470470
check_assertion_str2((size_t_arg(temp_pch_str.length()) <
471471
size_t_arg(PCH_ID_STRING_LENGTH)),
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//allowed_words: CMake DEDG
2+
[GH #426]
3+
Reproducible builds and generated code
4+
5+
The new configuration macro BUILD_IDENTIFIER (with CMake,
6+
-DEDG_BUILD_IDENTIFIER=<string>) is a string literal that replaces the
7+
build date and time in the --version and --dump_configuration output and
8+
in the precompiled header identification string, so rebuilding the same
9+
sources produces identical executables. The runtime library is likewise
10+
reproducible: its archives are deterministic, and its generated C files no
11+
longer pass through randomly named temporary directories.
12+
13+
Temporaries in generated C and C++ are now named from a per-file counter
14+
(e.g., "__T1") rather than from IL addresses, so the output no longer
15+
varies with address space layout randomization.
16+
17+
DEMO_VERSION_ID has been removed, and the --version copyright line now
18+
credits contributors.

‎src/version.h‎

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,26 @@ front end version. This must be a numeric value.
2929
#define VERSION_NUMBER_FOR_MACRO 700
3030

3131
/*
32-
The date and time that this version was built. These variables will
33-
be defined when fe_init.c is compiled.
32+
A string identifying this build of the front end, displayed by --version
33+
and --dump_configuration and used to identify compatible precompiled
34+
header files. If BUILD_IDENTIFIER is defined (as a string literal), it is
35+
used; otherwise, the date and time at which fe_init.c was compiled are
36+
used. Defining BUILD_IDENTIFIER makes it possible to build bit-for-bit
37+
reproducible executables; builds from different sources should use
38+
different identifiers so that their precompiled headers are not mistaken
39+
for each other.
3440
*/
41+
#ifdef BUILD_IDENTIFIER
42+
43+
EXTERN a_const_char
44+
*build_stamp
45+
#if VAR_INITIALIZERS
46+
= BUILD_IDENTIFIER
47+
#endif /* VAR_INITIALIZERS */
48+
;
49+
50+
#else /* !defined(BUILD_IDENTIFIER) */
51+
3552
#ifndef __DATE__
3653
#define __DATE__ "[date unknown]"
3754
#endif /* ifndef __DATE__ */
@@ -47,23 +64,18 @@ be defined when fe_init.c is compiled.
4764
#endif /* defined(_MSC_VER) */
4865

4966
EXTERN a_const_char
50-
*build_date
67+
*build_stamp
5168
#if VAR_INITIALIZERS
52-
= __DATE__
53-
#endif /* VAR_INITIALIZERS */
54-
;
55-
56-
EXTERN a_const_char
57-
*build_time
58-
#if VAR_INITIALIZERS
59-
= __TIME__
69+
= __DATE__ " " __TIME__
6070
#endif /* VAR_INITIALIZERS */
6171
;
6272

6373
#ifdef _MSC_VER
6474
#pragma warning( pop )
6575
#endif /* defined(_MSC_VER) */
6676

77+
#endif /* ifdef BUILD_IDENTIFIER */
78+
6779
/* Conditionally close the "edg" namespace. */
6880
END_EDG_NAMESPACE
6981

0 commit comments

Comments
 (0)