brew icon indicating copy to clipboard operation
brew copied to clipboard

In a package build environment, `cc` accepts the `-mpopcnt` flag despite it being unsupported, unless `-c` is also passed

Open gridbugs opened this issue 3 months ago • 3 comments

brew doctor output

Your system is ready to brew.

Verification

  • [x] I ran brew update twice and am still able to reproduce my issue.
  • [x] My "brew doctor output" above says Your system is ready to brew or a definitely unrelated Tier message.
  • [x] This issue's title and/or description do not reference a single formula e.g. brew install wget. If they do, open an issue at https://github.com/Homebrew/homebrew-core/issues/new/choose instead.

brew config output

HOMEBREW_VERSION: 5.0.2-56-g07a983c
ORIGIN: https://github.com/Homebrew/brew
HEAD: 07a983c338fe0df4409d75c04318c81f654805a4
Last commit: 13 hours ago
Branch: main
Core tap HEAD: a574a7831cb5a4543cabb1a86d8cc1fafe9ab552
Core tap last commit: 16 minutes ago
Core tap JSON: 19 Nov 06:40 UTC
Core cask tap JSON: 19 Nov 06:41 UTC
HOMEBREW_PREFIX: /opt/homebrew
HOMEBREW_CASK_OPTS: []
HOMEBREW_DOWNLOAD_CONCURRENCY: 16
HOMEBREW_EDITOR: nvim
HOMEBREW_FORBID_PACKAGES_FROM_PATHS: set
HOMEBREW_MAKE_JOBS: 8
HOMEBREW_SORBET_RUNTIME: set
Homebrew Ruby: 3.4.7 => /opt/homebrew/Library/Homebrew/vendor/portable-ruby/3.4.7/bin/ruby
CPU: octa-core 64-bit arm_blizzard_avalanche
Clang: 17.0.0 build 1700
Git: 2.50.1 => /Library/Developer/CommandLineTools/usr/bin/git
Curl: 8.7.1 => /usr/bin/curl
macOS: 15.6-arm64
CLT: 26.0.0.0.1.1757719676
Xcode: N/A
Rosetta 2: false

What were you trying to do (and why)?

I want to use a command like cc test.c -mpopcnt to check for C compiler support for the -mpopcnt flag, however the command succeeds even when the compiler does not support the flag.

Context:

In a build script for the base ocaml package, support for the -mpopcnt C compiler flag is tested by compiling a test program passing the flag. The code is here.

The command which tests compiler support for -mpopcnt is:

cc baseconf.c -mpopcnt -o /dev/null

This works as expected when building the package directly (outside of homebrew), where the command above fails with the error:

clang: error: unsupported option '-mpopcnt' for target 'arm64-apple-darwin24.6.0'

I'm trying to build this package as part of a larger project (vpnkit) in a homebrew formula.

What happened (include all command output)?

The command cc baseconf.c -mpopcnt -o /dev/null gives no error and exits with 0. This leads the build script to incorrectly believe that -mpopcnt is supported by the C compiler. Later in the build it attempts to compile C code with cc -c -mpopcnt ..., which fails with the error:

clang: error: unsupported option '-mpopcnt' for target 'arm64-apple-darwin24.6.0'

What did you expect to happen?

I expected the command cc baseconf.c -mpopcnt -o /dev/null to fail with the error:

clang: error: unsupported option '-mpopcnt' for target 'arm64-apple-darwin24.6.0'

That's how the command behaves outside of a homebrew build environment, and that's how the command behaves inside a homebrew environment if -c is also passed

Step-by-step reproduction instructions (by running brew commands)

$ brew sh
$ echo 'int main(){return 0;}' > a.c
$ cc -mpopcnt a.c   # no error
$ cc -mpopcnt -c a.c
clang: error: unsupported option '-mpopcnt' for target 'arm64-apple-darwin24.6.0'


Repeating the above sequence of commands without first entering a homebrew environment leads to both `cc` commands to fail with the error.

gridbugs avatar Nov 19 '25 06:11 gridbugs

We generally have different rulesets for configure and build stages. In this case, we probably don't yet have detection for dune-configurator. If there is a way to detect that and differentiate it from a regular build, we can add it which should fix the underlying problem.

The code you point out appears to also have been refactored in 0.18 which isn't released on opam yet. The logic is different on 0.17.3 and should work: https://github.com/janestreet/base/commit/68f18ed6a5e94dda1ed423c3435d1515259dcc7d

Bo98 avatar Nov 20 '25 19:11 Bo98

You're correct in that base's updated build script should now work in brew. The project I'm trying to package for homebrew (vpnkit) is stuck on an older version of base, but I've worked around the issue there too. This problem doesn't block my work packaging vpnkit.

But the reason I raised this issue is that the underlying problem, that cc -mpopcnt ... does not fail with an error, while cc -c -mpopcnt ... does fail (as it should), seemed like a more general problem in brew that could potentially affect other packages that test for support of -mpopcnt (and presumably other compiler flags too) by compiling and linking a simple C program passing those flags to the compiler, and expect a successful compilation to indicate support for those flags.

we probably don't yet have detection for dune-configurator

I don't really understand why this would be necessary. The fact that a project uses dune-configurator to discover facts about the system where it's being built seems like an implementation detail of the project, and not something that should affect brew's behaviour in building a package. Does brew's cc shim behave differently depending on the tool that's invoking it?

gridbugs avatar Nov 21 '25 03:11 gridbugs

I actually misread the original issue. I thought you were describing an issue calling make but I see you've reproduced it in brew sh.

You've unfortunately hit a combination with a Clang bug: https://github.com/llvm/llvm-project/issues/116278. Homebrew applies a set of LDFLAGS by default (we need -headerpad_max_install_names) so will trigger this in more cases. -c is the correct workaround for that Clang bug.

As for working around it on our side, it seems tricky. We could omit LDFLAGS for configure-stage builds as if it's not producing binaries but that then cycles around to the original discussion.

Bo98 avatar Nov 21 '25 07:11 Bo98

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

github-actions[bot] avatar Dec 12 '25 09:12 github-actions[bot]