denoland / deno_bindgen

Write high-level Deno FFI libraries in Rust.
MIT License
275 stars 28 forks source link

feat: Support build target for multiple platforms #70

Closed idranme closed 2 years ago

idranme commented 2 years ago

At present, the compiled library can only be used for a single platform. If you want to support cross platform, you need to build multiple sets of code. This adds a lot of tedious work.

idranme commented 2 years ago

It would be better to generate unified glue code after building libraries for multiple platforms.

littledivy commented 2 years ago

You can cross-compile to other platforms, deno_bindgen invokes cargo build internally - arguments after -- are passed to Cargo. It's a common practice to use Github CI to compile and distribute via Github releases. https://github.com/denoland/deno_bindgen#cli

This is similar to using similar workflows for publishing NAPI modules with napi-rs For example, see https://github.com/littledivy/deno_audio/blob/main/.github/workflows/release.yml

If you don't want to use the CI, consider using cross or similar tools to achieve Rust cross-compilation.

It would be better to generate unified glue code after building libraries for multiple platforms.

I'm not sure what you mean by "unified glue code". It is already unified / same for all platforms.

idranme commented 2 years ago

You can cross-compile to other platforms, deno_bindgen invokes cargo build internally - arguments after -- are passed to Cargo. It's a common practice to use Github CI to compile and distribute via Github releases. https://github.com/denoland/deno_bindgen#cli

This is similar to using similar workflows for publishing NAPI modules with napi-rs For example, see https://github.com/littledivy/deno_audio/blob/main/.github/workflows/release.yml

If you don't want to use the CI, consider using cross or similar tools to achieve Rust cross-compilation.

It would be better to generate unified glue code after building libraries for multiple platforms.

I'm not sure what you mean by "unified glue code". It is already unified / same for all platforms.

thanks, I have a problem

idranme commented 2 years ago
let libSuffix = "";
switch (Deno.build.os) {
  case "windows":
    libSuffix = "dll";
    break;
  case "darwin":
    libSuffix = "dylib";
    break;
  case "linux":
    libSuffix = "so";
    break;
}

const libName = `./libadd.${libSuffix}`;
// Open library and define exported symbols
const dylib = Deno.dlopen(libName, {
  "add": { parameters: ["isize", "isize"], result: "isize" },
});
idranme commented 2 years ago

"bindings.ts" may be able to judge that the platform loads the corresponding library like the above code

littledivy commented 2 years ago

It does that already using x/plug: https://deno.land/x/plug@0.5.1/plug.ts#L35

idranme commented 2 years ago

It does that already using x/plug: https://deno.land/x/plug@0.5.1/plug.ts#L35

well, thanks