|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright 2025 Espressif Systems (Shanghai) PTE LTD. All rights reserved. |
| 3 | + * Use is subject to license terms. |
| 4 | + *******************************************************************************/ |
| 5 | + |
| 6 | +package com.espressif.idf.core.toolchain; |
| 7 | + |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.List; |
| 10 | +import java.util.Set; |
| 11 | + |
| 12 | +/** |
| 13 | + * Class to hold ESP-IDF target information including preview status |
| 14 | + * |
| 15 | + * @author Kondal Kolipaka <[email protected]> |
| 16 | + * |
| 17 | + */ |
| 18 | +public class IDFTargets |
| 19 | +{ |
| 20 | + private static final Set<String> XTENSA_CHIPS = Set.of("esp32", "esp32s2", "esp32s3"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
| 21 | + private static final String XTENSA = "xtensa"; //$NON-NLS-1$ |
| 22 | + private static final String RISCV32 = "riscv32"; //$NON-NLS-1$ |
| 23 | + private static final String XTENSA_TOOLCHAIN_ID = XTENSA + "-%s-elf"; //$NON-NLS-1$ |
| 24 | + private static final String RISCV32_TOOLCHAIN_ID = RISCV32 + "-esp-elf"; //$NON-NLS-1$ |
| 25 | + private static final String XTENSA_UNIFIED_DIR = XTENSA + "-esp-elf"; //$NON-NLS-1$ |
| 26 | + private static final String TOOLCHAIN_NAME = "toolchain-%s.cmake"; //$NON-NLS-1$ |
| 27 | + private List<IDFTarget> supportedTargets; |
| 28 | + private List<IDFTarget> previewTargets; |
| 29 | + |
| 30 | + public IDFTargets() |
| 31 | + { |
| 32 | + this.supportedTargets = new ArrayList<>(); |
| 33 | + this.previewTargets = new ArrayList<>(); |
| 34 | + } |
| 35 | + |
| 36 | + public void addSupportedTarget(String target) |
| 37 | + { |
| 38 | + supportedTargets.add(new IDFTarget(target, false)); |
| 39 | + } |
| 40 | + |
| 41 | + public void addPreviewTarget(String target) |
| 42 | + { |
| 43 | + previewTargets.add(new IDFTarget(target, true)); |
| 44 | + } |
| 45 | + |
| 46 | + public List<IDFTarget> getAllTargets() |
| 47 | + { |
| 48 | + List<IDFTarget> allTargets = new ArrayList<>(); |
| 49 | + allTargets.addAll(supportedTargets); |
| 50 | + allTargets.addAll(previewTargets); |
| 51 | + return allTargets; |
| 52 | + } |
| 53 | + |
| 54 | + public List<IDFTarget> getSupportedTargets() |
| 55 | + { |
| 56 | + return supportedTargets; |
| 57 | + } |
| 58 | + |
| 59 | + public List<IDFTarget> getPreviewTargets() |
| 60 | + { |
| 61 | + return previewTargets; |
| 62 | + } |
| 63 | + |
| 64 | + public boolean hasTarget(String targetName) |
| 65 | + { |
| 66 | + return getAllTargets().stream().anyMatch(target -> target.getName().equals(targetName)); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Get a specific target by name |
| 71 | + * |
| 72 | + * @param targetName Name of the target to find |
| 73 | + * @return IDFTarget if found, null otherwise |
| 74 | + */ |
| 75 | + public IDFTarget getTarget(String targetName) |
| 76 | + { |
| 77 | + return getAllTargets().stream().filter(target -> target.getName().equals(targetName)).findFirst().orElse(null); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Get all target names as strings |
| 82 | + * |
| 83 | + * @return List of target names |
| 84 | + */ |
| 85 | + public List<String> getAllTargetNames() |
| 86 | + { |
| 87 | + return getAllTargets().stream().map(IDFTarget::getName).collect(java.util.stream.Collectors.toList()); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Get supported target names as strings |
| 92 | + * |
| 93 | + * @return List of supported target names |
| 94 | + */ |
| 95 | + public List<String> getSupportedTargetNames() |
| 96 | + { |
| 97 | + return getSupportedTargets().stream().map(IDFTarget::getName).collect(java.util.stream.Collectors.toList()); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Get preview target names as strings |
| 102 | + * |
| 103 | + * @return List of preview target names |
| 104 | + */ |
| 105 | + public List<String> getPreviewTargetNames() |
| 106 | + { |
| 107 | + return getPreviewTargets().stream().map(IDFTarget::getName).collect(java.util.stream.Collectors.toList()); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Inner class representing a single IDF target |
| 112 | + */ |
| 113 | + public static class IDFTarget |
| 114 | + { |
| 115 | + private final String name; |
| 116 | + private final boolean isPreview; |
| 117 | + |
| 118 | + public IDFTarget(String name, boolean isPreview) |
| 119 | + { |
| 120 | + this.name = name; |
| 121 | + this.isPreview = isPreview; |
| 122 | + } |
| 123 | + |
| 124 | + public String getName() |
| 125 | + { |
| 126 | + return name; |
| 127 | + } |
| 128 | + |
| 129 | + public boolean isPreview() |
| 130 | + { |
| 131 | + return isPreview; |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Get the architecture for this target |
| 136 | + * |
| 137 | + * @return "xtensa" for esp32/esp32s2/esp32s3, "riscv32" for others |
| 138 | + */ |
| 139 | + public String getArchitecture() |
| 140 | + { |
| 141 | + return XTENSA_CHIPS.contains(name) ? XTENSA : RISCV32; |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Get the toolchain ID for this target |
| 146 | + * |
| 147 | + * @return toolchain ID string |
| 148 | + */ |
| 149 | + public String getToolchainId() |
| 150 | + { |
| 151 | + return XTENSA_CHIPS.contains(name) ? String.format(XTENSA_TOOLCHAIN_ID, name) : RISCV32_TOOLCHAIN_ID; |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Get the compiler pattern for this target |
| 156 | + * |
| 157 | + * @return regex pattern for compiler |
| 158 | + */ |
| 159 | + public String getCompilerPattern() |
| 160 | + { |
| 161 | + String executableName = getExecutableName(); |
| 162 | + |
| 163 | + // Support both old and new unified directory structures |
| 164 | + String targetSpecificDir = getTargetSpecificDirectoryName(); |
| 165 | + String unifiedDir = getUnifiedDirectoryName(); |
| 166 | + |
| 167 | + // Create pattern that matches either directory structure |
| 168 | + return "(?:" + targetSpecificDir + "|" + unifiedDir + ")[\\\\/]+bin[\\\\/]+" + executableName //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
| 169 | + + "-gcc(?:\\.exe)?$"; //$NON-NLS-1$ |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Get the debugger pattern for this target |
| 174 | + * |
| 175 | + * @return regex pattern for debugger |
| 176 | + */ |
| 177 | + public String getDebuggerPattern() |
| 178 | + { |
| 179 | + String executableName = getExecutableName(); |
| 180 | + return executableName + "-gdb(?:\\.exe)?$"; //$NON-NLS-1$ |
| 181 | + } |
| 182 | + |
| 183 | + /** |
| 184 | + * Get the executable name prefix for this target (different from directory structure in ESP-IDF v5.5+) |
| 185 | + * |
| 186 | + * @return executable name prefix |
| 187 | + */ |
| 188 | + private String getExecutableName() |
| 189 | + { |
| 190 | + return XTENSA_CHIPS.contains(name) ? String.format(XTENSA_TOOLCHAIN_ID, name) |
| 191 | + : RISCV32_TOOLCHAIN_ID; |
| 192 | + } |
| 193 | + |
| 194 | + private String getTargetSpecificDirectoryName() |
| 195 | + { |
| 196 | + return XTENSA_CHIPS.contains(name) ? String.format(XTENSA_TOOLCHAIN_ID, name) |
| 197 | + : RISCV32_TOOLCHAIN_ID; |
| 198 | + } |
| 199 | + |
| 200 | + private String getUnifiedDirectoryName() |
| 201 | + { |
| 202 | + return XTENSA_CHIPS.contains(name) ? XTENSA_UNIFIED_DIR : RISCV32_TOOLCHAIN_ID; |
| 203 | + } |
| 204 | + |
| 205 | + /** |
| 206 | + * Get the CMake toolchain file name for this target |
| 207 | + * |
| 208 | + * @return toolchain file name |
| 209 | + */ |
| 210 | + public String getToolchainFileName() |
| 211 | + { |
| 212 | + return String.format(TOOLCHAIN_NAME, name); |
| 213 | + } |
| 214 | + } |
| 215 | +} |
0 commit comments