Skip to content

Commit

Permalink
Panic Instead Of Mismatched Types Error (#568)
Browse files Browse the repository at this point in the history
## Describe the changes

This PR adds conditional compilation attributes to the
`cuda_include_path` and `cuda_lib_path` functions to handle cases where
the target OS is neither Windows nor Linux. Previously, the functions
returned mismatched types, causing compilation errors when the target OS
was unsupported. The added code now explicitly panics with a descriptive
error message in such cases, improving code clarity and error handling.

### Changes made:

1. **`cuda_include_path` function:**
- Added `#[cfg(not(any(target_os = "windows", target_os = "linux")))]`
conditional attribute to handle unsupported OS cases.
- Included a panic with the message "Currently, ICICLE can only be built
for Windows or Linux".

2. **`cuda_lib_path` function:**
- Added `#[cfg(not(any(target_os = "windows", target_os = "linux")))]`
conditional attribute to handle unsupported OS cases.
- Included a panic with the message "Currently, ICICLE can only be built
for Windows or Linux".

### Example of new code:
```rust
fn cuda_include_path() -> &'static str {
    #[cfg(target_os = "windows")]
    {
        concat!(env!("CUDA_PATH"), "\\include")
    }

    #[cfg(target_os = "linux")]
    {
        "/usr/local/cuda/include"
    }

    #[cfg(not(any(target_os = "windows", target_os = "linux")))]
    {
        panic!("Currently, ICICLE can only be built for Windows or Linux")
    }
}

fn cuda_lib_path() -> &'static str {
    #[cfg(target_os = "windows")]
    {
        concat!(env!("CUDA_PATH"), "\\lib\\x64")
    }

    #[cfg(target_os = "linux")]
    {
        "/usr/local/cuda/lib64"
    }

    #[cfg(not(any(target_os = "windows", target_os = "linux")))]
    {
        panic!("Currently, ICICLE can only be built for Windows or Linux")
    }
}
```

### Previous error resolved:

Before these changes, attempting to compile for unsupported OSes
resulted in the following errors:
```
error[E0308]: mismatched types
 --> /path/to/icicle-cuda-runtime/build.rs:5:27
  |
5 | fn cuda_include_path() -> &'static str {
  |    -----------------      ^^^^^^^^^^^^ expected `&str`, found `()`
  |    |
  |    implicitly returns `()` as its body has no tail or `return` expression

error[E0308]: mismatched types
 --> /path/to/icicle-cuda-runtime/build.rs:17:23
  |
17 | fn cuda_lib_path() -> &'static str {
  |    -------------      ^^^^^^^^^^^^ expected `&str`, found `()`
  |    |
  |    implicitly returns `()` as its body has no tail or `return` expression
```

The added conditions now prevent these errors by explicitly handling
unsupported OS cases with a panic message.
  • Loading branch information
emirsoyturk authored Aug 22, 2024
1 parent d54be39 commit 2e2f9cc
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions wrappers/rust/icicle-cuda-runtime/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ fn cuda_include_path() -> &'static str {
{
"/usr/local/cuda/include"
}

#[cfg(not(any(target_os = "windows", target_os = "linux")))]
{
panic!("Currently, ICICLE can only be built for Windows or Linux")
}
}

fn cuda_lib_path() -> &'static str {
Expand All @@ -24,6 +29,11 @@ fn cuda_lib_path() -> &'static str {
{
"/usr/local/cuda/lib64"
}

#[cfg(not(any(target_os = "windows", target_os = "linux")))]
{
panic!("Currently, ICICLE can only be built for Windows or Linux")
}
}

fn main() {
Expand Down

0 comments on commit 2e2f9cc

Please sign in to comment.