dart-lang / native

Dart packages related to FFI and native assets bundling.
BSD 3-Clause "New" or "Revised" License
152 stars 42 forks source link

ffigen throws error when generate libjpeg #1492

Closed yanshouwang closed 1 month ago

yanshouwang commented 1 month ago

Here is the error logs

[SEVERE] : Header /Users/yanshouwang/dev/hybrid_sdk/hybrid_jpeg/./src/include/jpeglib.h: Total errors/warnings: 8.
[SEVERE] :     /Users/yanshouwang/dev/hybrid_sdk/hybrid_jpeg/./src/include/jpeglib.h:850:3: error: unknown type name 'size_t' [Semantic Issue]
[SEVERE] :     /Users/yanshouwang/dev/hybrid_sdk/hybrid_jpeg/./src/include/jpeglib.h:862:3: error: unknown type name 'size_t' [Semantic Issue]
[SEVERE] :     /Users/yanshouwang/dev/hybrid_sdk/hybrid_jpeg/./src/include/jpeglib.h:893:58: error: unknown type name 'size_t' [Semantic Issue]
[SEVERE] :     /Users/yanshouwang/dev/hybrid_sdk/hybrid_jpeg/./src/include/jpeglib.h:895:25: error: unknown type name 'size_t' [Semantic Issue]
[SEVERE] :     /Users/yanshouwang/dev/hybrid_sdk/hybrid_jpeg/./src/include/jpeglib.h:970:34: error: unknown type name 'size_t' [Semantic Issue]
[SEVERE] :     /Users/yanshouwang/dev/hybrid_sdk/hybrid_jpeg/./src/include/jpeglib.h:972:36: error: unknown type name 'size_t' [Semantic Issue]
[SEVERE] :     /Users/yanshouwang/dev/hybrid_sdk/hybrid_jpeg/./src/include/jpeglib.h:979:52: error: unknown type name 'FILE' [Semantic Issue]
[SEVERE] :     /Users/yanshouwang/dev/hybrid_sdk/hybrid_jpeg/./src/include/jpeglib.h:980:53: error: unknown type name 'FILE' [Semantic Issue]
[WARNING]: The compiler found warnings/errors in source files.
[WARNING]: This will likely generate invalid bindings.
[SEVERE] : Skipped generating bindings due to errors in source files. See https://github.com/dart-lang/native/blob/main/pkgs/ffigen/doc/errors.md.

I looked into the jpeglib.h, it said that the stdio.h is not included in this header by design. How can I solve this error when generate bindings with ffigen?

/* NOTE: This header file does not include stdio.h, despite the fact that it
 * uses FILE and size_t.  That is by design, since the libjpeg API predates the
 * widespread adoption of ANSI/ISO C.  Referring to libjpeg.txt, it is a
 * documented requirement that calling programs "include system headers that
 * define at least the typedefs FILE and size_t" before including jpeglib.h.
 * Technically speaking, changing that requirement by including stdio.h here
 * would break backward API compatibility.  Please do not file bug reports,
 * feature requests, or pull requests regarding this.
 */
liamappelbe commented 1 month ago

I assume you're using jpeglib.h as your entrypoint header in your config? The easiest fix is probably to just write a new header that includes stdio.h and jpeglib.h, then use that as your entrypoint instead:

#include <stdio.h>
#include "path/to/jpeglib.h"
yanshouwang commented 1 month ago
#include <stdio.h>
#include "path/to/jpeglib.h"

I did this, but seems the bindings of jpeglib.h is not generated in this way?

headers:
  entry-points:
    - 'src/include/jconfig.h'
    - 'src/include/jerror.h'
    - 'src/include/jmorecfg.h'
    # - 'src/include/jpeglib.h'
    - 'src/include/turbojpeg.h'
    - 'src/include/hybrid_jpeg.h'
  include-directives:
    - 'src/include/jconfig.h'
    - 'src/include/jerror.h'
    - 'src/include/jmorecfg.h'
    # - 'src/include/jpeglib.h'
    - 'src/include/turbojpeg.h'
    - 'src/include/hybrid_jpeg.h'
Levi-Lesches commented 1 month ago

I could be wrong but you might want:

headers:
  entry-points:
    - 'src/include/stdio_and_jpeglib.h'
  include-directives: 
    - 'path/to/jpeglib.h'
yanshouwang commented 1 month ago

I could be wrong but you might want:

headers:
  entry-points:
    - 'src/include/stdio_and_jpeglib.h'
  include-directives: 
    - 'path/to/jpeglib.h'

Tha't right, solved by only replace this header in entry-points.