lestrrat-go / libxml2

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

static build problems #62

Closed airedwin closed 4 years ago

airedwin commented 4 years ago

When building with CGO_ENABLED=0

I'm getting

vendor/github.com/lestrrat-go/libxml2/xpath/xpath.go:40:30: undefined: clib.XMLXPathObjectType vendor/github.com/lestrrat-go/libxml2/xpath/xpath.go:45:9: undefined: clib.XMLXPathObjectFloat64 vendor/github.com/lestrrat-go/libxml2/xpath/xpath.go:50:9: undefined: clib.XMLXPathObjectBool vendor/github.com/lestrrat-go/libxml2/xpath/xpath.go:74:13: undefined: clib.XMLXPathObjectNodeList vendor/github.com/lestrrat-go/libxml2/xpath/xpath.go:91:13: undefined: clib.XMLXPathObjectNodeList vendor/github.com/lestrrat-go/libxml2/xpath/xpath.go:124:2: undefined: clib.XMLXPathFreeObject vendor/github.com/lestrrat-go/libxml2/xpath/xpath.go:129:14: undefined: clib.XMLXPathCompile vendor/github.com/lestrrat-go/libxml2/xpath/xpath.go:149:2: undefined: clib.XMLXPathFreeCompExpr vendor/github.com/lestrrat-go/libxml2/xpath/xpath.go:163:17: undefined: clib.XMLXPathNewContext vendor/github.com/lestrrat-go/libxml2/xpath/xpath.go:179:9: undefined: clib.XMLXPathContextSetContextNode vendor/github.com/lestrrat-go/libxml2/xpath/xpath.go:179:9: too many errors

github.com/18F/e-QIP-prototype/api/vendor/github.com/lestrrat-go/libxml2/xsd

vendor/github.com/lestrrat-go/libxml2/xsd/xsd.go:29:15: undefined: clib.XMLSchemaParse vendor/github.com/lestrrat-go/libxml2/xsd/xsd.go:40:15: undefined: clib.XMLSchemaParseFromFile vendor/github.com/lestrrat-go/libxml2/xsd/xsd.go:55:12: undefined: clib.XMLSchemaFree vendor/github.com/lestrrat-go/libxml2/xsd/xsd.go:65:10: undefined: clib.XMLSchemaValidateDocument

lestrrat commented 4 years ago

This is a binding to libxml2, which requires cgo so CGO_ENABLED=0 does not make sense.

airedwin commented 4 years ago

This is a binding to libxml2, which requires cgo so CGO_ENABLED=0 does not make sense.

you are right, I thought I edited this post, I get errors when trying to make a static build with go build -ldflags '-w -extldflags "-static"'

lestrrat commented 4 years ago

Without thinking I just copied your command: go build -w -ldflags '-extldflags "-static"' and got

go build -w -ldflags '-extldflags "-static"'
flag provided but not defined: -w
usage: go build [-o output] [-i] [build flags] [packages]
Run 'go help build' for details.

I understand that the above could just be a typo and what not, but just to make sure, can you kindly reproduce this again and paste the actual go command invocation plus the associated logs (doesn't need to be the entire log, just a few lines is fine)?

airedwin commented 4 years ago

Without thinking I just copied your command: go build -w -ldflags '-extldflags "-static"' and got go build -w -ldflags '-extldflags "-static"' flag provided but not defined: -w usage: go build [-o output] [-i] [build flags] [packages] Run 'go help build' for details.

I understand that the above could just be a typo and what not, but just to make sure, can you kindly reproduce this again and paste the actual go command invocation plus the associated logs (doesn't need to be the entire log, just a few lines is fine)?

thanks for the help, here is the log, works fine when building without static, but I believe we had it configured to try to build static when deployed in docker

go build -ldflags '-w -extldflags "-static"' -o ./bin/app ./cmd/app

/usr/local/go/pkg/tool/linux_amd64/link: running gcc failed: exit status 1 /tmp/go-link-354092682/000021.o: In function mygetgrouplist': /workdir/go/src/os/user/getgrouplist_unix.go:16: warning: Using 'getgrouplist' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking /tmp/go-link-354092682/000020.o: In functionmygetgrgid_r': /workdir/go/src/os/user/cgo_lookup_unix.go:38: warning: Using 'getgrgid_r' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking /tmp/go-link-354092682/000020.o: In function mygetgrnam_r': /workdir/go/src/os/user/cgo_lookup_unix.go:43: warning: Using 'getgrnam_r' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking /tmp/go-link-354092682/000020.o: In functionmygetpwnam_r': /workdir/go/src/os/user/cgo_lookup_unix.go:33: warning: Using 'getpwnam_r' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking /tmp/go-link-354092682/000020.o: In function mygetpwuid_r': /workdir/go/src/os/user/cgo_lookup_unix.go:28: warning: Using 'getpwuid_r' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking /tmp/go-link-354092682/000004.o: In function_cgo_7e1b3c2abc8d_C2func_getaddrinfo': /tmp/go-build/cgo-gcc-prolog:57: warning: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking /usr/lib/gcc/x86_64-linux-gnu/6/../../../x86_64-linux-gnu/libxml2.a(nanohttp.o): In function `xmlNanoHTTPConnectHost': (.text+0x9ca): warning: Using 'gethostbyname' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking

lestrrat commented 4 years ago

I have a feeling it's because of this https://github.com/golang/go/issues/26492

Seems like the full solution hasn't landed yet, and for now it will probably be easier if you just edit (possibly via a custom build script that gets invoked when you build your tool) the file clib/clib.go and change

#cgo pkg-config: libxml-2.0

to

#cgo pkg-config: --static libxml-2.0
airedwin commented 4 years ago

I have a feeling it's because of this golang/go#26492 Seems like the full solution hasn't landed yet, and for now it will probably be easier if you just edit (possibly via a custom build script that gets invoked when you build your tool) the file clib/clib.go and change

cgo pkg-config: libxml-2.0

to

cgo pkg-config: --static libxml-2.0

thanks, I saw that issue as well, wasn't sure if it was related i'm doing normal builds for now but i'm not sure if that will affect anything, so far it's fine we run dep ensure in our deployments so i'm not certain I can modify clib.go without it getting changed back, is there something I need to configure... maybe the lockfile?

lestrrat commented 4 years ago

If I were you I'd just bite the bullet and run dep ensure then modify the file inplace using sed or perl, then build. I know, not an ideal solution but it should work

leonged commented 4 years ago

not sure if that worked

$ mv clib.go.custom /go/src/github.com/myapp/vendor/github.com/lestrrat-go/libxml2/clib/clib.go $ export GOOS=linux $ export GOARCH=amd64 $ go build -ldflags '-w -extldflags "-static"' -o ./bin/myapp ./myapp

/usr/local/go/pkg/tool/linux_amd64/link: running gcc failed: exit status 1 /tmp/go-link-736995541/000021.o: In function mygetgrouplist': /workdir/go/src/os/user/getgrouplist_unix.go:16: warning: Using 'getgrouplist' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking /tmp/go-link-736995541/000020.o: In functionmygetgrgid_r': /workdir/go/src/os/user/cgo_lookup_unix.go:38: warning: Using 'getgrgid_r' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking /tmp/go-link-736995541/000020.o: In function mygetgrnam_r': /workdir/go/src/os/user/cgo_lookup_unix.go:43: warning: Using 'getgrnam_r' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking /tmp/go-link-736995541/000020.o: In functionmygetpwnam_r': /workdir/go/src/os/user/cgo_lookup_unix.go:33: warning: Using 'getpwnam_r' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking /tmp/go-link-736995541/000020.o: In function mygetpwuid_r': /workdir/go/src/os/user/cgo_lookup_unix.go:28: warning: Using 'getpwuid_r' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking /tmp/go-link-736995541/000004.o: In function_cgo_7e1b3c2abc8d_C2func_getaddrinfo': /tmp/go-build/cgo-gcc-prolog:57: warning: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking /usr/lib/gcc/x86_64-linux-gnu/6/../../../x86_64-linux-gnu/libxml2.a(nanohttp.o): In function xmlNanoHTTPConnectHost': (.text+0x9ca): warning: Using 'gethostbyname' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking /usr/lib/gcc/x86_64-linux-gnu/6/../../../x86_64-linux-gnu/libicuuc.a(putil.ao): In functionuprv_dl_open_63': (.text+0x1952): undefined reference to dlopen' /usr/lib/gcc/x86_64-linux-gnu/6/../../../x86_64-linux-gnu/libicuuc.a(putil.ao): In functionuprv_dlsym_func_63': (.text+0x199d): undefined reference to dlsym' /usr/lib/gcc/x86_64-linux-gnu/6/../../../x86_64-linux-gnu/libicuuc.a(putil.ao): In functionuprv_dl_close_63': (.text+0x1981): undefined reference to dlclose' /usr/lib/gcc/x86_64-linux-gnu/6/../../../x86_64-linux-gnu/libicuuc.a(uobject.ao): In functionicu_63::UMemory::operator new(unsigned long) [clone .cold.0]': (.text.unlikely+0xc): undefined reference to __cxa_call_unexpected' /usr/lib/gcc/x86_64-linux-gnu/6/../../../x86_64-linux-gnu/libicuuc.a(uobject.ao): In functionicu_63::UMemory::operator delete(void*) [clone .cold.1]': (.text.unlikely+0x1c): undefined reference to __cxa_call_unexpected' /usr/lib/gcc/x86_64-linux-gnu/6/../../../x86_64-linux-gnu/libicuuc.a(uobject.ao):(.data.rel.ro._ZTIN6icu_637UMemoryE[_ZTIN6icu_637UMemoryE]+0x0): undefined reference tovtable for cxxabiv1::class_type_info' /usr/lib/gcc/x86_64-linux-gnu/6/../../../x86_64-linux-gnu/libicuuc.a(uobject.ao):(.data.rel.ro._ZTIN6icu_637UObjectE[_ZTIN6icu_637UObjectE]+0x0): undefined reference to vtable for __cxxabiv1::__si_class_type_info' /usr/lib/gcc/x86_64-linux-gnu/6/../../../x86_64-linux-gnu/libicuuc.a(ucln_cmn.ao):(.data.rel.local.DW.ref.__gxx_personality_v0[DW.ref.__gxx_personality_v0]+0x0): undefined reference to__gxx_personality_v0'

lestrrat commented 4 years ago

There are two problems that you can see in the logs: os/user, and libicu. The os/user problem seems to be referenced from the same issue golang/go#26492, and seems to stem from probably the same cause, and you probably need to use the incantation referenced in the original post on that same issue (or, stop using os/user).

For libicu, I'm not sure, but it's something from libxml2 -- I don't get that in my environment :/

lestrrat commented 4 years ago

I referenced this in the README. I believe there's little I can do at this point from libxml2 binding's side to help you guys, so I'm going to close this for now. Feel free to reopen it if you believe that I'm doing something wrong and that I can make things a bit easier for you. Thanks.

andreymazuro commented 3 years ago

@leonged did you manage to solve this issue?

airedwin commented 3 years ago

@leonged did you manage to solve this issue?

I don't remember what I did. I think I just scrapped it.