Setting up a Magic Lantern development environment on a Macintosh
For the terminally impatient try Danne's Magic lantern development compiler tool(Mac OS). It includes the quick install script originally written for this tutorial. You can still get just the install script and skip this tutorial--scroll down to the bottom of this first post.NewsflashApple released macOS Catalina, 10.15 so these instructions no longer work. However, there are some fixes in the works.
Before we begin did you know that all Macs have development tools already installed yet they are not available to the user? If you have a "virgin" Mac that you have never used for development try this, open up a terminal (located in Applications under Utilities) and type:
which gcc
Chances are that it will return /usr/bin/gcc, gcc is the compiler to build Mac binaries and one of the tools that we'll use to build Magic Lantern. So we're all set, right? Ok, not that easy try typing in gcc to launch the compiler:
We don't really need the full Xcode development environment so simply click the highlighted "Install" button, accept the licence to activate the developer tools. If you have already installed Xcode you might still need to install the command tools. Use this terminal command:
xcode-select --install
This will install the developer tools in /Library/Developer/CommandLineTools/. These tools need to play nicely with other developer tools required by Magic Lantern. If you are using OS 10.14 (Mojave) or newer there's another step we need to take so all these tools work nicely together:
open /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.*.pkg
Next, there are some additional tools needed to compile Magic Lantern and several ways to install them. The simplest is to use one of the popular package managers like Fink (
http://www.finkproject.org/) MacPorts (
https://www.macports.org/) or Homebrew (
http://brew.sh/) Homebrew is the newest of the bunch so we'll be using that. Homebrew installs the tools so they are available in /usr/local/bin which happens to be included in every new Mac's default path. However, your Mac may or may not have a /usr/local directory. Another possibility is that you have a /usr/local but don't have permissions to install anything in there. Do a directory listing to see where you stand:
Rosies-Air:~ Fort$ ls -la /usr
total 8
drwxr-xr-x@ 12 root wheel 408 Oct 12 11:45 .
drwxr-xr-x 31 root wheel 1122 Sep 30 21:56 ..
drwxr-xr-x 5 root wheel 170 Aug 22 18:51 X11
lrwxr-xr-x 1 root wheel 3 Sep 30 21:22 X11R6 -> X11
drwxr-xr-x 3 root wheel 102 Aug 26 19:17 adic
drwxr-xr-x 1061 root wheel 36074 Sep 30 21:28 bin
drwxr-xr-x 258 root wheel 8772 Oct 12 11:45 include
drwxr-xr-x 285 root wheel 9690 Oct 12 11:45 lib
drwxr-xr-x 183 root wheel 6222 Sep 30 21:22 libexec
drwxr-xr-x 243 root wheel 8262 Sep 30 21:22 sbin
drwxr-xr-x 46 root wheel 1564 Oct 12 11:45 share
drwxr-xr-x 4 root wheel 136 Sep 17 00:03 standalone
Notice that I used "ls -la" to get a detailed view of everything in the /usr directory. This system doesn't have a /usr/local directory so we'll have to create one. If you have a /usr/local directory but it is owned by root, you need to change permissions. Try this first:
sudo chown $(whoami):admin /usr/local && sudo chown -R $(whoami):admin /usr/local
Don't be intimidated by long command lines, you're on a Mac so simply copy and paste it into the terminal window. Note that you will have to enter your password.
If it didn't work you are probably on OS-X 10.11 "El Capitan" or 10.12 "Sierra" or even something newer so you need to temporarily turn off disable System Integrity Protection (SIP) in order to create a /usr/local you can use.
1. Restart the computer, while booting hold down Command-R to boot into recovery mode.
2. Once booted, navigate to the "Utilities > Terminal" in the top menu bar.
3. Enter
csrutil disable in the terminal window and hit the return key.
4. Restart the machine and System Integrity Protection will now be disabled.
Now in the terminal enter this command:
sudo mkdir /usr/local && sudo chflags norestricted /usr/local && sudo chown $(whoami):admin /usr/local && sudo chown -R $(whoami):admin /usr/local
What you are looking for is a /usr/local directory with your name on it:
drwxr-xr-x 2 Fort admin 68 Oct 12 12:52 local
Don't skip this next step -- go back into recovery mode and this time type
csrutil enable in the terminal.
Now that we've got a /usr/local that we can use, install Homebrew.
Ok--if you are really itching to get up and running you can now just skip to the bottom of this post and run the quick installation script. Go!If you want to understand what we're doing any why, keep following this tutorial:
First of all this command will download and run a ruby script to install a basic Homebrew setup:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Next start adding some of the tools needed to build Magic Lantern.
brew install python wget mercurial
What's the deal with Python, isn't it already installed? Yes, but getting the Apple version configured to work with the ML install scripts is more trouble than it is worth so we'll just use the Homebrew version. In addition, we're going to get some python scripts using the package manager that Apple left out of their version of Python.
pip install docutils
pip2 install docutils
pip3 install docutils
That's not a mistake--we want all three versions of docutils.
If for some reason you'd rather use Apple's version of Python, I've included instructions on how to set it up at the bottom of this post.
Although the compiler that comes with the Mac can compile ML there are reasons to use a different version. Technically it isn't really gcc but another compiler called "clang" and if you try using it to compile ML you'll probably get a bunch of clang error messages. In addition, Homebrew switched to version 6 of gcc and that's causing some issues with ML so we'll go with good old version 5.
brew install gcc@5
Two versions of gcc? Won't that confuse the system? Homebrew is designed to work with the system so the gcc that you just installed is called gcc-5. This requires some special handling that we'll get to a little later.
We're not not done with compilers, we'll need yet another version of gcc to compile binaries that will run on the ARM architecture used in our Canon cameras. To figure out which ARM compiler to get refer to the ML code, specifically: Makefile.user.default -- ARM_PATH=~/gcc-arm-none-eabi-4_8-2013q4 notice that little squiggly mark after the equal sign? That means that the path starts in your home directory. Ok, so much for the lecture let's get it and put everything in its place.
cd ~ && wget -c https://launchpad.net/gcc-arm-embedded/5.0/5-2016-q3-update/+download/gcc-arm-none-eabi-5_4-2016q3-20160926-mac.tar.bz2 && tar -jxf gcc-arm-none-eabi-5_4-2016q3-20160926-mac.tar.bz2 && rm gcc-arm-none-eabi-5_4-2016q3-20160926-mac.tar.bz2
Note that some of the branches will be looking for a different version of the ARM compiler. You can install multiple versions and the build scripts will search out the preferred version. Let's get 2013-q4 because several of the development branches use that version:
cd ~ && wget https://launchpad.net/gcc-arm-embedded/4.8/4.8-2013-q4-major/+download/gcc-arm-none-eabi-4_8-2013q4-20131218-mac.tar.bz2 && tar -jxf gcc-arm-none-eabi-4_8-2013q4-20131218-mac.tar.bz2 && rm gcc-arm-none-eabi-4_8-2013q4-20131218-mac.tar.bz2
Before doing this next step make sure you are in the directory where you want to save the magic-lantern source code.Finally, time to grab the ML source code and start building. There are plenty of resources on the forum but basically, to get the latest unified branch:
hg clone https://bitbucket.org/hudson/magic-lantern
cd magic-lantern
hg update unified
We're almost ready to build Magic Lantern but first we've got to make one small change first. Go into the magic-lantern directory, find the file named
Makefile.user.default and you'll see this block of code:
#
# Host compiler settings
#
HOST_CC=$(shell which gcc)
HOST_LD=$(shell which ld)
HOST_AR=$(shell which ar)
Now make a new file called
Makefile.user and put in just these lines:
#
# Host compiler settings
#
HOST_CC=gcc-5
HOST_LD=gcc-5
HOST_AR=$(shell which ar)
Warning:
DON'T EDIT Makefile.user.default -- Put the changes you want to make to Makefile.user.default in your Makefile.user file. Think of this as a way to customize the Magic Lantern build environment without modifying the code.
Another area you might want to customize is which modules to include when making a new build. The modules that the developers decided you probably want are in the modules directory in a file called
Makefile.modules.default but maybe you'd like to add another one, take for example bulb_nd. To do this without modifying any Magic Lantern code create a file in the modules directory called
Makefile.modules.user and put this in it:
ML_MODULES_DYNAMIC += bulb_nd
Now let's build Magic Lantern. Here's the way I do it for the EOSM:
cd ~/magic-lantern/platform/EOSM.202/
make clean && make zip
So you want to start hacking away on the code? There's lots of choices when it comes to text editors. You can even use TextEdit though I wouldn't recommend it because you have to turn off all the "smart" features so it doesn't destroy your source code. A better choice is
Aquamacs. I have no financial interest if you get it or not. Hey, it is free. What about the editor that comes with Xcode? You can certainly install the full Xcode app that includes a very capable editor though it is quite a beast to master. It is also quite a heavy download. Of course if you are a real propeller head there's all the old favorites like vi, emacs, pico and nano already installed for you.
Some other software you might want to look into if you are going to get serious and contribute back to the Magic Lantern project is
SourceTree. It supports the bitbucket repositories used by Magic Lantern and it has a nice graphical interface showing the various branches in ML. You're on a Mac, not everything has to be done on the terminal.
Added bonus -- Compiling Magic Lantern command line tools a.k.a. "Module Helpers"The Magic Lantrn source code comes with some applications that you can run in the terminal or incorporate them in your own programs. Both the cr2hdr Lightroom plugin and Danne's cr2hdr-r workflows use--surprise, cr2hdr which is a part of the dual_iso module. There is also a raw2dng, mlv_dump and a few others. Your system is already configured to compile these tools. In fact if you make a build for your camera the Makefile script builds a few of them for you. For example, in the terminal go to magic-lantern/modules/dual_iso and type:
./cr2hdr
We didn't give it any instructions so it simply lists a short list of the available options that it has available. It is beyond the scope of this tutorial how to use the various command line tools but search through the forum and wiki for instructions on how to use the command line tools.
If you find something interesting in the modules you'd like to try out like say raw2dng.c and there isn't a binary for it. Check out the Makefile and see if there is a "rule" to build it. In the case of raw2dng simply type:
make raw2dng
and presto, you have a Mac binary to play with.
Extra Added bonus -- Cross compile to WindowsLet you in on a little Magic Lantern secret. Most Windows binaries aren't compiled in Windows. Sure, there are developers using Windows systems but they are either running a Linux distribution inside of a "VirtualBox" or Cygwin which is a self contained environment that functions within Windows. Only a MinGW or MinGW-64 Magic Lantern development setup might be considered Windows native. In fact if you want the best environment to work with open source projects like Magic Lantern set up a Linux system but one issue is that although a Linux system can be easily set up to compile Windows binaries it is not so easy to do Mac binaries. Anyway, to make Windows binaries on a Mac you need yet another gcc compiler like the ones maintained by the MinGW project. MinGW is short for Minimalistic GNU for Windows. What does GNU stand for? GNU is a recursive acronym for GNU's Not Unix. If you check out Apple documentation you will see that they claim that OS-X is Unix. Geek alert - instead of getting caught up in techno-babble how about setting up a cross compiler on your Mac already and start building Windows binaries.
You will need a darwin (OS-X) build of the MinGW compiler. Homebrew to the rescue.
brew install mingw-w64
This give you a full 32-bit and 64-bit Windows cross compiling setup ready to go. Now to make a Windows binary all you need to do is add the ".exe" file extension when you invoke make. For example:
make cr2hdr.exe
Being able to create Windows binaries on a Mac is so easy that I am including it on the
quick installation script at the bottom of this post, though you'll need to uncomment that line first.
Tip - Removing pesky "._" files from your cardFilenames beginning with a dot are "invisible" and normally you won't even know they are there. Every time you copy files from a Mac file system to another type of filesystem it creates these "dot" files to store some information that is not normally used in other filesystems. If you are using lua on a memory card that is formatted as a FAT16 or FAT32 filesystem (usually cards under 64GB), you may see some strange messages on your screen when your camera starts up with the lua module enabled. It isn't a big deal and dmilligan put in some code to ignore files that begin with a dot but it is rather annoying. Apple actually included a utility to clean up this mess. Assuming your card is named EOS_DIGITAL, here's how to run it:
dot_clean -m /Volumes/EOS_DIGITAL
The "-m" option also removes files that start with an underbar.
Tip - Uninstalling Homebrewruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"
You might also want to clean out your /usr/local directory for any stragglers. Just remember that if you are on "El Capitan" or "Sierra" and you delete /usr/local you will have to do the Command-R csrutil enable/disable song and dance to get it back.
Tip - Configuring Apple's Python to work with MLNote: I don't use this and things might have changed so you're on your own if you want to experiment with this.
Makefile.user.default specifies that PYTHON=python2. Of course you can change that or make an alias for python2 but if you are comfortable disabling System Integrity Protection (SIP) go ahead and do that then:
ln -s /usr/bin/python /usr/bin/python2
The reason for installing the Homebrew version of Python is because Apple left out a Python utility called "pip" that makes it easy to install docutils. Follow these steps to install pip and docutils:
wget https://bootstrap.pypa.io/get-pip.py
sudo -H python get-pip.py
sudo -H pip install docutils
I like to do some cleanup. The docutil scripts are owned by root so let's change that and make sure that we've got permissions for everything in /usr/local:
sudo chown $(whoami):admin /usr/local && sudo chown -R $(whoami):admin /usr/local
You're done with the get-pip.py
rm get-pip.py
Quick installation script.If you have a Mac with a /usr/local directory and would like to get everything up and running in a hurry, you can use
Danne's menu driven compiler tool or download just the quick install script from his repository.
Let's download and run the script using only the terminal. First we'll use curl (copy URL) because it is available on a vanilla Mac. This command copies the script from Danne's repository and saves it to a file named mac_ml.sh
curl https://bitbucket.org/Dannephoto/compiler/raw/default/Compiler.app/Contents/mac_ml.sh > mac_ml.sh
In order to make the file executable you need to change the file's properties. Here's an easy way to do it:
chmod +x mac_ml.sh
Now it is ready to run. To run it from the current current directory you can start it this way:
./mac_ml.sh
You'll need to answer a few questions, enter your password and make decisions like if you want to install the Windows cross compiler and/or QEMU.
Happy coding!