fork/rules_rust

git clone https://git.julia.blue/fork/rules_rust

ThomasFix @libgit2 (#1457)ced94dec

0.8.0
7.5 KiB284 linesraw
1# This BUILD file was written based off of the `build.rs` script for the Rust
2# crate `libgit2-sys`. For more details, see the crate's source:
3# https://github.com/rust-lang/git2-rs/tree/libgit2-sys-0.13.0/libgit2-sys
4
5load("@bazel_skylib//lib:selects.bzl", "selects")
6load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
7load("@bazel_skylib//rules:write_file.bzl", "write_file")
8load("@rules_cc//cc:defs.bzl", "cc_library")
9
10[
11    config_setting(
12        name = plat,
13        constraint_values = ["@platforms//os:{}".format(plat)],
14    )
15    for plat in [
16        "macos",
17        "ios",
18        "tvos",
19        "windows",
20    ]
21]
22
23# env::var("CARGO_FEATURE_SSH").is_ok()
24bool_flag(
25    name = "ssh",
26    build_setting_default = False,
27)
28
29config_setting(
30    name = "ssh_setting",
31    flag_values = {":ssh": "True"},
32)
33
34# target.contains("apple")
35selects.config_setting_group(
36    name = "apple",
37    match_any = [
38        ":macos",
39        ":ios",
40        ":tvos",
41    ],
42)
43
44[
45    config_setting(
46        name = "cpu_" + cpu,
47        constraint_values = ["@platforms//cpu:{}".format(cpu)],
48    )
49    for cpu in [
50        "i386",
51        "x86_32",
52    ]
53]
54
55# env::var("CARGO_CFG_TARGET_POINTER_WIDTH").unwrap() == "32"
56selects.config_setting_group(
57    name = "pointer_width_32",
58    match_any = [
59        "cpu_i386",
60        "cpu_x86_32",
61    ],
62)
63
64# env::var("CARGO_FEATURE_HTTPS").is_ok()
65bool_flag(
66    name = "https",
67    build_setting_default = False,
68)
69
70config_setting(
71    name = "https_setting",
72    flag_values = {":https": "True"},
73)
74
75# if https && target.contains("windows")
76selects.config_setting_group(
77    name = "https_windows",
78    match_all = [
79        ":https_setting",
80        ":windows",
81    ],
82)
83
84# if https && target.contains("apple")
85selects.config_setting_group(
86    name = "https_apple",
87    match_all = [
88        ":https_setting",
89        ":apple",
90    ],
91)
92
93cc_library(
94    name = "http-parser",
95    srcs = glob(["deps/http-parser/*.c"]),
96    hdrs = glob(["deps/http-parser/*.h"]),
97    copts = select({
98        # Required in `opt` builds to solve the following error
99        # libpcre.a(pcre_compile.o): requires unsupported dynamic reloc 11; recompile with -fPIC
100        "@platforms//os:linux": ["-fPIC"],
101        "//conditions:default": [],
102    }),
103    includes = ["deps/http-parser"],
104    linkstatic = True,
105)
106
107cc_library(
108    name = "pcre",
109    srcs = glob(["deps/pcre/**/*.c"]),
110    hdrs = glob(["deps/pcre/**/*.h"]),
111    copts = select({
112        # Required in `opt` builds to solve the following error
113        # libhttp-parser.a(http_parser.o): requires unsupported dynamic reloc 11; recompile with -fPIC
114        "@platforms//os:linux": ["-fPIC"],
115        "//conditions:default": [],
116    }),
117    defines = [
118        "HAVE_STDINT_H=1",
119        "HAVE_MEMMOVE=1",
120        "NO_RECURSE=1",
121        "NEWLINE=10",
122        "POSIX_MALLOC_THRESHOLD=10",
123        "LINK_SIZE=2",
124        "PARENS_NEST_LIMIT=250",
125        "MATCH_LIMIT=10000000",
126        "MATCH_LIMIT_RECURSION=MATCH_LIMIT",
127        "MAX_NAME_SIZE=32",
128        "MAX_NAME_COUNT=10000",
129    ],
130    includes = ["deps/pcre"],
131    linkstatic = True,
132)
133
134write_file(
135    name = "configure_features",
136    out = "include/git2/sys/features.h",
137    content = [
138        "#ifndef INCLUDE_features_h",
139        "#define INCLUDE_features_h",
140        "#define GIT_THREADS 1",
141        "#define GIT_TRACE 1",
142    ] +
143    # !target.contains("android")
144    select({
145        "@platforms//os:android": [],
146        "//conditions:default": ["#define GIT_USE_NSEC 1"],
147    }) + select({
148        ":apple": ["#define GIT_USE_STAT_MTIMESPEC 1"],
149        "//conditions:default": ["#define GIT_USE_STAT_MTIM 1"],
150    }) + select({
151        ":pointer_width_32": ["#define GIT_ARCH_32 1"],
152        "//conditions:default": ["#define GIT_ARCH_64 1"],
153    }) + [
154        "#define GIT_SHA1_COLLISIONDETECT 1",
155    ] + select({
156        ":ssh_setting": ["#define GIT_SSH 1"],
157        "//conditions:default": [],
158    }) + select({
159        ":https_apple": [
160            "#define GIT_HTTPS 1",
161            "#define GIT_SECURE_TRANSPORT 1",
162        ],
163        ":https_setting": [
164            "#define GIT_HTTPS 1",
165            "#define GIT_OPENSSL 1",
166        ],
167        # target.contains("windows")
168        ":https_windows": [
169            "#define GIT_HTTPS 1",
170            "#define GIT_WINHTTP 1",
171        ],
172        "//conditions:default": [],
173    }) + select({
174        ":apple": ["#define GIT_USE_ICONV 1"],
175        "//conditions:default": [],
176    }) + [
177        "#endif",
178    ],
179)
180
181cc_library(
182    name = "include",
183    hdrs = [":configure_features"] + glob(["include/**/*.h"]),
184    strip_include_prefix = "include",
185)
186
187cc_library(
188    name = "git2",
189    srcs = [
190        "src/allocators/failalloc.c",
191        "src/allocators/stdalloc.c",
192        "src/allocators/win32_leakcheck.c",
193        # Use the CollisionDetection SHA1 implementation.
194        "src/hash/sha1/collisiondetect.c",
195        "src/hash/sha1/sha1dc/sha1.c",
196        "src/hash/sha1/sha1dc/ubc_check.c",
197    ] + glob(
198        include = [
199            "src/*.c",
200            "src/xdiff/*.c",
201            "src/transports/*.c",
202            "src/streams/*.c",
203            "src/hash/*.c",
204        ],
205        exclude = [
206            "src/win32/**",
207            "src/unix/**",
208        ],
209    ) + select({
210        "@platforms//os:windows": glob(["src/win32/**/*.c"]),
211        "//conditions:default": glob(["src/unix/**/*.c"]),
212    }),
213    hdrs = [
214        "src/allocators/failalloc.h",
215        "src/allocators/stdalloc.h",
216        "src/allocators/win32_leakcheck.h",
217        # Use the CollisionDetection SHA1 implementation.
218        "src/hash/sha1/collisiondetect.h",
219        "src/hash/sha1/sha1dc/sha1.h",
220        "src/hash/sha1/sha1dc/ubc_check.h",
221    ] + glob(
222        include = [
223            "src/*.h",
224            "src/xdiff/*.h",
225            "src/transports/*.h",
226            "src/streams/*.h",
227            "src/hash/*.h",
228        ],
229        exclude = [
230            "src/win32/**",
231            "src/unix/**",
232        ],
233    ) + select({
234        "@platforms//os:windows": glob(["src/win32/**/*.h"]),
235        "//conditions:default": glob(["src/unix/**/*.h"]),
236    }),
237    copts = select({
238        "@platforms//os:linux": [
239            "-fvisibility=hidden",
240            "-fPIC",
241            "-Wall",
242            "-std=gnu90",
243            # On linux, optimization is required to avoid issues with missing (and unused) symbols:
244            # `liblibgit2.a(pack.pic.o):pack.c:function packfile_open_locked: error: undefined reference to 'fstat'`
245            #
246            # Always enabling optimization helps us avoid this though, it does seem unnecessary and this should
247            # probably be fixed.
248            "-O3",
249        ],
250        "@platforms//os:windows": [],
251        "//conditions:default": [
252            "-fvisibility=hidden",
253            "-fPIC",
254            "-Wall",
255            "-std=gnu90",
256        ],
257    }),
258    defines = [
259        # Use the CollisionDetection SHA1 implementation.
260        "SHA1DC_NO_STANDARD_INCLUDES=1",
261        "SHA1DC_CUSTOM_INCLUDE_SHA1_C=\\\"common.h\\\"",
262        "SHA1DC_CUSTOM_INCLUDE_UBC_CHECK_C=\\\"common.h\\\"",
263        # Use the included PCRE regex backend.
264        "GIT_REGEX_BUILTIN=1",
265    ] + select({
266        ":windows": ["STRSAFE_NO_DEPRECATE"],
267        "//conditions:default": [],
268    }),
269    linkstatic = True,
270    strip_include_prefix = "src",
271    visibility = ["//visibility:public"],
272    deps = [
273        ":http-parser",
274        ":include",
275        ":pcre",
276        "@zlib",
277    ],
278)
279
280alias(
281    name = "libgit2",
282    actual = ":git2",
283    visibility = ["//visibility:public"],
284)