Skip to content

Commit b6652a9

Browse files
authored
Merge pull request #5 from ospfranco/fix-nondeterministic-builds
Fixes non deterministic builds by ensuring file compilation order
2 parents cd91a9f + 15acaf4 commit b6652a9

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

src/lib.rs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ impl Build {
134134
config
135135
.clone()
136136
.include(&ast_include_dir)
137-
.add_files_by_ext(&ast_source_dir, "cpp")
137+
.add_files_by_ext_sorted(&ast_source_dir, "cpp")
138138
.out_dir(&build_dir)
139139
.compile(ast_lib_name);
140140

@@ -153,7 +153,7 @@ impl Build {
153153
.include(&vm_include_dir)
154154
.include(&vm_source_dir)
155155
.define("LUACODEGEN_API", "extern \"C\"")
156-
.add_files_by_ext(&codegen_source_dir, "cpp")
156+
.add_files_by_ext_sorted(&codegen_source_dir, "cpp")
157157
.out_dir(&build_dir)
158158
.compile(codegen_lib_name);
159159
}
@@ -167,7 +167,7 @@ impl Build {
167167
.include(&compiler_include_dir)
168168
.include(&ast_include_dir)
169169
.define("LUACODE_API", "extern \"C\"")
170-
.add_files_by_ext(&compiler_source_dir, "cpp")
170+
.add_files_by_ext_sorted(&compiler_source_dir, "cpp")
171171
.out_dir(&build_dir)
172172
.compile(compiler_lib_name);
173173

@@ -178,7 +178,7 @@ impl Build {
178178
.clone()
179179
.include(&vm_include_dir)
180180
.include(&vm_source_dir)
181-
.add_files_by_ext(&custom_source_dir, "cpp")
181+
.add_files_by_ext_sorted(&custom_source_dir, "cpp")
182182
.out_dir(&build_dir)
183183
.compile(custom_lib_name);
184184

@@ -199,7 +199,7 @@ impl Build {
199199
for (source_dir, include_dir) in require_source_dirs.iter().zip(require_include_dirs) {
200200
require_config
201201
.include(include_dir)
202-
.add_files_by_ext(source_dir, "cpp");
202+
.add_files_by_ext_sorted(source_dir, "cpp");
203203
}
204204
require_config
205205
.include(&ast_include_dir)
@@ -212,7 +212,7 @@ impl Build {
212212
config
213213
.clone()
214214
.include(&vm_include_dir)
215-
.add_files_by_ext(&vm_source_dir, "cpp")
215+
.add_files_by_ext_sorted(&vm_source_dir, "cpp")
216216
.out_dir(&build_dir)
217217
.compile(vm_lib_name);
218218

@@ -298,18 +298,28 @@ impl Artifacts {
298298
}
299299

300300
trait AddFilesByExt {
301-
fn add_files_by_ext(&mut self, dir: &Path, ext: &str) -> &mut Self;
301+
fn add_files_by_ext_sorted(&mut self, dir: &Path, ext: &str) -> &mut Self;
302302
}
303303

304304
impl AddFilesByExt for cc::Build {
305-
fn add_files_by_ext(&mut self, dir: &Path, ext: &str) -> &mut Self {
306-
for entry in fs::read_dir(dir)
305+
// It's important to keep the order of the files to get consistent builds between machines
306+
// if the order is not always the same, the final binary produces a different SHA256 which
307+
// might cause issues if one needs to verify which binary is being executed
308+
fn add_files_by_ext_sorted(&mut self, dir: &Path, ext: &str) -> &mut Self {
309+
let mut sources: Vec<_> = fs::read_dir(dir)
307310
.unwrap()
308311
.filter_map(|e| e.ok())
309312
.filter(|e| e.path().extension() == Some(ext.as_ref()))
310-
{
311-
self.file(entry.path());
313+
.map(|e| e.path())
314+
.collect();
315+
316+
// Sort for determinism
317+
sources.sort();
318+
319+
for source in sources {
320+
self.file(source);
312321
}
322+
313323
self
314324
}
315325
}

0 commit comments

Comments
 (0)