Skip to content

Commit

Permalink
Merge pull request #5626 from xmake-io/runtimes
Browse files Browse the repository at this point in the history
Switch the default vs runtime as MD when 3.0
  • Loading branch information
waruqi authored Sep 16, 2024
2 parents ea1ed38 + 3223ff3 commit 971318d
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 174 deletions.
69 changes: 0 additions & 69 deletions tests/apis/add_allowedxxx/xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
--

8 changes: 8 additions & 0 deletions tests/plugins/macro/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Xmake cache
.xmake/
build/

# MacOS Cache
.DS_Store


6 changes: 6 additions & 0 deletions tests/plugins/macro/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <iostream>

int main(int argc, char** argv) {
std::cout << "hello world!" << std::endl;
return 0;
}
11 changes: 11 additions & 0 deletions tests/plugins/macro/test.lua
Original file line number Diff line number Diff line change
@@ -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

6 changes: 6 additions & 0 deletions tests/plugins/macro/xmake.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
add_rules("mode.debug", "mode.release")

target("macro")
set_kind("binary")
add_files("src/*.cpp")

69 changes: 0 additions & 69 deletions tests/projects/package/brew/xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
--

4 changes: 1 addition & 3 deletions tests/test_utils/context.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
end
31 changes: 2 additions & 29 deletions tests/test_utils/test_build.lua
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
24 changes: 22 additions & 2 deletions xmake/core/project/policy.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions xmake/core/project/project.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion xmake/modules/private/action/require/impl/package.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion xmake/rules/c++/xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 971318d

Please sign in to comment.