lestrrat-go / libxml2

Interface to libxml2, with DOM interface
MIT License
230 stars 55 forks source link

Cross-compiling from linux to windows #83

Closed hans-m-song closed 11 months ago

hans-m-song commented 2 years ago

I seem to be having some issues cross-compiling from linux to windows.

Here is a reproducible example:

Dockerfile

FROM --platform=linux/amd64 ubuntu:20.04

ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update && \
  apt-get install -y \
  ca-certificates \
  curl \
  gcc-mingw-w64 \
  libc6 \
  libc6-dev \
  libxml2 \
  libxml2-dev \
  pkg-config

RUN curl -Lf "https://go.dev/dl/go1.18.3.linux-amd64.tar.gz" -o "go.tar.gz" \
  && tar -C /usr/local -xzf go.tar.gz \
  && rm -f go.tar.gz \
  && mkdir -p /home/test
ENV PATH=/usr/local/go/bin:$PATH

WORKDIR /home/test
COPY go.mod main.go ./
RUN go mod tidy

ENV GOOS=windows
ENV CC=x86_64-w64-mingw32-gcc
ENV CGO_ENABLED=1
ENV GOARCH=amd64
ENV CGO_CFLAGS=-I/usr/include

# this fails!
RUN go build

go.mod

module test

go 1.18

require (
    github.com/lestrrat/go-libxml2 v0.0.0-20180221004755-bb78334e2019
)

main.go

package main

import (
    "github.com/lestrrat/go-libxml2/xsd"
)

func main() {
    xsd.ParseFromFile("")
}

Attempting to build the image results in the following error:

# github.com/lestrrat/go-libxml2/clib
In file included from /usr/include/libxml2/libxml/parser.h:810,
                 from /usr/include/libxml2/libxml/HTMLparser.h:16,
                 from /root/go/pkg/mod/github.com/lestrrat/go-libxml2@v0.0.0-20180221004755-bb78334e2019/clib/clib.go:28:
/usr/include/libxml2/libxml/encoding.h:28:10: fatal error: iconv.h: No such file or directory
   28 | #include <iconv.h>
      |          ^~~~~~~~~
compilation terminated.

Using CGO_FLAGS=-I/usr/include doesn't seem to help either.

I can see the file /usr/include/iconv.h exists if built omitting the failing step in the dockerfile and pkg-config recognises libxml2.

# pkg-config --list-all
shared-mime-info shared-mime-info - Freedesktop common MIME database
libxcrypt        libxcrypt - Extended crypt library for DES, MD5, Blowfish and others
icu-uc           icu-uc - International Components for Unicode: Common and Data libraries
icu-i18n         icu-i18n - International Components for Unicode: Internationalization library
libxml-2.0       libXML - libXML library version2.
libcrypt         libxcrypt - Extended crypt library for DES, MD5, Blowfish and others
icu-io           icu-io - International Components for Unicode: Stream and I/O Library

Sorry if this is the wrong place to put this, I figure this is more to do with cgo than libxml2 but I'm at a loss as to how to proceed. Probably similar to https://github.com/lestrrat-go/libxml2/issues/80 (I have encountered their issue as well).

lestrrat commented 2 years ago

I'm not much of an expert around this, but one thing I suspect is that libxml2 was not installed with /usr/include ?

What does pkg-config --libs libxml2 tell you?

hans-m-song commented 2 years ago

Here is the output:

# pkg-config --libs libxml2
Package libxml2 was not found in the pkg-config search path.
Perhaps you should add the directory containing `libxml2.pc'
to the PKG_CONFIG_PATH environment variable
No package 'libxml2' found

I take it you are expecting libxml2 instead of libxml-2.0?

When building with -x

# go build -x
WORK=/tmp/go-build3518064176
mkdir -p $WORK/b003/
cd /root/go/pkg/mod/github.com/lestrrat/go-libxml2@v0.0.0-20180221004755-bb78334e2019/clib
pkg-config --cflags -- libxml-2.0
pkg-config --libs -- libxml-2.0
TERM='dumb' CGO_LDFLAGS='"-g" "-O2" "-lxml2"' /usr/local/go/pkg/tool/linux_amd64/cgo -objdir $WORK/b003/ -importpath github.com/lestrrat/go-libxml2/clib -- -I/usr/include/libxml2 -I $WORK/b003/ -g -O2 ./clib.go
# github.com/lestrrat/go-libxml2/clib
In file included from /usr/include/libxml2/libxml/parser.h:810,
                 from /usr/include/libxml2/libxml/HTMLparser.h:16,
                 from /root/go/pkg/mod/github.com/lestrrat/go-libxml2@v0.0.0-20180221004755-bb78334e2019/clib/clib.go:28:
/usr/include/libxml2/libxml/encoding.h:28:10: fatal error: iconv.h: No such file or directory
   28 | #include <iconv.h>
      |          ^~~~~~~~~
compilation terminated.

It references /usr/include/libxml2 which I can verify does contain header files

lestrrat commented 2 years ago

Oh, now that I think about it, did you install libxml2-dev or libxml2-devel or whatever it is?

hans-m-song commented 2 years ago

Yes I have, if you have a look at the Dockerfile, I have installed

lestrrat commented 2 years ago

Oh, hmm... sorry now I'm stumped. :/

hans-m-song commented 2 years ago

No worries, I figured it's not really an issue with your package (which I have found quite useful, thanks for that!). I will keep digging. Feel free to close this.

github-actions[bot] commented 11 months ago

This issue is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 7 days.

github-actions[bot] commented 11 months ago

This issue was closed because it has been stalled for 7 days with no activity. This does not mean your issue is rejected, but rather it is done to hide it from the view of the maintains for the time being. Feel free to reopen if you have new comments