Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .bazelversion

This file was deleted.

97 changes: 84 additions & 13 deletions bazel/bison.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@
"""Bazel rule to run bison toolchain
"""

def _add_suffix_to_location_references(option, suffix):
"""Helper function to add a suffix to $(location ...) file names in options."""
if "$(location " in option:
# Extract the output file name
start = option.find("$(location ") + len("$(location ")
end = option.find(")", start)
file_name = option[start:end]

# Add suffix to the location reference
return option.replace("$(location " + file_name + ")", "$(location " + file_name + suffix + ")")
return option

# Adapter rule around the @rules_bison toolchain.
def genyacc(
name,
Expand All @@ -26,21 +38,80 @@ def genyacc(
extra_outs = []):
"""Build rule for generating C or C++ sources with Bison.
"""

# Note: some attributes such as (toolchains and name) cannot use select. So, although verbose, we create a
# genrule for each condition here and copy the resulting file to the expected location.

# For rules_bison toolchain case
native.genrule(
name = name + "_default",
srcs = [src],
outs = [header_out + ".default", source_out + ".default"] + [out + ".default" for out in extra_outs],
cmd = "M4=$(M4) $(BISON) --defines=$(location " + header_out + ".default) " +
"--output-file=$(location " + source_out + ".default) " +
" ".join([_add_suffix_to_location_references(opt, ".default") for opt in extra_options]) + " $<",
toolchains = [
"@rules_bison//bison:current_bison_toolchain",
"@rules_m4//m4:current_m4_toolchain",
],
tags = ["manual"],
)

# For local bison case
native.genrule(
name = name,
name = name + "_local",
srcs = [src],
outs = [header_out, source_out] + extra_outs,
cmd = select({
"//bazel:use_local_flex_bison_enabled": "bison --defines=$(location " + header_out + ") --output-file=$(location " + source_out + ") " + " ".join(extra_options) + " $<",
"@platforms//os:windows": "win_bison.exe --defines=$(location " + header_out + ") --output-file=$(location " + source_out + ") " + " ".join(extra_options) + " $<",
"//conditions:default": "M4=$(M4) $(BISON) --defines=$(location " + header_out + ") --output-file=$(location " + source_out + ") " + " ".join(extra_options) + " $<",
outs = [header_out + ".local", source_out + ".local"] + [out + ".local" for out in extra_outs],
cmd = "bison --defines=$(location " + header_out + ".local) " +
"--output-file=$(location " + source_out + ".local) " +
" ".join([_add_suffix_to_location_references(opt, ".local") for opt in extra_options]) + " $<",
tags = ["manual"],
)

# For Windows case
native.genrule(
name = name + "_windows",
srcs = [src],
outs = [header_out + ".windows", source_out + ".windows"] + [out + ".windows" for out in extra_outs],
cmd = "win_bison.exe --defines=$(location " + header_out + ".windows) " +
"--output-file=$(location " + source_out + ".windows) " +
" ".join([_add_suffix_to_location_references(opt, ".windows") for opt in extra_options]) + " $<",
tags = ["manual"],
)

# Create copy rules for each output
native.genrule(
name = name + "_copy_header",
srcs = select({
"//bazel:use_local_flex_bison_enabled": [header_out + ".local"],
"@platforms//os:windows": [header_out + ".windows"],
"//conditions:default": [header_out + ".default"],
}),
toolchains = select({
"//bazel:use_local_flex_bison_enabled": [],
"@platforms//os:windows": [],
"//conditions:default": [
"@rules_bison//bison:current_bison_toolchain",
"@rules_m4//m4:current_m4_toolchain",
],
outs = [header_out],
cmd = "cp $< $@",
)

native.genrule(
name = name + "_copy_source",
srcs = select({
"//bazel:use_local_flex_bison_enabled": [source_out + ".local"],
"@platforms//os:windows": [source_out + ".windows"],
"//conditions:default": [source_out + ".default"],
}),
outs = [source_out],
cmd = "cp $< $@",
)

# Create copy rules for each extra output
for extra_out in extra_outs:
sanitized_name = extra_out.replace(".", "_").replace("/", "_").replace("-", "_")
native.genrule(
name = name + "_copy_" + sanitized_name,
srcs = select({
"//bazel:use_local_flex_bison_enabled": [extra_out + ".local"],
"@platforms//os:windows": [extra_out + ".windows"],
"//conditions:default": [extra_out + ".default"],
}),
outs = [extra_out],
cmd = "cp $< $@",
)
56 changes: 42 additions & 14 deletions bazel/flex.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,49 @@
def genlex(name, src, out):
"""Generate C/C++ language source from lex file using Flex
"""

# Note: some attributes such as (toolchains and name) cannot use select. So, although verbose, we create a
# genrule for each condition here and copy the resulting file to the expected location.

# For rules_flex default case
native.genrule(
name = name,
name = name + "_default",
srcs = [src],
outs = [out],
cmd = select({
"//bazel:use_local_flex_bison_enabled": "flex --outfile=$@ $<",
"@platforms//os:windows": "win_flex.exe --outfile=$@ $<",
"//conditions:default": "M4=$(M4) $(FLEX) --outfile=$@ $<",
}),
toolchains = select({
"//bazel:use_local_flex_bison_enabled": [],
"@platforms//os:windows": [],
"//conditions:default": [
"@rules_flex//flex:current_flex_toolchain",
"@rules_m4//m4:current_m4_toolchain",
],
outs = [out + ".default"],
cmd = "M4=$(M4) $(FLEX) --outfile=$(location " + out + ".default) $<",
toolchains = [
"@rules_flex//flex:current_flex_toolchain",
"@rules_m4//m4:current_m4_toolchain",
],
tags = ["manual"],
)

# For local flex case
native.genrule(
name = name + "_local",
srcs = [src],
outs = [out + ".local"],
cmd = "flex --outfile=$(location " + out + ".local) $<",
tags = ["manual"],
)

# For Windows case
native.genrule(
name = name + "_windows",
srcs = [src],
outs = [out + ".windows"],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, the suffix is not mysterious anymore. But looks like it messes with the rest of the build-system

cmd = "win_flex.exe --outfile=$(location " + out + ".windows) $<",
tags = ["manual"],
)

# Create a copy rule to get the final output
native.genrule(
name = name,
srcs = select({
"//bazel:use_local_flex_bison_enabled": [out + ".local"],
"@platforms//os:windows": [out + ".windows"],
"//conditions:default": [out + ".default"],
}),
outs = [out],
cmd = "cp $< $@",
)
Loading