Create a minimal distribution based on compile_commands.json but keeping all build system files. First copies everything like MkDist, then only removes unused source files while keeping all headers.
(program, BSP_ROOT, RTT_ROOT, env, project_name, project_path=None)
| 253 | print('dist project successfully!') |
| 254 | |
| 255 | def MkDist_Strip(program, BSP_ROOT, RTT_ROOT, env, project_name, project_path=None): |
| 256 | """Create a minimal distribution based on compile_commands.json but keeping all build system files. |
| 257 | First copies everything like MkDist, then only removes unused source files while keeping all headers. |
| 258 | """ |
| 259 | print('Making minimal distribution for project...') |
| 260 | |
| 261 | if project_path == None: |
| 262 | dist_dir = os.path.join(BSP_ROOT, 'dist', project_name) |
| 263 | else: |
| 264 | dist_dir = project_path |
| 265 | |
| 266 | # First do a full distribution copy |
| 267 | MkDist(program, BSP_ROOT, RTT_ROOT, env, project_name, project_path) |
| 268 | print('\n=> Starting source files cleanup...') |
| 269 | |
| 270 | # Get the minimal required source paths |
| 271 | import compile_commands |
| 272 | used_paths = compile_commands.get_minimal_dist_paths( |
| 273 | os.path.join(BSP_ROOT, 'compile_commands.json'), |
| 274 | RTT_ROOT |
| 275 | ) |
| 276 | |
| 277 | # Clean up RT-Thread directory except tools and build files |
| 278 | rt_thread_dir = os.path.join(dist_dir, 'rt-thread') |
| 279 | source_extensions = ('.c', '.cpp', '.cxx', '.cc', '.s', '.S') |
| 280 | |
| 281 | removed_files = [] |
| 282 | removed_dirs = [] |
| 283 | |
| 284 | for root, dirs, files in os.walk(rt_thread_dir, topdown=False): |
| 285 | rel_path = os.path.relpath(root, rt_thread_dir) |
| 286 | |
| 287 | if rel_path.startswith('tools') or rel_path.startswith('include'): |
| 288 | continue |
| 289 | |
| 290 | keep_files = { |
| 291 | 'SConscript', |
| 292 | 'Kconfig', |
| 293 | 'Sconscript', |
| 294 | '.config', |
| 295 | 'rtconfig.h' |
| 296 | } |
| 297 | |
| 298 | for f in files: |
| 299 | if f in keep_files: |
| 300 | continue |
| 301 | |
| 302 | if not f.endswith(source_extensions): |
| 303 | continue |
| 304 | |
| 305 | file_path = os.path.join(root, f) |
| 306 | rel_file_path = os.path.relpath(file_path, rt_thread_dir) |
| 307 | dir_name = os.path.dirname(rel_file_path) |
| 308 | |
| 309 | if dir_name not in used_paths and rel_file_path not in used_paths: |
| 310 | os.remove(file_path) |
| 311 | removed_files.append(rel_file_path) |
| 312 |