diff options
| author | ruki <[email protected]> | 2025-12-02 23:50:53 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2025-12-02 23:50:53 +0800 |
| commit | e5b5b4dab997c27836d78ba5c49f428b67c3f31a (patch) | |
| tree | 62cbb65098027853af166834af2c2947e31f174c | |
| parent | bc7caf95a9cfdefed930937e8450997a4cdc8191 (diff) | |
add dynamic debugging policy
| -rw-r--r-- | xmake/core/project/policy.lua | 2 | ||||
| -rw-r--r-- | xmake/rules/c++/config/dynamic_debugging.lua | 96 | ||||
| -rw-r--r-- | xmake/rules/c++/config/main.lua | 10 |
3 files changed, 105 insertions, 3 deletions
diff --git a/xmake/core/project/policy.lua b/xmake/core/project/policy.lua index 6a3393cce..4a0150e2a 100644 --- a/xmake/core/project/policy.lua +++ b/xmake/core/project/policy.lua @@ -58,6 +58,8 @@ function policy.policies() ["build.warning"] = {description = "Enable build warning output.", default = true, type = "boolean"}, -- Enable LTO linker-time optimization for c/c++ building. ["build.optimization.lto"] = {description = "Enable LTO linker-time optimization for c/c++ building.", type = "boolean"}, + -- Enable C++ Dynamic Debugging for MSVC (requires MSVC 19.44+, x64 only, incompatible with LTCG/PGO/OPT-ICF). + ["build.dynamic_debugging"] = {description = "Enable C++ Dynamic Debugging for MSVC (requires MSVC 19.44+, x64 only, incompatible with LTCG/PGO/OPT-ICF).", type = "boolean"}, -- Enable address sanitizer for c/c++ building. ["build.sanitizer.address"] = {description = "Enable address sanitizer for c/c++ building.", type = "boolean"}, -- Enable thread sanitizer for c/c++ building. diff --git a/xmake/rules/c++/config/dynamic_debugging.lua b/xmake/rules/c++/config/dynamic_debugging.lua new file mode 100644 index 000000000..93f8fe01d --- /dev/null +++ b/xmake/rules/c++/config/dynamic_debugging.lua @@ -0,0 +1,96 @@ +--!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, Xmake Open Source Community. +-- +-- @author ruki +-- @file dynamic_debugging.lua +-- +-- @see https://devblogs.microsoft.com/cppblog/cpp-dynamic-debugging-full-debuggability-for-optimized-builds/ + +-- imports +import("core.project.project") +import("core.tool.toolchain") +import("core.base.semver") + +-- add dynamic debugging support +function main(target, sourcekind) + -- check if dynamic debugging is enabled + local enabled = target:policy("build.dynamic_debugging") + if enabled == nil then + enabled = project.policy("build.dynamic_debugging") + end + if not enabled then + return + end + + -- only support Windows platform + if not target:is_plat("windows") then + return + end + + -- only support x64 architecture + if not target:is_arch("x64", "x86_64") then + wprint("C++ Dynamic Debugging only supports x64 architecture, current arch: %s", target:arch()) + return + end + + -- check MSVC toolchain (only support cl, not clang-cl or clang) + if not target:has_tool(sourcekind, "cl") then + return + end + + -- check MSVC version (requires 19.44+) + local msvc = target:toolchain("msvc") + if not msvc or not msvc:check() then + wprint("MSVC toolchain not found for C++ Dynamic Debugging") + return + end + + local vcvars = msvc:config("vcvars") + if not vcvars or not vcvars.VCToolsVersion then + wprint("MSVC VCToolsVersion not found for C++ Dynamic Debugging") + return + end + + local version = vcvars.VCToolsVersion + if not version or not semver.compare(version, "19.44") >= 0 then + wprint("C++ Dynamic Debugging requires MSVC 19.44+, found: %s", version or "unknown") + return + end + + -- check incompatible optimizations + local has_lto = target:policy("build.optimization.lto") or project.policy("build.optimization.lto") + if has_lto then + wprint("C++ Dynamic Debugging is incompatible with LTO, disabling LTO") + target:set("policy", "build.optimization.lto", false) + end + + -- check for /GL flag (added by optimize="smallest") + local optimize = target:get("optimize") + if optimize == "smallest" then + wprint("C++ Dynamic Debugging is incompatible with optimize=\"smallest\" (which adds /GL flag), consider using optimize=\"fastest\" or optimize=\"faster\" instead") + end + + -- get flag name for sourcekind (only support cc and cxx) + local cflag = sourcekind == "cxx" and "cxxflags" or "cflags" + + -- add /dynamicdeopt compiler flag + target:add(cflag, "/dynamicdeopt", {force = true}) + + -- add /DYNAMICDEOPT linker flag + target:add("ldflags", "/DYNAMICDEOPT", {force = true}) + target:add("shflags", "/DYNAMICDEOPT", {force = true}) +end + diff --git a/xmake/rules/c++/config/main.lua b/xmake/rules/c++/config/main.lua index 78d1362af..dce7cff6b 100644 --- a/xmake/rules/c++/config/main.lua +++ b/xmake/rules/c++/config/main.lua @@ -19,9 +19,10 @@ -- -- imports -import("basic", {alias = "config_basic"}) -import("optimization", {alias = "config_optimization"}) -import("sanitizer", {alias = "config_sanitizer"}) +import("rules.c++.config.basic", {rootdir = os.programdir(), alias = "config_basic"}) +import("rules.c++.config.optimization", {rootdir = os.programdir(), alias = "config_optimization"}) +import("rules.c++.config.sanitizer", {rootdir = os.programdir(), alias = "config_sanitizer"}) +import("rules.c++.config.dynamic_debugging", {rootdir = os.programdir(), alias = "config_dynamic_debugging"}) -- main entry function main(target, sourcekind) @@ -29,6 +30,9 @@ function main(target, sourcekind) -- config basic configs config_basic(target, sourcekind) + -- config dynamic debugging configs (must be before optimization to disable incompatible flags) + config_dynamic_debugging(target, sourcekind) + -- config optimization configs config_optimization(target, sourcekind) |
