diff --git a/xmake/core/sandbox/modules/import/lib/detect/find_program.lua b/xmake/core/sandbox/modules/import/lib/detect/find_program.lua index baa180b4b3c..3923916d902 100644 --- a/xmake/core/sandbox/modules/import/lib/detect/find_program.lua +++ b/xmake/core/sandbox/modules/import/lib/detect/find_program.lua @@ -243,6 +243,28 @@ function sandbox_lib_detect_find_program._find(name, paths, opt) if program_path_real then return program_path_real end + + -- attempt to find it use `where.exe program.exe` command + -- + -- and we need to add `.exe` suffix to avoid find some incorrect programs. e.g. pkg-config.bat + if os.host() == "windows" then + local program_name = name:lower() + if not program_name:endswith(".exe") then + program_name = program_name .. ".exe" + end + local ok, wherepaths = os.iorunv("where.exe", {program_name}) + if ok and wherepaths then + for _, program_path in ipairs(wherepaths:split("\n")) do + program_path = program_path:trim() + if #program_path > 0 then + local program_path_real = sandbox_lib_detect_find_program._check(program_path, opt) + if program_path_real then + return program_path_real + end + end + end + end + end end -- find program diff --git a/xmake/modules/detect/tools/find_where.lua b/xmake/modules/detect/tools/find_where.lua new file mode 100644 index 00000000000..7cfdc8caa9b --- /dev/null +++ b/xmake/modules/detect/tools/find_where.lua @@ -0,0 +1,38 @@ +--!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, TBOOX Open Source Group. +-- +-- @author ruki +-- @file find_where.lua +-- + +-- imports +import("lib.detect.find_program") + +-- find where +-- +-- @code +-- local where = find_where() +-- @endcode +-- +function main(opt) + opt = opt or {} + opt.check = "/?" + local program + if is_host("windows") then + program = find_program(opt.program or "where.exe", opt) + end + return program +end diff --git a/xmake/toolchains/llvm/check.lua b/xmake/toolchains/llvm/check.lua index cee19baac23..6fe57b2aaba 100644 --- a/xmake/toolchains/llvm/check.lua +++ b/xmake/toolchains/llvm/check.lua @@ -21,6 +21,7 @@ -- imports import("core.project.config") import("lib.detect.find_path") +import("lib.detect.find_tool") import("detect.sdks.find_xcode") import("detect.sdks.find_cross_toolchain") @@ -64,7 +65,6 @@ function main(toolchain) elseif is_host("linux") and os.isfile("/usr/bin/llvm-ar") then sdkdir = "/usr" elseif is_host("macosx") then - local bindir if os.arch() == "arm64" then bindir = find_path("llvm-ar", "/opt/homebrew/opt/llvm/bin") else @@ -74,19 +74,9 @@ function main(toolchain) sdkdir = path.directory(bindir) end elseif is_host("windows") then - bindir = try {function () return path.directory(os.iorunv("where", {"llvm-ar.exe"})) end} - if not bindir then - local pathenv = os.getenv("PATH") - if pathenv then - for _, v in ipairs(path.splitenv(pathenv)) do - if os.isfile(path.join(v, "llvm-ar.exe")) then - bindir = v - break - end - end - end - end - if bindir then + local llvm_ar = find_tool("llvm-ar", {force = true, envs = {PATH = os.getenvs("PATH")}}) + if llvm_ar and llvm_ar.program and os.isfile(llvm_ar.program) then + bindir = path.directory(llvm_ar.program) sdkdir = path.directory(bindir) end end