7 整理make环境(harib24g)
也许是笔者的电脑性能比较差,最近感觉“make run”需要的时间很长。另外,操作系统、应用程序和库的源文件都混在一起,看起来非常混乱。因此,我们来把它们各归各位吧。
■■■■■
我们先从操作系统的部分开始吧。在harib24g中创建一个名为“haribote”的目录,将操作系统核心的源代码以及Makefile移动到这里。哦,清爽多了。这样一来目录结构就多了一层,我们也得对Makefile进行相应的修改。这个Makefile只是将原来的文件稍作修改而已,具体的代码我们就省略了哦。
现在如果我们要只make操作系统的话,只要双击haribote目录中的!cons.bat文件,并输入“make”就可以了。
由于这个目录中只包含操作系统核心部分,可以使用的命令只有“make”、“make clean”以及“make src_only”。如果要执行相当于 “make run”的操作的话,则需要在harib24g中输入“make run”来执行(稍后我们会详细讲解)。
■■■■■
接下来是库,我们创建一个名为“apilib”的目录,将库相关的源代码以及Makefile移动进去。唔,看上去也很清爽,真不错。这里的Makefile也是将原来的文件稍作修改而已,具体代码省略。
apilib中的命令也只有“make”、“make clean”以及“make src_only”,当然,对于库我们也不需要执行“make run”吧(笑)。
■■■■■
接下来轮到应用程序了。应用程序的Makefile比较短而且很有意思,我们还是写出来吧。下面这个是a.hrb的Makefile。
a.hrb用的Makefile
APP = a
STACK = 1k
MALLOC = 0k
include ../app_make.txt
怎么样?算上空行也才只有5行。不过话说回来,其实app_make.txt还是很长的,实际上也没有变短,只是“看上去变短了”而已。之所以要用include,是因为所有的应用程序的Makefile都大同小异,如果将其中相同的部分改为include方式来引用就可以缩短Makefile,而且如果以后要对Makefile进行修改的话,只需要修改app_make.txt就可以应用到所有的应用程序,修改起来会非常省事。
app_make.txt的内容如下,这个稍微有点长。
本次的app_make.txt
TOOLPATH = ../../z_tools/
INCPATH = ../../z_tools/haribote/
APILIBPATH = ../apilib/
HARIBOTEPATH = ../haribote/
MAKE = $(TOOLPATH)make.exe -r
NASK = $(TOOLPATH)nask.exe
CC1 = $(TOOLPATH)cc1.exe -I$(INCPATH) -I../ -Os -Wall -quiet
GAS2NASK = $(TOOLPATH)gas2nask.exe -a
OBJ2BIM = $(TOOLPATH)obj2bim.exe
MAKEFONT = $(TOOLPATH)makefont.exe
BIN2OBJ = $(TOOLPATH)bin2obj.exe
BIM2HRB = $(TOOLPATH)bim2hrb.exe
RULEFILE = ../haribote.rul
EDIMG = $(TOOLPATH)edimg.exe
IMGTOL = $(TOOLPATH)imgtol.com
GOLIB = $(TOOLPATH)golib00.exe
COPY = copy
DEL = del
#默认动作
default :
$(MAKE) $(APP).hrb
#文件生成规则
$(APP).bim : $(APP).obj $(APILIBPATH)apilib.lib Makefile ../app_make.txt
$(OBJ2BIM) @$(RULEFILE) out:$(APP).bim map:$(APP).map stack:$(STACK) \
$(APP).obj $(APILIBPATH)apilib.lib
$(APP).hrb : $(APP).bim Makefile ../app_make.txt
$(BIM2HRB) $(APP).bim $(APP).hrb $(MALLOC)
haribote.img : ../haribote/ipl10.bin ../haribote/haribote.sys $(APP).hrb \
Makefile ../app_make.txt
$(EDIMG) imgin:../../z_tools/fdimg0at.tek \
wbinimg src:../haribote/ipl10.bin len:512 from:0 to:0 \
copy from:../haribote/haribote.sys to:@: \
copy from:$(APP).hrb to:@: \
imgout:haribote.img
#一般规则
%.gas : %.c ../apilib.h Makefile ../app_make.txt
$(CC1) -o $*.gas $*.c
%.nas : %.gas Makefile ../app_make.txt
$(GAS2NASK) $*.gas $*.nas
%.obj : %.nas Makefile ../app_make.txt
$(NASK) $*.nas $*.obj $*.lst
#命令
run :
$(MAKE) haribote.img
$(COPY) haribote.img .. \.. \z_tools\qemu\fdimage0.bin
$(MAKE) -C ../../z_tools/qemu
full :
$(MAKE) -C $(APILIBPATH)
$(MAKE) $(APP).hrb
run_full :
$(MAKE) -C $(APILIBPATH)
$(MAKE) -C ../haribote
$(MAKE) run
clean :
-$(DEL) *.lst
-$(DEL) *.obj
-$(DEL) *.map
-$(DEL) *.bim
-$(DEL) haribote.img
src_only :
$(MAKE) clean
-$(DEL) $(APP).hrb
这里的重点是,可以使用的命令增加了。一般的“make”命令会生成a.hrb,这是理所当然的啦。如果执行“make run”的话,则会生成一个包含haribote.sys和a.hrb的精简版磁盘映像,然后调用QEMU来运行。
而这次我们又在此基础上新增了“make full”和“make run_full”两个命令。生成a.hrb时需要引用apilib.lib,但也可能出现在“make”a.hrb时apilib.lib还未完成的情况,这时我们应该用“make full”。在“make full”中,有“$(MAKE) –C $(APILIBPATH)”这样一条语句,表示“先执行apilib的make”的意思,而如果已经存在apilib.lib的话,这条语句将不执行任何操作。因此如果不放心的话,一直用“make full”来代替“make”也是可以的。而“make run_full”则是“make run”的full版,即将apilib和系统核心都make之后,再执行原本的“make run”操作。
最后来介绍一下harib24g的Makefile。
harib24g的Makefile
TOOLPATH = ../z_tools/
INCPATH = ../z_tools/haribote/
MAKE = $(TOOLPATH)make.exe -r
EDIMG = $(TOOLPATH)edimg.exe
IMGTOL = $(TOOLPATH)imgtol.com
COPY = copy
DEL = del
#默认动作
default :
$(MAKE) haribote.img
#文件生成规则
haribote.img : haribote/ipl10.bin haribote/haribote.sys Makefile\
a/a.hrb hello3/hello3.hrb hello4/hello4.hrb hello5/hello5.hrb \
winhelo/winhelo.hrb winhelo2/winhelo2.hrb winhelo3/winhelo3.hrb \
star1/star1.hrb stars/stars.hrb stars2/stars2.hrb \
lines/lines.hrb walk/walk.hrb noodle/noodle.hrb \
beepdown/beepdown.hrb color/color.hrb color2/color2.hrb
$(EDIMG) imgin:../z_tools/fdimg0at.tek \
wbinimg src:haribote/ipl10.bin len:512 from:0 to:0 \
copy from:haribote/haribote.sys to:@: \
copy from:haribote/ipl10.nas to:@: \
copy from:make.bat to:@: \
copy from:a/a.hrb to:@: \
copy from:hello3/hello3.hrb to:@: \
copy from:hello4/hello4.hrb to:@: \
copy from:hello5/hello5.hrb to:@: \
copy from:winhelo/winhelo.hrb to:@: \
copy from:winhelo2/winhelo2.hrb to:@: \
copy from:winhelo3/winhelo3.hrb to:@: \
copy from:star1/star1.hrb to:@: \
copy from:stars/stars.hrb to:@: \
copy from:stars2/stars2.hrb to:@: \
copy from:lines/lines.hrb to:@: \
copy from:walk/walk.hrb to:@: \
copy from:noodle/noodle.hrb to:@: \
copy from:beepdown/beepdown.hrb to:@: \
copy from:color/color.hrb to:@: \
copy from:color2/color2.hrb to:@: \
imgout:haribote.img
#命令
run :
$(MAKE) haribote.img
$(COPY) haribote.img ..\ z_tools\qemu \ fdimage0.bin
$(MAKE) -C ../z_tools/qemu
install :
$(MAKE) haribote.img
$(IMGTOL) w a: haribote.img
full :
$(MAKE) -C haribote
$(MAKE) -C apilib
$(MAKE) -C a
$(MAKE) -C hello3
$(MAKE) -C hello4
$(MAKE) -C hello5
$(MAKE) -C winhelo
$(MAKE) -C winhelo2
$(MAKE) -C winhelo3
$(MAKE) -C star1
$(MAKE) -C stars
$(MAKE) -C stars2
$(MAKE) -C lines
$(MAKE) -C walk
$(MAKE) -C noodle
$(MAKE) -C beepdown
$(MAKE) -C color
$(MAKE) -C color2
$(MAKE) haribote.img
run_full :
$(MAKE) full
$(COPY) haribote.img ../z_tools\qemu\fdimage0.bin
$(MAKE) -C ../z_tools/qemu
install_full :
$(MAKE) full
$(IMGTOL) w a: haribote.img
run_os :
$(MAKE) -C haribote
$(MAKE) run
clean :
#不执行任何操作
src_only :
$(MAKE) clean
-$(DEL) haribote.img
clean_full :
$(MAKE) -C haribote clean
$(MAKE) -C apilib clean
$(MAKE) -C a clean
$(MAKE) -C hello3 clean
$(MAKE) -C hello4 clean
$(MAKE) -C hello5 clean
$(MAKE) -C winhelo clean
$(MAKE) -C winhelo2 clean
$(MAKE) -C winhelo3 clean
$(MAKE) -C star1 clean
$(MAKE) -C stars clean
$(MAKE) -C stars2 clean
$(MAKE) -C lines clean
$(MAKE) -C walk clean
$(MAKE) -C noodle clean
$(MAKE) -C beepdown clean
$(MAKE) -C color clean
$(MAKE) -C color2 clean
src_only_full :
$(MAKE) -C haribote src_only
$(MAKE) -C apilib src_only
$(MAKE) -C a src_only
$(MAKE) -C hello3 src_only
$(MAKE) -C hello4 src_only
$(MAKE) -C hello5 src_only
$(MAKE) -C winhelo src_only
$(MAKE) -C winhelo2 src_only
$(MAKE) -C winhelo3 src_only
$(MAKE) -C star1 src_only
$(MAKE) -C stars src_only
$(MAKE) -C stars2 src_only
$(MAKE) -C lines src_only
$(MAKE) -C walk src_only
$(MAKE) -C noodle src_only
$(MAKE) -C beepdown src_only
$(MAKE) -C color src_only
$(MAKE) -C color2 src_only
-$(DEL) haribote.img
refresh :
$(MAKE) full
$(MAKE) clean_full
-$(DEL) haribote.img
在这里我们可以使用很多命令。
make | 像之前一样,生成一个包含操作系统内核及全部应用程序的磁盘映像 |
make run | “make”后启动QEMU |
make install | “make”后将磁盘映像安装到软盘中 |
make full | 将操作系统核心、apilib和应用程序全部make后生成磁盘映像 |
make run_full | “make full”后“make run” |
make install_full | “make full”后“make install” |
make run_os | 将操作系统核心make后执行“make run”,当只对操作系统核心进行修改时可使用这个命令 |
make clean | 本来clean命令是用于清除临时文件的,但由于在这个Makefile中并不生成临时文件,因此这个命令不执行任何操作 |
make src_only | 将生成的磁盘映像删除以释放磁盘空间 |
make clean_full | 对操作系统核心、apilib和应用程序全部执行“make clean”,这样将清除所有的临时文件 |
make src_only_full | 对操作系统核心、apilib和应用程序全部执行“make src_only”, 这样将清除所有的临时文件和最终生成物。不过执行这个命令后,“make”和“make run”就无法使用了(用带full版本的命令代替即可),make时会消耗更多的时间 |
make refresh | “make full”后“make clean_full”。从执行过“make src_only_full”的状态执行这个命令的话,就会恢复到可以直接“make”和“make run”的状态 |
■■■■■
像这样划分好不同的目录后,程序看起来更加清爽了,make所用时间也缩短了。例如,当编写了一个新的应用程序时,harib24f的话需要全部重新生成一遍(因为修改了Makefile),但现在由于操作系统核心和应用程序的Makefile是分开的,因此不需要每次新增应用程序都重新生成一遍,make的速度也就变快了,耶!
嗯,在整理的时候发现有几个应用程序有点问题,比如说winhelo.hrb这个程序,窗口弹出之后马上就结束了,看不清窗口上面的内容,因此我们按照lines.c的方式将winhelo.c修改一下。
本次的winhelo/winhelo.c
#include "apilib.h"
char buf[150 * 50];
void HariMain(void)
{
int win;
win = api_openwin(buf, 150, 50, -1, "hello");
for (;;) { /*从此开始*/
if (api_getkey(1) == 0x0a) {
break; /*按下回车键则break; */
}
} /*到此结束*/
api_end();
}
winhelo2、winhelo3、star1、stars和stars2也有同样的问题,顺便全都改了一下。
好了,我们来“make run_full”吧。哦哦,运行正常,撒花!为了庆祝成功,我们把修改过的stars.hrb和winhelo3.hrb也运行一下,摆出来装点门面。
好多应用程序
本书光盘中的harib24g是执行过“make refresh”的状态,因此大家可以直接执行“make run”,而不用执行比较耗时的“make fun_full”。
好啦,今天我们已经很努力了,就到这里吧。明天继续,晚安喽!