build_llvm.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #!/usr/bin/env python3
  2. #
  3. # Copyright (C) 2019 Intel Corporation. All rights reserved.
  4. # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  5. #
  6. import argparse
  7. import os
  8. import pathlib
  9. import shlex
  10. import shutil
  11. import subprocess
  12. import sys
  13. def clone_llvm(dst_dir, llvm_repo, llvm_branch):
  14. """
  15. any error will raise CallProcessError
  16. """
  17. llvm_dir = dst_dir.joinpath("llvm").resolve()
  18. if not llvm_dir.exists():
  19. print(f"Clone llvm to {llvm_dir} ...")
  20. GIT_CLONE_CMD = f"git clone --depth 1 --branch {llvm_branch} {llvm_repo} llvm"
  21. subprocess.check_output(shlex.split(GIT_CLONE_CMD), cwd=dst_dir)
  22. else:
  23. print(f"There is an LLVM local repo in {llvm_dir}, keep using it")
  24. return llvm_dir
  25. def build_llvm(llvm_dir, platform, backends):
  26. LLVM_COMPILE_OPTIONS = [
  27. '-DCMAKE_BUILD_TYPE:STRING="Release"',
  28. "-DCMAKE_EXPORT_COMPILE_COMMANDS=ON",
  29. "-DLLVM_APPEND_VC_REV:BOOL=ON",
  30. "-DLLVM_BUILD_BENCHMARKS:BOOL=OFF",
  31. "-DLLVM_BUILD_DOCS:BOOL=OFF",
  32. "-DLLVM_BUILD_EXAMPLES:BOOL=OFF",
  33. "-DLLVM_BUILD_LLVM_DYLIB:BOOL=OFF",
  34. "-DLLVM_BUILD_TESTS:BOOL=OFF",
  35. "-DLLVM_CCACHE_BUILD:BOOL=OFF",
  36. "-DLLVM_ENABLE_BINDINGS:BOOL=OFF",
  37. "-DLLVM_ENABLE_IDE:BOOL=OFF",
  38. "-DLLVM_ENABLE_LIBXML2:BOOL=OFF",
  39. "-DLLVM_ENABLE_TERMINFO:BOOL=OFF",
  40. "-DLLVM_ENABLE_ZLIB:BOOL=OFF",
  41. "-DLLVM_INCLUDE_BENCHMARKS:BOOL=OFF",
  42. "-DLLVM_INCLUDE_DOCS:BOOL=OFF",
  43. "-DLLVM_INCLUDE_EXAMPLES:BOOL=OFF",
  44. "-DLLVM_INCLUDE_UTILS:BOOL=OFF",
  45. "-DLLVM_INCLUDE_TESTS:BOOL=OFF",
  46. "-DLLVM_INCLUDE_TOOLS:BOOL=OFF",
  47. "-DLLVM_OPTIMIZED_TABLEGEN:BOOL=ON",
  48. ]
  49. LLVM_EXTRA_COMPILER_OPTIONS = {
  50. "arc": [
  51. '-DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD:STRING="ARC"',
  52. "-DLLVM_ENABLE_LIBICUUC:BOOL=OFF",
  53. "-DLLVM_ENABLE_LIBICUDATA:BOOL=OFF",
  54. ],
  55. "xtensa": [
  56. '-DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD:STRING="Xtensa"',
  57. ],
  58. "windows": [
  59. "-DCMAKE_INSTALL_PREFIX=LLVM-install",
  60. ],
  61. "default": [],
  62. }
  63. LLVM_TARGETS_TO_BUILD = [
  64. "-DLLVM_TARGETS_TO_BUILD:STRING=" + ";".join(backends)
  65. if backends
  66. else '-DLLVM_TARGETS_TO_BUILD:STRING="AArch64;ARM;Mips;RISCV;X86"'
  67. ]
  68. if not llvm_dir.exists():
  69. raise Exception(f"{llvm_dir} doesn't exist")
  70. build_dir = llvm_dir.joinpath(
  71. "win32build" if "windows" == platform else "build"
  72. ).resolve()
  73. build_dir.mkdir(exist_ok=True)
  74. lib_llvm_core_library = build_dir.joinpath("lib/libLLVMCore.a").resolve()
  75. if lib_llvm_core_library.exists():
  76. print(f"Please remove {build_dir} manually and try again")
  77. return
  78. compile_options = " ".join(
  79. LLVM_COMPILE_OPTIONS
  80. + LLVM_EXTRA_COMPILER_OPTIONS.get(
  81. platform, LLVM_EXTRA_COMPILER_OPTIONS["default"]
  82. )
  83. + LLVM_TARGETS_TO_BUILD
  84. )
  85. CONFIG_CMD = f"cmake {compile_options} ../llvm "
  86. subprocess.check_call(shlex.split(CONFIG_CMD), cwd=build_dir)
  87. BUILD_CMD = f"cmake --build . --parallel {os.cpu_count()}" + (
  88. " --config Release" if "windows" == platform else ""
  89. )
  90. subprocess.check_call(shlex.split(BUILD_CMD), cwd=build_dir)
  91. return build_dir
  92. def main():
  93. parser = argparse.ArgumentParser(description="build necessary LLVM libraries")
  94. parser.add_argument(
  95. "--platform",
  96. type=str,
  97. choices=["android", "arc", "darwin", "linux", "windows", "xtensa"],
  98. help="identify current platform",
  99. )
  100. parser.add_argument(
  101. "--arch",
  102. nargs="+",
  103. type=str,
  104. choices=["AArch64", "ARC", "ARM", "Mips", "RISCV", "X86", "Xtensa"],
  105. help="identify LLVM supported backends, separate by space, like '--arch ARM Mips X86'",
  106. )
  107. options = parser.parse_args()
  108. # if the "platform" is not identified in the command line option,
  109. # detect it
  110. if not options.platform:
  111. if sys.platform.startswith("win32") or sys.platform.startswith("msys"):
  112. platform = "windows"
  113. elif sys.platform.startswith("darwin"):
  114. platform = "darwin"
  115. else:
  116. platform = "linux"
  117. else:
  118. platform = options.platform
  119. print(f"========== Build LLVM for {platform} ==========\n")
  120. llvm_repo_and_branch = {
  121. "arc": {
  122. "repo": "https://github.com/llvm/llvm-project.git",
  123. "branch": "release/13.x"
  124. },
  125. "xtensa": {
  126. "repo": "https://github.com/espressif/llvm-project.git",
  127. "branch": "xtensa_release_11.0.0",
  128. },
  129. "default": {
  130. "repo": "https://github.com/llvm/llvm-project.git",
  131. "branch": "release/13.x",
  132. },
  133. }
  134. # retrieve the real file
  135. current_file = pathlib.Path(__file__)
  136. if current_file.is_symlink():
  137. current_file = pathlib.Path(os.readlink(current_file))
  138. current_dir = current_file.parent.resolve()
  139. deps_dir = current_dir.joinpath("../core/deps").resolve()
  140. print(f"==================== CLONE LLVM ====================")
  141. llvm_info = llvm_repo_and_branch.get(platform, llvm_repo_and_branch["default"])
  142. llvm_dir = clone_llvm(deps_dir, llvm_info["repo"], llvm_info["branch"])
  143. print()
  144. print(f"==================== BUILD LLVM ====================")
  145. build_llvm(llvm_dir, platform, options.arch)
  146. print()
  147. if __name__ == "__main__":
  148. main()