虽然标题有些奇葩,但是对于我这个强迫症来说,每次golang版本更新,第一件事就是把常见的交叉编译环境给弄好,每次都写也挺麻烦,再加上随着golang版本的升级,支持的GOARCH和GOOS也不断的变多,也懒得去查wiki或者手册,就直接用下面的代码生成吧#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
__author__ = 'Chungpei Wang'
import os
import json
if __name__ == '__main__':
env_dist = os.environ
goroot = env_dist['GOROOT']
gocmd = os.path.join(goroot, "bin", "go")
result = os.popen(gocmd + " tool dist list -json")
strresult = result.read()
jsonresult = json.loads(strresult)
shell_content = "#!/bin/bash" + os.linesep * 2 + "export CGO_ENABLED=0" + os.linesep * 2
# 仅仅是因为强迫症,按照GOARCH进行排序,这样看起来整齐点
jsonresult.sort(key=lambda k: (k.get('GOARCH', 0)))
for data in jsonresult:
shell_content += "export GOARCH=" + data['GOARCH'] + os.linesep
shell_content += "export GOOS=" + data['GOOS'] + os.linesep
shell_content += "./make.bash --no-clean" + os.linesep
shell_content += os.linesep
print(shell_content)
即:1.先从系统环境变量中获取到$GOROOT
2.再执行$GOROOT/bin/go tool list list -json,获取对应版本golang所支持的所有GOARCH和GOOS
3.依次export GOARCH、GOOS,并执行make.bash(windows下应该改为make.bat)