Skip to content

Improve clang to support for msvc envs#6904

Merged
waruqi merged 5 commits intodevfrom
msvc
Oct 8, 2025
Merged

Improve clang to support for msvc envs#6904
waruqi merged 5 commits intodevfrom
msvc

Conversation

@waruqi
Copy link
Member

@waruqi waruqi commented Oct 8, 2025

@waruqi waruqi added this to the v3.0.4 milestone Oct 8, 2025
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @waruqi, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request refactors Xmake's toolchain detection logic on Windows by centralizing Visual Studio environment setup into a new utility module. This change streamlines the integration of various compilers (Clang, Intel, MSVC) with the MSVC toolchain, reducing code duplication and improving maintainability.

Highlights

  • Intent: This pull request aims to improve Xmake's support for Clang and other compilers (like Intel compilers) when operating within a Microsoft Visual C++ (MSVC) environment. The primary goal is to centralize and streamline the detection and configuration of Visual Studio environment variables (like vcvars) across various toolchains that rely on MSVC.
  • Key Changes: The core change involves extracting common Visual Studio environment detection and configuration logic into a new utility module: xmake/modules/private/utils/toolchain.lua. This module introduces reusable functions (_check_vsenv, check_vstudio, check_vc_build_tools) that encapsulate the process of finding Visual Studio installations, setting up necessary environment variables, and configuring toolchain-specific settings.

Existing toolchain check files (clang-cl/check.lua, clang/check.lua, icc/check.lua, icx/check.lua, ifort/check.lua, ifx/check.lua, msvc/check.lua) have been refactored to leverage these new utility functions. This significantly reduces code duplication and centralizes the complex logic for integrating with MSVC environments. Each toolchain now imports the toolchain_utils module and passes its specific compiler detection logic as a callback to the generic Visual Studio environment checking functions.

  • Files Changed: - xmake/modules/private/utils/toolchain.lua: New functions for generic Visual Studio environment detection and configuration.
  • xmake/toolchains/clang-cl/check.lua: Refactored to use the new toolchain_utils for VS environment setup and simplified clang-cl detection.
  • xmake/toolchains/clang/check.lua: Refactored to use toolchain_utils for VS environment setup and introduced a dedicated _check_clang function.
  • xmake/toolchains/icc/check.lua, xmake/toolchains/icx/check.lua, xmake/toolchains/ifort/check.lua, xmake/toolchains/ifx/check.lua: Refactored to use toolchain_utils for VS environment setup, with a new _check_cl function for detecting the MSVC compiler.
  • xmake/toolchains/msvc/check.lua: Refactored to use toolchain_utils for its own VS environment setup, centralizing the cl.exe detection.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request does a great job of refactoring the Visual Studio environment detection logic into a shared utility, which significantly reduces code duplication across various toolchain check scripts. The overall approach is solid.

I've found one critical issue and a couple of medium-severity issues that should be addressed:

  1. Critical Bug in clang toolchain: There's a bug in xmake/toolchains/clang/check.lua where an incorrect function is passed as a callback, which will lead to a runtime error.
  2. Diagnostic Regression: In both clang and clang-cl toolchain checks, the logic to detect and print the compiler version has been removed. This is a regression in user-facing diagnostics, and I've suggested how to restore it.

After addressing these points, this will be a very good improvement to the codebase.

Comment on lines +50 to +52
local check = function (toolchain, vcvars)
return _check_clang(toolchain, vcvars, suffix)
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

This check function is defined but never used. Instead, _check_clang is passed directly as a callback to toolchain_utils functions on lines 55, 60, and 66.

This is a bug because _check_clang has the signature (toolchain, vcvars, suffix), but the callback is only invoked with (toolchain, vcvars). This will cause suffix to be nil inside _check_clang, leading to a runtime error when "clang" .. suffix is evaluated.

The check function correctly captures the suffix variable from the outer scope. Please use check instead of _check_clang for the callbacks.

For example, line 55 should be:

return toolchain_utils.check_vc_build_tools(toolchain, sdkdir, check)

Similar changes are needed for lines 60 and 66.

Comment on lines +33 to 36
local result = find_tool("clang-cl", {force = true, paths = paths, envs = vcvars})
if result then
cprint("checking for LLVM Clang C/C++ Compiler (%s) ... ${color.success}", toolchain:arch())
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The previous implementation detected and printed the version of clang-cl. This change removes that functionality, which is a regression in diagnostic output for users.

I suggest adding version = true to the find_tool options and updating the print statement to include the version if available. This provides more useful feedback to the user.

    local result = find_tool("clang-cl", {version = true, force = true, paths = paths, envs = vcvars})
    if result then
        if result.version then
            cprint("checking for LLVM Clang C/C++ Compiler (%s) version ... ${color.success}%s", toolchain:arch(), result.version)
        else
            cprint("checking for LLVM Clang C/C++ Compiler (%s) ... ${color.success}", toolchain:arch())
        end
    end

Comment on lines +32 to 35
local result = find_tool("clang" .. suffix, {force = true, paths = paths, envs = vcvars})
if result then
cprint("checking for LLVM Clang C/C++ Compiler (%s) ... ${color.success}", toolchain:arch())
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Similar to my other comment, the version detection and printing for clang has been removed. This is a regression in diagnostic information for the user.

Please consider adding version = true to find_tool and printing the version.

    local result = find_tool("clang" .. suffix, {version = true, force = true, paths = paths, envs = vcvars})
    if result then
        if result.version then
            cprint("checking for LLVM Clang C/C++ Compiler (%s) version ... ${color.success}%s", toolchain:arch(), result.version)
        else
            cprint("checking for LLVM Clang C/C++ Compiler (%s) ... ${color.success}", toolchain:arch())
        end
    end

@waruqi waruqi merged commit 79879d1 into dev Oct 8, 2025
41 of 50 checks passed
@waruqi waruqi deleted the msvc branch October 9, 2025 01:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants