From f06db8cb8fcb140822b78de593d4cb44dcb8b34c Mon Sep 17 00:00:00 2001 From: Neucrack Date: Sun, 31 Mar 2024 17:56:15 +0800 Subject: [PATCH] optimize add ADD_FILE_DEPENDS var --- Kconfig | 5 ++- README.md | 4 +-- README_ZH.md | 4 +-- components/component1/CMakeLists.txt | 7 ++++ components/component2/CMakeLists.txt | 7 ++++ components/component3/CMakeLists.txt | 7 ++++ examples/demo1/main/CMakeLists.txt | 13 +++++++ tools/cmake/compile.cmake | 43 ++++++++++++++++------ tools/cmake/project.py | 2 +- tools/cmake/utils.py | 53 ++++++++++++++++++++++++++++ tools/{ => cmds}/flash.py | 0 tools/{ => cmds}/run.py | 0 12 files changed, 129 insertions(+), 16 deletions(-) create mode 100644 tools/cmake/utils.py rename tools/{ => cmds}/flash.py (100%) rename tools/{ => cmds}/run.py (100%) diff --git a/Kconfig b/Kconfig index 3d74047..822e8a2 100644 --- a/Kconfig +++ b/Kconfig @@ -11,9 +11,12 @@ menu "Toolchain configuration" default "" endmenu -menu "Components configuration" +menu "SDK Components Configuration" osource "${SDK_PATH}/components/*/Kconfig" osource "${CUSTOM_COMPONENTS_PATH}/*/Kconfig" +endmenu +menu "Project Components Configuration" osource "${PROJECT_PATH}/../components/*/Kconfig" osource "${PROJECT_PATH}/*/Kconfig" + osource "${PROJECT_PATH}/components/*/Kconfig" endmenu diff --git a/README.md b/README.md index 99517f3..8746a0f 100644 --- a/README.md +++ b/README.md @@ -271,8 +271,8 @@ node demo1.js ## Add command -By default we can use `python project.py run` to call [tools/run.py](./tools/run.py) file, and execute the binary file. -If you want to add commands for your SDK, just create new `py` file in tools directory, write a script and content refer to [tools/run.py](./tools/run.py). +By default we can use `python project.py run` to call [tools/cmds/run.py](./tools/run.py) file, and execute the binary file. +If you want to add commands for your SDK, just create new `py` file in tools directory, write a script and content refer to [tools/cmds/run.py](./tools/cmds/run.py). ## Online Debugging diff --git a/README_ZH.md b/README_ZH.md index 933b9db..7432c44 100644 --- a/README_ZH.md +++ b/README_ZH.md @@ -269,8 +269,8 @@ node demo1.js ## 增加新的命令 -比如默认使用 `python project.py run` 命令会调用`tools/run.py`脚本来执行构建出来的可执行文件。 -如果你需要给你的 SDK 增加命令,只需要创建一个新的文件,参考[tools/run.py](./tools/run.py)文件的写法即可 +比如默认使用 `python project.py run` 命令会调用`tools/cmds/run.py`脚本来执行构建出来的可执行文件。 +如果你需要给你的 SDK 增加命令,只需要创建一个新的文件,参考[tools/cmds/run.py](./tools/cmds/run.py)文件的写法即可 ## 在线调试 diff --git a/components/component1/CMakeLists.txt b/components/component1/CMakeLists.txt index 3adab2b..70059a2 100644 --- a/components/component1/CMakeLists.txt +++ b/components/component1/CMakeLists.txt @@ -21,6 +21,13 @@ list(APPEND ADD_SRCS "src/lib1.c" ###### Add required/dependent components ###### # list(APPEND ADD_REQUIREMENTS component1) +# +# list(APPEND ADD_FILE_DEPENDS include/axx.h) +# set_property(SOURCE ${python_h_path} PROPERTY GENERATED 1) +# add_custom_command(OUTPUT include/axx.h +# COMMAND echo "" > include/axx.h +# COMMENT "Generating axx.h ..." +# ) ############################################### ###### Add link search path for requirements/libs ###### diff --git a/components/component2/CMakeLists.txt b/components/component2/CMakeLists.txt index f8bb8d3..8ac5660 100644 --- a/components/component2/CMakeLists.txt +++ b/components/component2/CMakeLists.txt @@ -25,6 +25,13 @@ if(CONFIG_COMPONENT2_ENABLED) ###### Add required/dependent components ###### list(APPEND ADD_REQUIREMENTS component1) + # + # list(APPEND ADD_FILE_DEPENDS include/axx.h) + # set_property(SOURCE ${python_h_path} PROPERTY GENERATED 1) + # add_custom_command(OUTPUT include/axx.h + # COMMAND echo "" > include/axx.h + # COMMENT "Generating axx.h ..." + # ) ############################################### ###### Add link search path for requirements/libs ###### diff --git a/components/component3/CMakeLists.txt b/components/component3/CMakeLists.txt index 3d50870..a78036d 100644 --- a/components/component3/CMakeLists.txt +++ b/components/component3/CMakeLists.txt @@ -25,6 +25,13 @@ if(CONFIG_COMPONENT3_ENABLED) ###### Add required/dependent components ###### list(APPEND ADD_REQUIREMENTS component1) + # + # list(APPEND ADD_FILE_DEPENDS include/axx.h) + # set_property(SOURCE ${python_h_path} PROPERTY GENERATED 1) + # add_custom_command(OUTPUT include/axx.h + # COMMAND echo "" > include/axx.h + # COMMENT "Generating axx.h ..." + # ) ############################################### ###### Add link search path for requirements/libs ###### diff --git a/examples/demo1/main/CMakeLists.txt b/examples/demo1/main/CMakeLists.txt index 8479c59..cf52dfd 100644 --- a/examples/demo1/main/CMakeLists.txt +++ b/examples/demo1/main/CMakeLists.txt @@ -30,6 +30,13 @@ endif() if(CONFIG_COMPONENT3_ENABLED) list(APPEND ADD_REQUIREMENTS component3) endif() +# +# list(APPEND ADD_FILE_DEPENDS include/axx.h) +# set_property(SOURCE ${python_h_path} PROPERTY GENERATED 1) +# add_custom_command(OUTPUT include/axx.h +# COMMAND echo "" > include/axx.h +# COMMENT "Generating axx.h ..." +# ) ############################################### ###### Add link search path for requirements/libs ###### @@ -43,6 +50,12 @@ endif() # list(APPEND ADD_STATIC_LIB "lib/libtest.a") ############################################### +############ Add dynamic libs ################## +# list(APPEND ADD_DYNAMIC_LIB "lib/arch/v831/libmaix_nn.so" +# "lib/arch/v831/libmaix_cam.so" +# ) +############################################### + #### Add compile option for this component #### #### Just for this component, won't affect other #### modules, including component that depend diff --git a/tools/cmake/compile.cmake b/tools/cmake/compile.cmake index 13b68f3..534dbbd 100644 --- a/tools/cmake/compile.cmake +++ b/tools/cmake/compile.cmake @@ -96,12 +96,19 @@ function(register_component) # Add definitions public foreach(difinition ${ADD_DEFINITIONS}) - target_compile_options(${component_name} PUBLIC ${difinition}) + if(${include_type} STREQUAL INTERFACE) + target_compile_options(${component_name} INTERFACE ${difinition}) + target_link_options(${component_name} INTERFACE ${difinition}) + else() + target_compile_options(${component_name} PUBLIC ${difinition}) + target_link_options(${component_name} PUBLIC ${difinition}) + endif() endforeach() # Add definitions private foreach(difinition ${ADD_DEFINITIONS_PRIVATE}) target_compile_options(${component_name} PRIVATE ${difinition}) + target_link_options(${component_name} PRIVATE ${difinition}) endforeach() # Add lib search path @@ -161,28 +168,43 @@ function(register_component) # Add requirements target_link_libraries(${component_name} ${include_type} ${ADD_REQUIREMENTS}) + + # Add file depends + if(ADD_FILE_DEPENDS) + add_custom_target(${component_name}_file_depends DEPENDS ${ADD_FILE_DEPENDS}) + add_dependencies(${component_name} ${component_name}_file_depends) + endif() endfunction() function(is_path_component ret param_path) - set(res 1) get_filename_component(abs_dir ${param_path} ABSOLUTE) if(NOT IS_DIRECTORY "${abs_dir}") - set(res 0) + set(${ret} 0 PARENT_SCOPE) + return() endif() get_filename_component(base_dir ${abs_dir} NAME) string(SUBSTRING "${base_dir}" 0 1 first_char) - if(NOT first_char STREQUAL ".") - if(NOT EXISTS "${abs_dir}/CMakeLists.txt") - set(res 0) - endif() - else() - set(res 0) + if(first_char STREQUAL ".") + set(${ret} 0 PARENT_SCOPE) + return() + endif() + if(NOT EXISTS "${abs_dir}/CMakeLists.txt") + set(${ret} 0 PARENT_SCOPE) + return() + endif() + + # check if register_component in CMakeLists.txt + file(READ "${abs_dir}/CMakeLists.txt" content) + string(FIND "${content}" "register_component" find_res) + if(find_res EQUAL -1) + set(${ret} 0 PARENT_SCOPE) + return() endif() - set(${ret} ${res} PARENT_SCOPE) + set(${ret} 1 PARENT_SCOPE) endfunction() function(find_components componet_dirs kconfigs configs found_main find_dir) @@ -249,6 +271,7 @@ macro(project name) find_components(components_dirs components_kconfig_files kconfig_defaults_files_args found_main ${PROJECT_SOURCE_DIR}/../components/*) # Find components in project folder find_components(components_dirs components_kconfig_files kconfig_defaults_files_args found_main ${PROJECT_SOURCE_DIR}/*) + find_components(components_dirs components_kconfig_files kconfig_defaults_files_args found_main ${PROJECT_SOURCE_DIR}/components/*) if(NOT found_main) message(FATAL_ERROR "=================\nCan not find main component(folder) in project folder!!\n=================") diff --git a/tools/cmake/project.py b/tools/cmake/project.py index c159122..19b284a 100644 --- a/tools/cmake/project.py +++ b/tools/cmake/project.py @@ -36,7 +36,7 @@ exit(1) # find extra tools -tools_dir = os.path.join(sdk_path, "tools") +tools_dir = os.path.join(sdk_path, "tools", "cmds") sys.path.insert(1, tools_dir) # find all .py files in tools dir extra_tools_names = [] diff --git a/tools/cmake/utils.py b/tools/cmake/utils.py new file mode 100644 index 0000000..b8c1c70 --- /dev/null +++ b/tools/cmake/utils.py @@ -0,0 +1,53 @@ +import time +import re +import os +import subprocess + +def check_all_submodule(sdk_path): + if (not os.path.exists(os.path.join(sdk_path, ".gitmodules"))) or (not os.path.exists(os.path.join(sdk_path, ".git"))): + return True, "" + with open(os.path.join(sdk_path, ".gitmodules")) as f: + content = f.read() + m = re.findall(r'path = (.*)', content) + if not m: + return True, "" + for path in m: + full_path = os.path.join(sdk_path, path) + err_msg = "Submodule {} not exists, please run `git submodule update --init --recursive` to init all submodules".format(path) + if (not os.path.exists(full_path)): + print("-- {} not exists".format(full_path)) + return False, err_msg + files = os.listdir(full_path) + if ".git" not in files: + print("-- {}/.git not exists".format(full_path)) + return False, err_msg + visible_files = [] + for name in files: + if not name.startswith("."): + visible_files.append(name) + if len(visible_files) == 0: + print("-- {} no files".format(full_path)) + return False, err_msg + # check if submodule version is the same as should be + cmd = ["git", "submodule", "status"] + p = subprocess.Popen(cmd, cwd=sdk_path, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + output, err = p.communicate("") + res = p.returncode + if res != 0: + print("-- git submodule status failed") + return False, err + try: + output = output.decode(encoding="utf-8" if os.name == "nt" else "utf-8") + except Exception: + output = output.decode(encoding="gbk" if os.name == "nt" else "utf-8") + lines = output.split("\n") + for line in lines: + if line.startswith("+"): + print("\n============================================") + print("-- [Warning]\n!! Submodule [{}] have changes, it maybe cause problems\nif you don't know what this means, please execute:\n git submodule update --init --recursive`\nto update submodule !".format(line.split(" ")[1])) + print("============================================\n") + time.sleep(1) + elif line.startswith("-"): + return False, "Submodule {} not exists, please run `git submodule update --init --recursive` to init all submodules".format(line.split(" ")[1]) + + return True, "" diff --git a/tools/flash.py b/tools/cmds/flash.py similarity index 100% rename from tools/flash.py rename to tools/cmds/flash.py diff --git a/tools/run.py b/tools/cmds/run.py similarity index 100% rename from tools/run.py rename to tools/cmds/run.py