Skip to content

Commit 288593b

Browse files
committed
rust: enable bindings to be built without existing cmake build dir
1 parent 53380d6 commit 288593b

File tree

3,510 files changed

+282651
-25
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

3,510 files changed

+282651
-25
lines changed

.gitattributes

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
/py/bitbox02/bitbox02/generated/* linguist-generated=true
1+
/py/bitbox02/bitbox02/generated/ linguist-generated=true
2+
/src/rust/vendor/ linguist-generated=true

src/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,14 @@ add_custom_target(rust-bindgen
306306
)
307307
add_dependencies(rust-bindgen generate-protobufs)
308308

309+
# A custom target to export the list of includes for rust tooling (RA, clippy)
310+
add_custom_command(OUTPUT rust-bindgen.flags
311+
COMMAND
312+
${CMAKE_COMMAND} -E echo ${RUST_INCLUDES} > rust-bindgen.flags
313+
)
314+
add_custom_target(rust-bindgen-includes DEPENDS rust-bindgen.flags)
315+
add_dependencies(rust-bindgen-includes generate-protobufs)
316+
309317

310318
# Test rust crates that contain business logic. Avoid testing crates that depend on hardware.
311319
if(NOT CMAKE_CROSSCOMPILING)

src/rust/Cargo.lock

Lines changed: 93 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/rust/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,9 @@ The bottom-most layer are bindings generated from C header files:
3535

3636
We generate one header file `rust.h` and ever product specific function is `#ifdeffed` with
3737
`RUST_PRODUCT_*` macro.
38+
39+
40+
# rust-analyzer / clippy
41+
42+
To be able to run static analyzers you'll need nanopb to work and libclang to be available on the
43+
host machine.

src/rust/bitbox02-sys/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,6 @@ license = "Apache-2.0"
2222

2323
[dependencies]
2424
util = {path = "../util"}
25+
26+
[build-dependencies]
27+
tempdir = "0.3"

src/rust/bitbox02-sys/build.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
use std::process::Command;
2+
use std::fs::File;
3+
use std::io::prelude::*;
4+
use tempdir::TempDir;
5+
6+
fn main() {
7+
let path_to_bindings = if let Ok(cmake_dir) = std::env::var("CMAKE_CURRENT_BINARY_DIR") {
8+
// if we are being invoked from CMAKE, the bindings are here:
9+
format!("{}/rust", cmake_dir)
10+
} else {
11+
let bitbox02_sys_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
12+
let cmake_dir = format!("{}/../../../", bitbox02_sys_dir);
13+
14+
// generate list of includes using CMake
15+
let tempdir = TempDir::new("bitbox02").unwrap();
16+
let tempdir = tempdir.as_ref().to_str().unwrap();
17+
let _ = Command::new("cmake").arg(&cmake_dir).current_dir(&tempdir).output().unwrap();
18+
let _ = Command::new("make").arg("rust-bindgen-includes").current_dir(&tempdir).output().unwrap();
19+
let mut includes_file = File::open(format!("{}/src/rust-bindgen.flags", tempdir)).unwrap();
20+
let mut includes = String::new();
21+
includes_file.read_to_string(&mut includes).unwrap();
22+
let includes:Vec<&str> = includes.trim().split_ascii_whitespace().collect();
23+
let mut flags = vec!["-target", "x86_64-pc-linux-gnu", "-DTESTING=1"];
24+
flags.extend(&includes);
25+
26+
// generate bindings
27+
let outdir = std::env::var("OUT_DIR").unwrap();
28+
let generate_bindings = format!("{}/scripts/generate-bindings.sh", cmake_dir);
29+
let bindings = format!("{}/bindings.rs", outdir);
30+
let wrapper = format!("{}/wrapper.h", bitbox02_sys_dir);
31+
let _ = Command::new(&generate_bindings)
32+
.args(&[&bindings, &wrapper])
33+
.args(&flags)
34+
.output()
35+
.unwrap();
36+
37+
outdir
38+
};
39+
println!("cargo:rustc-env=BINDINGS={}/bindings.rs", path_to_bindings);
40+
}

src/rust/bitbox02-sys/src/lib.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@
1717
#![allow(non_upper_case_globals)]
1818
#![allow(non_camel_case_types)]
1919
#![allow(non_snake_case)]
20+
#![allow(clippy::all)]
2021

2122
// include our generated bindings
22-
include!(concat!(
23-
env!("CMAKE_CURRENT_BINARY_DIR"),
24-
"/rust",
25-
"/bindings.rs"
26-
));
23+
include!(env!("BINDINGS"));

src/rust/bitbox02/build.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
fn main() {
22
#[cfg(feature = "testing")]
33
{
4-
let cmake_dir = std::env::var("CMAKE_CURRENT_BINARY_DIR").unwrap();
5-
println!("cargo:rustc-link-search={}/../lib", cmake_dir);
6-
// c and rust code merged :O
7-
println!("cargo:rustc-link-lib=bitbox_merged");
8-
println!(
9-
"cargo:rerun-if-changed={}/../lib/libbitbox_merged.a",
10-
cmake_dir
11-
);
4+
// CMAKE_CURRENT_BINARY_DIR is required for building tests
5+
if let Ok(cmake_dir) = std::env::var("CMAKE_CURRENT_BINARY_DIR") {
6+
println!("cargo:rustc-link-search={}/../lib", cmake_dir);
7+
// c and rust code merged :O
8+
println!("cargo:rustc-link-lib=bitbox_merged");
9+
println!(
10+
"cargo:rerun-if-changed={}/../lib/libbitbox_merged.a",
11+
cmake_dir
12+
);
1213

13-
// external libs
14-
println!("cargo:rustc-link-lib=wallycore");
15-
println!("cargo:rustc-link-lib=secp256k1");
16-
println!("cargo:rustc-link-lib=base32");
17-
println!("cargo:rustc-link-lib=ctaes");
18-
println!("cargo:rustc-link-lib=fatfs");
19-
println!("cargo:rustc-link-lib=sd-mock");
14+
// external libs
15+
println!("cargo:rustc-link-lib=wallycore");
16+
println!("cargo:rustc-link-lib=secp256k1");
17+
println!("cargo:rustc-link-lib=base32");
18+
println!("cargo:rustc-link-lib=ctaes");
19+
println!("cargo:rustc-link-lib=fatfs");
20+
println!("cargo:rustc-link-lib=sd-mock");
2021

21-
// system libs
22-
println!("cargo:rustc-link-lib=cmocka");
22+
// system libs
23+
println!("cargo:rustc-link-lib=cmocka");
24+
}
2325
}
2426
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"files":{"AUTHORS":"f82920a5bcfc71b86c1de4be4cdea8af2009ba9e5735824f95ed5043a03e46f0","Cargo.toml":"bb2497b2907c3af499ce7ac3722dae044968e13409a372b7760150f8a01c74c5","LICENSE":"03b114f53e6587a398931762ee11e2395bfdba252a329940e2c8c9e81813845b","PATENTS":"52beb3ac72a0e7f5060384d16e4e6f91573016448fbff363c0b01a66fe99f547","src/lib.rs":"3d76c35c13203093ddf7ce2a3be5e98d768a8091cd9d99bd083fe8db35364096"},"package":"a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba"}

src/rust/vendor/fuchsia-cprng/AUTHORS

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# This is the list of Fuchsia Authors.
2+
3+
# Names should be added to this file as one of
4+
# Organization's name
5+
# Individual's name <submission email address>
6+
# Individual's name <submission email address> <email2> <emailN>
7+
8+
Google Inc.
9+
The Chromium Authors
10+
The Go Authors
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
2+
#
3+
# When uploading crates to the registry Cargo will automatically
4+
# "normalize" Cargo.toml files for maximal compatibility
5+
# with all versions of Cargo and also rewrite `path` dependencies
6+
# to registry (e.g. crates.io) dependencies
7+
#
8+
# If you believe there's an error in this file please file an
9+
# issue against the rust-lang/cargo repository. If you're
10+
# editing this file be aware that the upstream Cargo.toml
11+
# will likely look very different (and much more reasonable)
12+
13+
[package]
14+
edition = "2018"
15+
name = "fuchsia-cprng"
16+
version = "0.1.1"
17+
authors = ["Erick Tryzelaar <[email protected]>"]
18+
include = ["src/*.rs", "Cargo.toml", "AUTHORS", "LICENSE", "PATENTS"]
19+
description = "Rust crate for the Fuchsia cryptographically secure pseudorandom number generator"
20+
readme = "README.md"
21+
license-file = "LICENSE"
22+
repository = "https://fuchsia.googlesource.com/fuchsia/+/master/garnet/public/rust/fuchsia-cprng"

src/rust/vendor/fuchsia-cprng/LICENSE

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Copyright 2019 The Fuchsia Authors. All rights reserved.
2+
3+
Redistribution and use in source and binary forms, with or without
4+
modification, are permitted provided that the following conditions are
5+
met:
6+
7+
* Redistributions of source code must retain the above copyright
8+
notice, this list of conditions and the following disclaimer.
9+
* Redistributions in binary form must reproduce the above
10+
copyright notice, this list of conditions and the following disclaimer
11+
in the documentation and/or other materials provided with the
12+
distribution.
13+
* Neither the name of Google Inc. nor the names of its
14+
contributors may be used to endorse or promote products derived from
15+
this software without specific prior written permission.
16+
17+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

src/rust/vendor/fuchsia-cprng/PATENTS

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Additional IP Rights Grant (Patents)
2+
3+
"This implementation" means the copyrightable works distributed by
4+
Google as part of the Fuchsia project.
5+
6+
Google hereby grants to you a perpetual, worldwide, non-exclusive,
7+
no-charge, royalty-free, irrevocable (except as stated in this
8+
section) patent license to make, have made, use, offer to sell, sell,
9+
import, transfer, and otherwise run, modify and propagate the contents
10+
of this implementation of Fuchsia, where such license applies only to
11+
those patent claims, both currently owned by Google and acquired in
12+
the future, licensable by Google that are necessarily infringed by
13+
this implementation. This grant does not include claims that would be
14+
infringed only as a consequence of further modification of this
15+
implementation. If you or your agent or exclusive licensee institute
16+
or order or agree to the institution of patent litigation or any other
17+
patent enforcement activity against any entity (including a
18+
cross-claim or counterclaim in a lawsuit) alleging that this
19+
implementation of Fuchsia constitutes direct or contributory patent
20+
infringement, or inducement of patent infringement, then any patent
21+
rights granted to you under this License for this implementation of
22+
Fuchsia shall terminate as of the date such litigation is filed.

0 commit comments

Comments
 (0)