Skip to content

Commit

Permalink
add CUSTOM_COMPONENTS_PATH support, and optimize RELEASE DEBUG define
Browse files Browse the repository at this point in the history
  • Loading branch information
Neutree committed Aug 31, 2023
1 parent b4753ec commit 8becc4d
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 10 deletions.
1 change: 1 addition & 0 deletions Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ endmenu

menu "Components configuration"
osource "${SDK_PATH}/components/*/Kconfig"
osource "${CUSTOM_COMPONENTS_PATH}/*/Kconfig"
osource "${PROJECT_PATH}/../components/*/Kconfig"
osource "${PROJECT_PATH}/*/Kconfig"
endmenu
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,19 @@ python3 project.py menuconfig
python3 project.py build
```

## Custom components path

Generally, the common components are placed in the `SDK directory -> components directory`, and the project-specific components are placed in the `project directory -> components directory`.
In addition, users can also customize the storage location of their common components by setting the system environment variable `CUSTOM_COMPONENTS_PATH`, for example:
Linux:
```
export CUSTOM_COMPONENTS_PATH=/home/neucrack/my_components
```
Windows just add `CUSTOM_COMPONENTS_PATH` variable in the environment variable interface.
> The name `CUSTOM_COMPONENTS_PATH` can be modified according to your project name or preference in the `project.py` and `CMakeLists.txt` of the project.
Then you can directly use `list(APPEND ADD_REQUIREMENTS component_name)` to reference it in the project component.

## Debug and Release version

By default, it is compiled in debug version. If you want to release version, you can use the following command:
Expand Down
13 changes: 13 additions & 0 deletions README_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,19 @@ python3 project.py menuconfig
python3 project.py build
```

## 设置自定义组件库路径

一般来说通用的组件放在 `SDK 目录 -> components 目录`下, 而工程特有的组件放在 `工程目录 -> components 目录`下。
除此之外,用户还可以自定义自己的通用组件的存放位置,通过在系统环境变量设置 `CUSTOM_COMPONENTS_PATH` 来指定自定义组件的存放位置,比如:
Linux 下:
```
export CUSTOM_COMPONENTS_PATH=/home/neucrack/my_components
```
Windows 直接在环境变量界面中添加`CUSTOM_COMPONENTS_PATH`变量即可。
> `CUSTOM_COMPONENTS_PATH`这个名称可以在工程的`project.py``CMakeLists.txt`中根据你的项目名称或者喜好修改。
然后就能在工程的组件里面直接通过 `list(APPEND ADD_REQUIREMENTS 组件名)` 来引用了。

## 调试 (Debug) 版本和发布 (Release) 版本

默认都是以 debug 版本编译,如果要发布版本,可以使用以下命令:
Expand Down
15 changes: 13 additions & 2 deletions examples/demo1/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
cmake_minimum_required(VERSION 3.7.2)

set(SDK_ENV_NAME "MY_SDK_PATH")
set(CUSTOM_COMPONENTS_PATH_ENV_NAME "CUSTOM_COMPONENTS_PATH")

set(SDK_PATH_ENV $ENV{${SDK_ENV_NAME}})
set(CUSTOM_COMPONENTS_PATH_ENV $ENV{${CUSTOM_COMPONENTS_PATH_ENV_NAME}})

# Get SDK path
if(NOT SDK_PATH)
get_filename_component(SDK_PATH ../../ ABSOLUTE)
if(SDK_PATH_ENV)
if(EXISTS ${SDK_PATH_ENV})
set(SDK_PATH $ENV{${SDK_ENV_NAME}})
set(SDK_PATH ${SDK_PATH_ENV})
else()
message(FATAL_ERROR "Env variable '${SDK_ENV_NAME}' set, but '$ENV{${SDK_ENV_NAME}}', path not exists")
message(FATAL_ERROR "Env variable '${SDK_ENV_NAME}' set, but '${SDK_PATH_ENV}', path not exists")
endif()
endif()
endif()
if(NOT CUSTOM_COMPONENTS_PATH)
if(CUSTOM_COMPONENTS_PATH_ENV)
if(EXISTS ${CUSTOM_COMPONENTS_PATH_ENV})
set(CUSTOM_COMPONENTS_PATH ${CUSTOM_COMPONENTS_PATH_ENV})
else()
message(FATAL_ERROR "Env variable '${CUSTOM_COMPONENTS_PATH_ENV_NAME}' set, but '${CUSTOM_COMPONENTS_PATH_ENV}', path not exists")
endif()
endif()
endif()
Expand Down
10 changes: 10 additions & 0 deletions examples/demo1/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import sys, os

sdk_env_name = "MY_SDK_PATH"
custom_component_path_name = "CUSTOM_COMPONENTS_PATH"

# get SDK absolute path
sdk_path = os.path.abspath(sys.path[0]+"/../../")
Expand All @@ -20,6 +21,15 @@
pass
print("-- SDK_PATH:{}".format(sdk_path))

# get custom components path
custom_components_path = None
try:
if os.environ[custom_component_path_name] and os.path.exists(os.environ[custom_component_path_name]):
custom_components_path = os.environ[custom_component_path_name]
except Exception:
pass
print("-- CUSTOM_COMPONENTS_PATH:{}".format(custom_components_path))

# execute project script from SDK
project_file_path = sdk_path+"/tools/cmake/project.py"
with open(project_file_path) as f:
Expand Down
10 changes: 8 additions & 2 deletions tools/cmake/compile.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,10 @@ macro(project name)

# Find components in SDK's components folder, register components
find_components(components_dirs components_kconfig_files kconfig_defaults_files_args found_main ${SDK_PATH}/components/*)
# Find components in custom components folder, register components
if(CUSTOM_COMPONENTS_PATH)
find_components(components_dirs components_kconfig_files kconfig_defaults_files_args found_main ${CUSTOM_COMPONENTS_PATH}/*)
endif()
# Find components in projects' shared components folder, register components
find_components(components_dirs components_kconfig_files kconfig_defaults_files_args found_main ${PROJECT_SOURCE_DIR}/../components/*)
# Find components in project folder
Expand Down Expand Up @@ -283,6 +287,7 @@ macro(project name)
--menuconfig False
--env "SDK_PATH=${SDK_PATH}"
--env "PROJECT_PATH=${PROJECT_SOURCE_DIR}"
--env "CUSTOM_COMPONENTS_PATH=${CUSTOM_COMPONENTS_PATH}"
--env "BUILD_TYPE=${CMAKE_BUILD_TYPE}"
--output makefile ${PROJECT_BINARY_DIR}/config/global_config.mk
--output cmake ${PROJECT_BINARY_DIR}/config/global_config.cmake
Expand All @@ -294,6 +299,7 @@ macro(project name)
--menuconfig True
--env "SDK_PATH=${SDK_PATH}"
--env "PROJECT_PATH=${PROJECT_SOURCE_DIR}"
--env "CUSTOM_COMPONENTS_PATH=${CUSTOM_COMPONENTS_PATH}"
--env "BUILD_TYPE=${CMAKE_BUILD_TYPE}"
--output makefile ${PROJECT_BINARY_DIR}/config/global_config.mk
--output cmake ${PROJECT_BINARY_DIR}/config/global_config.cmake
Expand Down Expand Up @@ -375,9 +381,9 @@ macro(project name)

# add DEBUG or RELEASE flag globally
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
add_definitions(-DDEBUG=1)
add_definitions(-DDEBUG=1 -DRELEASE=0)
else()
add_definitions(-DRELEASE=1)
add_definitions(-DRELEASE=1 -DDEBUG=0)
endif()

# Add dependence: update configfile, append time and git info for global config header file
Expand Down
14 changes: 10 additions & 4 deletions tools/cmake/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,12 @@ def get_config_files(config_file, sdk_path, project_path):
print("config file path error:{}".format(config_path))
exit(1)
print("-- build type: {}".format(build_type))
res = subprocess.call(["cmake", "-G", configs["CONFIG_CMAKE_GENERATOR"],
cmd = ["cmake", "-G", configs["CONFIG_CMAKE_GENERATOR"],
"-DCMAKE_BUILD_TYPE={}".format(build_type),
"-DDEFAULT_CONFIG_FILE={}".format(config_path), ".."])
"-DDEFAULT_CONFIG_FILE={}".format(config_path), ".."]
if custom_components_path:
cmd.insert(4, "-DCUSTOM_COMPONENTS_PATH={}".format(custom_components_path))
res = subprocess.call(cmd)
if res != 0:
exit(1)
if project_args.verbose:
Expand Down Expand Up @@ -274,9 +277,12 @@ def get_config_files(config_file, sdk_path, project_path):
if not os.path.exists(config_path):
print("config file path error:{}".format(config_path))
exit(1)
res = subprocess.call(["cmake", "-G", configs["CONFIG_CMAKE_GENERATOR"],
cmd = ["cmake", "-G", configs["CONFIG_CMAKE_GENERATOR"],
"-DCMAKE_BUILD_TYPE={}".format(build_type),
"-DDEFAULT_CONFIG_FILE={}".format(config_path), ".."])
"-DDEFAULT_CONFIG_FILE={}".format(config_path), ".."]
if custom_components_path:
cmd.insert(4, "-DCUSTOM_COMPONENTS_PATH={}".format(custom_components_path))
res = subprocess.call(cmd)
if res != 0:
exit(1)
# res = subprocess.call(["cmake", "--build", ".", "--parallel", "1", "--target", "menuconfig"])
Expand Down
4 changes: 2 additions & 2 deletions tools/kconfig/genconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ def write_header(kconfig, filename, gui):
build_type = os.environ.get("BUILD_TYPE", "debug").lower()
if build_type in ["release", "minsizerel", "relwithdebinfo"]:
print("-- build type:{}, write RELEASE definition to c header".format(build_type))
new += "#define RELEASE 1\n"
new += "#define RELEASE 1\n#define DEBUG 0\n"
else:
print("-- build type:{}, write DEBUG definition to c header".format(build_type))
new += "#define DEBUG 1\n"
new += "#define DEBUG 1\n#define RELEASE 0\n"
if old == new:
print("-- c header file not changed, skip write")
return
Expand Down

0 comments on commit 8becc4d

Please sign in to comment.