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 support for OSX #96

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 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
17 changes: 16 additions & 1 deletion bindgen/src/gen_cs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,25 @@ impl Config {
}

pub fn cdylib_name(&self) -> String {
self.cdylib_name
#[cfg(target_os = "macos")]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the branches are identical?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only change is on the macos branch, i thought macos, windows and linux may be treated differently in the future so i kept three branches. if you think that that's not necessary i can remove the windows branch

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, I'm blind. I think it would be better to write the code in some different way, to avoid these copy-pasted lines. It's kinda difficult to see what's the difference, but I think it's like that just in diff. When I looked at the changes without diff, it was more clear.

This branching makes the generated bindings dependent on the environment they are generated, i.e. you can't use Mac to generate bindings that will work on Windows. Are you sure that's the desired behaviour for your use case?

I tried to run the tests locally and it's working without .dylib suffix. Can you test again without .dylib suffix?

Copy link
Author

@ChristianBelloni ChristianBelloni Oct 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I first tested it the loading failed complaining that the shared lib couldn't be found, it's been a minute since I did any interop in c# so I assumed it didn't handle extensions, as soon as I can I'll test it again and see if it works.

In case the lib still cannot be found would you be open to #if-ing the dllimport declarations to support platform specific extensions?

Something like

<PropertyGroup>
    <IsWindows Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Windows)))' == 'true'">true</IsWindows>
    <IsOSX Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::OSX)))' == 'true'">true</IsOSX>
</PropertyGroup>
<PropertyGroup Condition="'$(IsWindows)'=='true'">
    <DefineConstants>Windows</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(IsOSX)'=='true'">
    <DefineConstants>OSX</DefineConstants>
</PropertyGroup>
#if OSX
[DllImport("lib.dylib")]
#else
[DllImport("lib")]
#endif
public static extern void myFunc();

Obviously I'd add Linux too, I'm keeping it short here.

I'm writing on my phone so if it's messed up I'll fix the formatting in the morning.

Thank you for taking the time to review this 😊

return self
.cdylib_name
.as_ref()
.expect("`cdylib_name` not specified")
.clone()
+ ".dylib";
#[cfg(target_os = "windows")]
return self
.cdylib_name
.as_ref()
.expect("`cdylib_name` not specified")
.clone();
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
return self
.cdylib_name
.as_ref()
.expect("`cdylib_name` not specified")
.clone();
}

pub fn access_modifier(&self) -> String {
Expand Down
10 changes: 9 additions & 1 deletion generate_bindings.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,12 @@ GEN_DIR="dotnet-tests/UniffiCS/gen"
rm -rf "$GEN_DIR"
mkdir -p "$GEN_DIR"

target/debug/uniffi-bindgen-cs target/debug/libuniffi_fixtures.so --library --out-dir="$GEN_DIR" --no-format
libname=""

if [[ $(uname) == "Darwin" ]]; then
libname="libuniffi_fixtures.dylib"
else
libname="libuniffi_fixtures.so"
fi

target/debug/uniffi-bindgen-cs "target/debug/$libname" --library --out-dir="$GEN_DIR" --no-format
5 changes: 5 additions & 0 deletions test_bindings.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,10 @@ SCRIPT_DIR="${SCRIPT_DIR:-$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>
SOLUTION_DIR="dotnet-tests"

export LD_LIBRARY_PATH="$SCRIPT_DIR/target/debug/:${LD_LIBRARY_PATH:-}"

if [[ $(uname) == "Darwin" ]]; then
export DYLD_LIBRARY_PATH="$SCRIPT_DIR/target/debug/:${DYLD_LIBRARY_PATH:-}"
fi

cd $SOLUTION_DIR
dotnet test -l "console;verbosity=normal"
Loading