From 1d4c2435225579b48e0ea4dc8aea4bf044868a62 Mon Sep 17 00:00:00 2001
From: Rokas Kupstys <rokups@zoho.com>
Date: Thu, 30 Nov 2017 18:51:36 +0200
Subject: [PATCH] Cloning llvm from git replaced with downloading archives of
 exacty commits from github. This is much faster.

---
 build/scripts/LLVM.lua | 46 +++++++++++++++++++++++++++---------------
 1 file changed, 30 insertions(+), 16 deletions(-)

diff --git a/build/scripts/LLVM.lua b/build/scripts/LLVM.lua
index e567cfb6..eee457ba 100644
--- a/build/scripts/LLVM.lua
+++ b/build/scripts/LLVM.lua
@@ -25,29 +25,38 @@ function clone_llvm()
   local clang_release = get_clang_rev()
   print("Clang release: " .. clang_release)
 
-  if os.isdir(llvm) and not os.isdir(llvm .. "/.git") then
-    error("LLVM directory is not a git repository.")
+  if os.ishost("windows") then
+    extract = extract_7z
+  else
+    extract = extract_tar_gz
   end
 
-  local quotedLLVM = '"' .. llvm .. '"'
-  if not os.isdir(llvm) then
-    git.clone(quotedLLVM, "https://github.com/llvm-mirror/llvm.git")
+  local archive = 'llvm-'..llvm_release..'.tar.gz'
+  if os.isfile(archive) then
+    print('Archive '..archive..' already exists.')
   else
-    git.reset_hard(quotedLLVM, "HEAD")
-    git.pull_rebase(quotedLLVM)
+    download('https://github.com/llvm-mirror/llvm/archive/'..llvm_release..'.tar.gz', archive)
   end
 
-  local clang = llvm .. "/tools/clang"
-  local quotedClang = '"' .. clang .. '"'
-  if not os.isdir(clang) then
-    git.clone(quotedClang, "https://github.com/llvm-mirror/clang.git")
-  else
-    git.reset_hard(quotedClang, "HEAD")
-    git.pull_rebase(quotedClang)
+  if os.isdir(llvm) then
+    os.rmdir(llvm)
+    if os.isdir(llvm) then
+      print('Removing '..llvm..' directory failed. Please remove it manually and restart.')
+      return
+    end
   end
 
-  git.reset_hard(quotedLLVM, llvm_release)
-  git.reset_hard(quotedClang, clang_release)
+  extract(archive, '.')
+  os.rename('llvm-'..llvm_release, llvm)
+
+  archive = 'clang-'..clang_release..'.tar.gz'
+  if os.isfile(archive) then
+    print('Archive '..archive..' already exists.')
+  else
+    download('https://github.com/llvm-mirror/clang/archive/'..clang_release..'.tar.gz', archive)
+  end
+  extract(archive, '.')
+  os.rename('clang-'..clang_release, llvm..'/tools/clang')
 end
 function get_vs_version()
   local function map_msvc_to_vs_version(major, minor)
@@ -129,6 +138,11 @@ function extract_tar_xz(archive, dest_dir)
 	return execute_or_die(string.format("tar xJf %s -C %s", archive, dest_dir), true)
 end
 
+function extract_tar_gz(archive, dest_dir)
+	execute("mkdir -p " .. dest_dir, true)
+	return execute_or_die(string.format("tar xf %s -C %s", archive, dest_dir), true)
+end
+
 local use_7zip = os.ishost("windows")
 local archive_ext = use_7zip and ".7z" or ".tar.xz"