libs-team icon indicating copy to clipboard operation
libs-team copied to clipboard

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

Open stevefan1999-personal opened this issue 10 months ago • 0 comments

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.

stevefan1999-personal avatar Dec 11 '24 15:12 stevefan1999-personal