Skip to content

Commit e12215c

Browse files
committed
[bindings] Use the same SipHash keys to make C++ header stable
1 parent 35e48cf commit e12215c

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

.github/workflows/build.yml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -173,15 +173,7 @@ jobs:
173173
cat lightning-c-bindings/include/lightning.h | grep -v "Generated with cbindgen:[0-9\.]*" | sort > lightning-c-bindings/include/lightning.h.sorted
174174
cat lightning-c-bindings/include/lightning.h.new | grep -v "Generated with cbindgen:[0-9\.]*" | sort > lightning-c-bindings/include/lightning.h.new.sorted
175175
diff lightning-c-bindings/include/lightning.h.sorted lightning-c-bindings/include/lightning.h.new.sorted
176-
#
177-
mv lightning-c-bindings/include/lightningpp.hpp lightning-c-bindings/include/lightningpp.hpp.new
178-
git checkout lightning-c-bindings/include/lightningpp.hpp
179-
cat lightning-c-bindings/include/lightningpp.hpp | sort > lightning-c-bindings/include/lightningpp.hpp.sorted
180-
cat lightning-c-bindings/include/lightningpp.hpp.new | sort > lightning-c-bindings/include/lightningpp.hpp.new.sorted
181-
diff lightning-c-bindings/include/lightningpp.hpp.sorted lightning-c-bindings/include/lightningpp.hpp.new.sorted
182-
#
183176
[ "$(diff lightning-c-bindings/include/lightning.h.sorted lightning-c-bindings/include/lightning.h.new.sorted)" != "" ] && exit 2
184-
[ "$(diff lightning-c-bindings/include/lightningpp.hpp.sorted lightning-c-bindings/include/lightningpp.hpp.new.sorted)" != "" ] && exit 3
185177
git diff --exit-code
186178
fi
187179

c-bindings-gen/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1350,7 +1350,7 @@ fn main() {
13501350
// ...then walk the ASTs tracking what types we will map, and how, so that we can resolve them
13511351
// when parsing other file ASTs...
13521352
let mut libtypes = CrateTypes { traits: HashMap::new(), opaques: HashMap::new(), mirrored_enums: HashMap::new(),
1353-
type_aliases: HashMap::new(), templates_defined: HashMap::new(), template_file: &mut derived_templates };
1353+
type_aliases: HashMap::new(), templates_defined: HashMap::default(), template_file: &mut derived_templates };
13541354
walk_ast(&args[1], "/lib.rs", "".to_string(), &libast, &mut libtypes);
13551355

13561356
// ... finally, do the actual file conversion/mapping, writing out types as we go.

c-bindings-gen/src/types.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::collections::HashMap;
22
use std::fs::File;
33
use std::io::Write;
4+
use std::hash;
45

56
use proc_macro2::{TokenTree, Span};
67

@@ -225,6 +226,13 @@ pub enum DeclType<'a> {
225226
EnumIgnored,
226227
}
227228

229+
// templates_defined is walked to write the C++ header, so if we use the default hashing it get
230+
// reordered on each genbindings run. Instead, we use SipHasher (which defaults to 0-keys) so that
231+
// the sorting is stable across runs. It is deprecated, but the "replacement" doesn't actually
232+
// accomplish the same goals, so we just ignore it.
233+
#[allow(deprecated)]
234+
type NonRandomHash = hash::BuildHasherDefault<hash::SipHasher>;
235+
228236
/// Top-level struct tracking everything which has been defined while walking the crate.
229237
pub struct CrateTypes<'a> {
230238
/// This may contain structs or enums, but only when either is mapped as
@@ -240,7 +248,7 @@ pub struct CrateTypes<'a> {
240248
/// exists.
241249
///
242250
/// This is used at the end of processing to make C++ wrapper classes
243-
pub templates_defined: HashMap<String, bool>,
251+
pub templates_defined: HashMap<String, bool, NonRandomHash>,
244252
/// The output file for any created template container types, written to as we find new
245253
/// template containers which need to be defined.
246254
pub template_file: &'a mut File,

0 commit comments

Comments
 (0)