All files / transpiler/logic/preprocessor ToolchainDetector.ts

100% Statements 89/89
94.64% Branches 53/56
100% Functions 10/10
100% Lines 78/78

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 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269                                              224x 224x 1x 1x       223x 223x     222x 222x     6x 6x   2x             2x   2x 2x   2x 2x   2x 2x   2x                     5x 3x   2x   5x       4x   4x   5x                           225x 225x   2x 225x   225x                             224x 224x   217x 224x   224x                           8x 8x   5x 8x   8x                           683x 683x         683x 447x           236x             228x 228x           228x 228x   1x               219x   219x         219x 219x   219x 2171x 217x 217x   1954x 217x   1737x 868x       218x   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 { basename, 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 {
    // Explicit override: a project can name the compiler that owns its target
    // headers (e.g. a cross-compiler such as xtensa-esp32s3-elf-gcc) via
    // CNEXT_CROSS_COMPILER. Host gcc/clang lack a cross target's system headers
    // and predefined macros, so their preprocessing of target headers fails.
    const override = process.env.CNEXT_CROSS_COMPILER;
    if (override) {
      const overridden = ToolchainDetector.fromPath(override);
      Eif (overridden) return overridden;
    }
 
    // 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;
  }
 
  /**
   * Build a toolchain from an explicit compiler path or executable name (e.g. a
   * target cross-compiler such as xtensa-esp32s3-elf-gcc). Used by the
   * CNEXT_CROSS_COMPILER override so a project can preprocess its target headers with the compiler
   * that owns them.
   */
  static fromPath(compiler: string): IToolchain | null {
    let cc: string | null;
    if (compiler.includes("/")) {
      cc = existsSync(compiler) ? compiler : null;
    } else {
      cc = this.findExecutable(compiler);
    }
    if (!cc) return null;
 
    // Best-effort C++ driver alongside the C driver (only cpp is used for
    // preprocessing; cxx is derived for completeness).
    const cxxCandidate = cc.replace(/gcc(\.exe)?$/, "g++$1");
    const cxx =
      cxxCandidate !== cc && existsSync(cxxCandidate) ? cxxCandidate : cc;
 
    return {
      name: basename(cc),
      cc,
      cxx,
      cpp: cc,
      version: this.getVersion(cc),
      isCrossCompiler: true,
    };
  }
 
  /**
   * 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;