diff --git a/llvm-build/build.py b/llvm-build/build.py index 4459e122e246513bee70cb7f934ae74843ba1700..c99813afd4802bfe66eb70ec584deb2738b5e45a 100755 --- a/llvm-build/build.py +++ b/llvm-build/build.py @@ -29,6 +29,7 @@ import stat import json import sys +from shutil import which from python_builder import MinGWPythonBuilder from prebuilts_clang_version import prebuilts_clang_version from get_ohos_flags import get_ohos_cflags, get_ohos_ldflags @@ -43,6 +44,7 @@ class BuildConfig(): assert not(args.no_build and args.build_only), "Error! --no-build and --build-only flags aren't compatible." + self.enable_ccache = args.enable_ccache self.no_strip_libs = args.no_strip_libs self.do_build = not args.skip_build self.do_package = not args.skip_package and not args.build_only @@ -141,6 +143,12 @@ class BuildConfig(): default=False, help='Strip others but not strip libs(use with --strip)') + parser.add_argument( + '--enable-ccache', + action='store_true', + default=False, + help='Enable ccache while build llvm toolchain') + parser.add_argument( '--enable-assertions', action='store_true', @@ -915,6 +923,12 @@ class LlvmCore(BuildUtils): llvm_defines['LLVM_ENABLE_PROJECTS'] = ';'.join(self.build_config.host_projects) llvm_defines['LLVM_ENABLE_RUNTIMES'] = ';'.join(self.build_config.host_runtimes) llvm_defines['LLVM_ENABLE_BINDINGS'] = 'OFF' + + if self.build_config.enable_ccache: + llvm_defines['CMAKE_C_COMPILER_LAUNCHER'] = which('ccache') + llvm_defines['CMAKE_CXX_COMPILER_LAUNCHER'] = which('ccache') + llvm_defines['CMAKE_LINKER_LAUNCHER'] = which('ccache') + llvm_defines['CMAKE_C_COMPILER'] = os.path.join(llvm_root, 'bin', 'clang') llvm_defines['CMAKE_CXX_COMPILER'] = os.path.join(llvm_root, 'bin', 'clang++') llvm_defines['CMAKE_AR'] = os.path.join(llvm_root, 'bin', 'llvm-ar') @@ -1121,6 +1135,10 @@ class LlvmCore(BuildUtils): self.build_config.LLDB_PYTHON) windows_defines['SWIG_EXECUTABLE'] = self.find_program('swig') + if self.build_config.enable_ccache: + windows_defines['CMAKE_C_COMPILER_LAUNCHER'] = which('ccache') + windows_defines['CMAKE_CXX_COMPILER_LAUNCHER'] = which('ccache') + windows_defines['CMAKE_LINKER_LAUNCHER'] = which('ccache') windows_defines['CMAKE_C_COMPILER'] = cc windows_defines['CMAKE_CXX_COMPILER'] = cxx windows_defines['CMAKE_SYSTEM_NAME'] = 'Windows' @@ -1473,7 +1491,10 @@ class LlvmLibs(BuildUtils): extra_flags): sysroot = self.merge_out_path('sysroot') - + if self.build_config.enable_ccache: + defines['CMAKE_C_COMPILER_LAUNCHER'] = which('ccache') + defines['CMAKE_CXX_COMPILER_LAUNCHER'] = which('ccache') + defines['CMAKE_LINKER_LAUNCHER'] = which('ccache') defines['CMAKE_C_COMPILER'] = cc defines['CMAKE_CXX_COMPILER'] = cxx defines['CMAKE_AR'] = ar @@ -2001,6 +2022,10 @@ class LlvmLibs(BuildUtils): '-D_FILE_OFFSET_BITS=64', '-D_WIN32_WINNT=0x0600', '-DWINVER=0x0600', '-D__MSVCRT_VERSION__=0x1400'] cmake_defines = {} + if self.build_config.enable_ccache: + cmake_defines['CMAKE_C_COMPILER_LAUNCHER'] = which('ccache') + cmake_defines['CMAKE_CXX_COMPILER_LAUNCHER'] = which('ccache') + cmake_defines['CMAKE_LINKER_LAUNCHER'] = which('ccache') cmake_defines['CMAKE_C_COMPILER'] = os.path.join(toolchain_dir, 'bin', 'clang') cmake_defines['CMAKE_CXX_COMPILER'] = os.path.join(toolchain_dir, 'bin', 'clang++') cmake_defines['CMAKE_CROSSCOMPILING'] = 'True' @@ -2936,6 +2961,12 @@ class LlvmPackage(BuildUtils): self.package_up_resulting(package_name, host, install_host_dir) return +def enable_ccache(): + ccache_path = which('ccache') + os.environ['CCACHE_EXEC'] = ccache_path + os.environ['CCACHE_MAXSIZE'] = '100G' + os.environ['CCACHE_LOGFILE'] = os.path.join(os.environ.get('HOME'), '.ccache.log') + os.environ['CCACHE_DIR'] = os.path.join(os.environ.get('HOME'), '.ccache') def main(): build_config = BuildConfig() @@ -2946,6 +2977,8 @@ def main(): llvm_libs = LlvmLibs(build_config, sysroot_composer, llvm_package) args = build_config.parse_args() + if args.enable_ccache: + enable_ccache() need_host = build_utils.host_is_darwin() or ('linux' not in args.no_build) need_windows = build_utils.host_is_linux() and not build_utils.host_is_linux_aarch64() and \ not build_config.build_only and ('windows' not in args.no_build)