Skip to content

Commit ca141a1

Browse files
committed
Allow building as dependency on docs.rs with no features enabled
1 parent f072277 commit ca141a1

File tree

4 files changed

+30
-11
lines changed

4 files changed

+30
-11
lines changed

android-activity/build.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![allow(dead_code)]
2-
31
fn build_glue_for_game_activity() {
42
let android_games_sdk =
53
std::env::var("ANDROID_GAMES_SDK").unwrap_or_else(|_err| "android-games-sdk".to_string());
@@ -94,6 +92,21 @@ fn build_glue_for_game_activity() {
9492
}
9593

9694
fn main() {
97-
#[cfg(feature = "game-activity")]
98-
build_glue_for_game_activity();
95+
// Enable Cargo's change-detection to avoid re-running build script if
96+
// irrelvant parts changed. Using build.rs here is just a dummy used to
97+
// disable the default "rerun on every change" behaviour Cargo has.
98+
println!("cargo:rerun-if-changed=build.rs");
99+
100+
if cfg!(feature = "game-activity") {
101+
build_glue_for_game_activity();
102+
}
103+
104+
// Whether this is used directly in or as a dependency on docs.rs.
105+
//
106+
// `cfg(docsrs)` cannot be used, since it's only set for the crate being
107+
// built, and not for any dependent crates.
108+
println!("cargo:rustc-check-cfg=cfg(used_on_docsrs)");
109+
if std::env::var("DOCS_RS").is_ok() {
110+
println!("cargo:rustc-cfg=used_on_docsrs");
111+
}
99112
}

android-activity/src/game_activity/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![cfg(feature = "game-activity")]
2-
31
use std::collections::HashMap;
42
use std::marker::PhantomData;
53
use std::ops::Deref;

android-activity/src/lib.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ compile_error!(
141141
);
142142
#[cfg(all(
143143
not(any(feature = "game-activity", feature = "native-activity")),
144-
not(doc)
144+
not(any(doc, used_on_docsrs)),
145145
))]
146146
compile_error!(
147147
r#"Either "game-activity" or "native-activity" must be enabled as features
@@ -160,8 +160,18 @@ You may need to add a `[patch]` into your Cargo.toml to ensure a specific versio
160160
android-activity is used across all of your application's crates."#
161161
);
162162

163-
#[cfg_attr(any(feature = "native-activity", doc), path = "native_activity/mod.rs")]
164-
#[cfg_attr(any(feature = "game-activity", doc), path = "game_activity/mod.rs")]
163+
#[cfg_attr(feature = "native-activity", path = "native_activity/mod.rs")]
164+
#[cfg_attr(feature = "game-activity", path = "game_activity/mod.rs")]
165+
#[cfg_attr(
166+
all(
167+
// No activities enabled.
168+
not(any(feature = "native-activity", feature = "game-activity")),
169+
// And building docs.
170+
any(doc, used_on_docsrs),
171+
),
172+
// Fall back to documenting native activity.
173+
path = "native_activity/mod.rs"
174+
)]
165175
pub(crate) mod activity_impl;
166176

167177
pub mod error;

android-activity/src/native_activity/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![cfg(any(feature = "native-activity", doc))]
2-
31
use std::collections::HashMap;
42
use std::marker::PhantomData;
53
use std::panic::AssertUnwindSafe;

0 commit comments

Comments
 (0)