RobLoach / node-raylib

Node.js bindings for Raylib
https://robloach.github.io/node-raylib/
Other
233 stars 20 forks source link

PLATFORM_DRM for installation in RPI #166

Closed JesperNaarttijarvi closed 1 year ago

JesperNaarttijarvi commented 1 year ago

Hi,

Similar to issue #86 and #91 I want to compile the npm package with the option PLATFORM_DRM for raylib, so I can run it without X-11.

I can get the original raylib to work just fine on minimal install without X11, with the flag "PLATFORM=PLATFORM_DRM"

I cant get #86 and #91 to work for 4.2, is there an easy way to add the flag "PLATFORM=PLATFORM_DRM" when installing the package?

konsumer commented 1 year ago

158 has some info on setting custom cmake flags, but I think you can also set PLATFORM=PLATFORM_DRM as an env-var to trigger the same behaviour, and that is easiest, if I remember right, so it just builds right when you npm install.

On pi:

export PLATFORM=PLATFORM_DRM
npm i raylib

or in cmake, in the node-raylib dir:

npm run compile -- --CDPLATFORM=DRM

This is untested (I'm on a mac, and don't know where my pi for testing is) but let me know if you have issues.

konsumer commented 1 year ago

I usually manually build the node release for pi/mac-arm, but I think there have been a couple releases without it. Github actions can't natively build for arm, so we have to do some tricky stuff to get it working. Looks like our automated arm-builds are currently failing.

JesperNaarttijarvi commented 1 year ago

Thanks for the fast response, but sadly none of the tips worked for me.

I looked at #158 before and tried to run npm run compile -- --CDPLATFORM=DRM in the node package, it did not work, same fault as before.

Deleted the old installation files, set env-var and then installing raylib manually. Also didnt work.

rm -rf ./node_modules
export PLATFORM=PLATFORM_DRM
npm i raylib

Problems:

node app.js yields

INFO: Initializing raylib 4.2
INFO: Supported raylib modules:
INFO:     > rcore:..... loaded (mandatory)
INFO:     > rlgl:...... loaded (mandatory)
INFO:     > rshapes:... loaded (optional)
INFO:     > rtextures:. loaded (optional)
INFO:     > rtext:..... loaded (optional)
INFO:     > rmodels:... loaded (optional)
INFO:     > raudio:.... loaded (optional)
WARNING: GLFW: Error: 65544 Description: X11: The DISPLAY environment variable is missing
WARNING: GLFW: Failed to initialize GLFW
FATAL: Failed to initialize Graphic Device

DISPLAY=:0 node app.js yields

INFO: Initializing raylib 4.2
INFO: Supported raylib modules:
INFO:     > rcore:..... loaded (mandatory)
INFO:     > rlgl:...... loaded (mandatory)
INFO:     > rshapes:... loaded (optional)
INFO:     > rtextures:. loaded (optional)
INFO:     > rtext:..... loaded (optional)
INFO:     > rmodels:... loaded (optional)
INFO:     > raudio:.... loaded (optional)
WARNING: GLFW: Error: 65544 Description: X11: Failed to open display :0
WARNING: GLFW: Failed to initialize GLFW
FATAL: Failed to initialize Graphic Device

user: ~/.../raylib/node_modules/raylib $ npm run compile -- --CDPLATFORM=DRM

> raylib@0.12.0 precompile
> npm i --no-save node-addon-api cmake-js

up to date, audited 677 packages in 6s

118 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

> raylib@0.12.0 compile
> cmake-js compile "--CDPLATFORM=DRM"

info TOOL Using Unix Makefiles generator.
info CMD BUILD
info RUN [
info RUN   'cmake',
info RUN   '--build',
info RUN   '/home/itos/passage/raylib/node_modules/raylib/build',
info RUN   '--config',
info RUN   'Release'
info RUN ]
[ 62%] Built target glfw
[ 92%] Built target raylib
[100%] Built target node-raylib

Packages i had to install to run npm i raylib:

libxrandr-dev
libxinerama-dev
libxcursor-dev
libxi-dev

The only packages I've installed before this is libdrm-dev libegl1-mesa-dev libgles2-mesa-dev libgbm-dev as recommended by Raylibs website.

konsumer commented 1 year ago

hmm, lemme see if I can get a pi setup for building, and I will build you one for release. I can't quite remember what is needed, but I think there is some simple cmake thing that will enable the flag (it used to be on by default for arm linux builds, specifically for pi and arm handhelds, which benefit from this flag, too.)

konsumer commented 1 year ago

Sidenote: @twuky is working on fixing the auto-build, too, so that might help things.

konsumer commented 1 year ago

Looking at cmakejs docs, I think it should be :

--CDPLATFORM=DRM

So I am wondering if maybe the script doesn't like it. Try this in node-raylib dir:

cmake-js compile --CDPLATFORM=DRM

I can't seem to remember the env-var format, but I think that is an easier way to set same thing. I will try to figure it out.

JesperNaarttijarvi commented 1 year ago

Now its running with the correct flag. Got an other error, looking into it now.

user@user100:~/rep/raylib/node_modules/raylib $ cmake-js compile --CDPLATFORM=DRM
info TOOL Using Unix Makefiles generator.
info CMD BUILD
info RUN [
info RUN   'cmake',
info RUN   '--build',
info RUN   '/home/user/rep/raylib/node_modules/raylib/build',
info RUN   '--config',
info RUN   'Release'
info RUN ]
[ 62%] Built target glfw
[ 92%] Built target raylib
Scanning dependencies of target node-raylib
[ 96%] Building CXX object CMakeFiles/node-raylib.dir/src/generated/node-raylib.cc.o
/home/user/rep/raylib/node_modules/raylib/src/generated/node-raylib.cc:4:10: fatal error: napi.h: No such file or directory
    4 | #include <napi.h>
      |          ^~~~~~~~
compilation terminated.
gmake[2]: *** [CMakeFiles/node-raylib.dir/build.make:82: CMakeFiles/node-raylib.dir/src/generated/node-raylib.cc.o] Error 1
gmake[1]: *** [CMakeFiles/Makefile2:172: CMakeFiles/node-raylib.dir/all] Error 2
gmake: *** [Makefile:171: all] Error 2
info REP Build has been failed, trying to do a full rebuild.
info CMD CLEAN
info RUN [
info RUN   'cmake',
info RUN   '-E',
info RUN   'remove_directory',
info RUN   '/home/user/rep/raylib/node_modules/raylib/build'
info RUN ]
info CMD CONFIGURE
info RUN [
info RUN   'cmake',
info RUN   '/home/user/rep/raylib/node_modules/raylib',
info RUN   '--no-warn-unused-cli',
info RUN   '-G',
info RUN   'Unix Makefiles',
info RUN   '-DCMAKE_JS_VERSION=undefined',
info RUN   '-DCMAKE_BUILD_TYPE=Release',
info RUN   '-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=/home/user/rep/raylib/node_modules/raylib/build/Release',
info RUN   '-DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreaded$<$<CONFIG:Debug>:Debug>',
info RUN   '-DCMAKE_JS_INC=/home/user/.cmake-js/node-arm/v17.4.0/include/node',
info RUN   '-DCMAKE_JS_SRC=',
info RUN   '-DNODE_RUNTIME=node',
info RUN   '-DNODE_RUNTIMEVERSION=17.4.0',
info RUN   '-DNODE_ARCH=arm',
info RUN   '-DPLATFORM=DRM',
info RUN   '-DCMAKE_JS_LIB=',
info RUN   '-DCMAKE_CXX_FLAGS=-DBUILDING_NODE_EXTENSION'
info RUN ]
Not searching for unused variables given on the command line.
-- The C compiler identification is GNU 10.2.1
-- The CXX compiler identification is GNU 10.2.1
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Populating raylib
-- Configuring done
-- Generating done
-- Build files have been written to: /home/user/rep/raylib/node_modules/raylib/build/_deps/raylib-subbuild
Scanning dependencies of target raylib-populate
[ 11%] Creating directories for 'raylib-populate'
[ 22%] Performing download step (git clone) for 'raylib-populate'
Cloning into 'raylib-src'...
Note: switching to '4.2.0'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at bf2ad9d P A I N
[ 33%] Performing update step for 'raylib-populate'
[ 44%] No patch step for 'raylib-populate'
[ 55%] No configure step for 'raylib-populate'
[ 66%] No build step for 'raylib-populate'
[ 77%] No install step for 'raylib-populate'
[ 88%] No test step for 'raylib-populate'
[100%] Completed 'raylib-populate'
[100%] Built target raylib-populate
-- Performing Test COMPILER_HAS_THOSE_TOGGLES
-- Performing Test COMPILER_HAS_THOSE_TOGGLES - Success
-- Testing if -Werror=pointer-arith can be used -- compiles
-- Testing if -Werror=implicit-function-declaration can be used -- compiles
-- Testing if -fno-strict-aliasing can be used -- compiles
-- No GLFW required on PLATFORM_DRM
-- Audio Backend: miniaudio
-- Building raylib static library
-- Generated build type: Release
-- Compiling with the flags:
--   PLATFORM=PLATFORM_DRM
--   GRAPHICS=GRAPHICS_API_OPENGL_ES2
-- Configuring done
-- Generating done
-- Build files have been written to: /home/user/rep/raylib/node_modules/raylib/build
info CMD BUILD
info RUN [
info RUN   'cmake',
info RUN   '--build',
info RUN   '/home/user/rep/raylib/node_modules/raylib/build',
info RUN   '--config',
info RUN   'Release'
info RUN ]
Scanning dependencies of target raylib
[ 10%] Building C object _deps/raylib-build/raylib/CMakeFiles/raylib.dir/rcore.c.o
[ 20%] Building C object _deps/raylib-build/raylib/CMakeFiles/raylib.dir/rmodels.c.o
[ 30%] Building C object _deps/raylib-build/raylib/CMakeFiles/raylib.dir/rshapes.c.o
[ 40%] Building C object _deps/raylib-build/raylib/CMakeFiles/raylib.dir/rtext.c.o
[ 50%] Building C object _deps/raylib-build/raylib/CMakeFiles/raylib.dir/rtextures.c.o
[ 60%] Building C object _deps/raylib-build/raylib/CMakeFiles/raylib.dir/utils.c.o
[ 70%] Building C object _deps/raylib-build/raylib/CMakeFiles/raylib.dir/raudio.c.o
[ 80%] Linking C static library libraylib.a
[ 80%] Built target raylib
Scanning dependencies of target node-raylib
[ 90%] Building CXX object CMakeFiles/node-raylib.dir/src/generated/node-raylib.cc.o
/home/user/rep/raylib/node_modules/raylib/src/generated/node-raylib.cc:4:10: fatal error: napi.h: No such file or directory
    4 | #include <napi.h>
      |          ^~~~~~~~
compilation terminated.
gmake[2]: *** [CMakeFiles/node-raylib.dir/build.make:82: CMakeFiles/node-raylib.dir/src/generated/node-raylib.cc.o] Error 1
gmake[1]: *** [CMakeFiles/Makefile2:133: CMakeFiles/node-raylib.dir/all] Error 2
gmake: *** [Makefile:171: all] Error 2
ERR! OMG Process terminated: 2
konsumer commented 1 year ago

napi missing means it doesn't have all the dev-deps installed. This was a bit of a catch-22 I remember from building DRM with npm i that an env-var helps with. It will take me a bit, but I will setup a pi for testing, here, and try to help figure it out.

Maybe, if you run npm i first, then run the above "manual" cmakejs command after, it will have all the stuff it needs.

twuky commented 1 year ago

we don't include node-addon-api or cmake-js as deps or devdeps any more, i think you need to install those both with --no-save

twuky commented 1 year ago

we don't include node-addon-api or cmake-js as deps or devdeps any more, i think you need to install those both with --no-save

konsumer commented 1 year ago

ah, yeh, I think the preinstall stuff does that, so "normal" users don't have to download a bunch of dev-deps.

JesperNaarttijarvi commented 1 year ago

Installed node-addon-api, still gets an error. Its doing a full rebuild now so will see soon what's crashing.

/home/user/rep/raylib/node_modules/raylib/src/generated/node-raylib.cc: In function ‘rlVertexBuffer rlVertexBufferFromValue(const Napi::CallbackInfo&, int)’:
/home/user/rep/raylib/node_modules/raylib/src/generated/node-raylib.cc:381:3: error: could not convert ‘{intFromValue((* & info), (index + 0)), ((float*)pointerFromValue((* & info), (index + 1))), ((float*)pointerFromValue((* & info), (index + 2))), ((unsigned char*)pointerFromValue((* & info), (index + 3))), ((unsigned int*)pointerFromValue((* & info), (index + 4))), unsignedintFromValue((* & info), (index + 5)), pointerFromValue((* & info), (index + 6))}’ from ‘<brace-enclosed initializer list>’ to ‘rlVertexBuffer’
  381 |   };
      |   ^
      |   |
      |   <brace-enclosed initializer list>

.....

gmake[2]: *** [CMakeFiles/node-raylib.dir/build.make:82: CMakeFiles/node-raylib.dir/src/generated/node-raylib.cc.o] Error 1
gmake[1]: *** [CMakeFiles/Makefile2:133: CMakeFiles/node-raylib.dir/all] Error 2
gmake: *** [Makefile:171: all] Error 2
ERR! OMG Process terminated: 2
twuky commented 1 year ago

okay, I've got CI working on building on arm with DRM enabled and can reproduce your error. It seems like it has something to do with compiler differences, and structs with default values? I'm not a C expert, so it may take a bit to find out whats going on. it sounds like it may be platform specific to gmake?

this is the code in question. it looks like we need to find another way to initialize this struct. if we figure that out i can update the codegen to use that format, or just override for this struct if it's the only one with this error

inline rlVertexBuffer rlVertexBufferFromValue(const Napi::CallbackInfo& info, int index) {
  return {
     intFromValue(info, index + 0),
     (float *) pointerFromValue(info, index + 1),
     (float *) pointerFromValue(info, index + 2),
     (unsigned char *) pointerFromValue(info, index + 3),
     (unsigned int *) pointerFromValue(info, index + 4),
     unsignedintFromValue(info, index + 5),
     (unsigned int) pointerFromValue(info, index + 6)
  };
}
twuky commented 1 year ago

looks like this struct uses different values for opengl ES2.0 on drm. so we need to construct it like this to match:

// exception for this constructor, which has different input depending on platform
inline rlVertexBuffer rlVertexBufferFromValue(const Napi::CallbackInfo& info, int index) {
  return {
     intFromValue(info, index + 0),
     (float *) pointerFromValue(info, index + 1),
     (float *) pointerFromValue(info, index + 2),
     (unsigned char *) pointerFromValue(info, index + 3),
      #if defined(GRAPHICS_API_OPENGL_11) || defined(GRAPHICS_API_OPENGL_33)
        (unsigned int *) pointerFromValue(info, index + 4),      // Vertex indices (in case vertex data comes indexed) (6 indices per quad)
      #endif
      #if defined(GRAPHICS_API_OPENGL_ES2)
        (unsigned short *) pointerFromValue(info, index + 4),    // Vertex indices (in case vertex data comes indexed) (6 indices per quad)
      #endif
     unsignedintFromValue(info, index + 5),
     (unsigned int) pointerFromValue(info, index + 6)
  };
}

pushing a change to the generator that includes this and we'll see how it builds.

twuky commented 1 year ago

@JesperNaarttijarvi It looks like CI builds of DRM raylib are completing succesfully. I haven't published anything to npm because I don't have a system to test, but if you want to try you can use the prebuilt copies at https://github.com/RobLoach/node-raylib/releases/tag/v0.12.5 - either replace node-raylib.node in build/Release or you can require the .node directly and use raylib functions, though you won't have built in constant colors, so you'll have to create those as objects yourself.

Let me know if these work and we can figure out how to integrate them better into the install process. I was thinking that whenever an ARM target installs, it will grab both the normal and DRM versions, and users will instead import * as rl from 'raylib/drm' to use that version.

JesperNaarttijarvi commented 1 year ago

It worked, and now we got an other error. Maybe not related.

Currently installing original raylib and compiling, to see if we get the same error.

INFO: Initializing raylib 4.2
INFO: Supported raylib modules:
INFO:     > rcore:..... loaded (mandatory)
INFO:     > rlgl:...... loaded (mandatory)
INFO:     > rshapes:... loaded (optional)
INFO:     > rtextures:. loaded (optional)
INFO:     > rtext:..... loaded (optional)
INFO:     > rmodels:... loaded (optional)
INFO:     > raudio:.... loaded (optional)
INFO: DISPLAY: No graphic card set, trying platform-gpu-card
WARNING: DISPLAY: Failed to find a suitable DRM connector mode
FATAL: Failed to initialize Graphic Device

Raylib original library:

user@user:~/raylib/examples $ ./core/core_input_mouse
INFO: Initializing raylib 4.5-dev
INFO: Supported raylib modules:
INFO:     > rcore:..... loaded (mandatory)
INFO:     > rlgl:...... loaded (mandatory)
INFO:     > rshapes:... loaded (optional)
INFO:     > rtextures:. loaded (optional)
INFO:     > rtext:..... loaded (optional)
INFO:     > rmodels:... loaded (optional)
INFO:     > raudio:.... loaded (optional)
INFO: DISPLAY: No graphic card set, trying platform-gpu-card
INFO: DISPLAY: Selected DRM connector mode FIXED_MODE (2560x1600p@50)
INFO: DISPLAY: Upscaling required: Screen size (800x450) smaller than display size (2560x1600)
INFO: DISPLAY: Device initialized successfully
INFO:     > Display size: 2560 x 1600
INFO:     > Screen size:  800 x 450
INFO:     > Render size:  800 x 450
INFO:     > Viewport offsets: 0, 50
INFO: GL: Supported extensions count: 91
INFO: GL: OpenGL device information:
INFO:     > Vendor:   Broadcom
INFO:     > Renderer: V3D 4.2
INFO:     > Version:  OpenGL ES 3.1 Mesa 20.3.5
INFO:     > GLSL:     OpenGL ES GLSL ES 3.10
INFO: GL: VAO extension detected, VAO functions loaded successfully
INFO: GL: NPOT textures extension detected, full NPOT textures supported
INFO: GL: DXT compressed textures supported
INFO: GL: ETC1 compressed textures supported
INFO: TEXTURE: [ID 1] Texture loaded successfully (1x1 | R8G8B8A8 | 1 mipmaps)
INFO: TEXTURE: [ID 1] Default texture loaded successfully
INFO: SHADER: [ID 1] Vertex shader compiled successfully
INFO: SHADER: [ID 2] Fragment shader compiled successfully
INFO: SHADER: [ID 3] Program shader loaded successfully
INFO: SHADER: [ID 3] Default shader loaded successfully
INFO: RLGL: Render batch vertex buffers loaded successfully in RAM (CPU)
INFO: RLGL: Render batch vertex buffers loaded successfully in VRAM (GPU)
INFO: RLGL: Default OpenGL state initialized successfully
INFO: TEXTURE: [ID 2] Texture loaded successfully (128x128 | GRAY_ALPHA | 1 mipmaps)
INFO: FONT: Default font loaded successfully (224 glyphs)
INFO: RPI: Opening input device: /dev/input/event0 (multitouch touchscreen )
INFO: RPI: Gamepad device initialized successfully
WARNING: RPI: Failed to change keyboard mode, an SSH keyboard is probably used
INFO: TIMER: Target time per frame: 16.667 milliseconds
JesperNaarttijarvi commented 1 year ago

Problem was the resolution i set was too high, lowering the resolution solved the problem.

twuky commented 1 year ago

@JesperNaarttijarvi do you have a preference for how you would want to use the DRM version after installing from npm?

Right now I've set it up so that if you import 'raylib/drm' it should try to load that version of the node addon. But I'm realizing now I think that changes how it interacts with typescript definitions. Maybe it would be better to have only one place to import, but it checks your package.json for a configuration:

"raylib": {"drm": true}

on second thought this would not really persist when you run a code bundler. maybe it would be best to publish node-raylib-drm seperately?

konsumer commented 1 year ago

Problem was the resolution i set was too high, lowering the resolution solved the problem.

I had this problem too!

I thought for sure arm handhelds were not working, but it turned out to just be the resolution.

JesperNaarttijarvi commented 1 year ago

The easiest for me would definitely be to change the import to drm, but we dont run raylib cross-platform which might cause problems.

But on second thought, the people running drm probably have a little bit of knowledge about installations, so having flags is probably not a problem either. But i dont want to download the package twice and recompile it several times, because that takes time.

A seperate npm would also work, but then the gap in version handling between the two repos might grow apart?

twuky commented 1 year ago

Well now that CI is properly building both versions of the linux-arm raylib, users shouldn't have to compile anything unless there are some additional flags they want to set. When you install from npm, we run a postinstall script that checks the Releases on github for a version of raylib compatible with the system. In the case of ARM, I've changed it so it downloads both the drm and nondrm version. It should only have to build raylib now if you are on an unsupported platform by our prebuilt native addons. For instance we don't have macos-arm (M1,M2,etc) set up in CI. but sometimes we add those to the releases manually.

I think for now I'll work on having a secondary import for DRM users within this package

twuky commented 1 year ago

It should be working now, if you install through npm i https://github.com/RobLoach/node-raylib and use import * as rl from 'raylib/drm'. Let me know if that works alright and I'll publish to NPM

konsumer commented 1 year ago

But i dont want to download the package twice and recompile it several times, because that takes time.

Yeh, the builds will be done for you, so when you npm i it, it will download (the pre-built) and setup the drm version. The reason you have to build it now is because our CI auto-builds were failing for arm, which @twuky just fixed.

I think for now I'll work on having a secondary import for DRM users within this package

I am all for this, but also I think just setting it to build like that for linux-arm is ok, too. Like I said, pretty much all linux arm can benefit from DRM build, and the only reason not to do it on linux x86_64 is that it's possible the X drivers are better than the DRM, but it should mostly work there too. It used to be that pi had a special build, that used these special pi framebuffer drivers vs X/DRM (which are both linux standards), but now it's just standard DRM which most linux users have, and latest pi os uses DRM, and also most handheld arm devices, even if they don't have X installed.

JesperNaarttijarvi commented 1 year ago

Reinstalled all node_modules and changed raylib-node to npm i https://github.com/RobLoach/node-raylib.

First problem:

import * as rl from 'raylib/drm'

ERROR 

Error [ERR_UNSUPPORTED_DIR_IMPORT]: Directory import '/home/user/rep/raylib/node_modules/raylib/drm' is not supported resolving ES modules imported from /home/user/rep/raylib/app.js
Did you mean to import raylib/node_modules/raylib/drm/index.js?

After updating to import index.js

user@user100:~/rep/raylib $ node app.js 
node:internal/modules/cjs/loader:933
  const err = new Error(message);
              ^

Error: Cannot find module '../../build/Release/node-raylib-drm.node'
Require stack:
- /home/user/rep/raylib/node_modules/raylib/src/generated/node-raylib-drm.js
- /home/user/rep/raylib/node_modules/raylib/drm/index.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Module.require (node:internal/modules/cjs/loader:999:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (/home/user/rep/raylib/node_modules/raylib/src/generated/node-raylib-drm.js:10:11)
    at Module._compile (node:internal/modules/cjs/loader:1097:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1149:10)
    at Module.load (node:internal/modules/cjs/loader:975:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:999:19) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/home/user/rep/raylib/node_modules/raylib/src/generated/node-raylib-drm.js',
    '/home/user/rep/raylib/node_modules/raylib/drm/index.js'
  ]
}
user@user100:~/rep/raylib $ ls node_modules/raylib/build/Release/
node-raylib.node
konsumer commented 1 year ago

not sure what the prob is, but this should fix:

wget https://github.com/RobLoach/node-raylib/releases/download/v0.13.0/node-raylib-linux-arm-drm.node -O node_modules/raylib/build/Release/node-raylib-drm.node

I think you also need this in es6:

import rl from 'raylib/drm/index.js'
twuky commented 1 year ago

Ohhh, I missed that postinstall calls process.exit() when it downloads the first prebuilt, so it was never grabbing the DRM version. Reversed the order so it tries for DRM first, and doesn't exit prematurely. I've also updated the docs in readme about the index.js issue.

This should be everything it needs now!

konsumer commented 1 year ago

It is going to have problems in mjs anyway, due to the requires (which are mapped in node to import, with mjs, only if it's the module's entrypoint.) I am not sure of a good way to resolve that, other than building for both targets with esbuild/rollup/etc, or multiple published libraries (each with different entrypoint) or putting it all in the same file. maybe something like this?

import { getDrm } from 'raylib'
const raylib = await getDrm()

We have already talked about generating mjs and transpiling to cjs (for old node) and that has other benefits. In the interest of easier typescript bindings, that could also be an option: generate typescript, then transpile that to cjs & mjs.

It I remember right, typescript has some AST libs that can make generating an object representing code (instead of a string) a bit easier, so maybe the generator could be reworked a bit to work that way. With an AST transformer, you can generate multiple languages from the same source, or generate a typescript AST (and skip the initial code) to generate typescript/cjs/mjs, so we would have a few interfaces from the same generated AST. ASTs are generally much more complicated, but they are objects and easier to reliably generate in code than huge strings of code.

This seems verbose and tedious, but might be an ok path, if it was automated. This is kind of a hybrid approach, where you get AST nodes for simple code-strings. This shows going both directions (code to AST, then AST to code.)

twuky commented 1 year ago

I'd be okay with moving the whole thing over to es6 modules and typescript eventually, possibly alongside raylib 4.5. I don't think we'd need to change the generator that much for supporting it though. I think we could just merge the generator for the js wrappers into the typescript definitions generator, which is already set up like an es6 module.

konsumer commented 1 year ago

Yep, I think that would be a good step in the right direction. I don't use typescript myself, but it seems a bit jenky to generate the typedefs separate from the code. It's just what it outputs, so I really don't care if it's in typescript, as long as ES6/CJS is exported by module (in build step before publish.)

Long-term, I think the AST path might be better, just because it's no strings-of-code-in-code like we have with the current generator, and it feels like the right way to generate multiple targets of js-like code, but you are right, the change could be fairly minimal to support both now: generate ES6 strings, run it through esbuild (or easier: microbundle) to generate cjs, then link to both in package.json.