From 93e0636c039deb6fc3f489851d4a8ef4db3ea89e Mon Sep 17 00:00:00 2001 From: Pure White Date: Wed, 12 Jan 2022 19:10:08 +0800 Subject: [PATCH] feat: support build on darwin arm64 (#2439) main: support build on darwin arm64 --- Makefile | 1 + builder/commands.go | 12 +++++++++++- cgo/libclang_config.go | 14 ++++++++------ cgo/libclang_config_llvm12.go | 18 ++++++++++-------- cgo/libclang_config_llvm13.go | 14 ++++++++------ 5 files changed, 38 insertions(+), 21 deletions(-) diff --git a/Makefile b/Makefile index 4bf5a215..22ba40dd 100644 --- a/Makefile +++ b/Makefile @@ -9,6 +9,7 @@ CLANG_SRC ?= $(LLVM_PROJECTDIR)/clang LLD_SRC ?= $(LLVM_PROJECTDIR)/lld # Try to autodetect LLVM build tools. +# FIXME(#2438): This doesn't work on darwin now. Homebrew installed llvm is key-only. detect = $(shell command -v $(1) 2> /dev/null && echo $(1)) CLANG ?= $(word 1,$(abspath $(call detect,llvm-build/bin/clang))$(call detect,clang-13)$(call detect,clang-12)$(call detect,clang-11)$(call detect,clang)) LLVM_AR ?= $(word 1,$(abspath $(call detect,llvm-build/bin/llvm-ar))$(call detect,llvm-ar-13)$(call detect,llvm-ar-12)$(call detect,llvm-ar-11)$(call detect,llvm-ar)) diff --git a/builder/commands.go b/builder/commands.go index 9b167c6f..110ab4ac 100644 --- a/builder/commands.go +++ b/builder/commands.go @@ -2,6 +2,7 @@ package builder import ( "errors" + "fmt" "os" "os/exec" "runtime" @@ -25,7 +26,16 @@ func init() { // Add the path to a Homebrew-installed LLVM for ease of use (no need to // manually set $PATH). if runtime.GOOS == "darwin" { - prefix := "/usr/local/opt/llvm@" + llvmMajor + "/bin/" + var prefix string + switch runtime.GOARCH { + case "amd64": + prefix = "/usr/local/opt/llvm@" + llvmMajor + "/bin/" + case "arm64": + prefix = "/opt/homebrew/opt/llvm@" + llvmMajor + "/bin/" + default: + // unknown GOARCH + panic(fmt.Sprintf("unknown GOARCH: %s on darwin", runtime.GOARCH)) + } commands["clang"] = append(commands["clang"], prefix+"clang-"+llvmMajor) commands["ld.lld"] = append(commands["ld.lld"], prefix+"ld.lld") commands["wasm-ld"] = append(commands["wasm-ld"], prefix+"wasm-ld") diff --git a/cgo/libclang_config.go b/cgo/libclang_config.go index ed929f79..14a065aa 100644 --- a/cgo/libclang_config.go +++ b/cgo/libclang_config.go @@ -4,11 +4,13 @@ package cgo /* -#cgo linux CFLAGS: -I/usr/lib/llvm-11/include -#cgo darwin CFLAGS: -I/usr/local/opt/llvm@11/include -#cgo freebsd CFLAGS: -I/usr/local/llvm11/include -#cgo linux LDFLAGS: -L/usr/lib/llvm-11/lib -lclang -#cgo darwin LDFLAGS: -L/usr/local/opt/llvm@11/lib -lclang -lffi -#cgo freebsd LDFLAGS: -L/usr/local/llvm11/lib -lclang +#cgo linux CFLAGS: -I/usr/lib/llvm-11/include +#cgo darwin amd64 CFLAGS: -I/usr/local/opt/llvm@11/include +#cgo darwin arm64 CFLAGS: -I/opt/homebrew/opt/llvm@11/include +#cgo freebsd CFLAGS: -I/usr/local/llvm11/include +#cgo linux LDFLAGS: -L/usr/lib/llvm-11/lib -lclang +#cgo darwin amd64 LDFLAGS: -L/usr/local/opt/llvm@11/lib -lclang -lffi +#cgo darwin arm64 LDFLAGS: -L/opt/homebrew/opt/llvm@11/lib -lclang -lffi +#cgo freebsd LDFLAGS: -L/usr/local/llvm11/lib -lclang */ import "C" diff --git a/cgo/libclang_config_llvm12.go b/cgo/libclang_config_llvm12.go index 1837cc15..69808d2e 100644 --- a/cgo/libclang_config_llvm12.go +++ b/cgo/libclang_config_llvm12.go @@ -1,14 +1,16 @@ -// +build !byollvm -// +build llvm12 +//go:build !byollvm && llvm12 +// +build !byollvm,llvm12 package cgo /* -#cgo linux CFLAGS: -I/usr/lib/llvm-12/include -#cgo darwin CFLAGS: -I/usr/local/opt/llvm@12/include -#cgo freebsd CFLAGS: -I/usr/local/llvm12/include -#cgo linux LDFLAGS: -L/usr/lib/llvm-12/lib -lclang -#cgo darwin LDFLAGS: -L/usr/local/opt/llvm@12/lib -lclang -lffi -#cgo freebsd LDFLAGS: -L/usr/local/llvm12/lib -lclang +#cgo linux CFLAGS: -I/usr/lib/llvm-12/include +#cgo darwin amd64 CFLAGS: -I/usr/local/opt/llvm@12/include +#cgo darwin arm64 CFLAGS: -I/opt/homebrew/opt/llvm@12/include +#cgo freebsd CFLAGS: -I/usr/local/llvm12/include +#cgo linux LDFLAGS: -L/usr/lib/llvm-12/lib -lclang +#cgo darwin amd64 LDFLAGS: -L/usr/local/opt/llvm@12/lib -lclang -lffi +#cgo darwin arm64 LDFLAGS: -L/opt/homebrew/opt/llvm@12/lib -lclang -lffi +#cgo freebsd LDFLAGS: -L/usr/local/llvm12/lib -lclang */ import "C" diff --git a/cgo/libclang_config_llvm13.go b/cgo/libclang_config_llvm13.go index 3c961a3f..81c11e67 100644 --- a/cgo/libclang_config_llvm13.go +++ b/cgo/libclang_config_llvm13.go @@ -4,11 +4,13 @@ package cgo /* -#cgo linux CFLAGS: -I/usr/lib/llvm-13/include -#cgo darwin CFLAGS: -I/usr/local/opt/llvm@13/include -#cgo freebsd CFLAGS: -I/usr/local/llvm13/include -#cgo linux LDFLAGS: -L/usr/lib/llvm-13/lib -lclang -#cgo darwin LDFLAGS: -L/usr/local/opt/llvm@13/lib -lclang -lffi -#cgo freebsd LDFLAGS: -L/usr/local/llvm13/lib -lclang +#cgo linux CFLAGS: -I/usr/lib/llvm-13/include +#cgo darwin amd64 CFLAGS: -I/usr/local/opt/llvm@13/include +#cgo darwin arm64 CFLAGS: -I/opt/homebrew/opt/llvm@13/include +#cgo freebsd CFLAGS: -I/usr/local/llvm13/include +#cgo linux LDFLAGS: -L/usr/lib/llvm-13/lib -lclang +#cgo darwin amd64 LDFLAGS: -L/usr/local/opt/llvm@13/lib -lclang -lffi +#cgo darwin arm64 LDFLAGS: -L/opt/homebrew/opt/llvm@13/lib -lclang -lffi +#cgo freebsd LDFLAGS: -L/usr/local/llvm13/lib -lclang */ import "C"