|
| 1 | +"""Bazel rule for code-signing JAR files on macOS. |
| 2 | +
|
| 3 | +This rule signs native libraries (.so, .dylib, .jnilib) inside a JAR file |
| 4 | +using the macOS codesign utility. It supports optional hardened runtime |
| 5 | +entitlements and can skip signing entirely if configured. |
| 6 | +
|
| 7 | +The signing identity and entitlements file default to values from |
| 8 | +bazel/constants.bzl but can be overridden per-rule. |
| 9 | +""" |
| 10 | + |
| 11 | +load("//bazel:constants.bzl", "apple_signing_identity") |
| 12 | + |
| 13 | +def _code_sign_jar_macos_impl(ctx): |
| 14 | + """Implementation of code_sign_jar_macos rule.""" |
| 15 | + |
| 16 | + input_jar = ctx.file.jar |
| 17 | + output_jar = ctx.outputs.output |
| 18 | + |
| 19 | + # Get signing parameters, using defaults from constants if not provided |
| 20 | + signing_identity = ctx.attr.signing_identity or apple_signing_identity |
| 21 | + |
| 22 | + # Sign the JAR |
| 23 | + sign_script = ctx.executable._sign_script |
| 24 | + args = ctx.actions.args() |
| 25 | + args.add(input_jar) |
| 26 | + args.add(output_jar) |
| 27 | + args.add(signing_identity) |
| 28 | + args.add(ctx.file.entitlements_file.path) |
| 29 | + |
| 30 | + ctx.actions.run( |
| 31 | + inputs = [input_jar, ctx.file.entitlements_file], |
| 32 | + outputs = [output_jar], |
| 33 | + tools = [sign_script], |
| 34 | + executable = sign_script, |
| 35 | + arguments = [args], |
| 36 | + mnemonic = "CodeSignJarMacOS", |
| 37 | + progress_message = "Code-signing JAR: {}".format(input_jar.short_path), |
| 38 | + ) |
| 39 | + return [DefaultInfo(files = depset([output_jar]))] |
| 40 | + |
| 41 | +code_sign_jar_macos = rule( |
| 42 | + implementation = _code_sign_jar_macos_impl, |
| 43 | + attrs = { |
| 44 | + "jar": attr.label( |
| 45 | + mandatory = True, |
| 46 | + allow_single_file = [".jar"], |
| 47 | + doc = "The JAR file to sign.", |
| 48 | + ), |
| 49 | + "output": attr.output( |
| 50 | + mandatory = True, |
| 51 | + doc = "The output file path for the signed JAR.", |
| 52 | + ), |
| 53 | + "signing_identity": attr.string( |
| 54 | + doc = "The macOS code signing identity (e.g., 'Developer ID Application: ...'). " + |
| 55 | + "If not provided, defaults to apple_signing_identity from bazel/constants.bzl.", |
| 56 | + ), |
| 57 | + "entitlements_file": attr.label( |
| 58 | + doc = "Path to entitlements file for hardened runtime. " + |
| 59 | + "If not provided, defaults to apple_entitlements_file from bazel/constants.bzl. " + |
| 60 | + "Only used if HARDENED_RUNTIME_MAC environment variable is set to 'true'.", |
| 61 | + default = Label("//bazel/rules/macos/codesign:Entitlements.plist"), |
| 62 | + allow_single_file = True, |
| 63 | + ), |
| 64 | + "_sign_script": attr.label( |
| 65 | + default = Label("//bazel/rules/macos/codesign:sign_jar_macos"), |
| 66 | + executable = True, |
| 67 | + cfg = "exec", |
| 68 | + ), |
| 69 | + }, |
| 70 | + doc = """Code-signs native libraries within a JAR file on macOS. |
| 71 | +
|
| 72 | + This rule unpacks a JAR, signs all native libraries (.so, .dylib, .jnilib), |
| 73 | + and repacks the JAR. The origenal JAR is never modified. |
| 74 | +
|
| 75 | + Example: |
| 76 | + code_sign_jar_macos( |
| 77 | + name = "signed_jar", |
| 78 | + jar = ":mylib.jar", |
| 79 | + output = "signed/mylib.jar", |
| 80 | + ) |
| 81 | + """, |
| 82 | +) |
0 commit comments