Clang is the native C compiler for Mac OS, but I expect it can’t link an ARM executable in the way Rust wants. So, it looks like we need a linker for ARM. Reading the guideseems to suggest that Rust doesn’t have its own linker for Linux targets – it uses the linker from a C toolchain, for example the GNU C compiler. To replace the text Apple with Linux in all text files in current directory and down you can run this.name '.txt' -print xargs perl -pi -e's/Apple/Linux/ig'.txt. Listing files changed today. Sometimes, we create a file during the day and forget what name we gave it. So here is the command to locate such files.
Wanting to compile a small program I’d written in Rust to run on my home router, I found this guide to cross compilation of Rust code. The router is a Netgear R7000 with an ARM processor, running FreshTomato, a distribution of Linux for ARM and MIPS architecture consumer routers. The top of that guide shows an example of installing the cross-compilation toolchain for ARM on Ubuntu, but it required some work to adapt to Mac OS High Sierra, my desktop environment.
The guide suggests rustup can be used to install extra cross compilation targets. I already have rustup which I’ve used to install Rust for Mac OS and keep it up-to-date, so that’s handy. So I ran “rustup target list” to list all the installable targets:
That’s a lot of possible targets. It looks like in the ARM space, there’s AArch64, arm-unknown, armebv7r, armv5te and armv7(r?) architectures of various variants. So, let’s google to see what kind of CPU the router has.
According to the OpenWRT wiki it’s a Broadcom BCM4709A0. So, what kind of architecture is that? Googling for “BCM4709A0” brought me to Wikidevi, which says it’s an ARM Cortex-A9. Looking at Wikipedia for the Cortex-A9 tells me:
So I’d guess one of the armv7 targets is the best one. It’s probably not armv7-apple-ios or armv7-linux-androideabi, since this isn’t an iOS or Android OS. That leaves armv7-unknown-linux-gnueabihf, armv7-unknown-linux-musleabihf, armv7r-none-eabi and armv7r-none-eabihf. I know the router runs Linux, so let’s try the first two. I installed the armv7-unknown-linux-gnueabihf target with:
OK, let’s try compiling a “hello world” Rust application with that target:
That failed with a message “error: linking with cc failed: exit code: 1” and then a note showing the entire cc command, and a note saying:
OK, so I guess this is clang giving that error. Clang is the native C compiler for Mac OS, but I expect it can’t link an ARM executable in the way Rust wants. So, it looks like we need a linker for ARM. Reading the guide seems to suggest that Rust doesn’t have its own linker for Linux targets – it uses the linker from a C toolchain, for example the GNU C compiler. So we need to install a C toolchain targeting ARM.
My first stop when looking to install open source tools on Mac OS is Homebrew, and indeed there’s a formula on there for arm-linux-gnueabihf-binutils – it looks like that could be what we need to get a linker targeting ARM Linux. So let’s install that with:
That installs a set of tools named arm-linux-gnueabihf-addr2line, arm-linux-gnueabihf-ar and so on. I know the linker is normally invoked as “ld”, and cross-compilation toolchains by convention prefix their tool names with the target name, so the ARM Linux linker should be arm-linux-gnueabihf-ld. I know from the guide that this needs to go in ~/.cargo/config in a section like this:
But the Homebrew formula didn’t install arm-linux-gnueabihf-gcc – it only has arm-linux-gnueabihf-ld. Well, let’s try that instead, so the config is:
OK, let’s try compiling again…
This is more promising, but it looks like the linker can’t find all of those libraries to link with. Those look like parts of the GNU C library and other system libraries for Linux, which the Homebrew package arm-linux-gnueabihf-binutils doesn’t seem to include. These would normally be installed on a Linux system, but on Mac OS we don’t have them.
It seemed like I might need to install a more complete Linux toolchain that includes those libraries, but before trying that, let’s look at the other Rust target – armv7-unknown-linux-musleabihf. The “musl” in the name refers to the musl C library, a small C library that can be statically linked with Rust programs instead of the GNU C library. This sounds promising as it removes the need to link against libpthread, etc, which we had problems with earlier.
Let’s put the same linker configuration in ~/.cargo/config for the armv7-unknown-linux-musleabihf target:
And try compiling our Rust program with this target:
It built, so let’s copy the executable to the router:
And then SSH on to the router and run it:
Great, it works! Using the musl C library and statically linking everything is somewhat less optimal than linking against the C library that’s already installed on Linux, as it means the built executable size is larger, but it’s good enough for a simple Rust program.
Don't junk your old Mac hardware just because it can no longer get new software updates. Get some new life in your old Macs with the GNU/Linux operating system! We'll show you how to 'try it before you buy it' so to speak to see how a specific version of GNU/Linux, Ubuntu, will run on your aging Mac.
Compile For Macos On Linux Windows 7Why bother trying GNU/Linux on your Mac?
I have a late 2010 MacBook Air. It's not as fast or as optically pretty as my 5K iMac nor my iPad Pro for that matter. I do, however, use it daily. I'm writing this article on it. As old as it is, it's 'good enough' for what I use it for and I still prefer the keyboard on it to the one on more modern MacBooks.
My MacBook Air just made the cutoff for being compatible with the newly released macOS High Sierra. I'm happy to know that for at least one more year, I'll be able to get the latest and greatest doodads, bells and whistles, and more importantly, I'll get the latest security updates (good thing too as while I'll write this, we've just found out that WPA2 has been cracked and clients will need to be updated with security patches).
However, Apple at one point will deem this well working, useful, good-enough MacBook Air as unworthy of any more updates. And as I understand the reasons why my venerable MacBook Air can't be supported indefinitely, I still find it to be wasteful that a decent-enough, capable, and not to mention still functioning computer be put to pasture because of lack of software updates.
This is where GNU/Linux comes in. GNU/Linux is a free and open source operating system very similar in many respects to the under-pinnings of macOS. It comes with modern networking capabilities, web browsers, and content creation tools. Not only is it known to run well with older hardware, but you will also get many years of software and security updates that you'd likely miss out on if you were to remain on a macOS-only installation.
See how well Ubuntu Linux runs on your Mac
You needn't throw out the baby with the bathwater and wipe your old Mac's hard drive clean before trying it out. All you need is a USB drive of a least 2GB in size and an internet connection to get started. Here's how to do it.
Get your USB drive ready
Download the Ubuntu Linux installation file.
Prepare your USB key to run Ubuntu Linux
We now need to make the USB drive capable of booting Ubuntu Linux with special software. Ubuntu recommends using Etcher.
Try Ubuntu Linux!
You're now ready to try Ubuntu linux by booting off of your newly created bootable USB key drive.
Cross Compile For Mac Os X On Linux
You'll be able to connect to Wi-Fi, browse the web, write up an article (or anything else you may want to do) without making any permanent changes to your existing hard drive. See how much you like it and you can eventually install Ubuntu side by side with your macOS install or go full bore with a Linux only installation (tutorials coming later).
What do you do with your old hardware?
I get a real kick out of repurposing old technology with new abilities. I really value the longevity of a computer system that can still run modern software. If I can keep it going beyond it's expected lifespan, I'm a happy camper. What's your take? What do you do with your old hardware? Let us know in the comments!
We may earn a commission for purchases using our links. Learn more.
Up and up
Compile For Macos On Linux Windows 7Analyst says iPhone 12 will cost more even without charger and headphonesCompile For Macos On Linux Download
Jeff Pu, an analyst at Chinese research firm GF Securities, predicts that Apple will raise the price of the iPhone 12 by at least $50.
Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
December 2020
Categories |