Support configuration variables in py_wheel "version" attirbute. (#640)

diff --git a/docs/packaging.md b/docs/packaging.md
index 0271750..d3595c4 100755
--- a/docs/packaging.md
+++ b/docs/packaging.md
@@ -103,7 +103,7 @@
 | requires |  List of requirements for this package   | List of strings | optional | [] |
 | stamp |  Whether to encode build information into the wheel. Possible values:<br><br>- <code>stamp = 1</code>: Always stamp the build information into the wheel, even in [--nostamp](https://docs.bazel.build/versions/main/user-manual.html#flag--stamp) builds. This setting should be avoided, since it potentially kills remote caching for the target and any downstream actions that depend on it.<br><br>- <code>stamp = 0</code>: Always replace build information by constant values. This gives good build result caching.<br><br>- <code>stamp = -1</code>: Embedding of build information is controlled by the [--[no]stamp](https://docs.bazel.build/versions/main/user-manual.html#flag--stamp) flag.<br><br>Stamped targets are not rebuilt unless their dependencies change.   | Integer | optional | -1 |
 | strip_path_prefixes |  path prefixes to strip from files added to the generated package   | List of strings | optional | [] |
-| version |  Version number of the package. Note that this attribute supports stamp format strings. Eg <code>1.2.3-{BUILD_TIMESTAMP}</code>   | String | required |  |
+| version |  Version number of the package. Note that this attribute supports stamp format strings (eg. <code>1.2.3-{BUILD_TIMESTAMP}</code>) as well as 'make variables' (e.g. <code>1.2.3-$(VERSION)</code>).   | String | required |  |
 
 
 <a name="#PyWheelInfo"></a>
diff --git a/python/packaging.bzl b/python/packaging.bzl
index 5e79900..9ad2daf 100644
--- a/python/packaging.bzl
+++ b/python/packaging.bzl
@@ -114,10 +114,18 @@
             escaped += "_"
     return escaped
 
+def _replace_make_variables(flag, ctx):
+    """Replace $(VERSION) etc make variables in flag"""
+    if "$" in flag:
+        for varname, varsub in ctx.var.items():
+            flag = flag.replace("$(%s)" % varname, varsub)
+    return flag
+
 def _py_wheel_impl(ctx):
+    version = _replace_make_variables(ctx.attr.version, ctx)
     outfile = ctx.actions.declare_file("-".join([
         _escape_filename_segment(ctx.attr.distribution),
-        _escape_filename_segment(ctx.attr.version),
+        _escape_filename_segment(version),
         _escape_filename_segment(ctx.attr.python_tag),
         _escape_filename_segment(ctx.attr.abi),
         _escape_filename_segment(ctx.attr.platform),
@@ -143,7 +151,7 @@
 
     args = ctx.actions.args()
     args.add("--name", ctx.attr.distribution)
-    args.add("--version", ctx.attr.version)
+    args.add("--version", version)
     args.add("--python_tag", ctx.attr.python_tag)
     args.add("--python_requires", ctx.attr.python_requires)
     args.add("--abi", ctx.attr.abi)
@@ -298,7 +306,8 @@
         mandatory = True,
         doc = (
             "Version number of the package. Note that this attribute " +
-            "supports stamp format strings. Eg `1.2.3-{BUILD_TIMESTAMP}`"
+            "supports stamp format strings (eg. `1.2.3-{BUILD_TIMESTAMP}`) " +
+            "as well as 'make variables' (e.g. `1.2.3-$(VERSION)`)."
         ),
     ),
     "_stamp_flag": attr.label(