Skip to content

Commit f80f236

Browse files
committed
增加 混合编译工程模板
1 parent 436d1aa commit f80f236

File tree

18 files changed

+387
-0
lines changed

18 files changed

+387
-0
lines changed

template/.vscode/settings.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33
"cmake.sourceDirectory": [
44
"${workspaceFolder}/test_sample",
55
"${workspaceFolder}/test_vcpkg",
6+
"${workspaceFolder}/test_rust",
67
],
78
"cmake.buildDirectory": "${sourceDirectory}/build",
89
"cmake.autoSelectActiveFolder": true,
910
"cmake.buildBeforeRun": true,
1011
"cmake.configureOnOpen": false,
12+
"rust-analyzer.linkedProjects": [
13+
"${workspaceFolder}/test_rust/utils_rust/Cargo.toml",
14+
],
1115
}

template/test_rust/.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
**/build
2+
**/.cache
3+
**/target
4+
**/dist
5+
6+
.config.mk
7+
.flash.conf.json
8+
9+
*.lock

template/test_rust/CMakeLists.txt

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
cmake_minimum_required(VERSION 3.7.2)
2+
3+
set(SDK_ENV_NAME "MY_SDK_PATH")
4+
set(CUSTOM_COMPONENTS_PATH_ENV_NAME "CUSTOM_COMPONENTS_PATH")
5+
6+
set(SDK_PATH_ENV $ENV{${SDK_ENV_NAME}})
7+
set(CUSTOM_COMPONENTS_PATH_ENV $ENV{${CUSTOM_COMPONENTS_PATH_ENV_NAME}})
8+
9+
# Get SDK path
10+
if(NOT SDK_PATH)
11+
get_filename_component(SDK_PATH ../../ ABSOLUTE)
12+
if(SDK_PATH_ENV)
13+
if(EXISTS ${SDK_PATH_ENV})
14+
set(SDK_PATH ${SDK_PATH_ENV})
15+
else()
16+
message(FATAL_ERROR "Env variable '${SDK_ENV_NAME}' set, but '${SDK_PATH_ENV}', path not exists")
17+
endif()
18+
endif()
19+
endif()
20+
if(NOT CUSTOM_COMPONENTS_PATH)
21+
if(CUSTOM_COMPONENTS_PATH_ENV)
22+
if(EXISTS ${CUSTOM_COMPONENTS_PATH_ENV})
23+
set(CUSTOM_COMPONENTS_PATH ${CUSTOM_COMPONENTS_PATH_ENV})
24+
else()
25+
message(FATAL_ERROR "Env variable '${CUSTOM_COMPONENTS_PATH_ENV_NAME}' set, but '${CUSTOM_COMPONENTS_PATH_ENV}', path not exists")
26+
endif()
27+
endif()
28+
endif()
29+
30+
# Check SDK Path
31+
if(NOT EXISTS ${SDK_PATH})
32+
message(FATAL_ERROR "SDK path Error, Please set SDK_PATH or ${SDK_ENV_NAME} variable")
33+
endif()
34+
35+
# Get Toolchain path
36+
if(NOT CONFIG_TOOLCHAIN_PATH)
37+
if(EXISTS $ENV{MY_TOOLCHAIN_PATH})
38+
set(CONFIG_TOOLCHAIN_PATH $ENV{MY_TOOLCHAIN_PATH})
39+
endif()
40+
endif()
41+
42+
## Add preprocessor definitions for whole project
43+
# add_definitions(-DAAAAA=1)
44+
45+
# Call compile
46+
include(${SDK_PATH}/tools/cmake/compile.cmake)
47+
48+
49+
# Project Name, default the same as project directory name
50+
get_filename_component(parent_dir ${CMAKE_PARENT_LIST_FILE} DIRECTORY)
51+
get_filename_component(project_dir_name ${parent_dir} NAME)
52+
53+
set(PROJECT_NAME ${project_dir_name}) # change this var if don't want the same as directory's
54+
55+
message("-- PROJECT_NAME:${PROJECT_NAME}")
56+
project(${PROJECT_NAME})
57+
58+
59+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# component register priority
2+
# The upper components have higher priority
3+
# comments start with `#`
4+
5+
utils_rust
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Components configuration
2+
CONFIG_UTILS_RUST_ENABLED=y
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
################# Add include #################
2+
# list(APPEND ADD_INCLUDE "inc")
3+
# list(APPEND ADD_PRIVATE_INCLUDE "include_private")
4+
###############################################
5+
6+
############## Add source files ###############
7+
append_srcs_dir(ADD_SRCS "src")
8+
# FILE(GLOB_RECURSE EXTRA_SRC "src/*.c")
9+
# FILE(GLOB EXTRA_SRC "src/*.c")
10+
# list(APPEND ADD_SRCS ${EXTRA_SRC})
11+
# aux_source_directory(src ADD_SRCS) # collect all source file in src dir, will set var ADD_SRCS
12+
# append_srcs_dir(ADD_SRCS "src") # append source file in src dir to var ADD_SRCS
13+
# list(REMOVE_ITEM COMPONENT_SRCS "src/test.c")
14+
# set(ADD_ASM_SRCS "src/asm.S")
15+
# list(APPEND ADD_SRCS ${ADD_ASM_SRCS})
16+
# SET_PROPERTY(SOURCE ${ADD_ASM_SRCS} PROPERTY LANGUAGE C) # set .S ASM file as C language
17+
# SET_SOURCE_FILES_PROPERTIES(${ADD_ASM_SRCS} PROPERTIES COMPILE_FLAGS "-x assembler-with-cpp -D BBBBB")
18+
###############################################
19+
20+
###### Add required/dependent components ######
21+
list(APPEND ADD_REQUIREMENTS utils_rust)
22+
###############################################
23+
24+
###### Add link search path for requirements/libs ######
25+
# list(APPEND ADD_LINK_SEARCH_PATH "${CONFIG_TOOLCHAIN_PATH}/lib")
26+
# list(APPEND ADD_REQUIREMENTS pthread m) # add system libs, pthread and math lib for example here
27+
# set (OpenCV_DIR opencv/lib/cmake/opencv4)
28+
# find_package(OpenCV REQUIRED)
29+
###############################################
30+
31+
############ Add static libs ##################
32+
# list(APPEND ADD_STATIC_LIB "libtest.a")
33+
###############################################
34+
35+
############ Add dynamic libs ##################
36+
# list(APPEND ADD_DYNAMIC_LIB "libtest.so")
37+
###############################################
38+
39+
#### Add compile option for this component ####
40+
#### Just for this component, won't affect other
41+
#### modules, including component that depend
42+
#### on this component
43+
# list(APPEND ADD_DEFINITIONS_PRIVATE -DAAAA=1)
44+
###############################################
45+
46+
#### Add compile option for this component
47+
#### and components denpend on this component
48+
# list(APPEND ADD_DEFINITIONS -DAAAA=1)
49+
###############################################
50+
51+
############ Add static libs ##################
52+
#### Update parent's variables like CMAKE_C_LINK_FLAGS
53+
# set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -Wl,--start-group libmaix/libtest.a -ltest2 -Wl,--end-group" PARENT_SCOPE)
54+
###############################################
55+
56+
# register component, DYNAMIC or SHARED flags will make component compiled to dynamic(shared) lib
57+
register_component()

template/test_rust/main/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

template/test_rust/main/src/main.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <stdint.h>
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
#include <string.h>
5+
6+
#include "global_build_info_time.h"
7+
#include "global_build_info_version.h"
8+
#include "global_config.h"
9+
#include "libutils_rust.h"
10+
11+
int main() {
12+
printf("==============================\n");
13+
printf("version: %d.%d.%d-%s-%d.%d\n",
14+
BUILD_VERSION_MAJOR, BUILD_VERSION_MINOR, BUILD_VERSION_MICRO,
15+
BUILD_GIT_COMMIT_ID, BUILD_VERSION_DEV, BUILD_GIT_IS_DIRTY);
16+
printf("build_time: %04d-%02d-%02d %02d:%02d:%02d\n",
17+
BUILD_TIME_YEAR, BUILD_TIME_MONTH, BUILD_TIME_DAY,
18+
BUILD_TIME_HOUR, BUILD_TIME_MINUTE, BUILD_TIME_SECOND);
19+
printf("week_of_day: %d\n", BUILD_TIME_WEEK_OF_DAY);
20+
printf("year_of_day: %d\n", BUILD_TIME_YEAR_OF_DAY);
21+
printf("==============================\n");
22+
23+
int a = 28;
24+
int b = 44;
25+
26+
printf("hello world!!!\r\n");
27+
printf("%d + %d = %d\n", a, b, add(a, b));
28+
printf("%d - %d = %d\n", a, b, sub(a, b));
29+
30+
return 0;
31+
}

template/test_rust/project.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env python
2+
#-*- coding = utf-8 -*-
3+
4+
#
5+
# @file from https://github.com/Neutree/c_cpp_project_framework
6+
# @author neucrack
7+
# @license Apache 2.0
8+
#
9+
10+
import sys, os
11+
12+
sdk_env_name = "MY_SDK_PATH"
13+
custom_component_path_name = "CUSTOM_COMPONENTS_PATH"
14+
15+
# get SDK absolute path
16+
sdk_path = os.path.abspath(sys.path[0]+"/../../")
17+
try:
18+
if os.environ[sdk_env_name] and os.path.exists(os.environ[sdk_env_name]):
19+
sdk_path = os.environ[sdk_env_name]
20+
except Exception:
21+
pass
22+
print("-- SDK_PATH:{}".format(sdk_path))
23+
24+
# get custom components path
25+
custom_components_path = None
26+
try:
27+
if os.environ[custom_component_path_name] and os.path.exists(os.environ[custom_component_path_name]):
28+
custom_components_path = os.environ[custom_component_path_name]
29+
except Exception:
30+
pass
31+
print("-- CUSTOM_COMPONENTS_PATH:{}".format(custom_components_path))
32+
33+
# execute project script from SDK
34+
project_file_path = sdk_path+"/tools/cmake/project.py"
35+
with open(project_file_path) as f:
36+
exec(f.read())
37+
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
if(CONFIG_UTILS_RUST_ENABLED)
2+
############## Import package #################
3+
include(cargo_build.cmake)
4+
rust_header_gen()
5+
add_crate(adder)
6+
add_crate(suber)
7+
###############################################
8+
9+
################# Add include #################
10+
# list(APPEND ADD_INCLUDE "inc")
11+
# list(APPEND ADD_PRIVATE_INCLUDE "include_private")
12+
###############################################
13+
14+
############## Add source files ###############
15+
# list(APPEND ADD_SRCS "src/lib1.c")
16+
# FILE(GLOB_RECURSE EXTRA_SRC "src/*.c")
17+
# FILE(GLOB EXTRA_SRC "src/*.c")
18+
# list(APPEND ADD_SRCS ${EXTRA_SRC})
19+
# aux_source_directory(src ADD_SRCS) # collect all source file in src dir, will set var ADD_SRCS
20+
# append_srcs_dir(ADD_SRCS "src") # append source file in src dir to var ADD_SRCS
21+
# list(REMOVE_ITEM COMPONENT_SRCS "src/test.c")
22+
# set(ADD_ASM_SRCS "src/asm.S")
23+
# list(APPEND ADD_SRCS ${ADD_ASM_SRCS})
24+
# SET_PROPERTY(SOURCE ${ADD_ASM_SRCS} PROPERTY LANGUAGE C) # set .S ASM file as C language
25+
# SET_SOURCE_FILES_PROPERTIES(${ADD_ASM_SRCS} PROPERTIES COMPILE_FLAGS "-x assembler-with-cpp -D BBBBB")
26+
###############################################
27+
28+
###### Add required/dependent components ######
29+
# list(APPEND ADD_REQUIREMENTS component1)
30+
###############################################
31+
32+
###### Add link search path for requirements/libs ######
33+
# list(APPEND ADD_LINK_SEARCH_PATH "${CONFIG_TOOLCHAIN_PATH}/lib")
34+
# list(APPEND ADD_REQUIREMENTS pthread m) # add system libs, pthread and math lib for example here
35+
# set (OpenCV_DIR opencv/lib/cmake/opencv4)
36+
# find_package(OpenCV REQUIRED)
37+
list(APPEND ADD_REQUIREMENTS adder suber)
38+
###############################################
39+
40+
############ Add static libs ##################
41+
# list(APPEND ADD_STATIC_LIB "lib/libtest.a")
42+
###############################################
43+
44+
############ Add dynamic libs ##################
45+
# list(APPEND ADD_DYNAMIC_LIB "libtest.so")
46+
###############################################
47+
48+
#### Add compile option for this component ####
49+
#### Just for this component, won't affect other
50+
#### modules, including component that depend
51+
#### on this component
52+
# list(APPEND ADD_DEFINITIONS_PRIVATE -DAAAA=1)
53+
###############################################
54+
55+
#### Add compile option for this component
56+
#### and components denpend on this component
57+
# list(APPEND ADD_DEFINITIONS -DAAAA=1)
58+
###############################################
59+
60+
############ Add static libs ##################
61+
#### Update parent's variables like CMAKE_C_LINK_FLAGS
62+
# set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -Wl,--start-group libmaix/libtest.a -ltest2 -Wl,--end-group" PARENT_SCOPE)
63+
###############################################
64+
65+
# register component, DYNAMIC or SHARED flags will make component compiled to dynamic(shared) lib
66+
register_component()
67+
endif()

0 commit comments

Comments
 (0)