Skip to content

Commit fa1d2c2

Browse files
committed
1 parent 0b06759 commit fa1d2c2

File tree

6 files changed

+185
-0
lines changed

6 files changed

+185
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module(
2+
name = "libmagic",
3+
version = "5.46.bcr.1",
4+
compatibility_level = 0,
5+
)
6+
bazel_dep(name = "aspect_bazel_lib", version = "2.14.0")
7+
bazel_dep(name = "platforms", version = "0.0.11")
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
load("@aspect_bazel_lib//lib:expand_template.bzl", "expand_template")
2+
3+
expand_template(
4+
name = "gen_magic_h",
5+
out = "src/magic.h",
6+
template = "src/magic.h.in",
7+
substitutions = {
8+
"X.YY": "546",
9+
},
10+
)
11+
12+
cc_library(
13+
name = "lib_bootstrap",
14+
hdrs = ["src/magic.h"],
15+
srcs = [
16+
"src/apprentice.c",
17+
"src/apptype.c",
18+
"src/ascmagic.c",
19+
"src/buffer.c",
20+
"src/cdf.c",
21+
"src/cdf.h",
22+
"src/cdf_time.c",
23+
"src/compress.c",
24+
"src/der.c",
25+
"src/der.h",
26+
"src/elfclass.h",
27+
"src/encoding.c",
28+
"src/file.h",
29+
"src/file_opts.h",
30+
"src/fmtcheck.c",
31+
"src/fsmagic.c",
32+
"src/funcs.c",
33+
"src/is_csv.c",
34+
"src/is_json.c",
35+
"src/is_simh.c",
36+
"src/is_tar.c",
37+
"src/mygetopt.h",
38+
"src/print.c",
39+
"src/readcdf.c",
40+
"src/readelf.c",
41+
"src/readelf.h",
42+
"src/softmagic.c",
43+
"src/tar.h",
44+
] + select({
45+
"@platforms//os:osx": [],
46+
"//conditions:default": ["src/strlcpy.c"],
47+
}),
48+
includes = ["src"],
49+
copts = [
50+
"-Wno-deprecated-declarations",
51+
"-Wno-unused-variable",
52+
"-Wno-unused-const-variable",
53+
],
54+
defines = [
55+
"HAVE_INTTYPES_H",
56+
"HAVE_MKSTEMP",
57+
"HAVE_STDINT_H",
58+
"HAVE_UNISTD_H",
59+
"VERSION=\\\"546\\\"",
60+
],
61+
)
62+
63+
cc_binary(
64+
name = "file_bootstrap",
65+
srcs = ["src/magic.c", "src/file.c", "src/seccomp.c"],
66+
deps = [":lib_bootstrap"],
67+
)
68+
69+
genrule(
70+
name = "gen_magic",
71+
tools = [":file_bootstrap"],
72+
srcs = glob(["magic/Magdir/*"]),
73+
cmd = """cat $(SRCS) > combined_magic && $(execpath :file_bootstrap) -C -m combined_magic 2>/dev/null && mv combined_magic.mgc $@ && rm combined_magic""",
74+
outs = ["magic.mgc"],
75+
visibility = ["//visibility:public"],
76+
)
77+
78+
cc_library(
79+
name = "lib",
80+
deps = [":lib_bootstrap"],
81+
srcs = ["src/magic.c"],
82+
data = [":magic.mgc"],
83+
defines = ["MAGIC=\\\"$(rlocationpath :magic.mgc)\\\""],
84+
visibility = ["//visibility:public"],
85+
)
86+
87+
cc_shared_library(
88+
name = "magic",
89+
deps = [":lib"],
90+
)
91+
92+
cc_binary(
93+
name = "file",
94+
srcs = ["src/file.c", "src/seccomp.c"],
95+
deps = [":lib"],
96+
)
97+
98+
alias(
99+
name = "libmagic",
100+
actual = "magic",
101+
visibility = ["//visibility:public"],
102+
)
103+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module(
2+
name = "libmagic",
3+
version = "5.46.bcr.1",
4+
compatibility_level = 0,
5+
)
6+
bazel_dep(name = "aspect_bazel_lib", version = "2.14.0")
7+
bazel_dep(name = "platforms", version = "0.0.11")
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
diff --git a/src/magic.c b/src/magic.c
2+
index def46fa3..6e4f47b5 100644
3+
--- a/src/magic.c
4+
+++ b/src/magic.c
5+
@@ -185,6 +185,33 @@ get_default_magic(void)
6+
free(default_magic);
7+
default_magic = NULL;
8+
}
9+
+
10+
+ // Limited runfiles lookup bootstrap.
11+
+ char exe_path[PATH_MAX];
12+
+#ifdef __linux__
13+
+ ssize_t len = readlink("/proc/self/exe", exe_path, sizeof(exe_path)-1);
14+
+ if (len < 0) {
15+
+ fprintf(stderr, "[magic] readlink failed: %s\n", strerror(errno));
16+
+ return MAGIC;
17+
+ }
18+
+ exe_path[len] = '\0';
19+
+#elif defined(__APPLE__)
20+
+ uint32_t size = sizeof(exe_path);
21+
+ if (_NSGetExecutablePath(exe_path, &size) != 0) {
22+
+ fprintf(stderr,
23+
+ "[magic] _NSGetExecutablePath buffer too small (need %u bytes)\n",
24+
+ size);
25+
+ return MAGIC;
26+
+ }
27+
+#endif
28+
+
29+
+ // Build runfiles path and make sure it exists.
30+
+ if (asprintf(&default_magic, "%s.runfiles/%s", exe_path, MAGIC) >= 0 && access(default_magic, R_OK) == 0) {
31+
+ return default_magic;
32+
+ }
33+
+
34+
+ default_magic = NULL;
35+
+
36+
if ((home = getenv("HOME")) == NULL)
37+
return MAGIC;
38+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
matrix:
2+
platform:
3+
- debian10
4+
- ubuntu2004
5+
- macos
6+
- macos_arm64
7+
bazel:
8+
- 8.x
9+
- 7.x
10+
tasks:
11+
verify_targets:
12+
name: Verify build targets
13+
platform: ${{ platform }}
14+
bazel: ${{ bazel }}
15+
build_targets:
16+
- '@libmagic'
17+
- '@libmagic//:file'
18+
- '@libmagic//:magic.mgc'
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"url": "https://github.com/file/file/archive/refs/tags/FILE5_46.tar.gz",
3+
"integrity": "sha256-c8XxGo7fD97S/jRxsjp/zLPzNpoT6mElKbhpyNyWqis=",
4+
"strip_prefix": "file-FILE5_46",
5+
"overlay": {
6+
"MODULE.bazel": "sha256-jVWMH6cImLcrjxKS1rpjpT8omTSgTmHxfbW+gKOMWZM=",
7+
"BUILD.bazel": "sha256-4SK5iM144G0dLX+rfTD4i0NsJr9XIjD0Q432dPOHVDo="
8+
},
9+
"patches": {
10+
"magic_mgc_from_runfiles.patch": "sha256-4SK5iM144G0dLX+rfTD4i0NsJr9XIjD0Q432dPOHVDo="
11+
}
12+
}

0 commit comments

Comments
 (0)