janet-lang / janet

A dynamic language and bytecode vm
https://janet-lang.org
MIT License
3.52k stars 227 forks source link

Support building native modules from other languages with jpm #709

Closed ghost closed 3 months ago

ghost commented 3 years ago

jpm is able to compile native modules for use in artifacts, but currently only supports those modules being written in one language: C. There are other languages, such as Zig or Rust, that people may also want to use, but integrating them into the jpm build system requires hacks. I propose we remove the assumptions that impose this restriction.

I see two ways this could be done:

andrewchambers commented 3 years ago

I'm not sure i would classify other language support as hacks, it's more like some languages come with builtin 'make' rules and others don't. Since both rust and zig do their own fine grained dependency tracking, simply making jpm call out to those tools seems more reasonable than trying to reverse engineer how they build things.

Perhaps jpm needs something like an 'externally built native' and the tool just needs to arrange for a .a and .so to be put in a specific place.

It's a good thing to think about, these languages have lots of good libraries that janet can leverage.

sogaiu commented 3 years ago

A side note -- perhaps it's well-understood, but IIUC there is C++ support (in addition to the aforementioned C). See: https://github.com/janet-lang/janet/blob/master/CHANGELOG.md#1155---2021-04-25

bakpakin commented 3 years ago

So jpm's general rules system is quite general and language agnostic, but yes all of the built in declare-* functions (any function that generates rules) are tailored for C and C++. The question is whether or not things like declare-native should accept/auto-detect language, or should we just have functions like declare-zig-native. I can see arguments for both.

I can perhaps see the desire to mix languages in one declare call to generate a single .so/.a/binary file, but I don't think it is as simple as it is made out to be above - the linker needs to be aware of all of the languages and how they would interact. C and C++ work together because we can just say "if any of the files are C++ files, just use c++ instead of cc." I image other languages wouldn't be that much harder, but it would be a case-by-case basis.

A function like declare-zig-native is much easier to implement because we wouldn't have to worry about how to handle other C and C++ source files unless we wanted to.

uvtc commented 3 years ago

Another language that I could imagine being of interest for this kind of thing: Drew Devault's new not-yet-released (or even named?) systems language: https://drewdevault.com/2021/03/19/A-new-systems-language.html (more sample code here).

GrayJack commented 3 years ago

I was thinking the same, implementing declare-{lang}-native would be easier

tionis commented 4 months ago

Could this be solved by the changes done/proposed in #1444 (bundle/* and related)?

bakpakin commented 3 months ago

Yeah, for now closing this. The bundle/ doesn't directly solve this, but makes it clear that interfacing with arbitrary compiler toolchains is outside the scope of what Janet can do out of the box.