From c8f23de807e6efcd6b797a6dde6366d67f64906d Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 25 May 2016 10:56:12 +0800 Subject: impl extract library for windows --- xmake/tools/lib.lua | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 58 insertions(+), 2 deletions(-) (limited to 'xmake/tools/lib.lua') diff --git a/xmake/tools/lib.lua b/xmake/tools/lib.lua index b2d81a2c6..4c7f6c819 100755 --- a/xmake/tools/lib.lua +++ b/xmake/tools/lib.lua @@ -21,10 +21,14 @@ -- -- init it -function init(shellname) +function init(shellname, kind) -- save the shell name _g.shellname = shellname or "lib.exe" + + -- save the tool kind + _g.kind = kind or "ar" + end -- get the property @@ -34,9 +38,48 @@ function get(name) return _g[name] end +-- extract the static library to object directory +function extract(libraryfile, objectdir) + + -- make the object directory first + os.mkdir(objectdir) + + -- list object files + local objectfiles = os.iorun("%s -nologo -list %s", _g.shellname, libraryfile) + + -- extrace all object files + for _, objectfile in ipairs(objectfiles:split('\n')) do + + -- is object file? + if objectfile:find("%.obj") then + + -- make the outputfile + local outputfile = path.translate(format("%s\\%s", objectdir, path.filename(objectfile))) + + -- repeat? rename it + if os.isfile(outputfile) then + for i = 0, 10 do + outputfile = path.translate(format("%s\\%d_%s", objectdir, i, path.filename(objectfile))) + if not os.isfile(outputfile) then + break + end + end + end + + -- extract it + os.run("%s -nologo -extract:%s -out:%s %s", _g.shellname, objectfile, outputfile, libraryfile) + end + end +end + -- run command function run(...) + -- extract it + if _g.kind == "ex" then + return extract(...) + end + -- run it os.run(...) end @@ -44,7 +87,20 @@ end -- check the given flags function check(flags) + -- make an stub source file + local libfile = path.join(os.tmpdir(), "xmake.lib.lib") + local objfile = path.join(os.tmpdir(), "xmake.lib.obj") + local srcfile = path.join(os.tmpdir(), "xmake.lib.c") + io.write(srcfile, "int test(void)\n{return 0;}") + -- check it - os.run("%s", _g.shellname, ifelse(flags, flags, "")) + os.run("cl -c -Fo%s %s", objfile, srcfile) + os.run("link -lib -out:%s %s", libfile, objfile) + os.run("%s %s -list %s", _g.shellname, ifelse(flags, flags, ""), libfile) + + -- remove files + os.rm(objfile) + os.rm(srcfile) + os.rm(libfile) end -- cgit v1.3.1