0% found this document useful (0 votes)
50 views3 pages

Guide To Make For ARM Cross-Development

This document provides an introduction to using makefiles for cross-development on ARM architecture. It explains the basics of makefile syntax and rules, demonstrating a simple makefile for building a binary. It then expands on this with macros, wildcards, and additional rules to make the makefile more efficient and reusable across projects. The key aspects of makefiles - rules, dependencies, recipes and macros - are broken down with examples to help the reader understand how makefiles work.

Uploaded by

Nan Roh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views3 pages

Guide To Make For ARM Cross-Development

This document provides an introduction to using makefiles for cross-development on ARM architecture. It explains the basics of makefile syntax and rules, demonstrating a simple makefile for building a binary. It then expands on this with macros, wildcards, and additional rules to make the makefile more efficient and reusable across projects. The key aspects of makefiles - rules, dependencies, recipes and macros - are broken down with examples to help the reader understand how makefiles work.

Uploaded by

Nan Roh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

16/1/2018 CS107E Guide to Make for ARM cross-development

Guide to Make for ARM cross-development


Written for CS107E by Anna Zeng

Make is a tool that automates building executable programs; a make le is a le that tells make what to do in order
to build the programs you want. As you will see soon enough, they make life as a computer science student a
whole lot smoother!

In this guide, we’ll learn about using make les for cross-development on the ARM architecture. We will brie y
review make le syntax and usage. For more information about make les, check out the CS107 Guide to Make les
(http://cs107.stanford.edu/guide/make.html) and other resources on the bottom of the page.

Below is a simple make le used to build a binary for the ARM processor.

NAME = blink

CFLAGS = -std=c99 -Og -g -Wall -ffreestanding

all: $(NAME).bin

%.bin: %.o
$(ARM)-objcopy $< -O binary $@

%.o: %.c
$(ARM)-gcc $(CFLAGS) -c $< -o $@

%.o: %.s
$(ARM)-as $(CFLAGS) -o $< $@

%.list: %.o
$(ARM)-objdump -d $< > $@

install: $(NAME).bin
rpi-install.py $<

clean:
rm -f *.o *.bin *.list

Now, this Make le may look a bit cryptic at rst! Let’s try breaking it down and see if this makes sense.

Make le Basics: Rules


From lecture, we were introduced to Make les as an improvement on the doit script; our rst Make le looked a
little something like:

all: button.bin

button.bin: button.c
arm-none-eabi-gcc -Og -g -Wall -std=c99 -ffreestanding -c button.c -o button.o
arm-none-eabi-objcopy button.o -O binary button.bin
arm-none-eabi-objdump button.o -d > button.list

clean:
rm -f *.list *.bin *.o

Rules are written in terms of “you require dependencies on the right-hand-side to satisfy the need for our target
on the left-hand-side.” Here, we indicate: by default, make all; to do that, make button.bin .

http://cs107e.github.io/guides/make/ 1/3
16/1/2018 CS107E Guide to Make for ARM cross-development

all: button.bin

This brings us to the next rule, which tells us how to make button.bin . You may interpret this as requiring certain
ingredients (dependencies on the right-hand-side) to create the thing you want (target on the left-hand-side).

button.bin: button.c

The text which immediately follow the rule, or recipe for the rule, are commands necessary to turn the ingredients
( button.c in this case) into the nal product ( button.bin in this case). We also throw in a comment to explain the
additional ags included with our call to arm-none-eabi-gcc .

# Here, we set compile flags for gcc:


# -std=c99 use the c99 standard
# -Og generate optimized code designed for debugging
# -g add debugging information
# -Wall give warnings about *all* issues
# -ffreestanding generate code assuming no operating system

button.bin: button.c
arm-none-eabi-gcc -Og -g -Wall -std=c99 -ffreestanding -c button.c -o button.o
arm-none-eabi-objcopy button.o -O binary button.bin
arm-none-eabi-objdump button.o -d > button.list

The line below indicates what should happen when we make clean ; the keyword clean tells Make to run the
command below.

clean:
rm -f *.list *.bin *.o

Make le Magic: Macros & More Rules


After copy-pasting and editing every Make le each time a new program is created, we have nally decided that it’s
time to channel a little Dawson Engler and become more e cient. After all, Make les are written for convenience!

NAME = blink
ARM = arm-none-eabi

CFLAGS = -std=c99 -Og -g -Wall -ffreestanding

all: $(NAME).bin

$(NAME).bin: $(NAME).c
$(ARM)-gcc $(CFLAGS) -c $(NAME).c -o $(NAME).o
$(ARM)-objcopy $(NAME).o -O binary $(NAME).bin
$(ARM)-objdump $(NAME).o -d > $(NAME).list

clean:
rm -f *.list *.bin *.o

So we’ve just added three macros up top. They’re similar to variables in that they put text in where you expect
them to go. Be sure to use the $(<macro_name>) syntax to access the value of the macro and allow string
concatenation. (Did you see what we did there with the ARM macro?) Phew, this saves us a lot of visual space!

Now, let’s introduce a few special rules here to replace our one rule for blink.bin .

http://cs107e.github.io/guides/make/ 2/3
16/1/2018 CS107E Guide to Make for ARM cross-development

# This is the rule for compiling a C program to make an object file.


%.o: %.c
$(ARM)-gcc $(CFLAGS) -c $< -o $@

# This is the rule for converting an assembly language program


# to machine code in an object file.
%.o: %.s
$(ARM)-as $(CFLAGS) -o $< $@

# This is predefined rule to creating a *.bin file from a *.o file,


# in this case, blink.bin from blink.o
%.bin: %.o
$(ARM)-objcopy $< -O binary $@

# This is the rule for disassembling the object file


# to make a listing
%.list: %.o
$(ARM)-objdump -d $< > $@

The key to guring out what they do is to know:

% is a wildcard symbol when used in a rule; %.o for example matches any le that ends with .o
$@ refers to the left part of the rule, before the :
$< refers to the rst element in the right part of the rule, after the :

So, really, you can think of the make le as a big cookbook that culminates in the program you ultimately wish to
create.

For convenience, we can also throw in another rule so we don’t have to type in rpi-install.py blink.bin every time
we want to run our program on the Pi.

# The install target uploads freshly made binary image to rpi bootloader
install: $(NAME).bin
rpi-install.py $<

Congratulations! You are now a make le wizard!✨

If you’d like to learn more, check out a Make le tutorial (http://www.opussoftware.com/tutorial/TutMake le.htm),
another Make le tutorial (http://www.delorie.com/djgpp/doc/ug/larger/make les.html), the CS107 Guide to
Make les (http://cs107.stanford.edu/guide/make.html), and the GNU Documentation about Compiler Options
(https://gcc.gnu.org/onlinedocs/gcc-4.2.2/gcc/C-Dialect-Options.html).

http://cs107e.github.io/guides/make/ 3/3

You might also like