7 整理make环境(harib24g)

也许是笔者的电脑性能比较差,最近感觉“make run”需要的时间很长。另外,操作系统、应用程序和库的源文件都混在一起,看起来非常混乱。因此,我们来把它们各归各位吧。

7 整理make环境(harib24g) - 图1

■■■■■

我们先从操作系统的部分开始吧。在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

  1. APP = a
  2. STACK = 1k
  3. MALLOC = 0k
  4. include ../app_make.txt

怎么样?算上空行也才只有5行。不过话说回来,其实app_make.txt还是很长的,实际上也没有变短,只是“看上去变短了”而已。之所以要用include,是因为所有的应用程序的Makefile都大同小异,如果将其中相同的部分改为include方式来引用就可以缩短Makefile,而且如果以后要对Makefile进行修改的话,只需要修改app_make.txt就可以应用到所有的应用程序,修改起来会非常省事。

app_make.txt的内容如下,这个稍微有点长。

本次的app_make.txt

  1. TOOLPATH = ../../z_tools/
  2. INCPATH = ../../z_tools/haribote/
  3. APILIBPATH = ../apilib/
  4. HARIBOTEPATH = ../haribote/
  5. MAKE = $(TOOLPATH)make.exe -r
  6. NASK = $(TOOLPATH)nask.exe
  7. CC1 = $(TOOLPATH)cc1.exe -I$(INCPATH) -I../ -Os -Wall -quiet
  8. GAS2NASK = $(TOOLPATH)gas2nask.exe -a
  9. OBJ2BIM = $(TOOLPATH)obj2bim.exe
  10. MAKEFONT = $(TOOLPATH)makefont.exe
  11. BIN2OBJ = $(TOOLPATH)bin2obj.exe
  12. BIM2HRB = $(TOOLPATH)bim2hrb.exe
  13. RULEFILE = ../haribote.rul
  14. EDIMG = $(TOOLPATH)edimg.exe
  15. IMGTOL = $(TOOLPATH)imgtol.com
  16. GOLIB = $(TOOLPATH)golib00.exe
  17. COPY = copy
  18. DEL = del
  19. #默认动作
  20. default :
  21. $(MAKE) $(APP).hrb
  22. #文件生成规则
  23. $(APP).bim : $(APP).obj $(APILIBPATH)apilib.lib Makefile ../app_make.txt
  24. $(OBJ2BIM) @$(RULEFILE) out:$(APP).bim map:$(APP).map stack:$(STACK) \
  25. $(APP).obj $(APILIBPATH)apilib.lib
  26. $(APP).hrb : $(APP).bim Makefile ../app_make.txt
  27. $(BIM2HRB) $(APP).bim $(APP).hrb $(MALLOC)
  28. haribote.img : ../haribote/ipl10.bin ../haribote/haribote.sys $(APP).hrb \
  29. Makefile ../app_make.txt
  30. $(EDIMG) imgin:../../z_tools/fdimg0at.tek \
  31. wbinimg src:../haribote/ipl10.bin len:512 from:0 to:0 \
  32. copy from:../haribote/haribote.sys to:@: \
  33. copy from:$(APP).hrb to:@: \
  34. imgout:haribote.img
  35. #一般规则
  36. %.gas : %.c ../apilib.h Makefile ../app_make.txt
  37. $(CC1) -o $*.gas $*.c
  38. %.nas : %.gas Makefile ../app_make.txt
  39. $(GAS2NASK) $*.gas $*.nas
  40. %.obj : %.nas Makefile ../app_make.txt
  41. $(NASK) $*.nas $*.obj $*.lst
  42. #命令
  43. run :
  44. $(MAKE) haribote.img
  45. $(COPY) haribote.img .. \.. \z_tools\qemu\fdimage0.bin
  46. $(MAKE) -C ../../z_tools/qemu
  47. full :
  48. $(MAKE) -C $(APILIBPATH)
  49. $(MAKE) $(APP).hrb
  50. run_full :
  51. $(MAKE) -C $(APILIBPATH)
  52. $(MAKE) -C ../haribote
  53. $(MAKE) run
  54. clean :
  55. -$(DEL) *.lst
  56. -$(DEL) *.obj
  57. -$(DEL) *.map
  58. -$(DEL) *.bim
  59. -$(DEL) haribote.img
  60. src_only :
  61. $(MAKE) clean
  62. -$(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

  1. TOOLPATH = ../z_tools/
  2. INCPATH = ../z_tools/haribote/
  3. MAKE = $(TOOLPATH)make.exe -r
  4. EDIMG = $(TOOLPATH)edimg.exe
  5. IMGTOL = $(TOOLPATH)imgtol.com
  6. COPY = copy
  7. DEL = del
  8. #默认动作
  9. default :
  10. $(MAKE) haribote.img
  11. #文件生成规则
  12. haribote.img : haribote/ipl10.bin haribote/haribote.sys Makefile\
  13. a/a.hrb hello3/hello3.hrb hello4/hello4.hrb hello5/hello5.hrb \
  14. winhelo/winhelo.hrb winhelo2/winhelo2.hrb winhelo3/winhelo3.hrb \
  15. star1/star1.hrb stars/stars.hrb stars2/stars2.hrb \
  16. lines/lines.hrb walk/walk.hrb noodle/noodle.hrb \
  17. beepdown/beepdown.hrb color/color.hrb color2/color2.hrb
  18. $(EDIMG) imgin:../z_tools/fdimg0at.tek \
  19. wbinimg src:haribote/ipl10.bin len:512 from:0 to:0 \
  20. copy from:haribote/haribote.sys to:@: \
  21. copy from:haribote/ipl10.nas to:@: \
  22. copy from:make.bat to:@: \
  23. copy from:a/a.hrb to:@: \
  24. copy from:hello3/hello3.hrb to:@: \
  25. copy from:hello4/hello4.hrb to:@: \
  26. copy from:hello5/hello5.hrb to:@: \
  27. copy from:winhelo/winhelo.hrb to:@: \
  28. copy from:winhelo2/winhelo2.hrb to:@: \
  29. copy from:winhelo3/winhelo3.hrb to:@: \
  30. copy from:star1/star1.hrb to:@: \
  31. copy from:stars/stars.hrb to:@: \
  32. copy from:stars2/stars2.hrb to:@: \
  33. copy from:lines/lines.hrb to:@: \
  34. copy from:walk/walk.hrb to:@: \
  35. copy from:noodle/noodle.hrb to:@: \
  36. copy from:beepdown/beepdown.hrb to:@: \
  37. copy from:color/color.hrb to:@: \
  38. copy from:color2/color2.hrb to:@: \
  39. imgout:haribote.img
  40. #命令
  41. run :
  42. $(MAKE) haribote.img
  43. $(COPY) haribote.img ..\ z_tools\qemu \ fdimage0.bin
  44. $(MAKE) -C ../z_tools/qemu
  45. install :
  46. $(MAKE) haribote.img
  47. $(IMGTOL) w a: haribote.img
  48. full :
  49. $(MAKE) -C haribote
  50. $(MAKE) -C apilib
  51. $(MAKE) -C a
  52. $(MAKE) -C hello3
  53. $(MAKE) -C hello4
  54. $(MAKE) -C hello5
  55. $(MAKE) -C winhelo
  56. $(MAKE) -C winhelo2
  57. $(MAKE) -C winhelo3
  58. $(MAKE) -C star1
  59. $(MAKE) -C stars
  60. $(MAKE) -C stars2
  61. $(MAKE) -C lines
  62. $(MAKE) -C walk
  63. $(MAKE) -C noodle
  64. $(MAKE) -C beepdown
  65. $(MAKE) -C color
  66. $(MAKE) -C color2
  67. $(MAKE) haribote.img
  68. run_full :
  69. $(MAKE) full
  70. $(COPY) haribote.img ../z_tools\qemu\fdimage0.bin
  71. $(MAKE) -C ../z_tools/qemu
  72. install_full :
  73. $(MAKE) full
  74. $(IMGTOL) w a: haribote.img
  75. run_os :
  76. $(MAKE) -C haribote
  77. $(MAKE) run
  78. clean :
  79. #不执行任何操作
  80. src_only :
  81. $(MAKE) clean
  82. -$(DEL) haribote.img
  83. clean_full :
  84. $(MAKE) -C haribote clean
  85. $(MAKE) -C apilib clean
  86. $(MAKE) -C a clean
  87. $(MAKE) -C hello3 clean
  88. $(MAKE) -C hello4 clean
  89. $(MAKE) -C hello5 clean
  90. $(MAKE) -C winhelo clean
  91. $(MAKE) -C winhelo2 clean
  92. $(MAKE) -C winhelo3 clean
  93. $(MAKE) -C star1 clean
  94. $(MAKE) -C stars clean
  95. $(MAKE) -C stars2 clean
  96. $(MAKE) -C lines clean
  97. $(MAKE) -C walk clean
  98. $(MAKE) -C noodle clean
  99. $(MAKE) -C beepdown clean
  100. $(MAKE) -C color clean
  101. $(MAKE) -C color2 clean
  102. src_only_full :
  103. $(MAKE) -C haribote src_only
  104. $(MAKE) -C apilib src_only
  105. $(MAKE) -C a src_only
  106. $(MAKE) -C hello3 src_only
  107. $(MAKE) -C hello4 src_only
  108. $(MAKE) -C hello5 src_only
  109. $(MAKE) -C winhelo src_only
  110. $(MAKE) -C winhelo2 src_only
  111. $(MAKE) -C winhelo3 src_only
  112. $(MAKE) -C star1 src_only
  113. $(MAKE) -C stars src_only
  114. $(MAKE) -C stars2 src_only
  115. $(MAKE) -C lines src_only
  116. $(MAKE) -C walk src_only
  117. $(MAKE) -C noodle src_only
  118. $(MAKE) -C beepdown src_only
  119. $(MAKE) -C color src_only
  120. $(MAKE) -C color2 src_only
  121. -$(DEL) haribote.img
  122. refresh :
  123. $(MAKE) full
  124. $(MAKE) clean_full
  125. -$(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

  1. #include "apilib.h"
  2. char buf[150 * 50];
  3. void HariMain(void)
  4. {
  5. int win;
  6. win = api_openwin(buf, 150, 50, -1, "hello");
  7. for (;;) { /*从此开始*/
  8. if (api_getkey(1) == 0x0a) {
  9. break; /*按下回车键则break; */
  10. }
  11. } /*到此结束*/
  12. api_end();
  13. }

winhelo2、winhelo3、star1、stars和stars2也有同样的问题,顺便全都改了一下。

好了,我们来“make run_full”吧。哦哦,运行正常,撒花!为了庆祝成功,我们把修改过的stars.hrb和winhelo3.hrb也运行一下,摆出来装点门面。

7 整理make环境(harib24g) - 图2

好多应用程序

本书光盘中的harib24g是执行过“make refresh”的状态,因此大家可以直接执行“make run”,而不用执行比较耗时的“make fun_full”。

好啦,今天我们已经很努力了,就到这里吧。明天继续,晚安喽!