From 9ce7f00469d31229a619d40642683a147e0d7178 Mon Sep 17 00:00:00 2001 From: Kai Hermann Date: Fri, 29 Nov 2024 11:42:50 +0100 Subject: [PATCH] Auto-detect everest project dirs (eg. everest-core) from CMakeCache.txt (#169) * Auto-detect everest project dirs (eg. everest-core) from CMakeCache.txt Signed-off-by: Kai-Uwe Hermann * Refactor everest-framework schemas detection to use new helper function Signed-off-by: Kai-Uwe Hermann * Bump version of ev-cli to 0.4.4 Signed-off-by: Kai-Uwe Hermann --------- Signed-off-by: Kai-Uwe Hermann --- ev-dev-tools/src/ev_cli/__init__.py | 2 +- ev-dev-tools/src/ev_cli/ev.py | 33 ++++++++++++------------ ev-dev-tools/src/ev_cli/helpers.py | 40 +++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 18 deletions(-) diff --git a/ev-dev-tools/src/ev_cli/__init__.py b/ev-dev-tools/src/ev_cli/__init__.py index 04bbb7b..e90309c 100644 --- a/ev-dev-tools/src/ev_cli/__init__.py +++ b/ev-dev-tools/src/ev_cli/__init__.py @@ -1,2 +1,2 @@ """EVerest command line utility.""" -__version__ = '0.4.3' +__version__ = '0.4.4' diff --git a/ev-dev-tools/src/ev_cli/ev.py b/ev-dev-tools/src/ev_cli/ev.py index b75ed13..f7c4684 100755 --- a/ev-dev-tools/src/ev_cli/ev.py +++ b/ev-dev-tools/src/ev_cli/ev.py @@ -539,6 +539,10 @@ def generate_interface_headers(interface, all_interfaces_flag, output_dir): def module_create(args): create_strategy = 'force-create' if args.force else 'create' + detected_projects = helpers.detect_everest_projects(args.everest_projects, args.build_dir) + if detected_projects: + helpers.everest_dirs.extend(detected_projects) + mod_files = generate_module_files(args.module, False, args.licenses) if args.only == 'which': @@ -559,6 +563,10 @@ def module_create(args): def module_update(args): + detected_projects = helpers.detect_everest_projects(args.everest_projects, args.build_dir) + if detected_projects: + helpers.everest_dirs.extend(detected_projects) + # Always generate type info before updating module for type_with_namespace in list_types_with_namespace(): _tmpl_data, _last_mtime = TypeParser.generate_type_info(type_with_namespace, all_types=True) @@ -748,7 +756,11 @@ def main(): common_parser.add_argument('--work-dir', '-wd', type=str, help='work directory containing the manifest definitions (default: .)', default=str(Path.cwd())) common_parser.add_argument('--everest-dir', '-ed', nargs='*', - help='everest directory containing the interface definitions (default: .)', default=[str(Path.cwd())]) + help='everest directory containing the interface definitions (default: .)', + default=[str(Path.cwd()), str(Path.cwd() / '../everest-core')]) + common_parser.add_argument('--everest-projects', '-ep', nargs='*', + help='everest project names. used in auto detection of their directories to get eg. interface defintions (default: everest-core)', + default=['everest-core']) common_parser.add_argument('--schemas-dir', '-sd', type=str, help='everest framework directory containing the schema definitions (default: ../everest-framework/schemas)', default=str(Path.cwd() / '../everest-framework/schemas')) @@ -872,23 +884,10 @@ def main(): ' doesn\'t exist.\n' f'dir: {schemas_dir}') cmake_cache_path = Path(args.build_dir) / 'CMakeCache.txt' - print(f'Searching for everest-framework in: {cmake_cache_path}') - print('You can either provide the schemas directory with --schemas-dir or influence the' - ' automatic search path by setting --build-dir (default: ./build)') - if not cmake_cache_path.exists(): - print(f'CMakeCache.txt does not exist: {cmake_cache_path}') + found_dir = helpers.get_path_from_cmake_cache('everest-framework', cmake_cache_path, '--schemas-dir') + if not found_dir: exit(1) - with open(cmake_cache_path, 'r') as cmake_cache_file: - search = 'everest-framework_SOURCE_DIR:STATIC=' - for line in cmake_cache_file: - if line.startswith(search): - found_schemas_dir = Path(line.replace(search, '', 1).strip(' \t\n\r')) / 'schemas' - if found_schemas_dir.exists(): - print(f'Found everest-framework schemas directory: {found_schemas_dir}') - user_choice = input('Do you want to use this? [Y/n] ').lower() - if user_choice == 'y' or not user_choice: - schemas_dir = found_schemas_dir - break + schemas_dir = found_dir / 'schemas' if not schemas_dir.exists(): exit(1) diff --git a/ev-dev-tools/src/ev_cli/helpers.py b/ev-dev-tools/src/ev_cli/helpers.py index 20a9673..ad2cc66 100644 --- a/ev-dev-tools/src/ev_cli/helpers.py +++ b/ev-dev-tools/src/ev_cli/helpers.py @@ -775,3 +775,43 @@ def get_license_header(license_dirs, license_url): return None with open(license_path, 'r') as custom_license_file: return custom_license_file.read().strip() + + +def get_path_from_cmake_cache(variable_prefix, cmake_cache_path, option_name): + print(f'Searching for {variable_prefix} in: {cmake_cache_path}') + print(f'You can either provide the {variable_prefix} directory with {option_name} or influence the' + ' automatic search path by setting --build-dir (default: ./build)') + if not cmake_cache_path.exists(): + print(f'CMakeCache.txt does not exist: {cmake_cache_path}') + return None + with open(cmake_cache_path, 'r') as cmake_cache_file: + search = f'{variable_prefix}_SOURCE_DIR:STATIC=' + for line in cmake_cache_file: + if line.startswith(search): + found_dir = Path(line.replace(search, '', 1).strip(' \t\n\r')) + if found_dir.exists(): + print(f'Found {variable_prefix} directory: {found_dir}') + user_choice = input('Do you want to use this? [Y/n] ').lower() + if user_choice == 'y' or not user_choice: + return found_dir + break + return None + + +def detect_everest_projects(everest_projects, build_dir): + detected_everest_project = False + for everest_dir in everest_dirs: + if everest_dir.exists() and everest_dir.name in everest_projects: + detected_everest_project = True + + found_dirs = [] + + if not detected_everest_project: + print('Could not detect ' + ", ".join(everest_projects) + ' path in --everest-dir') + cmake_cache_path = Path(build_dir) / 'CMakeCache.txt' + for everest_project in everest_projects: + found_dir = get_path_from_cmake_cache(everest_project, cmake_cache_path, '--everest-dir') + if found_dir: + found_dirs.append(found_dir) + + return found_dirs