gen2brain / go-fitz

Golang wrapper for the MuPDF Fitz library
GNU Affero General Public License v3.0
369 stars 87 forks source link

Dockerfile with alpine - undefined reference to `__fprintf_chk` #59

Closed pksieminski closed 2 years ago

pksieminski commented 2 years ago

Hello,

I'm running the following Dockerfile on my OSX:

FROM golang:alpine
WORKDIR /app
RUN apk add --no-cache git gcc musl-dev
COPY go.mod go.sum ./
COPY example.go test.pdf ./
RUN go mod download
RUN go build -tags musl -o /example
CMD [ "/example" ]

Which results in the following outcome:

Step 1/8 : FROM golang:alpine
 ---> f5ae5d299f4c
Step 2/8 : WORKDIR /app
 ---> Using cache
 ---> faab50477730
Step 3/8 : RUN apk add --no-cache git gcc musl-dev
 ---> Using cache
 ---> 0fa39dfd6012
Step 4/8 : COPY go.mod go.sum ./
 ---> Using cache
 ---> 99ba26559e57
Step 5/8 : COPY example.go test.pdf ./
 ---> Using cache
 ---> f8076516ab72
Step 6/8 : RUN go mod download
 ---> Using cache
 ---> 3f6abaf51608
Step 7/8 : RUN go build -tags musl -o /example
 ---> Running in 14372ec812da
# github.com/gen2brain/go-fitz
/usr/lib/gcc/aarch64-alpine-linux-musl/11.2.1/../../../../aarch64-alpine-linux-musl/bin/ld: /go/pkg/mod/github.com/gen2brain/go-fitz@v1.20.0/libs/libmupdf_linux_arm64.a(context.o): in function `fz_new_context_imp':
context.c:(.text.fz_new_context_imp+0x284): undefined reference to `__fprintf_chk'

I have also tried to run it with platform targets:

I would want it to actually work properly for multiple targets.

gen2brain commented 2 years ago

As noted in the README, the musl tag and compiled library are for linux/amd64 only, it doesn't exist for linux/arm64 so the musl tag does nothing. You can use extlib or just do not use alpine based docker but anything Glibc based.

pksieminski commented 2 years ago

Thank you for your reply. I will give extlib a try.

Also - I forgot to mention, that for linux/arm64 build run I removed the -tags musl, and thats when it fails with the given error:

FROM golang:1.18-alpine
WORKDIR /app
RUN apk add --no-cache gcc
RUN apk add --no-cache bash make g++ linux-headers udev
COPY go.mod go.sum ./
COPY example.go test.pdf ./
RUN go mod download
RUN go build -o /example
CMD [ "/example" ]
#12 1.607 /usr/lib/gcc/aarch64-alpine-linux-musl/11.2.1/../../../../aarch64-alpine-linux-musl/bin/ld: /go/pkg/mod/github.com/gen2brain/go-fitz@v1.20.0/libs/libmupdf_linux_arm64.a(context.o): in function `fz_new_context_imp':
#12 1.607 context.c:(.text.fz_new_context_imp+0x284): undefined reference to `__fprintf_chk'

So there is no way to build Alpine on other arch than amd64, while using included compiled libraries from the go-fitz repository?

gen2brain commented 2 years ago

Doesn't matter if removed the tag or not, there is a static library built for linux/arm64 but only for Glibc, not for Musl, you cannot use it with Alpine. Can I ask what would be the use case for linux/arm64 docker image, and where would be used? I can add the library, but I need to compile the toolchain for Musl linux/arm64, build the library, etc.

pksieminski commented 2 years ago

Thank you for clarification, now I can see that in libs there is only musl compiled version for amd64.

I will check as I might settle only for amd64 image build, so I will close this issue now. If it will turn out that arm64 is required, I will re-open with a kind request to build that one.

Thank you for your help for now.

pksieminski commented 2 years ago

Actually there is one common scenario which could be very helpful - the new M1 & M2 are running Alpine for arch64 so that would at least help with running those Docker images and testing them locally.

I know that might not be a good enough reason for you, but I think that might help developers that run test suites on containerized applications, and that is also scenario which fails for me.

Local development runs fain, since it is using included compiled lib and I have Glibc, but not in container on Alpine.

gen2brain commented 2 years ago

Ok, I added support in https://github.com/gen2brain/go-fitz/commit/298d4898f02ae35a036b4bb1ffd39c47c18479d0, please check. Not sure I understand the issue though, I thought in macOS Docker is using VM to run Linux, but anyway I guess VM would run better if it doesn't have to emulate different architecture so here it is.

Maybe also someone would want to run Alpine Docker on RPi/arm64, though I don't see a point for that, but perhaps there is one.

pksieminski commented 2 years ago

Thank you for the update.

The alpine docker image is built as multi-platform. When ran on macOS it will by default pick the same platform as host, to not emulate the VM, as this might fail. In case of macOS it picks linux/arm64.

Also I have tried it with go module: require github.com/gen2brain/go-fitz v1.20.1-0.20220630122136-298d4898f02a // indirect

FROM golang:alpine
WORKDIR /app
RUN apk add --no-cache git gcc musl-dev
COPY go.mod go.sum ./
COPY example.go test.pdf ./
RUN go mod download
RUN go build -tags musl -o /example
CMD [ "/example" ]

Building with docker build . --platform=linux/arm64 I get this error now:

#11 1.652 # github.com/gen2brain/go-fitz
#11 1.652 /usr/lib/gcc/aarch64-alpine-linux-musl/11.2.1/../../../../aarch64-alpine-linux-musl/bin/ld: /go/pkg/mod/github.com/gen2brain/go-fitz@v1.20.1-0.20220630122136-298d4898f02a/libs/libmupdf_linux_arm64_musl.a(html-layout.o): in function `destroy_hb_shaper_data':
#11 1.652 html-layout.c:(.text.destroy_hb_shaper_data+0x18): undefined reference to `hb_font_destroy'
#11 1.652 /usr/lib/gcc/aarch64-alpine-linux-musl/11.2.1/../../../../aarch64-alpine-linux-musl/bin/ld: /go/pkg/mod/github.com/gen2brain/go-fitz@v1.20.1-0.20220630122136-298d4898f02a/libs/libmupdf_linux_arm64_musl.a(html-layout.o): in function `walk_string':
#11 1.652 html-layout.c:(.text.walk_string+0x284): undefined reference to `hb_buffer_clear_contents'
#11 1.652 /usr/lib/gcc/aarch64-alpine-linux-musl/11.2.1/../../../../aarch64-alpine-linux-musl/bin/ld: html-layout.c:(.text.walk_string+0x29c): undefined reference to `hb_buffer_set_direction'
#11 1.652 /usr/lib/gcc/aarch64-alpine-linux-musl/11.2.1/../../../../aarch64-alpine-linux-musl/bin/ld: html-layout.c:(.text.walk_string+0x2cc): undefined reference to `hb_language_from_string'
#11 1.652 /usr/lib/gcc/aarch64-alpine-linux-musl/11.2.1/../../../../aarch64-alpine-linux-musl/bin/ld: html-layout.c:(.text.walk_string+0x2d8): undefined reference to `hb_buffer_set_language'
#11 1.652 /usr/lib/gcc/aarch64-alpine-linux-musl/11.2.1/../../../../aarch64-alpine-linux-musl/bin/ld: html-layout.c:(.text.walk_string+0x2f4): undefined reference to `hb_buffer_add_utf8'
#11 1.652 /usr/lib/gcc/aarch64-alpine-linux-musl/11.2.1/../../../../aarch64-alpine-linux-musl/bin/ld: html-layout.c:(.text.walk_string+0x324): undefined reference to `hb_buffer_guess_segment_properties'
#11 1.652 /usr/lib/gcc/aarch64-alpine-linux-musl/11.2.1/../../../../aarch64-alpine-linux-musl/bin/ld: html-layout.c:(.text.walk_string+0x344): undefined reference to `hb_shape'
#11 1.652 /usr/lib/gcc/aarch64-alpine-linux-musl/11.2.1/../../../../aarch64-alpine-linux-musl/bin/ld: html-layout.c:(.text.walk_string+0x354): undefined reference to `hb_buffer_get_glyph_positions'
#11 1.652 /usr/lib/gcc/aarch64-alpine-linux-musl/11.2.1/../../../../aarch64-alpine-linux-musl/bin/ld: html-layout.c:(.text.walk_string+0x368): undefined reference to `hb_buffer_get_glyph_infos'
#11 1.652 /usr/lib/gcc/aarch64-alpine-linux-musl/11.2.1/../../../../aarch64-alpine-linux-musl/bin/ld: html-layout.c:(.text.walk_string+0x570): undefined reference to `hb_shape'
#11 1.652 /usr/lib/gcc/aarch64-alpine-linux-musl/11.2.1/../../../../aarch64-alpine-linux-musl/bin/ld: html-layout.c:(.text.walk_string+0x58c): undefined reference to `hb_ft_font_create'
#11 1.652 /usr/lib/gcc/aarch64-alpine-linux-musl/11.2.1/../../../../aarch64-alpine-linux-musl/bin/ld: /go/pkg/mod/github.com/gen2brain/go-fitz@v1.20.1-0.20220630122136-298d4898f02a/libs/libmupdf_linux_arm64_musl.a(html-layout.o): in function `fz_layout_html':
#11 1.652 html-layout.c:(.text.fz_layout_html+0x17c): undefined reference to `hb_buffer_create'
#11 1.652 /usr/lib/gcc/aarch64-alpine-linux-musl/11.2.1/../../../../aarch64-alpine-linux-musl/bin/ld: html-layout.c:(.text.fz_layout_html+0x24c): undefined reference to `hb_buffer_destroy'
#11 1.652 /usr/lib/gcc/aarch64-alpine-linux-musl/11.2.1/../../../../aarch64-alpine-linux-musl/bin/ld: /go/pkg/mod/github.com/gen2brain/go-fitz@v1.20.1-0.20220630122136-298d4898f02a/libs/libmupdf_linux_arm64_musl.a(html-layout.o): in function `fz_draw_html':
#11 1.652 html-layout.c:(.text.fz_draw_html+0x12c): undefined reference to `hb_buffer_destroy'
#11 1.652 /usr/lib/gcc/aarch64-alpine-linux-musl/11.2.1/../../../../aarch64-alpine-linux-musl/bin/ld: html-layout.c:(.text.fz_draw_html+0x17c): undefined reference to `hb_buffer_create'
#11 1.652 collect2: error: ld returned 1 exit status
gen2brain commented 2 years ago

Can you try now with https://github.com/gen2brain/go-fitz/commit/74ac65a0bae176821af6140811bdaeb9d3bc9bf2, the harfbuzz symbols were made local because of issue https://github.com/gen2brain/go-fitz/issues/53, it looks like it is not needed on linux/arm64.

gen2brain commented 2 years ago

Actually, try with https://github.com/gen2brain/go-fitz/commit/b7987b40405dcc893c1f7ec168f502fa75476681.

pksieminski commented 2 years ago

It works on https://github.com/gen2brain/go-fitz/commit/b7987b40405dcc893c1f7ec168f502fa75476681, thank you for that update. I will close the issue.