PMunch / futhark

Automatic wrapping of C headers in Nim
MIT License
393 stars 22 forks source link

template/generic instantiation of `importc` from here #118

Open blockoutdev opened 3 months ago

blockoutdev commented 3 months ago

trying to generate a wrapper for flecs (https://github.com/SanderMertens/flecs)

directory looks like this

root /
   wrap_flecs.nim
   flecs /
      flecs.c
      flecs.h
      libflecs.so
      libflecs_static.a

wrap_flecs.nim is just

import futhark

{.passL: "./flecs/libflecs_static.a".}

importc:
  outputPath "./libflecs_generated.nim"
  path "./flecs" 
  "flecs.h"

nim has an issue with importc line, its giving the error in the title.

PMunch commented 3 months ago

Do you have a full error? That is only a small part of the error messages.

blockoutdev commented 3 months ago

the editor is also pointing out the issue but it only shows "template/generic instantiation of importc from here"

stack trace: (most recent call last)
/home/alex/.nimble/pkgs2/futhark-0.13.4-0bfc8e592b6707dd725093236736b06956840cd7/futhark.nim(952, 21) importcImpl
/home/alex/.nimble/pkgs2/futhark-0.13.4-0bfc8e592b6707dd725093236736b06956840cd7/futhark.nim(454, 41) createStruct
/home/alex/.nimble/pkgs2/futhark-0.13.4-0bfc8e592b6707dd725093236736b06956840cd7/futhark.nim(214, 50) sanitizeName
/home/alex/.choosenim/toolchains/nim-2.0.8/lib/pure/strutils.nim(2199, 26) replace
/home/alex/.choosenim/toolchains/nim-2.0.8/lib/pure/strutils.nim(1891, 16) initSkipTable
/home/alex/.choosenim/toolchains/nim-2.0.8/lib/pure/strutils.nim(1879, 7) initSkipTable
/home/alex/.choosenim/toolchains/nim-2.0.8/lib/pure/algorithm.nim(98, 15) fill
/home/alex/VSCode/NimProjects/testNim/wrap_flecs.nim(5, 1) template/generic instantiation of `importc` from here
/home/alex/.nimble/pkgs2/futhark-0.13.4-0bfc8e592b6707dd725093236736b06956840cd7/futhark.nim(708, 14) template/generic instantiation of `importcImpl` from here
/home/alex/.choosenim/toolchains/nim-2.0.8/lib/pure/algorithm.nim(98, 15) Error: interpretation requires too many iterations; if you are sure this is not a bug in your code, compile with `--maxLoopIterationsVM:number` (current value: 10000000)
PMunch commented 3 months ago

Yeah, the editor shortens the message, sometimes to the point where it is useless..

The actual problem here is at the end:

Error: interpretation requires too many iterations; if you are sure this is not a bug in your code, compile with `--maxLoopIterationsVM:number` (current value: 10000000)

This happens if the output from Opir is large, which happens very easily. Just throw a couple of zeros on the end of that number and it works fine.

blockoutdev commented 3 months ago

Thanks for the help. The nim wrapper code is being generated now. I put together a simple test to see if it works, but the compiler is giving "undefined reference" errors for all the symbols im using from the library. im compiling with just nim c test_flecs_wrapper.nim

honestly im not sure if Im grasping this fully. once the nim wrapper code is generated, do I need to reference the nim file that generated the wrapper? and is an include or import statement used if I want to use the wrapper?

the editor is picking up the symbols fine and suggesting autocompletes

test_flecs_wrapper:


import libflecs
var world = ecs_init()

var e = ecs_new(world)
echo ecs_is_alive(world, e)
ecs_delete(world, e)
echo ecs_is_alive(world, e)

discard ecs_fini(world) 

error message:

Hint: used config file '/home/alex/.choosenim/toolchains/nim-2.0.8/config/nim.cfg' [Conf]
Hint: used config file '/home/alex/.choosenim/toolchains/nim-2.0.8/config/config.nims' [Conf]
...................................................................................................................
/home/alex/VSCode/NimProjects/testNim/libflecs.nim(4815, 16) Warning: Declaration of FILE exists but with different size [User]
/home/alex/VSCode/NimProjects/testNim/libflecs.nim(7603, 9) Hint: Declaration of FILE already exists, not redeclaring [User]
.
/home/alex/VSCode/NimProjects/testNim/libflecs.nim(4815, 16) Warning: Declaration of FILE exists but with different size [User]
/home/alex/VSCode/NimProjects/testNim/libflecs.nim(7603, 9) Hint: Declaration of FILE already exists, not redeclaring [User]
CC: libflecs.nim
CC: test_flecs_wrapper.nim
Hint:  [Link]
/usr/bin/ld: /home/alex/.cache/nim/test_flecs_wrapper_d/@mtest_flecs_wrapper.nim.c.o: in function `NimMainModule':
@mtest_flecs_wrapper.nim.c:(.text+0x1d7): undefined reference to `ecs_init'
/usr/bin/ld: @mtest_flecs_wrapper.nim.c:(.text+0x1f5): undefined reference to `ecs_new'
/usr/bin/ld: @mtest_flecs_wrapper.nim.c:(.text+0x224): undefined reference to `ecs_is_alive'
/usr/bin/ld: @mtest_flecs_wrapper.nim.c:(.text+0x282): undefined reference to `ecs_delete'
/usr/bin/ld: @mtest_flecs_wrapper.nim.c:(.text+0x2aa): undefined reference to `ecs_is_alive'
/usr/bin/ld: @mtest_flecs_wrapper.nim.c:(.text+0x308): undefined reference to `ecs_fini'

beneath the above messages the whole gcc command is printed out, but I excluded it for readability. ill put it down below.

blockoutdev commented 3 months ago
collect2: error: ld returned 1 exit status
Error: execution of an external program failed: 'gcc   -o /home/alex/VSCode/NimProjects/testNim/test_flecs_wrapper  /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@s..@s.choosenim@stoolchains@snim-2.0.8@slib@ssystem@sexceptions.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@s..@s.choosenim@stoolchains@snim-2.0.8@slib@sstd@sprivate@ssince.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@s..@s.choosenim@stoolchains@snim-2.0.8@slib@ssystem@sctypes.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@s..@s.choosenim@stoolchains@snim-2.0.8@slib@sstd@ssysatomics.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@s..@s.choosenim@stoolchains@snim-2.0.8@slib@ssystem@sansi_c.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@s..@s.choosenim@stoolchains@snim-2.0.8@slib@ssystem@smemory.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@s..@s.choosenim@stoolchains@snim-2.0.8@slib@sstd@sprivate@ssyslocks.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@s..@s.choosenim@stoolchains@snim-2.0.8@slib@sstd@sprivate@sthreadtypes.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@s..@s.choosenim@stoolchains@snim-2.0.8@slib@sstd@sprivate@sdigitsutils.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@s..@s.choosenim@stoolchains@snim-2.0.8@slib@sstd@sprivate@smiscdollars.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@s..@s.choosenim@stoolchains@snim-2.0.8@slib@sstd@sassertions.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@s..@s.choosenim@stoolchains@snim-2.0.8@slib@ssystem@siterators.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@s..@s.choosenim@stoolchains@snim-2.0.8@slib@ssystem@scoro_detection.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@s..@s.choosenim@stoolchains@snim-2.0.8@slib@sstd@sprivate@sdragonbox.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@s..@s.choosenim@stoolchains@snim-2.0.8@slib@sstd@sprivate@sschubfach.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@s..@s.choosenim@stoolchains@snim-2.0.8@slib@sstd@sformatfloat.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@s..@s.choosenim@stoolchains@snim-2.0.8@slib@sstd@sobjectdollar.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@s..@s.choosenim@stoolchains@snim-2.0.8@slib@ssystem@sdollars.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@s..@s.choosenim@stoolchains@snim-2.0.8@slib@sstd@stypedthreads.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@s..@s.choosenim@stoolchains@snim-2.0.8@slib@ssystem@sstacktraces.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@s..@s.choosenim@stoolchains@snim-2.0.8@slib@sstd@sprivate@sbitops_utils.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@s..@s.choosenim@stoolchains@snim-2.0.8@slib@ssystem@scountbits_impl.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@s..@s.choosenim@stoolchains@snim-2.0.8@slib@ssystem@srepr_v2.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@s..@s.choosenim@stoolchains@snim-2.0.8@slib@sstd@swidestrs.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@s..@s.choosenim@stoolchains@snim-2.0.8@slib@sstd@ssyncio.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@s..@s.choosenim@stoolchains@snim-2.0.8@slib@ssystem.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@s..@s.choosenim@stoolchains@snim-2.0.8@slib@score@smacros.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@s..@s.choosenim@stoolchains@snim-2.0.8@slib@spure@sparseutils.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@s..@s.choosenim@stoolchains@snim-2.0.8@slib@spure@sbitops.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@s..@s.choosenim@stoolchains@snim-2.0.8@slib@spure@sfenv.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@s..@s.choosenim@stoolchains@snim-2.0.8@slib@spure@smath.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@s..@s.choosenim@stoolchains@snim-2.0.8@slib@spure@salgorithm.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@s..@s.choosenim@stoolchains@snim-2.0.8@slib@spure@stypetraits.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@s..@s.choosenim@stoolchains@snim-2.0.8@slib@sstd@senumutils.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@s..@s.choosenim@stoolchains@snim-2.0.8@slib@sstd@sstrbasics.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@s..@s.choosenim@stoolchains@snim-2.0.8@slib@spure@sunicode.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@s..@s.choosenim@stoolchains@snim-2.0.8@slib@sstd@sprivate@sjsutils.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@s..@s.choosenim@stoolchains@snim-2.0.8@slib@sstd@sprivate@sstrimpl.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@s..@s.choosenim@stoolchains@snim-2.0.8@slib@spure@sstrutils.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@s..@s.choosenim@stoolchains@snim-2.0.8@slib@sstd@sprivate@sosseps.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@s..@s.choosenim@stoolchains@snim-2.0.8@slib@spure@spathnorm.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@s..@s.choosenim@stoolchains@snim-2.0.8@slib@sstd@soserrors.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@s..@s.choosenim@stoolchains@snim-2.0.8@slib@sposix@sposix.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@s..@s.choosenim@stoolchains@snim-2.0.8@slib@sstd@sprivate@soscommon.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@s..@s.choosenim@stoolchains@snim-2.0.8@slib@sstd@sprivate@sospaths2.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@s..@s.choosenim@stoolchains@snim-2.0.8@slib@sstd@sprivate@sossymlinks.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@s..@s.choosenim@stoolchains@snim-2.0.8@slib@spure@soptions.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@s..@s.choosenim@stoolchains@snim-2.0.8@slib@spure@stimes.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@s..@s.choosenim@stoolchains@snim-2.0.8@slib@sstd@sprivate@sosfiles.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@s..@s.choosenim@stoolchains@snim-2.0.8@slib@sstd@sprivate@sosdirs.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@s..@s.choosenim@stoolchains@snim-2.0.8@slib@sstd@senvvars.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@s..@s.choosenim@stoolchains@snim-2.0.8@slib@sstd@sprivate@sosappdirs.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@s..@s.choosenim@stoolchains@snim-2.0.8@slib@sstd@scmdline.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@s..@s.choosenim@stoolchains@snim-2.0.8@slib@spure@sos.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@mlibflecs.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@mtest_flecs_wrapper.nim.c.o  -pthread -pthread -lm -lm -lrt   -ldl'
blockoutdev commented 3 months ago

libflecs.nim.txt

heres the generated code for reference

PMunch commented 3 months ago

You just import the module containing the Futhark code, it will cache the output so once you've run it once it's basically a no-op unless you change anything.

Those errors are from the C compiler, Futhark doesn't attempt to tell the C compiler how to find the symbols because it can be done in multiple different ways. Typically you'd have a so/dll/dynlib file, or you link against a .a file, or even you just include the .h files. But that all depends on your library.

blockoutdev commented 3 months ago

wow, it works. I have it running on compilation and it echos everything out as expected. Whats the best way to get the editor to recognize the symbols? I had trouble trying to import the generated nim code directly (the result of passing outputPath to importc). am I better off refactoring the wrapper with my own nim code to obscure all the 'undeclared reference' issues, or is there a way to get editor integration with the futhark code?