summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2018-05-26 23:24:13 +0800
committerruki <[email protected]>2018-05-26 23:24:13 +0800
commit26038ac60976706764745d0a6fd93febb10765f9 (patch)
tree82f7a2d862900b2b363c388d63e050386599684b
parentcbd06bb6936b3257faf907d6d5ff3708861f1119 (diff)
impl testcert
-rw-r--r--xmake/modules/detect/sdks/find_wdk.lua18
-rw-r--r--xmake/modules/utils/wdk/testcert.lua127
-rw-r--r--xmake/rules/wdk/sign/xmake.lua15
3 files changed, 155 insertions, 5 deletions
diff --git a/xmake/modules/detect/sdks/find_wdk.lua b/xmake/modules/detect/sdks/find_wdk.lua
index 1bdda516d..bd2e8b887 100644
--- a/xmake/modules/detect/sdks/find_wdk.lua
+++ b/xmake/modules/detect/sdks/find_wdk.lua
@@ -28,6 +28,7 @@ import("lib.detect.find_file")
import("core.base.option")
import("core.base.global")
import("core.project.config")
+import("detect.sdks.find_vstudio")
-- find WDK directory
function _find_sdkdir(sdkdir)
@@ -39,8 +40,19 @@ function _find_sdkdir(sdkdir)
-- get sdk directory from vsvars
if not sdkdir then
- local arch = config.arch()
+ local arch = config.arch() or os.arch()
local vcvarsall = config.get("__vcvarsall")
+ if not vcvarsall then
+ local vstudio = find_vstudio()
+ if vstudio then
+ for _, vsinfo in pairs(vstudio) do
+ if vsinfo.vcvarsall then
+ vcvarsall = vsinfo.vcvarsall
+ break
+ end
+ end
+ end
+ end
if vcvarsall then
sdkdir = (vcvarsall[arch] or {}).WindowsSdkDir
end
@@ -58,7 +70,7 @@ function _find_umdfver(libdir, includedir)
end
-- find version
- local arch = config.arch()
+ local arch = config.arch() or os.arch()
if arch then
for _, ver in ipairs(versions) do
if os.isfile(path.join(libdir, "wdf", "umdf", arch, ver, "WdfDriverStubUm.lib")) then
@@ -78,7 +90,7 @@ function _find_kmdfver(libdir, includedir)
end
-- find version
- local arch = config.arch()
+ local arch = config.arch() or os.arch()
if arch then
for _, ver in ipairs(versions) do
if os.isfile(path.join(libdir, "wdf", "kmdf", arch, ver, "wdfdriverentry.lib")) then
diff --git a/xmake/modules/utils/wdk/testcert.lua b/xmake/modules/utils/wdk/testcert.lua
new file mode 100644
index 000000000..045d2045a
--- /dev/null
+++ b/xmake/modules/utils/wdk/testcert.lua
@@ -0,0 +1,127 @@
+--!A cross-platform build utility based on Lua
+--
+-- Licensed to the Apache Software Foundation (ASF) under one
+-- or more contributor license agreements. See the NOTICE file
+-- distributed with this work for additional information
+-- regarding copyright ownership. The ASF licenses this file
+-- to you under the Apache License, Version 2.0 (the
+-- "License"); you may not use this file except in compliance
+-- with the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+-- Copyright (C) 2015 - 2018, TBOOX Open Source Group.
+--
+-- @author ruki
+-- @file testcert.lua
+--
+
+-- imports
+import("core.base.option")
+import("core.base.global")
+import("core.project.config")
+import("detect.sdks.find_wdk")
+
+-- get tool
+function _get_tool(wdk, name)
+
+ -- init cache
+ _g.tools = _g.tools or {}
+
+ -- get it from the cache
+ local tool = _g.tools[name]
+ if not tool then
+
+ -- get arch
+ local arch = config.arch() or os.arch()
+
+ -- get tool
+ tool = path.join(wdk.bindir, arch, name .. ".exe")
+ if not os.isexec(tool) then
+ tool = path.join(wdk.bindir, wdk.sdkver, arch, name .. ".exe")
+ end
+ assert(os.isexec(tool), name .. " not found!")
+ _g.tools[name] = tool
+ end
+ return tool
+end
+
+-- install test certificate
+function _install(wdk)
+
+ -- get signtool
+ local signtool = _get_tool(wdk, "signtool")
+
+ -- check test certificate first
+ local ok = try
+ {
+ function ()
+ local tmpfile = os.tmpfile(os.programfile())
+ if not os.isfile(tmpfile) then
+ os.cp(os.programfile(), tmpfile)
+ end
+ os.vrunv(signtool, {"sign", "/v", "/a", "/s", "PrivateCertStore", "/n", "tboox.org(test)", tmpfile})
+ return true
+ end
+ }
+ if ok then
+ return
+ end
+
+ -- get makecert
+ local makecert = _get_tool(wdk, "makecert")
+
+ -- get certmgr
+ local certmgr = _get_tool(wdk, "certmgr")
+
+ -- get a test certificate
+ local testcer = path.join(global.directory(), "sign", "test.cer")
+ local company = "tboox.org(test)"
+
+ -- make a new test certificate @note need re-generate certificate when reinstalling
+ local signdir = path.directory(testcer)
+ if not os.isdir(signdir) then
+ os.mkdir(signdir)
+ end
+ os.vrunv(makecert, {"-r", "-pe", "-ss", "PrivateCertStore", "-n", "CN=" .. company, testcer})
+
+ -- register this test certificate
+ try
+ {
+ function ()
+ os.vrunv(certmgr, {"/add", testcer, "/s", "/r", "localMachine", "root"})
+ os.vrunv(certmgr, {"/add", testcer, "/s", "/r", "localMachine", "trustedpublisher"})
+ end,
+ catch
+ {
+ function (errors)
+ os.tryrm(testcer)
+ raise(errors)
+ end
+ }
+ }
+
+ -- trace
+ print("install test certificate ok!")
+ print(" - company: %s", company)
+ print(" - cerfile: %s", testcer)
+end
+
+-- entry function
+function main(action)
+
+ -- find wdk envirnoment first
+ local wdk = find_wdk()
+ assert(wdk, "wdk not found!")
+
+ -- install or uninstall test certificate
+ if action == "install" then
+ _install(wdk)
+ end
+end
diff --git a/xmake/rules/wdk/sign/xmake.lua b/xmake/rules/wdk/sign/xmake.lua
index e3c28c615..1e388043e 100644
--- a/xmake/rules/wdk/sign/xmake.lua
+++ b/xmake/rules/wdk/sign/xmake.lua
@@ -42,6 +42,7 @@ rule("wdk.sign")
-- imports
import("core.project.config")
+ import("utils.wdk.testcert")
-- get wdk
local wdk = target:data("wdk")
@@ -67,9 +68,19 @@ rule("wdk.sign")
local thumbprint = target:values("wdk.sign.thumbprint")
if mode and (not certfile and not thumbprint and not store) then
if mode == "test" then
- raise([[please first select one following step for signing:
-1. run `$xmake l utils.wdk.testsign install` as admin in console (only once) or
+ -- attempt to install test certificate first
+ local ok = try
+ {
+ function ()
+ testcert("install")
+ return true
+ end
+ }
+ if not ok then
+ raise([[please first select one following step for signing:
+1. run `$xmake l utils.wdk.testcert install` as admin in console (only once) or
2. add set_values(\"wdk.sign.[certfile|store|thumbprint]\", ...) in xmake.lua]], mode)
+ end
else
raise("please add set_values(\"wdk.sign.[certfile|store|thumbprint]\", ...) for %s signing!", mode)
end