每次添加了一个文件就要改Makefile,效率太低了,绝对不能容忍!所以必须写一个通用的Makefile,以下的这个Makefile是从u-boot-1.1.6的编译部分修改而来,效果还不错。
一、通用Makefile
-
CROSS_COMPILE = /opt/EmbedSky/crosstools_3.4.5_softfloat/gcc-3.4.5-glibc-2.3.6/arm-linux/bin/arm-linux-
-
-
AS = $(CROSS_COMPILE)as
-
LD = $(CROSS_COMPILE)ld
-
CC = $(CROSS_COMPILE)gcc
-
CPP = $(CC) -E
-
AR = $(CROSS_COMPILE)ar
-
NM = $(CROSS_COMPILE)nm
-
STRIP = $(CROSS_COMPILE)strip
-
OBJCOPY = $(CROSS_COMPILE)objcopy
-
OBJDUMP = $(CROSS_COMPILE)objdump
-
RANLIB = $(CROSS_COMPILE)RANLIB
-
-
TEXT_BASE = 0x33D80000
-
gccincdir := $(shell $(CC) -print-file-name=include)
-
PLATFORM_LIBS += -L $(shell dirname `$(CC) $(CFLAGS) -print-libgcc-file-name`) -lgcc
-
PLATFORM_RELFLAGS += -fno-strict-aliasing -fno-common -ffixed-r8 -msoft-float -malignment-traps
-
PLATFORM_CPPFLAGS += -DCONFIG_ARM -D__ARM__ -march=armv4 -mapcs-32
-
-
CFLAGS += $(DBGFLAGS) $(OPTFLAGS)
-
CFLAGS += $(PLATFORM_RELFLAGS)
-
CFLAGS += -D__KERNEL__ -DTEXT_BASE=$(TEXT_BASE)
-
CFLAGS += -I$(CURDIR)/include
-
CFLAGS += -fno-builtin -ffreestanding -nostdinc -isystem $(gccincdir) -pipe
-
CFLAGS += $(PLATFORM_CPPFLAGS)
-
CFLAGS += -Wall -Wstrict-prototypes
-
-
LDSCRIPT := $(CURDIR)/u-boot.lds
-
AFLAGS := $(AFLAGS_DEBUG) -D__ASSEMBLY__ $(CFLAGS)
-
LDFLAGS += -Bstatic -T $(LDSCRIPT) -Ttext $(TEXT_BASE) $(PLATFORM_LDFLAGS)
-
-
OBJS := start.o
-
ALL = u-boot.bin System.map u-boot.dis
-
-
all: $(ALL)
-
-
u-boot.bin: u-boot
-
$(OBJCOPY) ${OBJCFLAGS} -O binary $< $@
-
-
u-boot.dis: u-boot
-
$(OBJDUMP) -d $< > $@
-
-
u-boot: $(OBJS) $(LDSCRIPT)
-
$(LD) $(LDFLAGS) $(OBJS) -Map u-boot.map -o u-boot
-
-
System.map: u-boot
-
@$(NM) $< | \
-
grep -v '\(compiled\)\|\(\.o$$\)\|\( [aUw] \)\|\(\.\.ng$$\)\|\(LASH[RL]DI\)' | \
-
sort > System.map
-
-
%.o: %.S
-
$(CC) $(AFLAGS) -c -o $@ $<
-
%.o: %.c
-
$(CC) $(CFLAGS) -c -o $@ $<
-
-
clean:
-
find $(OBJTREE) -type f \
-
\( -name 'core' -o -name '*.bak' -o -name '*~' \
-
-o -name '*.o' -o -name '*.a' \) -print \
-
| xargs rm -f
-
rm -f $(ALL) u-boot System.map u-boot.map
每次添加一个文件时,只需把OBJS修改一下就可以了。
里面还有几个编译选项不太明白是什么意思,先放着,等有时间查一下手册
其中L15:gccincdir := $(shell $(CC) -print-file-name=include) ;是要显示当前gcc所查找include的完整路径: /opt/EmbedSky/crosstools_3.4.5_softfloat/gcc-3.4.5-glibc-2.3.6/arm-linux/lib/gcc/arm-linux/3.4.5/include
L16 $(CC) $(CFLAGS) -print-libgcc-file-name ;要显示libgcc的完整路径
/opt/EmbedSky/crosstools_3.4.5_softfloat/gcc-3.4.5-glibc-2.3.6/arm-linux/lib/gcc/arm-linux/3.4.5/libgcc.a
2. u-boot.lds链接脚本
-
OUTPUT_FORMAT("elf32-littlearm", "elf32-littlearm", "elf32-littlearm")
-
/*OUTPUT_FORMAT("elf32-arm", "elf32-arm", "elf32-arm")*/
-
OUTPUT_ARCH(arm)
-
ENTRY(_start)
-
SECTIONS
-
{
-
. = 0x00000000;
-
-
. = ALIGN(4);
-
.text :
-
{
-
start.o (.text)
-
*(.text)
-
}
-
-
. = ALIGN(4);
-
.rodata : { *(.rodata) }
-
-
. = ALIGN(4);
-
.data : { *(.data) }
-
-
. = ALIGN(4);
-
.got : { *(.got) }
-
-
. = .;
-
__u_boot_cmd_start = .;
-
.u_boot_cmd : { *(.u_boot_cmd) }
-
__u_boot_cmd_end = .;
-
-
. = ALIGN(4);
-
__bss_start = .;
-
.bss : { *(.bss) }
-
_end = .;
-
}
链接地址可以直接在Makefile中修改:TEXT_BASE = 0x33D80000
注意:代码中的tab在粘贴的时候可能丢失了,上传一个, 用的时候将Makefile.txt改名为Makefile就可以了。
二、中间文件输出到单独的目录
2.1 修改Makefile将生成的中间文件都放在单独的目录下,这样代码和编译出来的东东都很清晰了。
-
CROSS_COMPILE = /opt/EmbedSky/crosstools_3.4.5_softfloat/gcc-3.4.5-glibc-2.3.6/arm-linux/bin/arm-linux-
-
-
AS = $(CROSS_COMPILE)as
-
LD = $(CROSS_COMPILE)ld
-
CC = $(CROSS_COMPILE)gcc
-
CPP = $(CC) -E
-
AR = $(CROSS_COMPILE)ar
-
NM = $(CROSS_COMPILE)nm
-
STRIP = $(CROSS_COMPILE)strip
-
OBJCOPY = $(CROSS_COMPILE)objcopy
-
OBJDUMP = $(CROSS_COMPILE)objdump
-
RANLIB = $(CROSS_COMPILE)RANLIB
-
-
TEXT_BASE = 0x33D80000
-
gccincdir := $(shell $(CC) -print-file-name=include)
-
PLATFORM_LIBS += -L $(shell dirname `$(CC) $(CFLAGS) -print-libgcc-file-name`) -lgcc
-
PLATFORM_RELFLAGS += -fno-strict-aliasing -fno-common -ffixed-r8 -msoft-float -malignment-traps
-
PLATFORM_CPPFLAGS += -DCONFIG_ARM -D__ARM__ -march=armv4 -mapcs-32
-
-
#OPTFLAGS= -O0
-
#OPTFLAGS= -O1
-
#OPTFLAGS= -O2
-
#OPTFLAGS= -O3
-
OPTFLAGS= -Os
-
CFLAGS += $(DBGFLAGS) $(OPTFLAGS)
-
CFLAGS += $(PLATFORM_RELFLAGS)
-
CFLAGS += -D__KERNEL__ -DTEXT_BASE=$(TEXT_BASE)
-
CFLAGS += -I$(CURDIR)/include
-
CFLAGS += -fno-builtin -ffreestanding -nostdinc -isystem $(gccincdir) -pipe
-
CFLAGS += $(PLATFORM_CPPFLAGS)
-
CFLAGS += -Wall -Wstrict-prototypes
-
-
LDSCRIPT := $(CURDIR)/u-boot.lds
-
AFLAGS := $(AFLAGS_DEBUG) -D__ASSEMBLY__ $(CFLAGS)
-
LDFLAGS += -Bstatic -T $(LDSCRIPT) -Ttext $(TEXT_BASE) $(PLATFORM_LDFLAGS)
-
-
OBJDIR = ./obj/
-
# Attempt to create a output directory.
-
$(shell [ -d ${OBJDIR} ] || mkdir -p ${OBJDIR})
-
-
SRCS := $(wildcard *.c *.S)
-
OBJS := $(addprefix $(OBJDIR), $(patsubst %.S, %.o, $(patsubst %.c, %.o, $(SRCS))))
-
-
ALL = $(OBJDIR)u-boot.bin $(OBJDIR)System.map $(OBJDIR)u-boot.dis
-
-
all: $(ALL)
-
$(OBJDIR)u-boot.bin: $(OBJDIR)u-boot
-
$(OBJCOPY) ${OBJCFLAGS} -O binary $< $@
-
-
$(OBJDIR)u-boot.dis: $(OBJDIR)u-boot
-
$(OBJDUMP) -d $< > $@
-
-
$(OBJDIR)u-boot: $(OBJS) $(LDSCRIPT)
-
$(LD) $(LDFLAGS) $(OBJS) $(PLATFORM_LIBS) -Map $@.map -o $@
-
-
$(OBJDIR)System.map: $(OBJDIR)u-boot
-
@$(NM) $< | \
-
grep -v '\(compiled\)\|\(\.o$$\)\|\( [aUw] \)\|\(\.\.ng$$\)\|\(LASH[RL]DI\)' | \
-
sort > $@
-
-
$(OBJDIR)%.o: %.S
-
$(CC) $(AFLAGS) -c -o $@ $<
-
$(OBJDIR)%.o: %.c
-
$(CC) $(CFLAGS) -c -o $@ $<
-
-
clean:
-
find $(OBJTREE) -type f \
-
\( -name 'core' -o -name '*.bak' -o -name '*~' \
-
-o -name '*.o' -o -name '*.a' -o -name '*.map' \) -print \
-
| xargs rm -f
-
rm -f $(ALL) $(OBJDIR)u-boot $(OBJDIR)System.map $(OBJDIR)u-boot.map
2.2 连接脚本也要改: 将start.o 改为 obj/start.o
2.3 Makefile 中的第41-42行
SRCS := $(wildcard *.c *.S)
OBJS := $(addprefix $(OBJDIR), $(patsubst %.S, %.o, $(patsubst %.c, %.o, $(SRCS))))
SRCS中是 sdram.c start.S 既有.S文件,又有.c文件,OBJS要把.c和.S全部者替换成.o,但是Makefile中的patsubst函数一次只能替换一个,(即一次只能将.S替换成.o,或者将.c替换成.o)。为了在一个语句中替换两次需要把patsubst执行后的结果,交给patsubst再次处理即可。
阅读(2832) | 评论(1) | 转发(2) |