对gcc前缀赋值
ifndef GCCPREFIX
#ifndef 意思就是if not define, 还有一个关键词是ifdef
#:= 就是对左边的变量进行赋值
#shell是一个makefile函数,意思就是后边执行的shell语句正确结果赋值给变量
#正确结果就是默认由1标准输出描述符输出
GCCPREFIX := $(shell if i386-elf-objdump -i 2>&1 | grep '^elf32-i386$$' >/dev/null 2>&1; \
then echo 'i386-elf-'; \
elif objdump -i 2>&1 | grep 'elf32-i386' >/dev/null 2>&1; \
then echo ''; \
else echo "***" 1>&2; \
echo "*** Error: Couldn't find an i386-ucore-elf version of GCC/binutils." 1>&2; \
echo "*** Is the directory with i386-ucore-elf-gcc in your PATH?" 1>&2; \
echo "*** If your i386-ucore-elf toolchain is installed with a command" 1>&2; \
echo "*** prefix other than 'i386-ucore-elf-', set your GCCPREFIX" 1>&2; \
echo "*** environment variable to that prefix and run 'make' again." 1>&2; \
echo "*** To turn off this error, run 'gmake GCCPREFIX= ...'." 1>&2; \
echo "***" 1>&2; exit 1; fi)
endif
makfie调试办法
$(warning $(XXX)) 打印变量的值
1,使用info/warning/error增加调试信息
方法1: $(info, “here add the debug info”)
但是此不能打印出.mk的行号
方法2: $(warning “here add the debug info”)
方法3: $(error “error: this will stop the compile”)
这个可以停止当前makefile的编译
方法4: 打印变量的值
$(info $(TARGET_DEVICE) )
2,使用echo增加调试信息(echo只能在target:后面的语句中使用,且前面是个TAB)
方法1: @echo “start the compilexxxxxxxxxxxxxxxxxxxxxxx”
方法2: @echo $(files)
对以上部分进行调试
ifndef GCCPREFIX
GCCPREFIX := $(shell if i386-elf-objdump -i 2>&1 | grep '^elf32-i386$$' >/dev/null 2>&1; \
then echo 'i386-elf-' 2>&1; \
elif objdump -i 2>&1 | grep 'elf32-i386' >/dev/null 2>&1; \
then echo ''; \
else echo "***" 1>&2 ; \
echo "*** Error: Couldn't find an i386-ucore-elf version of GCC/binutils." 1>&2; \
echo "*** Is the directory with i386-ucore-elf-gcc in your PATH?" 1>&2; \
echo "*** If your i386-ucore-elf toolchain is installed with a command" 1>&2; \
echo "*** prefix other than 'i386-ucore-elf-', set your GCCPREFIX" 1>&2; \
echo "*** environment variable to that prefix and run 'make' again." 1>&2; \
echo "*** To turn off this error, run 'gmake GCCPREFIX= ...'." 1>&2; \
echo "***" 1>&2; exit 1; fi)
endif
all:
$(info $(GCCPREFIX))
注意调试的时候如果想要输出某个值的话需要加上all或者其他词,这样才会有输出 写入文件test.mk 执行
make -f test.mk
就能看到结果
Shell重定向 &>file、2>&1、1>&2 、/dev/null的区别
在shell脚本中,默认情况下,总是有三个文件处于打开状态,标准输入(键盘输入)、标准输出(输出到屏幕)、标准错误(也是输出到屏幕),它们分别对应的文件描述符是0,1,2 。
默认为标准输出重定向,与 1> 相同
2>&1 意思是把 标准错误输出 重定向到 标准输出.
&>file 意思是把标准输出 和 标准错误输出 都重定向到文件file中
/dev/null是一个文件,这个文件比较特殊,所有传给它的东西它都丢弃掉
举例说明:
当前目录只有一个文件 a.txt.
[root@redhat box]# ls
a.txt
[root@redhat box]# ls a.txt b.txt
ls: b.txt: No such file or directory
由于没有b.txt这个文件, 于是返回错误值, 这就是所谓的2输出
a.txt 而这个就是所谓的1输出
[root@redhat box]# ls a.txt b.txt 1>file.out 2>file.err
执行后,没有任何返回值. 原因是, 返回值都重定向到相应的文件中了,而不再前端显示
[root@redhat box]# cat file.out
a.txt
[root@redhat box]# cat file.err
ls: b.txt: No such file or directory
一般来说, “1>” 通常可以省略成 “>”.
即可以把如上命令写成: ls a.txt b.txt >file.out 2>file.err
有了这些认识才能理解 “1>&2” 和 “2>&1”.
1>&2 正确返回值传递给2输出通道 &2表示2输出通道
如果此处错写成 1>2, 就表示把1输出重定向到文件2中.
2>&1 错误返回值传递给1输出通道, 同样&1表示1输出通道.
举个例子.
[root@redhat box]# ls a.txt b.txt 1>file.out 2>&1
[root@redhat box]# cat file.out
ls: b.txt: No such file or directory
a.txt
现在, 正确的输出和错误的输出都定向到了file.out这个文件中, 而不显示在前端.
补充下, 输出不只1和2, 还有其他的类型, 这两种只是最常用和最基本的.
例如:
rm -f $(find / -name core) &> /dev/null,/dev/null是一个文件,这个文件比较特殊,所有传给它的东西它都丢弃掉。
例如:
注意,为了方便理解,必须设置一个环境使得执行grep da *命令会有正常输出和错误输出,然后分别使用下面的命令生成三个文件:
grep da * > greplog1
grep da * > greplog2 1>&2
grep da * > greplog3 2>&1 //grep da * 2> greplog4 1>&2
结果一样
#查看greplog1会发现里面只有正常输出内容
#查看greplog2会发现里面什么都没有#查看greplog3会发现里面既有正常输出内容又有错误输出内容
对qemu进行平台匹配赋值
# try to infer the correct QEMU
ifndef QEMU
QEMU := $(shell if which qemu > /dev/null; \
then echo 'qemu'; exit; \
elif which qemu-system-i386 > /dev/null; \
then echo 'qemu-system-i386'; exit; \
else \
echo "***" 1>&2; \
echo "*** Error: Couldn't find a working QEMU executable." 1>&2; \
echo "*** Is the directory containing the qemu binary in your PATH" 1>&2; \
echo "***" 1>&2; exit 1; fi)
endif
if语句和上面类似,但是shell语句发生了变化 使用了which qemu 这个在shell中就是查找是否有这个命令,
如果没有的话,就没有任何输出,如果有的话,就会输出其命令绝对路径