[GODRIVER-1660] Cannot Cross Compiling from Windows to Linux Using Tags CSE Created: 19/Jun/20  Updated: 27/Oct/23  Resolved: 25/Jun/20

Status: Closed
Project: Go Driver
Component/s: Internal
Affects Version/s: 1.4.0
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Apartemenku Ap Assignee: Divjot Arora (Inactive)
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows amd64, go version go1.14.4 windows/amd64, mongo-go-driver version v1.4.0+prerelease


Attachments: PNG File error_mongo.png    

 Description   

Currently, I have successfully built an API server with MongoDB CSFLE on Windows Machine for "Windows OS" using this command:

set GOOS = windows, GOARCH = amd64

go build -tags cse github.com\user\example_app

output: example_app.exe

 

And then, I try to cross compiling the application for linux with same command,

set GOOS=linux, GOARCH = amd64

go build -tags cse github.com\user\example_app

output: build failed (screenshot attached)

Please help me to solve this issue,

Thanks in advance

 



 Comments   
Comment by Divjot Arora (Inactive) [ 25/Jun/20 ]

Thanks for your response! I agree that setting up a Linux environment is a much simpler solution than trying to cross-compile from Windows. Because this didn't end up being a driver bug, I'm going to close out this ticket as "Works as Designed". Feel free to leave more comments if you need any help building the driver with CSFLE on Linux, though, and we'd be happy to help out.

– Divjot

Comment by Apartemenku Ap [ 25/Jun/20 ]

Hi Divjot Arora,

Yes, I think so, it's about host environment issue and libmongocrypt need specific packages to compile in Linux OS. So I will try to set Linux environment for build this program.

Thank you for your help, really appreciate it 

Comment by Divjot Arora (Inactive) [ 24/Jun/20 ]

The closest I found to that error was https://stackoverflow.com/questions/48015645/unknown-type-when-compiling-xz-utils-on-mingw-msys2, which talks about the mingw compiler.

Overall, though, I don't think this is a driver bug. I don't have much experience with cross-compilation, but most of the tutorials I'm seeing are for Linux systems, not Windows. My understanding is that you'd have to at least get a Linux toolchain for compiling C. You may also have to set up pkg-config because the driver uses it on Linux and Darwin systems to find the headers and flags needed to compile libmongocrypt and you'd probably have to install libmongocrypt so all of the pkg-config files are in the right place (see https://github.com/mongodb/libmongocrypt#installing-libmongocrypt-from-distribution-packages).

Comment by Apartemenku Ap [ 24/Jun/20 ]

Hi Divjot Arora,

It seems correct after I set CGO_ENABLED = 1 and run build command:

I got this error:

  1. runtime/cgo
    gcc_linux_amd64.c: In function '_cgo_sys_thread_start':
    gcc_linux_amd64.c:61:2: error: unknown type name 'sigset_t'; did you mean '_sigset_t'?
    61 | sigset_t ign, oset;
    ^~~~~~~~
    _sigset_t
    gcc_linux_amd64.c:66:2: error: implicit declaration of function 'sigfillset' [-Werror=implicit-function-declaration]
    66
    sigfillset(&ign);
    ^~~~~~~~~~
    gcc_linux_amd64.c:61:16: error: unused variable 'oset' [-Werror=unused-variable]
    61
    sigset_t ign, oset;
    ^~~~
    cc1.exe: all warnings being treated as errors

I have searched this error in google, but can't find the solution.

Comment by Divjot Arora (Inactive) [ 23/Jun/20 ]

Hi app.apartemenku@gmail.com,

We don't test cross-compiling cgo-based features in our CI, but this paragraph from https://golang.org/cmd/cgo/#hdr-Using_cgo_with_the_go_command seems like a good start:

The cgo tool is enabled by default for native builds on systems where it is expected to work. It is disabled by default when cross-compiling. You can control this by setting the CGO_ENABLED environment variable when running the go tool: set it to 1 to enable the use of cgo, and to 0 to disable it. The go tool will set the build constraint "cgo" if cgo is enabled. The special import "C" implies the "cgo" build constraint, as though the file also said "// +build cgo". Therefore, if cgo is disabled, files that import "C" will not be built by the go tool. (For more about build constraints see https://golang.org/pkg/go/build/#hdr-Build_Constraints).

When cross-compiling, you must specify a C cross-compiler for cgo to use. You can do this by setting the generic CC_FOR_TARGET or the more specific CC_FOR_${GOOS}_${GOARCH} (for example, CC_FOR_linux_arm) environment variable when building the toolchain using make.bash, or you can set the CC environment variable any time you run the go tool.

My guess is that all of the undefined type errors are due to the fact that cgo is disabled when cross-compiling. So to start, you can try setting CGO_ENABLED=1. Based on the second paragraph, you might also have to set CC to specify which compiler you want to use (like gcc if your target GOOS is linux?). Can you try these suggestions and let us know if you still have issues? Hopefully doing these will at least provide better error messages.

– Divjot

Generated at Thu Feb 08 08:36:52 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.