1# Copyright 2023 The Bazel Authors. All rights reserved. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15"""Helper rules for demonstrating `py_wheel` examples""" 16 17def _directory_writer_impl(ctx): 18 output = ctx.actions.declare_directory(ctx.attr.out) 19 20 args = ctx.actions.args() 21 args.add("--output", output.path) 22 23 for path, content in ctx.attr.files.items(): 24 args.add("--file={}={}".format( 25 path, 26 json.encode(content), 27 )) 28 29 ctx.actions.run( 30 outputs = [output], 31 arguments = [args], 32 executable = ctx.executable._writer, 33 ) 34 35 return [DefaultInfo( 36 files = depset([output]), 37 runfiles = ctx.runfiles(files = [output]), 38 )] 39 40directory_writer = rule( 41 implementation = _directory_writer_impl, 42 doc = "A rule for generating a directory with the requested content.", 43 attrs = { 44 "files": attr.string_dict( 45 doc = "A mapping of file name to content to create relative to the generated `out` directory.", 46 ), 47 "out": attr.string( 48 doc = "The name of the directory to create", 49 ), 50 "_writer": attr.label( 51 executable = True, 52 cfg = "exec", 53 default = Label("//examples/wheel/private:directory_writer"), 54 ), 55 }, 56) 57 58def _make_variable_tags_impl(ctx): # buildifier: disable=unused-variable 59 # This example is contrived. In a real usage, this rule would 60 # look at flags or dependencies to determine what values to use. 61 # If all you're doing is setting constant values, then you can simply 62 # set them in the py_wheel() call. 63 vars = {} 64 vars["ABI"] = "cp38" 65 vars["PYTHON_TAG"] = "cp38" 66 vars["VERSION"] = "0.99.0" 67 return [platform_common.TemplateVariableInfo(vars)] 68 69make_variable_tags = rule( 70 attrs = {}, 71 doc = """Make variable tags to pass to a py_wheel rule.""", 72 implementation = _make_variable_tags_impl, 73) 74