-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
build.zig: Add ability to install a shared library #3680
Conversation
Shouldn't this be defining |
I didn't know about this constant, let me rework this a bit. Thanks! |
It's required to build a usable DLL. |
d0faccc
to
24b609d
Compare
Initial build.zig only installed a static library. This is fine for Zig itself but not sufficient for other ecosystems. Current patch doesn't break backwards compatibility but does a little refactoring where the old function addRaylib uses the newly added function addRaylibTo, which is more flexible. In fact, addRaylib is an alias for addStaticRaylib. addRaylib still generates a static library, whereas the newly added addSharedRaylib generates a shared library. Additionally, since a shared library is also a common artifact, build.zig also installs it to a default location of zig-out/lib/libraylib.dll. Example code how to generate a shared library artifact in Zig: const libraylib = raylib.addSharedRaylib(b, target, optimize, .{}); // Define a USE_LIBTYPE_SHARED macro for your project that is used // in raylib.h when using a shared library. Your project must use // raylib.h in order for this macro to work. It is not necessary // but saves performance when using on Windows. For more see: // raysan5#3680 (comment) my_project.defineCMacro("USE_LIBTYPE_SHARED", null);
Initial build.zig only installed a static library. This is fine for Zig itself but not sufficient for other ecosystems. Current patch doesn't break backwards compatibility but does a little refactoring where the old function addRaylib uses the newly added function addRaylibTo, which is more flexible. In fact, addRaylib is an alias for addStaticRaylib. addRaylib still generates a static library, whereas the newly added addSharedRaylib generates a shared library. Additionally, since a shared library is also a common artifact, build.zig also installs it to a default location of zig-out/lib/libraylib.dll. Example code how to generate a shared library artifact in Zig: const libraylib = raylib.addSharedRaylib(b, target, optimize, .{}); // Define a USE_LIBTYPE_SHARED macro for your project that is used // in raylib.h when using a shared library. Your project must use // raylib.h in order for this macro to work. It is not necessary // but saves performance when using on Windows. For more see: // raysan5#3680 (comment) my_project.defineCMacro("USE_LIBTYPE_SHARED", null);
24b609d
to
334f34e
Compare
@greenfork It seems this is a considerable redesign of the I'm not using neither maintaining this script but I think other users should review this PR. |
@Peter0x44 I have addressed your comment and also ported Lines 386 to 397 in e46b614
In order to port |
It does change a lot of lines, but conceptually it is not so scary. The patch essentially does these things:
The result is that Zig can now build a shared library and, more importantly, cross-compile it to different systems. Users of Raylib can now develop on Linux and cross-compile a dynamic library for Windows. For me this is a very essential functionality because my language of choice can only use shared C libraries and I would like to do cross-compilation of my games from Linux to Windows. PS: The cross-build for MacOS is broken at the moment both on master and on this branch, this will require some input from MacOS users. |
@raysan5 should I find someone or there's someone already and I should just wait? |
Also, I have a question about the goal of |
@greenfork The goal of I will ask in raylib Discord for any Zig experts that can review and evaluate this change. |
Line 5: To make a proper alias, you can just do pub const addRaylib = addStaticRaylib; Lines 25 and 46: Should use Line 76: Not sure if this flag is needed, if you throw it away, you can throw away all the arena allocation since all other flags are comptime and you can merge them with ++ like it was done originally. Or could roll it into common flags since I don't think it does anything when building a static library. Then you can also get rid of the error handling altogether in the wrapping functions. Lines 267+: I don't think you need to install headers for each library type since the headers are the same. Can get rid of the whole loop and just replace it with one extra installArtifact instead. |
As far as I'm concerned. I think adding a basic build script for Zig that can create a shared library is alright but I don't think it should go beyond just basic functionality. I use the provided build script for quick and dirty, get things setup fast and then migrate to writing my own customized build scripts as my projects get out of prototype state. Since the code is already written, and could be cleaned up with a few steps then it could be fine to accept it but as a Zig user, I wouldn't need this functionality myself. Zig can very well use the library produced by make script with no issues, and fewer things to maintain is better. |
I have read about the
My main reason for this was easy cross-compilation. Zig allows for that with ease. Unfortunately I failed to create bindings for Pony because of some strange interaction between the Pony runtime and Raylib. This patch requires more work to do and I will leave it for the next volunteer, closing this one. |
My understanding of -fvisibility=hidden is that it doesn't affect windows at all GCC has a bit of special handling for dlls that lets you access them without the I believe macos dylibs work the same way, but I'm not 100% on that, and I can't test it. |
Initial build.zig only installed a static library. This is fine for
Zig itself but not sufficient for other ecosystems.
Current patch doesn't break backwards compatibility but does a little
refactoring where the old function addRaylib uses the newly added
function addRaylibTo, which is more flexible. In fact, addRaylib is an
alias for addStaticRaylib. addRaylib still generates a static library,
whereas the newly added addSharedRaylib generates a shared library.
Additionally, since a shared library is also a common artifact,
build.zig also installs it to a default location of
zig-out/lib/libraylib.dll.
Example code how to generate a shared library artifact in Zig: