aboutsummaryrefslogtreecommitdiffstats
path: root/qt-cpp/src/commands/mingw-gdb.ts
blob: 3c6718e127c6a1f2096aa6a872142f1e22e26098 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only

import * as vscode from 'vscode';
import * as fs from 'fs';
import * as path from 'path';

import { IsWindows, telemetry } from 'qt-lib';
import { getQtInsRoot, getSelectedKit } from '@cmd/register-qt-path';
import { EXTENSION_ID } from '@/constants';

async function findMinGWgdbPath(): Promise<string | undefined> {
  telemetry.sendAction('minGWgdb');
  if (!IsWindows) {
    throw new Error('MinGW gdb is only available on Windows');
  }
  const kit = await getSelectedKit();
  if (!kit) {
    return undefined;
  }
  const insRoot = getQtInsRoot(kit);
  if (!insRoot) {
    const message = `Could not find VSCODE_QT_INSTALLATION in the selected kit: ${kit.name}`;
    void vscode.window.showErrorMessage(message);
    return undefined;
  }
  const selectedQtPath = insRoot;
  const toolsDir = locateToolsDir(selectedQtPath);
  const mingwDir = fs
    .readdirSync(toolsDir)
    .find((tool) => tool.startsWith('mingw'));
  if (!mingwDir) {
    throw new Error('No MinGW installation found');
  }
  const gdbPath = path.join(toolsDir, mingwDir, 'bin', 'gdb.exe');
  if (!fs.existsSync(gdbPath)) {
    throw new Error('No gdb found in MinGW installation');
  }
  return gdbPath;
}

function locateToolsDir(selectedQtPath: string): string {
  let toolsDir = '';
  for (let i = 0; i < 4; i++) {
    toolsDir = path.join(selectedQtPath, '../'.repeat(i), 'Tools');
    if (fs.existsSync(toolsDir)) {
      return toolsDir;
    }
  }
  throw new Error(
    'No Tools directory found in the up to 4 levels up from the selected Qt path'
  );
}

export function registerMinGWgdbCommand() {
  return vscode.commands.registerCommand(
    `${EXTENSION_ID}.minGWgdb`,
    findMinGWgdbPath
  );
}