Chapter 05 - Hongjayeong-02/Basic_SoC_implementation GitHub Wiki
05. Make Build System
Goal
Ch03์์ ์ง์ ์คํํ๋ GNU ARM Toolchain ๋ช ๋ น์ Makefile๋ก ์๋ํํ๊ณ , ํ์ผ ๊ฐ ์์กด์ฑ(dependency) ๊ณผ incremental build ์๋ฆฌ๋ฅผ ์ดํดํ๋ค.
1. Why Make?
Ch03์์๋ ์ง์ ๋ค์ ๊ณผ์ ์ ์ํํ๋ค.
main.c
โ
Compiler
โ
main.o
โ
Linker
โ
firmware.elf
โ
objcopy
โ
firmware.hex
์์ค ํ์ผ์ด ํ๋๋ผ๋ฉด ์ง์ ๋ช ๋ น์ด๋ฅผ ์ ๋ ฅํด๋ ํฐ ๋ฌธ์ ๊ฐ ์๋ค.
ํ์ง๋ง ์ค์ embedded project๋ ๋ณดํต:
main.c
gpio.c
uart.c
adc.c
timer.c
systick.c
...
์ฒ๋ผ ์ฌ๋ฌ ํ์ผ๋ก ๊ตฌ์ฑ๋๋ค.
์ด๋ ๋งค๋ฒ ๋ชจ๋ ํ์ผ์ ๋ค์ compileํ๋ฉด ๋ถํ์ํ ์๊ฐ์ด ๋ง์ด ๋ ๋ค.
Make๋ ํ์ผ ๊ฐ ๊ด๊ณ๋ฅผ ๊ธฐ์ตํ๊ณ :
๋ณ๊ฒฝ๋ ํ์ผ๊ณผ ๊ทธ ์ํฅ์ ๋ฐ๋ ๊ฒฐ๊ณผ๋ฌผ๋ง ๋ค์ ์์ฑํ๋ค.
2. Dependency
์๋ฅผ ๋ค์ด project๊ฐ ๋ค์๊ณผ ๊ฐ๋ค๊ณ ํ์.
main.c โโโโโโ main.o โโ
gpio.c โโโโโโ gpio.o โโผโโโ firmware.elf โโโ firmware.hex
uart.c โโโโโโ uart.o โโ
๋ง์ฝ gpio.c๋ง ์์ ํ๋ค๋ฉด:
gpio.c
โ
gpio.o
โ
firmware.elf
โ
firmware.hex
๋ง ๋ค์ ์์ฑํ๋ฉด ๋๋ค.
main.c, uart.c๋ ๋ฐ๋์ง ์์๊ธฐ ๋๋ฌธ์:
main.o
uart.o
๋ฅผ ๋ค์ compileํ ํ์๊ฐ ์๋ค.
์ด๊ฒ์ Incremental Build๋ผ๊ณ ํ๋ค.
3. How Does Make Know What Changed?
Make๋ ๊ธฐ๋ณธ์ ์ผ๋ก file์ timestamp๋ฅผ ๋น๊ตํ๋ค.
์:
gpio.c : 12:10
gpio.o : 12:05
source๊ฐ object file๋ณด๋ค ๋ ์ต๊ทผ์ ์์ ๋์์ผ๋ฏ๋ก:
gpio.c is newer than gpio.o
Make๋:
gpio.o must be rebuilt
๋ผ๊ณ ํ๋จํ๋ค.
๋ฐ๋๋ก:
gpio.c : 12:05
gpio.o : 12:10
์ด๋ฉด object๊ฐ ๋ ์ต์ ์ด๋ฏ๋ก ๋ค์ compileํ์ง ์๋๋ค.
4. Basic Makefile Rule
Makefile์ ๊ธฐ๋ณธ ๊ตฌ์กฐ:
target: dependency
command
์:
main.o: main.c
arm-none-eabi-gcc -c main.c -o main.o
์๋ฏธ:
main.o๊ฐ ์๊ฑฐ๋
main.c๊ฐ main.o๋ณด๋ค ์ต์ ์ด๋ฉด
โ
compiler ์คํ
โ
main.o ์์ฑ
5. Target and Dependency
๋ค์ rule:
firmware.elf: main.o gpio.o uart.o
arm-none-eabi-gcc main.o gpio.o uart.o -o firmware.elf
์ฌ๊ธฐ์:
Target
firmware.elf
Dependency:
main.o
gpio.o
uart.o
์ฆ object file ์ค ํ๋๋ผ๋ ELF๋ณด๋ค ์ต์ ์ด๋ฉด linker๋ฅผ ๋ค์ ์คํํ๋ค.
6. Variables
๋ช ๋ น์ด๋ฅผ ๊ณ์ ๋ฐ๋ณตํด์ ์ฐ๋ฉด Makefile์ด ๊ธธ์ด์ง๋ค.
๋ฐ๋ผ์ ๋ณ์๋ก ์ ์ํ๋ค.
CC := arm-none-eabi-gcc
OBJCOPY := arm-none-eabi-objcopy
SIZE := arm-none-eabi-size
์ด์ :
$(CC)
๋ ์ค์ ๋ก:
arm-none-eabi-gcc
๋ฅผ ์๋ฏธํ๋ค.
Example:
$(CC) -c main.c -o main.o
โ
arm-none-eabi-gcc -c main.c -o main.o
7. Why Use Variables?
Compiler๋ฅผ ๋ณ๊ฒฝํ๊ฑฐ๋ option์ ์์ ํ ๋ ํ ๊ณณ๋ง ๋ฐ๊พธ๋ฉด ๋๋ค.
๋์ ๊ตฌ์กฐ:
arm-none-eabi-gcc ...
arm-none-eabi-gcc ...
arm-none-eabi-gcc ...
arm-none-eabi-gcc ...
์ข์ ๊ตฌ์กฐ:
CC := arm-none-eabi-gcc
๊ทธ๋ฆฌ๊ณ :
$(CC)
๋ฅผ ์ฌ์ฌ์ฉํ๋ค.
8. Compiler Flags
Bare-metal Cortex-M4 build์ ์ฌ์ฉ๋๋ ๋ํ option:
CFLAGS := \
-mcpu=cortex-m4 \
-mthumb \
-O0 \
-g3 \
-Wall
๊ฐ ์๋ฏธ:
-mcpu=cortex-m4
โ Cortex-M4 target code ์์ฑ
-mthumb
โ Thumb instruction set ์ฌ์ฉ
-O0
โ optimization ๋นํ์ฑํ
-g3
โ debug information ์์ฑ
-Wall
โ ์ฃผ์ compiler warning ํ์ฑํ
9. CMSIS Build Options
CMSIS๋ฅผ ์ฌ์ฉํ๋ chapter์์๋ ์ถ๊ฐ option์ด ํ์ํ๋ค.
-mfloat-abi=hard
-mfpu=fpv4-sp-d16
-DSTM32F439xx
๊ทธ๋ฆฌ๊ณ include path:
-I...
๋ฅผ ์ถ๊ฐํ๋ค.
Example:
ifeq ($(USE_CMSIS),1)
CFLAGS += \
-mfloat-abi=hard \
-mfpu=fpv4-sp-d16 \
-DSTM32F439xx \
-I$(CMSIS_INC)
endif
USE_CMSIS=1์ผ ๋๋ง ์ด option๋ค์ด ์ถ๊ฐ๋๋ค.
10. Conditional Build
Make์์๋ ์กฐ๊ฑด๋ฌธ์ ์ฌ์ฉํ ์ ์๋ค.
ifeq ($(USE_CMSIS),1)
...
endif
์:
USE_CMSIS := 1
์ด๋ฉด CMSIS ๊ด๋ จ option์ ์ฌ์ฉํ๋ค.
๋ฐ๋๋ก:
USE_CMSIS := 0
์ด๋ฉด ํด๋น block์ ๊ฑด๋๋ด๋ค.
์ฆ ํ๋์ build system์ ์ฌ๋ฌ chapter์์ ์ฌ์ฌ์ฉํ ์ ์๋ค.
11. Pattern Rule
๊ฐ .c ํ์ผ๋ง๋ค ๋ค์์ ์ ๋ถ ์์ฑํ๋ ๊ฒ์ ๋นํจ์จ์ ์ด๋ค.
main.o: main.c
...
gpio.o: gpio.c
...
uart.o: uart.c
...
Make์ pattern rule์ ์ฌ์ฉํ๋ฉด ํ๋๋ก ์ฒ๋ฆฌํ ์ ์๋ค.
%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@
%๋ ๊ณตํต file name ๋ถ๋ถ์ ์๋ฏธํ๋ค.
๋ฐ๋ผ์:
main.c โ main.o
gpio.c โ gpio.o
uart.c โ uart.o
๋ชจ๋ ๊ฐ์ rule์ด ์ ์ฉ๋๋ค.
12. Automatic Variables
Make์๋ rule ๋ด๋ถ์์ ์ฌ์ฉํ ์ ์๋ ์๋ ๋ณ์๊ฐ ์๋ค.
$@
ํ์ฌ target.
main.o: main.c
์ด๋ฉด:
$@ = main.o
$<
์ฒซ ๋ฒ์งธ dependency.
$< = main.c
$^
๋ชจ๋ dependency.
์:
firmware.elf: main.o gpio.o uart.o
์ด๋ฉด:
$^ =
main.o gpio.o uart.o
13. Pattern Rule Expansion
Rule:
%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@
gpio.c๋ฅผ compileํ ๋ ์ค์ ๋ช
๋ น์:
arm-none-eabi-gcc \
$(CFLAGS) \
-c gpio.c \
-o gpio.o
๊ฐ ๋๋ค.
14. Linking Rule
์:
$(TARGET).elf: $(OBJS)
$(CC) $(CFLAGS) -T$(LDSCRIPT) $^ -o $@
๋ง์ฝ:
TARGET := ch05_make
๋ผ๋ฉด:
Target
โ ch05_make.elf
Dependencies:
main.o
startup.o
gpio.o
...
์ด๋ค.
Linker script:
-T$(LDSCRIPT)
๋ฅผ ํตํด memory layout์ ์ง์ ํ๋ค.
15. ELF to HEX
Link๊ฐ ๋๋ ๋ค:
$(TARGET).hex: $(TARGET).elf
$(OBJCOPY) -O ihex $< $@
๋ฅผ ์คํํ๋ค.
์ค์ ํ๋ฆ:
ch05_make.elf
โ
objcopy
โ
ch05_make.hex
16. Program Size
Build ํ:
$(SIZE) $@
๋ฅผ ์คํํ๋ฉด:
text
data
bss
ํฌ๊ธฐ๋ฅผ ํ์ธํ ์ ์๋ค.
Example:
text data bss
704 4 2564
์ด ๊ฐ์ firmware์ memory usage๋ฅผ ํ์ธํ ๋ ์ค์ํ๋ค.
17. Shared common.mk
18๊ฐ chapter์์ ๋์ผํ build logic์ ๋ฐ๋ณต ์์ฑํ๋ ๋์ ๊ณต์ฉ file๋ก ๋ถ๋ฆฌํ๋ค.
Structure:
common/
โโโ mk/
โโโ common.mk
chapter05/
โโโ Makefile
chapter06/
โโโ Makefile
chapter07/
โโโ Makefile
18. common.mk
์ค์ต ๊ตฌ์กฐ์ ํต์ฌ:
CC := arm-none-eabi-gcc
OBJCOPY := arm-none-eabi-objcopy
SIZE := arm-none-eabi-size
CFLAGS := \
-mcpu=cortex-m4 \
-mthumb \
-O0 \
-g3 \
-Wall
ifeq ($(USE_CMSIS),1)
CFLAGS += \
-mfloat-abi=hard \
-mfpu=fpv4-sp-d16 \
-I$(CMSIS_INC) \
-DSTM32F439xx
endif
%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@
$(TARGET).elf: $(OBJS)
$(CC) $(CFLAGS) \
-T$(LDSCRIPT) \
$^ \
-o $@
$(SIZE) $@
$(TARGET).hex: $(TARGET).elf
$(OBJCOPY) \
-O ihex \
$< \
$@
19. Chapter Makefile
๊ฐ chapter์์๋ ํ์ํ ์ ๋ณด๋ง ์์ฑํ๋ค.
Example:
TARGET := ch05_make
CH_SRCS := \
Src/main.c
USE_CMSIS := 1
include ../common/mk/common.mk
์ฆ:
Chapter Makefile
โ
"๋ฌด์์ ๋น๋ํ ๊ฒ์ธ๊ฐ?"
common.mk
โ
"์ด๋ป๊ฒ ๋น๋ํ ๊ฒ์ธ๊ฐ?"
๋ก ์ญํ ์ ๋ถ๋ฆฌํ๋ค.
20. Why Use include?
include ../common/mk/common.mk
๋ฅผ ์ฌ์ฉํ๋ฉด ๋ค๋ฅธ Makefile์ ๋ด์ฉ์ ํ์ฌ Makefile์ ํฌํจํ ์ ์๋ค.
์ฅ์ :
Compiler option
Link option
Pattern rule
objcopy rule
clean rule
๋ฑ์ ํ ๊ณณ์์ ๊ด๋ฆฌํ ์ ์๋ค.
์๋ฅผ ๋ค์ด compiler option์ ๋ณ๊ฒฝํด์ผ ํ ๋:
18๊ฐ Makefile ์์
๋์ :
common.mk 1๊ฐ ์์
๋ง ํ๋ฉด ๋๋ค.
21. Build Command
๊ธฐ๋ณธ build:
make
clean:
make clean
๋ค์ build:
make clean
make
22. Debug Build
์ค์ต์์๋ debug build์:
-O0
-g3
๋ฅผ ์ฌ์ฉํ๋ค.
ํน์ง:
- optimization ์ต์ํ
- source code์ assembly ๋์์ด ์ฌ์
- debugger ์ฌ์ฉ์ ์ ๋ฆฌ
- code size๊ฐ ๋ค์ ์ปค์ง ์ ์์
๊ธฐ๋ก๋ ๊ฒฐ๊ณผ:
text = 704
data = 4
bss = 2564
23. Release / Optimized Build
์ค์ต์์๋:
make DEBUG=0
์ผ๋ก optimization build๋ฅผ ๋น๊ตํ๋ค.
๋ํ์ ์ผ๋ก:
-O2
๋ฅผ ์ฌ์ฉํ๋ค.
๊ธฐ๋ก๋ ๊ฒฐ๊ณผ:
text = 668
data = 4
bss = 2564
Debug์ ๋น๊ต:
704 bytes
โ
668 bytes
.text๊ฐ:
36 bytes
์ค์๋ค.
24. Why Does Optimization Reduce Code Size?
Compiler๊ฐ ๋ค์ ์์ ์ ํ ์ ์๊ธฐ ๋๋ฌธ์ด๋ค.
Remove unnecessary instructions
Simplify expressions
Optimize branches
Optimize loops
Reuse registers
Inline or reorganize code
๋ฐ๋ผ์ ๊ฐ์ C source๋ผ๋:
Compiler Option
โ
Different Assembly
โ
Different Machine Code
๊ฐ ๋ ์ ์๋ค.
25. Source Code Is Not the Final Program
์ค์ํ ๊ฐ๋ :
C Source
โ
Compiler Options
โ
Optimization
โ
Assembly
โ
Machine Code
์ฆ MCU๊ฐ ์ค์ ๋ก ์คํํ๋ ๊ฒ์ C source๊ฐ ์๋๋ผ compiler๊ฐ ์์ฑํ machine code๋ค.
๊ทธ๋์ embedded debugging์์๋:
Compiler Option
Optimization
Linker Script
Startup
๊น์ง ๊ฐ์ด ๋ด์ผ ํ๋ค.
26. make clean
make clean์ ๊ธฐ์กด build artifact๋ฅผ ์ญ์ ํ๋ค.
์:
clean:
rm -f *.o *.elf *.hex
์ ํ์ํ๊ฐ?
๊ฐ๋ dependency ์ค์ ์ด ์๋ชป๋๊ฑฐ๋ build artifact๊ฐ stale ์ํ๊ฐ ๋๋ฉด ๊ธฐ์กด object file ๋๋ฌธ์ ์์ํ์ง ๋ชปํ ๊ฒฐ๊ณผ๊ฐ ๋์ฌ ์ ์๋ค.
์ด๋:
make clean
make
์ผ๋ก ์ฒ์๋ถํฐ ๋ค์ buildํ๋ฉด ๋ฌธ์ ๋ฅผ ๋ถ๋ฆฌํ๊ธฐ ์ฝ๋ค.
27. Parallel Build
Make๋:
make -j
๋ก ์ฌ๋ฌ compile job์ ๋์์ ์คํํ ์ ์๋ค.
์:
main.c โ main.o
gpio.c โ gpio.o
uart.c โ uart.o
๋ ์๋ก ๋ ๋ฆฝ์ ์ด๋ฏ๋ก ๋์์ compileํ ์ ์๋ค.
28. Current Build-System Limitation
ํ์ฌ ํ์ต์ฉ ๊ตฌ์กฐ์์๋ ์ผ๋ถ ๊ณต์ฉ object file์ ์ฌ๋ฌ chapter๊ฐ ๊ณต์ ํ ์ ์๋ค.
์:
common/
โโโ startup.o
์ด ์ํ์์ ์ฌ๋ฌ chapter๋ฅผ ๋์์:
make -j
ํ๋ฉด ๊ฐ์ object๋ฅผ ๋์์ ๊ฐฑ์ ํ ๊ฐ๋ฅ์ฑ์ด ์๋ค.
๋ ์์ ํ ๊ตฌ์กฐ:
build/
โโโ ch05/
โโโ ch06/
โโโ ch07/
โโโ ...
์ฒ๋ผ chapter๋ง๋ค ๋ ๋ฆฝ build directory๋ฅผ ๋๋ ๊ฒ์ด๋ค.
29. Recommended Project Structure
BareMetal/
โ
โโโ common/
โ โโโ inc/
โ โโโ src/
โ โโโ startup/
โ โโโ mk/
โ โโโ common.mk
โ
โโโ Chapter05/
โ โโโ Src/
โ โโโ Makefile
โ
โโโ Chapter06/
โ โโโ Src/
โ โโโ Makefile
โ
โโโ build/
๊ณตํต code์ chapter-specific code๋ฅผ ๋ถ๋ฆฌํ๋ฉด ์ ์ง๋ณด์๊ฐ ์ฌ์์ง๋ค.
30. Build Flow
์ ์ฒด ํ๋ฆ:
make
โ
โผ
Read Makefile
โ
โผ
Read Dependencies
โ
โผ
Check File Timestamps
โ
โโโ unchanged โ reuse object
โ
โโโ changed โ recompile
โ
โผ
Object Files
โ
โผ
Link
โ
โผ
ELF
โ
โผ
objcopy
โ
โผ
HEX
31. Make vs Compiler
ํท๊ฐ๋ฆฌ์ง ๋ง์์ผ ํ ์ :
Make
โ Compiler
Make๋ compiler๊ฐ ์๋๋ค.
Make์ ์ญํ :
Build Orchestration
Dependency Management
Command Automation
์ค์ C compile:
arm-none-eabi-gcc
๊ฐ ์ํํ๋ค.
์ฆ:
Make
โ
calls GCC
โ
calls assembler/linker
๊ตฌ์กฐ๋ค.
32. IDE Build System
STM32CubeIDE์์:
Project โ Build
๋ฅผ ๋๋ฌ๋ ๋ด๋ถ์ ์ผ๋ก ๋น์ทํ build system์ด ๋์ํ๋ค.
์ฆ:
IDE Button
โ
Make / Build System
โ
GNU ARM Toolchain
โ
ELF / HEX
์ด๋ค.
Ch03์์ ์ง์ ๋ช ๋ น์ ์คํํ๊ณ Ch05์์ Make๋ฅผ ๋ฐฐ์ฐ๋ ์ด์ ๋ IDE ๋ด๋ถ ๋์์ ์ดํดํ๊ธฐ ์ํด์๋ค.
33. Verification
Build
make clean
make
ํ์ธ:
Compile
Link
objcopy
size
๊ฐ ์์๋๋ก ์คํ๋๋์ง ํ์ธํ๋ค.
Rebuild Test
์๋ฌด file๋ ์์ ํ์ง ์๊ณ ๋ค์:
make
์คํํ๋ค.
์ ์ dependency๋ผ๋ฉด ์ด๋ฏธ ์ต์ ์ธ target์ ๋ถํ์ํ๊ฒ compileํ์ง ์๋๋ค.
Incremental Build Test
main.c์ ์์ ์์ ํ:
make
์คํํ๋ค.
ํ์ธ:
main.c
โ
main.o rebuild
โ
ELF relink
โ
HEX regenerate
Optimization Comparison
Debug:
make clean
make
Optimized:
make clean
make DEBUG=0
๋น๊ต:
Debug
text = 704
Optimized
text = 668
34. Screenshot to Add
์ฌ์ค์ตํ ๋ ๋ค์ ๊ฒฐ๊ณผ๋ฅผ ์บก์ฒํ๋ค.
Debug Build
images/ch05_debug_build.png
๋ณด์ฌ์ผ ํ ๊ฒ:
-O0
Build PASS
text/data/bss
Optimized Build
images/ch05_release_build.png
๋ณด์ฌ์ผ ํ ๊ฒ:
-O2
Build PASS
text/data/bss
Incremental Build
images/ch05_incremental_build.png
ํ ํ์ผ๋ง ์์ ํ ๋ค ํด๋น object๋ง ๋ค์ compile๋๋ ๋ชจ์ต์ ์บก์ฒํ๋ฉด ์ข๋ค.
35. Key Questions
Q1. What is Make?
Build dependency๋ฅผ ๊ด๋ฆฌํ๊ณ ํ์ํ command๋ฅผ ์๋ ์คํํ๋ build tool.
Q2. Is Make a compiler?
No.
Make
โ decides what should be built
GCC
โ actually compiles code
Q3. What is incremental build?
๋ณ๊ฒฝ๋ source์ ๊ทธ ์ํฅ์ ๋ฐ๋ target๋ง ๋ค์ buildํ๋ ๋ฐฉ์.
Q4. What does $@ mean?
ํ์ฌ rule์ target.
Q5. What does $< mean?
์ฒซ ๋ฒ์งธ dependency.
Q6. What does $^ mean?
๋ชจ๋ dependency.
Q7. What does %.o: %.c mean?
๋ชจ๋ .c file์ ๋์ผํ rule๋ก .o file๋ก compileํ๋ pattern rule.
Q8. Why use common.mk?
์ฌ๋ฌ project/chapter์์ ๋์ผํ build rule๊ณผ compiler option์ ์ฌ์ฌ์ฉํ๊ธฐ ์ํด.
Q9. Why does -O2 change the binary?
Compiler๊ฐ C code๋ฅผ ๋ ํจ์จ์ ์ธ machine code๋ก ๋ณํํ๊ธฐ ๋๋ฌธ์ด๋ค.
Q10. Why use make clean?
๊ธฐ์กด object/binary๋ฅผ ์ญ์ ํ๊ณ ์์ ํ ์ build๋ฅผ ์ํํ๊ธฐ ์ํด.
36. Key Takeaways
Source Files
โ
Dependency Graph
โ
Make
โ
GNU ARM Toolchain
โ
Object Files
โ
Link
โ
ELF
โ
HEX
ํต์ฌ์ ๋ค์๊ณผ ๊ฐ๋ค.
- Make๋ compiler๊ฐ ์๋๋ผ build automation tool์ด๋ค.
- Make๋ file timestamp์ dependency๋ฅผ ์ด์ฉํ๋ค.
- ๋ณ๊ฒฝ๋ ๋ถ๋ถ๋ง ๋ค์ buildํ๋ ๊ฒ์ด incremental build๋ค.
- Pattern rule์ ์ด์ฉํ๋ฉด ์ฌ๋ฌ source file์ ํ๋์ rule๋ก ์ฒ๋ฆฌํ ์ ์๋ค.
$@,$<,$^๋ Makefile ์์ฑ์์ ์์ฃผ ์ฌ์ฉํ๋ automatic variable์ด๋ค.common.mk๋ฅผ ์ฌ์ฉํ๋ฉด build logic์ ์ฌ์ฌ์ฉํ ์ ์๋ค.- Compiler option์ ๋ฐ๋ผ ๊ฐ์ C source์์๋ ๋ค๋ฅธ machine code๊ฐ ์์ฑ๋๋ค.
- Embedded project์์๋ source code๋ฟ ์๋๋ผ build system๊น์ง ์ดํดํด์ผ debugging์ด ์ฌ์์ง๋ค.