PMunch / futhark

Automatic wrapping of C headers in Nim
MIT License
355 stars 19 forks source link

Wrapping libmodbus? #71

Closed matkuki closed 1 year ago

matkuki commented 1 year ago

Hi,

Trying to wrap libmodbus. I am completely new to Futhark, so I may have messed something up. My specs:

The code:

# modbus_wrapper.nim
import futhark

# Tell futhark where to find the C libraries you will compile with, and what
# header files you wish to import.
importc:
  path "..\\libmodbus\\src"
  "modbus.h"

# Tell Nim how to compile against the library. If you have a dynamic library
# this would simply be a `--passL:"-l<library name>`
{.passL: "-lmodbus".}

# Use the library just like you would in C!
let modbus_client = modbus_new_rtu("COM1", 19200, 'E', 8, 1)
modbus_close(modbus_client)

Complied with:

nim c modbus_wrapper.nim

Output:

Hint: used config file 'C:\Users\matic\.choosenim\toolchains\nim-1.6.12\config\nim.cfg' [Conf]
Hint: used config file 'C:\Users\matic\.choosenim\toolchains\nim-1.6.12\config\config.nims' [Conf]
...................................................................................................................
C:\Users\matic\.nimble\pkgs\futhark-#head\futhark.nim(692, 12) Hint: Running: cmd /c opir -IJ:\Nim\libmodbus\futhark_test\libmodbus\src "-IC:\Program Files\LLVM\lib\clang\15.0.7\include" C:\Users\matic\nimcache\modbus_wrapper_d\futhark-includes.h [User]
stack trace: (most recent call last)
C:\Users\matic\.nimble\pkgs\futhark-#head\futhark.nim(704, 40) importcImpl
C:\Users\matic\.choosenim\toolchains\nim-1.6.12\lib\pure\strutils.nim(2815, 18) strip
C:\Users\matic\.choosenim\toolchains\nim-1.6.12\lib\system\iterators_1.nim(90, 18) substr
J:\Nim\libmodbus\futhark_test\src\modbus_wrapper.nim(5, 1) template/generic instantiation of `importc` from here
C:\Users\matic\.nimble\pkgs\futhark-#head\futhark.nim(637, 14) template/generic instantiation of `importcImpl` from here
C:\Users\matic\.choosenim\toolchains\nim-1.6.12\lib\system\iterators_1.nim(90, 18) 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)

Any hints?

PMunch commented 1 year ago

Ah yes, should probably leave a note about this in the README. If the header is too large the JSON parser requires too many iterations during compilation. Simply do as it says and set --maxLoopIterationsVM to a higher number and it should work. I run into this when wrapping Gtk as well.

By the way, you don't know what the error on the latest tagged version was do you?

matkuki commented 1 year ago

Ah, thanks 👍

By the way, you don't know what the error on the latest tagged version was do you?

Sure, here you go:

$ nimble install futhark
Downloading https://github.com/PMunch/futhark using git
   Warning: Package 'futhark' has an incorrect structure. The top level of the package source directory should contain at most one module, named 'futhark.nim', but a file named 'opir.nim' was found. This will be an error in the future.
      Hint: If this is the primary source file in the package, rename it to 'futhark.nim'. If it's a source file required by the main module, or if it is one of several modules exposed by 'futhark', then move it into a 'futhark\' subdirectory. If it's a test file or otherwise not required to build the the package 'futhark.nim', prevent its installation by adding `skipFiles = @["opir.nim"]` to the .nimble file. See https://github.com/nim-lang/nimble#libraries for more info.
  Verifying dependencies for futhark@0.9.1
      Info: Dependency on https://github.com/PMunch/libclang-nim@#head already satisfied
  Verifying dependencies for clang@#head
      Info: Dependency on termstyle@any version already satisfied
  Verifying dependencies for termstyle@0.1.0
      Info: Dependency on macroutils@any version already satisfied
  Verifying dependencies for macroutils@1.2.0
 Installing futhark@0.9.1
   Building futhark/opir.exe using c backend
C:\Users\matic\AppData\Local\Temp\nimble_10652\githubcom_PMunchfuthark\src\opir.nim(158, 1) Warning: catch a more precise Exception deriving from CatchableError or Defect. [BareExcept]
C:\Users\matic\AppData\Local\Temp\nimble_10652\githubcom_PMunchfuthark\src\opir.nim(357, 39) template/generic instantiation of `parseReturn` from here
C:\Users\matic\AppData\Local\Temp\nimble_10652\githubcom_PMunchfuthark\src\opir.nim(353, 11) Warning: The bare except clause is deprecated; use `except CatchableError:` instead [BareExcept]
C:\Users\matic\AppData\Local\Temp\nimble_10652\githubcom_PMunchfuthark\src\opir.nim(359, 35) template/generic instantiation of `parseReturn` from here
C:\Users\matic\AppData\Local\Temp\nimble_10652\githubcom_PMunchfuthark\src\opir.nim(353, 11) Warning: The bare except clause is deprecated; use `except CatchableError:` instead [BareExcept]
C:\Users\matic\AppData\Local\Temp\nimble_10652\githubcom_PMunchfuthark\src\opir.nim(360, 35) template/generic instantiation of `parseReturn` from here
C:\Users\matic\AppData\Local\Temp\nimble_10652\githubcom_PMunchfuthark\src\opir.nim(353, 11) Warning: The bare except clause is deprecated; use `except CatchableError:` instead [BareExcept]
C:\Users\matic\AppData\Local\Temp\nimble_10652\githubcom_PMunchfuthark\src\opir.nim(361, 35) template/generic instantiation of `parseReturn` from here
C:\Users\matic\AppData\Local\Temp\nimble_10652\githubcom_PMunchfuthark\src\opir.nim(353, 11) Warning: The bare except clause is deprecated; use `except CatchableError:` instead [BareExcept]
C:\Users\matic\AppData\Local\Temp\nimble_10652\githubcom_PMunchfuthark\src\opir.nim(362, 55) template/generic instantiation of `parseReturn` from here
C:\Users\matic\AppData\Local\Temp\nimble_10652\githubcom_PMunchfuthark\src\opir.nim(353, 11) Warning: The bare except clause is deprecated; use `except CatchableError:` instead [BareExcept]
C:\Users\matic\AppData\Local\Temp\nimble_10652\githubcom_PMunchfuthark\src\opir.nim(364, 38) template/generic instantiation of `parseReturn` from here
C:\Users\matic\AppData\Local\Temp\nimble_10652\githubcom_PMunchfuthark\src\opir.nim(353, 11) Warning: The bare except clause is deprecated; use `except CatchableError:` instead [BareExcept]
C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lclang: No such file or directory
collect2.exe: error: ld returned 1 exit status
Error: execution of an external program failed: 'gcc.exe   -o C:\Users\matic\AppData\Local\Temp\nimble_10652\githubcom_PMunchfuthark\opir.exe  C:\Users\matic\nimcache\opir_r\@m..@s..@s..@s..@s..@s..@s.choosenim@stoolchains@snim-1.6.12@slib@sstd@sprivate@sdigitsutils.nim.c.o C:\Users\matic\nimcache\opir_r\@m..@s..@s..@s..@s..@s..@s.choosenim@stoolchains@snim-1.6.12@slib@ssystem@sassertions.nim.c.o C:\Users\matic\nimcache\opir_r\@m..@s..@s..@s..@s..@s..@s.choosenim@stoolchains@snim-1.6.12@slib@ssystem@sformatfloat.nim.c.o C:\Users\matic\nimcache\opir_r\@m..@s..@s..@s..@s..@s..@s.choosenim@stoolchains@snim-1.6.12@slib@ssystem@sdollars.nim.c.o C:\Users\matic\nimcache\opir_r\@m..@s..@s..@s..@s..@s..@s.choosenim@stoolchains@snim-1.6.12@slib@ssystem@swidestrs.nim.c.o C:\Users\matic\nimcache\opir_r\@m..@s..@s..@s..@s..@s..@s.choosenim@stoolchains@snim-1.6.12@slib@ssystem@sio.nim.c.o C:\Users\matic\nimcache\opir_r\@m..@s..@s..@s..@s..@s..@s.choosenim@stoolchains@snim-1.6.12@slib@ssystem.nim.c.o C:\Users\matic\nimcache\opir_r\@m..@s..@s..@s..@s..@s..@s.choosenim@stoolchains@snim-1.6.12@slib@spure@sparseutils.nim.c.o C:\Users\matic\nimcache\opir_r\@m..@s..@s..@s..@s..@s..@s.choosenim@stoolchains@snim-1.6.12@slib@spure@smath.nim.c.o C:\Users\matic\nimcache\opir_r\@m..@s..@s..@s..@s..@s..@s.choosenim@stoolchains@snim-1.6.12@slib@spure@salgorithm.nim.c.o C:\Users\matic\nimcache\opir_r\@m..@s..@s..@s..@s..@s..@s.choosenim@stoolchains@snim-1.6.12@slib@spure@sstrutils.nim.c.o C:\Users\matic\nimcache\opir_r\@m..@s..@s..@s..@s..@s..@s.choosenim@stoolchains@snim-1.6.12@slib@spure@spathnorm.nim.c.o C:\Users\matic\nimcache\opir_r\@m..@s..@s..@s..@s..@s..@s.choosenim@stoolchains@snim-1.6.12@slib@spure@sdynlib.nim.c.o C:\Users\matic\nimcache\opir_r\@m..@s..@s..@s..@s..@s..@s.choosenim@stoolchains@snim-1.6.12@slib@swindows@swinlean.nim.c.o C:\Users\matic\nimcache\opir_r\@m..@s..@s..@s..@s..@s..@s.choosenim@stoolchains@snim-1.6.12@slib@spure@soptions.nim.c.o C:\Users\matic\nimcache\opir_r\@m..@s..@s..@s..@s..@s..@s.choosenim@stoolchains@snim-1.6.12@slib@spure@stimes.nim.c.o C:\Users\matic\nimcache\opir_r\@m..@s..@s..@s..@s..@s..@s.choosenim@stoolchains@snim-1.6.12@slib@sstd@sprivate@swin_setenv.nim.c.o C:\Users\matic\nimcache\opir_r\@m..@s..@s..@s..@s..@s..@s.choosenim@stoolchains@snim-1.6.12@slib@spure@sos.nim.c.o C:\Users\matic\nimcache\opir_r\@m..@s..@s..@s..@s..@s..@s.choosenim@stoolchains@snim-1.6.12@slib@spure@shashes.nim.c.o C:\Users\matic\nimcache\opir_r\@m..@s..@s..@s..@s..@s..@s.choosenim@stoolchains@snim-1.6.12@slib@spure@scollections@stables.nim.c.o C:\Users\matic\nimcache\opir_r\@m..@s..@s..@s..@s..@s..@s.choosenim@stoolchains@snim-1.6.12@slib@spure@sjson.nim.c.o C:\Users\matic\nimcache\opir_r\@m..@s..@s..@s..@s..@s..@s.choosenim@stoolchains@snim-1.6.12@slib@sposix@sposix.nim.c.o C:\Users\matic\nimcache\opir_r\@m..@s..@s..@s..@s..@s..@s.nimble@spkgs@sclang-@hhead@sclangpkg@sIndex.nim.c.o C:\Users\matic\nimcache\opir_r\@m..@s..@s..@s..@s..@s..@s.nimble@spkgs@stermstyle-0.1.0@stermstyle.nim.c.o C:\Users\matic\nimcache\opir_r\@mopir.nim.c.o   -lclang  '
    Prompt: Build failed for 'futhark@0.9.1', would you like to try installing 'futhark@#head' (latest unstable)? [y/N]
    Answer: