burrowers / garble

Obfuscate Go builds
BSD 3-Clause "New" or "Revised" License
3.93k stars 248 forks source link

Compiling for macos/darwin on linux #836

Closed FuckingToasters closed 5 months ago

FuckingToasters commented 7 months ago

What version of Garble and Go are you using?

$ garble version
mvdan.cc/garble v0.12.1-0.20240218112843-66b61406c129

Build settings:
      -buildmode exe
       -compiler gc
     CGO_ENABLED 1
          GOARCH amd64
            GOOS linux
         GOAMD64 v1

$ go version
go version go1.22.0 linux/amd64

What environment are you running Garble on?

go env Output
$ go env
GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/root/.cache/go-build'
GOENV='/root/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/root/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/root/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.22.0'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/root/tools/genetools/genefit/velocity/src/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build2762902091=/tmp/go-build -gno-record-gcc-switches'

What did you do?

I ran a little build.py script which performs the build process for different systems using garble, everythign except macos worked as expected.

What did you expect to see?

I expected that the compilation for macos worked the same way it did for the other systems.

What did you see instead?

First i got an error that clang is not found in PATH so i got rid of this error by simply installing it, after installing it and trying to run it again, i got following error:

🔄  Building with GOARCH: amd64 and GOOS: darwin
-seed chosen at random: NMj54tbsLnBiTFqmoMqx/A
runtime/cgo

clang: error: argument unused during compilation: '-arch x86_64' [-Werror,-Wunused-command-line-argument]
exit status 2
exit status 1

The garble command used by the build.py script were garble -literals -tiny -seed=random build -ldflags="-w -s" -o cache/ I also tried to run it without the -ldflags which caused the same issue.

FuckingToasters commented 7 months ago

A Qucik fix i found is following (basically disables CGO for darwin):

    # Setup the proper C compiler based on target OS
    if GOOS == "darwin":
        #os.environ["CC"] = "clang"
        os.environ["CGO_ENABLED"] = "0"
    else:
        os.environ["CC"] = "gcc"
        os.environ["CGO_ENABLED"] = "1"

But it's not an permanent solution

mvdan commented 6 months ago

Thanks, here's a simple reproducer:

$ GOOS=linux GOARCH=amd64 CGO_ENABLED=1 garble build runtime/cgo
$ echo $?
0
$ GOOS=darwin GOARCH=amd64 CGO_ENABLED=1 garble build runtime/cgo
# runtime/cgo
clang: error: argument unused during compilation: '-arch x86_64' [-Werror,-Wunused-command-line-argument]
exit status 2
exit status 1 
mvdan commented 6 months ago

I don't think this issue is at all related to garble, as I see it with plain Go 1.22.1 as well:

$ GOOS=darwin GOARCH=amd64 CGO_ENABLED=1 go build runtime/cgo
# runtime/cgo
clang: error: argument unused during compilation: '-arch x86_64' [-Werror,-Wunused-command-line-argument]
mvdan commented 5 months ago

Closing as unrelated to garble.