Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add --rename-world and --type-section-suffix options to C generator #805

Merged
merged 1 commit into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions crates/c/src/component_type_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ pub fn linking_symbol(name: &str) -> String {
format!("__component_type_object_force_link_{snake}")
}

pub fn object(resolve: &Resolve, world: WorldId, encoding: StringEncoding) -> Result<Vec<u8>> {
pub fn object(
resolve: &Resolve,
world: WorldId,
world_name: &str,
encoding: StringEncoding,
suffix: Option<&str>,
) -> Result<Vec<u8>> {
let mut module = Module::new();

// Build a module with one function that's a "dummy function"
Expand Down Expand Up @@ -41,8 +47,7 @@ pub fn object(resolve: &Resolve, world: WorldId, encoding: StringEncoding) -> Re
// otherwise is attempted to be unique here to ensure that this doesn't get
// concatenated to other custom sections by LLD by accident since LLD will
// concatenate custom sections of the same name.
let world_name = &resolve.worlds[world].name;
let section_name = format!("component-type:{world_name}");
let section_name = format!("component-type:{world_name}{}", suffix.unwrap_or(""));

// Add our custom section
module.section(&CustomSection {
Expand Down
35 changes: 26 additions & 9 deletions crates/c/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ pub struct Opts {
/// Rename the interface `K` to `V` in the generated source code.
#[cfg_attr(feature = "clap", arg(long, name = "K=V", value_parser = parse_rename))]
pub rename: Vec<(String, String)>,

/// Rename the world in the generated source code and file names.
#[cfg_attr(feature = "clap", arg(long))]
pub rename_world: Option<String>,

/// Add the specified suffix to the name of the custome section containing
/// the component type.
#[cfg_attr(feature = "clap", arg(long))]
pub type_section_suffix: Option<String>,
}

#[cfg(feature = "clap")]
Expand Down Expand Up @@ -107,8 +116,11 @@ enum Scalar {

impl WorldGenerator for C {
fn preprocess(&mut self, resolve: &Resolve, world: WorldId) {
let name = &resolve.worlds[world].name;
self.world = name.to_string();
self.world = self
.opts
.rename_world
.clone()
.unwrap_or_else(|| resolve.worlds[world].name.clone());
self.sizes.fill(resolve);
self.world_id = Some(world);

Expand Down Expand Up @@ -237,10 +249,9 @@ impl WorldGenerator for C {
}

fn finish(&mut self, resolve: &Resolve, id: WorldId, files: &mut Files) {
let world = &resolve.worlds[id];
let linking_symbol = component_type_object::linking_symbol(&world.name);
let linking_symbol = component_type_object::linking_symbol(&self.world);
self.include("<stdlib.h>");
let snake = world.name.to_snake_case();
let snake = self.world.to_snake_case();
uwrite!(
self.src.c_adapters,
"
Expand Down Expand Up @@ -335,7 +346,7 @@ impl WorldGenerator for C {
#define __BINDINGS_{0}_H
#ifdef __cplusplus
extern \"C\" {{",
world.name.to_shouty_snake_case(),
self.world.to_shouty_snake_case(),
);

// Deindent the extern C { declaration
Expand Down Expand Up @@ -419,9 +430,15 @@ impl WorldGenerator for C {
if !self.opts.no_object_file {
files.push(
&format!("{snake}_component_type.o",),
component_type_object::object(resolve, id, self.opts.string_encoding)
.unwrap()
.as_slice(),
component_type_object::object(
resolve,
id,
&self.world,
self.opts.string_encoding,
self.opts.type_section_suffix.as_deref(),
)
.unwrap()
.as_slice(),
);
}
}
Expand Down
Loading