DEV Community

Cover image for How to cross compile linux kernel on MacOS
Michał Kalbarczyk
Michał Kalbarczyk

Posted on • Originally published at puddleofcode.com

1

How to cross compile linux kernel on MacOS

You have a mac? You need to compile linux kernel for your Raspberry PI?

Great. Follow me…

Part 1. Case sensitive file system

Note: This step is required if you want to have kernel files outside docker. There is a case sensitive fs inside docker container. You can just copy needed file at the end.

Bad news. MacOS uses case insensitive file system by default. What we can do about it?
No we will not make whole root partition case sensitive. We might have trouble.
We try USB drive or SD card, but I don't want to get up from a chair.

We need to create an image file with case sensitive file system first:

$ hdiutil create -size 5g -fs "Case-sensitive APFS" -volname LinuxBuilder LinuxBuilder.dmg
Enter fullscreen mode Exit fullscreen mode

Then we need to mount it:

$ hdiutil attach LinuxBuilder.dmg -mountpoint linux_builder -nobrowse -readwrite
Enter fullscreen mode Exit fullscreen mode

Enter the new file system

$ cd linux_builder
Enter fullscreen mode Exit fullscreen mode

OK problem solved.

Part 2. Checkout kernel

Within aur new shiny file system checkout a kernel source:

$  git clone --depth 1 https://github.com/raspberrypi/linux
Enter fullscreen mode Exit fullscreen mode

Part 3. Cross compilation tools

I bet there are, but don't want to think about this where they are, how to install them. We have docker. Right?
I have you can install it too. It's easy. Just do it.

Create a "Dockerfile" file:

FROM debian:buster

ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get clean && apt-get update && \
    apt-get install -y \
    bc \
    bison \
    flex \
    libssl-dev \
    make \
    kmod \
    libc6-dev \
    libncurses5-dev \
    crossbuild-essential-armhf \
    crossbuild-essential-arm64

WORKDIR /linux
VOLUME ["/linux"]
Enter fullscreen mode Exit fullscreen mode

Build an image:

$ docker build -t linux_builder .
Enter fullscreen mode Exit fullscreen mode

Part 4. Make script

We will need one command: make. But let's create a simple wrapper that runs make in the container.
I am so creative, will call this file make.

# Run builder environment
docker run --rm \
    --device /dev/fuse \
    --cap-add SYS_ADMIN \
    --name linux_builder \
    -v "$(pwd)/linux":"/linux" \
    -e ARCH=arm \
    -e KERNEL=kernel7 \
    -e CROSS_COMPILE=arm-linux-gnueabihf- \
    -it linux_builder \
    make $@
Enter fullscreen mode Exit fullscreen mode

This example is for RPI3. For other platdorm just adjust ARCH, KERNEL, and CROSS_COMPILE envs.

Make it executable:

$ chmod +x make
Enter fullscreen mode Exit fullscreen mode

We're done.

Part 5. Build the kernel

Now instead use make menuconfig we will use ./make menuconfig.
You know what to do next. Right?

If not, here is a hint:

$ ./make bcm2709_defconfig
$ ./make -j12 zImage modules dtbs
Enter fullscreen mode Exit fullscreen mode

Wait. And look what is there:

$ ls linux/arch/arm/boot/
Enter fullscreen mode Exit fullscreen mode

This is all we want.
Cheers!

Image of AssemblyAI tool

Transforming Interviews into Publishable Stories with AssemblyAI

Insightview is a modern web application that streamlines the interview workflow for journalists. By leveraging AssemblyAI's LeMUR and Universal-2 technology, it transforms raw interview recordings into structured, actionable content, dramatically reducing the time from recording to publication.

Key Features:
🎥 Audio/video file upload with real-time preview
🗣️ Advanced transcription with speaker identification
⭐ Automatic highlight extraction of key moments
✍️ AI-powered article draft generation
📤 Export interview's subtitles in VTT format

Read full post

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay