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

std::file!/core::file! should normalize to using forward slashed path instead. #503

Open
stevefan1999-personal opened this issue Dec 11, 2024 · 0 comments

Comments

@stevefan1999-personal
Copy link

stevefan1999-personal commented Dec 11, 2024

I'm doing a sick hack to generate Rust callsite from C++ using export_name:

src/main.rs:

fn main() {
    println!("{:?}", unsafe { from_cpp() });
    println!("Hello, world!");
}

#[unsafe(no_mangle)]
#[unsafe(export_name = concat!("foo", "$", core::file!()))]
unsafe extern "C" fn foo(n: usize) -> usize { 
    println!("cpp passed us: {}", n);
    n + 1
}

unsafe extern "C" {
    fn from_cpp() ;
}

Then I can have it in C++:

foo.cpp

#include <stdio.h>

extern "C" int foo_src_main(int a) 
#ifdef _MSC_VER
__pragma(comment(linker, "/alternatename:foo_src_main=foo$src\\main.rs"))
#elif defined(__clang__)
asm("foo$src/main.rs")
#else 
asm("\"foo$src/main.rs\"")
#endif
;

extern "C"
{
    void from_cpp()
    {
        printf("rust gave us: %d\n", foo_src_main(1234));
    }
}

This is then linked with cc and then built as an executable and it worked. However on Windows, I noticed core::file!() actually expands to src\\main.rs. We should normalize this behavior so that we can go easy on the C++ call site code generator.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant