diff --git a/dependency_support/com_github_westes_flex/BUILD b/dependency_support/com_github_westes_flex/BUILD.bazel similarity index 100% rename from dependency_support/com_github_westes_flex/BUILD rename to dependency_support/com_github_westes_flex/BUILD.bazel diff --git a/dependency_support/com_github_westes_flex/bundled.BUILD.bazel b/dependency_support/com_github_westes_flex/bundled.BUILD.bazel deleted file mode 100644 index f24f8f64..00000000 --- a/dependency_support/com_github_westes_flex/bundled.BUILD.bazel +++ /dev/null @@ -1,208 +0,0 @@ -# Copyright 2020 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -""" flex - -!!! DO NOT DEPEND ON THIS PACKAGE DIRECTLY !!! -This package exists only to support the genlex rule, and all users should use -that if possible rather than using the binary directly through a genrule. -""" - -load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library") -load("@rules_hdl//dependency_support/com_github_westes_flex:flex.bzl", "genlex") -load("@rules_hdl//dependency_support:pseudo_configure.bzl", "pseudo_configure") - -package(default_visibility = ["//visibility:private"]) - -licenses(["notice"]) # BSD - -exports_files(["LICENSE"]) - -VERSION = "2.6.4" - -# Generate the skel.c file. The generated file is included in the Flex -# distribution, but we have local patches that require regeneration. This -# follows the command at line 94 of src/Makefile.am. -genrule( - name = "skel_c", - srcs = [ - "src/flex.skl", - "src/mkskel.sh", - "src/flexint.h", - "src/tables_shared.h", - "src/tables_shared.c", - ], - outs = ["src/skel.c"], - cmd = ("$(location src/mkskel.sh) `dirname $(location src/flex.skl)` " + - "$(location @org_gnu_m4//:m4) " + VERSION + " > $@"), - tools = [ - "@org_gnu_m4//:m4", - ], -) - -# Generate the stage1scan.c file. This follows the non-ENABLE_BOOTSTRAP command -# at line 102 of src/Makefile.am. -genrule( - name = "stage1scan", - srcs = ["src/scan.c"], - outs = ["src/stage1scan.c"], - cmd = """sed 's|^\\(#line .*\\)"'$$(basename $< | sed 's|[][\\\\.*]|\\\\&|g')'"|\\1"$@"|g' $< > $@""", -) - -# These need to be a separate rule, as cc_binary does not support textual_hdrs, -# and many of these headers are not standalone. -cc_library( - name = "flex_internal_headers", - textual_hdrs = [ - "src/config.h", - "src/flexdef.h", - "src/flexint.h", - "src/gettext.h", - "src/options.h", - "src/parse.h", - "src/scanopt.h", - "src/tables.h", - "src/tables_shared.h", - "src/version.h", - ], -) - -# Allows users to '#include ' correctly in their own code. -cc_library( - name = "FlexLexer", - hdrs = ["src/FlexLexer.h"], - includes = ["src"], - visibility = ["//visibility:public"], -) - -exports_files( - ["src/FlexLexer.h"], - visibility = ["//dependency_support/com_github_westes_flex/v2_5_35:__pkg__"], -) - -# The "flex" binary. This should only be used through genlex rules, not with -# genrules. -cc_binary( - name = "flex", - srcs = [ - "src/buf.c", - "src/ccl.c", - "src/dfa.c", - "src/ecs.c", - "src/filter.c", - "src/gen.c", - "src/main.c", - "src/misc.c", - "src/nfa.c", - "src/options.c", - "src/parse.c", - "src/regex.c", - "src/scanflags.c", - "src/scanopt.c", - "src/skel.c", - "src/stage1scan.c", - "src/sym.c", - "src/tables.c", - "src/tables_shared.c", - "src/tblcmp.c", - "src/yylex.c", - ], - includes = ["src"], - copts = [ - "-DHAVE_CONFIG_H", - "-DLOCALEDIR='\"" + "this_LOCALEDIR_does_not_exist" + "\"'", - "-w", - ], - visibility = ["//visibility:public"], - deps = [ - ":flex_internal_headers", - ], -) - -pseudo_configure( - name = "config_h", - src = "src/config.h.in", - out = "src/config.h", - defs = [ - "HAVE_ALLOCA", - "HAVE_ALLOCA_H", - "HAVE_DLFCN_H", - "HAVE_DUP2", - "HAVE_FORK", - "HAVE_INTTYPES_H", - "HAVE_LIBM", - "HAVE_LIMITS_H", - "HAVE_MALLOC", - "HAVE_MALLOC_H", - "HAVE_MEMORY_H", - "HAVE_MEMSET", - "HAVE_NETINET_IN_H", - "HAVE_POW", - "HAVE_PTHREAD_H", - "HAVE_REALLOC", - "HAVE_REGCOMP", - "HAVE_REGEX_H", - "HAVE_STDBOOL_H", - "HAVE_STDINT_H", - "HAVE_STDLIB_H", - "HAVE_STRCASECMP", - "HAVE_STRCHR", - "HAVE_STRDUP", - "HAVE_STRINGS_H", - "HAVE_STRING_H", - "HAVE_STRTOL", - "HAVE_SYS_STAT_H", - "HAVE_SYS_TYPES_H", - "HAVE_SYS_WAIT_H", - "HAVE_UNISTD_H", - "HAVE_VFORK", - "HAVE_WORKING_FORK", - "HAVE_WORKING_VFORK", - "HAVE__BOOL", - "STDC_HEADERS", - ], - mappings = { - "LT_OBJDIR": '".libs"', - "M4": '"M4_environment_variable_must_be_set"', - "PACKAGE": '"flex"', - "PACKAGE_BUGREPORT": '"NA"', - "PACKAGE_NAME": '"the fast lexical analyser generator"', - "PACKAGE_STRING": '"the fast lexical analyser generator 2.6.4"', - "PACKAGE_TARNAME": '"flex"', - "PACKAGE_URL": '""', - "PACKAGE_VERSION": '"2.6.4"', - "VERSION": '"2.6.4"', - }, -) - -genlex( - name = "wc_l", - src = "examples/fastwc/wc4.l", - out = "wc.c", -) - -cc_binary( - name = "wc", - srcs = ["wc.c"], - copts = ["-w"], -) - -sh_test( - name = "wc_test", - srcs = ["@rules_hdl//dependency_support/com_github_westes_flex:wc_test.sh"], - args = ["$(location :wc)"], - data = [ - ":wc", - ], -) diff --git a/dependency_support/com_github_westes_flex/com_github_westes_flex.bzl b/dependency_support/com_github_westes_flex/com_github_westes_flex.bzl index 48c78f06..1db6d7bc 100644 --- a/dependency_support/com_github_westes_flex/com_github_westes_flex.bzl +++ b/dependency_support/com_github_westes_flex/com_github_westes_flex.bzl @@ -20,11 +20,7 @@ load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe") def com_github_westes_flex(): maybe( http_archive, - name = "com_github_westes_flex", - urls = [ - "https://github.com/westes/flex/releases/download/v2.6.4/flex-2.6.4.tar.gz", - ], - strip_prefix = "flex-2.6.4", - sha256 = "e87aae032bf07c26f85ac0ed3250998c37621d95f8bd748b31f15b33c45ee995", - build_file = Label("@rules_hdl//dependency_support:com_github_westes_flex/bundled.BUILD.bazel"), + name = "rules_flex", + sha256 = "8929fedc40909d19a4b42548d0785f796c7677dcef8b5d1600b415e5a4a7749f", + urls = ["https://github.com/jmillikin/rules_flex/releases/download/v0.2.1/rules_flex-v0.2.1.tar.xz"], ) diff --git a/dependency_support/com_github_westes_flex/flex.bzl b/dependency_support/com_github_westes_flex/flex.bzl index ea32fbee..cc5158cb 100644 --- a/dependency_support/com_github_westes_flex/flex.bzl +++ b/dependency_support/com_github_westes_flex/flex.bzl @@ -12,7 +12,84 @@ # See the License for the specific language governing permissions and # limitations under the License. -"""Build rule for generating C or C++ sources with Flex. +"""Build rule for generating C or C++ sources with Flex.""" + +def _correct_flex_env_for_action(env, flex): + """Modify the flex environment variables to work in an action that doesn't a have built flex runfiles directory. + + The `flex_toolchain.flex_env` parameter assumes that flex will provided via an executable attribute + and thus have built runfiles available to it. This is not the case for this action and any other actions + trying to use flex as a tool via the toolchain. This function transforms existing environment variables + to support running Flex as desired. + + Args: + env (dict): The existing Flex environment variables + flex (File): The Flex executable + + Returns: + Dict: Environment variables required for running Flex. + """ + flex_env = dict(env) + + # Convert the environment variables to non-runfiles forms + flex_runfiles_dir = "{}.runfiles/{}".format( + flex.path, + flex.owner.workspace_name, + ) + + actual = "{}/external/{}".format(flex.root.path, flex.owner.workspace_name) + + for key, value in flex_env.items(): + flex_env[key] = value.replace(flex_runfiles_dir, actual) + + return flex_env + +def _genlex_impl(ctx): + """Implementation for genlex rule.""" + + flex_toolchain = ctx.toolchains["@rules_flex//flex:toolchain_type"].flex_toolchain + + # Compute the prefix, if not specified. + if ctx.attr.prefix: + prefix = ctx.attr.prefix + else: + prefix = ctx.file.src.basename.partition(".")[0] + + # Construct the arguments. + args = ctx.actions.args() + args.add("-o", ctx.outputs.out) + outputs = [ctx.outputs.out] + if ctx.outputs.header_out: + args.add("--header-file=%s" % ctx.outputs.header_out.path) + outputs.append(ctx.outputs.header_out) + args.add("-P", prefix) + args.add_all(ctx.attr.lexopts) + args.add(ctx.file.src) + + flex_env = _correct_flex_env_for_action( + env = flex_toolchain.flex_env, + flex = flex_toolchain.flex_tool.executable, + ) + + ctx.actions.run( + executable = flex_toolchain.flex_tool.executable, + env = flex_env, + arguments = [args], + inputs = ctx.files.src + ctx.files.includes, + tools = [flex_toolchain.all_files], + outputs = outputs, + mnemonic = "Flex", + progress_message = "Generating %s from %s" % ( + ctx.outputs.out.short_path, + ctx.file.src.short_path, + ), + ) + +genlex = rule( + implementation = _genlex_impl, + doc = """\ +Generate C/C++-language sources from a lex file using Flex. + IMPORTANT: we _strongly recommend_ that you include a unique and project- specific `%option prefix="myproject"` directive in your scanner spec to avoid very hard-to-debug symbol name conflict problems if two scanners are linked @@ -33,18 +110,20 @@ Flex invokes m4 behind the scenes to generate the output scanner. As such, all genlex rules have an implicit dependency on `@org_gnu_m4//:m4`. Note also that certain M4 control sequences (notably exactly the strings `"[["` and `"]]"`) are not correctly handled by flex as a result. + Examples -------- This is a simple example. -``` +```python genlex( name = "html_lex_lex", src = "html.lex", out = "html_lexer.c", ) ``` + This example uses a `.tab.hh` file. -``` +```python genlex( name = "rules_l", src = "rules.lex", @@ -54,47 +133,7 @@ genlex( out = "rules.yy.cc", ) ``` -""" - -def _genlex_impl(ctx): - """Implementation for genlex rule.""" - - # Compute the prefix, if not specified. - if ctx.attr.prefix: - prefix = ctx.attr.prefix - else: - prefix = ctx.file.src.basename.partition(".")[0] - - # Construct the arguments. - args = ctx.actions.args() - args.add("-o", ctx.outputs.out) - outputs = [ctx.outputs.out] - if ctx.outputs.header_out: - args.add("--header-file=%s" % ctx.outputs.header_out.path) - outputs.append(ctx.outputs.header_out) - args.add("-P", prefix) - args.add_all(ctx.attr.lexopts) - args.add(ctx.file.src) - - ctx.actions.run( - executable = ctx.executable._flex, - env = { - "M4": ctx.executable._m4.path, - }, - arguments = [args], - inputs = ctx.files.src + ctx.files.includes, - tools = [ctx.executable._m4], - outputs = outputs, - mnemonic = "Flex", - progress_message = "Generating %s from %s" % ( - ctx.outputs.out.short_path, - ctx.file.src.short_path, - ), - ) - -genlex = rule( - implementation = _genlex_impl, - doc = "Generate C/C++-language sources from a lex file using Flex.", +""", attrs = { "header_out": attr.output( mandatory = False, @@ -107,7 +146,10 @@ genlex = rule( "lexopts": attr.string_list( doc = "A list of options to be added to the flex command line.", ), - "out": attr.output(mandatory = True, doc = "The generated source file"), + "out": attr.output( + doc = "The generated source file", + mandatory = True, + ), "prefix": attr.string( doc = "External symbol prefix for Flex. This string is " + "passed to flex as the -P option, causing the resulting C " + @@ -121,16 +163,9 @@ genlex = rule( allow_single_file = [".l", ".ll", ".lex", ".lpp"], doc = "The .lex source file for this rule", ), - "_flex": attr.label( - default = "@com_github_westes_flex//:flex", - executable = True, - cfg = "exec", - ), - "_m4": attr.label( - default = "@org_gnu_m4//:m4", - executable = True, - cfg = "exec", - ), }, + toolchains = [ + "@rules_flex//flex:toolchain_type", + ], output_to_genfiles = True, ) diff --git a/dependency_support/com_github_westes_flex/wc_test.sh b/dependency_support/com_github_westes_flex/wc_test.sh deleted file mode 100755 index c321b6eb..00000000 --- a/dependency_support/com_github_westes_flex/wc_test.sh +++ /dev/null @@ -1,23 +0,0 @@ -#!/bin/bash -# Copyright 2020 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -RESULT=$(echo "ab cde f" | $1) - -if [ "$RESULT" == " 1 3 9" ]; then - echo "Success" -else - >&2 echo "Encountered unexpected result $RESULT" - exit 1 -fi diff --git a/dependency_support/dependency_support.bzl b/dependency_support/dependency_support.bzl index 6dd82360..1cabd592 100644 --- a/dependency_support/dependency_support.bzl +++ b/dependency_support/dependency_support.bzl @@ -48,7 +48,6 @@ load("@rules_hdl//dependency_support/org_gnu_bison:org_gnu_bison.bzl", "org_gnu_ load("@rules_hdl//dependency_support/org_gnu_glpk:org_gnu_glpk.bzl", "org_gnu_glpk") load("@rules_hdl//dependency_support/org_gnu_gnulib:org_gnu_gnulib.bzl", "org_gnu_gnulib") load("@rules_hdl//dependency_support/org_gnu_gperf:org_gnu_gperf.bzl", "org_gnu_gperf") -load("@rules_hdl//dependency_support/org_gnu_m4:org_gnu_m4.bzl", "org_gnu_m4") load("@rules_hdl//dependency_support/org_gnu_readline:org_gnu_readline.bzl", "org_gnu_readline") load("@rules_hdl//dependency_support/org_llvm_openmp:org_llvm_openmp.bzl", "org_llvm_openmp") load("@rules_hdl//dependency_support/org_nixos_patchelf:org_nixos_patchelf.bzl", "org_nixos_patchelf") @@ -102,7 +101,6 @@ def dependency_support(register_toolchains = True): org_gnu_glpk() org_gnu_gnulib() org_gnu_gperf() - org_gnu_m4() org_gnu_readline() org_llvm_openmp() org_nixos_patchelf() diff --git a/dependency_support/net_zlib/BUILD b/dependency_support/net_zlib/BUILD.bazel similarity index 100% rename from dependency_support/net_zlib/BUILD rename to dependency_support/net_zlib/BUILD.bazel diff --git a/dependency_support/net_zlib/bundled.BUILD.bazel b/dependency_support/net_zlib/bundled.BUILD.bazel index a35102e1..b0a857f7 100755 --- a/dependency_support/net_zlib/bundled.BUILD.bazel +++ b/dependency_support/net_zlib/bundled.BUILD.bazel @@ -12,7 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -package(default_visibility = ["//visibility:public"]) +load("@bazel_skylib//rules:copy_file.bzl", "copy_file") +load("@rules_cc//cc:defs.bzl", "cc_library") licenses(["notice"]) # BSD/MIT-like license (for zlib) @@ -30,18 +31,23 @@ _ZLIB_HEADERS = [ "zutil.h", ] -_ZLIB_PREFIXED_HEADERS = ["zlib/include/" + hdr for hdr in _ZLIB_HEADERS] - # In order to limit the damage from the `includes` propagation # via `:zlib`, copy the public headers to a subdirectory and # expose those. -genrule( - name = "copy_public_headers", - srcs = _ZLIB_HEADERS, - outs = _ZLIB_PREFIXED_HEADERS, - cmd = "cp $(SRCS) $(@D)/zlib/include/", - visibility = ["//visibility:private"], -) +_ZLIB_HEADER_PREFIX = "zlib/include" + +_ZLIB_PREFIXED_HEADERS = { + hdr: "{}/{}".format(_ZLIB_HEADER_PREFIX, hdr) for hdr in _ZLIB_HEADERS +} + +[ + copy_file( + name = "{}.copy".format(hdr), + src = hdr, + out = dest, + ) + for hdr, dest in _ZLIB_PREFIXED_HEADERS.items() +] cc_library( name = "zlib", @@ -65,10 +71,11 @@ cc_library( # around the fact that zlib isn't consistent in its # choice of <> or "" delimiter when including itself. ] + _ZLIB_HEADERS, - hdrs = _ZLIB_PREFIXED_HEADERS, + hdrs = _ZLIB_PREFIXED_HEADERS.values(), copts = [ "-Wno-unused-variable", "-Wno-implicit-function-declaration", ], includes = ["zlib/include/"], + visibility = ["//visibility:public"], ) diff --git a/dependency_support/net_zlib/zlib/BUILD b/dependency_support/net_zlib/zlib/BUILD deleted file mode 100644 index baebefe2..00000000 --- a/dependency_support/net_zlib/zlib/BUILD +++ /dev/null @@ -1,18 +0,0 @@ -# Copyright 2020 The XLS Authors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -package( - default_applicable_licenses = ["//:package_license"], - default_visibility = ["//visibility:private"], -) diff --git a/dependency_support/net_zlib/zlib/bundled.BUILD.bazel b/dependency_support/net_zlib/zlib/bundled.BUILD.bazel deleted file mode 100755 index cb849cde..00000000 --- a/dependency_support/net_zlib/zlib/bundled.BUILD.bazel +++ /dev/null @@ -1,74 +0,0 @@ -# Copyright 2020 The XLS Authors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -package(default_visibility = ["//visibility:public"]) - -licenses(["notice"]) # BSD/MIT-like license (for zlib) - -_ZLIB_HEADERS = [ - "crc32.h", - "deflate.h", - "gzguts.h", - "inffast.h", - "inffixed.h", - "inflate.h", - "inftrees.h", - "trees.h", - "zconf.h", - "zlib.h", - "zutil.h", -] - -_ZLIB_PREFIXED_HEADERS = ["zlib/include/" + hdr for hdr in _ZLIB_HEADERS] - -# In order to limit the damage from the `includes` propagation -# via `:zlib`, copy the public headers to a subdirectory and -# expose those. -genrule( - name = "copy_public_headers", - srcs = _ZLIB_HEADERS, - outs = _ZLIB_PREFIXED_HEADERS, - cmd = "cp $(SRCS) $(@D)/zlib/include/", - visibility = ["//visibility:private"], -) - -cc_library( - name = "zlib", - srcs = [ - "adler32.c", - "compress.c", - "crc32.c", - "deflate.c", - "gzclose.c", - "gzlib.c", - "gzread.c", - "gzwrite.c", - "infback.c", - "inffast.c", - "inflate.c", - "inftrees.c", - "trees.c", - "uncompr.c", - "zutil.c", - # Include the un-prefixed headers in srcs to work - # around the fact that zlib isn't consistent in its - # choice of <> or "" delimiter when including itself. - ] + _ZLIB_HEADERS, - hdrs = _ZLIB_PREFIXED_HEADERS, - copts = [ - "-Wno-unused-variable", - "-Wno-implicit-function-declaration", - ], - includes = ["zlib/include/"], -) diff --git a/dependency_support/org_gnu_bison/BUILD b/dependency_support/org_gnu_bison/BUILD deleted file mode 100644 index e40fd381..00000000 --- a/dependency_support/org_gnu_bison/BUILD +++ /dev/null @@ -1,24 +0,0 @@ -# Copyright 2020 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -package( - default_applicable_licenses = ["//:package_license"], - default_visibility = ["//visibility:private"], -) - -licenses(["restricted"]) - -exports_files([ - "calc_test.sh", -]) diff --git a/dependency_support/org_gnu_m4/BUILD b/dependency_support/org_gnu_bison/BUILD.bazel similarity index 100% rename from dependency_support/org_gnu_m4/BUILD rename to dependency_support/org_gnu_bison/BUILD.bazel diff --git a/dependency_support/org_gnu_bison/bison.bzl b/dependency_support/org_gnu_bison/bison.bzl index ffdbd31e..4d3fb008 100644 --- a/dependency_support/org_gnu_bison/bison.bzl +++ b/dependency_support/org_gnu_bison/bison.bzl @@ -15,31 +15,41 @@ """Build rule for generating C or C++ sources with Bison. """ -def locate_bison_data_root(files): - """Locate the GNU Bison runtime data directory based on a set of inputs +def correct_bison_env_for_action(env, bison): + """Modify the Bison environment variables to work in an action that doesn't a have built bison runfiles directory. - This function assumes all files are located within the same directory. + The `bison_toolchain.bison_env` parameter assumes that Bison will provided via an executable attribute + and thus have built runfiles available to it. This is not the case for this action and any other actions + trying to use bison as a tool via the toolchain. This function transforms existing environment variables + to support running Bison as desired. Args: - files (list[File]): A list of files to use for locating Bison runtime data. + env (dict): The existing bison environment variables + bison (File): The Bison executable Returns: - str: The execpath of the data directory. + Dict: Environment variables required for running Bison. """ - if not files: - fail("No bison data files provided") + bison_env = dict(env) - file = files[0] + # Convert the environment variables to non-runfiles forms + bison_runfiles_dir = "{}.runfiles/{}".format( + bison.path, + bison.owner.workspace_name, + ) + + actual = "external/{}".format(bison.owner.workspace_name) - parent, _, _ = file.path.partition("/data/") - if parent == file.path: - fail("Unable to locate data directory from: {}", file.owner) + for key, value in bison_env.items(): + bison_env[key] = value.replace(bison_runfiles_dir, actual) - return "{}/data".format(parent) + return bison_env def _genyacc_impl(ctx): """Implementation for genyacc rule.""" + bison_toolchain = ctx.toolchains["@rules_bison//bison:toolchain_type"].bison_toolchain + # Argument list args = ctx.actions.args() args.add("--defines=%s" % ctx.outputs.header_out.path) @@ -56,14 +66,14 @@ def _genyacc_impl(ctx): ] ctx.actions.run( - executable = ctx.executable._bison, - env = { - "BISON_PKGDATADIR": locate_bison_data_root(ctx.files._bison_data), - "M4": ctx.executable._m4.path, - }, + executable = bison_toolchain.bison_tool.executable, + env = correct_bison_env_for_action( + env = bison_toolchain.bison_env, + bison = bison_toolchain.bison_tool.executable, + ), arguments = [args], inputs = ctx.files._bison_data + ctx.files.src, - tools = [ctx.executable._m4], + tools = [bison_toolchain.all_files], outputs = outputs, mnemonic = "Yacc", progress_message = "Generating %s and %s from %s" % @@ -104,20 +114,9 @@ genyacc = rule( allow_single_file = [".y", ".yy", ".yc", ".ypp", ".yxx"], doc = "The .y, .yy, or .yc source file for this rule", ), - "_bison": attr.label( - default = Label("@org_gnu_bison//:bison"), - executable = True, - cfg = "exec", - ), - "_bison_data": attr.label( - default = Label("@org_gnu_bison//:bison_runtime_data"), - allow_files = True, - ), - "_m4": attr.label( - default = Label("@org_gnu_m4//:m4"), - executable = True, - cfg = "exec", - ), }, + toolchains = [ + "@rules_bison//bison:toolchain_type", + ], output_to_genfiles = True, ) diff --git a/dependency_support/org_gnu_bison/bundled.BUILD.bazel b/dependency_support/org_gnu_bison/bundled.BUILD.bazel deleted file mode 100644 index 67795f51..00000000 --- a/dependency_support/org_gnu_bison/bundled.BUILD.bazel +++ /dev/null @@ -1,197 +0,0 @@ -# Copyright 2020 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -""" bison """ - -load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library") -load("@rules_hdl//dependency_support:copy.bzl", "copy", "touch") -load("@rules_hdl//dependency_support/org_gnu_bison:bison.bzl", "genyacc") - -package(default_visibility = ["//visibility:private"]) - -licenses(["restricted"]) - -exports_files(["LICENSE"]) - -PKG_DATA_DIR = "./data" - -LOCALE_DIR = "/dev/null" - -# In order to use bison properly in a shell command, set the env variable -# BISON_PKGDATADIR=[workspace dir]/bison/data in any shell command you invoke -# bison in. If you use the genyacc rule, Bazel should do this for for you. -# You will also need to set M4 to the location of m4, which is -# generally $(location @org_gnu_m4//:m4) in a Bazel genrule. - -filegroup( - name = "bison_runtime_data", - srcs = glob(["data/**/*"]), - output_licenses = ["unencumbered"], - path = "data", - visibility = ["//visibility:public"], -) - -alias( - name = "data", - actual = ":bison_runtime_data", - visibility = ["//visibility:public"], -) - -cc_library( - name = "bison_src_headers", - textual_hdrs = glob(["src/*.h"]) + [ - "src/scan-skel.c", - "src/scan-gram.c", - "src/scan-code.c", - ], -) - -# Bison ONLY (non-GNUlib) stuff -copy( - name = "get-errno_c", - src = "lib/get-errno.c", - out = "bison-lib/get-errno.c", -) - -copy( - name = "get-errno_h", - src = "lib/get-errno.h", - out = "bison-lib/get-errno.h", -) - -copy( - name = "path-join_c", - src = "lib/path-join.c", - out = "bison-lib/path-join.c", -) - -copy( - name = "path-join_h", - src = "lib/path-join.h", - out = "bison-lib/path-join.h", -) - -touch( - name = "configmake_h", - out = "bison-lib/configmake.h", - contents = dict( - LIBDIR = '"/usr/local/lib"', - ), -) - -touch( - name = "bazel_version_info", - out = "bison-lib/bazel_version_info.h", - contents = dict( - PACKAGE_VERSION = '"3.5"', - VERSION = '"3.5"', - ) -) - -# Note: This target is used by the Bazel "genyacc" rule, as well as by the -# explictly-declared dependencies. -cc_binary( - name = "bison", - srcs = [ - "bison-lib/configmake.h", - "bison-lib/get-errno.c", - "bison-lib/get-errno.h", - "bison-lib/path-join.c", - "bison-lib/path-join.h", - "bison-lib/bazel_version_info.h", - "src/AnnotationList.c", - "src/InadequacyList.c", - "src/Sbitset.c", - "src/assoc.c", - "src/closure.c", - "src/complain.c", - "src/conflicts.c", - "src/derives.c", - "src/files.c", - "src/fixits.c", - "src/getargs.c", - "src/gram.c", - "src/graphviz.c", - "src/ielr.c", - "src/lalr.c", - "src/location.c", - "src/lr0.c", - "src/main.c", - "src/muscle-tab.c", - "src/named-ref.c", - "src/nullable.c", - "src/output.c", - "src/parse-gram.c", - "src/print.c", - "src/print-graph.c", - "src/print-xml.c", - "src/reader.c", - "src/reduce.c", - "src/relation.c", - "src/scan-code-c.c", - "src/scan-gram-c.c", - "src/scan-skel-c.c", - "src/state.c", - "src/symlist.c", - "src/symtab.c", - "src/tables.c", - "src/uniqstr.c", - ], - includes = [ - ".", - "bison-lib", - "lib", - ], - copts = [ - "-Wno-vla", - "-DHAVE_CONFIG_H", - "-DPKGDATADIR='\"" + PKG_DATA_DIR + "\"'", - "-DLOCALEDIR='\"" + LOCALE_DIR + "\"'", - "-DM4='\"m4\"'", - "-DM4_GNU_OPTION='\"--gnu\"'", - # This is defined in gnulib's stdio.h, but we don't need anything else - # from that file. - "-D_GL_ATTRIBUTE_FORMAT_PRINTF(A,B)=", - "-DBAZEL_VERSION_INFO", - ], - data = [":bison_runtime_data"], - output_licenses = ["unencumbered"], - visibility = ["//visibility:public"], - deps = [ - ":bison_src_headers", - "@org_gnu_gnulib//:gnulib", - ], -) - -# Compile one of the examples and use that as a basic smoke test. -genyacc( - name = "calc_y", - src = "examples/c/calc/calc.y", - header_out = "calc.h", - source_out = "calc.cc", -) - -cc_binary( - name = "calc", - srcs = ["calc.cc", "calc.h"], -) - -sh_test( - name = "calc_test", - srcs = ["@rules_hdl//dependency_support/org_gnu_bison:calc_test.sh"], - args = ["$(location :calc)"], - data = [ - ":calc", - ], -) diff --git a/dependency_support/org_gnu_bison/calc_test.sh b/dependency_support/org_gnu_bison/calc_test.sh deleted file mode 100755 index c56901b3..00000000 --- a/dependency_support/org_gnu_bison/calc_test.sh +++ /dev/null @@ -1,25 +0,0 @@ - -#!/bin/bash -# Copyright 2020 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - - -RESULT=$(echo "21*2" | $1) - -if [ "$RESULT" == "42" ]; then - echo "Success" -else - >&2 echo "Encountered unexpected result $RESULT" - exit 1 -fi diff --git a/dependency_support/org_gnu_bison/org_gnu_bison.bzl b/dependency_support/org_gnu_bison/org_gnu_bison.bzl index 76c0beb0..4722e24f 100644 --- a/dependency_support/org_gnu_bison/org_gnu_bison.bzl +++ b/dependency_support/org_gnu_bison/org_gnu_bison.bzl @@ -20,12 +20,14 @@ load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe") def org_gnu_bison(): maybe( http_archive, - name = "org_gnu_bison", - urls = [ - "http://ftp.acc.umu.se/mirror/gnu.org/gnu/bison/bison-3.5.tar.xz", - "http://ftp.gnu.org/gnu/bison/bison-3.5.tar.xz", - ], - strip_prefix = "bison-3.5", - sha256 = "55e4a023b1b4ad19095a5f8279f0dc048fa29f970759cea83224a6d5e7a3a641", - build_file = Label("@rules_hdl//dependency_support:org_gnu_bison/bundled.BUILD.bazel"), + name = "rules_m4", + sha256 = "10ce41f150ccfbfddc9d2394ee680eb984dc8a3dfea613afd013cfb22ea7445c", + urls = ["https://github.com/jmillikin/rules_m4/releases/download/v0.2.3/rules_m4-v0.2.3.tar.xz"], + ) + + maybe( + http_archive, + name = "rules_bison", + sha256 = "2279183430e438b2dc77cacd7b1dbb63438971b2411406570f1ddd920b7c9145", + urls = ["https://github.com/jmillikin/rules_bison/releases/download/v0.2.2/rules_bison-v0.2.2.tar.xz"], ) diff --git a/dependency_support/org_gnu_m4/bundled.BUILD.bazel b/dependency_support/org_gnu_m4/bundled.BUILD.bazel deleted file mode 100644 index 0c3ce42e..00000000 --- a/dependency_support/org_gnu_m4/bundled.BUILD.bazel +++ /dev/null @@ -1,59 +0,0 @@ -# Copyright 2020 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -""" A BUILD file for m4 based on FSF stock source. """ - -load("@rules_cc//cc:defs.bzl", "cc_binary") -load("@rules_hdl//dependency_support:copy.bzl", "touch") - - -package( - default_visibility = ["//visibility:public"], - features = [ - "-parse_headers", - "no_layering_check", - ], -) - -licenses(["restricted"]) # GPLv3 - -exports_files(["COPYING"]) - -touch( - name = "bazel_version_info", - out = "src/bazel_version_info.h", - contents = dict( - PACKAGE_VERSION = '"1.4.18"', - VERSION = '"1.4.18"', - ) -) - - -cc_binary( - name = "m4", - srcs = glob(["src/*.c", "src/*.h"]) + [ - "src/bazel_version_info.h" - ], - deps = [ - "@org_gnu_gnulib//:gnulib", - ], - includes = [ - "src/", - ], - output_licenses = ["unencumbered"], - copts = [ - "-w", - "-DBAZEL_VERSION_INFO", - ], -) diff --git a/dependency_support/org_gnu_m4/org_gnu_m4.bzl b/dependency_support/org_gnu_m4/org_gnu_m4.bzl deleted file mode 100644 index ea0dd275..00000000 --- a/dependency_support/org_gnu_m4/org_gnu_m4.bzl +++ /dev/null @@ -1,31 +0,0 @@ -# Copyright 2020 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Loads the m4 macro processor, used by Bison.""" - -load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") -load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe") - -def org_gnu_m4(): - maybe( - http_archive, - name = "org_gnu_m4", - urls = [ - "http://ftp.acc.umu.se/mirror/gnu.org/gnu/m4/m4-1.4.18.tar.xz", - "http://ftp.gnu.org/gnu/m4/m4-1.4.18.tar.xz", - ], - strip_prefix = "m4-1.4.18", - sha256 = "f2c1e86ca0a404ff281631bdc8377638992744b175afb806e25871a24a934e07", - build_file = Label("@rules_hdl//dependency_support:org_gnu_m4/bundled.BUILD.bazel"), - ) diff --git a/dependency_support/verilator/private/verilator_utils.bzl b/dependency_support/verilator/private/verilator_utils.bzl index 9c7149ee..f848c8d4 100644 --- a/dependency_support/verilator/private/verilator_utils.bzl +++ b/dependency_support/verilator/private/verilator_utils.bzl @@ -1,6 +1,6 @@ """Utility rules used to compile Verilator""" -load("//dependency_support/org_gnu_bison:bison.bzl", "locate_bison_data_root") +load("//dependency_support/org_gnu_bison:bison.bzl", "correct_bison_env_for_action") def _verilator_astgen_impl(ctx): args = ctx.actions.args() @@ -52,78 +52,67 @@ verilator_astgen = rule( }, ) -def _is_expandable(value): - for prefix in ("$(execpath", "$(location"): - if prefix in value: - return True - - return False - def _verilator_bisonpre_impl(ctx): - data = [ctx.attr.bisonpre] + ctx.attr.srcs + ctx.attr.tools + bison_toolchain = ctx.toolchains["@rules_bison//bison:toolchain_type"].bison_toolchain + args = ctx.actions.args() args.add(ctx.file.bisonpre) - args.add_all([ - ctx.expand_location(a, data) if _is_expandable(a) else a - for a in ctx.attr.args - ]) + args.add("--yacc", bison_toolchain.bison_tool.executable) + args.add("-d") + args.add("-v") + args.add("-o", ctx.outputs.out_src) + args.add(ctx.file.yacc_src) + + outputs = [ + ctx.outputs.out_src, + ctx.outputs.out_hdr, + ] + + tools = depset([ctx.file.bisonpre], transitive = [bison_toolchain.all_files]) + + bison_env = correct_bison_env_for_action( + env = bison_toolchain.bison_env, + bison = bison_toolchain.bison_tool.executable, + ) - envs = { - "BISON_PKGDATADIR": locate_bison_data_root(ctx.files.bison_data), - } - envs.update({ - # Expand $(location) / $(locations) in the values. - k: ctx.expand_location(v, data) if _is_expandable(v) else v - for k, v in ctx.attr.env.items() - }) ctx.actions.run( - outputs = ctx.outputs.outs, - inputs = ctx.files.srcs, - tools = [ctx.file.bisonpre] + ctx.files.tools + ctx.files.bison_data, + outputs = outputs, + inputs = [ctx.file.yacc_src], + tools = tools, executable = ctx.executable._process_wrapper, arguments = [args], mnemonic = "VerilatorBisonPre", use_default_shell_env = False, - env = envs, + env = bison_env, ) + return DefaultInfo( - files = depset(ctx.outputs.outs), - runfiles = ctx.runfiles(files = ctx.outputs.outs), + files = depset(outputs), + runfiles = ctx.runfiles(files = outputs), ) verilator_bisonpre = rule( doc = "Run Verilator's `bisonpre` tool and collect the requested outputs.", implementation = _verilator_bisonpre_impl, attrs = { - "args": attr.string_list( - doc = "Command line arguments of the `bisonpre`", - ), - "bison_data": attr.label( - doc = "Runtime data for GNU Bison.", - allow_files = True, - mandatory = True, - ), "bisonpre": attr.label( doc = "The path to the `bisonpre` tool.", allow_single_file = True, mandatory = True, cfg = "exec", ), - "env": attr.string_dict( - doc = "Environment variables of the action.", - ), - "outs": attr.output_list( + "out_hdr": attr.output( mandatory = True, doc = "Output files generated by the action.", ), - "srcs": attr.label_list( - allow_files = True, - doc = "Additional inputs of the action.", + "out_src": attr.output( + mandatory = True, + doc = "Output files generated by the action.", ), - "tools": attr.label_list( - allow_files = True, - cfg = "exec", - doc = "Additional tools of the action.", + "yacc_src": attr.label( + doc = "The yacc file to run on.", + allow_single_file = True, + mandatory = True, ), "_process_wrapper": attr.label( executable = True, @@ -131,6 +120,10 @@ verilator_bisonpre = rule( default = Label("//dependency_support/verilator/private:verilator_bisonpre"), ), }, + toolchains = [ + "@rules_bison//bison:toolchain_type", + "@rules_m4//m4:toolchain_type", + ], ) def _verilator_flexfix_impl(ctx): diff --git a/dependency_support/verilator/verilator.BUILD.bazel b/dependency_support/verilator/verilator.BUILD.bazel index f9584909..c048fd9a 100644 --- a/dependency_support/verilator/verilator.BUILD.bazel +++ b/dependency_support/verilator/verilator.BUILD.bazel @@ -184,31 +184,10 @@ verilator_flexfix( verilator_bisonpre( name = "verilator_bison", - srcs = [ - "src/verilog.y", - ], - outs = [ - "V3ParseBison.c", - "V3ParseBison.h", - ], - args = [ - "--yacc", - "$(execpath @org_gnu_bison//:bison)", - "-d", - "-v", - "-o", - "$(execpath V3ParseBison.c)", - "$(execpath src/verilog.y)", - ], + yacc_src = "src/verilog.y", + out_src = "V3ParseBison.c", + out_hdr = "V3ParseBison.h", bisonpre = "src/bisonpre", - bison_data = "@org_gnu_bison//:bison_runtime_data", - env = { - "M4": "$(execpath @org_gnu_m4//:m4)", - }, - tools = [ - "@org_gnu_bison//:bison", - "@org_gnu_m4//:m4", - ], ) cc_library( @@ -272,7 +251,7 @@ cc_library( ], deps = [ ":verilatedos", - "@com_github_westes_flex//:FlexLexer", + # "@com_github_westes_flex//:FlexLexer", ], ) diff --git a/init.bzl b/init.bzl index b56c2d08..6a811821 100644 --- a/init.bzl +++ b/init.bzl @@ -16,6 +16,9 @@ load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace") load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps") +load("@rules_bison//bison:bison.bzl", "bison_register_toolchains") +load("@rules_flex//flex:flex.bzl", "flex_register_toolchains") +load("@rules_m4//m4:m4.bzl", "m4_register_toolchains") load( "@rules_proto//proto:repositories.bzl", "rules_proto_dependencies", @@ -62,3 +65,7 @@ def init(python_interpreter = None, python_interpreter_target = None): protobuf_deps() bazel_skylib_workspace() + + m4_register_toolchains(version = "1.4.18") + bison_register_toolchains(version = "3.3.2") + flex_register_toolchains(version = "2.6.4") diff --git a/tools/test_everything.py b/tools/test_everything.py index 3f06d35e..c123e139 100755 --- a/tools/test_everything.py +++ b/tools/test_everything.py @@ -50,10 +50,8 @@ '@net_sourceforge_ngspice//...', '@net_zlib//...', '@org_fftw//...', - '@org_gnu_bison//...', '@org_gnu_glpk//...', '@org_gnu_gperf//...', - '@org_gnu_m4//...', '@org_gnu_readline//...', '@org_llvm_openmp//...', '@org_pcre_ftp//...', @@ -65,7 +63,6 @@ '@org_theopenroadproject_asap7sc6t_26//...', '@org_theopenroadproject_asap7sc7p5t_27//...', '@org_theopenroadproject_asap7sc7p5t_28//...', - '@rules_pkg//...', '@tk_tcl//...', ]