build_llvm.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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}, clean and keep using it")
  24. return llvm_dir
  25. def build_llvm(llvm_dir, platform, backends, projects):
  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_TERMINFO:BOOL=OFF",
  39. "-DLLVM_ENABLE_ZLIB:BOOL=OFF",
  40. "-DLLVM_INCLUDE_BENCHMARKS:BOOL=OFF",
  41. "-DLLVM_INCLUDE_DOCS:BOOL=OFF",
  42. "-DLLVM_INCLUDE_EXAMPLES:BOOL=OFF",
  43. "-DLLVM_INCLUDE_UTILS:BOOL=OFF",
  44. "-DLLVM_INCLUDE_TESTS:BOOL=OFF",
  45. "-DLLVM_BUILD_TESTS:BOOL=OFF",
  46. "-DLLVM_OPTIMIZED_TABLEGEN:BOOL=ON",
  47. ]
  48. LLVM_EXTRA_COMPILE_OPTIONS = {
  49. "arc": [
  50. '-DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD:STRING="ARC"',
  51. "-DLLVM_ENABLE_LIBICUUC:BOOL=OFF",
  52. "-DLLVM_ENABLE_LIBICUDATA:BOOL=OFF",
  53. ],
  54. "xtensa": [
  55. '-DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD:STRING="Xtensa"',
  56. ],
  57. "windows": [
  58. "-DCMAKE_INSTALL_PREFIX=LLVM-install",
  59. ],
  60. "default": [],
  61. }
  62. LLVM_TARGETS_TO_BUILD = [
  63. '-DLLVM_TARGETS_TO_BUILD:STRING="' + ";".join(backends) + '"'
  64. if backends
  65. else '-DLLVM_TARGETS_TO_BUILD:STRING="AArch64;ARM;Mips;RISCV;X86"'
  66. ]
  67. LLVM_PROJECTS_TO_BUILD = [
  68. '-DLLVM_ENABLE_PROJECTS:STRING="' + ";".join(projects) + '"' if projects else ""
  69. ]
  70. # lldb project requires libxml2
  71. LLVM_LIBXML2_OPTION = [
  72. "-DLLVM_ENABLE_LIBXML2:BOOL=" + ("ON" if "lldb" in projects else "OFF")
  73. ]
  74. # enabling LLVM_INCLUDE_TOOLS will increase ~300M to the final package
  75. LLVM_INCLUDE_TOOLS_OPTION = [
  76. "-DLLVM_INCLUDE_TOOLS:BOOL=ON" if projects else "-DLLVM_INCLUDE_TOOLS:BOOL=OFF"
  77. ]
  78. if not llvm_dir.exists():
  79. raise Exception(f"{llvm_dir} doesn't exist")
  80. build_dir = llvm_dir.joinpath(
  81. "win32build" if "windows" == platform else "build"
  82. ).resolve()
  83. build_dir.mkdir(exist_ok=True)
  84. lib_llvm_core_library = build_dir.joinpath("lib/libLLVMCore.a").resolve()
  85. if lib_llvm_core_library.exists():
  86. print(f"Please remove {build_dir} manually and try again")
  87. return
  88. compile_options = " ".join(
  89. LLVM_COMPILE_OPTIONS
  90. + LLVM_LIBXML2_OPTION
  91. + LLVM_EXTRA_COMPILE_OPTIONS.get(
  92. platform, LLVM_EXTRA_COMPILE_OPTIONS["default"]
  93. )
  94. + LLVM_TARGETS_TO_BUILD
  95. + LLVM_PROJECTS_TO_BUILD
  96. + LLVM_INCLUDE_TOOLS_OPTION
  97. )
  98. CONFIG_CMD = f"cmake {compile_options} ../llvm " + (
  99. "-A x64" if "windows" == platform else ""
  100. )
  101. print(f"{CONFIG_CMD}")
  102. subprocess.check_call(shlex.split(CONFIG_CMD), cwd=build_dir)
  103. BUILD_CMD = f"cmake --build . --target package --parallel {os.cpu_count()}" + (
  104. " --config Release" if "windows" == platform else ""
  105. )
  106. subprocess.check_call(shlex.split(BUILD_CMD), cwd=build_dir)
  107. return build_dir
  108. def main():
  109. parser = argparse.ArgumentParser(description="build necessary LLVM libraries")
  110. parser.add_argument(
  111. "--platform",
  112. type=str,
  113. choices=["android", "arc", "darwin", "linux", "windows", "xtensa"],
  114. help="identify current platform",
  115. )
  116. parser.add_argument(
  117. "--arch",
  118. nargs="+",
  119. type=str,
  120. choices=[
  121. "AArch64",
  122. "ARC",
  123. "ARM",
  124. "Mips",
  125. "RISCV",
  126. "WebAssembly",
  127. "X86",
  128. "Xtensa",
  129. ],
  130. help="identify LLVM supported backends, separate by space, like '--arch ARM Mips X86'",
  131. )
  132. parser.add_argument(
  133. "--project",
  134. nargs="+",
  135. type=str,
  136. default="",
  137. choices=["clang", "lldb"],
  138. help="identify extra LLVM projects, separate by space, like '--project clang lldb'",
  139. )
  140. options = parser.parse_args()
  141. print(f"options={options}")
  142. # if the "platform" is not identified in the command line option,
  143. # detect it
  144. if not options.platform:
  145. if sys.platform.startswith("win32") or sys.platform.startswith("msys"):
  146. platform = "windows"
  147. elif sys.platform.startswith("darwin"):
  148. platform = "darwin"
  149. else:
  150. platform = "linux"
  151. else:
  152. platform = options.platform
  153. print(f"========== Build LLVM for {platform} ==========\n")
  154. llvm_repo_and_branch = {
  155. "arc": {
  156. "repo": "https://github.com/llvm/llvm-project.git",
  157. "branch": "release/13.x",
  158. },
  159. "xtensa": {
  160. "repo": "https://github.com/espressif/llvm-project.git",
  161. "branch": "xtensa_release_11.0.0",
  162. },
  163. "default": {
  164. "repo": "https://github.com/llvm/llvm-project.git",
  165. "branch": "release/13.x",
  166. },
  167. }
  168. # retrieve the real file
  169. current_file = pathlib.Path(__file__)
  170. if current_file.is_symlink():
  171. current_file = pathlib.Path(os.readlink(current_file))
  172. current_dir = current_file.parent.resolve()
  173. deps_dir = current_dir.joinpath("../core/deps").resolve()
  174. print(f"==================== CLONE LLVM ====================")
  175. llvm_info = llvm_repo_and_branch.get(platform, llvm_repo_and_branch["default"])
  176. llvm_dir = clone_llvm(deps_dir, llvm_info["repo"], llvm_info["branch"])
  177. print()
  178. print(f"==================== BUILD LLVM ====================")
  179. build_llvm(llvm_dir, platform, options.arch, options.project)
  180. print()
  181. if __name__ == "__main__":
  182. main()