Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent behavior between target:has_tool and package:has_tool with clang-cl #5057

Closed
Hyiker opened this issue May 5, 2024 · 2 comments
Labels

Comments

@Hyiker
Copy link
Contributor

Hyiker commented May 5, 2024

Xmake Version

2.9.1

Operating System Version and Architecture

Windows 10 22H2

Describe Bug

Inconsistent behavior between target:has_tool("cxx", "clang_cl") and package:has_tool("cxx", "clang_cl"). I confirm there is a valid clang-cl compiler in the environment, but the former returned true while the later gave false.

image

Moreover, this leads to the absense of subsystem:console linker flag in catch2 in xmake-repo.

Expected Behavior

The bahavior should be consistent, which both return value should be true.

Project Configuration

I used a modified version of catch2.lua in xmake-repo, and a minimal xmake.lua to reproduce it. I use --cc=clang-cl --cxx=clang-cl to enforce compiler to be clang-cl:

xmake.lua:

add_rules("mode.debug", "mode.release")
add_requires("catch2mod")

includes("catch2mod.lua")

target("Test")
    set_kind("binary")
    add_files("main.cpp")
    add_packages("catch2mod")
    set_languages("cxx20")
    on_config(function (target)
      if target:has_tool("cxx", "clang_cl") then
          print("target:has_tool(clang_cl)")
      end
    end)

catch2mod.lua:

package("catch2mod")
    set_homepage("https://github.com/catchorg/Catch2")
    set_description("Catch2 is a multi-paradigm test framework for C++. which also supports Objective-C (and maybe C). ")
    set_license("BSL-1.0")

    add_urls("https://github.com/catchorg/Catch2/archive/refs/tags/$(version).zip",
             "https://github.com/catchorg/Catch2.git")
    add_versions("v3.5.4", "190a236fe0772ac4f5eebfdebfc18f92eeecfd270c55a1e5095ae4f10be2343f")
    add_versions("v3.5.3", "2de1868288b26a19c2aebfc3fe53a748ec3ec5fc32cc742dfccaf6c685a0dc07")
    add_versions("v3.5.2", "85fcc78d0c3387b15ad82f22a94017b29e4fe7c1cf0a05c3dd465b2746eef73f")
    add_versions("v3.5.1", "b422fcd526a95e6057839f93a18099261bdc8c595f932ed4b1a978b358b3f1ed")
    add_versions("v3.5.0", "82079168b2304cfd0dfc70338f0c4b3caa4f3ef76b2643110d3f74a632252fc6")
    add_versions("v3.4.0", "cd175f5b7e62c29558d4c17d2b94325ee0ab6d0bf1a4b3d61bc8dbcc688ea3c2")
    add_versions("v3.3.2", "802a1d7f98f8e38a7913b596c5e3356ea76c544acb7c695bfd394544556359f3")
    add_versions("v3.2.1", "bfee681eaa920c6ddbe05c1eef1912440d38c5f9a7924f68a6aa219ed1a39c0f")
    add_versions("v3.2.0", "b9f3887915f32eb732140af6a153065a11fabcd3f3e9355f3abff3d3618fd0fe")
    add_versions("v3.1.1", "eec6c327cd9187c63bbaaa8486f715e31544000bf8876c0543e1181a2a52a5de")
    add_versions("v3.1.0", "7219c2ca75a6b2a157b1b162e4ad819fb32585995cac32542a4f72d950dd96f7")
    add_versions("v2.13.10", "121e7488912c2ce887bfe4699ebfb983d0f2e0d68bcd60434cdfd6bb0cf78b43")
    add_versions("v2.13.9", "860e3917f07d7ee75654f86900d50a03acf0047f6fe5ba31d437e1e9cda5b456")
    add_versions("v2.13.8", "de0fd1f4c51a1021ffcb33a4d42028545bf1a0665a4ab59ddb839a0cc93f03a5")
    add_versions("v2.13.7", "3f3ccd90ad3a8fbb1beeb15e6db440ccdcbebe378dfd125d07a1f9a587a927e9")
    add_versions("v2.13.6", "39d50f5d1819cdf2908066664d57c2cde4a4000c364ad3376ea099735c896ff4")
    add_versions("v2.13.5", "728679b056dc1248cc79b3a1999ff7453f76422c68417563fc47a0ac2aaeeaef")
    add_versions("v2.9.2", "dc486300de22b0d36ddba1705abb07b9e5780639d824ba172ddf7062b2a1bf8f")


    add_configs("cxx17", {description = "Compiles Catch as a C++17 library (requires a C++17 compiler).", default = true, type = "boolean"})

    if is_plat("mingw") and is_subhost("msys") then
        add_extsources("pacman::catch")
    elseif is_plat("linux") then
        add_extsources("pacman::catch2", "apt::catch2")
    end

    on_load(function (package)
        if package:version():ge("3.0") then
            package:add("deps", "cmake")
            package:add("components", "main", "lib")
            if package:is_plat("macosx") then
                package:add("extsources", "brew::catch2/catch2-with-main")
            end
        else
            package:set("kind", "library", {headeronly = true})
            if package:is_plat("macosx") then
                package:add("extsources", "brew::catch2")
            end
        end
    end)

    on_component("main", function (package, component)
        local link = "Catch2Main"
        if package:is_debug() then
            link = link.."d"
        end
        component:add("links", link)
        if package:is_plat("windows") then
            print("package:is_plat(\"windows\")")
            if package:has_tool("cxx", "clang_cl") then
                print("package:has_tool(\"clang_cl\")")
                component:add("ldflags", "-subsystem:console")
            elseif package:has_tool("cxx", "clang", "clangxx") then
                component:add("ldflags", "-Wl,/subsystem:console")
            end
        end
    end)

    on_component("lib", function (package, component)
        local link = "Catch2"
        if package:is_debug() then
            link = link.."d"
        end
        component:add("links", link)
    end)

    on_install(function (package)
        if package:version():ge("3.0") then
            local configs = {"-DCATCH_INSTALL_DOCS=OFF", "-DCATCH_BUILD_TESTING=OFF", "-DCATCH_BUILD_EXAMPLES=OFF"}
            table.insert(configs, "-DCMAKE_BUILD_TYPE=" .. (package:debug() and "Debug" or "Release"))
            table.insert(configs, "-DBUILD_SHARED_LIBS=" .. (package:config("shared") and "ON" or "OFF"))
            if package:config("cxx17") then
                table.insert(configs, "-DCMAKE_CXX_STANDARD=17")
            end
            import("package.tools.cmake").install(package, configs)
        else
            os.cp("single_include/catch2", package:installdir("include"))
        end
    end)

    on_test(function (package)
        if package:version():ge("3.0") then
            assert(package:check_cxxsnippets({test = [[
                int factorial(int number) { return number <= 1 ? number : factorial(number - 1) * number; }

                TEST_CASE("Factorials are computed", "[factorial]") {
                    REQUIRE(factorial(1) == 1);
                    REQUIRE(factorial(2) == 2);
                    REQUIRE(factorial(3) == 6);
                    REQUIRE(factorial(10) == 3628800);
                }
            ]]}, {configs = {languages = "c++14"}, includes = "catch2/catch_test_macros.hpp"}))
        else
            assert(package:check_cxxsnippets({test = [[
                int factorial(int number) { return number <= 1 ? number : factorial(number - 1) * number; }

                TEST_CASE("testing the factorial function") {
                    CHECK(factorial(1) == 1);
                    CHECK(factorial(2) == 2);
                    CHECK(factorial(3) == 6);
                    CHECK(factorial(10) == 3628800);
                }
            ]]}, {configs = {languages = "c++11"}, includes = "catch2/catch.hpp", defines = "CATCH_CONFIG_MAIN"}))
        end
    end)

Additional Information and Error Logs

 xmake f -p windows -a x64 -m release --cc=clang-cl --cxx=clang-cl -c -v

gives:

checking for cl.exe ... C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\bin\HostX64\x64\cl.exe
checking for Microsoft Visual Studio (x64) version ... 2022
checking for zig ... no
checking for zig ... no
checking for nim ... no
checking for nim ... no
checking for unzip ... ok
checking for git ... ok
checking for gzip ... ok
checking for tar ... ok   
package:is_plat("windows")
git rev-parse HEAD
checking for cmake ... no
checking for cmake ... no
checking for cmake ... no
checking for cmake ... C:\Program Files\CMake\bin\cmake
checking for xmake::catch2mod ... catch2mod v3.5.4
checking for link.exe ... C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\bin\HostX64\x64\link.exe
checking for the linker (ld) ... link.exe
target:has_tool(clang_cl)
configure
{
    mode = release
    clean = true
    cc = clang-cl
    plat = windows
    buildir = build
    ccache = true
    theme = default
    kind = static
    host = windows
    vs = 2022
    proxy_pac = pac.lua
    ndk_stdcxx = true
    arch = x64
    network = public
    cxx = clang-cl
}
@Hyiker Hyiker added the bug label May 5, 2024
@waruqi
Copy link
Member

waruqi commented May 5, 2024

please use xmake f --toolchain=clang-cl or set_toolchains("clang-cl"), or add_requires("xxx", {configs = {toolchains = "clang-cl"}}) instead of xmake f --cc=clang-cl --cxx=clang-cl

only toolchains can be passed into package.

@Hyiker
Copy link
Contributor Author

Hyiker commented May 6, 2024

Ok, I'm specifying compiler directly to workaround another problem, in which ninja + clang-cl won't enable c++ exceptions by default on Windows. Perhaps I'll open another issue for this.

@Hyiker Hyiker closed this as completed May 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants