Merge "Split out JSON configuration reading to a separate module." into main

This commit is contained in:
Frederick Mayle
2023-08-01 19:23:38 +00:00
committed by Gerrit Code Review
2 changed files with 111 additions and 87 deletions

View File

@@ -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<String> {
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<String>,
/// Whether to build with --workspace.
#[serde(default)]
pub workspace: bool,
/// When workspace is enabled, list of --exclude crates.
#[serde(default)]
pub workspace_excludes: Vec<String>,
/// Value to use for every generated module's "defaults" field.
pub global_defaults: Option<String>,
/// Value to use for every generated library module's "apex_available" field.
#[serde(default = "default_apex_available")]
pub apex_available: Vec<String>,
/// 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<String, String>,
/// Package specific config options.
#[serde(default)]
pub package: BTreeMap<String, PackageConfig>,
/// Modules in this list will not be generated.
#[serde(default)]
pub module_blocklist: Vec<String>,
/// Modules name => Soong "visibility" property.
#[serde(default)]
pub module_visibility: BTreeMap<String, Vec<String>>,
}
/// 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<bool>,
/// Whether to compile for host. Defaults to true.
#[serde(default)]
pub host_supported: Option<bool>,
/// 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<PathBuf>,
/// File with content to append to the end of each generated module.
pub add_module_block: Option<PathBuf>,
/// Modules in this list will not be added as dependencies of generated modules.
#[serde(default)]
pub dep_blocklist: Vec<String>,
/// Patch file to apply after Android.bp is generated.
pub patch: Option<PathBuf>,
/// 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"), "/<some_file>.rs"))
#[serde(default)]
pub copy_out: bool,
}

View File

@@ -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<String> {
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<String>,
/// Whether to build with --workspace.
#[serde(default)]
workspace: bool,
/// When workspace is enabled, list of --exclude crates.
#[serde(default)]
workspace_excludes: Vec<String>,
/// Value to use for every generated module's "defaults" field.
global_defaults: Option<String>,
/// Value to use for every generated library module's "apex_available" field.
#[serde(default = "default_apex_available")]
apex_available: Vec<String>,
/// 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<String, String>,
/// Package specific config options.
#[serde(default)]
package: BTreeMap<String, PackageConfig>,
/// Modules in this list will not be generated.
#[serde(default)]
module_blocklist: Vec<String>,
/// Modules name => Soong "visibility" property.
#[serde(default)]
module_visibility: BTreeMap<String, Vec<String>>,
}
/// 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<bool>,
/// Whether to compile for host. Defaults to true.
#[serde(default)]
host_supported: Option<bool>,
/// 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<PathBuf>,
/// File with content to append to the end of each generated module.
add_module_block: Option<PathBuf>,
/// Modules in this list will not be added as dependencies of generated modules.
#[serde(default)]
dep_blocklist: Vec<String>,
/// Patch file to apply after Android.bp is generated.
patch: Option<PathBuf>,
/// 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"), "/<some_file>.rs"))
#[serde(default)]
copy_out: bool,
}
fn main() -> Result<()> {
let args = Args::parse();