xref: /aosp_15_r20/external/bazelbuild-rules_rust/examples/sys/complex/3rdparty/BUILD.libgit2.bazel (revision d4726bddaa87cc4778e7472feed243fa4b6c267f)
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_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_SHA256_COMMON_CRYPTO 1",
161            "#define GIT_HTTPS 1",
162            "#define GIT_SECURE_TRANSPORT 1",
163        ],
164        ":https_setting": [
165            "#define GIT_SHA256_OPENSSL 1",
166            "#define GIT_HTTPS 1",
167            "#define GIT_OPENSSL 1",
168        ],
169        # target.contains("windows")
170        ":https_windows": [
171            "#define GIT_SHA256_WIN32 1",
172            "#define GIT_HTTPS 1",
173            "#define GIT_WINHTTP 1",
174        ],
175        "//conditions:default": [
176            "#define GIT_SHA256_BUILTIN 1",
177        ],
178    }) + select({
179        ":apple": ["#define GIT_USE_ICONV 1"],
180        "//conditions:default": [],
181    }) + [
182        "#endif",
183    ],
184)
185
186cc_library(
187    name = "include",
188    hdrs = [":configure_features"] + glob(["include/**/*.h"]),
189    strip_include_prefix = "include",
190)
191
192cc_library(
193    name = "git2",
194    srcs = [
195        "src/util/allocators/failalloc.c",
196        "src/util/allocators/stdalloc.c",
197        "src/util/allocators/win32_leakcheck.c",
198        # Use the CollisionDetection SHA1 implementation.
199        "src/util/hash/collisiondetect.c",
200        "src/util/hash/sha1dc/sha1.c",
201        "src/util/hash/sha1dc/ubc_check.c",
202    ] + glob(
203        include = [
204            "src/libgit2/*.c",
205            "src/util/*.c",
206            "src/libgit2/xdiff/*.c",
207            "src/libgit2/transports/*.c",
208            "src/libgit2/streams/*.c",
209        ],
210        exclude = [
211            "src/util/win32/**",
212            "src/util/unix/**",
213        ],
214    ) + select({
215        "@platforms//os:windows": glob(["src/util/win32/**/*.c"]),
216        "//conditions:default": glob(["src/util/unix/**/*.c"]),
217    }) + select({
218        ":https_setting": [],
219        "//conditions:default": [
220            "src/util/hash/builtin.c",
221            "src/util/hash/rfc6234/sha224-256.c",
222        ],
223    }),
224    hdrs = [
225        "src/util/allocators/failalloc.h",
226        "src/util/allocators/stdalloc.h",
227        "src/util/allocators/win32_leakcheck.h",
228        # Use the CollisionDetection SHA1 implementation.
229        "src/util/hash/sha1dc/sha1.h",
230        "src/util/hash/sha1dc/ubc_check.h",
231    ] + glob(
232        include = [
233            "src/libgit2/*.h",
234            "src/libgit2/streams/*.h",
235            "src/libgit2/transports/*.h",
236            "src/libgit2/xdiff/*.h",
237            "src/util/*.h",
238            "src/util/hash/*.h",
239            "src/util/hash/rfc6234/*.h",
240        ],
241        exclude = [
242            "src/util/win32/**",
243            "src/util/unix/**",
244        ],
245    ) + select({
246        "@platforms//os:windows": glob(["src/util/win32/**/*.h"]),
247        "//conditions:default": glob(["src/util/unix/**/*.h"]),
248    }),
249    copts = select({
250        "@platforms//os:linux": [
251            "-fvisibility=hidden",
252            "-fPIC",
253            "-Wall",
254            "-std=gnu90",
255            # On linux, optimization is required to avoid issues with missing (and unused) symbols:
256            # `liblibgit2.a(pack.pic.o):pack.c:function packfile_open_locked: error: undefined reference to 'fstat'`
257            #
258            # Always enabling optimization helps us avoid this though, it does seem unnecessary and this should
259            # probably be fixed.
260            "-O3",
261        ],
262        "@platforms//os:windows": [],
263        "//conditions:default": [
264            "-fvisibility=hidden",
265            "-fPIC",
266            "-Wall",
267            "-std=gnu90",
268        ],
269    }),
270    defines = [
271        # Use the CollisionDetection SHA1 implementation.
272        "SHA1DC_NO_STANDARD_INCLUDES=1",
273        "SHA1DC_CUSTOM_INCLUDE_SHA1_C=\\\"common.h\\\"",
274        "SHA1DC_CUSTOM_INCLUDE_UBC_CHECK_C=\\\"common.h\\\"",
275        # Use the included PCRE regex backend.
276        "GIT_REGEX_BUILTIN=1",
277    ] + select({
278        ":windows": ["STRSAFE_NO_DEPRECATE"],
279        "//conditions:default": [],
280    }),
281    includes = [
282        "src/libgit2",
283        "src/util",
284    ],
285    linkstatic = True,
286    strip_include_prefix = "src",
287    visibility = ["//visibility:public"],
288    deps = [
289        ":http-parser",
290        ":include",
291        ":pcre",
292        "@zlib",
293    ],
294)
295
296alias(
297    name = "libgit2",
298    actual = ":git2",
299    visibility = ["//visibility:public"],
300)
301