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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
|
# -*- coding: utf-8 -*-
"""
Project generator framework for RT-Thread build system.
This module provides the base classes for project generators (MDK, IAR, VS Code, etc.).
"""
import os
import shutil
import json
import xml.etree.ElementTree as ET
from abc import ABC, abstractmethod
from typing import Dict, List, Any, Optional
from dataclasses import dataclass
from .utils import PathService
@dataclass
class GeneratorConfig:
"""Configuration for project generators."""
output_dir: str
project_name: str = "rtthread"
target_name: str = "rtthread.elf"
class ProjectGenerator(ABC):
"""Abstract base class for project generators."""
def __init__(self, config: GeneratorConfig):
self.config = config
self.path_service = PathService(os.getcwd())
@abstractmethod
def get_name(self) -> str:
"""Get generator name."""
pass
@abstractmethod
def generate(self, context, project_info: Dict[str, Any]) -> bool:
"""
Generate project files.
Args:
context: BuildContext instance
project_info: Project information from registry
Returns:
True if successful
"""
pass
@abstractmethod
def clean(self) -> bool:
"""
Clean generated files.
Returns:
True if successful
"""
pass
def _ensure_output_dir(self) -> None:
"""Ensure output directory exists."""
os.makedirs(self.config.output_dir, exist_ok=True)
def _copy_template(self, template_name: str, output_name: str = None) -> str:
"""
Copy template file to output directory.
Args:
template_name: Template file name
output_name: Output file name (defaults to template_name)
Returns:
Output file path
"""
if output_name is None:
output_name = template_name
template_dir = os.path.join(os.path.dirname(__file__), '..', 'targets')
template_path = os.path.join(template_dir, template_name)
output_path = os.path.join(self.config.output_dir, output_name)
if os.path.exists(template_path):
shutil.copy2(template_path, output_path)
return output_path
else:
raise FileNotFoundError(f"Template not found: {template_path}")
class VscodeGenerator(ProjectGenerator):
"""Visual Studio Code project generator."""
def get_name(self) -> str:
return "vscode"
def generate(self, context, project_info: Dict[str, Any]) -> bool:
"""Generate VS Code project files."""
self._ensure_output_dir()
# Create .vscode directory
vscode_dir = os.path.join(self.config.output_dir, '.vscode')
os.makedirs(vscode_dir, exist_ok=True)
# Generate c_cpp_properties.json
self._generate_cpp_properties(vscode_dir, context, project_info)
# Generate tasks.json
self._generate_tasks(vscode_dir, context)
# Generate launch.json
self._generate_launch(vscode_dir, context)
# Generate settings.json
self._generate_settings(vscode_dir)
return True
def clean(self) -> bool:
"""Clean VS Code files."""
vscode_dir = os.path.join(self.config.output_dir, '.vscode')
if os.path.exists(vscode_dir):
shutil.rmtree(vscode_dir)
return True
def _generate_cpp_properties(self, vscode_dir: str, context, project_info: Dict) -> None:
"""Generate c_cpp_properties.json."""
# Get toolchain info
toolchain = context.toolchain_manager.get_current()
compiler_path = ""
if toolchain and toolchain.info:
if toolchain.get_name() == "gcc":
compiler_path = os.path.join(toolchain.info.path, toolchain.info.prefix + "gcc")
config = {
"configurations": [
{
"name": "RT-Thread",
"includePath": [
"${workspaceFolder}/**"
] + project_info.get('all_includes', []),
"defines": [f"{k}={v}" if v != '1' else k
for k, v in project_info.get('all_defines', {}).items()],
"compilerPath": compiler_path,
"cStandard": "c99",
"cppStandard": "c++11",
"intelliSenseMode": "gcc-arm" if "arm" in compiler_path else "gcc-x64"
}
],
"version": 4
}
output_path = os.path.join(vscode_dir, 'c_cpp_properties.json')
with open(output_path, 'w') as f:
json.dump(config, f, indent=4)
def _generate_tasks(self, vscode_dir: str, context) -> None:
"""Generate tasks.json."""
tasks = {
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "scons",
"problemMatcher": "$gcc",
"group": {
"kind": "build",
"isDefault": True
}
},
{
"label": "clean",
"type": "shell",
"command": "scons -c",
"problemMatcher": "$gcc"
},
{
"label": "rebuild",
"type": "shell",
"command": "scons -c && scons",
"problemMatcher": "$gcc"
}
]
}
output_path = os.path.join(vscode_dir, 'tasks.json')
with open(output_path, 'w') as f:
json.dump(tasks, f, indent=4)
def _generate_launch(self, vscode_dir: str, context) -> None:
"""Generate launch.json."""
launch = {
"version": "0.2.0",
"configurations": [
{
"name": "Cortex Debug",
"type": "cortex-debug",
"request": "launch",
"servertype": "openocd",
"cwd": "${workspaceRoot}",
"executable": "${workspaceRoot}/" + self.config.target_name,
"device": "STM32F103C8",
"configFiles": [
"interface/stlink-v2.cfg",
"target/stm32f1x.cfg"
]
}
]
}
output_path = os.path.join(vscode_dir, 'launch.json')
with open(output_path, 'w') as f:
json.dump(launch, f, indent=4)
def _generate_settings(self, vscode_dir: str) -> None:
"""Generate settings.json."""
settings = {
"files.associations": {
"*.h": "c",
"*.c": "c",
"*.cpp": "cpp",
"*.cc": "cpp",
"*.cxx": "cpp"
},
"C_Cpp.errorSquiggles": "Enabled"
}
output_path = os.path.join(vscode_dir, 'settings.json')
with open(output_path, 'w') as f:
json.dump(settings, f, indent=4)
class CMakeGenerator(ProjectGenerator):
"""CMake project generator."""
def get_name(self) -> str:
return "cmake"
def generate(self, context, project_info: Dict[str, Any]) -> bool:
"""Generate CMakeLists.txt."""
self._ensure_output_dir()
# Get toolchain info
toolchain = context.toolchain_manager.get_current()
lines = [
"cmake_minimum_required(VERSION 3.10)",
"",
"# RT-Thread CMake Project",
f"project({self.config.project_name} C CXX ASM)",
"",
"# C Standard",
"set(CMAKE_C_STANDARD 99)",
"set(CMAKE_CXX_STANDARD 11)",
""
]
# Toolchain configuration
if toolchain and toolchain.get_name() == "gcc":
lines.extend([
"# Toolchain",
f"set(CMAKE_C_COMPILER {toolchain.info.prefix}gcc)",
f"set(CMAKE_CXX_COMPILER {toolchain.info.prefix}g++)",
f"set(CMAKE_ASM_COMPILER {toolchain.info.prefix}gcc)",
""
])
# Include directories
lines.extend([
"# Include directories",
"include_directories("
])
for inc in project_info.get('all_includes', []):
lines.append(f" {inc}")
lines.extend([")", ""])
# Definitions
lines.extend([
"# Definitions",
"add_definitions("
])
for k, v in project_info.get('all_defines', {}).items():
if v == '1':
lines.append(f" -D{k}")
else:
lines.append(f" -D{k}={v}")
lines.extend([")", ""])
# Source files
lines.extend([
"# Source files",
"set(SOURCES"
])
for src in project_info.get('all_sources', []):
lines.append(f" {src}")
lines.extend([")", ""])
# Executable
lines.extend([
"# Executable",
f"add_executable(${{PROJECT_NAME}} ${{SOURCES}})",
""
])
# Libraries
if project_info.get('all_libs'):
lines.extend([
"# Libraries",
f"target_link_libraries(${{PROJECT_NAME}}"
])
for lib in project_info['all_libs']:
lines.append(f" {lib}")
lines.extend([")", ""])
# Write file
output_path = os.path.join(self.config.output_dir, 'CMakeLists.txt')
with open(output_path, 'w') as f:
f.write('\n'.join(lines))
return True
def clean(self) -> bool:
"""Clean CMake files."""
files_to_remove = ['CMakeLists.txt', 'CMakeCache.txt']
dirs_to_remove = ['CMakeFiles']
for file in files_to_remove:
file_path = os.path.join(self.config.output_dir, file)
if os.path.exists(file_path):
os.remove(file_path)
for dir in dirs_to_remove:
dir_path = os.path.join(self.config.output_dir, dir)
if os.path.exists(dir_path):
shutil.rmtree(dir_path)
return True
class GeneratorRegistry:
"""Registry for project generators."""
def __init__(self):
self.generators: Dict[str, type] = {}
self._register_default_generators()
def _register_default_generators(self) -> None:
"""Register default generators."""
self.register("vscode", VscodeGenerator)
self.register("vsc", VscodeGenerator) # Alias
self.register("cmake", CMakeGenerator)
def register(self, name: str, generator_class: type) -> None:
"""Register a generator class."""
self.generators[name] = generator_class
def create_generator(self, name: str, config: GeneratorConfig) -> ProjectGenerator:
"""Create a generator instance."""
if name not in self.generators:
raise ValueError(f"Unknown generator: {name}")
return self.generators[name](config)
def list_generators(self) -> List[str]:
"""List available generators."""
return list(self.generators.keys())
|