All files / transpiler/logic/preprocessor ToolchainDetector.ts

100% Statements 76/76
95.23% Branches 40/42
100% Functions 9/9
100% Lines 67/67

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228                                        172x 172x     171x 171x     6x 6x   2x             2x   2x 2x   2x 2x   2x 2x   2x             174x 174x   2x 174x   174x                             173x 173x   166x 173x   173x                           8x 8x   5x 8x   8x                           528x 528x         528x 343x           185x             173x 173x           173x 173x   1x               167x   167x         167x 167x   167x 1650x 165x 165x   1485x 165x   1320x 660x       166x   1x                 5x 5x   5x 1x     4x   4x           4x           4x 4x 4x 5x 4x               4x          
/**
 * Toolchain Detector
 * Finds available C/C++ compilers on the system
 */
 
import { execSync } from "node:child_process";
import { existsSync } from "node:fs";
import { join } from "node:path";
import IToolchain from "./types/IToolchain";
 
/**
 * Detects available C/C++ toolchains
 */
class ToolchainDetector {
  /**
   * Detect the best available toolchain
   * Priority: ARM cross-compiler > clang > gcc
   */
  static detect(): IToolchain | null {
    // Try ARM cross-compiler first (for embedded)
    const arm = this.detectArmToolchain();
    if (arm) return arm;
 
    // Try clang
    const clang = this.detectClang();
    if (clang) return clang;
 
    // Try gcc
    const gcc = this.detectGcc();
    if (gcc) return gcc;
 
    return null;
  }
 
  /**
   * Detect all available toolchains
   */
  static detectAll(): IToolchain[] {
    const toolchains: IToolchain[] = [];
 
    const arm = this.detectArmToolchain();
    if (arm) toolchains.push(arm);
 
    const clang = this.detectClang();
    if (clang) toolchains.push(clang);
 
    const gcc = this.detectGcc();
    if (gcc) toolchains.push(gcc);
 
    return toolchains;
  }
 
  /**
   * Detect ARM cross-compiler (arm-none-eabi-gcc)
   */
  private static detectArmToolchain(): IToolchain | null {
    const cc = this.findExecutable("arm-none-eabi-gcc");
    if (!cc) return null;
 
    const cxx = this.findExecutable("arm-none-eabi-g++") ?? cc;
    const version = this.getVersion(cc);
 
    return {
      name: "arm-none-eabi-gcc",
      cc,
      cxx,
      cpp: cc, // Use cc with -E flag
      version,
      isCrossCompiler: true,
      target: "arm-none-eabi",
    };
  }
 
  /**
   * Detect clang
   */
  private static detectClang(): IToolchain | null {
    const cc = this.findExecutable("clang");
    if (!cc) return null;
 
    const cxx = this.findExecutable("clang++") ?? cc;
    const version = this.getVersion(cc);
 
    return {
      name: "clang",
      cc,
      cxx,
      cpp: cc,
      version,
      isCrossCompiler: false,
    };
  }
 
  /**
   * Detect GCC
   */
  private static detectGcc(): IToolchain | null {
    const cc = this.findExecutable("gcc");
    if (!cc) return null;
 
    const cxx = this.findExecutable("g++") ?? cc;
    const version = this.getVersion(cc);
 
    return {
      name: "gcc",
      cc,
      cxx,
      cpp: cc,
      version,
      isCrossCompiler: false,
    };
  }
 
  /**
   * Find an executable in PATH
   */
  private static findExecutable(name: string): string | null {
    try {
      const result = execSync(`which ${name}`, {
        encoding: "utf-8",
        stdio: ["pipe", "pipe", "pipe"],
      }).trim();
 
      if (result && existsSync(result)) {
        return result;
      }
    } catch {
      // Not found
    }
 
    return null;
  }
 
  /**
   * Get compiler version string
   */
  private static getVersion(compiler: string): string | undefined {
    try {
      const result = execSync(`${compiler} --version`, {
        encoding: "utf-8",
        stdio: ["pipe", "pipe", "pipe"],
      });
 
      // Extract first line which usually has version info
      const firstLine = result.split("\n")[0];
      return firstLine?.trim();
    } catch {
      return undefined;
    }
  }
 
  /**
   * Get default include paths for a toolchain
   */
  static getDefaultIncludePaths(toolchain: IToolchain): string[] {
    try {
      // Ask the compiler for its default include paths
      const result = execSync(`echo | ${toolchain.cc} -E -Wp,-v - 2>&1`, {
        encoding: "utf-8",
        stdio: ["pipe", "pipe", "pipe"],
      });
 
      const paths: string[] = [];
      let inIncludeSection = false;
 
      for (const line of result.split("\n")) {
        if (line.includes("#include <...> search starts here:")) {
          inIncludeSection = true;
          continue;
        }
        if (line.includes("End of search list.")) {
          break;
        }
        if (inIncludeSection && line.trim()) {
          paths.push(line.trim());
        }
      }
 
      return paths;
    } catch {
      return [];
    }
  }
 
  /**
   * Parse PlatformIO environment for include paths
   * Looks for platformio.ini in project root
   */
  static getPlatformIOIncludePaths(projectRoot: string): string[] {
    const paths: string[] = [];
    const pioIniPath = join(projectRoot, "platformio.ini");
 
    if (!existsSync(pioIniPath)) {
      return paths;
    }
 
    try {
      // Use pio to get the include paths
      const result = execSync("pio project config --json-output", {
        cwd: projectRoot,
        encoding: "utf-8",
        stdio: ["pipe", "pipe", "pipe"],
      });
 
      const config = JSON.parse(result) as Record<
        string,
        { build_flags?: string[] }
      >;
 
      // Extract include directories from build flags
      for (const env of Object.values(config)) {
        const buildFlags = env.build_flags ?? [];
        for (const flag of buildFlags) {
          if (flag.startsWith("-I")) {
            paths.push(flag.slice(2));
          }
        }
      }
    } catch {
      // PlatformIO not available or project not configured
    }
 
    return paths;
  }
}
 
export default ToolchainDetector;