electronstudio / raylib-python-cffi

Python CFFI bindings for Raylib
http://electronstudio.github.io/raylib-python-cffi
Eclipse Public License 2.0
142 stars 29 forks source link

Windows Build Script #1

Closed Pebaz closed 4 years ago

Pebaz commented 5 years ago

This is what I've got so far based off of the Linux/MacOS scripts but it's not building successufully.

from cffi import FFI

ffibuilder = FFI()
ffibuilder.cdef(open("raylib_modified.h").read().replace('RLAPI ', ''))
ffibuilder.set_source("_raylib_cffi",
    """
    #include "raylib.h"   // the C header of the library
    """,
    extra_link_args=['/NODEFAULTLIB:MSVCRTD'],
    libraries=['raylib', 'gdi32', 'shell32', 'user32','OpenGL32'],
)

if __name__ == "__main__":
    ffibuilder.compile(verbose=True)

Oddly enough, I'm getting "undefined externals for Raylib functions even though it successfully adds the raylib.lib...?

electronstudio commented 5 years ago

Im using latest Raylib from Github, probably should have specified an exact version in docs. I guess you’re using an older released version?

Pebaz commented 5 years ago

Oh gotcha (was using 2.0).

I downloaded the prebuild binary of Raylib 2.5 (which strangely is labeled 2.0...) and nearly all of the "undefined externals" went away.

image

I'm so close but I really don't like to build anything from source on Windows because it never goes well. Once this happens, it should get way easier though for certain tasks but not necessarily for a Windows-specific build. :/

Getting:

_raylib_cffi.obj : error LNK2001: unresolved external symbol TextCountCodepoints
_raylib_cffi.obj : error LNK2001: unresolved external symbol GetNextCodepoint
_raylib_cffi.obj : error LNK2001: unresolved external symbol DrawLineStrip
_raylib_cffi.obj : error LNK2001: unresolved external symbol SetVrConfiguration
_raylib_cffi.obj : error LNK2001: unresolved external symbol DrawTriangleFan
electronstudio commented 5 years ago

I will give it a go myself as soon as I get windows setup. What do I need to install? I think python expects extensions to be compiled in visual studio 2017 v14 but I’m not sure what packages I need.

I’m also going to try to do more work on the dynamic bindings so compiling won’t be necessary. (Currently they don’t work)

Pebaz commented 5 years ago

It would definitely be awesome not to have to compile ;)

As for compiling on Windows, I used Visual Studio 2019 (v16.0.1) with Python 3.7.1.

I'm not sure what else you need to do for that, I would install Numpy/Numba or something else that probably requires local compilation in order to test your system before trying out the script.

Pebaz commented 5 years ago

Also, here is my configuration that worked:

image

electronstudio commented 5 years ago

Thanks that is brilliant. I will have a go with VS when I get time. Until then I have included your Windows binary in the pypi release.

electronstudio commented 5 years ago

I got the dynamic bindings to work, although I'm not sure how because I didnt change anything except recompiling Raylib. Therefore I have split the module into two sub-modules, one for static and one for dynamic.

test_dynamic.py should now work.

However I don't have windows DLLs. Could you compile raylib.dll (or libraylib.dll or whatever its called) for me and put it in raylib/dynamic please?

Pebaz commented 5 years ago

Sure! I'm taking the RHCSA exam today so I may be able to get to it this evening or tomorrow. Fantastic work on this! Can't wait to actually have textured models in raylib :)

electronstudio commented 5 years ago

I'm doing it because I've been using Pygame Zero to teach simple 2d game programming, and I wanted a similar library to teach 3d game programming. To that end I've started on the simple API, see test_richlib.py . Will probably split it out into its own project at some point.

Pebaz commented 5 years ago

For some reason, I simply cannot get a DLL to work.

  1. It builds just fine.
  2. The file size is far smaller than the other dynamic libs for the other platforms.
  3. No functions show up in DLL Export Viewer
  4. Python loads the library just fine and then fails because it cannot find any functions within it.

image

I ran CMake to generate both the static and dynamic libraries. I don't know what I did wrong but Visual Studio is not generating a DLL that contains the functions.

Never really seen this issue before lol. Do you happen to have a Windows computer you could generate the DLL from?

electronstudio commented 5 years ago

I tried doing it like this:

cmake SHARED=ON ..
msbuild ALL_BUILDL.vxproj

It built raylib_static.lib and raylib_static.pdb but no DLLs.

How did you build Raylib with VS? The wiki page only has instructions for mingw32.

Thinking ahead... once Raylib 2.5 is officially released we ought to be able to just use the official DLLs.

However, as I've been using it more, I find the dynamic bindings quite annoying. If you make a mistake sometimes you get a segfault and sometimes it just silently doesn't work. The static bindings are better. I think the ideal solution might be to ship static binaries for Windows and Mac and have pip attempt to compile binaries upon install for Linux, because I'm not confident that binaries compiled on one Linux will work on another.

Pebaz commented 5 years ago

I ran CMake-GUI in the folder. There were 2 different option checkboxes that mentioned "dynamic".

However, I completely agree that we should either wait until Raylib 2.5 comes out or just use static compilation.

I'll ask a few people to test the static compilation soon to get a feel for a couple different people's computers.

Thanks for you help on this, I can't wait to use your port in my projects soon!

Pebaz commented 5 years ago

Update:

Was able to use the statically-compiled version on different computers just fine (Windows).

Just tested the new release of Raylib v2.5 (DLL) and it worked (Windows)!

electronstudio commented 5 years ago

I have updated everything to the release version v2.5 and done builds for static, dynamic, python 3.6, python 3.7, windows, mac. only thing I can't get working is any sort of useful static linux build.

Pebaz commented 5 years ago

Fantastic! What issues are you facing?

electronstudio commented 5 years ago

First the released libraylib.a isn't compiled with fPIC so cant be linked into a shared library. Then had lots of problems just getting raylib to build. Then built my own libraylib.a with fPIC but couldn't get gcc to include the actual functions in the shared library. Then found that the order of the l flags on the gcc command makes a difference and that cffi builder puts them in the wrong order.

Anyway now it's working, on Ubuntu 18.04 at least (and 16.04 I think) so that is probably the best we can do.

Pebaz commented 5 years ago

Oh my word, gotta love building C! It surprises me even today how difficult (tedious?) it is to build C/C++ projects. Great job, at least you got it working. You gonna put up a release soon? :)

Pebaz commented 5 years ago

BTW I'm already using Raylib-Python-CFFI in one of my own projects and once I get a little further into it I'll start converting some of the C Raylib examples after I figure out how to interface with the various components.