用Rant自动化D语言程序构建

阅读更多
上回说到 Rank 这个 Ruby 世界最广泛使用的构建工具在 Windows 下有大bug,根本不能运行。Python的scons太慢、自动得过了头,造成定制起来很麻烦.....
最终,我找到了一个叫 Rant 的Ruby构建工具,用起来几乎与 Rank一样,而且特性更多,最重要的是能在 windows 下面正常运行。因此我强烈推荐各位D语言的粉丝使用Rant作为软件构建工具,放弃那些不成熟的IDE。用Rant的好处是还能顺带学习一下Ruby语言,对于像我一样的Ruby&&D双料菜鸟,这是不可多得的学习机会。

如果没有Ruby,请先下载安装 Ruby。 然后在控制台中输入:gem --remote install rant,系统将会自动安装并配置好 rant
下面是我写的 for DMD 万用 Rantfile 模板,只要把它放到你的D程序所在的目录,稍加修改就能使用。
RANTFILE 代码
  1. # The Rantfile for DMD
  2. # Author: oldrev (wstringgmail.com)
  3. # No copyrights, use it freely
  4. import "AutoClean"
  5. #require "rant/filelist"
  6. # 请自行定义下面几行
  7. NAME = "foo.exe" # 可执行文件名
  8. SRC = "./src" # D 源程序在 ./src 目录下(包括子目录)
  9. LIBS = ["advapi32.lib", "uuid.lib", "ole32.lib"] # 程序用到的附加 .lib
  10. DEBUG_FLAGS = "-debug -g"
  11. RELEASE_FLAGS ="-release -O"
  12. DC = "dmd.exe"
  13. IMPLIB = "implib.exe"
  14. PROG = "#{NAME}"
  15. PROG_DEBUG = "#{NAME}"
  16. SRCS = Rant::FileList[SRC + "/**/*.d"]
  17. OBJS = SRCS.ext "obj"
  18. OBJS_DEBUG = SRCS.map {|file| file.sub /\.d$/, "_debug.obj"}
  19. DEFS = Rant::FileList[SRC + "/**/*.def"]
  20. ILIBS = DEFS.ext "lib"
  21. task :default => :debug
  22. task :release => :program
  23. task :debug => :program_d
  24. def dolink(target, t)
  25. sys.sh "#{DC} -of#{target} #{t.prerequisites.join(' ')} #{LIBS.join(' ')}"
  26. end
  27. task :program => OBJS.entries + ILIBS.entries do |t|
  28. dolink PROG_DEBUG, t
  29. end
  30. task :program_d => OBJS_DEBUG.entries + ILIBS.entries do |t|
  31. dolink PROG, t
  32. end
  33. gen Rule, ".obj" => ".d" do |t|
  34. sys.sh "#{DC} #{t.source} -c -I#{SRC} #{RELEASE_FLAGS} -of#{t.name}"
  35. end
  36. gen Rule, "_debug.obj" => ".d" do |t|
  37. sys "#{DC} #{t.source} #{DEBUG_FLAGS} -c -I#{SRC} -of#{t.name}"
  38. end
  39. gen Rule, ".lib" => ".def" do |t|
  40. # DigitalMars 的 implib.exe 程序不认识 '/' 分割的路径
  41. lib = t.name.gsub("/", "\\")
  42. dotdef = t.source.gsub("/", "\\")
  43. sys.sh "#{IMPLIB} /system #{lib} #{dotdef}"
  44. end
  45. task :clean do
  46. sys.rm_f OBJS
  47. sys.rm_f OBJS_DEBUG
  48. sys.rm_f ILIBS
  49. sys.rm_f PROG
  50. sys.rm_f PROG_DEBUG
  51. end

此 Rantfile 能扫描源程序目录的所有.d文件,并自动编译连接。如果源程序目录存在 .def 的 DLL 导入库定义文件的话,也会自动生成 .lib,并链接到程序中。

rant 的用法与make基本一致:
rant debug //建立 debug 版程序
rant release // 建立 release 版程序
rant clean // 清理零时文件
rant -f build.rb //指定build.rb为rantfile,而不是当前目录下的 Rantfile

Have Fun!

你可能感兴趣的:(D语言,Ruby,EXT,Python,F#)