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

Cross-platform replacement for c_compiler.lua? #516

Open
hugomg opened this issue Apr 21, 2022 · 13 comments
Open

Cross-platform replacement for c_compiler.lua? #516

hugomg opened this issue Apr 21, 2022 · 13 comments
Labels
enhancement New feature or request

Comments

@hugomg
Copy link
Member

hugomg commented Apr 21, 2022

If we ever want to make Pallene more cross-platform, the c_compiler.lua part will be one of the sticking points. Currently, windows support is completely missing and the macOS support is not actively tested. (None of the devs still use macOS and it is not tested in our continuous integration). This is all very unfortunate, and it is a problem area that is out of our area of expertise. We should consider delegating this functionality to someone else. Perhaps Luarocks? It knows how to compile C code in a cross-platform way...

@hugomg hugomg added the enhancement New feature or request label Apr 21, 2022
hugomg added a commit that referenced this issue May 26, 2022
To address #516, I'm planning to use Luarocks as a compilation backend. If we set things up the right way, we should be able to use Luarocks to compile our c files into shared libraries, in a cross-platform manner.

This PR has some CFLAGS-related changes to pave the way for that. When using Luarocks to build C modules, the only way to pass CFLAGS is via the CFLAGS environment variable. So what this commit does is take the flags we were passing directly in the c_compiler.lua and passing them via CFLAGS environment variable instead. This implies in a change in behavior. By default we will no longer compile with -g and -Wall. IMO, that might actually be a good thing. The -g causes larger executables and the -Wall is not relevant to end-users. (I'm still leaving the -Wall in the ./run-tests though)
@Mehgugs
Copy link

Mehgugs commented Oct 23, 2022

You could use zig's c compiler frontend to do the cross compilation. You can invoke zig cc with the same options as clang/gcc and compile C code on any platform zig supports (targeting any platform zig supports).

I personally think using luarocks is not the solution because the onus is on the user to teach luarocks how to compile C modules on their system, for windows users this often requires a lot of extra work.

@hugomg
Copy link
Member Author

hugomg commented Oct 23, 2022

Could you talk more about why Luarocks would be more difficult on Windows? I thought Luarocks also knew how to compile on Windows?

Also, another reason we were thinking about Luarocks is that it knows where to find the Lua interpreters and header files. Even if we used zig cc, maybe we'd still need some help from Luarocks to sort out the compilation flags.

@Mehgugs
Copy link

Mehgugs commented Oct 23, 2022

Well given that pallene wants you to build a customized version of lua from source why not just have it be built as part of the set up?
Then you can make sure all the headers (plus luacore.h) are known to the pallene system.

EDIT:

In regards to setup on windows my main sticking point is having someone compile lua from source sourcing their own compiler toolchain, rather than specifically running the luarocks installer (which happens after you've got lua + a compiler ready).

@hugomg
Copy link
Member Author

hugomg commented Oct 23, 2022

Sorry, I'm not sure I understand 100%. Currently we have to install the custom Lua by hand and the Pallene compiler using Luarocks. Are you suggesting that the whole thing (custom Lua + pallene compiler) should be packaged as a Luarocks rock? Or that we should ditch Luarocks and install the compiler with a custom installation script? Or something else?

@Mehgugs
Copy link

Mehgugs commented Oct 23, 2022

Okay so what I'm saying is:

zig cc is a good compiler frontend for cross platform c compilation. It is also very easy to use on windows.

I would like this project to be usable without luarocks since I – and many other prospective users I imagine – already have lua environments set up. Zig's c compiler seems like the best way to achieve cross platform c compilation without assuming luarocks has been set up by the user beforehand.

@hugomg
Copy link
Member Author

hugomg commented Oct 23, 2022

I see. I suppose we can consider Zig if at some point we decide that we should bundle a C compiler into the project.

In the mean time, one escape valve that already exists is that you can compile Pallene to C using --emit-c. This is 100% portable and then you can compile the C code however you want.

@srijan-paul
Copy link
Member

None of the devs still use macOS and it is not tested in our continuous integration

I haven't actively used Pallene in quite some time, but continue to follow activity on the repository.
I'm currently using MacOS (M1) for work.
Attempting to install lua-internals throws the following error on apple clang:

gcc -std=gnu99 -o lua   lua.o liblua.a -lm -Wl,-E -ldl -lreadline
gcc -std=gnu99 -o luac   luac.o liblua.a -lm -Wl,-E -ldl -lreadline
ld: unknown option: -E
ld: unknown option: -E
clangclang: : error: linker command failed with exit code 1 (use -v to see invocation)
error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [luac] Error 1
make[2]: *** Waiting for unfinished jobs....
make[2]: *** [lua] Error 1
make[1]: *** [linux-readline] Error 2
make: *** [linux-readline] Error 2

I guess a good place to start would be to make the installation process cross-platform.

@hugomg
Copy link
Member Author

hugomg commented Oct 28, 2022

Hi Srijan, nice to hear from you again! How are you doing? :)

By any chance, did you compile with make linux-readline? If so, then make macos might work. (And then I presume we should update the README to tell people about that).

@srijan-paul
Copy link
Member

srijan-paul commented Oct 30, 2022

I'm doing fine, thanks for asking! :D
And yeah, compiling with macosx did work.
I was able to compile Pallene as well.

However ./run-test throws the following error:

binarytrees / pallene
./benchmarks/benchlib.lua:33: command failed: make --quiet -f benchmarks/Makefile 'benchmarks/binarytrees/pallene.so'

stack traceback:
	./benchmarks/benchlib.lua:33: in function 'benchmarks.benchlib.prepare_benchmark'
	./benchmarks/benchlib.lua:226: in function 'benchmarks.benchlib.run_with_impl_name'
	spec/benchmarks_spec.lua:34: in function <spec/benchmarks_spec.lua:33>

--- Lua Lint ---
/opt/homebrew/opt/lua/bin/lua5.4: cannot open /Users/srijanpaul/.luarocks/lib/luarocks/rocks-5.4/luacheck/0.25.0-1/bin/luacheck: No such file or directory

If I try to compile only 1 file however, it does work:

Screenshot 2022-10-30 at 5 06 42 PM

I just posted it here as soon as I ran into the error, so I might be able to resolve this with some digging.

@Nomarian
Copy link

I see. I suppose we can consider Zig if at some point we decide that we should bundle a C compiler into the project.

In the mean time, one escape valve that already exists is that you can compile Pallene to C using --emit-c. This is 100% portable and then you can compile the C code however you want.

Not that portable, currently luacore.h contains non C code, (gnu awk script), so not only is it nonportable, its only usable on gnu systems.

@hugomg
Copy link
Member Author

hugomg commented Jul 24, 2023

Is that AWK script using something that only works on GNU? The plan was to work with any AWK.

In any case, the AWK script is only used to generate luacore.h. If we distribute that in the tarball then the end user wouldn't need AWK. To be honest, when it comes to portability I'm more worried about the C99 code we're using here and there.

@Nomarian
Copy link

Nomarian commented Jul 24, 2023

Yes, BEGINFILE is gawk specific, it can probably be replaced by a NR==1 {}.

Also, now I see why there was awk code in my luacore.h, it probably emitted itself because the awk code was incorrect?

@hugomg
Copy link
Member Author

hugomg commented Jul 24, 2023

Interesting. Let's move the AWK discussion to pallene-lang/lua-internals#1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants