aconfig: add support for cargo
Officially, aconfig is build using the Android tool-chain. However, to speed up the local development cycle, add support for building with cargo. While it is possible to tell cargo to place the build artifacts outside the source tree, there is no way to tell it to not generate the cargo lock file in the same directory as Cargo.toml. Add a .gitignore to ignore Cargo.lock and the target directory. The way the Android build system and cargo generates code from the protobuf files is slightly different. Tell cargo to enable the "cargo" feature and introduce src/protos.rs to hide this difference from the rest of the aconfig source. Bug: 279485059 Test: m aconfig && aconfig Test: atest aconfig.test Test: cargo build Test: cargo test Change-Id: I85741f58cadae353ed95c124f566e4f4a7484186
This commit is contained in:
2
tools/aconfig/.gitignore
vendored
Normal file
2
tools/aconfig/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
/Cargo.lock
|
||||
/target
|
15
tools/aconfig/Cargo.toml
Normal file
15
tools/aconfig/Cargo.toml
Normal file
@@ -0,0 +1,15 @@
|
||||
[package]
|
||||
name = "aconfig"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
build = "build.rs"
|
||||
|
||||
[features]
|
||||
default = ["cargo"]
|
||||
cargo = []
|
||||
|
||||
[dependencies]
|
||||
protobuf = "3.2.0"
|
||||
|
||||
[build-dependencies]
|
||||
protobuf-codegen = "3.2.0"
|
17
tools/aconfig/build.rs
Normal file
17
tools/aconfig/build.rs
Normal file
@@ -0,0 +1,17 @@
|
||||
use protobuf_codegen::Codegen;
|
||||
|
||||
fn main() {
|
||||
let proto_files = vec!["protos/aconfig.proto"];
|
||||
|
||||
// tell cargo to only re-run the build script if any of the proto files has changed
|
||||
for path in &proto_files {
|
||||
println!("cargo:rerun-if-changed={}", path);
|
||||
}
|
||||
|
||||
Codegen::new()
|
||||
.pure()
|
||||
.include("protos")
|
||||
.inputs(proto_files)
|
||||
.cargo_out_dir("aconfig_proto")
|
||||
.run_from_script();
|
||||
}
|
@@ -16,9 +16,11 @@
|
||||
|
||||
//! `aconfig` is a build time tool to manage build time configurations, such as feature flags.
|
||||
|
||||
use aconfig_protos::aconfig::Placeholder;
|
||||
use protobuf::text_format::{parse_from_str, ParseError};
|
||||
|
||||
mod protos;
|
||||
use protos::Placeholder;
|
||||
|
||||
fn foo() -> Result<String, ParseError> {
|
||||
let placeholder = parse_from_str::<Placeholder>(r#"name: "aconfig""#)?;
|
||||
Ok(placeholder.name)
|
||||
|
38
tools/aconfig/src/protos.rs
Normal file
38
tools/aconfig/src/protos.rs
Normal file
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
// When building with the Android tool-chain
|
||||
//
|
||||
// - an external crate `aconfig_protos` will be generated
|
||||
// - the feature "cargo" will be disabled
|
||||
//
|
||||
// When building with cargo
|
||||
//
|
||||
// - a local sub-module will be generated in OUT_DIR and included in this file
|
||||
// - the feature "cargo" will be enabled
|
||||
//
|
||||
// This module hides these differences from the rest of aconfig.
|
||||
|
||||
// When building with the Android tool-chain:
|
||||
#[cfg(not(feature = "cargo"))]
|
||||
pub use aconfig_protos::aconfig::Placeholder;
|
||||
|
||||
// When building with cargo:
|
||||
#[cfg(feature = "cargo")]
|
||||
include!(concat!(env!("OUT_DIR"), "/aconfig_proto/mod.rs"));
|
||||
|
||||
#[cfg(feature = "cargo")]
|
||||
pub use aconfig::Placeholder;
|
Reference in New Issue
Block a user