imuncle / imuncle.github.io

大叔的个人小站
https://imuncle.github.io/
82 stars 17 forks source link

VBScript模拟CMake #93

Open imuncle opened 4 years ago

imuncle commented 4 years ago

之前有提到一个想使用Windows批处理脚本模拟CMake的想法,具体见gcc编译带资源的Win32程序,正当我还在纠结怎么使用batch比较文件修改时间的时候,VBScript进入了我的视野,只因一句话,我就震惊了:

Msgbox "Hello World"

将它保存为hello.vbs,然后双击运行: image

一个Message Box就这样产生了!这也太简洁了吧。而且VB是深度嵌入进Windows系统的,也就是说这是真正的随便一个Windows都可以运行的代码,并且因为是脚本文件,运行时是一行一行解析运行的,所以这还省去了编译的步骤,这也太帅了吧。

后续进一步了解发现,原来VBScript曾经是和JavaScript一争高下的脚本语言,可惜VBScript只支持IE,而后者则是所有浏览器都支持,自然被搞下台。现在基本上网页开发里已经看不到它的身影了。

鉴于VBScript的API接口多很多,所以我打算使用它来模拟CMake,以实现我的“自动化编译”。

我的工程文件如下: image

VBScript代码如下:

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set obj = createobject("wscript.shell")

Const targetFile = "poppad"

'判断是否存在build文件夹,若不存在则创建
if objFSO.FolderExists("build") then
else
    objFSO.createfolder("build")
end if

'判断是否存在目标文件
if objFSO.FileExists(targetFile &".exe") then
    Set objFile1 = objFSO.GetFile(targetFile &".exe")
    Set objFile2 = objFSO.GetFile(targetFile &".rc")
    '判断资源文件是否被修改,若修改则重新编译
    if DateDiff("s",objFile1.DateLastModified,objFile2.DateLastModified) > 0 then
        obj.run "cmd /k windres " & targetFile & ".rc build/resource.o && gcc -mwindows " & targetFile & ".c popfind.c popfont.c popfile.c popprnt0.c build/resource.o -o " & targetFile & ".exe && exit"
    end if
    Set objFile1 = Nothing
    Set objFile2 = Nothing
else
    '若不存在则从头编译
    obj.run "cmd /k windres " & targetFile & ".rc build/resource.o && gcc -mwindows " & targetFile & ".c popfind.c popfont.c popfile.c popprnt0.c build/resource.o -o " & targetFile & ".exe && exit"
end if

很简单,通过比较可执行文件与被编译文件的修改时间来判断文件是否被修改,如果修改了就重新编译,否则就不编译。

保存为make.vbs,然后在命令行直接输入make或者双击该文件就行了。

参考