Skip to content

Commit 41ccbe8

Browse files
committed
Feature-gate #[doc(cfg)] in generated code behind published_docs cfg
Will be used for our website's API docs and later crates.io. Needed because generated code in /target cannot be modified by tools like sed/sd anymore; this would re-run rustdoc. Instead, the code generator is already aware of any #[doc(cfg)] statements ending up in the output code. It gates them behind a --cfg that's enabled by the website CI.
1 parent d4a52fd commit 41ccbe8

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

godot-codegen/src/generator/classes.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,24 @@ fn make_class(class: &Class, ctx: &mut Context, view: &ApiView) -> GeneratedClas
9494
let api_level = class.api_level;
9595
let init_level = api_level.to_init_level();
9696

97-
// This is not strictly needed because the classes are included/excluded by codegen depending on that feature, however it helps the
98-
// docs pipeline which converts #[ cfg(...) ] to #[ doc(cfg(...)) ], resulting in "only available in ..." labels in the HTML output.
99-
// (The above has spaces because there's a primitive sed expression at work :) )
97+
// These attributes are for our nightly docs pipeline, which enables "only available in ..." labels in the HTML output. The website CI sets
98+
// RUSTFLAGS="--cfg published_docs" during the `cargo +nightly doc` invocation. They are applied to classes, interface traits, sidecar modules,
99+
// the notification enum, other enums and default-parameter extender structs.
100+
//
101+
// In other parts of the codebase, #[cfg] statements are replaced with #[doc(cfg)] using sed/sd. However, that doesn't work here, because
102+
// generated files are output in ./target/build/debug. Upon doing sed/sd replacements on these files, cargo doc will either treat them as
103+
// unchanged (doing nothing), or rebuild the generated files into a _different_ folder. Therefore, the generator itself must already provide
104+
// the correct attributes from the start.
100105
let (cfg_attributes, cfg_inner_attributes);
101106
if class.is_experimental {
102-
cfg_attributes = quote! { #[cfg(feature = "experimental-godot-api")] };
103-
cfg_inner_attributes = quote! { #![cfg(feature = "experimental-godot-api")] };
107+
cfg_attributes = quote! {
108+
// #[cfg(feature = "experimental-godot-api")]
109+
#[cfg_attr(published_docs, doc(cfg(feature = "experimental-godot-api")))]
110+
};
111+
cfg_inner_attributes = quote! {
112+
// #![cfg(feature = "experimental-godot-api")]
113+
#![cfg_attr(published_docs, doc(cfg(feature = "experimental-godot-api")))]
114+
};
104115
} else {
105116
cfg_attributes = TokenStream::new();
106117
cfg_inner_attributes = TokenStream::new();

0 commit comments

Comments
 (0)