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

refactor: [toolchain] migrate toolchain detection from shell to python #1007

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions src/common/toolchain/toolchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,36 @@
#include <QJsonDocument>

namespace toolchains {
const QString K_SCIRPTNAME{"toolchain.sh"};
const QString K_TOOLCHAINFILE{"toolchains.support"};
const QString K_VERSION{"version"};
const QString K_HOSTK_TOOLCHAINFILEK_TOOLCHAINFILEK_TOOLCHAINFILE_OS{"host_os"};
const QString K_HOST_ARCH{"host_arch"};
const QString K_HOST_KERNEL{"host_kernel"};
const QString K_TOOLCHAINS{"toolchains"};
const QString K_TOOLCHAIN_NAME{"toolchain_name"};
const QString K_TOOLCHAIN_ABI{"toolchain_abi"};
const QString K_TOOLCHAIN_PREFIX{"toolchain_prefix"};
const QString K_TOOLCHAIN_PATH{"toolchain_path"};
const QString K_TOOLCHAIN_C_COMPILER{"toolchain_c_compiler"};
const QString K_TOOLCHAIN_CXX_COMPILER{"toolchain_cxx_compiler"};
const QString K_TOOLCHAIN_DEBUGGER{"toolchain_debugger"};
} // namespace toolchain
const QString K_SCIRPTNAME { "toolchain/main.py" };
const QString K_TOOLCHAINFILE { "toolchains.json" };
const QString K_VERSION { "version" };
const QString K_HOSTK_TOOLCHAINFILEK_TOOLCHAINFILEK_TOOLCHAINFILE_OS { "host_os" };
const QString K_HOST_ARCH { "host_arch" };
const QString K_HOST_KERNEL { "host_kernel" };
const QString K_TOOLCHAINS { "toolchains" };
const QString K_TOOLCHAIN_NAME { "toolchain_name" };
const QString K_TOOLCHAIN_ABI { "toolchain_abi" };
const QString K_TOOLCHAIN_PREFIX { "toolchain_prefix" };
const QString K_TOOLCHAIN_PATH { "toolchain_path" };
const QString K_TOOLCHAIN_C_COMPILER { "toolchain_c_compiler" };
const QString K_TOOLCHAIN_CXX_COMPILER { "toolchain_cxx_compiler" };
const QString K_TOOLCHAIN_DEBUGGER { "toolchain_debugger" };
} // namespace toolchain

bool toolchains::generatGlobalFile()
{
auto script = CustomPaths::global(CustomPaths::Scripts) + QDir::separator() + toolchains::K_SCIRPTNAME;
if (!QFileInfo(script).isFile())
return false;

QString result = CustomPaths::user(CustomPaths::Configures) + QDir::separator() + toolchains::K_TOOLCHAINFILE;
ProcessUtil::execute(script, {result}, [=](const QByteArray &out){
QString outputFile = CustomPaths::user(CustomPaths::Configures) + QDir::separator() + toolchains::K_TOOLCHAINFILE;
QStringList args { script, "-o", outputFile };
ProcessUtil::execute("python3", args, [=](const QByteArray &out) {
qInfo() << out;
});

if (QFile(result).exists())
if (QFile(outputFile).exists())
return true;

return false;
}

3 changes: 3 additions & 0 deletions src/scripts/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ install(DIRECTORY rag/

install(DIRECTORY compiledb/
DESTINATION "${LIBRARY_INSTALL_PREFIX}/scripts/compiledb")

install(DIRECTORY compiledb/
DESTINATION "${LIBRARY_INSTALL_PREFIX}/scripts/toolchain")
281 changes: 0 additions & 281 deletions src/scripts/toolchain.sh

This file was deleted.

51 changes: 51 additions & 0 deletions src/scripts/toolchain/base_toolchain.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd.
#
# SPDX-License-Identifier: GPL-3.0-or-later

from abc import ABC, abstractmethod
from typing import List, Dict
import os
import glob
from dataclasses import dataclass

@dataclass
class ToolchainInfo:
name: str
path: str

class BaseToolchain(ABC):
def __init__(self):
self._executables: List[str] = []

def find_tools(self, tool_name: str) -> List[str]:
tools = []
paths = os.environ.get("PATH", "").split(os.pathsep)

priority_paths = ['/usr/bin', '/bin']
other_paths = [p for p in paths if p not in priority_paths]
scan_paths = priority_paths + other_paths

for path in scan_paths:
if not os.path.exists(path):
continue
matches = glob.glob(os.path.join(path, tool_name))
for match in matches:
if os.access(match, os.X_OK | os.F_OK):
tools.append(match)
return self.auto_detect_toolchains(tools)

def auto_detect_toolchains(self, compiler_paths: List[str]) -> List[str]:
result = []
for compiler_path in compiler_paths:
already_exists = False
for existing_tc in result:
already_exists = os.lstat(existing_tc).st_ino == os.lstat(compiler_path).st_ino
if already_exists:
break
if not already_exists:
result.append(compiler_path)
return result

@abstractmethod
def scan(self) -> List[ToolchainInfo]:
pass
Loading
Loading