Skip to content

Commit 9844f8a

Browse files
authored
Add package copts toolchain attribute and flags (#70)
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.
1 parent 0e1a699 commit 9844f8a

4 files changed

Lines changed: 26 additions & 0 deletions

File tree

BUILD.bazel

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ repeatable_string_flag(
66
visibility = ["//visibility:public"],
77
)
88

9+
repeatable_string_flag(
10+
name = "mojo_package_copt",
11+
build_setting_default = [],
12+
visibility = ["//visibility:public"],
13+
)
14+
915
toolchain_type(
1016
name = "toolchain_type",
1117
visibility = ["//visibility:public"],

mojo/mojo_library.bzl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Compile Mojo files into a mojopkg that can be consumed by other Mojo targets."""
22

3+
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
34
load("//mojo:providers.bzl", "MojoInfo")
45
load("//mojo/private:utils.bzl", "MOJO_EXTENSIONS", "collect_mojoinfo")
56

@@ -14,6 +15,10 @@ def _mojo_library_implementation(ctx):
1415
args.add("package")
1516
args.add("-strip-file-prefix=.")
1617
args.add("-o", mojo_package)
18+
19+
args.add_all(mojo_toolchain.package_copts)
20+
if "-exec-" not in ctx.bin_dir.path:
21+
args.add_all(ctx.attr._mojo_package_copts[BuildSettingInfo].value)
1722
args.add_all([
1823
ctx.expand_location(copt, targets = ctx.attr.additional_compiler_inputs)
1924
for copt in ctx.attr.copts
@@ -78,6 +83,12 @@ then be used in copts with the $(location) function.
7883
doc = """\
7984
Additional compiler options to pass to the Mojo compiler.
8085
86+
Order of options:
87+
1. copts from mojo_toolchain.package_copts
88+
2. copts from //:mojo_package_copt (if not building in exec config)
89+
3. copts from this attribute, with $(location) expanded for files in
90+
additional_compiler_inputs.
91+
8192
NOTE: copts from --mojocopt and mojo_toolchain.copts are not passed to 'mojo
8293
package' since it does not accept many flags.
8394
""",
@@ -90,6 +101,9 @@ package' since it does not accept many flags.
90101
providers = [MojoInfo],
91102
),
92103
"data": attr.label_list(),
104+
"_mojo_package_copts": attr.label(
105+
default = Label("//:mojo_package_copt"),
106+
),
93107
},
94108
toolchains = ["//:toolchain_type"],
95109
)

mojo/providers.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ MojoToolchainInfo = provider(
1313
fields = {
1414
"all_tools": "All the files that must be available in actions in order for the toolchain to work.",
1515
"copts": "Additional compiler options to pass to the Mojo compiler.",
16+
"package_copts": "Additional compiler options to pass to the Mojo compiler when running 'mojo package'.",
1617
"lld": "The lld compiler executable to link with",
1718
"mojo": "The mojo compiler executable to build with",
1819
"implicit_deps": "Implicit dependencies that every target should depend on, providing either CcInfo, or MojoInfo",

mojo/toolchain.bzl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def _mojo_toolchain_impl(ctx):
2727
mojo_toolchain_info = MojoToolchainInfo(
2828
all_tools = tool_files,
2929
copts = copts,
30+
package_copts = ctx.attr.package_copts,
3031
lld = ctx.executable.lld,
3132
mojo = ctx.executable.mojo,
3233
implicit_deps = ctx.attr.implicit_deps,
@@ -41,6 +42,10 @@ mojo_toolchain = rule(
4142
mandatory = False,
4243
doc = "Additional compiler options to pass to the Mojo compiler.",
4344
),
45+
"package_copts": attr.string_list(
46+
mandatory = False,
47+
doc = "Additional compiler options to pass to the Mojo compiler when running 'mojo package'.",
48+
),
4449
"extra_tools": attr.label_list(
4550
providers = [DefaultInfo],
4651
allow_files = True,

0 commit comments

Comments
 (0)