From 04559b2260c74d32691a8641a921268ff1fa3dbc Mon Sep 17 00:00:00 2001 From: Kai-Uwe Hermann Date: Fri, 4 Oct 2024 18:19:03 +0200 Subject: [PATCH] Install ev-cli locally and depend on templates files for generation Signed-off-by: Kai-Uwe Hermann --- CMakeLists.txt | 2 ++ cmake/ev-cli.cmake | 32 +++++++++++++------ cmake/everest-generate.cmake | 21 +++++++++++-- scripts/get_package_location.py | 54 +++++++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+), 13 deletions(-) create mode 100755 scripts/get_package_location.py diff --git a/CMakeLists.txt b/CMakeLists.txt index 1863b4c1c..0f2ab2dba 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -65,6 +65,8 @@ ev_setup_python_executable( PYTHON_VENV_PATH ${${PROJECT_NAME}_PYTHON_VENV_PATH} ) +set(EVEREST_SCRIPTS_DIR "${CMAKE_CURRENT_SOURCE_DIR}/scripts") + # Already include CTest here to allow it to find tests defined in subdirectories like lib and modules if(EVEREST_CORE_BUILD_TESTING) include(CTest) diff --git a/cmake/ev-cli.cmake b/cmake/ev-cli.cmake index 9a63653a6..5d5fa997c 100644 --- a/cmake/ev-cli.cmake +++ b/cmake/ev-cli.cmake @@ -1,11 +1,4 @@ macro(setup_ev_cli) - if(NOT TARGET ev-cli) - add_custom_target(ev-cli) - endif() - if(${EV_CLI}) - message(FATAL_ERROR "EV_CLI is already defined.") - return() - endif() if(NOT ${${PROJECT_NAME}_USE_PYTHON_VENV}) find_program(EV_CLI ev-cli REQUIRED) else() @@ -15,9 +8,28 @@ macro(setup_ev_cli) if(NOT ${IS_PYTHON_VENV_ACTIVE}) message(FATAL_ERROR "Python venv is not active. Please activate the python venv before running this command.") endif() - set(EV_CLI "${${PROJECT_NAME}_PYTHON_VENV_PATH}/bin/ev-cli") - add_dependencies(ev-cli - ev-dev-tools_pip_install_dist + find_program(EV_CLI ev-cli REQUIRED) + + find_program(EV_GET_PACKAGE_LOCATION + NAMES get_package_location.py + PATHS "${EVEREST_SCRIPTS_DIR}" + NO_DEFAULT_PATH + ) + execute_process( + COMMAND ${EV_GET_PACKAGE_LOCATION} --package-name ev-dev-tools + OUTPUT_VARIABLE EV_CLI_PACKAGE_LOCATION + OUTPUT_STRIP_TRAILING_WHITESPACE + RESULT_VARIABLE + EV_GET_PACKAGE_LOCATION_RESULT + ) + if(EV_GET_PACKAGE_LOCATION_RESULT AND NOT EV_GET_PACKAGE_LOCATION_RESULT EQUAL 0) + # TODO: this probably does not have to be a FATAL_ERROR + message(FATAL_ERROR "Could not get ev-dev-tools package location") + endif() + message(STATUS "Using ev-cli package: ${EV_CLI_PACKAGE_LOCATION}") + set_property( + GLOBAL + PROPERTY EV_CLI_TEMPLATES_DIR "${EV_CLI_PACKAGE_LOCATION}/src/ev_cli/templates" ) endif() endmacro() diff --git a/cmake/everest-generate.cmake b/cmake/everest-generate.cmake index c5fe381d3..b5d98e9d3 100644 --- a/cmake/everest-generate.cmake +++ b/cmake/everest-generate.cmake @@ -307,13 +307,18 @@ function (_ev_add_interfaces) # FIXME (aw): check for duplicates here! get_target_property(GENERATED_OUTPUT_DIR generate_cpp_files EVEREST_GENERATED_OUTPUT_DIR) set(CHECK_DONE_FILE "${GENERATED_OUTPUT_DIR}/.interfaces_generated_${EVEREST_PROJECT_NAME}") + get_property(EV_CLI_TEMPLATES_DIR + GLOBAL + PROPERTY EV_CLI_TEMPLATES_DIR + ) add_custom_command( OUTPUT "${CHECK_DONE_FILE}" DEPENDS ${ARGV} - ev-cli + ${EV_CLI_TEMPLATES_DIR}/interface-Base.hpp.j2 + ${EV_CLI_TEMPLATES_DIR}/interface-Exports.hpp.j2 COMMENT "Generating/updating interface files ..." VERBATIM @@ -346,13 +351,17 @@ function (_ev_add_types) # FIXME (aw): check for duplicates here! get_target_property(GENERATED_OUTPUT_DIR generate_cpp_files EVEREST_GENERATED_OUTPUT_DIR) set(CHECK_DONE_FILE "${GENERATED_OUTPUT_DIR}/.types_generated_${EVEREST_PROJECT_NAME}") + get_property(EV_CLI_TEMPLATES_DIR + GLOBAL + PROPERTY EV_CLI_TEMPLATES_DIR + ) add_custom_command( OUTPUT "${CHECK_DONE_FILE}" DEPENDS ${ARGV} - ev-cli + ${EV_CLI_TEMPLATES_DIR}/types.hpp.j2 COMMENT "Generating/updating type files ..." VERBATIM @@ -484,6 +493,11 @@ function (ev_add_cpp_module MODULE_NAME) set(GENERATED_MODULE_DIR "${GENERATED_OUTPUT_DIR}/modules") set(MODULE_LOADER_DIR ${GENERATED_MODULE_DIR}/${MODULE_NAME}) + get_property(EV_CLI_TEMPLATES_DIR + GLOBAL + PROPERTY EV_CLI_TEMPLATES_DIR + ) + add_custom_command( OUTPUT ${MODULE_LOADER_DIR}/ld-ev.hpp @@ -496,7 +510,8 @@ function (ev_add_cpp_module MODULE_NAME) ${RELATIVE_MODULE_DIR} DEPENDS ${MODULE_PATH}/manifest.yaml - ev-cli + ${EV_CLI_TEMPLATES_DIR}/ld-ev.cpp.j2 + ${EV_CLI_TEMPLATES_DIR}/ld-ev.hpp.j2 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} COMMENT diff --git a/scripts/get_package_location.py b/scripts/get_package_location.py new file mode 100755 index 000000000..72d66041f --- /dev/null +++ b/scripts/get_package_location.py @@ -0,0 +1,54 @@ +#!/usr/bin/env -S python3 -tt +# -*- coding: utf-8 -*- +# +# SPDX-License-Identifier: Apache-2.0 +# Copyright Pionix GmbH and Contributors to EVerest +# +""" +author: kai-uwe.hermann@pionix.de +""" + +import argparse +from importlib.metadata import Distribution, PackageNotFoundError +import json + + +__version__ = '0.1.0' + + +class EVerestParsingException(SystemExit): + pass + + +def main(): + parser = argparse.ArgumentParser( + description='EVerest get package location') + parser.add_argument('--version', action='version', + version=f'%(prog)s {__version__}') + parser.add_argument('--package-name', type=str, + help='Name of the package that the location should be retrieved from', default=None) + args = parser.parse_args() + + if not 'package_name' in args or not args.package_name: + raise EVerestParsingException('Please provide a valid package name') + + direct_url = "" + try: + direct_url = Distribution.from_name( + args.package_name).read_text('direct_url.json') + except PackageNotFoundError as e: + raise EVerestParsingException(e) + url = json.loads(direct_url).get('url', None) + + if url and url.startswith('file://'): + url = url.replace('file://', '') + print(f'{url}') + else: + raise EVerestParsingException() + + +if __name__ == '__main__': + try: + main() + except EVerestParsingException as e: + raise SystemExit(e)