Michael Pittino
Michael Pittino

Reputation: 1636

Writing a makefile for ARM project

My project structure looks like:

.
├── build
├── src
|   ├── rbpi
|   |   └── gpio.h
|   ├── boot.c
|   ├── boot.s
|   └── kernel.c
└── linker.ld

This is a simple raspberry pi kernel that makes a LED blink! I currently use a simple bat file with the following content to build it:

arm-none-eabi-gcc -g -O0 -mfpu=neon-vfpv4 -mfloat-abi=hard -march=armv7-a -mtune=cortex-a7 -nostartfiles -Wl,-T,linker.ld src/kernel.c src/boot.c src/boot.s -o build/kernel.elf
arm-none-eabi-objcopy build/kernel.elf -O binary build/kernel7.img

Since I will add more files to this project, I would have to append every file to my "buildscript".
If possible, I'd like to use a makefile.

How should my makefile look like if I want the following "rules"?

Upvotes: 4

Views: 15440

Answers (1)

Julien Palard
Julien Palard

Reputation: 11646

A typical Makefile may look like... Wait there's a documentation about GNU Make here with a nice simple Makefile: http://www.gnu.org/software/make/manual/make.html#Simple-Makefile

So for you a simple one to start may be:

SRC := $(wildcard src/*.c src/*.s)
CFLAGS :=  -g -O0 -mfpu=neon-vfpv4 -mfloat-abi=hard -march=armv7-a -mtune=cortex-a7 -nostartfiles -Wl,-T,linker.ld

all: build/kernel.img

build/kernel.elf: $(SRC)
    arm-none-eabi-gcc $(CFLAGS) $(SRC) -o $@


%.img: %.elf
    arm-none-eabi-objcopy $< -O binary $@

clean:
    rm -f build/*.elf build/*.img

(Be carefull, recipes have to start with a tab, not four spaces like here, it's important for make to understand your file, so copying-pasting won't work.)

You don't actually need to remove elf and img files before compiling, that's the GNU Make role to know if it has to rebuild or not according to file modification times.

Here it is, working:

$ tree
.
├── build
├── Makefile
└── src
    ├── boot.c
    ├── boot.s
    └── kernel.c

$ make
arm-none-eabi-gcc -g -O0 -mfpu=neon-vfpv4 -mfloat-abi=hard -march=armv7-a -mtune=cortex-a7 -nostartfiles -Wl,-T,linker.ld src/boot.c src/kernel.c src/boot.s -o build/kernel.elf
arm-none-eabi-objcopy build/kernel.elf -O binary build/kernel.img

$ tree
.
├── build
│   ├── kernel.elf
│   └── kernel.img
├── Makefile
└── src
    ├── boot.c
    ├── boot.s
    └── kernel.c

$ make
make: Nothing to be done for 'all'.

$ touch src/boot.c # If I touch a file, make will have to rebuild evrything:

$ make
arm-none-eabi-gcc -g -O0 -mfpu=neon-vfpv4 -mfloat-abi=hard -march=armv7-a -mtune=cortex-a7 -nostartfiles -Wl,-T,linker.ld src/boot.c src/kernel.c src/boot.s -o build/kernel.elf
arm-none-eabi-objcopy build/kernel.elf -O binary build/kernel.img

You should really take a look at the documentation which is really nice: http://www.gnu.org/software/make/manual/make.html you won't be able to ask on stackoverflow for any modification you'll need to do on your makefile, starting from this "bootstrap makefile" you should be able to modify it to learn step by step, with the documentation as a reference.

Upvotes: 6

Related Questions