diff options
author | Ilya Biryukov <ibiryukov@google.com> | 2018-03-22 05:33:42 -0700 |
---|---|---|
committer | TensorFlower Gardener <gardener@tensorflow.org> | 2018-03-22 05:36:18 -0700 |
commit | 9e651e4571f7b7c2d32bdafe43cc4ced9bb0c750 (patch) | |
tree | 969e4652bf4774e474ac8449df53a14761f156ab /third_party/clang_toolchain | |
parent | f9ccb89134d89469ae962bba832e78d1f116b96b (diff) |
Allow to download clang and use clang for CPU builds.
Previously we only allowed to download clang when doing GPU builds.
The added skylark files use bazel's autoconf scripts, which were only added in
0.10.0. To provide nice error message for older versions of bazel
(i.e. 'version is less than 0.10' vs 'can't load @bazel_tools/cpp/...'), we
move the bazel version check into WORKSPACE file from workspace.bzl.
PiperOrigin-RevId: 190050798
Diffstat (limited to 'third_party/clang_toolchain')
-rw-r--r-- | third_party/clang_toolchain/BUILD | 0 | ||||
-rw-r--r-- | third_party/clang_toolchain/cc_configure_clang.bzl | 27 | ||||
-rw-r--r-- | third_party/clang_toolchain/download_clang.bzl | 54 |
3 files changed, 81 insertions, 0 deletions
diff --git a/third_party/clang_toolchain/BUILD b/third_party/clang_toolchain/BUILD new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/third_party/clang_toolchain/BUILD diff --git a/third_party/clang_toolchain/cc_configure_clang.bzl b/third_party/clang_toolchain/cc_configure_clang.bzl new file mode 100644 index 0000000000..1181110ea9 --- /dev/null +++ b/third_party/clang_toolchain/cc_configure_clang.bzl @@ -0,0 +1,27 @@ +""" Downloads clang and configures the crosstool using bazel's autoconf.""" + +load("@bazel_tools//tools/cpp:cc_configure.bzl", "cc_autoconf_impl") +load(":download_clang.bzl", "download_clang") + +_TF_DOWNLOAD_CLANG = "TF_DOWNLOAD_CLANG" +_TF_NEED_CUDA = "TF_NEED_CUDA" + +def _cc_clang_autoconf(repo_ctx): + if repo_ctx.os.environ.get(_TF_DOWNLOAD_CLANG) != "1": + return + if repo_ctx.os.environ.get(_TF_NEED_CUDA) == "1": + # Clang is handled separately for CUDA configs. + # See cuda_configure.bzl for more details. + return + + download_clang(repo_ctx, out_folder='extra_tools') + overriden_tools = {'gcc': 'extra_tools/bin/clang'} + cc_autoconf_impl(repo_ctx, overriden_tools) + +cc_download_clang_toolchain = repository_rule( + environ = [ + _TF_DOWNLOAD_CLANG, + _TF_NEED_CUDA, + ], + implementation = _cc_clang_autoconf, +) diff --git a/third_party/clang_toolchain/download_clang.bzl b/third_party/clang_toolchain/download_clang.bzl new file mode 100644 index 0000000000..54d383d7d7 --- /dev/null +++ b/third_party/clang_toolchain/download_clang.bzl @@ -0,0 +1,54 @@ +""" Helpers to download a recent clang release.""" + +def _get_platform_folder(os_name): + os_name = os_name.lower() + if os_name.startswith('windows'): + return 'Win' + if os_name.startswith('mac os'): + return 'Mac' + if not os_name.startswith('linux'): + fail('Unknown platform') + return 'Linux_x64' + +def _download_chromium_clang(repo_ctx, platform_folder, package_version, sha256, + out_folder): + cds_url = 'https://commondatastorage.googleapis.com/chromium-browser-clang' + cds_file = 'clang-%s.tgz' % package_version + cds_full_url = '{0}/{1}/{2}'.format(cds_url, platform_folder, cds_file) + repo_ctx.download_and_extract(cds_full_url, output=out_folder, sha256=sha256) + +def download_clang(repo_ctx, out_folder): + """ Download a fresh clang release and put it into out_folder. + + Clang itself will be located in 'out_folder/bin/clang'. + We currently download one of the latest releases of clang by the + Chromium project (see + https://chromium.googlesource.com/chromium/src/+/master/docs/clang.md). + + Args: + repo_ctx: An instance of repository_context object. + out_folder: A folder to extract the compiler into. + """ + # TODO(ibiryukov): we currently download and extract some extra tools in the + # clang release (e.g., sanitizers). We should probably remove the ones + # we don't need and document the ones we want provide in addition to clang. + + # Latest CLANG_REVISION and CLANG_SUB_REVISION of the Chromiums's release + # can be found in https://chromium.googlesource.com/chromium/src/tools/clang/+/master/scripts/update.py + CLANG_REVISION = '321529' + CLANG_SUB_REVISION = 2 + + package_version = '%s-%s' % (CLANG_REVISION, CLANG_SUB_REVISION) + + checksums = { + 'Linux_x64': + '76d4eb1ad011e3127c4a9de9b9f5d4ac624b5a9395c4d7395c9e0a487b13daf6', + 'Mac': + '4b2a7a65ac1ee892b318c723eec8771f514bb306f346aa8216bb0006f19d87b7', + 'Win': + 'eba51bb8f84af41a85903113666bd21c22709010c39c4cb19dc20cf1ed14581b', + } + + platform_folder = _get_platform_folder(repo_ctx.os.name) + _download_chromium_clang(repo_ctx, platform_folder, package_version, + checksums[platform_folder], out_folder) |