Skip to content

Commit 26dd8ee

Browse files
committed
[tools][eclipse]: Improve RelativeProjectPath function to handle multiple path cases
1 parent 85e9c7f commit 26dd8ee

File tree

1 file changed

+77
-42
lines changed

1 file changed

+77
-42
lines changed

tools/targets/eclipse.py

Lines changed: 77 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -434,76 +434,111 @@ def UpdateProjectStructure(env, prj_name):
434434
def GenExcluding(env, project):
435435
rtt_root = os.path.abspath(env['RTT_ROOT'])
436436
bsp_root = os.path.abspath(env['BSP_ROOT'])
437+
438+
abs_source_folders = []
439+
try:
440+
import rtconfig
441+
if hasattr(rtconfig, 'PROJECT_SOURCE_FOLDERS'):
442+
for folder in rtconfig.PROJECT_SOURCE_FOLDERS:
443+
abs_source_folders.append(os.path.abspath(os.path.join(bsp_root, folder)))
444+
except ImportError:
445+
pass
446+
437447
coll_dirs = CollectPaths(project['DIRS'])
438448
all_paths_temp = [OSPath(path) for path in coll_dirs]
439449
all_paths = []
440450

441451
# add used path
442452
for path in all_paths_temp:
443-
if path.startswith(rtt_root) or path.startswith(bsp_root):
453+
is_valid_path = False
454+
# Check whether the path is within BSP, RTT, or any external source folder.
455+
if path.lower().startswith(bsp_root.lower()) or path.lower().startswith(rtt_root.lower()):
456+
is_valid_path = True
457+
else:
458+
for source_folder in abs_source_folders:
459+
if path.lower().startswith(source_folder.lower()):
460+
is_valid_path = True
461+
break
462+
463+
if is_valid_path:
444464
all_paths.append(path)
445465

446-
if bsp_root.startswith(rtt_root):
447-
# bsp folder is in the RT-Thread root folder, such as the RT-Thread source code on GitHub
448-
exclude_paths = ExcludePaths(rtt_root, all_paths)
449-
elif rtt_root.startswith(bsp_root):
450-
# RT-Thread root folder is in the bsp folder, such as project folder which generate by 'scons --dist' cmd
451-
check_path = []
452-
exclude_paths = []
453-
# analyze the primary folder which relative to BSP_ROOT and in all_paths
454-
for path in all_paths:
455-
if path.startswith(bsp_root):
456-
folders = RelativeProjectPath(env, path).split('\\')
457-
if folders[0] != '.' and '\\' + folders[0] not in check_path:
458-
check_path += ['\\' + folders[0]]
459-
# exclue the folder which has managed by scons
460-
for path in check_path:
461-
exclude_paths += ExcludePaths(bsp_root + path, all_paths)
462-
else:
463-
exclude_paths = ExcludePaths(rtt_root, all_paths)
464-
exclude_paths += ExcludePaths(bsp_root, all_paths)
465466

466-
paths = exclude_paths
467+
# Exclude the entire unused directory to support external folders.
467468
exclude_paths = []
468-
# remove the folder which not has source code by source_pattern
469-
for path in paths:
470-
# add bsp and libcpu folder and not collect source files (too more files)
469+
470+
# 1. Exclude unused directories under BSP ROOT
471+
exclude_paths += ExcludePaths(bsp_root, all_paths)
472+
473+
# 2. Exclude unused directories under RTT ROOT (if it is not within the BSP)
474+
if not rtt_root.lower().startswith(bsp_root.lower()):
475+
exclude_paths += ExcludePaths(rtt_root, all_paths)
476+
477+
# 3. Exclude all unused directories under external folders.
478+
for folder in abs_source_folders:
479+
# Avoid reprocessing folders that have already been processed as RTT_ROOT.
480+
if folder.lower() != rtt_root.lower():
481+
exclude_paths += ExcludePaths(folder, all_paths)
482+
483+
# Filter out the "unused" directories that do not actually have source files.
484+
filtered_exclude_paths = []
485+
for path in exclude_paths:
471486
if path.endswith('rt-thread\\bsp') or path.endswith('rt-thread\\libcpu'):
472-
exclude_paths += [path]
487+
filtered_exclude_paths.append(path)
473488
continue
489+
if len(CollectAllFilesinPath(path, source_pattern)):
490+
filtered_exclude_paths.append(path)
474491

475-
set = CollectAllFilesinPath(path, source_pattern)
476-
if len(set):
477-
exclude_paths += [path]
478-
479-
exclude_paths = [RelativeProjectPath(env, path).replace('\\', '/') for path in exclude_paths]
492+
# Convert the path to a project relative path.
493+
exclude_paths_relative = [RelativeProjectPath(env, path).replace('\\', '/') for path in filtered_exclude_paths]
480494

495+
# Calculate the individual files that need to be excluded.
481496
all_files = CollectFiles(all_paths, source_pattern)
482497
src_files = project['FILES']
483498

484499
exclude_files = ExcludeFiles(all_files, src_files)
485-
exclude_files = [RelativeProjectPath(env, file).replace('\\', '/') for file in exclude_files]
500+
exclude_files_relative = [RelativeProjectPath(env, file).replace('\\', '/') for file in exclude_files]
486501

487-
env['ExPaths'] = exclude_paths
488-
env['ExFiles'] = exclude_files
502+
env['ExPaths'] = exclude_paths_relative
503+
env['ExFiles'] = exclude_files_relative
489504

490-
return exclude_paths + exclude_files
505+
return exclude_paths_relative + exclude_files_relative
506+
def RelativeProjectPath(env, path):
491507

508+
clean_path_str = str(path).strip().strip(',').strip('"')
509+
510+
try:
511+
abs_path = os.path.abspath(clean_path_str)
512+
except Exception:
513+
return clean_path_str
492514

493-
def RelativeProjectPath(env, path):
494515
project_root = os.path.abspath(env['BSP_ROOT'])
516+
517+
# 1. Check if the path is within the project root directory (BSP_ROOT)
518+
if abs_path.lower().startswith(project_root.lower()):
519+
return _make_path_relative(project_root, abs_path)
520+
521+
# 2. Check if the path is within the RT-Thread root directory.
495522
rtt_root = os.path.abspath(env['RTT_ROOT'])
523+
if abs_path.lower().startswith(rtt_root.lower()):
524+
return 'rt-thread/' + _make_path_relative(rtt_root, abs_path)
525+
526+
# 3. Check the PROJECT_SOURCE_FOLDERS defined in rtconfig.py.
527+
if hasattr(rtconfig, 'PROJECT_SOURCE_FOLDERS'):
528+
for folder_entry in rtconfig.PROJECT_SOURCE_FOLDERS:
529+
# Get the absolute path of the source folder (for example 'E:/.../lib')
530+
abs_source_folder = os.path.abspath(os.path.join(project_root, folder_entry))
496531

497-
if path.startswith(project_root):
498-
return _make_path_relative(project_root, path)
532+
if abs_path.lower().startswith(abs_source_folder.lower()):
533+
# The link name in the project is the base name of the folder path (for example, '../lib' -> 'lib')
534+
link_name = os.path.basename(os.path.normpath(folder_entry))
499535

500-
if path.startswith(rtt_root):
501-
return 'rt-thread/' + _make_path_relative(rtt_root, path)
536+
relative_part = _make_path_relative(abs_source_folder, abs_path)
502537

503-
# TODO add others folder
504-
print('ERROR: the ' + path + ' not support')
538+
return os.path.join(link_name, relative_part).replace('\\', '/')
505539

506-
return path
540+
print(f'WARNING: The path "{path}" could not be made relative to the project.')
541+
return clean_path_str
507542

508543

509544
def HandleExcludingOption(entry, sourceEntries, excluding):

0 commit comments

Comments
 (0)