Skip to content

Commit 1ce173a

Browse files
authored
Setup build for static/dynamic LLVM distributions (#6)
* Setup build for static/dynamic LLVM distributions * Detect LLVM build type automatically Add a switch for using static or dynamic LLVM libraries. Depending on how liblldb.so is linked, we also need to use different setups.
1 parent 70b5d61 commit 1ce173a

File tree

9 files changed

+165
-49
lines changed

9 files changed

+165
-49
lines changed

.bazelrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# Try loading per-user configuration.
2+
try-import %workspace%/user.bazelrc
3+
14
# On Windows clang.exe tries to find the MSVC toolchain by looking at environmental variables,
25
# querying Visual Studio instances via COM (>=2017) and reading Registry keys (<=2015).
36
# ProgramData data is typically required for COM api querying, unless a custom location for

.github/workflows/main.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@ jobs:
1414
tag: [10, 11]
1515
env:
1616
LLVM_INSTALL_PATH: /usr/lib/llvm-${{ matrix.tag }}
17-
17+
1818
steps:
1919
- uses: actions/checkout@v2
20-
20+
2121
- name: Setup LLVM-${{ matrix.tag }}
2222
run: |
2323
wget https://apt.llvm.org/llvm.sh
2424
chmod +x ./llvm.sh
2525
sudo ./llvm.sh ${{ matrix.tag }}
2626
sudo apt install -y llvm-${{ matrix.tag }}-dev libclang-${{ matrix.tag }}-dev liblldb-${{ matrix.tag }}-dev
27-
27+
2828
- name: Build
2929
run: bazel build :all
30-
30+
3131
- name: Test
3232
run: bazel test --test_output=errors :all

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
# Bazel directories/symlinks
55
bazel-*
6+
user.bazelrc
67

78
# YouCompleteMe
89
.ycm_extra_conf.py

BUILD

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,8 @@ cc_library(
110110
data = [
111111
"//testdata:test_binary_gen",
112112
"//testdata:test_binary_srcs",
113-
] + select({
114-
"@bazel_tools//src/conditions:windows": [
115-
# There is no lldb-server on Windows.
116-
],
117-
"//conditions:default": [
118-
"@llvm_project_local//:lldb-server",
119-
],
120-
}),
113+
"@llvm_project_local//:lldb-server",
114+
],
121115
deps = [
122116
"@bazel_tools//tools/cpp/runfiles",
123117
"@llvm_project_local//:lldb-api",

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,15 @@ bazel test :all
9191
bazel run :main -- "(1 + 2) * 42 / 4"
9292
```
9393

94+
Depending on your distribution of LLVM, you may need to provide
95+
`--@llvm_project_local//:llvm_build={static,dynamic}` flag. For example, if your
96+
`liblldb.so` is linked dynamically (this is the case when installing via `apt`),
97+
then you need to use `llvm_build=dynamic`. The build script [tries to choose the
98+
correct default value automatically](/build_defs/repo_rules.bzl#L21), but it can
99+
be wrong in some situations (please, report and contribute 🙂).
100+
101+
> **Hint:** You can add this option to your `user.bazelrc`.
102+
94103
## Disclamer
95104

96105
This is not an officially supported Google product.

WORKSPACE

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,20 @@ workspace(name = "lldb_eval")
22

33
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
44

5+
http_archive(
6+
name = "bazel_skylib",
7+
sha256 = "1c531376ac7e5a180e0237938a2536de0c54d93f5c278634818e0efc952dd56c",
8+
urls = [
9+
"https://github.com/bazelbuild/bazel-skylib/releases/download/1.0.3/bazel-skylib-1.0.3.tar.gz",
10+
"https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.0.3/bazel-skylib-1.0.3.tar.gz",
11+
],
12+
)
13+
514
http_archive(
615
name = "com_google_googletest",
7-
sha256 = "ff7a82736e158c077e76188232eac77913a15dac0b22508c390ab3f88e6d6d86",
8-
strip_prefix = "googletest-b6cd405286ed8635ece71c72f118e659f4ade3fb",
9-
urls = ["https://github.com/google/googletest/archive/b6cd405286ed8635ece71c72f118e659f4ade3fb.zip"], # 2019-01-07
16+
sha256 = "94c634d499558a76fa649edb13721dce6e98fb1e7018dfaeba3cd7a083945e91",
17+
strip_prefix = "googletest-release-1.10.0",
18+
urls = ["https://github.com/google/googletest/archive/release-1.10.0.zip"],
1019
)
1120

1221
load("//build_defs:repo_rules.bzl", "llvm_project_configure")

build_defs/llvm_project_local.BUILD

Lines changed: 103 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,44 @@
1+
load("@bazel_skylib//lib:selects.bzl", "selects")
2+
load("@bazel_skylib//rules:common_settings.bzl", "string_flag")
13
load("@rules_cc//cc:defs.bzl", "cc_library")
24

35
package(default_visibility = ["//visibility:public"])
46

7+
string_flag(
8+
name = "llvm_build",
9+
build_setting_default = "{LLVM_BUILD_DEFAULT}",
10+
values = [
11+
"static",
12+
"dynamic",
13+
],
14+
)
15+
16+
config_setting(
17+
name = "llvm_build_static",
18+
flag_values = {":llvm_build": "static"},
19+
)
20+
21+
config_setting(
22+
name = "llvm_build_dynamic",
23+
flag_values = {":llvm_build": "dynamic"},
24+
)
25+
26+
selects.config_setting_group(
27+
name = "linux_dynamic",
28+
match_all = [
29+
"@bazel_tools//src/conditions:linux_x86_64",
30+
":llvm_build_dynamic",
31+
],
32+
)
33+
34+
selects.config_setting_group(
35+
name = "linux_static",
36+
match_all = [
37+
"@bazel_tools//src/conditions:linux_x86_64",
38+
":llvm_build_static",
39+
],
40+
)
41+
542
LLVM_LINKOPTS = select({
643
"@bazel_tools//src/conditions:windows": [],
744
"//conditions:default": [
@@ -68,6 +105,17 @@ filegroup(
68105
]),
69106
)
70107

108+
filegroup(
109+
name = "libllvm-so",
110+
srcs = select({
111+
"@bazel_tools//src/conditions:windows": [
112+
],
113+
"//conditions:default": glob([
114+
"lib/libLLVM*.so*",
115+
]),
116+
}),
117+
)
118+
71119
filegroup(
72120
name = "clang-headers",
73121
srcs = glob([
@@ -80,21 +128,36 @@ filegroup(
80128
]),
81129
)
82130

131+
filegroup(
132+
name = "libclang-cpp-so",
133+
srcs = select({
134+
"@bazel_tools//src/conditions:windows": [
135+
],
136+
"//conditions:default": glob([
137+
"lib/libclang-cpp*.so*",
138+
]),
139+
}),
140+
)
141+
83142
cc_library(
84143
name = "llvm-support",
85144
srcs = select({
86145
"@bazel_tools//src/conditions:windows": [
87146
"lib/LLVMSupport.lib",
88147
"lib/LLVMDemangle.lib",
89148
],
90-
"//conditions:default": [
149+
":linux_dynamic": [
150+
":libllvm-so",
151+
],
152+
":linux_static": [
91153
"lib/libLLVMSupport.a",
92154
"lib/libLLVMDemangle.a",
93155
],
94156
}),
95157
hdrs = [":llvm-headers"],
96158
includes = ["include"],
97159
linkopts = LLVM_LINKOPTS,
160+
linkstatic = 1,
98161
)
99162

100163
cc_library(
@@ -107,8 +170,10 @@ cc_library(
107170
"lib/LLVMBitstreamReader.lib",
108171
"lib/LLVMBinaryFormat.lib",
109172
],
110-
"//conditions:default": [
111-
# $(llvm-config --libs mc core support)
173+
":linux_dynamic": [
174+
":libllvm-so",
175+
],
176+
":linux_static": [
112177
"lib/libLLVMCore.a",
113178
"lib/libLLVMRemarks.a",
114179
"lib/libLLVMBitstreamReader.a",
@@ -117,6 +182,7 @@ cc_library(
117182
}),
118183
hdrs = [":llvm-headers"],
119184
includes = ["include"],
185+
linkstatic = 1,
120186
deps = [":llvm-support"],
121187
)
122188

@@ -128,33 +194,27 @@ cc_library(
128194
"lib/LLVMDebugInfoCodeView.lib",
129195
"lib/LLVMDebugInfoMSF.lib",
130196
],
131-
"//conditions:default": [
197+
":linux_dynamic": [
198+
":libllvm-so",
199+
],
200+
":linux_static": [
132201
"lib/libLLVMMC.a",
133202
"lib/libLLVMDebugInfoCodeView.a",
134203
"lib/libLLVMDebugInfoMSF.a",
135204
],
136205
}),
137206
hdrs = [":llvm-headers"],
138207
includes = ["include"],
208+
linkstatic = 1,
139209
deps = [":llvm-core"],
140210
)
141211

142212
cc_library(
143213
name = "llvm-shared",
144-
srcs = select({
145-
"@bazel_tools//src/conditions:windows": [
146-
],
147-
"//conditions:default": glob([
148-
"lib/libLLVM*.so",
149-
]),
150-
}),
214+
srcs = [":libllvm-so"],
151215
hdrs = [":llvm-headers"],
152216
includes = ["include"],
153-
deps = [
154-
":llvm-core",
155-
":llvm-mc",
156-
":llvm-support",
157-
],
217+
linkstatic = 1,
158218
)
159219

160220
cc_library(
@@ -163,12 +223,16 @@ cc_library(
163223
"@bazel_tools//src/conditions:windows": [
164224
"lib/clangBasic.lib",
165225
],
166-
"//conditions:default": [
226+
":linux_dynamic": [
227+
":libclang-cpp-so",
228+
],
229+
":linux_static": [
167230
"lib/libclangBasic.a",
168231
],
169232
}),
170233
hdrs = [":clang-headers"],
171234
includes = ["include"],
235+
linkstatic = 1,
172236
deps = [
173237
":llvm-core",
174238
":llvm-mc",
@@ -182,18 +246,30 @@ cc_library(
182246
"@bazel_tools//src/conditions:windows": [
183247
"lib/clangLex.lib",
184248
],
185-
"//conditions:default": [
249+
":linux_dynamic": glob([
250+
"lib/libclang-cpp*.so*",
251+
]),
252+
":linux_static": [
186253
"lib/libclangLex.a",
187254
],
188255
}),
189256
hdrs = [":clang-headers"],
190257
includes = ["include"],
258+
linkstatic = 1,
191259
deps = [
192260
":clang-basic",
193261
":llvm-support",
194262
],
195263
)
196264

265+
cc_library(
266+
name = "clangcpp-shared",
267+
srcs = [":libclang-cpp-so"],
268+
hdrs = [":clang-headers"],
269+
includes = ["include"],
270+
linkstatic = 1,
271+
)
272+
197273
cc_library(
198274
name = "lldb-api",
199275
srcs = select({
@@ -211,8 +287,13 @@ cc_library(
211287
"include/lldb/**/*.inc",
212288
]),
213289
includes = ["include"],
214-
deps = [
215-
":llvm-shared", # liblldb can be dynamically linked and depend on libLLVM.
216-
":llvm-support",
217-
],
290+
deps = select({
291+
":llvm_build_dynamic": [
292+
":llvm-shared",
293+
":clangcpp-shared",
294+
],
295+
":llvm_build_static": [
296+
# Don't have any dependencies if linked statically.
297+
],
298+
}),
218299
)

build_defs/repo_rules.bzl

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,25 @@
1616
This module configures a local repository for an llvm-project.
1717
"""
1818

19+
load("@bazel_skylib//lib:paths.bzl", "paths")
20+
21+
def _get_llvm_build_type(ctx, llvm_path):
22+
if ctx.os.name.startswith("windows"):
23+
# There are no prebuilt binaries for Windows, so always assume static.
24+
return "static"
25+
26+
ldd = ctx.which("ldd")
27+
if ldd == None:
28+
# No `ldd`, can't do without it.
29+
return "static"
30+
31+
# Execute `ldd liblldb.so` to figure out whether it depends on libLLVM.so.
32+
result = ctx.execute([ldd, paths.join(llvm_path, "lib", "liblldb.so")])
33+
if result.stdout.find("libLLVM") != -1:
34+
return "dynamic"
35+
36+
return "static"
37+
1938
def _impl(repository_ctx):
2039
llvm_path = repository_ctx.os.environ.get("LLVM_INSTALL_PATH")
2140
if llvm_path == None:
@@ -24,15 +43,21 @@ def _impl(repository_ctx):
2443
"with LLVM {bin,lib,include} directories. E.g. \"/usr/lib/llvm\".",
2544
)
2645

27-
repository_ctx.symlink(llvm_path + "/bin", "bin")
28-
repository_ctx.symlink(llvm_path + "/lib", "lib")
29-
repository_ctx.symlink(llvm_path + "/include", "include")
46+
# Try to guess a default value for :llvm_build flag. It should be "dynamic"
47+
# if liblldb.so is built dynamically and "static" otherwise.
48+
llvm_build = _get_llvm_build_type(repository_ctx, llvm_path)
49+
50+
repository_ctx.symlink(paths.join(llvm_path, "bin"), "bin")
51+
repository_ctx.symlink(paths.join(llvm_path, "lib"), "lib")
52+
repository_ctx.symlink(paths.join(llvm_path, "include"), "include")
3053
repository_ctx.template(
3154
"BUILD",
3255
Label("//build_defs:llvm_project_local.BUILD"),
56+
substitutions = {"{LLVM_BUILD_DEFAULT}": llvm_build},
3357
)
3458

3559
llvm_project_configure = repository_rule(
3660
implementation = _impl,
3761
environ = ["LLVM_INSTALL_PATH"],
62+
local = True,
3863
)

tools/BUILD

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,11 @@ cc_binary(
4646
data = [
4747
"//testdata:fuzzer_binary_gen",
4848
"//testdata:fuzzer_binary_srcs",
49-
] + select({
50-
"@bazel_tools//src/conditions:windows": [
51-
# There is no lldb-server on Windows.
52-
],
53-
"//conditions:default": [
54-
"@llvm_project_local//:lldb-server",
55-
],
56-
}),
49+
"@llvm_project_local//:lldb-server",
50+
],
5751
deps = [
52+
":fuzzer_lib",
5853
"//:lldb-eval",
59-
"//tools:fuzzer_lib",
6054
"@bazel_tools//tools/cpp/runfiles",
6155
"@llvm_project_local//:lldb-api",
6256
],

0 commit comments

Comments
 (0)