diff --git a/.bazelversion b/.bazelversion deleted file mode 100644 index 93c8ddab9..000000000 --- a/.bazelversion +++ /dev/null @@ -1 +0,0 @@ -7.6.0 diff --git a/bazel/bison.bzl b/bazel/bison.bzl index f19120912..2a27fbf2b 100644 --- a/bazel/bison.bzl +++ b/bazel/bison.bzl @@ -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, @@ -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 $< $@", + ) diff --git a/bazel/flex.bzl b/bazel/flex.bzl index a398c6f7e..3a4f840ea 100644 --- a/bazel/flex.bzl +++ b/bazel/flex.bzl @@ -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"], + 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 $< $@", )