#![allow(dead_code)]
pub mod analytics;
pub mod archive;
pub mod cache;
pub mod cmake;
pub mod cmake_templates;
pub mod docker;
pub mod elf;
pub mod incremental;
pub mod linkage;
pub mod materialize;
pub mod platforms;
pub mod toolchains;
pub mod verinfo;
pub mod profile;
pub mod sanitizer;
use std::collections::HashSet;
use std::path::PathBuf;
use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use crate::commands::build::{BuildTarget, LinkType, WindowsToolchain};
use crate::config::CcgoConfig;
pub fn resolve_stl(cli: Option<&str>, from_toml: Option<&str>) -> String {
cli.or(from_toml).unwrap_or("c++_shared").to_string()
}
#[derive(Debug, Clone)]
pub struct BuildOptions {
pub target: BuildTarget,
pub architectures: Vec<String>,
pub link_type: LinkType,
pub use_docker: bool,
pub auto_docker: bool,
pub jobs: Option<usize>,
pub ide_project: bool,
pub release: bool,
pub stl: Option<String>,
pub native_only: bool,
pub toolchain: WindowsToolchain,
pub verbose: bool,
pub dev: bool,
pub features: Vec<String>,
pub use_default_features: bool,
pub all_features: bool,
pub variants: Vec<String>,
pub cache: Option<String>,
pub analytics: bool,
pub linkage_default: Option<crate::config::Linkage>,
pub linkage_overrides: std::collections::HashMap<String, crate::config::Linkage>,
pub linkage_on_shared_default: Option<crate::config::Linkage>,
pub linkage_on_shared_overrides: std::collections::HashMap<String, crate::config::Linkage>,
pub linkage_on_static_default: Option<crate::config::Linkage>,
pub linkage_on_static_overrides: std::collections::HashMap<String, crate::config::Linkage>,
pub profile_name: Option<String>,
pub sanitizer: Option<sanitizer::SanitizerKind>,
}
impl Default for BuildOptions {
fn default() -> Self {
Self {
target: BuildTarget::Linux,
architectures: Vec::new(),
link_type: LinkType::Both,
stl: None,
use_docker: false,
auto_docker: false,
jobs: None,
ide_project: false,
release: true,
native_only: false,
toolchain: WindowsToolchain::Auto,
verbose: false,
dev: false,
features: Vec::new(),
variants: Vec::new(),
use_default_features: true,
all_features: false,
cache: None,
analytics: false,
linkage_default: None,
linkage_overrides: std::collections::HashMap::new(),
linkage_on_shared_default: None,
linkage_on_shared_overrides: std::collections::HashMap::new(),
linkage_on_static_default: None,
linkage_on_static_overrides: std::collections::HashMap::new(),
profile_name: None,
sanitizer: None,
}
}
}
#[derive(Debug)]
pub struct BuildContext {
pub project_root: PathBuf,
pub config: CcgoConfig,
pub options: BuildOptions,
pub ccgo_build_root: PathBuf,
pub cmake_build_dir: PathBuf,
pub output_dir: PathBuf,
pub git_version: Option<crate::utils::git_version::GitVersion>,
pub resolved_profile: Option<crate::builder::profile::ResolvedProfile>,
pub profile_name_override: Option<String>,
}
impl BuildContext {
pub fn new(project_root: PathBuf, config: CcgoConfig, options: BuildOptions) -> Self {
let platform_name = options.target.to_string().to_lowercase();
Self::new_with_platform_name(project_root, config, options, platform_name)
}
pub fn new_with_platform_name(
project_root: PathBuf,
config: CcgoConfig,
options: BuildOptions,
platform_name: String,
) -> Self {
let build_dir_name = config
.build
.as_ref()
.and_then(|b| b.build_dir.as_deref())
.unwrap_or(crate::utils::paths::CCGO_BUILD_DIR)
.to_string();
let mode = if options.release { "release" } else { "debug" };
let build_subdir = {
let mut parts: Vec<String> = vec![mode.to_string()];
if let Some(ref p) = options.profile_name {
parts.push(crate::utils::paths::sanitize_for_path(p));
}
if let Some(ref s) = options.sanitizer {
parts.push(s.name().to_string());
}
parts.join("-")
};
let ccgo_build_root = project_root.join(&build_dir_name);
let cmake_build_dir = ccgo_build_root.join(&build_subdir).join(&platform_name);
let output_dir = project_root
.join("target")
.join(&build_subdir)
.join(&platform_name);
let package = config
.package
.as_ref()
.expect("Build requires [package] section in CCGO.toml");
let git_version = crate::utils::git_version::GitVersion::from_project_root(
&project_root,
&package.version,
)
.ok();
if let Some(header_rel) = config
.build
.as_ref()
.and_then(|b| b.verinfo_path.as_deref())
{
if let Some(gv) = git_version.as_ref() {
let identity = gv.veridentity(&package.version);
let _ = verinfo::generate(&project_root, header_rel, &package.name, &identity);
}
}
let (resolved_profile, profile_name_override) = {
let (mut base, name_override) = if let Some(ref pname) = options.profile_name {
match crate::builder::profile::resolve_profile(pname, &config.profile) {
Ok(rp) => {
let name_override = rp.name.clone();
(Some(rp), name_override)
}
Err(e) => panic!(
"BUG: profile '{pname}' was already validated by \
apply_profile_scalars but failed here: {e}"
),
}
} else {
(None, None)
};
if let Some(ref san) = options.sanitizer {
let san_rp = match crate::builder::profile::resolve_profile(
san.name(),
&config.profile,
) {
Ok(rp) => rp,
Err(_) => san.to_resolved_profile(),
};
if let Some(ref mut b) = base {
b.cmake.c_flags.extend_from_slice(&san_rp.cmake.c_flags);
b.cmake.cpp_flags.extend_from_slice(&san_rp.cmake.cpp_flags);
b.cmake.arguments.extend_from_slice(&san_rp.cmake.arguments);
for (plat, san_cmake) in &san_rp.platform_cmake {
let acc = b.platform_cmake.entry(plat.clone()).or_default();
acc.c_flags.extend_from_slice(&san_cmake.c_flags);
acc.cpp_flags.extend_from_slice(&san_cmake.cpp_flags);
acc.arguments.extend_from_slice(&san_cmake.arguments);
}
} else {
base = Some(san_rp);
}
}
(base, name_override)
};
Self {
project_root,
config,
options,
ccgo_build_root,
cmake_build_dir,
output_dir,
git_version,
resolved_profile,
profile_name_override,
}
}
pub fn lib_name(&self) -> &str {
if let Some(ref name) = self.profile_name_override {
return name.as_str();
}
&self
.config
.package
.as_ref()
.expect("Build requires [package] section")
.name
}
pub fn include_source_dir(&self) -> PathBuf {
if let Some(src) = self.config.include.as_ref().and_then(|c| c.src.as_deref()) {
self.project_root.join(src)
} else {
self.project_root.join("include")
}
}
pub fn version(&self) -> &str {
&self
.config
.package
.as_ref()
.expect("Build requires [package] section")
.version
}
pub fn publish_suffix(&self) -> &str {
""
}
pub fn version_with_suffix(&self) -> &str {
self.version()
}
pub fn jobs(&self) -> usize {
self.options.jobs.unwrap_or_else(num_cpus)
}
pub fn deps_map(&self) -> Option<String> {
let submodule_deps = self
.config
.build
.as_ref()
.map(|b| &b.submodule_deps)
.filter(|deps| !deps.is_empty())?;
let mut deps_list = Vec::new();
for (module, deps) in submodule_deps {
if !deps.is_empty() {
deps_list.push(module.clone());
deps_list.push(deps.join(","));
}
}
if deps_list.is_empty() {
None
} else {
Some(deps_list.join(";"))
}
}
pub fn symbol_visibility(&self) -> u8 {
if self
.config
.build
.as_ref()
.map(|b| b.symbol_visibility)
.unwrap_or(false)
{
1
} else {
0
}
}
fn global_platform_linkage(
&self,
platform: &str,
consumer: &crate::commands::build::LinkType,
) -> (Option<crate::config::Linkage>, Option<crate::config::Linkage>) {
use crate::commands::build::LinkType;
let p = self.config.platforms.as_ref();
macro_rules! extract {
($cfg:expr) => {{
let build_as = $cfg.and_then(|c| match consumer {
LinkType::Shared => c.dep_linkage_on_shared,
LinkType::Static => c.dep_linkage_on_static,
_ => None,
});
(build_as, $cfg.and_then(|c| c.default_dep_linkage))
}};
}
match platform {
"android" => extract!(p.and_then(|p| p.android.as_ref())),
"ios" => extract!(p.and_then(|p| p.ios.as_ref())),
"macos" => extract!(p.and_then(|p| p.macos.as_ref())),
"ohos" => extract!(p.and_then(|p| p.ohos.as_ref())),
"linux" => extract!(p.and_then(|p| p.linux.as_ref())),
"windows" => extract!(p.and_then(|p| p.windows.as_ref())),
_ => (None, None),
}
}
pub fn resolved_features(&self) -> Result<HashSet<String>> {
let features_config = &self.config.features;
if self.options.all_features {
let mut all = HashSet::new();
for name in features_config.feature_names() {
features_config.resolve_feature(name, &mut all)?;
}
features_config.resolve_feature("default", &mut all)?;
Ok(all)
} else {
features_config
.resolve_features(&self.options.features, self.options.use_default_features)
}
}
pub fn cmake_feature_defines(&self) -> Result<String> {
let resolved = self.resolved_features()?;
let defines: Vec<String> = resolved
.iter()
.filter(|f| !f.contains('/')) .map(|f| format!("CCGO_FEATURE_{}", f.to_uppercase().replace('-', "_")))
.collect();
Ok(defines.join(";"))
}
pub fn archive_builder(
&self,
platform: &str,
stl: &str,
) -> Result<crate::builder::archive::ArchiveBuilder> {
let mut archive = crate::builder::archive::ArchiveBuilder::new(
self.lib_name(),
self.version(),
self.publish_suffix(),
self.options.release,
platform,
self.output_dir.clone(),
)?;
if stl.ends_with("_static") {
archive = archive.with_variant("stdembed");
}
for variant in &self.options.variants {
archive = archive.with_variant(variant.as_str());
}
Ok(archive)
}
pub fn enabled_dependencies(&self) -> Result<Vec<&crate::config::DependencyConfig>> {
let resolved = self.resolved_features()?;
Ok(self
.config
.features
.get_enabled_optional_deps(&resolved, &self.config.dependencies))
}
pub fn ccgo_cmake_dir(&self) -> Option<PathBuf> {
if let Ok(cmake_dir) = cmake_templates::get_cmake_dir() {
return Some(cmake_dir);
}
if let Ok(dir) = std::env::var("CCGO_CMAKE_DIR") {
let path = PathBuf::from(dir);
if path.exists() {
return Some(path);
}
}
if let Ok(exe) = std::env::current_exe() {
if let Some(ccgo_rs_root) = exe
.parent()
.and_then(|p| p.parent())
.and_then(|p| p.parent())
{
let cmake_dir = ccgo_rs_root
.parent()
.map(|p| p.join("ccgo/ccgo/build_scripts/cmake"));
if let Some(ref dir) = cmake_dir {
if dir.exists() {
return cmake_dir;
}
}
}
}
if let Ok(output) = std::process::Command::new("pip3")
.args(["show", "-f", "ccgo"])
.output()
{
if output.status.success() {
let stdout = String::from_utf8_lossy(&output.stdout);
for line in stdout.lines() {
if let Some(location) = line.strip_prefix("Location: ") {
let cmake_dir =
PathBuf::from(location.trim()).join("ccgo/build_scripts/cmake");
if cmake_dir.exists() {
return Some(cmake_dir);
}
}
}
}
}
None
}
pub fn compiler_cache(&self) -> Option<cache::CacheConfig> {
let cache_option = self.options.cache.as_ref()?;
match cache_option.as_str() {
"none" | "disabled" | "off" => None,
"auto" => cache::CacheConfig::auto().ok(),
"ccache" => cache::CacheConfig::with_type(cache::CacheType::CCache).ok(),
"sccache" => cache::CacheConfig::with_type(cache::CacheType::SCache).ok(),
_ => {
eprintln!(
"⚠️ Unknown cache type '{}', using auto-detection",
cache_option
);
cache::CacheConfig::auto().ok()
}
}
}
pub fn config_hash(&self) -> Result<String> {
let config_path = self.project_root.join("CCGO.toml");
incremental::BuildState::hash_config(&config_path)
}
pub fn options_hash(&self) -> String {
let options_str = format!(
"target={:?},arch={:?},link={:?},release={},jobs={:?},features={:?},cache={:?}",
self.options.target,
self.options.architectures,
self.options.link_type,
self.options.release,
self.options.jobs,
self.options.features,
self.options.cache
);
incremental::BuildState::hash_options(&options_str)
}
pub fn resolved_dep_linkages(
&self,
platform: &str,
) -> anyhow::Result<Vec<(String, crate::builder::linkage::ResolvedLinkage)>> {
use crate::builder::linkage::{detect_dep_artifacts, resolve_linkage};
let platform = platform.to_lowercase();
let declared: std::collections::HashSet<&str> = self
.config
.dependencies
.iter()
.map(|d| d.name.as_str())
.collect();
for name in self.options.linkage_overrides.keys() {
if !declared.contains(name.as_str()) {
eprintln!(
"warning: --linkage {name}=<...> referenced an unknown dependency. \
'{name}' is not declared in [[dependencies]]; the override is ignored. \
Declared deps: {}",
if declared.is_empty() {
"(none)".to_string()
} else {
let mut names: Vec<&&str> = declared.iter().collect();
names.sort();
names
.iter()
.map(|s| s.to_string())
.collect::<Vec<_>>()
.join(", ")
}
);
}
}
let consumer = self.options.link_type.preferred_single();
let mut out = Vec::new();
for dep in &self.config.dependencies {
let dep_root = self.project_root.join(".ccgo/deps").join(&dep.name);
let artifacts = detect_dep_artifacts(&dep_root, &platform);
let hint = self.resolved_linkage_hint(dep, &platform, &consumer);
let resolved = resolve_linkage(consumer.clone(), artifacts, hint, &dep.name)?;
out.push((dep.name.clone(), resolved));
}
Ok(out)
}
fn profile_linkage_tiers(
&self,
platform: &str,
consumer: &crate::commands::build::LinkType,
) -> (
Option<crate::config::Linkage>,
Option<crate::config::Linkage>,
Option<crate::config::Linkage>,
Option<crate::config::Linkage>,
) {
use crate::commands::build::LinkType;
let (plat_build_as, plat_default) = self
.resolved_profile
.as_ref()
.and_then(|rp| rp.platform_dep_linkage.get(platform))
.map(|pdl| {
let build_as = match consumer {
LinkType::Shared => pdl.on_shared,
LinkType::Static => pdl.on_static,
_ => None,
};
(build_as, pdl.default)
})
.unwrap_or((None, None));
let global_build_as =
self.resolved_profile.as_ref().and_then(|rp| match consumer {
LinkType::Shared => rp.dep_linkage.on_shared,
LinkType::Static => rp.dep_linkage.on_static,
_ => None,
});
let global_default = self
.resolved_profile
.as_ref()
.and_then(|rp| rp.dep_linkage.default);
(plat_build_as, global_build_as, plat_default, global_default)
}
fn resolved_linkage_hint(
&self,
dep: &crate::config::DependencyConfig,
platform: &str,
consumer: &crate::commands::build::LinkType,
) -> Option<crate::config::Linkage> {
use crate::commands::build::LinkType;
let plat_dep = dep.platform_config(platform);
let (global_plat_build_as, global_plat_default) =
self.global_platform_linkage(platform, consumer);
let build_cfg = self.config.build.as_ref();
let global_build_as = build_cfg.and_then(|b| match consumer {
LinkType::Shared => b.dep_linkage_on_shared,
LinkType::Static => b.dep_linkage_on_static,
_ => None,
});
let global_default = build_cfg.and_then(|b| b.default_dep_linkage);
let (profile_plat_build_as, profile_global_build_as, profile_plat_default, profile_global_default) =
self.profile_linkage_tiers(platform, consumer);
let plat_dep_build_as = plat_dep.and_then(|p| match consumer {
LinkType::Shared => p.linkage_on_shared,
LinkType::Static => p.linkage_on_static,
_ => None,
});
let dep_build_as = match consumer {
LinkType::Shared => dep.linkage_on_shared,
LinkType::Static => dep.linkage_on_static,
_ => None,
};
let (cli_build_as_override, cli_build_as_default) = match consumer {
LinkType::Shared => (
self.options
.linkage_on_shared_overrides
.get(&dep.name)
.copied(),
self.options.linkage_on_shared_default,
),
LinkType::Static => (
self.options
.linkage_on_static_overrides
.get(&dep.name)
.copied(),
self.options.linkage_on_static_default,
),
_ => (None, None),
};
self.options
.linkage_overrides
.get(&dep.name)
.copied()
.or(self.options.linkage_default)
.or(cli_build_as_override)
.or(cli_build_as_default)
.or(plat_dep_build_as)
.or(dep_build_as)
.or_else(|| plat_dep.and_then(|p| p.linkage))
.or(dep.linkage)
.or(profile_plat_build_as)
.or(profile_global_build_as)
.or(profile_plat_default)
.or(profile_global_default)
.or(global_plat_build_as)
.or(global_build_as)
.or(global_plat_default)
.or(global_default)
}
fn resolved_materialize_hint(
&self,
dep: &crate::config::DependencyConfig,
platform: &str,
) -> Option<crate::config::Linkage> {
use crate::commands::build::LinkType;
let shared_hint = self.resolved_linkage_hint(dep, platform, &LinkType::Shared);
let static_hint = self.resolved_linkage_hint(dep, platform, &LinkType::Static);
if shared_hint == static_hint {
shared_hint
} else {
None
}
}
pub fn materialize_source_deps(&self, platform: &str) -> anyhow::Result<()> {
use crate::builder::materialize::{global_fingerprint_cache, materialize_source_deps_inner};
let dep_hints: Vec<(String, Option<crate::config::Linkage>)> = self
.config
.dependencies
.iter()
.map(|d| (d.name.clone(), self.resolved_materialize_hint(d, platform)))
.collect();
let archs: Vec<String> = self
.options
.architectures
.iter()
.map(|a| a.to_lowercase())
.collect();
let ccgo_bin =
std::env::current_exe().context("failed to resolve current ccgo binary path")?;
materialize_source_deps_inner(
&self.project_root,
platform,
&archs,
self.options.release,
&dep_hints,
ccgo_bin
.to_str()
.context("ccgo binary path is not UTF-8")?,
&global_fingerprint_cache(),
)
}
pub fn cmake_user_config(&self, platform: &str) -> crate::config::CmakeUserConfig {
use crate::config::CmakeUserConfig;
let global = self
.config
.build
.as_ref()
.and_then(|b| b.cmake.clone())
.unwrap_or_default();
let profile_global = self
.resolved_profile
.as_ref()
.map(|rp| CmakeUserConfig {
arguments: rp.cmake.arguments.clone(),
c_flags: rp.cmake.c_flags.clone(),
cpp_flags: rp.cmake.cpp_flags.clone(),
})
.unwrap_or_default();
let p = self.config.platforms.as_ref();
let plat_lower = platform.to_lowercase();
let platform_cfg: CmakeUserConfig = match plat_lower.as_str() {
"android" => p
.and_then(|p| p.android.as_ref())
.and_then(|a| a.build.as_ref())
.and_then(|b| b.cmake.clone())
.unwrap_or_default(),
"ios" => p
.and_then(|p| p.ios.as_ref())
.and_then(|a| a.build.as_ref())
.and_then(|b| b.cmake.clone())
.unwrap_or_default(),
"macos" => p
.and_then(|p| p.macos.as_ref())
.and_then(|a| a.build.as_ref())
.and_then(|b| b.cmake.clone())
.unwrap_or_default(),
"windows" => p
.and_then(|p| p.windows.as_ref())
.and_then(|a| a.build.as_ref())
.and_then(|b| b.cmake.clone())
.unwrap_or_default(),
"linux" => p
.and_then(|p| p.linux.as_ref())
.and_then(|a| a.build.as_ref())
.and_then(|b| b.cmake.clone())
.unwrap_or_default(),
"ohos" => p
.and_then(|p| p.ohos.as_ref())
.and_then(|a| a.build.as_ref())
.and_then(|b| b.cmake.clone())
.unwrap_or_default(),
_ => CmakeUserConfig::default(),
};
let profile_plat = self
.resolved_profile
.as_ref()
.and_then(|rp| rp.platform_cmake.get(plat_lower.as_str()))
.map(|rc| CmakeUserConfig {
arguments: rc.arguments.clone(),
c_flags: rc.c_flags.clone(),
cpp_flags: rc.cpp_flags.clone(),
})
.unwrap_or_default();
CmakeUserConfig {
arguments: [
global.arguments,
profile_global.arguments,
platform_cfg.arguments,
profile_plat.arguments,
]
.concat(),
c_flags: [
global.c_flags,
profile_global.c_flags,
platform_cfg.c_flags,
profile_plat.c_flags,
]
.concat(),
cpp_flags: [
global.cpp_flags,
profile_global.cpp_flags,
platform_cfg.cpp_flags,
profile_plat.cpp_flags,
]
.concat(),
}
}
pub fn cmake_user_files(&self, platform: &str) -> Option<Vec<std::path::PathBuf>> {
let global = self
.config
.build
.as_ref()
.and_then(|b| b.cmake_file.as_deref());
let p = self.config.platforms.as_ref();
let plat_lower = platform.to_lowercase();
let platform_val: Option<&str> = match plat_lower.as_str() {
"android" => p
.and_then(|p| p.android.as_ref())
.and_then(|a| a.build.as_ref())
.and_then(|b| b.cmake_file.as_deref()),
"ios" => p
.and_then(|p| p.ios.as_ref())
.and_then(|a| a.build.as_ref())
.and_then(|b| b.cmake_file.as_deref()),
"macos" => p
.and_then(|p| p.macos.as_ref())
.and_then(|a| a.build.as_ref())
.and_then(|b| b.cmake_file.as_deref()),
"windows" => p
.and_then(|p| p.windows.as_ref())
.and_then(|a| a.build.as_ref())
.and_then(|b| b.cmake_file.as_deref()),
"linux" => p
.and_then(|p| p.linux.as_ref())
.and_then(|a| a.build.as_ref())
.and_then(|b| b.cmake_file.as_deref()),
"ohos" => p
.and_then(|p| p.ohos.as_ref())
.and_then(|a| a.build.as_ref())
.and_then(|b| b.cmake_file.as_deref()),
_ => None,
};
match (global, platform_val) {
(None, None) => None,
(g, p) => {
if p == Some("") {
return Some(vec![]);
}
let mut files: Vec<std::path::PathBuf> = vec![];
if let Some(g) = g {
if !g.is_empty() {
files.push(self.project_root.join(g));
}
}
if let Some(p) = p {
if !p.is_empty() {
files.push(self.project_root.join(p));
}
}
Some(files)
}
}
}
pub fn create_incremental_analyzer(
&self,
link_type: &str,
) -> Result<incremental::IncrementalAnalyzer> {
let config_hash = self.config_hash()?;
let options_hash = self.options_hash();
incremental::IncrementalAnalyzer::new(
&self.cmake_build_dir,
self.lib_name().to_string(),
self.options.target.to_string(),
link_type.to_string(),
config_hash,
options_hash,
)
}
}
fn num_cpus() -> usize {
std::thread::available_parallelism()
.map(|p| p.get())
.unwrap_or(4)
}
#[derive(Debug, Serialize, Deserialize)]
pub struct BuildInfo {
pub project: String,
pub platform: String,
pub version: String,
pub link_type: String,
pub build_time: String,
pub build_host: String,
pub architectures: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub toolchain: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub git_commit: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub git_branch: Option<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct BuildInfoFull {
pub build_metadata: BuildMetadata,
pub project: ProjectInfo,
pub git: GitInfo,
pub build: BuildDetails,
pub environment: EnvironmentInfo,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct BuildMetadata {
pub version: String,
pub generated_at: String,
pub generator: String,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct ProjectInfo {
pub name: String,
pub version: String,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct GitInfo {
pub branch: String,
pub revision: String,
pub revision_full: String,
pub tag: String,
pub is_dirty: bool,
pub remote_url: String,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct BuildDetails {
pub time: String,
pub timestamp: i64,
pub platform: String,
#[serde(flatten)]
pub platform_info: Option<PlatformBuildInfo>,
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum PlatformBuildInfo {
Ios { ios: IosBuildInfo },
Android { android: AndroidBuildInfo },
Macos { macos: MacosBuildInfo },
}
#[derive(Debug, Serialize, Deserialize)]
pub struct IosBuildInfo {
pub xcode_version: String,
pub xcode_build: String,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct AndroidBuildInfo {
pub ndk_version: String,
pub stl: String,
pub min_sdk_version: String,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct MacosBuildInfo {
pub xcode_version: String,
pub xcode_build: String,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct EnvironmentInfo {
pub os: String,
pub os_version: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub python_version: Option<String>,
pub ccgo_version: String,
}
pub trait PlatformBuilder {
fn platform_name(&self) -> &str;
fn default_architectures(&self) -> Vec<String>;
fn validate_prerequisites(&self, ctx: &BuildContext) -> Result<()>;
fn build(&self, ctx: &BuildContext) -> Result<BuildResult>;
fn clean(&self, ctx: &BuildContext) -> Result<()>;
}
#[derive(Debug)]
pub struct BuildResult {
pub sdk_archive: PathBuf,
pub symbols_archive: Option<PathBuf>,
pub aar_archive: Option<PathBuf>,
pub duration_secs: f64,
pub architectures: Vec<String>,
}
#[cfg(test)]
mod tests {
use super::*;
use crate::commands::build::LinkType;
use crate::config::{BuildConfig, CcgoConfig, DependencyConfig, Linkage, PlatformLinkageConfig};
fn make_ctx(options: BuildOptions, config: CcgoConfig) -> BuildContext {
BuildContext {
project_root: PathBuf::from("/tmp/test"),
config,
options,
ccgo_build_root: PathBuf::from("/tmp/test/ccgo_build"),
cmake_build_dir: PathBuf::from("/tmp/test/ccgo_build"),
output_dir: PathBuf::from("/tmp/test/target"),
git_version: None,
resolved_profile: None,
profile_name_override: None,
}
}
fn bare_dep(name: &str) -> DependencyConfig {
DependencyConfig {
name: name.to_string(),
..Default::default()
}
}
fn bare_config() -> CcgoConfig {
toml::from_str("").expect("empty toml should parse")
}
fn bare_options() -> BuildOptions {
BuildOptions::default()
}
fn variant_ctx(toml: &str, features: &[&str], variants: &[&str]) -> BuildContext {
let toml = format!("[package]\nname = \"demo\"\nversion = \"5.1.0\"\n\n{toml}");
let config: CcgoConfig = toml::from_str(&toml).expect("toml should parse");
let mut options = bare_options();
options.features = features.iter().map(|s| s.to_string()).collect();
options.variants = variants.iter().map(|s| s.to_string()).collect();
make_ctx(options, config)
}
fn variant_name(ctx: &BuildContext) -> String {
ctx.archive_builder("Android", "c++_shared")
.expect("archive builder")
.archive_file_name(".zip")
}
#[test]
fn features_do_not_change_the_archive_name() {
let plain = variant_name(&variant_ctx("", &[], &[]));
let with_feature = variant_ctx("[features]\noversea = []\n", &["oversea"], &[]);
assert_eq!(variant_name(&with_feature), plain);
}
#[test]
fn cli_variant_is_what_names_the_archive() {
let ctx = variant_ctx("[features]\noversea = []\n", &["oversea"], &["oversea"]);
assert!(
variant_name(&ctx).contains("-OVERSEA"),
"expected -OVERSEA in {}",
variant_name(&ctx)
);
}
#[test]
fn dep_linkage_field_used_when_no_platform_or_build_as() {
let mut dep = bare_dep("leaf");
dep.linkage = Some(Linkage::StaticEmbedded);
let ctx = make_ctx(bare_options(), bare_config());
assert_eq!(
ctx.resolved_linkage_hint(&dep, "linux", &LinkType::Shared),
Some(Linkage::StaticEmbedded)
);
}
#[test]
fn dep_linkage_on_shared_beats_dep_linkage() {
let mut dep = bare_dep("leaf");
dep.linkage = Some(Linkage::SharedExternal);
dep.linkage_on_shared = Some(Linkage::StaticEmbedded);
let ctx = make_ctx(bare_options(), bare_config());
assert_eq!(
ctx.resolved_linkage_hint(&dep, "linux", &LinkType::Shared),
Some(Linkage::StaticEmbedded)
);
assert_eq!(
ctx.resolved_linkage_hint(&dep, "linux", &LinkType::Static),
Some(Linkage::SharedExternal)
);
}
#[test]
fn dep_linkage_on_static_beats_dep_linkage() {
let mut dep = bare_dep("leaf");
dep.linkage = Some(Linkage::SharedExternal);
dep.linkage_on_static = Some(Linkage::StaticExternal);
let ctx = make_ctx(bare_options(), bare_config());
assert_eq!(
ctx.resolved_linkage_hint(&dep, "linux", &LinkType::Static),
Some(Linkage::StaticExternal)
);
}
#[test]
fn platform_dep_linkage_beats_bare_dep_linkage() {
let mut dep = bare_dep("leaf");
dep.linkage = Some(Linkage::SharedExternal);
dep.android = Some(PlatformLinkageConfig {
linkage: Some(Linkage::StaticEmbedded),
linkage_on_shared: None,
linkage_on_static: None,
});
let ctx = make_ctx(bare_options(), bare_config());
assert_eq!(
ctx.resolved_linkage_hint(&dep, "android", &LinkType::Shared),
Some(Linkage::StaticEmbedded),
"android.linkage should win over dep.linkage for android platform"
);
assert_eq!(
ctx.resolved_linkage_hint(&dep, "ios", &LinkType::Shared),
Some(Linkage::SharedExternal),
);
}
#[test]
fn platform_dep_build_as_beats_platform_dep_linkage() {
let mut dep = bare_dep("leaf");
dep.android = Some(PlatformLinkageConfig {
linkage: Some(Linkage::SharedExternal),
linkage_on_shared: Some(Linkage::StaticEmbedded),
linkage_on_static: None,
});
let ctx = make_ctx(bare_options(), bare_config());
assert_eq!(
ctx.resolved_linkage_hint(&dep, "android", &LinkType::Shared),
Some(Linkage::StaticEmbedded),
"android.linkage_on_shared beats android.linkage"
);
assert_eq!(
ctx.resolved_linkage_hint(&dep, "android", &LinkType::Static),
Some(Linkage::SharedExternal),
"android.linkage used when linkage_on_static is absent"
);
}
#[test]
fn global_build_default_used_when_nothing_else_set() {
let dep = bare_dep("leaf");
let mut config = bare_config();
config.build = Some(BuildConfig {
default_dep_linkage: Some(Linkage::StaticExternal),
..Default::default()
});
let ctx = make_ctx(bare_options(), config);
assert_eq!(
ctx.resolved_linkage_hint(&dep, "linux", &LinkType::Shared),
Some(Linkage::StaticExternal)
);
}
#[test]
fn global_build_build_as_beats_global_build_default() {
let dep = bare_dep("leaf");
let mut config = bare_config();
config.build = Some(BuildConfig {
default_dep_linkage: Some(Linkage::SharedExternal),
dep_linkage_on_shared: Some(Linkage::StaticEmbedded),
dep_linkage_on_static: None,
..Default::default()
});
let ctx = make_ctx(bare_options(), config);
assert_eq!(
ctx.resolved_linkage_hint(&dep, "linux", &LinkType::Shared),
Some(Linkage::StaticEmbedded)
);
assert_eq!(
ctx.resolved_linkage_hint(&dep, "linux", &LinkType::Static),
Some(Linkage::SharedExternal)
);
}
#[test]
fn cli_per_dep_override_beats_all_toml() {
let mut dep = bare_dep("leaf");
dep.linkage = Some(Linkage::SharedExternal);
dep.android = Some(PlatformLinkageConfig {
linkage: Some(Linkage::StaticEmbedded),
linkage_on_shared: Some(Linkage::StaticEmbedded),
linkage_on_static: Some(Linkage::StaticEmbedded),
});
let mut options = bare_options();
options
.linkage_overrides
.insert("leaf".to_string(), Linkage::StaticExternal);
let ctx = make_ctx(options, bare_config());
assert_eq!(
ctx.resolved_linkage_hint(&dep, "android", &LinkType::Shared),
Some(Linkage::StaticExternal)
);
}
#[test]
fn cli_linkage_on_shared_default_beats_toml_dep_linkage_for_shared_consumer() {
let mut dep = bare_dep("leaf");
dep.linkage = Some(Linkage::SharedExternal);
let mut options = bare_options();
options.linkage_on_shared_default = Some(Linkage::StaticEmbedded);
let ctx = make_ctx(options, bare_config());
assert_eq!(
ctx.resolved_linkage_hint(&dep, "linux", &LinkType::Shared),
Some(Linkage::StaticEmbedded),
"--linkage-on-shared default should beat dep.linkage for shared consumer"
);
let ctx2 = make_ctx(
{
let mut o = bare_options();
o.linkage_on_shared_default = Some(Linkage::StaticEmbedded);
o
},
bare_config(),
);
assert_eq!(
ctx2.resolved_linkage_hint(&dep, "linux", &LinkType::Static),
Some(Linkage::SharedExternal),
);
}
#[test]
fn cli_linkage_on_shared_per_dep_beats_default() {
let dep = bare_dep("leaf");
let mut options = bare_options();
options.linkage_on_shared_default = Some(Linkage::SharedExternal);
options
.linkage_on_shared_overrides
.insert("leaf".to_string(), Linkage::StaticEmbedded);
let ctx = make_ctx(options, bare_config());
assert_eq!(
ctx.resolved_linkage_hint(&dep, "linux", &LinkType::Shared),
Some(Linkage::StaticEmbedded),
"--linkage-on-shared leaf=X should beat --linkage-on-shared default"
);
}
#[test]
fn cli_linkage_on_static_default_beats_toml_dep_linkage_for_static_consumer() {
let mut dep = bare_dep("leaf");
dep.linkage = Some(Linkage::SharedExternal);
let mut options = bare_options();
options.linkage_on_static_default = Some(Linkage::StaticExternal);
let ctx = make_ctx(options, bare_config());
assert_eq!(
ctx.resolved_linkage_hint(&dep, "linux", &LinkType::Static),
Some(Linkage::StaticExternal)
);
}
#[test]
fn cli_bare_linkage_still_beats_cli_build_as_linkage() {
let dep = bare_dep("leaf");
let mut options = bare_options();
options.linkage_default = Some(Linkage::SharedExternal);
options.linkage_on_shared_default = Some(Linkage::StaticEmbedded);
let ctx = make_ctx(options, bare_config());
assert_eq!(
ctx.resolved_linkage_hint(&dep, "linux", &LinkType::Shared),
Some(Linkage::SharedExternal),
);
}
#[test]
fn materialize_hint_returns_hint_when_shared_static_agree() {
let mut dep = bare_dep("leaf");
dep.linkage = Some(Linkage::StaticEmbedded);
let ctx = make_ctx(bare_options(), bare_config());
assert_eq!(
ctx.resolved_materialize_hint(&dep, "linux"),
Some(Linkage::StaticEmbedded)
);
}
#[test]
fn materialize_hint_returns_none_when_shared_static_differ() {
let mut dep = bare_dep("leaf");
dep.linkage_on_shared = Some(Linkage::SharedExternal);
dep.linkage_on_static = Some(Linkage::StaticEmbedded);
let ctx = make_ctx(bare_options(), bare_config());
assert_eq!(ctx.resolved_materialize_hint(&dep, "linux"), None);
}
#[test]
fn profile_release_applied_to_context() {
use crate::config::CcgoConfig;
use crate::builder::profile::resolve_profile;
let toml = r#"
[package]
name = "mylib"
version = "0.1.0"
[profile.mysanitize]
release = false
"#;
let config = CcgoConfig::parse(toml).unwrap();
let resolved = resolve_profile("mysanitize", &config.profile).unwrap();
assert_eq!(resolved.release, Some(false));
}
#[test]
fn cmake_user_config_includes_profile_flags() {
use crate::config::CcgoConfig;
let toml = r#"
[package]
name = "mylib"
version = "0.1.0"
[build.cmake]
arguments = ["-DGLOBAL=1"]
[profile.foo.cmake]
merge = "extend"
arguments = ["-DPROFILE=1"]
"#;
let config = CcgoConfig::parse(toml).unwrap();
let resolved = crate::builder::profile::resolve_profile("foo", &config.profile).unwrap();
let mut ctx = make_ctx(bare_options(), config);
ctx.resolved_profile = Some(resolved);
let cmake_cfg = ctx.cmake_user_config("linux");
assert!(cmake_cfg.arguments.contains(&"-DGLOBAL=1".to_string()));
assert!(cmake_cfg.arguments.contains(&"-DPROFILE=1".to_string()));
}
#[test]
fn profile_dep_linkage_default_applied() {
use crate::builder::profile::ResolvedProfile;
let config: CcgoConfig = toml::from_str(
r#"
[package]
name = "mylib"
version = "0.1.0"
"#,
)
.unwrap();
let dep = bare_dep("leafdep");
let mut profile = ResolvedProfile::default();
profile.dep_linkage.default = Some(Linkage::StaticEmbedded);
let mut ctx = make_ctx(bare_options(), config);
ctx.resolved_profile = Some(profile);
let hint = ctx.resolved_linkage_hint(&dep, "linux", &LinkType::Shared);
assert_eq!(hint, Some(Linkage::StaticEmbedded));
}
#[test]
fn cli_linkage_beats_profile_dep_linkage() {
use crate::builder::profile::ResolvedProfile;
let config: CcgoConfig = toml::from_str(
r#"
[package]
name = "mylib"
version = "0.1.0"
"#,
)
.unwrap();
let dep = bare_dep("leafdep");
let mut profile = ResolvedProfile::default();
profile.dep_linkage.default = Some(Linkage::StaticEmbedded);
let mut options = bare_options();
options.linkage_default = Some(Linkage::SharedExternal);
let mut ctx = make_ctx(options, config);
ctx.resolved_profile = Some(profile);
let hint = ctx.resolved_linkage_hint(&dep, "linux", &LinkType::Shared);
assert_eq!(hint, Some(Linkage::SharedExternal));
}
#[test]
fn full_profile_integration() {
use crate::config::CcgoConfig;
use crate::builder::profile::resolve_profile;
let toml = r#"
[package]
name = "mylib"
version = "0.1.0"
[build.cmake]
arguments = ["-DGLOBAL=1"]
[profile.debug]
release = false
[profile.sanitize]
inherits = "debug"
name = "mylib-asan"
jobs = 2
[profile.sanitize.cmake]
merge = "extend"
arguments = ["-DENABLE_ASAN=ON"]
[profile.sanitize.platforms.android.build.cmake]
merge = "replace"
arguments = ["-DANDROID_SPECIFIC=1"]
[profile.sanitize.dep_linkage]
default = "static-embedded"
"#;
let config = CcgoConfig::parse(toml).unwrap();
let rp = resolve_profile("sanitize", &config.profile).unwrap();
assert_eq!(rp.release, Some(false));
assert_eq!(rp.name.as_deref(), Some("mylib-asan"));
assert_eq!(rp.jobs, Some(2));
assert_eq!(rp.cmake.arguments, vec!["-DENABLE_ASAN=ON"]);
let android_cmake = rp.platform_cmake.get("android").unwrap();
assert_eq!(android_cmake.arguments, vec!["-DANDROID_SPECIFIC=1"]);
assert_eq!(rp.dep_linkage.default, Some(crate::config::Linkage::StaticEmbedded));
let mut ctx = make_ctx(bare_options(), config);
ctx.resolved_profile = Some(rp);
let cfg = ctx.cmake_user_config("linux");
assert_eq!(cfg.arguments, vec!["-DGLOBAL=1", "-DENABLE_ASAN=ON"]);
let cfg_android = ctx.cmake_user_config("android");
assert_eq!(cfg_android.arguments, vec!["-DGLOBAL=1", "-DENABLE_ASAN=ON", "-DANDROID_SPECIFIC=1"]);
}
const PATH_TEST_TOML: &str = r#"
[package]
name = "mylib"
version = "0.1.0"
"#;
#[test]
fn build_context_default_dir_no_profile() {
let config: CcgoConfig = CcgoConfig::parse(PATH_TEST_TOML).unwrap();
let options = BuildOptions {
target: BuildTarget::Android,
release: true,
..BuildOptions::default()
};
let ctx = BuildContext::new(PathBuf::from("/proj"), config, options);
assert_eq!(ctx.ccgo_build_root, PathBuf::from("/proj/ccgo_build"));
assert_eq!(ctx.cmake_build_dir, PathBuf::from("/proj/ccgo_build/release/android"));
}
#[test]
fn build_context_with_profile_uses_sanitized_subdir() {
let toml = r#"
[package]
name = "mylib"
version = "0.1.0"
[profile.my-profile]
"#;
let config: CcgoConfig = CcgoConfig::parse(toml).unwrap();
let options = BuildOptions {
target: BuildTarget::Android,
release: true,
profile_name: Some("my-profile".to_string()),
..BuildOptions::default()
};
let ctx = BuildContext::new(PathBuf::from("/proj"), config, options);
assert_eq!(ctx.ccgo_build_root, PathBuf::from("/proj/ccgo_build"));
assert_eq!(ctx.cmake_build_dir, PathBuf::from("/proj/ccgo_build/release-my-profile/android"));
}
#[test]
fn build_context_respects_custom_build_dir() {
let toml = r#"
[package]
name = "mylib"
version = "0.1.0"
[build]
build_dir = "my_build"
"#;
let config: CcgoConfig = CcgoConfig::parse(toml).unwrap();
let options = BuildOptions {
target: BuildTarget::Android,
release: true,
..BuildOptions::default()
};
let ctx = BuildContext::new(PathBuf::from("/proj"), config, options);
assert_eq!(ctx.ccgo_build_root, PathBuf::from("/proj/my_build"));
assert_eq!(ctx.cmake_build_dir, PathBuf::from("/proj/my_build/release/android"));
}
#[test]
fn build_context_custom_dir_and_profile_combined() {
let toml = r#"
[package]
name = "mylib"
version = "0.1.0"
[build]
build_dir = "my_build"
[profile.staging]
"#;
let config: CcgoConfig = CcgoConfig::parse(toml).unwrap();
let options = BuildOptions {
target: BuildTarget::Android,
release: false,
profile_name: Some("staging".to_string()),
..BuildOptions::default()
};
let ctx = BuildContext::new(PathBuf::from("/proj"), config, options);
assert_eq!(ctx.ccgo_build_root, PathBuf::from("/proj/my_build"));
assert_eq!(ctx.cmake_build_dir, PathBuf::from("/proj/my_build/debug-staging/android"));
}
#[test]
fn asan_flag_produces_debug_asan_subdir() {
let config: CcgoConfig = CcgoConfig::parse(PATH_TEST_TOML).unwrap();
let options = BuildOptions {
target: BuildTarget::Android,
release: false,
sanitizer: Some(sanitizer::SanitizerKind::Address),
..BuildOptions::default()
};
let ctx = BuildContext::new(PathBuf::from("/proj"), config, options);
assert_eq!(ctx.cmake_build_dir, PathBuf::from("/proj/ccgo_build/debug-asan/android"));
}
#[test]
fn tsan_flag_produces_debug_tsan_subdir() {
let config: CcgoConfig = CcgoConfig::parse(PATH_TEST_TOML).unwrap();
let options = BuildOptions {
target: BuildTarget::Android,
release: false,
sanitizer: Some(sanitizer::SanitizerKind::Thread),
..BuildOptions::default()
};
let ctx = BuildContext::new(PathBuf::from("/proj"), config, options);
assert_eq!(ctx.cmake_build_dir, PathBuf::from("/proj/ccgo_build/debug-tsan/android"));
}
#[test]
fn asan_injects_builtin_cmake_flags() {
let config: CcgoConfig = CcgoConfig::parse(PATH_TEST_TOML).unwrap();
let options = BuildOptions {
target: BuildTarget::Android,
release: false,
sanitizer: Some(sanitizer::SanitizerKind::Address),
..BuildOptions::default()
};
let ctx = BuildContext::new(PathBuf::from("/proj"), config, options);
let cmake_cfg = ctx.cmake_user_config("android");
assert!(cmake_cfg.c_flags.iter().any(|f| f.contains("-fsanitize=address")));
assert!(cmake_cfg.cpp_flags.iter().any(|f| f.contains("-fsanitize=address")));
assert!(cmake_cfg.arguments.iter().any(|a| a.contains("EXE_LINKER_FLAGS")));
}
#[test]
fn asan_user_profile_overrides_builtin() {
let toml = r#"
[package]
name = "mylib"
version = "0.1.0"
[profile.asan]
[profile.asan.cmake]
c_flags = ["-fsanitize=address", "-DCUSTOM=1"]
cpp_flags = ["-fsanitize=address", "-DCUSTOM=1"]
"#;
let config: CcgoConfig = CcgoConfig::parse(toml).unwrap();
let options = BuildOptions {
target: BuildTarget::Android,
release: false,
sanitizer: Some(sanitizer::SanitizerKind::Address),
..BuildOptions::default()
};
let ctx = BuildContext::new(PathBuf::from("/proj"), config, options);
let cmake_cfg = ctx.cmake_user_config("android");
assert!(cmake_cfg.c_flags.contains(&"-DCUSTOM=1".to_string()), "user profile should be used");
}
#[test]
fn asan_user_profile_cmake_flags_fully_replace_builtin() {
let toml = r#"
[package]
name = "mylib"
version = "0.1.0"
[profile.asan.cmake]
c_flags = ["-fsanitize=address", "-DUSER_FLAG=1"]
cpp_flags = ["-fsanitize=address", "-DUSER_FLAG=1"]
"#;
let config: CcgoConfig = CcgoConfig::parse(toml).unwrap();
let options = BuildOptions {
target: BuildTarget::Android,
release: false,
sanitizer: Some(sanitizer::SanitizerKind::Address),
..BuildOptions::default()
};
let ctx = BuildContext::new(PathBuf::from("/proj"), config, options);
let cmake_cfg = ctx.cmake_user_config("android");
assert!(cmake_cfg.c_flags.contains(&"-DUSER_FLAG=1".to_string()));
assert!(
cmake_cfg.arguments.is_empty(),
"user profile should replace built-in linker args; got {:?}",
cmake_cfg.arguments
);
}
#[test]
fn builtin_asan_linker_args_absent_when_user_profile_defines_asan() {
let toml = r#"
[package]
name = "mylib"
version = "0.1.0"
[profile.asan]
"#;
let config: CcgoConfig = CcgoConfig::parse(toml).unwrap();
let options = BuildOptions {
target: BuildTarget::Android,
release: false,
sanitizer: Some(sanitizer::SanitizerKind::Address),
..BuildOptions::default()
};
let ctx = BuildContext::new(PathBuf::from("/proj"), config, options);
let cmake_cfg = ctx.cmake_user_config("android");
assert!(cmake_cfg.c_flags.is_empty());
assert!(cmake_cfg.arguments.is_empty());
}
#[test]
fn profile_and_asan_compose_path() {
let toml = r#"
[package]
name = "mylib"
version = "0.1.0"
[profile.ci]
"#;
let config: CcgoConfig = CcgoConfig::parse(toml).unwrap();
let options = BuildOptions {
target: BuildTarget::Android,
release: false,
profile_name: Some("ci".to_string()),
sanitizer: Some(sanitizer::SanitizerKind::Address),
..BuildOptions::default()
};
let ctx = BuildContext::new(PathBuf::from("/proj"), config, options);
assert_eq!(
ctx.cmake_build_dir,
PathBuf::from("/proj/ccgo_build/debug-ci-asan/android")
);
}
#[test]
fn release_profile_and_tsan_compose_path() {
let toml = r#"
[package]
name = "mylib"
version = "0.1.0"
[profile.ci]
"#;
let config: CcgoConfig = CcgoConfig::parse(toml).unwrap();
let options = BuildOptions {
target: BuildTarget::Android,
release: true,
profile_name: Some("ci".to_string()),
sanitizer: Some(sanitizer::SanitizerKind::Thread),
..BuildOptions::default()
};
let ctx = BuildContext::new(PathBuf::from("/proj"), config, options);
assert_eq!(
ctx.cmake_build_dir,
PathBuf::from("/proj/ccgo_build/release-ci-tsan/android")
);
}
#[test]
fn profile_cmake_flags_extended_by_builtin_asan() {
let toml = r#"
[package]
name = "mylib"
version = "0.1.0"
[profile.ci.cmake]
merge = "extend"
c_flags = ["-DCI=1"]
cpp_flags = ["-DCI=1"]
"#;
let config: CcgoConfig = CcgoConfig::parse(toml).unwrap();
let options = BuildOptions {
target: BuildTarget::Android,
release: false,
profile_name: Some("ci".to_string()),
sanitizer: Some(sanitizer::SanitizerKind::Address),
..BuildOptions::default()
};
let ctx = BuildContext::new(PathBuf::from("/proj"), config, options);
let cmake_cfg = ctx.cmake_user_config("android");
assert!(cmake_cfg.c_flags.contains(&"-DCI=1".to_string()), "ci flag missing");
assert!(
cmake_cfg.c_flags.iter().any(|f| f.contains("-fsanitize=address")),
"asan c_flags missing"
);
assert!(
cmake_cfg.arguments.iter().any(|a| a.contains("EXE_LINKER_FLAGS")),
"asan linker args missing"
);
}
#[test]
fn profile_cmake_flags_extended_by_user_asan_profile() {
let toml = r#"
[package]
name = "mylib"
version = "0.1.0"
[profile.ci.cmake]
merge = "extend"
c_flags = ["-DCI=1"]
[profile.asan.cmake]
merge = "extend"
c_flags = ["-fsanitize=address", "-DASAN_CUSTOM=1"]
"#;
let config: CcgoConfig = CcgoConfig::parse(toml).unwrap();
let options = BuildOptions {
target: BuildTarget::Android,
release: false,
profile_name: Some("ci".to_string()),
sanitizer: Some(sanitizer::SanitizerKind::Address),
..BuildOptions::default()
};
let ctx = BuildContext::new(PathBuf::from("/proj"), config, options);
let cmake_cfg = ctx.cmake_user_config("android");
assert!(cmake_cfg.c_flags.contains(&"-DCI=1".to_string()), "ci flag missing");
assert!(cmake_cfg.c_flags.contains(&"-DASAN_CUSTOM=1".to_string()), "user asan flag missing");
}
}