From a314f8e3fdf10a6ba45822029965a8912dd158f2 Mon Sep 17 00:00:00 2001 From: Andrew Walbran Date: Wed, 26 Jul 2023 16:58:09 +0100 Subject: [PATCH] Split out JSON configuration reading to a separate module. Bug: 293289578 Test: m cargo_embargo Change-Id: I993da0c666a2dd90fa46f697553b9778e1e3ad37 --- tools/cargo_embargo/src/config.rs | 108 ++++++++++++++++++++++++++++++ tools/cargo_embargo/src/main.rs | 90 +------------------------ 2 files changed, 111 insertions(+), 87 deletions(-) create mode 100644 tools/cargo_embargo/src/config.rs diff --git a/tools/cargo_embargo/src/config.rs b/tools/cargo_embargo/src/config.rs new file mode 100644 index 000000000..6c76376d6 --- /dev/null +++ b/tools/cargo_embargo/src/config.rs @@ -0,0 +1,108 @@ +// Copyright (C) 2023 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! Code for reading configuration json files. +//! +//! These are usually called `cargo_embargo.json` or `cargo2android.json`. + +use serde::Deserialize; +use std::collections::BTreeMap; +use std::path::PathBuf; + +fn default_apex_available() -> Vec { + vec!["//apex_available:platform".to_string(), "//apex_available:anyapex".to_string()] +} + +fn default_true() -> bool { + true +} + +/// Options that apply to everything. +#[derive(Deserialize)] +#[serde(deny_unknown_fields)] +pub struct Config { + /// Whether to output "rust_test" modules. + pub tests: bool, + /// Set of features to enable. If non-empty, disables the default crate features. + #[serde(default)] + pub features: Vec, + /// Whether to build with --workspace. + #[serde(default)] + pub workspace: bool, + /// When workspace is enabled, list of --exclude crates. + #[serde(default)] + pub workspace_excludes: Vec, + /// Value to use for every generated module's "defaults" field. + pub global_defaults: Option, + /// Value to use for every generated library module's "apex_available" field. + #[serde(default = "default_apex_available")] + pub apex_available: Vec, + /// Value to use for every generated library module's `product_available` field. + #[serde(default = "default_true")] + pub product_available: bool, + /// Value to use for every generated library module's `vendor_available` field. + #[serde(default = "default_true")] + pub vendor_available: bool, + /// Map of renames for modules. For example, if a "libfoo" would be generated and there is an + /// entry ("libfoo", "libbar"), the generated module will be called "libbar" instead. + /// + /// Also, affects references to dependencies (e.g. in a "static_libs" list), even those outside + /// the project being processed. + #[serde(default)] + pub module_name_overrides: BTreeMap, + /// Package specific config options. + #[serde(default)] + pub package: BTreeMap, + /// Modules in this list will not be generated. + #[serde(default)] + pub module_blocklist: Vec, + /// Modules name => Soong "visibility" property. + #[serde(default)] + pub module_visibility: BTreeMap>, +} + +/// Options that apply to everything in a package (i.e. everything associated with a particular +/// Cargo.toml file). +#[derive(Deserialize, Default)] +#[serde(deny_unknown_fields)] +pub struct PackageConfig { + /// Whether to compile for device. Defaults to true. + #[serde(default)] + pub device_supported: Option, + /// Whether to compile for host. Defaults to true. + #[serde(default)] + pub host_supported: Option, + /// Generate "rust_library_rlib" instead of "rust_library". + #[serde(default)] + pub force_rlib: bool, + /// Whether to disable "unit_test" for "rust_test" modules. + // TODO: Should probably be a list of modules or crates. A package might have a mix of unit and + // integration tests. + #[serde(default)] + pub no_presubmit: bool, + /// File with content to append to the end of the generated Android.bp. + pub add_toplevel_block: Option, + /// File with content to append to the end of each generated module. + pub add_module_block: Option, + /// Modules in this list will not be added as dependencies of generated modules. + #[serde(default)] + pub dep_blocklist: Vec, + /// Patch file to apply after Android.bp is generated. + pub patch: Option, + /// Copy build.rs output to ./out/* and add a genrule to copy ./out/* to genrule output. + /// For crates with code pattern: + /// include!(concat!(env!("OUT_DIR"), "/.rs")) + #[serde(default)] + pub copy_out: bool, +} diff --git a/tools/cargo_embargo/src/main.rs b/tools/cargo_embargo/src/main.rs index 67ebd59e5..e47f22ccb 100644 --- a/tools/cargo_embargo/src/main.rs +++ b/tools/cargo_embargo/src/main.rs @@ -28,7 +28,10 @@ mod bp; mod cargo_out; +mod config; +use crate::config::Config; +use crate::config::PackageConfig; use anyhow::bail; use anyhow::Context; use anyhow::Result; @@ -66,93 +69,6 @@ struct Args { reuse_cargo_out: bool, } -fn default_apex_available() -> Vec { - vec!["//apex_available:platform".to_string(), "//apex_available:anyapex".to_string()] -} - -fn default_true() -> bool { - true -} - -/// Options that apply to everything. -#[derive(serde::Deserialize)] -#[serde(deny_unknown_fields)] -struct Config { - /// Whether to output "rust_test" modules. - tests: bool, - /// Set of features to enable. If non-empty, disables the default crate features. - #[serde(default)] - features: Vec, - /// Whether to build with --workspace. - #[serde(default)] - workspace: bool, - /// When workspace is enabled, list of --exclude crates. - #[serde(default)] - workspace_excludes: Vec, - /// Value to use for every generated module's "defaults" field. - global_defaults: Option, - /// Value to use for every generated library module's "apex_available" field. - #[serde(default = "default_apex_available")] - apex_available: Vec, - /// Value to use for every generated library module's `product_available` field. - #[serde(default = "default_true")] - product_available: bool, - /// Value to use for every generated library module's `vendor_available` field. - #[serde(default = "default_true")] - vendor_available: bool, - /// Map of renames for modules. For example, if a "libfoo" would be generated and there is an - /// entry ("libfoo", "libbar"), the generated module will be called "libbar" instead. - /// - /// Also, affects references to dependencies (e.g. in a "static_libs" list), even those outside - /// the project being processed. - #[serde(default)] - module_name_overrides: BTreeMap, - /// Package specific config options. - #[serde(default)] - package: BTreeMap, - /// Modules in this list will not be generated. - #[serde(default)] - module_blocklist: Vec, - /// Modules name => Soong "visibility" property. - #[serde(default)] - module_visibility: BTreeMap>, -} - -/// Options that apply to everything in a package (i.e. everything associated with a particular -/// Cargo.toml file). -#[derive(serde::Deserialize, Default)] -#[serde(deny_unknown_fields)] -struct PackageConfig { - /// Whether to compile for device. Defaults to true. - #[serde(default)] - device_supported: Option, - /// Whether to compile for host. Defaults to true. - #[serde(default)] - host_supported: Option, - /// Generate "rust_library_rlib" instead of "rust_library". - #[serde(default)] - force_rlib: bool, - /// Whether to disable "unit_test" for "rust_test" modules. - // TODO: Should probably be a list of modules or crates. A package might have a mix of unit and - // integration tests. - #[serde(default)] - no_presubmit: bool, - /// File with content to append to the end of the generated Android.bp. - add_toplevel_block: Option, - /// File with content to append to the end of each generated module. - add_module_block: Option, - /// Modules in this list will not be added as dependencies of generated modules. - #[serde(default)] - dep_blocklist: Vec, - /// Patch file to apply after Android.bp is generated. - patch: Option, - /// Copy build.rs output to ./out/* and add a genrule to copy ./out/* to genrule output. - /// For crates with code pattern: - /// include!(concat!(env!("OUT_DIR"), "/.rs")) - #[serde(default)] - copy_out: bool, -} - fn main() -> Result<()> { let args = Args::parse();