Skip to content

Commit 014477a

Browse files
committed
resolving review comments
I took some motivation from Denys's comments and further tried to enhance and generalize the class for future proofing. It adds a bit more code but the maintainability is not impacted and is much easier then trying to adjust or add targets in one class but multiple places
1 parent ffa3fad commit 014477a

File tree

3 files changed

+200
-163
lines changed

3 files changed

+200
-163
lines changed

bundles/com.espressif.idf.core/src/com/espressif/idf/core/toolchain/IDFTargets.java

Lines changed: 36 additions & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77

88
import java.util.ArrayList;
99
import java.util.List;
10+
import java.util.stream.Collectors;
11+
12+
import com.espressif.idf.core.toolchain.enums.Target;
1013

1114
/**
1215
* Class to hold ESP-IDF target information including preview status
1316
*
14-
* @author Kondal Kolipaka <[email protected]>
17+
* @author Kondal Kolipaka <[email protected]>, Ali Azam Rana <[email protected]>
1518
*
1619
*/
1720
public class IDFTargets
@@ -25,22 +28,25 @@ public IDFTargets()
2528
this.previewTargets = new ArrayList<>();
2629
}
2730

28-
public void addSupportedTarget(String target)
31+
public void addSupportedTarget(String targetName)
2932
{
30-
supportedTargets.add(new IDFTarget(target, false));
33+
Target t = Target.fromString(targetName);
34+
if (t != null)
35+
supportedTargets.add(new IDFTarget(t, false));
3136
}
3237

33-
public void addPreviewTarget(String target)
38+
public void addPreviewTarget(String targetName)
3439
{
35-
previewTargets.add(new IDFTarget(target, true));
40+
Target t = Target.fromString(targetName);
41+
if (t != null)
42+
previewTargets.add(new IDFTarget(t, true));
3643
}
3744

3845
public List<IDFTarget> getAllTargets()
3946
{
40-
List<IDFTarget> allTargets = new ArrayList<>();
41-
allTargets.addAll(supportedTargets);
42-
allTargets.addAll(previewTargets);
43-
return allTargets;
47+
List<IDFTarget> all = new ArrayList<>(supportedTargets);
48+
all.addAll(previewTargets);
49+
return all;
4450
}
4551

4652
public List<IDFTarget> getSupportedTargets()
@@ -55,216 +61,83 @@ public List<IDFTarget> getPreviewTargets()
5561

5662
public boolean hasTarget(String targetName)
5763
{
58-
return getAllTargets().stream().anyMatch(target -> target.getName().equals(targetName));
64+
Target t = Target.fromString(targetName);
65+
return t != null && getAllTargets().stream().anyMatch(x -> x.getTarget() == t);
5966
}
6067

61-
/**
62-
* Get a specific target by name
63-
*
64-
* @param targetName Name of the target to find
65-
* @return IDFTarget if found, null otherwise
66-
*/
6768
public IDFTarget getTarget(String targetName)
6869
{
69-
return getAllTargets().stream().filter(target -> target.getName().equals(targetName)).findFirst().orElse(null);
70+
Target t = Target.fromString(targetName);
71+
return t == null ? null : getAllTargets().stream().filter(x -> x.getTarget() == t).findFirst().orElse(null);
7072
}
7173

72-
/**
73-
* Get all target names as strings
74-
*
75-
* @return List of target names
76-
*/
7774
public List<String> getAllTargetNames()
7875
{
79-
return getAllTargets().stream().map(IDFTarget::getName).collect(java.util.stream.Collectors.toList());
76+
return getAllTargets().stream().map(IDFTarget::getName).collect(Collectors.toList());
8077
}
8178

82-
/**
83-
* Get supported target names as strings
84-
*
85-
* @return List of supported target names
86-
*/
8779
public List<String> getSupportedTargetNames()
8880
{
89-
return getSupportedTargets().stream().map(IDFTarget::getName).collect(java.util.stream.Collectors.toList());
81+
return getSupportedTargets().stream().map(IDFTarget::getName).collect(Collectors.toList());
9082
}
9183

92-
/**
93-
* Get preview target names as strings
94-
*
95-
* @return List of preview target names
96-
*/
9784
public List<String> getPreviewTargetNames()
9885
{
99-
return getPreviewTargets().stream().map(IDFTarget::getName).collect(java.util.stream.Collectors.toList());
86+
return getPreviewTargets().stream().map(IDFTarget::getName).collect(Collectors.toList());
10087
}
10188

10289
/**
10390
* Inner class representing a single IDF target
10491
*/
10592
public static class IDFTarget
10693
{
107-
private final String name;
94+
private final Target target;
10895
private final boolean isPreview;
10996

110-
public IDFTarget(String name, boolean isPreview)
97+
public IDFTarget(Target target, boolean isPreview)
11198
{
112-
this.name = name;
99+
this.target = target;
113100
this.isPreview = isPreview;
114101
}
115102

116103
public String getName()
117104
{
118-
return name;
105+
return target.idfName();
119106
}
120107

121108
public boolean isPreview()
122109
{
123110
return isPreview;
124111
}
125112

126-
/**
127-
* Get the architecture for this target
128-
*
129-
* @return "xtensa" for esp32/esp32s2/esp32s3, "riscv32" for others
130-
*/
113+
public Target getTarget()
114+
{
115+
return target;
116+
}
117+
131118
public String getArchitecture()
132119
{
133-
switch (name)
134-
{
135-
case "esp32": //$NON-NLS-1$
136-
case "esp32s2": //$NON-NLS-1$
137-
case "esp32s3": //$NON-NLS-1$
138-
return "xtensa"; //$NON-NLS-1$
139-
case "esp32c2": //$NON-NLS-1$
140-
case "esp32c3": //$NON-NLS-1$
141-
case "esp32c6": //$NON-NLS-1$
142-
case "esp32h2": //$NON-NLS-1$
143-
case "esp32p4": //$NON-NLS-1$
144-
default:
145-
return "riscv32"; //$NON-NLS-1$
146-
}
120+
return target.architectureId();
147121
}
148122

149-
/**
150-
* Get the toolchain ID for this target
151-
*
152-
* @return toolchain ID string
153-
*/
154123
public String getToolchainId()
155124
{
156-
switch (name)
157-
{
158-
case "esp32": //$NON-NLS-1$
159-
case "esp32s2": //$NON-NLS-1$
160-
case "esp32s3": //$NON-NLS-1$
161-
return "xtensa-" + name + "-elf"; //$NON-NLS-1$ //$NON-NLS-2$
162-
case "esp32c2": //$NON-NLS-1$
163-
case "esp32c3": //$NON-NLS-1$
164-
case "esp32c6": //$NON-NLS-1$
165-
case "esp32h2": //$NON-NLS-1$
166-
case "esp32p4": //$NON-NLS-1$
167-
default:
168-
return "riscv32-esp-elf"; //$NON-NLS-1$
169-
}
125+
return target.toolchainId();
170126
}
171127

172-
/**
173-
* Get the compiler pattern for this target
174-
*
175-
* @return regex pattern for compiler
176-
*/
177128
public String getCompilerPattern()
178129
{
179-
String executableName = getExecutableName();
180-
181-
// Support both old and new unified directory structures
182-
String targetSpecificDir = getTargetSpecificDirectoryName();
183-
String unifiedDir = getUnifiedDirectoryName();
184-
185-
// Create pattern that matches either directory structure
186-
return "(?:" + targetSpecificDir + "|" + unifiedDir + ")[\\\\/]+bin[\\\\/]+" + executableName //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
187-
+ "-gcc(?:\\.exe)?$"; //$NON-NLS-1$
130+
return target.compilerPattern();
188131
}
189132

190-
/**
191-
* Get the debugger pattern for this target
192-
*
193-
* @return regex pattern for debugger
194-
*/
195133
public String getDebuggerPattern()
196134
{
197-
String executableName = getExecutableName();
198-
return executableName + "-gdb(?:\\.exe)?$"; //$NON-NLS-1$
199-
}
200-
201-
/**
202-
* Get the executable name prefix for this target (different from directory structure in ESP-IDF v5.5+)
203-
*
204-
* @return executable name prefix
205-
*/
206-
private String getExecutableName()
207-
{
208-
switch (name)
209-
{
210-
case "esp32": //$NON-NLS-1$
211-
case "esp32s2": //$NON-NLS-1$
212-
case "esp32s3": //$NON-NLS-1$
213-
return "xtensa-" + name + "-elf"; // Target-specific executable names //$NON-NLS-1$ //$NON-NLS-2$
214-
case "esp32c2": //$NON-NLS-1$
215-
case "esp32c3": //$NON-NLS-1$
216-
case "esp32c6": //$NON-NLS-1$
217-
case "esp32h2": //$NON-NLS-1$
218-
case "esp32p4": //$NON-NLS-1$
219-
default:
220-
return "riscv32-esp-elf"; // Unified executable name //$NON-NLS-1$
221-
}
222-
}
223-
224-
private String getTargetSpecificDirectoryName()
225-
{
226-
switch (name)
227-
{
228-
case "esp32": //$NON-NLS-1$
229-
case "esp32s2": //$NON-NLS-1$
230-
case "esp32s3": //$NON-NLS-1$
231-
return "xtensa-" + name + "-elf"; // Target-specific directory names //$NON-NLS-1$ //$NON-NLS-2$
232-
case "esp32c2": //$NON-NLS-1$
233-
case "esp32c3": //$NON-NLS-1$
234-
case "esp32c6": //$NON-NLS-1$
235-
case "esp32h2": //$NON-NLS-1$
236-
case "esp32p4": //$NON-NLS-1$
237-
default:
238-
return "riscv32-esp-elf"; // Same for both old and new //$NON-NLS-1$
239-
}
240-
}
241-
242-
private String getUnifiedDirectoryName()
243-
{
244-
switch (name)
245-
{
246-
case "esp32": //$NON-NLS-1$
247-
case "esp32s2": //$NON-NLS-1$
248-
case "esp32s3": //$NON-NLS-1$
249-
return "xtensa-esp-elf"; // Unified directory name //$NON-NLS-1$
250-
case "esp32c2": //$NON-NLS-1$
251-
case "esp32c3": //$NON-NLS-1$
252-
case "esp32c6": //$NON-NLS-1$
253-
case "esp32h2": //$NON-NLS-1$
254-
case "esp32p4": //$NON-NLS-1$
255-
default:
256-
return "riscv32-esp-elf"; //$NON-NLS-1$
257-
}
135+
return target.debuggerPattern();
258136
}
259137

260-
/**
261-
* Get the CMake toolchain file name for this target
262-
*
263-
* @return toolchain file name
264-
*/
265138
public String getToolchainFileName()
266139
{
267-
return "toolchain-" + name + ".cmake"; //$NON-NLS-1$ //$NON-NLS-2$
140+
return target.toolchainFileName();
268141
}
269142
}
270143
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*******************************************************************************
2+
* Copyright 2025 Espressif Systems (Shanghai) PTE LTD. All rights reserved.
3+
* Use is subject to license terms.
4+
*******************************************************************************/
5+
package com.espressif.idf.core.toolchain.enums;
6+
7+
/**
8+
* Architecture enum for ESP-IDF toolchains
9+
*
10+
* @author Ali Azam Rana
11+
*
12+
*/
13+
public enum Architecture
14+
{
15+
XTENSA("xtensa", "xtensa-esp-elf", "xtensa-%s-elf"), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
16+
RISCV32("riscv32", "riscv32-esp-elf", null); //$NON-NLS-1$//$NON-NLS-2$
17+
18+
private final String id; // e.g., "xtensa" / "riscv32"
19+
private final String unifiedDirName; // e.g., "xtensa-esp-elf" / "riscv32-esp-elf"
20+
private final String prefixTemplate; // e.g., "xtensa-%s-elf" or null when fixed
21+
22+
Architecture(String id, String unifiedDirName, String prefixTemplate)
23+
{
24+
this.id = id;
25+
this.unifiedDirName = unifiedDirName;
26+
this.prefixTemplate = prefixTemplate;
27+
}
28+
29+
public String id()
30+
{
31+
return id;
32+
}
33+
34+
public String unifiedDirName()
35+
{
36+
return unifiedDirName;
37+
}
38+
39+
/** For XTENSA, returns "xtensa-<idfName>-elf"; for RISCV, fixed "riscv32-esp-elf". */
40+
public String toolchainPrefixFor(Target t)
41+
{
42+
return prefixTemplate == null ? unifiedDirName : String.format(prefixTemplate, t.idfName());
43+
}
44+
45+
public String executablePrefixFor(Target t)
46+
{
47+
return toolchainPrefixFor(t);
48+
}
49+
50+
public String targetSpecificDirFor(Target t)
51+
{
52+
return toolchainPrefixFor(t);
53+
}
54+
}

0 commit comments

Comments
 (0)