summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2023-06-22 23:33:49 +0800
committerGitHub <[email protected]>2023-06-22 23:33:49 +0800
commit15d078e5fdcf93632bf135272edf43d94616296a (patch)
tree319b8323bab5f4220a50525ef21c5f31e4d1a2e8
parent3168a2fd823af3fc068aef67daa71da0e8f978b4 (diff)
parent1bda9dce9f0acb17a1357dea95263587a01863ff (diff)
Merge pull request #3876 from xmake-io/msbuild
improve msbuild to upgrade vs project
-rw-r--r--xmake/modules/package/tools/msbuild.lua13
-rw-r--r--xmake/modules/private/utils/upgrade_vsproj.lua90
-rw-r--r--xmake/plugins/project/vstudio/impl/vs201x_vcxproj.lua2
3 files changed, 100 insertions, 5 deletions
diff --git a/xmake/modules/package/tools/msbuild.lua b/xmake/modules/package/tools/msbuild.lua
index e3194322e..21fd2e693 100644
--- a/xmake/modules/package/tools/msbuild.lua
+++ b/xmake/modules/package/tools/msbuild.lua
@@ -22,13 +22,13 @@
import("core.base.option")
import("core.tool.toolchain")
import("lib.detect.find_tool")
+import("private.utils.upgrade_vsproj")
-- get the number of parallel jobs
function _get_parallel_njobs(opt)
return opt.jobs or option.get("jobs") or tostring(os.default_njob())
end
--- tra
-- get msvc
function _get_msvc(package)
local msvc = toolchain.load("msvc", {plat = package:plat(), arch = package:arch()})
@@ -74,8 +74,6 @@ end
-- build package
function build(package, configs, opt)
-
- -- init options
opt = opt or {}
-- pass configurations
@@ -91,6 +89,15 @@ function build(package, configs, opt)
end
end
+ -- upgrade vs solution file?
+ -- @see https://github.com/xmake-io/xmake/issues/3871
+ if opt.upgrade then
+ local msvc = _get_msvc(package)
+ for _, value in ipairs(opt.upgrade) do
+ upgrade_vsproj.upgrade(value, table.join(opt, {msvc = msvc}))
+ end
+ end
+
-- do build
local envs = opt.envs or buildenvs(package, opt)
local msbuild = find_tool("msbuild", {envs = envs})
diff --git a/xmake/modules/private/utils/upgrade_vsproj.lua b/xmake/modules/private/utils/upgrade_vsproj.lua
new file mode 100644
index 000000000..373c4d955
--- /dev/null
+++ b/xmake/modules/private/utils/upgrade_vsproj.lua
@@ -0,0 +1,90 @@
+--!A cross-platform build utility based on Lua
+--
+-- Licensed 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-present, TBOOX Open Source Group.
+--
+-- @author ruki
+-- @file upgrade_vsproj.lua
+--
+
+-- imports
+import("core.base.option")
+import("core.tool.toolchain")
+import("plugins.project.vstudio.impl.vsinfo", {rootdir = os.programdir()})
+
+-- the options
+local options = {
+ {nil, "vs", "kv", nil, "Set the vs version. (default: latest)." }
+, {nil, "vs_toolset", "kv", nil, "Set the vs toolset." }
+, {nil, "vs_sdkver", "kv", nil, "Set the vs sdk version." }
+, {nil, "vs_projectfiles", "vs", nil, "Set the solution or project files." }
+}
+
+-- get toolset version
+function _get_toolset_ver(vs_toolset)
+ local toolset_ver = nil
+ if vs_toolset then
+ local verinfo = vs_toolset:split('%.')
+ if #verinfo >= 2 then
+ toolset_ver = "v" .. verinfo[1] .. (verinfo[2]:sub(1, 1) or "0")
+ end
+ end
+ return toolset_ver
+end
+
+-- upgrade *.sln
+function _upgrade_sln(projectfile, opt)
+ opt = opt or {}
+ local msvc = opt.msvc or import("core.tool.toolchain").load("msvc")
+ local vs_version = opt.vs or msvc:config("vs")
+ local vs_info = assert(vsinfo(tonumber(vs_version)), "unknown vs version!")
+ io.gsub(projectfile, "Microsoft Visual Studio Solution File, Format Version %d+%.%d+",
+ "Microsoft Visual Studio Solution File, Format Version " .. vs_info.solution_version .. ".00")
+ io.gsub(projectfile, "# Visual Studio %d+", "# Visual Studio " .. vs_version)
+end
+
+-- upgrade *.vcxproj
+function _upgrade_vcxproj(projectfile, opt)
+ opt = opt or {}
+ local msvc = opt.msvc or import("core.tool.toolchain").load("msvc")
+ local vs_version = opt.vs or msvc:config("vs")
+ local vs_info = assert(vsinfo(tonumber(vs_version)), "unknown vs version!")
+ local vs_sdkver = opt.vs_sdkver or msvc:config("vs_sdkver") or vs_info.sdk_version
+ local vs_toolset = _get_toolset_ver(opt.vs_toolset or msvc:config("vs_toolset")) or vs_info.toolset_version
+ io.gsub(projectfile, "<PlatformToolset>v%d+</PlatformToolset>",
+ "<PlatformToolset>" .. vs_toolset .. "</PlatformToolset>")
+ io.gsub(projectfile, "<WindowsTargetPlatformVersion>.*</WindowsTargetPlatformVersion>",
+ "<WindowsTargetPlatformVersion>" .. vs_sdkver .. "</WindowsTargetPlatformVersion>")
+end
+
+-- upgrade vs project file
+function upgrade(projectfile, opt)
+ if projectfile:endswith(".sln") then
+ _upgrade_sln(projectfile, opt)
+ elseif projectfile:endswith(".vcxproj") then
+ _upgrade_vcxproj(projectfile, opt)
+ end
+end
+
+-- https://github.com/xmake-io/xmake/issues/3871
+function main(...)
+ local argv = {...}
+ local opt = option.parse(argv, options, "Upgrade all the vs project files."
+ , ""
+ , "Usage: xmake l private.utils.upgrade_vsproj [options]")
+
+ for _, projectfile in ipairs(opt.vs_projectfiles) do
+ upgrade(projectfile, opt)
+ end
+end
diff --git a/xmake/plugins/project/vstudio/impl/vs201x_vcxproj.lua b/xmake/plugins/project/vstudio/impl/vs201x_vcxproj.lua
index 64a6f5419..10ddb8dbc 100644
--- a/xmake/plugins/project/vstudio/impl/vs201x_vcxproj.lua
+++ b/xmake/plugins/project/vstudio/impl/vs201x_vcxproj.lua
@@ -214,8 +214,6 @@ end
-- make header
function _make_header(vcxprojfile, vsinfo)
-
- -- make header
vcxprojfile:print("<?xml version=\"1.0\" encoding=\"utf-8\"?>")
vcxprojfile:enter("<Project DefaultTargets=\"Build\" ToolsVersion=\"%s.0\" xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\">", assert(vsinfo.project_version))
end