1# Copyright 2020 Google LLC 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# https://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"""A trivial rule to turn a string into a C++ constant.""" 15 16def _constant_gen_impl(ctx): 17 # Turn text into a C++ constant. 18 outputs = [ctx.outputs.src_out] 19 ctx.actions.run( 20 mnemonic = "GenerateConstant", 21 progress_message = "Generating %s" % ctx.attr.var, 22 outputs = outputs, 23 executable = ctx.executable._generator, 24 arguments = [ctx.outputs.src_out.path, ctx.attr.var, ctx.attr.text], 25 ) 26 return [DefaultInfo(files = depset(outputs))] 27 28_constant_gen = rule( 29 implementation = _constant_gen_impl, 30 attrs = { 31 "src_out": attr.output(mandatory = True), 32 "text": attr.string(mandatory = True), 33 "var": attr.string(mandatory = False), 34 "_generator": attr.label( 35 default = Label("@rules_license//examples/vndor/constant_gen:constant_generator"), 36 executable = True, 37 allow_files = True, 38 cfg = "exec", 39 ), 40 }, 41) 42 43def constant_gen(name, text, var): 44 # Generate the code 45 _constant_gen( 46 name = name + "_src_", 47 src_out = name + "_src_.cc", 48 text = text, 49 var = var, 50 applicable_licenses = ["@rules_license//examples/vndor/constant_gen:license_for_emitted_code"], 51 ) 52 53 # And turn it into a library we can link against 54 native.cc_library( 55 name = name, 56 srcs = [name + "_src_"], 57 applicable_licenses = ["@rules_license//examples/vndor/constant_gen:license_for_emitted_code"], 58 ) 59