fork/rules_rust

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

UebelAndreReplaced `crates_universe` rule with `crates_repository` (#1158)59fab4e7

0.6.0
7.2 KiB277 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 = "git2",
183    srcs = glob(
184        include = [
185            "src/*.c",
186            "src/*.h",
187            "src/xdiff/*.h",
188            "src/xdiff/*.c",
189            "src/transports/*.h",
190            "src/transports/*.c",
191            "src/streams/*.h",
192            "src/streams/*.c",
193            "src/hash/*.h",
194            "src/hash/*.c",
195        ],
196        exclude = [
197            "src/win32/**",
198            "src/unix/**",
199        ],
200    ) + select({
201        "@platforms//os:windows": glob([
202            "src/win32/**/*.h",
203            "src/win32/**/*.c",
204        ]),
205        "//conditions:default": glob([
206            "src/unix/**/*.h",
207            "src/unix/**/*.c",
208        ]),
209    }) + [
210        "src/allocators/failalloc.c",
211        "src/allocators/failalloc.h",
212        "src/allocators/stdalloc.c",
213        "src/allocators/stdalloc.h",
214        "src/allocators/win32_leakcheck.c",
215        "src/allocators/win32_leakcheck.h",
216        # Use the CollisionDetection SHA1 implementation.
217        "src/hash/sha1/collisiondetect.h",
218        "src/hash/sha1/collisiondetect.c",
219        "src/hash/sha1/sha1dc/sha1.h",
220        "src/hash/sha1/sha1dc/sha1.c",
221        "src/hash/sha1/sha1dc/ubc_check.h",
222        "src/hash/sha1/sha1dc/ubc_check.c",
223    ],
224    hdrs = glob(["include/**/*.h"]) + [
225        ":configure_features",
226    ],
227    copts = select({
228        "@platforms//os:linux": [
229            "-fvisibility=hidden",
230            "-fPIC",
231            "-Wall",
232            "-std=gnu90",
233            # On linux, optimization is required to avoid issues with missing (and unused) symbols:
234            # `liblibgit2.a(pack.pic.o):pack.c:function packfile_open_locked: error: undefined reference to 'fstat'`
235            #
236            # Always enabling optimization helps us avoid this though, it does seem unnecessary and this should
237            # probably be fixed.
238            "-O3",
239        ],
240        "@platforms//os:windows": [],
241        "//conditions:default": [
242            "-fvisibility=hidden",
243            "-fPIC",
244            "-Wall",
245            "-std=gnu90",
246        ],
247    }),
248    defines = [
249        # Use the CollisionDetection SHA1 implementation.
250        "SHA1DC_NO_STANDARD_INCLUDES=1",
251        "SHA1DC_CUSTOM_INCLUDE_SHA1_C=\\\"common.h\\\"",
252        "SHA1DC_CUSTOM_INCLUDE_UBC_CHECK_C=\\\"common.h\\\"",
253        # Use the included PCRE regex backend.
254        "GIT_REGEX_BUILTIN=1",
255    ] + select({
256        ":windows": ["STRSAFE_NO_DEPRECATE"],
257        "//conditions:default": [],
258    }),
259    includes = [
260        "include",
261        "src",
262    ],
263    linkstatic = True,
264    strip_include_prefix = "include",
265    visibility = ["//visibility:public"],
266    deps = [
267        ":http-parser",
268        ":pcre",
269        "@zlib",
270    ],
271)
272
273alias(
274    name = "libgit2",
275    actual = ":git2",
276    visibility = ["//visibility:public"],
277)