feat: add universal build script

This commit is contained in:
yicheng 2020-11-18 10:39:00 +08:00
parent f878cc4562
commit a0daee3a3c
3 changed files with 94 additions and 2 deletions

View File

@ -15,8 +15,7 @@ def build_clash(version):
build_time = datetime.datetime.now().strftime("%Y-%m-%d-%H%M")
command = f"""CGO_CFLAGS=-mmacosx-version-min=10.12 \
CGO_LDFLAGS=-mmacosx-version-min=10.12 \
GOBUILD=CGO_ENABLED=0 \
go build -ldflags '-X "github.com/Dreamacro/clash/constant.Version={version}" \
go build -trimpath -ldflags '-X "github.com/Dreamacro/clash/constant.Version={version}" \
-X "github.com/Dreamacro/clash/constant.BuildTime={build_time}"' \
-buildmode=c-archive -o goClash.a """
subprocess.check_output(command, shell=True)

View File

@ -0,0 +1,81 @@
import subprocess
import datetime
import plistlib
import os
import filecmp
def get_version():
with open('./go.mod') as file:
for line in file.readlines():
if "clash" in line and "ClashX" not in line:
return line.split("-")[-1].strip()[:6]
return "unknown"
def build_clash(version,build_time,arch):
clang = f"{os.getcwd()}/clangWrap.sh"
command = f"""
go build -trimpath -ldflags '-X "github.com/Dreamacro/clash/constant.Version={version}" \
-X "github.com/Dreamacro/clash/constant.BuildTime={build_time}"' \
-buildmode=c-archive -o goClash_{arch}.a """
envs = os.environ.copy()
envs.update({
"CC":clang,
"CXX":clang,
"GOOS":"darwin",
"GOARCH":arch,
"CGO_ENABLED":"1",
"CGO_LDFLAGS":"-mmacosx-version-min=10.12",
"CGO_CFLAGS":"-mmacosx-version-min=10.12",
})
subprocess.check_output(command, shell=True,env=envs)
def mergeLibs():
if not filecmp.cmp('goClash_amd64.h','goClash_arm64.h'):
exit(-1)
os.rename('goClash_amd64.h', 'goClash.h')
command = "lipo *.a -create -output goClash.a"
subprocess.check_output(command, shell=True)
def clean():
cmd = "rm -f *amd* *arm*"
subprocess.check_output(cmd, shell=True)
def write_to_info(version):
path = "../info.plist"
with open(path, 'rb') as f:
contents = plistlib.load(f)
if not contents:
exit(-1)
contents["coreVersion"] = version
with open(path, 'wb') as f:
plistlib.dump(contents, f, sort_keys=False)
def run():
version = get_version()
print("current clash version:", version)
build_time = datetime.datetime.now().strftime("%Y-%m-%d-%H%M")
print("clean existing")
subprocess.check_output("rm -f *.h *.a", shell=True)
print("create arm64")
build_clash(version,build_time,"arm64")
print("create amd64")
build_clash(version,build_time,"amd64")
print("merge")
mergeLibs()
print("clean")
clean()
if os.environ.get("CI", False) or os.environ.get("GITHUB_ACTIONS", False):
print("writing info.plist")
write_to_info(version)
print("done")
if __name__ == "__main__":
run()

12
ClashX/goClash/clangWrap.sh Executable file
View File

@ -0,0 +1,12 @@
#!/bin/sh
SDK=macosx
PLATFORM=macosx
if [ "$GOARCH" == "arm64" ]; then
CLANGARCH="arm64"
else
CLANGARCH="x86_64"
fi
SDK_PATH=`xcrun --sdk $SDK --show-sdk-path`
CLANG=`xcrun --sdk $SDK --find clang`
exec "$CLANG" -arch $CLANGARCH -isysroot "$SDK_PATH" -mmacosx-version-min=10.12 "$@"