From 1022379b36b66607415c98c9b51e667a1597b296 Mon Sep 17 00:00:00 2001 From: Keith Smiley Date: Tue, 6 Jan 2026 13:55:33 -0800 Subject: [PATCH] Add package copts toolchain attribute and flags This allows passing extra options to 'mojo package'. These aren't mirrored from the other mojo flags since this subcommand doesn't accept the same flags, but there are some flags that might be useful to pass. --- BUILD.bazel | 6 ++++++ mojo/mojo_library.bzl | 14 ++++++++++++++ mojo/providers.bzl | 1 + mojo/toolchain.bzl | 5 +++++ 4 files changed, 26 insertions(+) diff --git a/BUILD.bazel b/BUILD.bazel index d28b285..b8d3268 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -6,6 +6,12 @@ repeatable_string_flag( visibility = ["//visibility:public"], ) +repeatable_string_flag( + name = "mojo_package_copt", + build_setting_default = [], + visibility = ["//visibility:public"], +) + toolchain_type( name = "toolchain_type", visibility = ["//visibility:public"], diff --git a/mojo/mojo_library.bzl b/mojo/mojo_library.bzl index 886b255..27a5c50 100644 --- a/mojo/mojo_library.bzl +++ b/mojo/mojo_library.bzl @@ -1,5 +1,6 @@ """Compile Mojo files into a mojopkg that can be consumed by other Mojo targets.""" +load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo") load("//mojo:providers.bzl", "MojoInfo") load("//mojo/private:utils.bzl", "MOJO_EXTENSIONS", "collect_mojoinfo") @@ -14,6 +15,10 @@ def _mojo_library_implementation(ctx): args.add("package") args.add("-strip-file-prefix=.") args.add("-o", mojo_package) + + args.add_all(mojo_toolchain.package_copts) + if "-exec-" not in ctx.bin_dir.path: + args.add_all(ctx.attr._mojo_package_copts[BuildSettingInfo].value) args.add_all([ ctx.expand_location(copt, targets = ctx.attr.additional_compiler_inputs) for copt in ctx.attr.copts @@ -78,6 +83,12 @@ then be used in copts with the $(location) function. doc = """\ Additional compiler options to pass to the Mojo compiler. +Order of options: +1. copts from mojo_toolchain.package_copts +2. copts from //:mojo_package_copt (if not building in exec config) +3. copts from this attribute, with $(location) expanded for files in + additional_compiler_inputs. + NOTE: copts from --mojocopt and mojo_toolchain.copts are not passed to 'mojo package' since it does not accept many flags. """, @@ -90,6 +101,9 @@ package' since it does not accept many flags. providers = [MojoInfo], ), "data": attr.label_list(), + "_mojo_package_copts": attr.label( + default = Label("//:mojo_package_copt"), + ), }, toolchains = ["//:toolchain_type"], ) diff --git a/mojo/providers.bzl b/mojo/providers.bzl index 275e1ec..af965ba 100644 --- a/mojo/providers.bzl +++ b/mojo/providers.bzl @@ -13,6 +13,7 @@ MojoToolchainInfo = provider( fields = { "all_tools": "All the files that must be available in actions in order for the toolchain to work.", "copts": "Additional compiler options to pass to the Mojo compiler.", + "package_copts": "Additional compiler options to pass to the Mojo compiler when running 'mojo package'.", "lld": "The lld compiler executable to link with", "mojo": "The mojo compiler executable to build with", "implicit_deps": "Implicit dependencies that every target should depend on, providing either CcInfo, or MojoInfo", diff --git a/mojo/toolchain.bzl b/mojo/toolchain.bzl index c7f7b1f..76fb91a 100644 --- a/mojo/toolchain.bzl +++ b/mojo/toolchain.bzl @@ -27,6 +27,7 @@ def _mojo_toolchain_impl(ctx): mojo_toolchain_info = MojoToolchainInfo( all_tools = tool_files, copts = copts, + package_copts = ctx.attr.package_copts, lld = ctx.executable.lld, mojo = ctx.executable.mojo, implicit_deps = ctx.attr.implicit_deps, @@ -41,6 +42,10 @@ mojo_toolchain = rule( mandatory = False, doc = "Additional compiler options to pass to the Mojo compiler.", ), + "package_copts": attr.string_list( + mandatory = False, + doc = "Additional compiler options to pass to the Mojo compiler when running 'mojo package'.", + ), "extra_tools": attr.label_list( providers = [DefaultInfo], allow_files = True,