Last active
August 16, 2021 10:22
-
-
Save shravanasati/fba56ae3802dbf07106597f4aab5d2f2 to your computer and use it in GitHub Desktop.
A python script to cross compile go executables parallely.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
import os | |
from multiprocessing import Process | |
from typing import List | |
def build(appname:str, platform: str) -> None: | |
try: | |
goos = platform.split("/")[0] | |
goarch = platform.split("/")[1] | |
print(f"==> 🚧 Building executable for `{platform}`...") | |
os.environ["GOOS"] = goos | |
os.environ["GOARCH"] = goarch | |
outpath = f"./bin/{appname}-{goos}-{goarch}" | |
if goos == "windows": | |
outpath += ".exe" | |
subprocess.run(["go", "build", "-v", "-o", outpath, "."]) | |
print(f"==> ✅ Built executable for `{platform}` at `{outpath}`.") | |
except Exception as e: | |
print(e) | |
print("==> ❌ An error occured! Aborting script execution.") | |
os._exit(1) | |
if __name__ == "__main__": | |
# add all platforms to the tuple you want to build | |
platforms = ("windows/amd64", "linux/amd64", "darwin/amd64") | |
appname = "appname" # name of the executable | |
multithreaded = False # set to True to enable multithreading | |
if multithreaded: | |
threads: List[Process] = [] | |
for p in platforms: | |
threads.append(Process(target=build, args=(appname, p))) | |
for t in threads: | |
t.start() | |
for t in threads: | |
t.join() | |
else: | |
for p in platforms: | |
build(appname, p) | |
print(f"==> 👍 Built executables for {len(platforms)} platforms!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment