Last active
May 14, 2021 00:45
-
-
Save maccam912/42c239edbe0480b7e1c1eb70e40b7bad to your computer and use it in GitHub Desktop.
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
def check_for_dependencies(): | |
# Dependency on python should be good if we make it this far. Python 3.7+ required for this script. | |
all_good = True | |
try: | |
# Check for requests, which this script uses | |
import requests | |
print("Found requests") | |
except: | |
print("Please install requests python library, `pip install requests`.") | |
all_good = False | |
try: | |
# Check for cmake | |
import subprocess | |
p = subprocess.run(['cmake', '--version'], capture_output=True) | |
if p.returncode != 0: | |
print("Please install cmake 3.17+. Cmake not found on the system, or in the path checked by Python.") | |
all_good = False | |
version = p.stdout.decode("utf-8").split("\n")[0].split(" ")[-1] | |
print("Found cmake version %s" % version) | |
major = version.split(".")[0] | |
minor = version.split(".")[1] | |
if int(float(major)) < 3 and int(float(minor)) < 17: | |
print("Please install cmake 3.17+. We only found %s" % version) | |
all_good = False | |
except e: | |
print("Error occured checking for cmake: %s" % e) | |
all_good = False | |
return all_good | |
def download(url): | |
filename = url.split("/")[-1] | |
print("Downloading %s..." % filename) | |
r = requests.get(url, allow_redirects=True) | |
with open(filename, 'wb') as f: | |
f.write(r.content) | |
return filename | |
def extract_file(filename): | |
productname = filename.replace(".tar.xz", "") | |
print("Extracting %s..." % filename) | |
tf = tarfile.open(filename, 'r') | |
tf.extractall(".") | |
print("Extracted %s to %s" % (filename, productname)) | |
return productname | |
def download_and_extract(url): | |
filename = download(url) | |
productname = extract_file(filename) | |
directory = Path('./%s/build' % productname) | |
os.makedirs(directory) | |
return productname | |
def maybe_download_and_extract(url): | |
filename = url.split("/")[-1] | |
productname = filename.replace(".tar.xz", "") | |
if os.path.exists(Path("./%s/build" % productname)): | |
print("Already downloaded and extracted") | |
return productname | |
else: | |
return download_and_extract(url) | |
win_command = "cmake .. -Thost=x64 -G \"Visual Studio 16 2019\" -A x64 -DCMAKE_INSTALL_PREFIX=~/local -DCMAKE_PREFIX_PATH=~/local -DCMAKE_BUILD_TYPE=Release -DLLVM_CONFIG_PATH=~/local/bin/llvm-config -DLLVM_ENABLE_LIBXML2=OFF -DLLVM_USE_CRT_RELEASE=MT" | |
unix_command = "cmake .. -DCMAKE_INSTALL_PREFIX=~/local -DCMAKE_PREFIX_PATH=~/local -DCMAKE_BUILD_TYPE=Release -DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD=AVR -DLLVM_ENABLE_LIBXML2=OFF -DCMAKE_CXX_FLAGS=-std=c++14 -DCMAKE_CXX_STANDARD=14" | |
def setup(product): | |
command = '' | |
if os.name == 'nt': | |
# On windows | |
command = win_command | |
else: | |
command = unix_command | |
directory = Path('./%s/build' % product) | |
p = subprocess.run(shlex.split(command), cwd=directory) | |
if p.returncode != 0: | |
exit() | |
def build(product): | |
command = "cmake --build . -j" | |
directory = Path('./%s/build' % product) | |
p = subprocess.run(shlex.split(command), cwd=directory) | |
if p.returncode != 0: | |
exit() | |
def install(product): | |
command = '' | |
if os.name == 'nt': | |
command = "msbuild /m -p:Configuration=Release INSTALL.vcxproj" | |
else: | |
command = "cmake --install ." | |
directory = Path('./%s/build' % product) | |
p = subprocess.run(shlex.split(command), cwd=directory) | |
if p.returncode != 0: | |
exit() | |
def setup_build_install(product): | |
setup(product) | |
if os.name != 'nt': | |
build(product) | |
install(product) | |
if __name__ == '__main__': | |
import lzma | |
import tarfile | |
import requests | |
import subprocess | |
from pathlib import Path | |
import os | |
import shlex | |
if not check_for_dependencies(): | |
exit() | |
urls = [ | |
"https://github.com/llvm/llvm-project/releases/download/llvmorg-12.0.0/llvm-12.0.0.src.tar.xz", | |
"https://github.com/llvm/llvm-project/releases/download/llvmorg-12.0.0/clang-12.0.0.src.tar.xz", | |
"https://github.com/llvm/llvm-project/releases/download/llvmorg-12.0.0/lld-12.0.0.src.tar.xz", | |
] | |
products = [] | |
for url in urls: | |
#Download all first, the the rest can be done offline. | |
product = maybe_download_and_extract(url) | |
products += [product] | |
for product in products: | |
setup_build_install(product) | |
r = subprocess.run(shlex.split("git clone --depth=1 https://github.com/ziglang/zig.git")) | |
os.makedirs(Path("./zig/build")) | |
setup("zig") | |
if os.name != 'nt': | |
build("zig") | |
install("zig") | |
print("Done!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment