-
Notifications
You must be signed in to change notification settings - Fork 64
Initial working patch to build arm64 Nim binaries on M1 macs #301
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
Conversation
I cloned this PR and rebuilt choosenim. That seems fine so far. Running $ choosenim devel
Downloading Nim latest-devel from GitHub
[##################################################] 100.0% 0kb/s
Extracting macosx_x64.tar.xz
Extracting macosx_x64.tar
Setting up git repository
Building Nim #devel
Compiler: Already built
Building: ProxyExe
Exception: switcher.nim(46, 12) `exitCode == 0` ("arch: posix_spawnp: /Users/jaremycreechley/.choosenim/toolchains/nim-#devel/bin/nimble: Bad CPU type in executable\n", "arch -arm64 /Users/jaremycreechley/.choosenim/toolchains/nim-#devel/bin/nimble --nim:\'/Users/jaremycreechley/.choosenim/toolchains/nim-#devel/bin/nim\' c -d:release /var/folders/dl/98d12czj5_b9lyfvzykftzt80000gn/T/proxyexe/src/choosenimpkg/proxyexe.nim")
Cleaning failed install of #devel
Tip: 9 messages have been suppressed, use --verbose to show them.
Error: Installation failed It seems to work with $ choosenim 1.4.4
Downloading Nim 1.4.4 from nim-lang.org
[##################################################] 100.0% 0kb/s
Extracting nim-1.4.4.tar.xz
Extracting nim-1.4.4.tar
Building Nim 1.4.4
Building koch
Building Nim
Building tools (nimble, nimgrep, nimpretty, nimsuggest, testament)
Building: ProxyExe
Installed component 'nim'
Prompt: Symlink for 'nimble' detected in '/Users/jaremycreechley/.nimble/bin'. Can I remove it? [y/N]
Answer: y
Removed symlink pointing to ../pkgs/nimble-#8f7af86/nimble
Installed component 'nimble'
Installed component 'nimgrep'
Installed component 'nimpretty'
Installed component 'nimsuggest'
Installed component 'testament'
Installed component 'nim-gdb'
Switched to Nim 1.4.4
# jaremycreechley @ Jaremys-MacBook-Air in ~/projs/nims/third-party/choosenim on git:master o [22:18:28]
$ nim -v
Nim Compiler Version 1.4.4 [MacOSX: arm64]
Compiled at 2022-07-08
Copyright (c) 2006-2020 by Andreas Rumpf
active boot switches: -d:release However running $ choosenim 1.6.4
Downloading Nim 1.6.4 from nim-lang.org
[##################################################] 100.0% 0kb/s
Extracting nim-1.6.4.tar.xz
Extracting nim-1.6.4.tar
Building Nim 1.6.4
Building koch
Building Nim
Building tools (nimble, nimgrep, nimpretty, nimsuggest, testament)
Building: ProxyExe
Installed component 'nim'
Installed component 'nimble'
Installed component 'nimgrep'
Installed component 'nimpretty'
Installed component 'nimsuggest'
Installed component 'testament'
Installed component 'nim-gdb'
Switched to Nim 1.6.4
# jaremycreechley @ Jaremys-MacBook-Air in ~/projs/nims/third-party/choosenim on git:master o [22:23:51]
$ nim -v
[1] 92907 killed nim -v Here's trying to re-install 1.6.6: $ choosenim 1.6.6
Building: ProxyExe
Exception: switcher.nim(46, 12) `exitCode == 0` ("arch: posix_spawnp: /Users/jaremycreechley/.choosenim/toolchains/nim-1.6.6/bin/nimble: Bad CPU type in executable\n", "arch -arm64 /Users/jaremycreechley/.choosenim/toolchains/nim-1.6.6/bin/nimble --nim:\'/Users/jaremycreechley/.choosenim/toolchains/nim-1.6.6/bin/nim\' c -d:release /var/folders/dl/98d12czj5_b9lyfvzykftzt80000gn/T/proxyexe/src/choosenimpkg/proxyexe.nim")
Error: Installation failed Lastly: $ file $(which nim)
/Users/jaremycreechley/.nimble/bin/nim: Mach-O 64-bit executable arm64 |
P.S. I'm on a MacOS Monterey 12.0.1 (21A559) on a M1 MacBook Air. |
I see ... let me try those versions. Edit: I assumed Here're differences: nim devel:
nim 1.4.4:
Like that. For 1.6.6, I think you already have x64 Nim binaries. |
It seems mixing up the sources, or partial builds breaks things. I did a fresh install on choosenim, then built this PR. That route was able to download and build Nim 1.6.4 which runs fine. |
Though once I have an arm64 build of 1.6.4 when I try remove and rebuild 1.6.6 I still get a bad compiler build: # jaremycreechley @ Jaremys-MacBook-Air in ~/projs/nims/third-party/choosenim on git:master o [23:05:15]
$ ./bin/choosenim remove 1.6.6
Info: Removed version 1.6.6
# jaremycreechley @ Jaremys-MacBook-Air in ~/projs/nims/third-party/choosenim on git:master o [23:05:42]
$ ./bin/choosenim 1.6.6
Downloading Nim 1.6.6 from nim-lang.org
[##################################################] 100.0% 0kb/s
Extracting nim-1.6.6.tar.xz
Extracting nim-1.6.6.tar
Building Nim 1.6.6
Building koch
Building Nim
Building tools (nimble, nimgrep, nimpretty, nimsuggest, testament)
Building: ProxyExe
Installed component 'nim'
Installed component 'nimble'
Installed component 'nimgrep'
Installed component 'nimpretty'
Installed component 'nimsuggest'
Installed component 'testament'
Installed component 'nim-gdb'
Switched to Nim 1.6.6
# jaremycreechley @ Jaremys-MacBook-Air in ~/projs/nims/third-party/choosenim on git:master o [23:07:32]
$ nim -v
[1] 7211 killed nim -v Binary seems like it's an arm64: $ file $(which nim)
/Users/jaremycreechley/.nimble/bin/nim: Mach-O 64-bit executable arm64 |
I'm figuring out about why process is killed and I found something like this: "code signing rejecting invalid page at address" |
Great, and yah that follows what I'm seeing with ProxeExe. I re-installed choosenim and then my Nim 1.6.6 arm64 build worked. The ProxyExe seemed to be breaking between choosenim installs. |
@elcritch You can try again now. I think it should fix As far as I tested, I can switch between |
But for the case that choosenim downloading |
I'm re-testing your updates. I'll let you know how it goes. |
Ok did some testing. It works when using a default choosenim and then use this PR to install a new Nim version. However when I do a Executing: arch -arm64 /Users/jaremycreechley/.choosenim/toolchains/nim-1.6.6/bin/nimble --nim:'/Users/jaremycreechley/.choosenim/toolchains/nim-1.6.6/bin/nim' c -d:release /var/folders/dl/98d12czj5_b9lyfvzykftzt80000gn/T/proxyexe/src/choosenimpkg/proxyexe.nim
Exception: switcher.nim(46, 12) `exitCode == 0` (" Verifying dependencies for choosenim@0.8.4\n Prompt: No local packages.json found, download it from internet? [y/N]\nio.nim(156) raiseEOF\nError: unhandled exception: EOF reached [EOFError]\n Answer: \n", "arch -arm64 /Users/jaremycreechley/.choosenim/toolchains/nim-1.6.6/bin/nimble --nim:\'/Users/jaremycreechley/.choosenim/toolchains/nim-1.6.6/bin/nim\' c -d:release /var/folders/dl/98d12czj5_b9lyfvzykftzt80000gn/T/proxyexe/src/choosenimpkg/proxyexe.nim")
Cleaning failed install of 1.6.6
Debug: Reporting to analytics...
Debug: Reporting to analytics...
Debug: Reporting to analytics...
Error: Installation failed |
Nice! I added the |
Maybe you could add |
Adding the Edit: also just tested this on a linux machine of mine. Works fine. |
Oh, the compiler runs much faster when it's not using Rosetta! ❤️ |
Huge thanks @elcritch. We can build choosenim universal binary after this by tweaking |
Glad to help! |
@dom96 We made progress so far 😁 What do you think? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome collab between both of you here. Thank you!
I haven't read all the discussion in detail so please forgive me if I missed something, but here are my questions from just looking at the code:
- The proxyexe is now compiled at choosenim runtime. Can we do it at compile-time as it was done before? I'm concerned that performing this at runtime will be too unreliable.
- In general I'm wondering: do we need proxyexe and choosenim to be compiled as arm64? Can't we just keep the changes in utils.nim and have choosenim make an arm64 Nim build with just these changes?
We can pre-compile proxies as usual. But we need to build choosenim binaries (
Here's the case:
import os, osproc, strutils
let (output, exitCode) = execCmdEx("cc -o $# test.c" % "test".addFileExt(ExeExt))
assert exitCode == 0, output And it's compiled in arm64.
import os, osproc
const nimBin = "compiler".addFileExt(ExeExt)
let nimProcess = startProcess(nimBin)
let exitCode = nimProcess.waitForExit()
nimProcess.close()
assert exitCode == 0 This is compiled in x86_64.
The strange case is:
So, to conclude, I believe, although We DON'T NEED |
@dom96, Ahh ... I get it ... we can build universal proxies on mac. So, we can accomplish with minimal changes |
@elcritch @dom96 We combined ideas together 🍻 But there's an issue left: Choosenim is downloading x64 tar in devel channel. Other than that, choosenim can build arm64 nim binaries.
Solved! ( Proxies are compiled and embed in choosenim )
Yeah, like above explanation, proxies need to be arm64 on M1 macs, so I build universal proxies. Solved! I think we can merge this now after some testing. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Beautiful.
But there's an issue left: Choosenim is downloading x64 tar in devel channel. Other than that, choosenim can build arm64 nim binaries.
You should be able to find the code responsible for this by searching for this constant ident: https://github.com/dom96/choosenim/blob/master/src/choosenimpkg/download.nim#L14
Co-authored-by: Dominik Picheta <dominikpicheta@googlemail.com>
Cool, resolved suggestions from reviews. |
Cool, I also fixed choosenim downloading if not isRosetta() and hostCPU == "amd64": that did the trick. On my machine, choosenim works fine! How about yours? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just one thing, other than that looks good. Thank you so much for working on this!
macOS without RosettaI added several checks (
|
Are we waiting for something to merge this? |
Also eagerly awaiting any updates here, as I keep having issues with different nim installs (brew, nix, and choosenim) and this issue seems like the closest to being fixed :) |
Any update on this PR? Looks like it is ready to go? |
still waiting on approval? |
I don’t know … but @dom96 seems like inactive on this repo.
|
This all looks good, but really it doesn't seem to work when using choosenim to build on an M1 MacBook. Is the --firstinstall option required? |
@lewisl Hi, use the fork from https://github.com/nim-lang/choosenim |
I’ll give it a try…
The published instructions for building from source certainly work (as does homebrew), but neither have the advantages of choosenim.
From: Hein Thant ***@***.***>
Reply-To: dom96/choosenim ***@***.***>
Date: Friday, August 30, 2024 at 5:50 PM
To: dom96/choosenim ***@***.***>
Cc: Lewis Levin ***@***.***>, Mention ***@***.***>
Subject: Re: [dom96/choosenim] Initial working patch to build arm64 Nim binaries on M1 macs (PR #301)
@lewisl<https://github.com/lewisl> Hi, use the fork from https://github.com/nim-lang/choosenim
—
Reply to this email directly, view it on GitHub<#301 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AAIYWLLTFGBIHEOLHV7BPATZUEHLVAVCNFSM6AAAAABNM4IAG6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGMRSGYZTSMZVGQ>.
You are receiving this because you were mentioned.Message ID: ***@***.***>
|
I'll close this PR as we should use https://github.com/nim-lang/choosenim fork instead of this one. |
Sure.
Fork going to be accepted as PR at some point?
From: Hein Thant ***@***.***>
Reply-To: dom96/choosenim ***@***.***>
Date: Friday, August 30, 2024 at 8:04 PM
To: dom96/choosenim ***@***.***>
Cc: Lewis Levin ***@***.***>, Mention ***@***.***>
Subject: Re: [dom96/choosenim] Initial working patch to build arm64 Nim binaries on M1 macs (PR #301)
I'll close this PR as we should use https://github.com/nim-lang/choosenim fork instead of this one.
—
Reply to this email directly, view it on GitHub<#301 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AAIYWLO74HFLS34CBM35NLLZUEXCFAVCNFSM6AAAAABNM4IAG6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGMRSG42DGOJVGE>.
You are receiving this because you were mentioned.Message ID: ***@***.***>
|
@lewisl, I don't think so. The fork is under official Nim organization. I think that will be the one actively maintained. |
Sure. That’s the right outcome. Will the fork get merged? Same difference…
- Lewis
On Aug 30, 2024, at 9:01 PM, Hein Thant ***@***.***> wrote:
@lewisl<https://github.com/lewisl>, I don't think so. The fork is under official Nim organization. I think that will be the one actively maintained.
—
Reply to this email directly, view it on GitHub<#301 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AAIYWLJEQKVMBTZR56JBIKTZUE5ZXAVCNFSM6AAAAABNM4IAG6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGMRSG42TMNZUGA>.
You are receiving this because you were mentioned.Message ID: ***@***.***>
|
Sorry, I couldn’t get this to work.
First problem is running the script as shown in the forked readme.
I downloaded the zip and ran choosenim-unix-init.sh that came directly from the forked repo.
This unfortunately ended up building the x86-64 executable, which runs under Mac Rosetta. Exactly what I didn’t want.
Can you give some tip for how to run the right thing from the fork?
From: Hein Thant ***@***.***>
Reply-To: dom96/choosenim ***@***.***>
Date: Friday, August 30, 2024 at 8:04 PM
To: dom96/choosenim ***@***.***>
Cc: Lewis Levin ***@***.***>, Mention ***@***.***>
Subject: Re: [dom96/choosenim] Initial working patch to build arm64 Nim binaries on M1 macs (PR #301)
Closed #301<#301>.
—
Reply to this email directly, view it on GitHub<#301 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AAIYWLME6I74GY2ULEFY6TDZUEXCBAVCNFSM6AAAAABNM4IAG6VHI2DSMVQWIX3LMV45UABCJFZXG5LFIV3GK3TUJZXXI2LGNFRWC5DJN5XDWMJUGA4TANRQHE2TIMA>.
You are receiving this because you were mentioned.Message ID: ***@***.***>
|
Not clear how this can ever work. I copied the install script RAW directly from the forked repo.
Here is how it started out:
```
trychoosenim sh install.sh
choosenim-init: Downloading choosenim-0.8.5_macosx_amd64
```
Note that “trychoosenim” was the directory where I was isolating the install.
You can see the script doesn’t detect the platform architecture correctly and downloads the wrong source tarball.
From: Hein Thant ***@***.***>
Reply-To: dom96/choosenim ***@***.***>
Date: Friday, August 30, 2024 at 8:04 PM
To: dom96/choosenim ***@***.***>
Cc: Lewis Levin ***@***.***>, Mention ***@***.***>
Subject: Re: [dom96/choosenim] Initial working patch to build arm64 Nim binaries on M1 macs (PR #301)
Closed #301<#301>.
—
Reply to this email directly, view it on GitHub<#301 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AAIYWLME6I74GY2ULEFY6TDZUEXCBAVCNFSM6AAAAABNM4IAG6VHI2DSMVQWIX3LMV45UABCJFZXG5LFIV3GK3TUJZXXI2LGNFRWC5DJN5XDWMJUGA4TANRQHE2TIMA>.
You are receiving this because you were mentioned.Message ID: ***@***.***>
|
@lewisl this PR is merged to the fork just 2 days ago and the script is not updated yet. But I saw Ringabout created a test CI/CD for arm64 build. You can try this release https://github.com/nim-lang/choosenim/releases/tag/master. There's arm64 binary and let us know. |
Sorry, spent enough time on it.
Two ways to install arm64 nim that work:
1. Homebrew
2. Follow the directions on the website to download and build from source.
No one loves being a sysop except a REAL sysop and that’s not you or me or most of the nim team, who have better/cooler stuff to do.
This is the kind of stuff that hurts nim adoption, though to be fair the documentation on the nim website is clear and works, so really there is not that much of an obstacle.
Perhaps choosenim just focuses on a corner case for package developers who want to target multiple versions of nim and keep them “at their fingertips”. That’s not most people who develop applications with nim.
One day for kicks I’ll have to try bootstrapping a new version of nim from the preceding version…
From: Hein Thant ***@***.***>
Reply-To: dom96/choosenim ***@***.***>
Date: Saturday, August 31, 2024 at 5:26 AM
To: dom96/choosenim ***@***.***>
Cc: Lewis Levin ***@***.***>, Mention ***@***.***>
Subject: Re: [dom96/choosenim] Initial working patch to build arm64 Nim binaries on M1 macs (PR #301)
@lewisl<https://github.com/lewisl> this PR is merged to the fork just 2 days ago and the script is not updated yet.
But I saw Ringabout created a test CI/CD for arm64 build. You can try this release https://github.com/nim-lang/choosenim/releases/tag/master. There's arm64 binary and let us know.
—
Reply to this email directly, view it on GitHub<#301 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AAIYWLLVG75WXASZSFXIJNDZUGY5ZAVCNFSM6AAAAABNM4IAG6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGMRSHA4DENBTHE>.
You are receiving this because you were mentioned.Message ID: ***@***.***>
|
Without the script it’s not too useful.
From: Hein Thant ***@***.***>
Reply-To: dom96/choosenim ***@***.***>
Date: Saturday, August 31, 2024 at 5:26 AM
To: dom96/choosenim ***@***.***>
Cc: Lewis Levin ***@***.***>, Mention ***@***.***>
Subject: Re: [dom96/choosenim] Initial working patch to build arm64 Nim binaries on M1 macs (PR #301)
@lewisl<https://github.com/lewisl> this PR is merged to the fork just 2 days ago and the script is not updated yet.
But I saw Ringabout created a test CI/CD for arm64 build. You can try this release https://github.com/nim-lang/choosenim/releases/tag/master. There's arm64 binary and let us know.
—
Reply to this email directly, view it on GitHub<#301 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AAIYWLLVG75WXASZSFXIJNDZUGY5ZAVCNFSM6AAAAABNM4IAG6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGMRSHA4DENBTHE>.
You are receiving this because you were mentioned.Message ID: ***@***.***>
|
@lewisl yes, I agree. Script helps alot easier. But I think those are still in progress. I saw Apple Silicon Github action was setup in the fork. So, I hope these issues will be solved in near future. Since I got Apple Silicon machines, I was building Nim from source. |
The patch is quite simple.
choosenim use
proxyexe
that is statically compiled and statically read and is embedded in choosenim like this:Instead of statically compiled
proxyexe
, I patched thecompileProxyexe
proc to compileproxyexe
on user machine with embedded source code and previously compiled Nim like this:I mean I embedded source codes required by
proxyexe
on choosenim binary. Then, I compiled them with previously compiled arm64nim
andnimble
binaries ( choosenim compile nim first, then write proxy ).Yeah, this is the main trick. And also on M1 macs with rosetta, I appended
arch -arm64
flags on most commands to run as arm process like this:You can mark this PR as draft. On my mac, it works fine. How about others?