ThomasMertes / seed7

Source code of Seed7
GNU General Public License v2.0
224 stars 12 forks source link

Cross compilation? #12

Open Ruulul opened 1 year ago

Ruulul commented 1 year ago

I dont see any option for compile to another target in the s7c options. Is it planned/possible?

Cross compilation feels like something primordial to a modern compiled language to me, specially when you enforces that the code is portable across multiple targets... we should be able to compile to them from the same environment.

Just asking, amazing project, found yesterday and I am following the manual.

ThomasMertes commented 1 year ago

The Seed7 compiler uses a C compiler as back-end. Seed7 can cross compile if there are multiple C compilers (for different targets) or a C compiler that can cross-compile.

Currently cross compilation is supported for compilation to JavaScript/web-assembly (with Emscripten). When Seed7 is compiled with Emscripten (makefile mk_emccl.mak for Linux and mk_emccw.mak for Windows) preparations for cross compilation are done:

If Seed7 has been compiled for the local machine and for JavaScript/web-assembly the Seed7 compiler can be used in two ways:

Basically this concept can be extended to other cross compilation situations.

renatoathaydes commented 1 year ago

Could you let the C compiler be configurable (along with compiler flags), so Zig could be used and consequently you would get cross-compilation to any target for free?

ThomasMertes commented 1 year ago

Could you let the C compiler be configurable (along with compiler flags), so Zig could be used ...

I assume you are referring to the Seed7 compiler that generates C.

The Seed7 compiler is configurable but it always generates C (tailored for a specific C compiler and operating system). The settings for the generation of C code and for the invocation of the C compiler are in the library cc_conf.s7i. The current settings can be displayed with s7 confval. I have doubts that this configuration values can be extended such that a different language (not C) can be generated.

Changing the Seed7 compiler to emit another language would be a significant effort. Probably a complete rewrite would be necessary. If I would do that I would aim for a much more common platform such as JAVA or the JVM. Supporting the JVM as platform would also be possible by using a C compiler that targets the JVM. Unfortunately such C compilers are highly experimental and not well maintained.

renatoathaydes commented 1 year ago

I should've been more clear. The Zig compiler can (cross-)compile C code.

https://andrewkelley.me/post/zig-cc-powerful-drop-in-replacement-gcc-clang.html

Uber is using Zig to cross compiler their C code and they probably don't use Zig as a language (yet).

To configure Seed7 to use zig, you basically need to replace clang with zig cc, plus allow passing cross-compilation options if desired.

renatoathaydes commented 1 year ago

In the linked blog post, Andrew Kelly, author of Zig, shows how to cross compile a C project, LuaJIT:

$ git clean -xfdq
$ export CC="zig cc -target aarch64-linux-gnu"
$ export HOST_CC="zig cc"
$ make CC="$CC" HOST_CC="$HOST_CC" TARGET_STRIP="echo"

It works so easily in this case because the LuaJIT makefile allowed for "plugging in" another C compiler (using CC), and allowed passing custom options to the compiler.

What I am suggesting is that you make compilation in Seed7 work similarly.

renatoathaydes commented 1 year ago

The cross compiler can even emit WASM, besides a large number of OS/architectures.

https://ziglang.org/documentation/0.10.1/#WASI

ThomasMertes commented 1 year ago

I should've been more clear. The Zig compiler can (cross-)compile C code.

I see what you mean:

zig cc -o hello.exe hello.c -target x86_64-windows-gnu

would compile to a Windows executable and

zig cc -o hello hello.c -target mipsel-linux-musl

would compile to a Linux executable for the MIPS processor with the MUSL run-time library.

It works so easily in this case because the LuaJIT makefile allowed for "plugging in" another C compiler (using CC), and allowed passing custom options to the compiler.

The makefiles of Seed7 use also CC. Additionally they contain also C compiler specific options. So changing CC might not be enough.

But if zig cc is like clang you could just copy mk_clang.mak to mk_zigcc.mak and set CC to zig cc. If you do make -f mk_zigcc.mak depend the program chkccomp.c will determine how good zig cc supports the functionality needed by Seed7. Finally the settings used for zig cc could be saved to a file and this file could be used for cross-compilation with the Seed7 compiler option -c.

Seed7 does not just use C but also the C run-time library and many operating system specific functions.

The cross compiler can even emit WASM, besides a large number of OS/architectures.

WASM is already supported by Seed7. Take a look at mk_emccl.mak and mk_emccw.mak. They use the Emscripten C compiler. As mentioned further above this is supported with the cross-compilation option -c emcc.

The Seed7 compiler is prepared to switch between different C compilers (and their run-time libraries) with the option -c. A file with settings is searched for in the bin directory. In case of Emscripten the option -e emcc takes settings from the file bin/cc_conf_emcc.prop.