From e12215ca8a232405fcd0b5c9ff69b6db15d6bfaa Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Thu, 8 Oct 2020 14:43:37 -0400 Subject: [PATCH] [bindings] Use the same SipHash keys to make C++ header stable --- .github/workflows/build.yml | 8 -------- c-bindings-gen/src/main.rs | 2 +- c-bindings-gen/src/types.rs | 10 +++++++++- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index f1e519bee..bafdd29ff 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -173,15 +173,7 @@ jobs: cat lightning-c-bindings/include/lightning.h | grep -v "Generated with cbindgen:[0-9\.]*" | sort > lightning-c-bindings/include/lightning.h.sorted cat lightning-c-bindings/include/lightning.h.new | grep -v "Generated with cbindgen:[0-9\.]*" | sort > lightning-c-bindings/include/lightning.h.new.sorted diff lightning-c-bindings/include/lightning.h.sorted lightning-c-bindings/include/lightning.h.new.sorted - # - mv lightning-c-bindings/include/lightningpp.hpp lightning-c-bindings/include/lightningpp.hpp.new - git checkout lightning-c-bindings/include/lightningpp.hpp - cat lightning-c-bindings/include/lightningpp.hpp | sort > lightning-c-bindings/include/lightningpp.hpp.sorted - cat lightning-c-bindings/include/lightningpp.hpp.new | sort > lightning-c-bindings/include/lightningpp.hpp.new.sorted - diff lightning-c-bindings/include/lightningpp.hpp.sorted lightning-c-bindings/include/lightningpp.hpp.new.sorted - # [ "$(diff lightning-c-bindings/include/lightning.h.sorted lightning-c-bindings/include/lightning.h.new.sorted)" != "" ] && exit 2 - [ "$(diff lightning-c-bindings/include/lightningpp.hpp.sorted lightning-c-bindings/include/lightningpp.hpp.new.sorted)" != "" ] && exit 3 git diff --exit-code fi diff --git a/c-bindings-gen/src/main.rs b/c-bindings-gen/src/main.rs index 611607ede..fed13f11c 100644 --- a/c-bindings-gen/src/main.rs +++ b/c-bindings-gen/src/main.rs @@ -1350,7 +1350,7 @@ fn main() { // ...then walk the ASTs tracking what types we will map, and how, so that we can resolve them // when parsing other file ASTs... let mut libtypes = CrateTypes { traits: HashMap::new(), opaques: HashMap::new(), mirrored_enums: HashMap::new(), - type_aliases: HashMap::new(), templates_defined: HashMap::new(), template_file: &mut derived_templates }; + type_aliases: HashMap::new(), templates_defined: HashMap::default(), template_file: &mut derived_templates }; walk_ast(&args[1], "/lib.rs", "".to_string(), &libast, &mut libtypes); // ... finally, do the actual file conversion/mapping, writing out types as we go. diff --git a/c-bindings-gen/src/types.rs b/c-bindings-gen/src/types.rs index 973d1909e..32036180d 100644 --- a/c-bindings-gen/src/types.rs +++ b/c-bindings-gen/src/types.rs @@ -1,6 +1,7 @@ use std::collections::HashMap; use std::fs::File; use std::io::Write; +use std::hash; use proc_macro2::{TokenTree, Span}; @@ -225,6 +226,13 @@ pub enum DeclType<'a> { EnumIgnored, } +// templates_defined is walked to write the C++ header, so if we use the default hashing it get +// reordered on each genbindings run. Instead, we use SipHasher (which defaults to 0-keys) so that +// the sorting is stable across runs. It is deprecated, but the "replacement" doesn't actually +// accomplish the same goals, so we just ignore it. +#[allow(deprecated)] +type NonRandomHash = hash::BuildHasherDefault; + /// Top-level struct tracking everything which has been defined while walking the crate. pub struct CrateTypes<'a> { /// This may contain structs or enums, but only when either is mapped as @@ -240,7 +248,7 @@ pub struct CrateTypes<'a> { /// exists. /// /// This is used at the end of processing to make C++ wrapper classes - pub templates_defined: HashMap, + pub templates_defined: HashMap, /// The output file for any created template container types, written to as we find new /// template containers which need to be defined. pub template_file: &'a mut File, -- 2.39.5