summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2024-09-16 18:46:53 +0800
committerGitHub <[email protected]>2024-09-16 18:46:53 +0800
commit971318d9d33ab5190745cb4a86b4c75e916e72e4 (patch)
tree5c236046b908a898de0c51acb84aa97b218c491f
parentea1ed3818378496488e693bd264a520d84cdacea (diff)
parent3223ff3972ae01d8325f993107ed925acebd016c (diff)
Merge pull request #5626 from xmake-io/runtimes
Switch the default vs runtime as MD when 3.0
-rw-r--r--tests/apis/add_allowedxxx/xmake.lua69
-rw-r--r--tests/plugins/macro/.gitignore8
-rw-r--r--tests/plugins/macro/src/main.cpp6
-rw-r--r--tests/plugins/macro/test.lua11
-rw-r--r--tests/plugins/macro/xmake.lua6
-rw-r--r--tests/projects/package/brew/xmake.lua69
-rw-r--r--tests/test_utils/context.lua4
-rw-r--r--tests/test_utils/test_build.lua31
-rw-r--r--xmake/core/project/policy.lua24
-rw-r--r--xmake/core/project/project.lua2
-rw-r--r--xmake/modules/private/action/require/impl/package.lua6
-rw-r--r--xmake/rules/c++/xmake.lua12
12 files changed, 74 insertions, 174 deletions
diff --git a/tests/apis/add_allowedxxx/xmake.lua b/tests/apis/add_allowedxxx/xmake.lua
index d15f0ee32..1d99a8ecc 100644
--- a/tests/apis/add_allowedxxx/xmake.lua
+++ b/tests/apis/add_allowedxxx/xmake.lua
@@ -12,72 +12,3 @@ target("test")
set_kind("binary")
add_files("src/*.cpp")
---
--- If you want to known more usage about xmake, please see https://xmake.io
---
--- ## FAQ
---
--- You can enter the project directory firstly before building project.
---
--- $ cd projectdir
---
--- 1. How to build project?
---
--- $ xmake
---
--- 2. How to configure project?
---
--- $ xmake f -p [macosx|linux|iphoneos ..] -a [x86_64|i386|arm64 ..] -m [debug|release]
---
--- 3. Where is the build output directory?
---
--- The default output directory is `./build` and you can configure the output directory.
---
--- $ xmake f -o outputdir
--- $ xmake
---
--- 4. How to run and debug target after building project?
---
--- $ xmake run [targetname]
--- $ xmake run -d [targetname]
---
--- 5. How to install target to the system directory or other output directory?
---
--- $ xmake install
--- $ xmake install -o installdir
---
--- 6. Add some frequently-used compilation flags in xmake.lua
---
--- @code
--- -- add debug and release modes
--- add_rules("mode.debug", "mode.release")
---
--- -- add macro definition
--- add_defines("NDEBUG", "_GNU_SOURCE=1")
---
--- -- set all warnings as errors
--- set_warnings("all", "error")
---
--- -- set language: c99, c++11
--- set_languages("c99", "c++11")
---
--- -- set optimization: none, faster, fastest, smallest
--- set_optimize("fastest")
---
--- -- add include search directories
--- add_includedirs("/usr/include", "/usr/local/include")
---
--- -- add link libraries and search directories
--- add_links("tbox")
--- add_linkdirs("/usr/local/lib", "/usr/lib")
---
--- -- add system link libraries
--- add_syslinks("z", "pthread")
---
--- -- add compilation and link flags
--- add_cxflags("-stdnolib", "-fno-strict-aliasing")
--- add_ldflags("-L/usr/local/lib", "-lpthread", {force = true})
---
--- @endcode
---
-
diff --git a/tests/plugins/macro/.gitignore b/tests/plugins/macro/.gitignore
new file mode 100644
index 000000000..152105761
--- /dev/null
+++ b/tests/plugins/macro/.gitignore
@@ -0,0 +1,8 @@
+# Xmake cache
+.xmake/
+build/
+
+# MacOS Cache
+.DS_Store
+
+
diff --git a/tests/plugins/macro/src/main.cpp b/tests/plugins/macro/src/main.cpp
new file mode 100644
index 000000000..7c775d288
--- /dev/null
+++ b/tests/plugins/macro/src/main.cpp
@@ -0,0 +1,6 @@
+#include <iostream>
+
+int main(int argc, char** argv) {
+ std::cout << "hello world!" << std::endl;
+ return 0;
+}
diff --git a/tests/plugins/macro/test.lua b/tests/plugins/macro/test.lua
new file mode 100644
index 000000000..54955a21d
--- /dev/null
+++ b/tests/plugins/macro/test.lua
@@ -0,0 +1,11 @@
+function test_macro(t)
+ -- we force to enable ccache to test it on ci
+ os.exec("xmake f --mode=debug --policies=build.ccache:y -D -y")
+ os.exec("xmake m -b")
+ os.exec("xmake -r -a -D")
+ os.exec("xmake m -e buildtest")
+ os.exec("xmake m -l")
+ os.exec("xmake m buildtest")
+ os.exec("xmake m -d buildtest")
+end
+
diff --git a/tests/plugins/macro/xmake.lua b/tests/plugins/macro/xmake.lua
new file mode 100644
index 000000000..a565ecb6d
--- /dev/null
+++ b/tests/plugins/macro/xmake.lua
@@ -0,0 +1,6 @@
+add_rules("mode.debug", "mode.release")
+
+target("macro")
+ set_kind("binary")
+ add_files("src/*.cpp")
+
diff --git a/tests/projects/package/brew/xmake.lua b/tests/projects/package/brew/xmake.lua
index 2b0128fda..144488e84 100644
--- a/tests/projects/package/brew/xmake.lua
+++ b/tests/projects/package/brew/xmake.lua
@@ -7,72 +7,3 @@ target("brew")
add_files("src/*.cpp")
add_packages("pcre2")
---
--- If you want to known more usage about xmake, please see https://xmake.io
---
--- ## FAQ
---
--- You can enter the project directory firstly before building project.
---
--- $ cd projectdir
---
--- 1. How to build project?
---
--- $ xmake
---
--- 2. How to configure project?
---
--- $ xmake f -p [macosx|linux|iphoneos ..] -a [x86_64|i386|arm64 ..] -m [debug|release]
---
--- 3. Where is the build output directory?
---
--- The default output directory is `./build` and you can configure the output directory.
---
--- $ xmake f -o outputdir
--- $ xmake
---
--- 4. How to run and debug target after building project?
---
--- $ xmake run [targetname]
--- $ xmake run -d [targetname]
---
--- 5. How to install target to the system directory or other output directory?
---
--- $ xmake install
--- $ xmake install -o installdir
---
--- 6. Add some frequently-used compilation flags in xmake.lua
---
--- @code
--- -- add debug and release modes
--- add_rules("mode.debug", "mode.release")
---
--- -- add macro definition
--- add_defines("NDEBUG", "_GNU_SOURCE=1")
---
--- -- set warning all as error
--- set_warnings("all", "error")
---
--- -- set language: c99, c++11
--- set_languages("c99", "c++11")
---
--- -- set optimization: none, faster, fastest, smallest
--- set_optimize("fastest")
---
--- -- add include search directories
--- add_includedirs("/usr/include", "/usr/local/include")
---
--- -- add link libraries and search directories
--- add_links("tbox")
--- add_linkdirs("/usr/local/lib", "/usr/lib")
---
--- -- add system link libraries
--- add_syslinks("z", "pthread")
---
--- -- add compilation and link flags
--- add_cxflags("-stdnolib", "-fno-strict-aliasing")
--- add_ldflags("-L/usr/local/lib", "-lpthread", {force = true})
---
--- @endcode
---
-
diff --git a/tests/test_utils/context.lua b/tests/test_utils/context.lua
index 50271ac76..06cd5b6d6 100644
--- a/tests/test_utils/context.lua
+++ b/tests/test_utils/context.lua
@@ -3,11 +3,9 @@ import("test_skip")
import("test_assert")
function main(filename)
-
local context = { filename = filename }
table.join2(context, test_build())
table.join2(context, test_skip())
table.join2(context, test_assert())
-
return context
-end \ No newline at end of file
+end
diff --git a/tests/test_utils/test_build.lua b/tests/test_utils/test_build.lua
index 43941d320..b456d7749 100644
--- a/tests/test_utils/test_build.lua
+++ b/tests/test_utils/test_build.lua
@@ -1,37 +1,10 @@
--- imports
-import("privilege.sudo")
-
local test_build = {}
function test_build:build(argv)
-
- -- check global config
- os.exec("xmake g -c")
-
- -- generic?
os.exec("xmake f -c -D -y")
os.exec("xmake")
- os.exec("xmake p -D")
- if not is_host("windows") then
- os.exec("xmake install -o $(tmpdir) -a -D")
- os.exec("xmake uninstall --installdir=$(tmpdir) -D")
- end
- os.exec("xmake c -D")
- -- we force to enable ccache to test it on ci
- os.exec("xmake f --mode=debug --policies=build.ccache:y -D -y")
- os.exec("xmake m -b")
- os.exec("xmake -r -a -D")
- os.exec("xmake m -e buildtest")
- os.exec("xmake m -l")
- os.exec("xmake m buildtest")
- os.exec("xmake m -d buildtest")
-
- -- test iphoneos?
- if argv and argv.iphoneos then
- if is_host("macosx") then
- os.exec("xmake m package -p iphoneos")
- end
- end
+ os.exec("xmake f -c -D -y --policies=compatibility.version=3.0")
+ os.exec("xmake -r")
end
function main()
diff --git a/xmake/core/project/policy.lua b/xmake/core/project/policy.lua
index e82774a0c..dcef4c421 100644
--- a/xmake/core/project/policy.lua
+++ b/xmake/core/project/policy.lua
@@ -84,6 +84,8 @@ function policy.policies()
["build.c++.clang.fallbackscanner"] = {description = "Force clang fallback module dependency scanner.", default = false, type = "boolean"},
-- Force C++ modules fallback dependency scanner for msvc
["build.c++.msvc.fallbackscanner"] = {description = "Force msvc fallback module dependency scanner.", default = false, type = "boolean"},
+ -- Set the default vs runtime, e.g. MT, MD
+ ["build.c++.msvc.runtime"] = {description = "Set the default vs runtime.", type = "string", values = {"MT", "MD"}},
-- Force C++ modules fallback dependency scanner for gcc
["build.c++.gcc.fallbackscanner"] = {description = "Force gcc fallback module dependency scanner.", default = false, type = "boolean"},
-- Force to enable new cxx11 abi in C++ modules for gcc
@@ -151,7 +153,7 @@ function policy.policies()
-- private: it will disable fetch remote package repositories
["network.mode"] = {description = "Set the network mode", type = "string"},
-- Set the compatibility version, e.g. 2.0, 3.0
- ["compatibility.version"] = {description = "Set the compatibility version", type = "string"}
+ ["compatibility.version"] = {description = "Set the compatibility version", type = "string", values = {"2.0", "3.0"}}
}
policy._POLICIES = policies
end
@@ -177,7 +179,25 @@ function policy.check(name, value)
else
local valtype = type(value)
if valtype ~= defined_policy.type then
- utils.warning("policy(%s): invalid value type(%s), it shound be '%s'!", name, valtype, defined_policy.type)
+ utils.warning("policy(%s): invalid value type(%s), it shound be '%s'!",
+ name, valtype, defined_policy.type)
+ end
+ if defined_policy.values then
+ local found = false
+ for _, policy_value in ipairs(defined_policy.values) do
+ if tostring(value) == tostring(policy_value) then
+ found = true
+ break
+ end
+ end
+ if not found then
+ local values = {}
+ for _, policy_value in ipairs(defined_policy.values) do
+ table.insert(values, tostring(policy_value))
+ end
+ utils.warning("policy(%s): invalid value(%s), please set value from {%s}.",
+ name, value, table.concat(values, ", "))
+ end
end
end
return value
diff --git a/xmake/core/project/project.lua b/xmake/core/project/project.lua
index 836ece6b6..b6c7e6334 100644
--- a/xmake/core/project/project.lua
+++ b/xmake/core/project/project.lua
@@ -827,8 +827,10 @@ function project._init_default_policies()
if compatibility_version then
if semver.compare(compatibility_version, "3.0") >= 0 then
policy.set_default("package.cmake_generator.ninja", true)
+ policy.set_default("build.c++.msvc.runtime", "MD")
else
policy.set_default("package.cmake_generator.ninja", false)
+ policy.set_default("build.c++.msvc.runtime", "MT")
end
end
end
diff --git a/xmake/modules/private/action/require/impl/package.lua b/xmake/modules/private/action/require/impl/package.lua
index 48339fef3..307d1620a 100644
--- a/xmake/modules/private/action/require/impl/package.lua
+++ b/xmake/modules/private/action/require/impl/package.lua
@@ -597,7 +597,11 @@ function _finish_requireinfo(requireinfo, package)
runtimes = {}
end
if not table.contains(runtimes, "MT", "MD", "MTd", "MDd") then
- table.insert(runtimes, "MT")
+ local vs_runtime_default = project.policy("build.c++.msvc.runtime")
+ if vs_runtime_default and is_mode("debug") then
+ vs_runtime_default = vs_runtime_default .. "d"
+ end
+ table.insert(runtimes, vs_runtime_default or "MT")
end
requireinfo.configs.runtimes = table.concat(runtimes, ",")
end
diff --git a/xmake/rules/c++/xmake.lua b/xmake/rules/c++/xmake.lua
index 49e1963a9..3723b9d71 100644
--- a/xmake/rules/c++/xmake.lua
+++ b/xmake/rules/c++/xmake.lua
@@ -35,10 +35,20 @@ rule("c++.build")
add_deps("c++.build.pcheader", "c++.build.modules", "c++.build.optimization", "c++.build.sanitizer")
on_build_files("private.action.build.object", {batch = true, distcc = true})
on_config(function (target)
- -- we enable c++ exceptions by default
+ -- enable c++ exceptions by default
if target:is_plat("windows") and not target:get("exceptions") then
target:set("exceptions", "cxx")
end
+ -- enable vs runtime as MD by default
+ if target:is_plat("windows") and not target:get("runtimes") then
+ local vs_runtime_default = target:policy("build.c++.msvc.runtime")
+ if vs_runtime_default and target:has_tool("cxx", "cl", "clang_cl") then
+ if is_mode("debug") then
+ vs_runtime_default = vs_runtime_default .. "d"
+ end
+ target:set("runtimes", vs_runtime_default)
+ end
+ end
-- https://github.com/xmake-io/xmake/issues/4621
if target:is_plat("windows") and target:is_static() and target:has_tool("cxx", "tcc") then
target:set("extension", ".a")