C++ Logo

std-proposals

Advanced search

Re: [std-proposals] constexpr support for std::chrono::system_clock

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Fri, 7 Mar 2025 12:15:45 +0000
An idea just came into my head now for generating UUID's in source
code in such a way that the next build will be identical.

So let's say there's a preprocessor macro called UUID that inserts a
random 128-Bit number.

The random UUID could be generated as follows:

    1) Take the entire contents of the current source file (or just
the current header file if the macro is in a header file)
    2) Take contents from Step 1 and feed them into the MD5 hash function
    3) For each use of the UUID macro, append the line and character
number before computing the hash

So let's say we have a header file called "myheader.hpp" which
contains just one line as follows:

    inline int monkey = UUID & 1;

And let's say we have a source file "mysource.cpp" which contains two
lines as follows:

    #include "myheader.hpp"
    int main(void) {}

And so when the source file is put through the compiler, and the UUID
macro is encountered, the entire contents of "myheader.hpp" gets
inputted into a hash function. Next the line number and character
number go into the hash function (i.e. "1 20"), and then the MD5
digest is computed, yielding a 128-Bit number.

So unless you change the contents of some files, every header file
will always generate the same UUID. So you've got identical builds.

A few little nitty gritty points:
    * The character '\r' should be ignored so that Linux and
MS-Windows builds generate the same UUID
    * (I'll think of more points later)

Received on 2025-03-07 12:15:56