From 7235ad9f40fbbf27b9fd1097817587e892690556 Mon Sep 17 00:00:00 2001 From: Xpol Wan Date: Fri, 23 Oct 2015 16:14:18 +0800 Subject: Automatically setup MSVC command line tools for VC2008 or above. --- install.bat | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/install.bat b/install.bat index 57e3611c..90e9b8d4 100644 --- a/install.bat +++ b/install.bat @@ -409,6 +409,25 @@ local function get_architecture() return proc end +-- uses vars.LUA_RUNTIME and vars.UNAME_M +local function get_compiler_env_cmd() + local major, minor = vars.LUA_RUNTIME:match('^MSVCR(%d+)(%d)$') + if not major then return "" end + local key = "HKEY_LOCAL_MACHINE\\SOFTWARE%s\\Microsoft\\VisualStudio\\%d.%d" + local hostarch64 = os.getenv("ProgramFiles(x86)")~=nil -- 'PROCESSOR_ARCHITECTURE' will always return 'x86' if interpreter is 32 bit. + key = key:format(hostarch64 and "\\Wow6432Node" or "", major, minor) + + local h = io.popen('reg query "'..key..'" /v InstallDir 2>NUL') + local output = h:read('*a') + h:close() + + local msvcdir = output:match("REG_SZ%s+(.+)\\Common7\\IDE") + if not msvcdir then return "" end + local msvcarch = vars.UNAME_M=="x86_64" and " x86_amd64" or "" + + return ('call "%s\\VC\\vcvarsall.bat"%s'):format(msvcdir, msvcarch) +end + local function look_for_lua_install () print("Looking for Lua interpreter") local directories @@ -627,6 +646,7 @@ if SELFCONTAINED then vars.TREE_ROOT = vars.PREFIX..[[\systree]] REGISTRY = false end +vars.COMPILER_ENV_CMD = get_compiler_env_cmd(vars.LUA_RUNTIME, vars.UNAME_M) print(S[[ @@ -731,6 +751,7 @@ for _, c in ipairs{"luarocks", "luarocks-admin"} do f:write(S[[ @ECHO OFF SETLOCAL +$COMPILER_ENV_CMD SET "LUA_PATH=$LUADIR\?.lua;$LUADIR\?\init.lua;%LUA_PATH%" IF NOT "%LUA_PATH_5_2%"=="" ( SET "LUA_PATH_5_2=$LUADIR\?.lua;$LUADIR\?\init.lua;%LUA_PATH_5_2%" -- cgit v1.2.3-55-g6feb From e5aa0e030f061d7bb9f7bd7f57d379619a9f4a4a Mon Sep 17 00:00:00 2001 From: Xpol Wan Date: Sat, 24 Oct 2015 14:41:50 +0800 Subject: Better setup vc compiler, should compatible with Windows SDK. 1. Use reg: HKEY_LOCAL_MACHINE\SOFTWARE[\Wow6432Node]\Microsoft\VisualStudio\\Setup\VC:ProductDir 2. Use bin\vcvars32.bat and bin\amd64\vcvars64.bat instead of vcvarsall.bat. --- install.bat | 43 ++++++++++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/install.bat b/install.bat index 90e9b8d4..533a0700 100644 --- a/install.bat +++ b/install.bat @@ -409,23 +409,40 @@ local function get_architecture() return proc end --- uses vars.LUA_RUNTIME and vars.UNAME_M -local function get_compiler_env_cmd() +-- get a string value from windows registry. +local function get_registry(key, value) + local h = io.popen('reg query "'..key..'" /v '..value..' 2>NUL') + local output = h:read('*a') + h:close() + + return output:match('REG_SZ%s+([^\n]+)') +end + +local function visual_studio_registry_key(major, minor) + local key = "HKEY_LOCAL_MACHINE\\SOFTWARE%s\\Microsoft\\VisualStudio\\%d.%d\\Setup\\VC" + -- os.getenv('PROCESSOR_ARCHITECTURE') will always return 'x86' if lua interpreter is 32 bit. + local hostarch64 = os.getenv("ProgramFiles(x86)")~=nil + return key:format(hostarch64 and "\\Wow6432Node" or "", major, minor) +end + +-- returns the batch command to setup msvc compiler path. +-- requires vars.LUA_RUNTIME and vars.UNAME_M to be set before calling this function. +local function get_vc_env_setup_cmd() + -- 1. check installed lua runtime version local major, minor = vars.LUA_RUNTIME:match('^MSVCR(%d+)(%d)$') if not major then return "" end - local key = "HKEY_LOCAL_MACHINE\\SOFTWARE%s\\Microsoft\\VisualStudio\\%d.%d" - local hostarch64 = os.getenv("ProgramFiles(x86)")~=nil -- 'PROCESSOR_ARCHITECTURE' will always return 'x86' if interpreter is 32 bit. - key = key:format(hostarch64 and "\\Wow6432Node" or "", major, minor) - local h = io.popen('reg query "'..key..'" /v InstallDir 2>NUL') - local output = h:read('*a') - h:close() + -- 2. check if required VC version is in registry + local product_dir = get_registry(visual_studio_registry_key(major, minor), 'ProductDir') + + if not product_dir then return "" end - local msvcdir = output:match("REG_SZ%s+(.+)\\Common7\\IDE") - if not msvcdir then return "" end - local msvcarch = vars.UNAME_M=="x86_64" and " x86_amd64" or "" + -- 4. check VC batch file exists + local relative_path = vars.UNAME_M=="x86_64" and "bin\\amd64\\vcvars64.bat" or "bin\\vcvars32.bat" + local full_path = product_dir .. relative_path + if not exists(full_path) then return "" end - return ('call "%s\\VC\\vcvarsall.bat"%s'):format(msvcdir, msvcarch) + return ('call "%s"'):format(full_path) end local function look_for_lua_install () @@ -646,7 +663,7 @@ if SELFCONTAINED then vars.TREE_ROOT = vars.PREFIX..[[\systree]] REGISTRY = false end -vars.COMPILER_ENV_CMD = get_compiler_env_cmd(vars.LUA_RUNTIME, vars.UNAME_M) +vars.COMPILER_ENV_CMD = get_vc_env_setup_cmd(vars.LUA_RUNTIME, vars.UNAME_M) print(S[[ -- cgit v1.2.3-55-g6feb From d4b2ec1aa1291ec2b06ba161bc960fb848b98132 Mon Sep 17 00:00:00 2001 From: Xpol Wan Date: Tue, 27 Oct 2015 00:19:19 +0800 Subject: Better msvc compiler setup compatibility. 1. Use vcvarsall.bat if exists. 2. Use vcvars32.bat / vcvars64.bat if exists. 3. Do no compiler env setup in luarocks.bat if 1 and 2 are both not exists. --- install.bat | 54 ++++++++++++++++++++++++++++++++---------------------- 1 file changed, 32 insertions(+), 22 deletions(-) diff --git a/install.bat b/install.bat index 533a0700..d2ab2976 100644 --- a/install.bat +++ b/install.bat @@ -411,38 +411,48 @@ end -- get a string value from windows registry. local function get_registry(key, value) - local h = io.popen('reg query "'..key..'" /v '..value..' 2>NUL') - local output = h:read('*a') - h:close() + local h = io.popen('reg query "'..key..'" /v '..value..' 2>NUL') + local output = h:read('*a') + h:close() - return output:match('REG_SZ%s+([^\n]+)') + return output:match('REG_SZ%s+([^\n]+)') end local function visual_studio_registry_key(major, minor) - local key = "HKEY_LOCAL_MACHINE\\SOFTWARE%s\\Microsoft\\VisualStudio\\%d.%d\\Setup\\VC" - -- os.getenv('PROCESSOR_ARCHITECTURE') will always return 'x86' if lua interpreter is 32 bit. - local hostarch64 = os.getenv("ProgramFiles(x86)")~=nil - return key:format(hostarch64 and "\\Wow6432Node" or "", major, minor) + local key = "HKLM\\SOFTWARE%s\\Microsoft\\VisualStudio\\%d.%d\\Setup\\VC" + -- os.getenv('PROCESSOR_ARCHITECTURE') will always return 'x86' if lua interpreter is 32 bit. + local hostx64 = os.getenv("ProgramFiles(x86)")~=nil + return key:format(hostx64 and "\\Wow6432Node" or "", major, minor) +end + +-- requires vars.LUA_RUNTIME to be set before calling this function. +local function get_visual_studio_directory() + local major, minor = vars.LUA_RUNTIME:match('VCR%u*(%d+)(%d)$') -- MSVCR or VCRUNTIME + if not major then return "" end + + return get_registry(visual_studio_registry_key(major, minor), 'ProductDir') end -- returns the batch command to setup msvc compiler path. -- requires vars.LUA_RUNTIME and vars.UNAME_M to be set before calling this function. -local function get_vc_env_setup_cmd() - -- 1. check installed lua runtime version - local major, minor = vars.LUA_RUNTIME:match('^MSVCR(%d+)(%d)$') - if not major then return "" end +local function get_msvc_env_setup_cmd() + local product_dir = get_visual_studio_directory() + local x64 = vars.UNAME_M=="x86_64" - -- 2. check if required VC version is in registry - local product_dir = get_registry(visual_studio_registry_key(major, minor), 'ProductDir') - - if not product_dir then return "" end + -- 1. try vcvarsall.bat + local vcvarsall = product_dir .. 'vcvarsall.bat' + if exists(vcvarsall) then + return ('call "%s"%s'):format(vcvarsall, x64 and ' amd64' or '') + end - -- 4. check VC batch file exists - local relative_path = vars.UNAME_M=="x86_64" and "bin\\amd64\\vcvars64.bat" or "bin\\vcvars32.bat" - local full_path = product_dir .. relative_path - if not exists(full_path) then return "" end + -- 2. try vcvars32.bat / vcvars64.bat + local relative_path = x64 and "bin\\amd64\\vcvars64.bat" or "bin\\vcvars32.bat" + local full_path = product_dir .. relative_path + if exists(full_path) then + return ('call "%s"'):format(full_path) + end - return ('call "%s"'):format(full_path) + return "" end local function look_for_lua_install () @@ -663,7 +673,7 @@ if SELFCONTAINED then vars.TREE_ROOT = vars.PREFIX..[[\systree]] REGISTRY = false end -vars.COMPILER_ENV_CMD = get_vc_env_setup_cmd(vars.LUA_RUNTIME, vars.UNAME_M) +vars.COMPILER_ENV_CMD = get_msvc_env_setup_cmd() print(S[[ -- cgit v1.2.3-55-g6feb From a5cec1848d8318f4ea8f28daf2f221d71178cf5d Mon Sep 17 00:00:00 2001 From: Xpol Wan Date: Tue, 27 Oct 2015 00:23:01 +0800 Subject: Add comments about Windows SDKs. --- install.bat | 3 +++ 1 file changed, 3 insertions(+) diff --git a/install.bat b/install.bat index d2ab2976..de572d3d 100644 --- a/install.bat +++ b/install.bat @@ -452,6 +452,9 @@ local function get_msvc_env_setup_cmd() return ('call "%s"'):format(full_path) end + -- 3. TODO: add support for Windows SDKs here. + + -- finaly, we can't detect more, just don't setup the msvc compiler in luarocks.bat. return "" end -- cgit v1.2.3-55-g6feb From 04e45199b0a76808fce9850713ba8e79a47cb9fd Mon Sep 17 00:00:00 2001 From: Xpol Wan Date: Tue, 27 Oct 2015 00:35:51 +0800 Subject: Don't setup msvc when user desires MinGW. --- install.bat | 2 ++ 1 file changed, 2 insertions(+) diff --git a/install.bat b/install.bat index de572d3d..c3099988 100644 --- a/install.bat +++ b/install.bat @@ -436,6 +436,8 @@ end -- returns the batch command to setup msvc compiler path. -- requires vars.LUA_RUNTIME and vars.UNAME_M to be set before calling this function. local function get_msvc_env_setup_cmd() + if USE_MINGW then return "" end -- Don't setup msvc for MinGW. + local product_dir = get_visual_studio_directory() local x64 = vars.UNAME_M=="x86_64" -- cgit v1.2.3-55-g6feb From 00184f35d70af9b2cf746e8a2de156d06c88101b Mon Sep 17 00:00:00 2001 From: Xpol Wan Date: Tue, 27 Oct 2015 00:39:07 +0800 Subject: Move mingw test out of get_msvc_env_setup_cmd(). --- install.bat | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/install.bat b/install.bat index c3099988..cfc5f074 100644 --- a/install.bat +++ b/install.bat @@ -436,8 +436,6 @@ end -- returns the batch command to setup msvc compiler path. -- requires vars.LUA_RUNTIME and vars.UNAME_M to be set before calling this function. local function get_msvc_env_setup_cmd() - if USE_MINGW then return "" end -- Don't setup msvc for MinGW. - local product_dir = get_visual_studio_directory() local x64 = vars.UNAME_M=="x86_64" @@ -678,7 +676,7 @@ if SELFCONTAINED then vars.TREE_ROOT = vars.PREFIX..[[\systree]] REGISTRY = false end -vars.COMPILER_ENV_CMD = get_msvc_env_setup_cmd() +vars.COMPILER_ENV_CMD = USE_MINGW and "" or get_msvc_env_setup_cmd() print(S[[ -- cgit v1.2.3-55-g6feb From 4c3b41e765cf42afecfba8e32965e5c057c96870 Mon Sep 17 00:00:00 2001 From: Xpol Wan Date: Sun, 1 Nov 2015 13:35:33 +0800 Subject: Add support for Windows SDK v7.1 and v6.1. Other Windows SDK versions requires a separate installation of Visual Studio. --- install.bat | 84 +++++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 60 insertions(+), 24 deletions(-) diff --git a/install.bat b/install.bat index cfc5f074..0514af1b 100644 --- a/install.bat +++ b/install.bat @@ -411,48 +411,84 @@ end -- get a string value from windows registry. local function get_registry(key, value) - local h = io.popen('reg query "'..key..'" /v '..value..' 2>NUL') - local output = h:read('*a') - h:close() + local keys = {key} + local key64, replaces = key:gsub("(%u+\\SOFTWARE\\)", "\1Wow6432Node\\", 1) + if replaces == 0 then + key64, replaces = key:gsub("(%u+\\Software\\)", "\1Wow6432Node\\", 1) + end - return output:match('REG_SZ%s+([^\n]+)') -end + if replaces == 1 then + table.insert(keys, 1, key64) + end + + for _, k in ipairs(keys) do + local h = io.popen('reg query "'..k..'" /v '..value..' 2>NUL') + local output = h:read('*a') + h:close() -local function visual_studio_registry_key(major, minor) - local key = "HKLM\\SOFTWARE%s\\Microsoft\\VisualStudio\\%d.%d\\Setup\\VC" - -- os.getenv('PROCESSOR_ARCHITECTURE') will always return 'x86' if lua interpreter is 32 bit. - local hostx64 = os.getenv("ProgramFiles(x86)")~=nil - return key:format(hostx64 and "\\Wow6432Node" or "", major, minor) + local v = output:match('REG_SZ%s+([^\n]+)') + if v then + return v + end + end + return nil end -- requires vars.LUA_RUNTIME to be set before calling this function. local function get_visual_studio_directory() local major, minor = vars.LUA_RUNTIME:match('VCR%u*(%d+)(%d)$') -- MSVCR or VCRUNTIME if not major then return "" end - - return get_registry(visual_studio_registry_key(major, minor), 'ProductDir') + local key = ("HKLM\\SOFTWARE\\Microsoft\\VisualStudio\\%d.%d\\Setup\\VC"):format(major, minor) + return get_registry(key, 'ProductDir') end +-- requires vars.LUA_RUNTIME to be set before calling this function. +local function get_windows_sdk_directory() + -- Only v7.1 and v6.1 shipped with compilers + -- Other versions requires a separate installation of Visual Studio. + -- see https://github.com/keplerproject/luarocks/pull/443#issuecomment-152792516 + local wsdks = { + ["MSVCR100"] = "v7.1", -- shipped with Visual Studio 2010 compilers. + ["MSVCR90"] = "v6.1", -- shipped with Visual Studio 2008 compilers. + } + local wsdkver = wsdks[vars.LUA_RUNTIME] + if not wsdkver then + return nil + end + + local key = "HKLM\\SOFTWARE\\Microsoft\\Microsoft SDKs\\Windows\\"..wsdkver + return get_registry(key, 'InstallationFolder') +end -- returns the batch command to setup msvc compiler path. -- requires vars.LUA_RUNTIME and vars.UNAME_M to be set before calling this function. local function get_msvc_env_setup_cmd() - local product_dir = get_visual_studio_directory() local x64 = vars.UNAME_M=="x86_64" - -- 1. try vcvarsall.bat - local vcvarsall = product_dir .. 'vcvarsall.bat' - if exists(vcvarsall) then - return ('call "%s"%s'):format(vcvarsall, x64 and ' amd64' or '') - end + -- 1. try visual studio command line tools + local vcdir = get_visual_studio_directory() + if vcdir then + -- 1.1. try vcvarsall.bat + local vcvarsall = vcdir .. 'vcvarsall.bat' + if exists(vcvarsall) then + return ('call "%s"%s'):format(vcvarsall, x64 and ' amd64' or '') + end - -- 2. try vcvars32.bat / vcvars64.bat - local relative_path = x64 and "bin\\amd64\\vcvars64.bat" or "bin\\vcvars32.bat" - local full_path = product_dir .. relative_path - if exists(full_path) then - return ('call "%s"'):format(full_path) + -- 1.2. try vcvars32.bat / vcvars64.bat + local relative_path = x64 and "bin\\amd64\\vcvars64.bat" or "bin\\vcvars32.bat" + local full_path = vcdir .. relative_path + if exists(full_path) then + return ('call "%s"'):format(full_path) + end end - -- 3. TODO: add support for Windows SDKs here. + -- 2. try for Windows SDKs command line tools. + local wsdkdir = get_windows_sdk_directory() + if wsdkdir then + local setenv = wsdkdir.."Bin\\SetEnv.cmd" + if exists(setenv) then + return ("call \"%s\" /Release /%s"):format(setenv, x64 and "x64" or "x86") + end + end -- finaly, we can't detect more, just don't setup the msvc compiler in luarocks.bat. return "" -- cgit v1.2.3-55-g6feb From 21839814515a42e9c2da4a9cbc28029bddbab43e Mon Sep 17 00:00:00 2001 From: Xpol Wan Date: Sun, 1 Nov 2015 14:08:09 +0800 Subject: Support Visual Studio Express Editions and cleanup code. --- install.bat | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/install.bat b/install.bat index 0514af1b..01461667 100644 --- a/install.bat +++ b/install.bat @@ -412,21 +412,18 @@ end -- get a string value from windows registry. local function get_registry(key, value) local keys = {key} - local key64, replaces = key:gsub("(%u+\\SOFTWARE\\)", "\1Wow6432Node\\", 1) - if replaces == 0 then - key64, replaces = key:gsub("(%u+\\Software\\)", "\1Wow6432Node\\", 1) - end + local key64, replaced = key:gsub("(%u+\\Software\\)", "\1Wow6432Node\\", 1) - if replaces == 1 then - table.insert(keys, 1, key64) + if replaced == 1 then + keys = {key64, key} end for _, k in ipairs(keys) do local h = io.popen('reg query "'..k..'" /v '..value..' 2>NUL') - local output = h:read('*a') + local output = h:read("*a") h:close() - local v = output:match('REG_SZ%s+([^\n]+)') + local v = output:match("REG_SZ%s+([^\n]+)") if v then return v end @@ -437,9 +434,16 @@ end -- requires vars.LUA_RUNTIME to be set before calling this function. local function get_visual_studio_directory() local major, minor = vars.LUA_RUNTIME:match('VCR%u*(%d+)(%d)$') -- MSVCR or VCRUNTIME - if not major then return "" end - local key = ("HKLM\\SOFTWARE\\Microsoft\\VisualStudio\\%d.%d\\Setup\\VC"):format(major, minor) - return get_registry(key, 'ProductDir') + if not major then return nil end + local keys = { + "HKLM\\Software\\Microsoft\\VisualStudio\\%d.%d\\Setup\\VC", + "HKLM\\Software\\Microsoft\\VCExpress\\%d.%d\\Setup\\VS" + } + for _, key in ipairs(keys) do + local vcdir = get_registry(key:format(major, minor), "ProductDir") + if vcdir then return vcdir end + end + return nil end -- requires vars.LUA_RUNTIME to be set before calling this function. @@ -456,8 +460,8 @@ local function get_windows_sdk_directory() return nil end - local key = "HKLM\\SOFTWARE\\Microsoft\\Microsoft SDKs\\Windows\\"..wsdkver - return get_registry(key, 'InstallationFolder') + local key = "HKLM\\Software\\Microsoft\\Microsoft SDKs\\Windows\\"..wsdkver + return get_registry(key, "InstallationFolder") end -- returns the batch command to setup msvc compiler path. -- requires vars.LUA_RUNTIME and vars.UNAME_M to be set before calling this function. @@ -486,7 +490,7 @@ local function get_msvc_env_setup_cmd() if wsdkdir then local setenv = wsdkdir.."Bin\\SetEnv.cmd" if exists(setenv) then - return ("call \"%s\" /Release /%s"):format(setenv, x64 and "x64" or "x86") + return ('call "%s" /Release /%s'):format(setenv, x64 and "x64" or "x86") end end -- cgit v1.2.3-55-g6feb From c9ed3e1e72d0a58f26692b2dab84b6b0cec382a1 Mon Sep 17 00:00:00 2001 From: Xpol Wan Date: Sun, 1 Nov 2015 15:04:36 +0800 Subject: Enable batch script delayed expansion and extensions. This is required for Windows SDK setenv.cmd. --- install.bat | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/install.bat b/install.bat index 01461667..79b71645 100644 --- a/install.bat +++ b/install.bat @@ -820,7 +820,7 @@ for _, c in ipairs{"luarocks", "luarocks-admin"} do local f = io.open(vars.BINDIR.."\\"..c..".bat", "w") f:write(S[[ @ECHO OFF -SETLOCAL +SETLOCAL ENABLEDELAYEDEXPANSION ENABLEEXTENSIONS $COMPILER_ENV_CMD SET "LUA_PATH=$LUADIR\?.lua;$LUADIR\?\init.lua;%LUA_PATH%" IF NOT "%LUA_PATH_5_2%"=="" ( -- cgit v1.2.3-55-g6feb From c8459585c9cb0f52d3a117125b537bd98f5515ab Mon Sep 17 00:00:00 2001 From: xpol Date: Tue, 3 Nov 2015 11:42:35 +0800 Subject: User assertion for required condition check. --- install.bat | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/install.bat b/install.bat index 79b71645..ab8618b9 100644 --- a/install.bat +++ b/install.bat @@ -431,8 +431,8 @@ local function get_registry(key, value) return nil end --- requires vars.LUA_RUNTIME to be set before calling this function. local function get_visual_studio_directory() + assert(type(vars.LUA_RUNTIME)=="string", "requires vars.LUA_RUNTIME to be set before calling this function.") local major, minor = vars.LUA_RUNTIME:match('VCR%u*(%d+)(%d)$') -- MSVCR or VCRUNTIME if not major then return nil end local keys = { @@ -446,8 +446,8 @@ local function get_visual_studio_directory() return nil end --- requires vars.LUA_RUNTIME to be set before calling this function. local function get_windows_sdk_directory() + assert(type(vars.LUA_RUNTIME) == "string", "requires vars.LUA_RUNTIME to be set before calling this function.") -- Only v7.1 and v6.1 shipped with compilers -- Other versions requires a separate installation of Visual Studio. -- see https://github.com/keplerproject/luarocks/pull/443#issuecomment-152792516 @@ -464,8 +464,8 @@ local function get_windows_sdk_directory() return get_registry(key, "InstallationFolder") end -- returns the batch command to setup msvc compiler path. --- requires vars.LUA_RUNTIME and vars.UNAME_M to be set before calling this function. local function get_msvc_env_setup_cmd() + assert(type(vars.UNAME_M) == "string", "requires vars.UNAME_M to be set before calling this function.") local x64 = vars.UNAME_M=="x86_64" -- 1. try visual studio command line tools -- cgit v1.2.3-55-g6feb