Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cross-compiling from linux to windows #83

Closed
hans-m-song opened this issue Jun 19, 2022 · 8 comments
Closed

Cross-compiling from linux to windows #83

hans-m-song opened this issue Jun 19, 2022 · 8 comments
Labels

Comments

@hans-m-song
Copy link

hans-m-song commented Jun 19, 2022

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 #80 (I have encountered their issue as well).

@lestrrat
Copy link
Collaborator

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
Copy link
Author

hans-m-song commented Jun 19, 2022

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
Copy link
Collaborator

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

@hans-m-song
Copy link
Author

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

  • pkg-config
  • gcc-mingw-w64 for cross-compilation
  • libc6, libc6-dev which I believe contains libiconv
  • libxml2, libxml2-dev

@lestrrat
Copy link
Collaborator

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

@hans-m-song
Copy link
Author

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.

Copy link

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 github-actions bot added the Stale label Nov 25, 2023
Copy link

github-actions bot commented Dec 2, 2023

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

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Dec 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants