From 95db53c2338b030d0dd99620d388882a3faeb653 Mon Sep 17 00:00:00 2001 From: ruki Date: Mon, 16 Sep 2024 22:38:51 +0800 Subject: [PATCH 1/9] add vs runtime policy --- xmake/core/project/policy.lua | 2 ++ xmake/core/project/project.lua | 2 ++ 2 files changed, 4 insertions(+) diff --git a/xmake/core/project/policy.lua b/xmake/core/project/policy.lua index e82774a0c5..acfcc2a0e7 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.", default = "MT", type = "string"}, -- 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 diff --git a/xmake/core/project/project.lua b/xmake/core/project/project.lua index 836ece6b6f..b6c7e63349 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 From be182710a90a20276ac7f2ff76547ddf8e63254e Mon Sep 17 00:00:00 2001 From: ruki Date: Mon, 16 Sep 2024 22:41:32 +0800 Subject: [PATCH 2/9] switch to MT runtime --- .../modules/private/action/require/impl/package.lua | 6 +++++- xmake/rules/c++/xmake.lua | 13 ++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/xmake/modules/private/action/require/impl/package.lua b/xmake/modules/private/action/require/impl/package.lua index 48339fef3f..307d1620a4 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 49e1963a91..432d378559 100644 --- a/xmake/rules/c++/xmake.lua +++ b/xmake/rules/c++/xmake.lua @@ -35,10 +35,21 @@ 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 vs_runtime_default == "MD" and target:has_tool("cxx", "cl", "clang_cl") then + if is_mode("debug") then + target:set("runtimes", "MDd") + else + target:set("runtimes", "MD") + end + 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") From f9b056838089fad93ef2bd09d520b9503c27a35f Mon Sep 17 00:00:00 2001 From: ruki Date: Mon, 16 Sep 2024 22:43:31 +0800 Subject: [PATCH 3/9] check policy values --- xmake/core/project/policy.lua | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/xmake/core/project/policy.lua b/xmake/core/project/policy.lua index acfcc2a0e7..0647fa0916 100644 --- a/xmake/core/project/policy.lua +++ b/xmake/core/project/policy.lua @@ -85,7 +85,7 @@ function policy.policies() -- 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.", default = "MT", type = "string"}, + ["build.c++.msvc.runtime"] = {description = "Set the default vs runtime.", default = "MT", 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 @@ -179,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 From ef52fd5ca12201966f533c92aa58a4fe1ad441f6 Mon Sep 17 00:00:00 2001 From: ruki Date: Mon, 16 Sep 2024 22:44:22 +0800 Subject: [PATCH 4/9] fix tips --- xmake/core/project/policy.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xmake/core/project/policy.lua b/xmake/core/project/policy.lua index 0647fa0916..cd842d2b9a 100644 --- a/xmake/core/project/policy.lua +++ b/xmake/core/project/policy.lua @@ -195,7 +195,7 @@ function policy.check(name, value) 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}.", + utils.warning("policy(%s): invalid value(%s), please set value from {%s}.", name, value, table.concat(values, ", ")) end end From b61bb48dd89bfa135cdcf7399add04ee04c05267 Mon Sep 17 00:00:00 2001 From: ruki Date: Mon, 16 Sep 2024 22:45:21 +0800 Subject: [PATCH 5/9] limit policy values --- xmake/core/project/policy.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xmake/core/project/policy.lua b/xmake/core/project/policy.lua index cd842d2b9a..9390d336ea 100644 --- a/xmake/core/project/policy.lua +++ b/xmake/core/project/policy.lua @@ -153,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 From 647b1efa1d3646ed0e753e3b41d0fa3486313e63 Mon Sep 17 00:00:00 2001 From: ruki Date: Mon, 16 Sep 2024 23:52:37 +0800 Subject: [PATCH 6/9] fix runtime --- xmake/core/project/policy.lua | 2 +- xmake/rules/c++/xmake.lua | 7 +++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/xmake/core/project/policy.lua b/xmake/core/project/policy.lua index 9390d336ea..dcef4c421b 100644 --- a/xmake/core/project/policy.lua +++ b/xmake/core/project/policy.lua @@ -85,7 +85,7 @@ function policy.policies() -- 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.", default = "MT", type = "string", values = {"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 diff --git a/xmake/rules/c++/xmake.lua b/xmake/rules/c++/xmake.lua index 432d378559..3723b9d71b 100644 --- a/xmake/rules/c++/xmake.lua +++ b/xmake/rules/c++/xmake.lua @@ -42,12 +42,11 @@ rule("c++.build") -- 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 vs_runtime_default == "MD" and target:has_tool("cxx", "cl", "clang_cl") then + if vs_runtime_default and target:has_tool("cxx", "cl", "clang_cl") then if is_mode("debug") then - target:set("runtimes", "MDd") - else - target:set("runtimes", "MD") + vs_runtime_default = vs_runtime_default .. "d" end + target:set("runtimes", vs_runtime_default) end end -- https://github.com/xmake-io/xmake/issues/4621 From 1a44c4f4712650117c5303e1fef87d812983f30f Mon Sep 17 00:00:00 2001 From: ruki Date: Mon, 16 Sep 2024 23:55:20 +0800 Subject: [PATCH 7/9] improve tests --- tests/plugins/macro/.gitignore | 8 ++++ tests/plugins/macro/src/main.cpp | 6 +++ tests/plugins/macro/test.lua | 11 +++++ tests/plugins/macro/xmake.lua | 75 ++++++++++++++++++++++++++++++++ tests/test_utils/context.lua | 4 +- tests/test_utils/test_build.lua | 31 +------------ 6 files changed, 103 insertions(+), 32 deletions(-) create mode 100644 tests/plugins/macro/.gitignore create mode 100644 tests/plugins/macro/src/main.cpp create mode 100644 tests/plugins/macro/test.lua create mode 100644 tests/plugins/macro/xmake.lua diff --git a/tests/plugins/macro/.gitignore b/tests/plugins/macro/.gitignore new file mode 100644 index 0000000000..1521057612 --- /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 0000000000..7c775d288c --- /dev/null +++ b/tests/plugins/macro/src/main.cpp @@ -0,0 +1,6 @@ +#include + +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 0000000000..54955a21d0 --- /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 0000000000..22e7de5d94 --- /dev/null +++ b/tests/plugins/macro/xmake.lua @@ -0,0 +1,75 @@ +add_rules("mode.debug", "mode.release") + +target("macro") + 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 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 50271ac76a..06cd5b6d6e 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 43941d3208..a548322755 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 --policy=compatibility.version=3.0") + os.exec("xmake -r") end function main() From 3c33328bc25382bbfd0995fa0a62d8f5d2fda181 Mon Sep 17 00:00:00 2001 From: ruki Date: Mon, 16 Sep 2024 23:58:20 +0800 Subject: [PATCH 8/9] fix tests --- tests/test_utils/test_build.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_utils/test_build.lua b/tests/test_utils/test_build.lua index a548322755..b456d7749a 100644 --- a/tests/test_utils/test_build.lua +++ b/tests/test_utils/test_build.lua @@ -3,7 +3,7 @@ local test_build = {} function test_build:build(argv) os.exec("xmake f -c -D -y") os.exec("xmake") - os.exec("xmake f -c -D -y --policy=compatibility.version=3.0") + os.exec("xmake f -c -D -y --policies=compatibility.version=3.0") os.exec("xmake -r") end From 3223ff3972ae01d8325f993107ed925acebd016c Mon Sep 17 00:00:00 2001 From: ruki Date: Mon, 16 Sep 2024 23:59:25 +0800 Subject: [PATCH 9/9] remove FAQ --- tests/apis/add_allowedxxx/xmake.lua | 69 --------------------------- tests/plugins/macro/xmake.lua | 69 --------------------------- tests/projects/package/brew/xmake.lua | 69 --------------------------- 3 files changed, 207 deletions(-) diff --git a/tests/apis/add_allowedxxx/xmake.lua b/tests/apis/add_allowedxxx/xmake.lua index d15f0ee324..1d99a8eccb 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/xmake.lua b/tests/plugins/macro/xmake.lua index 22e7de5d94..a565ecb6d7 100644 --- a/tests/plugins/macro/xmake.lua +++ b/tests/plugins/macro/xmake.lua @@ -4,72 +4,3 @@ target("macro") 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 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/projects/package/brew/xmake.lua b/tests/projects/package/brew/xmake.lua index 2b0128fda3..144488e846 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 --- -