h12w / cwrap

Wraps C libraries in Go.
BSD 2-Clause "Simplified" License
117 stars 4 forks source link

Wrapping Fitz from MuPDF not working as expected #4

Closed frytoli closed 11 months ago

frytoli commented 1 year ago

Hello -- firstly, thank you for your work here. I'm excited to use this package.

I'm attempting to use this project to create bindings for MuPDF's Fitz. I'm using your MuPDF example as a reference, but my code is not working. Any advice would be greatly appreciated.

Here's my directory setup. I effectively cloned MuPDF and plucked out the include/ directory:

./
|  main.go
|  include/
|  | mupdf/
|  |  | fitz.h
|  |  | memento.h
|  |  | pdf.h
|  |  | ucdn.h
|  |  | fitz/
|  |  |  | lots of fitz headers...
|  |  | helpers
|  |  |  | helpers...
|  |  | pdf/
|  |  |  | pdf headers...

My go code:

package main

import (
    . "h12.io/cwrap"
    "fmt"
)

const (
    Ldflags   = "LDFLAGS: /usr/local/lib/libmupdf.a /usr/local/lib/libmupdf-js-none.a -lm -lfreetype -ljpeg -lopenjp2 -ljbig2dec -lssl -lcrypto"
)

var (
    fitz = &Package{
        PacName: "fitz",
        PacPath: "go-fitz/",
        From: Header{
            Dir:         "include/",
            File:        "mupdf/fitz.h",
            NamePattern: `(?i:\Afz(.*))`,
            Excluded: []string{
                "fz_get_annot_type",
                "fz_open_file_w",
            },
            CgoDirectives: []string{Ldflags},
        },
        Included: []*Package{},
    }
)

func main() {
    err := fitz.Wrap()

    if err != nil {
        fmt.Println(err)
    }
}

And, the errors I'm getting. I'm at a complete loss for how to solve the first error about the "angled include." The second one is about relative imports, and I'm able to make it go away by flattening all of the fitz code into one directory. But, I don't think that's the right way to solve the issue:

./_cwrap-2668373447.h:2:10: error: 'mupdf/fitz.h' file not found with <angled> include; use "quotes" instead
#include <mupdf/fitz.h>
         ^~~~~~~~~~~~~~
         "mupdf/fitz.h"
In file included from ./_cwrap-2668373447.h:2:
./mupdf/fitz.h:30:10: fatal error: 'mupdf/fitz/version.h' file not found
#include "mupdf/fitz/version.h"
         ^~~~~~~~~~~~~~~~~~~~~~
exit status 1

Thank you in advance for your help!

h12w commented 1 year ago

I haven't worked on this for a long time so it's amazing that it can still run and generate some Go wrapper code with small fixes. Here is what I did:

There are some issues with the generated code, the go-mupdf/fz/auto_amd64.go file is almost empty. Without the fz package, the generated wrapper cannot compile, unfortunately.

I made some modifications so that the intermediate header file and XML file are written to the "temp" directory, which could potentially help with troubleshooting. I'm not sure about the root cause but it could be some simple logic error, e.g. the layout of the parsed XML changes so it failed to find the functions under fitz which means the configs or some logic for finding those functions need to be changed.