I wrote a library in Cython that has two different "modes":
- If rendering, I compile using GLFW.
- If not rendering, I compile using EGL, which is faster, but I have not figured out how to render with it.
What is the recommended way to handle this situation?
Right now, I have the following directory structure:
mujoco
├── __init__.py
├── simEgl.pyx
├── simGlfw.pyx
├── sim.pxd
└── sim.pyx
simEgl.pyx
contains EGL code and simGlfw.pyx
contains GLFW code. setup.py
uses an environment variable to choose one or the other for the build.
This works ok, except that I need to recompile the code every time I want to switch between modes. There must be a better way.
Update
I agree that the best approach is to simultaneously compile two different libraries and use a toggle to choose which one to import. I already do have a base class in sim.pyx
with shared functionality. However this base class must itself be compiled with the separate libraries. Specifically, sim.pyx
depends on libmujoco.so
which depends on either GLFW or EGL.
Here is my exhaustive search of possible approaches:
- If I do not compile an extension for
sim.pyx
, I getImportError: No module named 'mujoco.sim'
- If I compile an extension for
sim.pyx
without including graphics libraries in the extension, I getImportError: /home/ethanbro/.mujoco/mjpro150/bin/libmujoco150.so: undefined symbol: __glewBlitFramebuffer
- If I compile an extension for
sim.pyx
and choose one set of graphics libraries (GLFW), then when I try to use the other set of graphics libraries (EGL) this does not work either unsurprisingly:ERROR: GLEW initalization error: Missing GL version
- If I compile two different versions of the
sim.pyx
library, one with one set of libraries, one with the other, I get:TypeError: unorderable types: dict() < dict()
which is not a very helpful error message, but appears to result from trying to share a source file between two different extensions.
Something like option 4 should be possible. In fact, if I were working in raw C, I would simply build two shared objects side by side using the different libraries. Any advice on how to get around this Cython limitation would be very welcome.