PMunch / futhark

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

[Question] Futhark usage - how to split code #103

Closed mantielero closed 6 months ago

mantielero commented 6 months ago

In order to generate smaller files, I am using the following pattern:

importc:
  outputPath currentSourcePath.parentDir / "libavutil" / "mathematics.nim"
  path "./FFmpeg/libavutil"
  "mathematics.h"

importc:
  outputPath currentSourcePath.parentDir / "libavutil" / "imgutils.nim"
  path "./FFmpeg/libavutil"
  "libavutil/imgutils.h"

importc:
  outputPath currentSourcePath.parentDir / "libavutil" / "frame.nim"
  path "./FFmpeg/libavutil"
  "libavutil/frame.h"

importc:
  outputPath currentSourcePath.parentDir / "libavutil" / "avutil.nim"
  path "./FFmpeg/libavutil"
  "libavutil/avutil.h"

The works well until I get a message such as:

futhark.nim(382, 68) Error: redefinition of 'structavchannellayout_u_t'; previous declaration here: /home/jose/.nimble/pkgs2/futhark-0.12.4-aa38b35bbacbf292471c3bc6c5d8ab3ba3c9a4f3/futhark.nim(382, 68)

How should I use Futhark correctly? Is there a way to tell Futhark: don't create bindings for what has been created before?

Because I understand that this:

importc:
  outputPath currentSourcePath.parentDir / "libavutil" / "avutils.nim"
  path "./FFmpeg/libavutil"
  "mathematics.h"
  "libavutil/imgutils.h"
  "libavutil/frame.h"
  "libavutil/avutil.h"

creates a single file.

Besides I always try to import and avoid using include. Are the futhark outcomes thought to be included or imported?

PMunch commented 6 months ago

How should I use Futhark correctly?

You should allow Futhark to work the way its intended and create a single file. The idea behind Futhark is to allow you to import things just like you would in C, but it requires you to do all the imports in a single block so it can figure out what to generate and what not to generate.

Are the futhark outcomes thought to be included or imported?

Neither, you're supposed to import a module which has the Futhark importc block in it. That being said if you want to ship wrappers without requiring the user to have Futhark or the original sources available then you should have a look at the shipping wrappers section in the README. This is really the only scenario where you're supposed to use outputPath, otherwise you can just omit that line and let Futhark handle its own cache.

I have been working on a "project mode" for Futhark in which it will create Nim files analogous to the original C files to wrap a larger project. This isn't quite complete yet, but seems like maybe what you're trying to do. So keep an eye out for that once it launches. In the meantime just put all your C imports into a single Futhark statement, remove the outputPath statement, and just let Futhark do its thing.