Skip to content

Commit 45a6bd2

Browse files
Automatically assume that win32_ and unix_ should use the related cfg
1 parent 8d78966 commit 45a6bd2

15 files changed

+279
-48
lines changed

src/analysis/constants.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::borrow::Borrow;
22

3-
use crate::{config, env::Env, library, nameutil, traits::*, version::Version};
3+
use crate::{config, env::Env, library, nameutil, traits::*, update_cfgs, version::Version};
44

55
#[derive(Debug)]
66
pub struct Info {
@@ -49,6 +49,7 @@ pub fn analyze<F: Borrow<library::Constant>>(
4949
let cfg_condition = configured_constants
5050
.iter()
5151
.find_map(|c| c.cfg_condition.clone());
52+
let cfg_condition = update_cfgs::get_constant_cfg_condition(&constant.name, &cfg_condition);
5253

5354
let name = nameutil::mangle_keywords(&*constant.name).into_owned();
5455

src/analysis/functions.rs

+4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ use crate::{
3535
},
3636
nameutil,
3737
traits::*,
38+
update_cfgs,
3839
version::Version,
3940
};
4041

@@ -651,6 +652,9 @@ fn analyze_function(
651652
let cfg_condition = configured_functions
652653
.iter()
653654
.find_map(|f| f.cfg_condition.clone());
655+
let ns = env.library.namespace(ns_id);
656+
let cfg_condition =
657+
update_cfgs::get_cfg_condition(&func.name, &cfg_condition, &ns.symbol_prefixes);
654658
let doc_hidden = configured_functions.iter().any(|f| f.doc_hidden);
655659
let doc_trait_name = configured_functions
656660
.iter()

src/analysis/namespaces.rs

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub struct Namespace {
1414
pub higher_crate_name: String,
1515
pub package_names: Vec<String>,
1616
pub symbol_prefixes: Vec<String>,
17+
pub identifier_prefixes: Vec<String>,
1718
pub shared_libs: Vec<String>,
1819
pub versions: Vec<Version>,
1920
}
@@ -58,6 +59,7 @@ pub fn run(gir: &library::Library) -> Info {
5859
higher_crate_name,
5960
package_names: ns.package_names.clone(),
6061
symbol_prefixes: ns.symbol_prefixes.clone(),
62+
identifier_prefixes: ns.identifier_prefixes.clone(),
6163
shared_libs: ns.shared_library.clone(),
6264
versions: ns.versions.iter().copied().collect(),
6365
});

src/analysis/object.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::{
1212
library::{self, FunctionKind},
1313
nameutil::*,
1414
traits::*,
15+
update_cfgs,
1516
};
1617

1718
/// The location of an item within the object
@@ -302,6 +303,9 @@ pub fn class(env: &Env, obj: &GObject, deps: &[library::TypeId]) -> Option<Info>
302303
imports.add("glib::prelude::*");
303304
}
304305

306+
let ns = env.library.namespace(class_tid.ns_id);
307+
let cfg_condition =
308+
update_cfgs::get_object_cfg_condition(&name, &obj.cfg_condition, &ns.identifier_prefixes);
305309
let base = InfoBase {
306310
full_name,
307311
type_id: class_tid,
@@ -311,7 +315,7 @@ pub fn class(env: &Env, obj: &GObject, deps: &[library::TypeId]) -> Option<Info>
311315
imports,
312316
version,
313317
deprecated_version,
314-
cfg_condition: obj.cfg_condition.clone(),
318+
cfg_condition,
315319
concurrency: obj.concurrency,
316320
visibility: obj.visibility,
317321
};
@@ -428,6 +432,9 @@ pub fn interface(env: &Env, obj: &GObject, deps: &[library::TypeId]) -> Option<I
428432
deps,
429433
);
430434

435+
let ns = env.library.namespace(iface_tid.ns_id);
436+
let cfg_condition =
437+
update_cfgs::get_object_cfg_condition(&name, &obj.cfg_condition, &ns.identifier_prefixes);
431438
let base = InfoBase {
432439
full_name,
433440
type_id: iface_tid,
@@ -437,7 +444,7 @@ pub fn interface(env: &Env, obj: &GObject, deps: &[library::TypeId]) -> Option<I
437444
imports,
438445
version,
439446
deprecated_version,
440-
cfg_condition: obj.cfg_condition.clone(),
447+
cfg_condition,
441448
concurrency: obj.concurrency,
442449
visibility: obj.visibility,
443450
};

src/analysis/record.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::{
1212
library,
1313
nameutil::*,
1414
traits::*,
15+
update_cfgs,
1516
version::Version,
1617
};
1718

@@ -195,6 +196,10 @@ pub fn new(env: &Env, obj: &GObject) -> Option<Info> {
195196
}
196197
}
197198

199+
let ns = env.library.namespace(record_tid.ns_id);
200+
let cfg_condition =
201+
update_cfgs::get_object_cfg_condition(&name, &obj.cfg_condition, &ns.identifier_prefixes);
202+
198203
let base = InfoBase {
199204
full_name,
200205
type_id: record_tid,
@@ -204,7 +209,7 @@ pub fn new(env: &Env, obj: &GObject) -> Option<Info> {
204209
imports,
205210
version,
206211
deprecated_version,
207-
cfg_condition: obj.cfg_condition.clone(),
212+
cfg_condition,
208213
concurrency: obj.concurrency,
209214
visibility: obj.visibility,
210215
};

src/codegen/sys/fields.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::{
44
env::Env,
55
library::*,
66
traits::{IntoString, MaybeRefAs},
7+
update_cfgs,
78
};
89

910
pub struct Fields {
@@ -164,10 +165,13 @@ fn field_ffi_type(env: &Env, field: &Field) -> Result {
164165
}
165166

166167
fn get_gobject_cfg_condition(env: &Env, name: &str) -> Option<String> {
167-
let full_name = format!("{}.{}", env.namespaces.main().name, name);
168-
if let Some(obj) = env.config.objects.get(&full_name) {
169-
obj.cfg_condition.clone()
170-
} else {
171-
None
172-
}
168+
let ns = env.namespaces.main();
169+
let full_name = format!("{}.{}", ns.name, name);
170+
env.config
171+
.objects
172+
.get(&full_name)
173+
.and_then(|obj| {
174+
update_cfgs::get_object_cfg_condition(name, &obj.cfg_condition, &ns.identifier_prefixes)
175+
})
176+
.or_else(|| update_cfgs::get_object_cfg_condition(name, &None, &ns.identifier_prefixes))
173177
}

src/codegen/sys/functions.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::{
1010
env::Env,
1111
library, nameutil,
1212
traits::*,
13+
update_cfgs,
1314
};
1415

1516
// used as glib:get-type in GLib-2.0.gir
@@ -180,13 +181,19 @@ pub fn generate_other_funcs(
180181

181182
fn generate_cfg_configure(
182183
w: &mut dyn Write,
184+
env: &Env,
185+
function_name: &str,
183186
configured_functions: &[&Function],
184187
commented: bool,
185188
) -> Result<()> {
189+
let ns = env.namespaces.main();
186190
let cfg_condition_ = configured_functions
187191
.iter()
188-
.find_map(|f| f.cfg_condition.as_ref());
189-
cfg_condition(w, cfg_condition_, commented, 1)?;
192+
.find_map(|f| {
193+
update_cfgs::get_cfg_condition(function_name, &f.cfg_condition, &ns.symbol_prefixes)
194+
})
195+
.or_else(|| update_cfgs::get_cfg_condition(function_name, &None, &ns.symbol_prefixes));
196+
cfg_condition(w, cfg_condition_.as_ref(), commented, 1)?;
190197
Ok(())
191198
}
192199

@@ -224,7 +231,7 @@ fn generate_object_funcs(
224231
.max()
225232
.flatten();
226233
version_condition(w, env, None, version, false, 1)?;
227-
generate_cfg_configure(w, &configured_functions, false)?;
234+
generate_cfg_configure(w, env, glib_get_type, &configured_functions, false)?;
228235
writeln!(w, " pub fn {glib_get_type}() -> GType;")?;
229236
}
230237
}
@@ -254,7 +261,7 @@ fn generate_object_funcs(
254261

255262
version_condition(w, env, None, version, commented, 1)?;
256263
let name = func.c_identifier.as_ref().unwrap();
257-
generate_cfg_configure(w, &configured_functions, commented)?;
264+
generate_cfg_configure(w, env, name, &configured_functions, commented)?;
258265
writeln!(w, " {comment}pub fn {name}{sig};")?;
259266
}
260267

src/codegen/sys/lib_.rs

+70-18
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use crate::{
1414
library::*,
1515
nameutil::*,
1616
traits::*,
17+
update_cfgs,
1718
};
1819

1920
pub fn generate(env: &Env) {
@@ -164,7 +165,8 @@ fn generate_aliases(w: &mut dyn Write, env: &Env, items: &[&Alias]) -> Result<()
164165
writeln!(w, "// Aliases")?;
165166
}
166167
for item in items {
167-
let full_name = format!("{}.{}", env.namespaces.main().name, item.name);
168+
let ns = env.namespaces.main();
169+
let full_name = format!("{}.{}", ns.name, item.name);
168170
if !env.type_status_sys(&full_name).need_generate() {
169171
continue;
170172
}
@@ -176,8 +178,17 @@ fn generate_aliases(w: &mut dyn Write, env: &Env, items: &[&Alias]) -> Result<()
176178
.config
177179
.objects
178180
.get(&full_name)
179-
.and_then(|obj| obj.cfg_condition.as_ref());
180-
cfg_condition(w, cfg_condition_, false, 0)?;
181+
.and_then(|obj| {
182+
update_cfgs::get_object_cfg_condition(
183+
&item.name,
184+
&obj.cfg_condition,
185+
&ns.identifier_prefixes,
186+
)
187+
})
188+
.or_else(|| {
189+
update_cfgs::get_object_cfg_condition(&item.name, &None, &ns.identifier_prefixes)
190+
});
191+
cfg_condition(w, cfg_condition_.as_ref(), false, 0)?;
181192
writeln!(w, "{}pub type {} = {};", comment, item.c_identifier, c_type)?;
182193
}
183194
if !items.is_empty() {
@@ -224,13 +235,15 @@ fn generate_bitfields(w: &mut dyn Write, env: &Env, items: &[&Bitfield]) -> Resu
224235

225236
fn generate_constant_cfg_configure(
226237
w: &mut dyn Write,
238+
constant: &Constant,
227239
configured_constants: &[&constants::Constant],
228240
commented: bool,
229241
) -> Result<()> {
230242
let cfg_condition_ = configured_constants
231243
.iter()
232-
.find_map(|f| f.cfg_condition.as_ref());
233-
cfg_condition(w, cfg_condition_, commented, 1)?;
244+
.find_map(|f| update_cfgs::get_constant_cfg_condition(&constant.name, &f.cfg_condition))
245+
.or_else(|| update_cfgs::get_constant_cfg_condition(&constant.name, &None));
246+
cfg_condition(w, cfg_condition_.as_ref(), commented, 1)?;
234247
Ok(())
235248
}
236249

@@ -270,7 +283,12 @@ fn generate_constants(w: &mut dyn Write, env: &Env, constants: &[Constant]) -> R
270283

271284
if let Some(obj) = config {
272285
let configured_constants = obj.constants.matched(&full_name);
273-
generate_constant_cfg_configure(w, &configured_constants, !comment.is_empty())?;
286+
generate_constant_cfg_configure(
287+
w,
288+
constant,
289+
&configured_constants,
290+
!comment.is_empty(),
291+
)?;
274292
}
275293

276294
writeln!(
@@ -291,7 +309,8 @@ fn generate_enums(w: &mut dyn Write, env: &Env, items: &[&Enumeration]) -> Resul
291309
writeln!(w, "// Enums")?;
292310
}
293311
for item in items {
294-
let full_name = format!("{}.{}", env.namespaces.main().name, item.name);
312+
let ns = env.namespaces.main();
313+
let full_name = format!("{}.{}", ns.name, item.name);
295314
let config = env.config.objects.get(&full_name);
296315
if let Some(false) = config.map(|c| c.status.need_generate()) {
297316
continue;
@@ -300,8 +319,17 @@ fn generate_enums(w: &mut dyn Write, env: &Env, items: &[&Enumeration]) -> Resul
300319
.config
301320
.objects
302321
.get(&full_name)
303-
.and_then(|obj| obj.cfg_condition.as_ref());
304-
cfg_condition(w, cfg_condition_, false, 0)?;
322+
.and_then(|obj| {
323+
update_cfgs::get_object_cfg_condition(
324+
&item.name,
325+
&obj.cfg_condition,
326+
&ns.identifier_prefixes,
327+
)
328+
})
329+
.or_else(|| {
330+
update_cfgs::get_object_cfg_condition(&item.name, &None, &ns.identifier_prefixes)
331+
});
332+
cfg_condition(w, cfg_condition_.as_ref(), false, 0)?;
305333
writeln!(w, "pub type {} = c_int;", item.c_type)?;
306334
for member in &item.members {
307335
let member_config = config
@@ -317,7 +345,7 @@ fn generate_enums(w: &mut dyn Write, env: &Env, items: &[&Enumeration]) -> Resul
317345
continue;
318346
}
319347

320-
cfg_condition(w, cfg_condition_, false, 0)?;
348+
cfg_condition(w, cfg_condition_.as_ref(), false, 0)?;
321349
version_condition(w, env, None, version, false, 0)?;
322350
writeln!(
323351
w,
@@ -404,18 +432,32 @@ fn generate_interfaces_structs(
404432
writeln!(w, "// Interfaces")?;
405433
}
406434
for interface in interfaces {
407-
let full_name = format!("{}.{}", env.namespaces.main().name, interface.name);
435+
let ns = env.namespaces.main();
436+
let full_name = format!("{}.{}", ns.name, interface.name);
408437
if !env.type_status_sys(&full_name).need_generate() {
409438
continue;
410439
}
411440
let cfg_condition_ = env
412441
.config
413442
.objects
414443
.get(&full_name)
415-
.and_then(|obj| obj.cfg_condition.as_ref());
416-
cfg_condition(w, cfg_condition_, false, 0)?;
444+
.and_then(|obj| {
445+
update_cfgs::get_object_cfg_condition(
446+
&interface.name,
447+
&obj.cfg_condition,
448+
&ns.identifier_prefixes,
449+
)
450+
})
451+
.or_else(|| {
452+
update_cfgs::get_object_cfg_condition(
453+
&interface.name,
454+
&None,
455+
&ns.identifier_prefixes,
456+
)
457+
});
458+
cfg_condition(w, cfg_condition_.as_ref(), false, 0)?;
417459
generate_opaque_type(w, &interface.c_type)?;
418-
cfg_condition(w, cfg_condition_, false, 0)?;
460+
cfg_condition(w, cfg_condition_.as_ref(), false, 0)?;
419461
generate_debug_impl(
420462
w,
421463
&interface.c_type,
@@ -490,15 +532,25 @@ impl ::std::fmt::Debug for GHookList {
490532
}
491533

492534
fn generate_disguised(w: &mut dyn Write, env: &Env, record: &Record) -> Result<()> {
493-
let full_name = format!("{}.{}", env.namespaces.main().name, record.name);
535+
let ns = env.namespaces.main();
536+
let full_name = format!("{}.{}", ns.name, record.name);
494537
let cfg_condition_ = env
495538
.config
496539
.objects
497540
.get(&full_name)
498-
.and_then(|obj| obj.cfg_condition.as_ref());
499-
cfg_condition(w, cfg_condition_, false, 0)?;
541+
.and_then(|obj| {
542+
update_cfgs::get_object_cfg_condition(
543+
&record.name,
544+
&obj.cfg_condition,
545+
&ns.identifier_prefixes,
546+
)
547+
})
548+
.or_else(|| {
549+
update_cfgs::get_object_cfg_condition(&record.name, &None, &ns.identifier_prefixes)
550+
});
551+
cfg_condition(w, cfg_condition_.as_ref(), false, 0)?;
500552
generate_opaque_type(w, &format!("_{}", record.c_type))?;
501-
cfg_condition(w, cfg_condition_, false, 0)?;
553+
cfg_condition(w, cfg_condition_.as_ref(), false, 0)?;
502554
if record.pointer {
503555
writeln!(w, "pub type {name} = *mut _{name};", name = record.c_type)?;
504556
} else {

src/config/functions.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -344,10 +344,7 @@ impl Parse for Function {
344344
.lookup("version")
345345
.and_then(Value::as_str)
346346
.and_then(|s| s.parse().ok());
347-
let cfg_condition = toml
348-
.lookup("cfg_condition")
349-
.and_then(Value::as_str)
350-
.map(ToOwned::to_owned);
347+
let cfg_condition = super::get_cfg_condition(toml, object_name);
351348
let parameters = Parameters::parse(toml.lookup("parameter"), object_name);
352349
let ret = Return::parse(toml.lookup("return"), object_name);
353350
let doc_hidden = toml

src/config/gobjects.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -330,10 +330,7 @@ fn parse_object(
330330
.lookup("version")
331331
.and_then(Value::as_str)
332332
.and_then(|s| s.parse().ok());
333-
let cfg_condition = toml_object
334-
.lookup("cfg_condition")
335-
.and_then(Value::as_str)
336-
.map(ToOwned::to_owned);
333+
let cfg_condition = super::get_object_cfg_condition(toml_object, &name);
337334
let generate_trait = toml_object.lookup("trait").and_then(Value::as_bool);
338335
let final_type = toml_object
339336
.lookup("final_type")

0 commit comments

Comments
 (0)