diff options
Diffstat (limited to 'cobol')
| -rw-r--r-- | cobol/BUILD | 0 | ||||
| -rw-r--r-- | cobol/def.bzl | 5 | ||||
| -rw-r--r-- | cobol/deps.bzl | 22 | ||||
| -rw-r--r-- | cobol/internal/BUILD | 0 | ||||
| -rw-r--r-- | cobol/internal/actions.bzl | 23 | ||||
| -rw-r--r-- | cobol/internal/rules.bzl | 30 |
6 files changed, 80 insertions, 0 deletions
diff --git a/cobol/BUILD b/cobol/BUILD new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/cobol/BUILD diff --git a/cobol/def.bzl b/cobol/def.bzl new file mode 100644 index 0000000..c78094b --- /dev/null +++ b/cobol/def.bzl @@ -0,0 +1,5 @@ +"""Public definitions for rules_cobol""" + +load("//cobol/internal:rules.bzl", _cobol_binary = "cobol_binary") + +cobol_binary = _cobol_binary diff --git a/cobol/deps.bzl b/cobol/deps.bzl new file mode 100644 index 0000000..baaf611 --- /dev/null +++ b/cobol/deps.bzl @@ -0,0 +1,22 @@ +"""Macro for declaring repository dependencies""" + +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") + +def cobol_rules_dependencies(): + """Declares the external repostories that rules_cobol depends on.""" + + _maybe( + http_archive, + name = "bazel_skylib", + urls = [ + "https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.0.2/bazel-skylib-1.0.2.tar.gz", + "https://github.com/bazelbuild/bazel-skylib/releases/download/1.0.2/bazel-skylib-1.0.2.tar.gz", + ], + sha256 = "97e70364e9249702246c0e9444bccdc4b847bed1eb03c5a3ece4f83dfe6abc44", + ) + +def _maybe(rule, name, **kwargs): + """Declares an external repository if it has not been declared already.""" + + if name not in native.existing_rules(): + rule(name = name, **kwargs) diff --git a/cobol/internal/BUILD b/cobol/internal/BUILD new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/cobol/internal/BUILD diff --git a/cobol/internal/actions.bzl b/cobol/internal/actions.bzl new file mode 100644 index 0000000..a57bd58 --- /dev/null +++ b/cobol/internal/actions.bzl @@ -0,0 +1,23 @@ +"""Common functions for creating actions to build COBOL programs""" + +load("@bazel_skylib//lib:shell.bzl", "shell") + +def cobol_compile_executable(ctx, srcs, out): + """Compiles a single COBOL program from sources + + Args: + ctx: the build context + srcs: the source files to compile + out: the output file + """ + + ctx.actions.run_shell( + outputs = [out], + inputs = srcs, + command = "cobc -x -o {out} {srcs}".format( + out = shell.quote(out.path), + srcs = " ".join([shell.quote(src.path) for src in srcs]), + ), + mnemonic = "COBOLCompile", + use_default_shell_env = True, + ) diff --git a/cobol/internal/rules.bzl b/cobol/internal/rules.bzl new file mode 100644 index 0000000..8dac747 --- /dev/null +++ b/cobol/internal/rules.bzl @@ -0,0 +1,30 @@ +"Rules for building COBOL programs" + +load(":actions.bzl", "cobol_compile_executable") + +def _cobol_binary_impl(ctx): + prefix = ctx.label.name + "%/" + executable = ctx.actions.declare_file(prefix + ctx.label.name) + + cobol_compile_executable( + ctx, + srcs = ctx.files.srcs, + out = executable, + ) + + return [DefaultInfo( + files = depset([executable]), + executable = executable, + )] + +cobol_binary = rule( + _cobol_binary_impl, + attrs = { + "srcs": attr.label_list( + allow_files = True, + doc = "The COBOL source files to compile for this binary", + ), + }, + doc = "Builds an executable program from COBOL source code", + executable = True, +) |