Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

choosenim breaks user code on M1 Mac #318

Open
mode80 opened this issue Feb 2, 2023 · 1 comment
Open

choosenim breaks user code on M1 Mac #318

mode80 opened this issue Feb 2, 2023 · 1 comment

Comments

@mode80
Copy link

mode80 commented Feb 2, 2023

choosenim seems to break packages and user code when installing Nim on an M1 mac (Apple silicone, ARM64) .

The issue seems to be that choosenim compiles the nim compiler as an x86 executable. Checking this, I get:

$ file `which nim`
/Users/mode80/.nimble/bin/nim: Mach-O 64-bit executable x86_64

When packages and user code make use of code like when defined(arm64),
it incorrectly reports false, and instead reports true for defined(amd64). The program usually crashes thereafter because whatever code path was needed for M1 doesn't run. (For example, the threading module using processor-specific assembly instructions based on this kind of check. )

When I install nim with homebrew, and test, I get

$ file `which nim`
/opt/homebrew/bin/nim: Mach-O 64-bit executable arm64

Then things work as expected.

@quantimnot
Copy link

I just wasted a lot of time on this myself.

Here is what I learned:

  • The default arch on macOS is that of the calling process.
  • Choosenim doesn't have an arm64 build.
  • The choosenim proxy (~/.nimble/bin/nim) calling an arm64 nim compiler, will target x86_64.
  • hostCPU is not determined at runtime, but is inherited from the bootstrapping compiler.
  • Passing --cpu:arm64 doesn't help.
  • x86_64 emulation can be detected at runtime
  • Adding this configuration to the global nim.cfg will make this work for clang and ld:
    @if macosx:
      clang.options.always %= "${clang.options.always} -arch arm64"
      clang.options.linker %= "${clang.options.linker} -arch arm64"
    @end

See Also

https://developer.apple.com/documentation/apple-silicon/addressing-architectural-differences-in-your-macos-code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants