From f3425f13c0365a11874cdf310e850d33dd61a303 Mon Sep 17 00:00:00 2001 From: Chris Novakovic Date: Wed, 5 Nov 2025 18:02:13 +0000 Subject: [PATCH 1/2] Don't attempt to link binaries statically on Darwin While it is theoretically possible to generate statically-linked Mach-O binaries for Darwin, Apple heavily discourages it [1]. Emit a warning when `static` is set to `True` when building a `c_binary` target for Darwin, and generate a dynamically-linked binary instead. Fixes #86. [1] https://developer.apple.com/library/archive/qa/qa1118/_index.html --- build_defs/c.build_defs | 3 ++- build_defs/cc.build_defs | 6 +++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/build_defs/c.build_defs b/build_defs/c.build_defs index 91e1f31..9583909 100644 --- a/build_defs/c.build_defs +++ b/build_defs/c.build_defs @@ -234,7 +234,8 @@ def c_binary(name:str, srcs:list=[], hdrs:list=[], private_hdrs:list=[], compile Alternatively can be a dict of name -> value to define, in which case values are surrounded by quotes. test_only (bool): If True, this rule can only be used by tests. - static (bool): If True, the binary will be linked statically. + static (bool): If True, the binary will be linked statically. Statically-linked binaries are + not supported on macOS, so this is a no-op when generating a Mach-O binary. optional_outs (list): Name of optional outputs. """ return cc_binary( diff --git a/build_defs/cc.build_defs b/build_defs/cc.build_defs index 2e99014..ec41ea0 100644 --- a/build_defs/cc.build_defs +++ b/build_defs/cc.build_defs @@ -548,12 +548,16 @@ def cc_binary(name:str, srcs:list=[], hdrs:list=[], private_hdrs:list=[], Alternatively can be a dict of name -> value to define, in which case values are surrounded by quotes. test_only (bool): If True, this rule can only be used by tests. - static (bool): If True, the binary will be linked statically. + static (bool): If True, the binary will be linked statically. Statically-linked binaries are + not supported on macOS, so this is a no-op when generating a Mach-O binary. linkstatic (bool): Only provided for Bazel compatibility. Has no actual effect since we always link roughly equivalently to their "mostly-static" mode. labels (list): Labels to attach to this rule. optional_outs (list): Name of optional outputs. """ + if CONFIG.TARGET_OS == "darwin" and static: + log.warning("%s: statically-linked binaries are unsupported on Darwin; ignoring value of 'static' parameter" % canonicalise(f":{name}")) + static = False if CONFIG.BAZEL_COMPATIBILITY: linker_flags = ['-lpthread' if l == '-pthread' else l for l in linker_flags] if CONFIG.CC.DEFAULT_LDFLAGS: From 4c87626501ce327d8f35ff470db4009c0716a521 Mon Sep 17 00:00:00 2001 From: Chris Novakovic Date: Thu, 6 Nov 2025 10:27:06 +0000 Subject: [PATCH 2/2] Fix //test:static_test for Darwin --- test/BUILD | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/BUILD b/test/BUILD index e663307..8282d4d 100644 --- a/test/BUILD +++ b/test/BUILD @@ -104,6 +104,8 @@ plugin_e2e_test( "file $(plz query outputs //:static) > file", ], expect_output_contains = { - "file": "statically linked", + # Statically-linked executables can't be generated for Darwin, but //:static should at least + # output a functioning dynamically-linked executable. + "file": "executable" if CONFIG.TARGET_OS == "darwin" else "statically linked", }, )