Skip to content

Commit b36dc41

Browse files
Copilotphlax
andcommitted
Fix bzl macro and symbol renaming script
Co-authored-by: phlax <454682+phlax@users.noreply.github.com>
1 parent 6f2b89f commit b36dc41

File tree

4 files changed

+121
-94
lines changed

4 files changed

+121
-94
lines changed

bazel-registry/modules/hyperscan/5.4.2.envoy/overlay/BUILD.bazel

Lines changed: 7 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,5 @@
1-
load("@rules_cc//cc:defs.bzl", "cc_library", "cc_import")
2-
3-
# Macro to build a single architecture variant of the runtime library
4-
def _hs_exec_variant(name, arch, march_flag, additional_srcs = []):
5-
"""Build an architecture-specific variant of the runtime library.
6-
7-
Args:
8-
name: Name of the variant (e.g., "core2", "avx2")
9-
arch: Architecture identifier for symbol prefixing
10-
march_flag: GCC -march flag (e.g., "-march=core2")
11-
additional_srcs: Additional source files specific to this variant
12-
"""
13-
lib_name = "hs_exec_" + name
14-
renamed_name = lib_name + "_renamed"
15-
16-
# Compile the runtime sources with specific architecture flags
17-
native.cc_library(
18-
name = lib_name,
19-
srcs = native.glob(
20-
[
21-
"src/crc32.c",
22-
"src/database.c",
23-
"src/runtime.c",
24-
"src/stream_compress.c",
25-
"src/hs_version.c",
26-
"src/hs_valid_platform.c",
27-
"src/fdr/*.c",
28-
"src/hwlm/*.c",
29-
"src/nfa/*.c",
30-
"src/rose/*.c",
31-
"src/som/*.c",
32-
"src/util/masked_move.c",
33-
"src/util/simd_utils.c",
34-
"src/util/state_compress.c",
35-
],
36-
exclude = [
37-
"src/**/*_dump*.c",
38-
"src/**/test_*.c",
39-
"src/hwlm/noodle_engine_avx2.c",
40-
"src/hwlm/noodle_engine_avx512.c",
41-
"src/hwlm/noodle_engine_sse.c",
42-
],
43-
) + additional_srcs,
44-
hdrs = native.glob(["src/**/*.h"]),
45-
textual_hdrs = [
46-
"src/hwlm/noodle_engine_avx2.c",
47-
"src/hwlm/noodle_engine_avx512.c",
48-
"src/hwlm/noodle_engine_sse.c",
49-
],
50-
copts = [
51-
"-std=c99",
52-
"-O3",
53-
"-DNDEBUG",
54-
"-fno-strict-aliasing",
55-
march_flag,
56-
"-Wno-unused-parameter",
57-
"-Wno-sign-compare",
58-
],
59-
includes = [
60-
".",
61-
"src",
62-
],
63-
target_compatible_with = [
64-
"@platforms//cpu:x86_64",
65-
"@platforms//os:linux",
66-
],
67-
visibility = ["//visibility:private"],
68-
deps = [":hs_common"],
69-
alwayslink = 1,
70-
)
71-
72-
# Extract the archive and rename symbols
73-
native.genrule(
74-
name = renamed_name,
75-
srcs = [":" + lib_name],
76-
outs = ["lib" + renamed_name + ".a"],
77-
cmd = "$(location :rename_symbols.sh) " + arch + " $(location :" + lib_name + ") $@",
78-
tools = [":rename_symbols.sh"],
79-
)
80-
81-
# Import the renamed archive back as a cc_library
82-
native.cc_import(
83-
name = renamed_name + "_import",
84-
static_library = ":" + renamed_name,
85-
)
1+
load("@rules_cc//cc:defs.bzl", "cc_library")
2+
load(":fat_runtime.bzl", "hs_exec_variant")
863

874
genrule(
885
name = "config_h",
@@ -190,11 +107,11 @@ cc_library(
190107
exports_files(["rename_symbols.sh"])
191108

192109
# Build five architecture variants
193-
_hs_exec_variant("core2", "core2", "-march=core2")
194-
_hs_exec_variant("corei7", "corei7", "-march=corei7")
195-
_hs_exec_variant("avx2", "avx2", "-march=core-avx2")
196-
_hs_exec_variant("avx512", "avx512", "-march=skylake-avx512")
197-
_hs_exec_variant("avx512vbmi", "avx512vbmi", "-march=icelake-server")
110+
hs_exec_variant("core2", "core2", "-march=core2")
111+
hs_exec_variant("corei7", "corei7", "-march=corei7")
112+
hs_exec_variant("avx2", "avx2", "-march=core-avx2")
113+
hs_exec_variant("avx512", "avx512", "-march=skylake-avx512")
114+
hs_exec_variant("avx512vbmi", "avx512vbmi", "-march=icelake-server")
198115

199116
# Dispatcher library
200117
cc_library(
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
"""Macro for building fat runtime variants of hyperscan."""
2+
3+
load("@rules_cc//cc:defs.bzl", "cc_import", "cc_library")
4+
5+
def hs_exec_variant(name, arch, march_flag):
6+
"""Build an architecture-specific variant of the runtime library.
7+
8+
Args:
9+
name: Name of the variant (e.g., "core2", "avx2")
10+
arch: Architecture identifier for symbol prefixing
11+
march_flag: GCC -march flag (e.g., "-march=core2")
12+
"""
13+
lib_name = "hs_exec_" + name
14+
renamed_name = lib_name + "_renamed"
15+
16+
# Compile the runtime sources with specific architecture flags
17+
cc_library(
18+
name = lib_name,
19+
srcs = native.glob(
20+
[
21+
"src/crc32.c",
22+
"src/database.c",
23+
"src/runtime.c",
24+
"src/stream_compress.c",
25+
"src/hs_version.c",
26+
"src/hs_valid_platform.c",
27+
"src/fdr/*.c",
28+
"src/hwlm/*.c",
29+
"src/nfa/*.c",
30+
"src/rose/*.c",
31+
"src/som/*.c",
32+
"src/util/masked_move.c",
33+
"src/util/simd_utils.c",
34+
"src/util/state_compress.c",
35+
],
36+
exclude = [
37+
"src/**/*_dump*.c",
38+
"src/**/test_*.c",
39+
"src/hwlm/noodle_engine_avx2.c",
40+
"src/hwlm/noodle_engine_avx512.c",
41+
"src/hwlm/noodle_engine_sse.c",
42+
],
43+
),
44+
hdrs = native.glob(["src/**/*.h"]),
45+
textual_hdrs = [
46+
"src/hwlm/noodle_engine_avx2.c",
47+
"src/hwlm/noodle_engine_avx512.c",
48+
"src/hwlm/noodle_engine_sse.c",
49+
],
50+
copts = [
51+
"-std=c99",
52+
"-O3",
53+
"-DNDEBUG",
54+
"-fno-strict-aliasing",
55+
march_flag,
56+
"-Wno-unused-parameter",
57+
"-Wno-sign-compare",
58+
],
59+
includes = [
60+
".",
61+
"src",
62+
],
63+
linkstatic = True,
64+
target_compatible_with = [
65+
"@platforms//cpu:x86_64",
66+
"@platforms//os:linux",
67+
],
68+
visibility = ["//visibility:private"],
69+
deps = [":hs_common"],
70+
)
71+
72+
# Extract the static library and rename symbols
73+
native.genrule(
74+
name = renamed_name,
75+
srcs = [":" + lib_name],
76+
outs = ["lib" + renamed_name + ".a"],
77+
cmd = """
78+
# Find the .a file in the inputs
79+
for f in $(SRCS); do
80+
if [[ "$$f" == *.a ]]; then
81+
INPUT_AR="$$f"
82+
break
83+
fi
84+
done
85+
if [ -z "$$INPUT_AR" ]; then
86+
echo "Error: Could not find .a file in inputs"
87+
exit 1
88+
fi
89+
# Run the symbol renaming script
90+
$(location :rename_symbols.sh) """ + arch + """ "$$INPUT_AR" $@
91+
""",
92+
tools = [":rename_symbols.sh"],
93+
target_compatible_with = [
94+
"@platforms//cpu:x86_64",
95+
"@platforms//os:linux",
96+
],
97+
)
98+
99+
# Import the renamed archive back as a cc_library
100+
cc_import(
101+
name = renamed_name + "_import",
102+
static_library = ":" + renamed_name,
103+
target_compatible_with = [
104+
"@platforms//cpu:x86_64",
105+
"@platforms//os:linux",
106+
],
107+
visibility = ["//visibility:private"],
108+
)
109+

bazel-registry/modules/hyperscan/5.4.2.envoy/overlay/rename_symbols.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
set -e
66

77
PREFIX=$1
8-
INPUT_AR=$2
9-
OUTPUT_AR=$3
8+
INPUT_AR=$(realpath "$2")
9+
OUTPUT_AR=$(realpath "$3")
1010

1111
# Create temporary directory for work
1212
TMPDIR=$(mktemp -d)

bazel-registry/modules/hyperscan/5.4.2.envoy/source.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
},
88
"patch_strip": 1,
99
"overlay": {
10-
"BUILD.bazel": "sha256-KOEiiIESrQoo6ziXPLzEUP5oSSyyz0WLYWxzzUghtC4=",
10+
"BUILD.bazel": "sha256-i7NsaaTDXpWaXsXzjyMgki/tMeeseuuV6kqihn/CohQ=",
1111
"MODULE.bazel": "sha256-rhTO03T2Bmq1sVdF5qhFZ74M8CPkFwl8g2qNmHpgTmQ=",
1212
"dispatcher.c": "sha256-QxcWJt0umsfBxzGhCgurySAxpNpAmT2+VDdXHcQd24o=",
13-
"rename_symbols.sh": "sha256-3q3bWPe1IoyBqyNHTTGpyhDOZcmR2mn97u6+/hwd2yo="
13+
"fat_runtime.bzl": "sha256-CP3rZhMbOThXZBwGnNEpRom7lPef6Qt xBGo2jkYDxKw=",
14+
"rename_symbols.sh": "sha256-T89H3ivs1BvUGZWqyJqbZ3uz1kMxU8ZJKkxAGY3xMFs="
1415
}
1516
}

0 commit comments

Comments
 (0)