Skip to content

Commit

Permalink
refactor: [toolchain] migrate toolchain detection from shell to python
Browse files Browse the repository at this point in the history
Refactored the toolchain detection mechanism by replacing the shell script
with a Python implementation for better maintainability and extensibility.

- Replaced toolchain.sh with main.py as the detection script
- Updated file paths and script execution logic in toolchain.cpp

Log: refactor toolchain detection implementation
  • Loading branch information
Kakueeen committed Nov 28, 2024
1 parent 1e206c5 commit 2d8b9d4
Show file tree
Hide file tree
Showing 12 changed files with 459 additions and 307 deletions.
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

0 comments on commit 2d8b9d4

Please sign in to comment.