cancel
Showing results for 
Search instead for 
Did you mean: 

How to make a reproducible build?

Nicolas1
Associate II

My goal is to have the same binary when compiling the same source files. This works well when the path to the source files doesn't change but if it is changed, the generated binaries will be different.

So, how to make a reproducible build when the path to source files changes?

7 REPLIES 7
Ozone
Lead

Most probably the order of processing.

The linker will usually place objects in the according segments sequentially, according to the processing sequence.

A makefile would be the best way to control this.

Differences in the produced binary are not necessary an indicator of different behavior.

The very same routines and variables might end up on different addresses.

Danish1
Lead II

This is actually quite a difficult thing if you are using a high-level language (such as C).

This is because the C compiler translates the code you write into binary machine instructions that have the same effect.

As compilers get better (i.e. when you upgrade your toolchain), they manage to get this effect using fewer machine instructions, or to execute faster. This is particularly the case if you have any optimisations enabled. So by definition the binary will be different.

And where you have multiple source files, the order which they get placed into memory might change - particularly if you move these files. I suppose the order could reflect the order they appear in your project. So if you copy the source files, and make a new project with the same files (in the new location), then the order you put the new files into the new project might make a difference.

And then there are cases where the filename is embedded into the generated binary - e.g. if you use the __FILE__ macro. Or maybe the build time with __DATE__. You'll get different binaries each time you build.

When I was writing assembly code for 8-bit microcontrollers, I made a note of the build checksum

But for stm32, I use version-control of the source code. This is much better.

Hope this helps,

Danish

Thank you for your reply. STM32 Cube IDE is based on Eclipse which generates the required makefiles.

To be more complete: the generated .map and .list are the same so the order does not appear to be the right answer in that case.

Thank you for your reply. I can easily do reproducible builds with GCC, when I compile the same code with the same version of GCC. Here, I use the same environment on the same computer. The only variable I'm aware of is the path of source files.

The __DATE__ macro is not present in my code, compiling the same code with the source files at the same location will give me the same binary file. I checked for the __FILE__ macro which is not present. It could have been it.

I do use a version-control of the source code. The issue is that 2 people starting a development with the same code will produce different outputs. Or even if I change the path of my working copy, I will produce a different binary.

With every change in the project setup, the makefile is re-created.

And obviously different.

The Eclipse project management files are a pain in the b*tt when using version control software.

I do agree about the pain part. What I can't figure out is how they differ as both project files (.project and .cproject) are not modified with a build even though source files are located at different paths.

Same message posted twice.