From 8711eb82d6216cd6c8b3b84c6c37242d841be2d1 Mon Sep 17 00:00:00 2001 From: ljedrz Date: Thu, 30 Jun 2022 09:40:14 +0200 Subject: [PATCH 1/2] feat: introduce a build script checking licenses Signed-off-by: ljedrz --- .rustfmt.toml | 1 - Cargo.toml | 3 +++ build.rs | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 build.rs diff --git a/.rustfmt.toml b/.rustfmt.toml index 891f0e9e52..30b32606ff 100644 --- a/.rustfmt.toml +++ b/.rustfmt.toml @@ -10,7 +10,6 @@ use_try_shorthand = true # Nightly configurations imports_layout = "HorizontalVertical" -license_template_path = ".license_header" imports_granularity = "Crate" overflow_delimited_expr = true reorder_impl_items = true diff --git a/Cargo.toml b/Cargo.toml index 3af60717ab..68de82f943 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -180,6 +180,9 @@ optional = true [dev-dependencies.rusty-hook] version = "0.11.2" +[build-dependencies.walkdir] +version = "2" + [profile.release] opt-level = 3 lto = "thin" diff --git a/build.rs b/build.rs new file mode 100644 index 0000000000..13bfc2f677 --- /dev/null +++ b/build.rs @@ -0,0 +1,64 @@ +// Copyright (C) 2019-2022 Aleo Systems Inc. +// This file is part of the snarkVM library. + +// The snarkVM library is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// The snarkVM library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with the snarkVM library. If not, see . + +use std::{fs::File, io::Read, path::Path}; + +use walkdir::WalkDir; + +// The following license text that should be present at the beginning of every source file. +const EXPECTED_LICENSE_TEXT: &[u8] = include_bytes!(".license_header"); + +// The following directories will be excluded from the license scan. +const DIRS_TO_SKIP: [&str; 5] = [".cargo", ".circleci", ".git", ".github", "target"]; + +fn check_file_licenses>(path: P) { + let path = path.as_ref(); + + let mut iter = WalkDir::new(path).into_iter(); + while let Some(entry) = iter.next() { + let entry = entry.unwrap(); + let entry_type = entry.file_type(); + + // Skip the specified directories. + if entry_type.is_dir() && DIRS_TO_SKIP.contains(&entry.file_name().to_str().unwrap_or("")) { + iter.skip_current_dir(); + + continue; + } + + // Check all files with the ".rs" extension. + if entry_type.is_file() && entry.file_name().to_str().unwrap_or("").ends_with(".rs") { + let file = File::open(entry.path()).unwrap(); + let mut contents = Vec::with_capacity(EXPECTED_LICENSE_TEXT.len()); + file.take(EXPECTED_LICENSE_TEXT.len() as u64).read_to_end(&mut contents).unwrap(); + + assert!( + contents == EXPECTED_LICENSE_TEXT, + "The license in \"{}\" is either missing or it doesn't match the expected string!", + entry.path().display() + ); + } + } + + // Re-run upon any changes to the workspace. + println!("cargo:rerun-if-changed=."); +} + +// The build script; it currently only checks the licenses. +fn main() { + // Check licenses in the current folder. + check_file_licenses("."); +} From 6c48a4cd273a5c8b9db5eed04bf5a3ec5610e04f Mon Sep 17 00:00:00 2001 From: ljedrz Date: Thu, 30 Jun 2022 09:40:22 +0200 Subject: [PATCH 2/2] chore: adjust Cargo.lock Signed-off-by: ljedrz --- Cargo.lock | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.lock b/Cargo.lock index f51dfca93d..5193530ca6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1999,6 +1999,7 @@ dependencies = [ "snarkvm-r1cs", "snarkvm-utilities", "thiserror", + "walkdir", ] [[package]]