Skip to content

Commit

Permalink
feat: new c compiler: zig cc
Browse files Browse the repository at this point in the history
  • Loading branch information
jhwgh1968 authored and Gankra committed Jul 18, 2024
1 parent 79a0b8d commit f1220cf
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 9 deletions.
34 changes: 34 additions & 0 deletions src/toolchains/c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ enum CCFlavor {
Clang,
Gcc,
Msvc,
Zigcc,
}

#[derive(PartialEq)]
Expand Down Expand Up @@ -87,6 +88,7 @@ impl Toolchain for CcToolchain {
"gcc" => self.compile_gcc(src_path, out_dir, lib_name),
"clang" => self.compile_clang(src_path, out_dir, lib_name),
"msvc" => self.compile_msvc(src_path, out_dir, lib_name),
"zigcc" => self.compile_zigcc(src_path, out_dir, lib_name),
_ => unimplemented!("unknown c compiler"),
}
}
Expand All @@ -102,6 +104,7 @@ impl Toolchain for CcToolchain {
"gcc" => self.compile_gcc(src_path, out_dir, lib_name),
"clang" => self.compile_clang(src_path, out_dir, lib_name),
"msvc" => self.compile_msvc(src_path, out_dir, lib_name),
"zigcc" => self.compile_zigcc(src_path, out_dir, lib_name),
_ => unimplemented!("unknown c compiler"),
}
}
Expand Down Expand Up @@ -273,6 +276,7 @@ impl CcToolchain {
TOOLCHAIN_GCC => CCFlavor::Gcc,
TOOLCHAIN_CLANG => CCFlavor::Clang,
TOOLCHAIN_MSVC => CCFlavor::Msvc,
TOOLCHAIN_ZIGCC => CCFlavor::Zigcc,
TOOLCHAIN_CC => {
let compiler = cc::Build::new()
.cargo_metadata(false)
Expand Down Expand Up @@ -369,6 +373,36 @@ impl CcToolchain {
Ok(String::from(lib_name))
}

fn compile_zigcc(
&self,
src_path: &Utf8Path,
out_dir: &Utf8Path,
lib_name: &str,
) -> Result<String, BuildError> {
let obj_path = out_dir.join(format!("{lib_name}.o"));
let lib_path = out_dir.join(format!("lib{lib_name}.a"));
let mut cmd = Command::new("zig");
cmd.arg("cc");
for flag in self.extra_flags() {
cmd.arg(flag);
}
cmd.arg("-ffunction-sections")
.arg("-fdata-sections")
.arg("-fPIC")
.arg("-o")
.arg(&obj_path)
.arg("-c")
.arg(src_path)
.status()?;
Command::new("ar")
.arg("cq")
.arg(&lib_path)
.arg(&obj_path)
.status()?;
Command::new("ar").arg("s").arg(&lib_path).status()?;
Ok(String::from(lib_name))
}

fn compile_gcc(
&self,
src_path: &Utf8Path,
Expand Down
16 changes: 8 additions & 8 deletions src/toolchains/c/declare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ impl CcToolchain {
CCFlavor::Gcc => Err(UnsupportedError::Other(
"GCC isn't known to support f16 on this target".to_owned(),
))?,
CCFlavor::Clang
CCFlavor::Clang | CCFlavor::Zigcc
if cfg!(any(
all(target_arch = "x86", target_feature = "sse2"),
target_arch = "x86_64",
Expand All @@ -106,7 +106,7 @@ impl CcToolchain {
{
"_Float16 "
}
CCFlavor::Clang => Err(UnsupportedError::Other(
CCFlavor::Clang | CCFlavor::Zigcc => Err(UnsupportedError::Other(
"Clang isn't known to support f16 on this target".to_owned(),
))?,
CCFlavor::Msvc => Err(UnsupportedError::Other(
Expand Down Expand Up @@ -134,7 +134,7 @@ impl CcToolchain {
CCFlavor::Gcc => Err(UnsupportedError::Other(
"GCC isn't known to support f128 on this target".to_owned(),
))?,
CCFlavor::Clang
CCFlavor::Clang | CCFlavor::Zigcc
if cfg!(any(
target_arch = "x86",
target_arch = "x86_64",
Expand All @@ -152,7 +152,7 @@ impl CcToolchain {
{
"__float128 "
}
CCFlavor::Clang => Err(UnsupportedError::Other(
CCFlavor::Clang | CCFlavor::Zigcc => Err(UnsupportedError::Other(
"Clang isn't known to support f128 on this target".to_owned(),
))?,
CCFlavor::Msvc => Err(UnsupportedError::Other(
Expand Down Expand Up @@ -555,7 +555,7 @@ impl CcToolchain {
if self.platform == Windows {
match self.cc_flavor {
Msvc => "__cdecl ",
Gcc | Clang => "__attribute__((cdecl)) ",
Gcc | Clang | Zigcc => "__attribute__((cdecl)) ",
}
} else {
return Err(self.unsupported_convention(&convention))?;
Expand All @@ -565,7 +565,7 @@ impl CcToolchain {
if self.platform == Windows {
match self.cc_flavor {
Msvc => "__stdcall ",
Gcc | Clang => "__attribute__((stdcall)) ",
Gcc | Clang | Zigcc => "__attribute__((stdcall)) ",
}
} else {
return Err(self.unsupported_convention(&convention))?;
Expand All @@ -575,7 +575,7 @@ impl CcToolchain {
if self.platform == Windows {
match self.cc_flavor {
Msvc => "__fastcall ",
Gcc | Clang => "__attribute__((fastcall)) ",
Gcc | Clang | Zigcc => "__attribute__((fastcall)) ",
}
} else {
return Err(self.unsupported_convention(&convention))?;
Expand All @@ -585,7 +585,7 @@ impl CcToolchain {
if self.platform == Windows {
match self.cc_flavor {
Msvc => "__vectorcall ",
Gcc | Clang => "__attribute__((vectorcall)) ",
Gcc | Clang | Zigcc => "__attribute__((vectorcall)) ",
}
} else {
return Err(self.unsupported_convention(&convention))?;
Expand Down
9 changes: 8 additions & 1 deletion src/toolchains/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,15 @@ pub const TOOLCHAIN_CC: &str = "cc";
pub const TOOLCHAIN_GCC: &str = "gcc";
pub const TOOLCHAIN_CLANG: &str = "clang";
pub const TOOLCHAIN_MSVC: &str = "msvc";
pub const TOOLCHAIN_ZIGCC: &str = "zigcc";

const C_TOOLCHAINS: &[&str] = &[TOOLCHAIN_CC, TOOLCHAIN_GCC, TOOLCHAIN_CLANG, TOOLCHAIN_MSVC];
const C_TOOLCHAINS: &[&str] = &[
TOOLCHAIN_CC,
TOOLCHAIN_GCC,
TOOLCHAIN_CLANG,
TOOLCHAIN_MSVC,
TOOLCHAIN_ZIGCC,
];

/// A compiler/language toolchain!
pub trait Toolchain {
Expand Down

0 comments on commit f1220cf

Please sign in to comment.