| Hendrik Hofstadt | 4b0e5c0 | 2019-11-07 20:21:24 +0100 | [diff] [blame] | 1 | # Copyright 2020 The Monogon Project Authors. |
| 2 | # |
| 3 | # SPDX-License-Identifier: Apache-2.0 |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| Hendrik Hofstadt | 4b0e5c0 | 2019-11-07 20:21:24 +0100 | [diff] [blame] | 17 | load( |
| Hendrik Hofstadt | 4b0e5c0 | 2019-11-07 20:21:24 +0100 | [diff] [blame] | 18 | "@io_bazel_rules_go//go:def.bzl", |
| Hendrik Hofstadt | 4b0e5c0 | 2019-11-07 20:21:24 +0100 | [diff] [blame] | 19 | "go_context", |
| Hendrik Hofstadt | 4b0e5c0 | 2019-11-07 20:21:24 +0100 | [diff] [blame] | 20 | ) |
| 21 | |
| 22 | def _bindata_impl(ctx): |
| 23 | out = ctx.actions.declare_file("bindata.go") |
| 24 | |
| 25 | arguments = ctx.actions.args() |
| 26 | arguments.add_all([ |
| 27 | "-pkg", |
| 28 | ctx.attr.package, |
| 29 | "-prefix", |
| Lorenz Brun | f857487 | 2020-07-28 17:12:04 +0200 | [diff] [blame] | 30 | ctx.label.workspace_root, |
| Hendrik Hofstadt | 4b0e5c0 | 2019-11-07 20:21:24 +0100 | [diff] [blame] | 31 | "-o", |
| 32 | out, |
| 33 | ]) |
| 34 | arguments.add_all(ctx.files.srcs) |
| 35 | |
| 36 | ctx.actions.run( |
| 37 | inputs = ctx.files.srcs, |
| 38 | outputs = [out], |
| 39 | executable = ctx.file.bindata, |
| 40 | arguments = [arguments], |
| 41 | ) |
| 42 | |
| 43 | go = go_context(ctx) |
| 44 | |
| 45 | source_files = [out] |
| 46 | |
| 47 | library = go.new_library( |
| 48 | go, |
| 49 | srcs = source_files, |
| 50 | ) |
| 51 | source = go.library_to_source(go, None, library, False) |
| 52 | providers = [library, source] |
| 53 | output_groups = { |
| 54 | "go_generated_srcs": source_files, |
| 55 | } |
| 56 | |
| 57 | return providers + [OutputGroupInfo(**output_groups)] |
| 58 | |
| Serge Bazanski | f12bedf | 2021-01-15 16:58:50 +0100 | [diff] [blame] | 59 | bindata = rule( |
| 60 | implementation = _bindata_impl, |
| Hendrik Hofstadt | 4b0e5c0 | 2019-11-07 20:21:24 +0100 | [diff] [blame] | 61 | attrs = { |
| Serge Bazanski | f12bedf | 2021-01-15 16:58:50 +0100 | [diff] [blame] | 62 | "srcs": attr.label_list( |
| 63 | mandatory = True, |
| 64 | allow_files = True, |
| Tim Windelschmidt | 156248b | 2025-01-10 00:27:45 +0100 | [diff] [blame^] | 65 | ), |
| Serge Bazanski | f12bedf | 2021-01-15 16:58:50 +0100 | [diff] [blame] | 66 | "package": attr.string( |
| 67 | mandatory = True, |
| Tim Windelschmidt | 156248b | 2025-01-10 00:27:45 +0100 | [diff] [blame^] | 68 | ), |
| Serge Bazanski | f12bedf | 2021-01-15 16:58:50 +0100 | [diff] [blame] | 69 | "bindata": attr.label( |
| 70 | allow_single_file = True, |
| 71 | default = Label("@com_github_kevinburke_go_bindata//go-bindata"), |
| 72 | ), |
| 73 | "_go_context_data": attr.label( |
| 74 | default = "@io_bazel_rules_go//:go_context_data", |
| 75 | ), |
| Hendrik Hofstadt | 4b0e5c0 | 2019-11-07 20:21:24 +0100 | [diff] [blame] | 76 | }, |
| Serge Bazanski | f12bedf | 2021-01-15 16:58:50 +0100 | [diff] [blame] | 77 | toolchains = ["@io_bazel_rules_go//go:toolchain"], |
| Hendrik Hofstadt | 4b0e5c0 | 2019-11-07 20:21:24 +0100 | [diff] [blame] | 78 | ) |