diff --git a/.bazelrc b/.bazelrc index 046daf91..fae06bb8 100644 --- a/.bazelrc +++ b/.bazelrc @@ -1,3 +1,6 @@ +# Try loading per-user configuration. +try-import %workspace%/user.bazelrc + # On Windows clang.exe tries to find the MSVC toolchain by looking at environmental variables, # querying Visual Studio instances via COM (>=2017) and reading Registry keys (<=2015). # ProgramData data is typically required for COM api querying, unless a custom location for diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 78bae3d6..63a92c3a 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -14,19 +14,19 @@ jobs: tag: [10, 11] env: LLVM_INSTALL_PATH: /usr/lib/llvm-${{ matrix.tag }} - + steps: - uses: actions/checkout@v2 - + - name: Setup LLVM-${{ matrix.tag }} run: | wget https://apt.llvm.org/llvm.sh chmod +x ./llvm.sh sudo ./llvm.sh ${{ matrix.tag }} sudo apt install -y llvm-${{ matrix.tag }}-dev libclang-${{ matrix.tag }}-dev liblldb-${{ matrix.tag }}-dev - + - name: Build run: bazel build :all - + - name: Test run: bazel test --test_output=errors :all diff --git a/.gitignore b/.gitignore index 03857970..9756bbc5 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ # Bazel directories/symlinks bazel-* +user.bazelrc # YouCompleteMe .ycm_extra_conf.py diff --git a/BUILD b/BUILD index 4fda45ce..fb8472f9 100644 --- a/BUILD +++ b/BUILD @@ -110,14 +110,8 @@ cc_library( data = [ "//testdata:test_binary_gen", "//testdata:test_binary_srcs", - ] + select({ - "@bazel_tools//src/conditions:windows": [ - # There is no lldb-server on Windows. - ], - "//conditions:default": [ - "@llvm_project_local//:lldb-server", - ], - }), + "@llvm_project_local//:lldb-server", + ], deps = [ "@bazel_tools//tools/cpp/runfiles", "@llvm_project_local//:lldb-api", diff --git a/README.md b/README.md index 2eac1ff2..b73a16b5 100644 --- a/README.md +++ b/README.md @@ -91,6 +91,15 @@ bazel test :all bazel run :main -- "(1 + 2) * 42 / 4" ``` +Depending on your distribution of LLVM, you may need to provide +`--@llvm_project_local//:llvm_build={static,dynamic}` flag. For example, if your +`liblldb.so` is linked dynamically (this is the case when installing via `apt`), +then you need to use `llvm_build=dynamic`. The build script [tries to choose the +correct default value automatically](/build_defs/repo_rules.bzl#L21), but it can +be wrong in some situations (please, report and contribute 🙂). + +> **Hint:** You can add this option to your `user.bazelrc`. + ## Disclamer This is not an officially supported Google product. diff --git a/WORKSPACE b/WORKSPACE index c28211d0..2981393a 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -2,11 +2,20 @@ workspace(name = "lldb_eval") load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") +http_archive( + name = "bazel_skylib", + sha256 = "1c531376ac7e5a180e0237938a2536de0c54d93f5c278634818e0efc952dd56c", + urls = [ + "https://github.com/bazelbuild/bazel-skylib/releases/download/1.0.3/bazel-skylib-1.0.3.tar.gz", + "https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.0.3/bazel-skylib-1.0.3.tar.gz", + ], +) + http_archive( name = "com_google_googletest", - sha256 = "ff7a82736e158c077e76188232eac77913a15dac0b22508c390ab3f88e6d6d86", - strip_prefix = "googletest-b6cd405286ed8635ece71c72f118e659f4ade3fb", - urls = ["https://github.com/google/googletest/archive/b6cd405286ed8635ece71c72f118e659f4ade3fb.zip"], # 2019-01-07 + sha256 = "94c634d499558a76fa649edb13721dce6e98fb1e7018dfaeba3cd7a083945e91", + strip_prefix = "googletest-release-1.10.0", + urls = ["https://github.com/google/googletest/archive/release-1.10.0.zip"], ) load("//build_defs:repo_rules.bzl", "llvm_project_configure") diff --git a/build_defs/llvm_project_local.BUILD b/build_defs/llvm_project_local.BUILD index 07af7912..50ab02fd 100644 --- a/build_defs/llvm_project_local.BUILD +++ b/build_defs/llvm_project_local.BUILD @@ -1,7 +1,44 @@ +load("@bazel_skylib//lib:selects.bzl", "selects") +load("@bazel_skylib//rules:common_settings.bzl", "string_flag") load("@rules_cc//cc:defs.bzl", "cc_library") package(default_visibility = ["//visibility:public"]) +string_flag( + name = "llvm_build", + build_setting_default = "{LLVM_BUILD_DEFAULT}", + values = [ + "static", + "dynamic", + ], +) + +config_setting( + name = "llvm_build_static", + flag_values = {":llvm_build": "static"}, +) + +config_setting( + name = "llvm_build_dynamic", + flag_values = {":llvm_build": "dynamic"}, +) + +selects.config_setting_group( + name = "linux_dynamic", + match_all = [ + "@bazel_tools//src/conditions:linux_x86_64", + ":llvm_build_dynamic", + ], +) + +selects.config_setting_group( + name = "linux_static", + match_all = [ + "@bazel_tools//src/conditions:linux_x86_64", + ":llvm_build_static", + ], +) + LLVM_LINKOPTS = select({ "@bazel_tools//src/conditions:windows": [], "//conditions:default": [ @@ -68,6 +105,17 @@ filegroup( ]), ) +filegroup( + name = "libllvm-so", + srcs = select({ + "@bazel_tools//src/conditions:windows": [ + ], + "//conditions:default": glob([ + "lib/libLLVM*.so*", + ]), + }), +) + filegroup( name = "clang-headers", srcs = glob([ @@ -80,6 +128,17 @@ filegroup( ]), ) +filegroup( + name = "libclang-cpp-so", + srcs = select({ + "@bazel_tools//src/conditions:windows": [ + ], + "//conditions:default": glob([ + "lib/libclang-cpp*.so*", + ]), + }), +) + cc_library( name = "llvm-support", srcs = select({ @@ -87,7 +146,10 @@ cc_library( "lib/LLVMSupport.lib", "lib/LLVMDemangle.lib", ], - "//conditions:default": [ + ":linux_dynamic": [ + ":libllvm-so", + ], + ":linux_static": [ "lib/libLLVMSupport.a", "lib/libLLVMDemangle.a", ], @@ -95,6 +157,7 @@ cc_library( hdrs = [":llvm-headers"], includes = ["include"], linkopts = LLVM_LINKOPTS, + linkstatic = 1, ) cc_library( @@ -107,8 +170,10 @@ cc_library( "lib/LLVMBitstreamReader.lib", "lib/LLVMBinaryFormat.lib", ], - "//conditions:default": [ - # $(llvm-config --libs mc core support) + ":linux_dynamic": [ + ":libllvm-so", + ], + ":linux_static": [ "lib/libLLVMCore.a", "lib/libLLVMRemarks.a", "lib/libLLVMBitstreamReader.a", @@ -117,6 +182,7 @@ cc_library( }), hdrs = [":llvm-headers"], includes = ["include"], + linkstatic = 1, deps = [":llvm-support"], ) @@ -128,7 +194,10 @@ cc_library( "lib/LLVMDebugInfoCodeView.lib", "lib/LLVMDebugInfoMSF.lib", ], - "//conditions:default": [ + ":linux_dynamic": [ + ":libllvm-so", + ], + ":linux_static": [ "lib/libLLVMMC.a", "lib/libLLVMDebugInfoCodeView.a", "lib/libLLVMDebugInfoMSF.a", @@ -136,25 +205,16 @@ cc_library( }), hdrs = [":llvm-headers"], includes = ["include"], + linkstatic = 1, deps = [":llvm-core"], ) cc_library( name = "llvm-shared", - srcs = select({ - "@bazel_tools//src/conditions:windows": [ - ], - "//conditions:default": glob([ - "lib/libLLVM*.so", - ]), - }), + srcs = [":libllvm-so"], hdrs = [":llvm-headers"], includes = ["include"], - deps = [ - ":llvm-core", - ":llvm-mc", - ":llvm-support", - ], + linkstatic = 1, ) cc_library( @@ -163,12 +223,16 @@ cc_library( "@bazel_tools//src/conditions:windows": [ "lib/clangBasic.lib", ], - "//conditions:default": [ + ":linux_dynamic": [ + ":libclang-cpp-so", + ], + ":linux_static": [ "lib/libclangBasic.a", ], }), hdrs = [":clang-headers"], includes = ["include"], + linkstatic = 1, deps = [ ":llvm-core", ":llvm-mc", @@ -182,18 +246,30 @@ cc_library( "@bazel_tools//src/conditions:windows": [ "lib/clangLex.lib", ], - "//conditions:default": [ + ":linux_dynamic": glob([ + "lib/libclang-cpp*.so*", + ]), + ":linux_static": [ "lib/libclangLex.a", ], }), hdrs = [":clang-headers"], includes = ["include"], + linkstatic = 1, deps = [ ":clang-basic", ":llvm-support", ], ) +cc_library( + name = "clangcpp-shared", + srcs = [":libclang-cpp-so"], + hdrs = [":clang-headers"], + includes = ["include"], + linkstatic = 1, +) + cc_library( name = "lldb-api", srcs = select({ @@ -211,8 +287,13 @@ cc_library( "include/lldb/**/*.inc", ]), includes = ["include"], - deps = [ - ":llvm-shared", # liblldb can be dynamically linked and depend on libLLVM. - ":llvm-support", - ], + deps = select({ + ":llvm_build_dynamic": [ + ":llvm-shared", + ":clangcpp-shared", + ], + ":llvm_build_static": [ + # Don't have any dependencies if linked statically. + ], + }), ) diff --git a/build_defs/repo_rules.bzl b/build_defs/repo_rules.bzl index 3365558f..4cb6c405 100644 --- a/build_defs/repo_rules.bzl +++ b/build_defs/repo_rules.bzl @@ -16,6 +16,25 @@ This module configures a local repository for an llvm-project. """ +load("@bazel_skylib//lib:paths.bzl", "paths") + +def _get_llvm_build_type(ctx, llvm_path): + if ctx.os.name.startswith("windows"): + # There are no prebuilt binaries for Windows, so always assume static. + return "static" + + ldd = ctx.which("ldd") + if ldd == None: + # No `ldd`, can't do without it. + return "static" + + # Execute `ldd liblldb.so` to figure out whether it depends on libLLVM.so. + result = ctx.execute([ldd, paths.join(llvm_path, "lib", "liblldb.so")]) + if result.stdout.find("libLLVM") != -1: + return "dynamic" + + return "static" + def _impl(repository_ctx): llvm_path = repository_ctx.os.environ.get("LLVM_INSTALL_PATH") if llvm_path == None: @@ -24,15 +43,21 @@ def _impl(repository_ctx): "with LLVM {bin,lib,include} directories. E.g. \"/usr/lib/llvm\".", ) - repository_ctx.symlink(llvm_path + "/bin", "bin") - repository_ctx.symlink(llvm_path + "/lib", "lib") - repository_ctx.symlink(llvm_path + "/include", "include") + # Try to guess a default value for :llvm_build flag. It should be "dynamic" + # if liblldb.so is built dynamically and "static" otherwise. + llvm_build = _get_llvm_build_type(repository_ctx, llvm_path) + + repository_ctx.symlink(paths.join(llvm_path, "bin"), "bin") + repository_ctx.symlink(paths.join(llvm_path, "lib"), "lib") + repository_ctx.symlink(paths.join(llvm_path, "include"), "include") repository_ctx.template( "BUILD", Label("//build_defs:llvm_project_local.BUILD"), + substitutions = {"{LLVM_BUILD_DEFAULT}": llvm_build}, ) llvm_project_configure = repository_rule( implementation = _impl, environ = ["LLVM_INSTALL_PATH"], + local = True, ) diff --git a/tools/BUILD b/tools/BUILD index 09ad2095..b68bedc0 100644 --- a/tools/BUILD +++ b/tools/BUILD @@ -46,17 +46,11 @@ cc_binary( data = [ "//testdata:fuzzer_binary_gen", "//testdata:fuzzer_binary_srcs", - ] + select({ - "@bazel_tools//src/conditions:windows": [ - # There is no lldb-server on Windows. - ], - "//conditions:default": [ - "@llvm_project_local//:lldb-server", - ], - }), + "@llvm_project_local//:lldb-server", + ], deps = [ + ":fuzzer_lib", "//:lldb-eval", - "//tools:fuzzer_lib", "@bazel_tools//tools/cpp/runfiles", "@llvm_project_local//:lldb-api", ],