Skip to content

nfc(xcode) refactor xcode toolchain, improve it and use it in llvm toolchain when targeting a apple device#6977

Merged
waruqi merged 2 commits intoxmake-io:devfrom
Arthapz:improve-xcode-toolchain
Nov 3, 2025
Merged

nfc(xcode) refactor xcode toolchain, improve it and use it in llvm toolchain when targeting a apple device#6977
waruqi merged 2 commits intoxmake-io:devfrom
Arthapz:improve-xcode-toolchain

Conversation

@Arthapz
Copy link
Member

@Arthapz Arthapz commented Oct 29, 2025

this PR cleanup the xcode toolchain, reduce repetition and make it compatible with llvm toolchain on apple platforms

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @Arthapz, 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 undertakes a significant refactoring and enhancement of the Xcode toolchain integration within Xmake, specifically targeting Apple development environments. The core change involves centralizing the complex logic for configuring compiler and linker flags, along with SDK path detection, into a new, reusable module. This strategic consolidation aims to boost maintainability and robustness across all supported Apple platforms, encompassing iOS, tvOS, watchOS, xros, and macOS (with specialized support for Catalyst), while simultaneously standardizing the approach to Swift compiler interactions.

Highlights

  • Xcode Toolchain Refactoring: Common Xcode toolchain configuration logic for Apple platforms has been centralized into a new shared load_platform.lua module, reducing redundancy and improving maintainability across various platform-specific load files.
  • Expanded Apple Platform Support: The LLVM toolchain now includes improved and more robust detection and configuration of Xcode SDKs for a wider range of Apple platforms, specifically iphoneos, tvos, watchos, and xros, in addition to macosx.
  • Standardized Swift Compiler Usage: The Swift interop rule has been updated to use a more consistent and standardized method for loading and invoking the Swift compiler, ensuring better integration and reliability within the build process.
  • Refined macOS and Catalyst Configuration: The macOS toolchain configuration has been refined with more explicit and consistent flag application, including dedicated handling for Catalyst builds to correctly set include, library, and framework paths.
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.

@Arthapz Arthapz marked this pull request as draft October 29, 2025 17:31
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 is a significant and valuable refactoring of the Xcode toolchain logic. By centralizing platform-specific flag settings for Apple devices into a new load_platform.lua module, it greatly improves maintainability and reduces code duplication. The updated logic in the llvm toolchain to use this new module is also a good improvement. However, I've found a critical issue in the new load_platform.lua module that will cause Swift builds to fail. The way the -sdk flag is passed to the Swift compiler is incorrect. My review includes a code suggestion to fix this.

@Arthapz Arthapz force-pushed the improve-xcode-toolchain branch from 7493d33 to 2a86285 Compare October 29, 2025 17:36
@Arthapz Arthapz marked this pull request as ready for review October 30, 2025 14:33
@Arthapz Arthapz force-pushed the improve-xcode-toolchain branch from 2a86285 to f52a15b Compare October 30, 2025 15:09
@Arthapz Arthapz marked this pull request as draft October 30, 2025 15:09
@Arthapz Arthapz marked this pull request as ready for review October 30, 2025 16:00
local mode = target:data("swift.interop")
local sc = target:tool("sc")
-- local sc = target:compiler("sc")
local sc = import("core.tool.compiler").load("sc")
Copy link
Member

Choose a reason for hiding this comment

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

why do not use target:compiler("sc")?

Copy link
Member Author

Choose a reason for hiding this comment

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

same reason as #6967 (comment)
is it a bug ? should we fix it ?

Copy link
Member

Choose a reason for hiding this comment

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

it works for me.

target("test")
    set_kind("binary")
    add_files("src/*.swift")
    on_config(function (target)
        local compinst = import("core.tool.compiler").load("sc")
        local compinst2 = target:compiler("sc")
        print(compinst:program())
        print(compinst2:program())
    end)
ruki:test ruki$ xmake f -c
checking for platform ... macosx
checking for architecture ... x86_64
checking for Xcode directory ... /Applications/Xcode.app
checking for SDK version of Xcode for macosx (x86_64) ... 15.2
checking for Minimal target version of Xcode for macosx (x86_64) ... 15.2
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend

you can debug here. target:compiler("sc") will get tool from target:tool().

import("core.tool.compiler").load("sc") will get tool from the global platform.

if target and target.tool then
local _, _, toolchain_info = target:tool(sourcekind)
if toolchain_info then
plat = toolchain_info.plat
arch = toolchain_info.arch
end

Copy link
Member

Choose a reason for hiding this comment

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

we should get tool from target instance.

Copy link
Member Author

@Arthapz Arthapz Nov 2, 2025

Choose a reason for hiding this comment

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

the problem happen when there are multiple soucekind in the same target

-- get the target compiler
function _instance:compiler(sourcekind)
    local compilerinst = self:_memcache():get("compiler")
    if not compilerinst then
        if not sourcekind then
            os.raise("please pass sourcekind to the first argument of target:compiler(), e.g. cc, cxx, as")
        end
        local instance, errors = compiler.load(sourcekind, self)
        if not instance then
            os.raise(errors)
        end
        compilerinst = instance
        self:_memcache():set("compiler", compilerinst)
    end
    return compilerinst
end

the target only cache the first call to compiler(sourcekind) for all sourcekind, so if i do target:compiler("cxx") it will cache clang and when i call target:compiler("sc") it will return the cached clang

changing it to

-- get the target compiler
function _instance:compiler(sourcekind)
    if not sourcekind then
        os.raise("please pass sourcekind to the first argument of target:compiler(), e.g. cc, cxx, as")
    end
    local compilerinst = self:_memcache():get("compiler_" .. sourcekind)
    if not compilerinst then
        local instance, errors = compiler.load(sourcekind, self)
        if not instance then
            os.raise(errors)
        end
        compilerinst = instance
        self:_memcache():set("compiler_" .. sourcekind, compilerinst)
    end
    return compilerinst
end

make it work as it's now caching for each sourcekind, but i don't know what impact it have on the rest of XMake
see my last commit

@Arthapz Arthapz force-pushed the improve-xcode-toolchain branch 4 times, most recently from 8dd0739 to 1b2be30 Compare November 1, 2025 19:26
@Arthapz Arthapz force-pushed the improve-xcode-toolchain branch from 1b2be30 to 6bb310b Compare November 1, 2025 20:23
@waruqi waruqi merged commit c5d08dc into xmake-io:dev Nov 3, 2025
22 checks passed
@waruqi waruqi added this to the v3.0.5 milestone Nov 3, 2025
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