-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbuild_example.py
More file actions
executable file
·127 lines (100 loc) · 4.04 KB
/
build_example.py
File metadata and controls
executable file
·127 lines (100 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env python3
# coding=utf-8
# 参数说明:
# $1 - params path: $1/build_param.[cmake/config/json]
# $2 - user cmd: build/clean/...
import os
import sys
import json
import subprocess
import shutil
def clean(root):
shutil.rmtree("build", ignore_errors=True)
pass
def parser_para_file(json_file):
if not os.path.isfile(json_file):
print(f"Error: File not found [{json_file}].")
return {}
try:
with open(json_file, 'r', encoding='utf-8') as f:
json_data = json.load(f)
except Exception as e:
print(f"JSON parsing error: [{str(e)}].")
return {}
return json_data
def check_dependency_changes(build_param_path, build_dir="build"):
"""
检查应用库文件是否比当前可执行文件更新
如果是,则需要强制重新构建以避免缓存问题
"""
try:
build_param_file = os.path.join(build_param_path, "build_param.json")
param_data = parser_para_file(build_param_file)
if not param_data:
return False
project_name = param_data.get('CONFIG_PROJECT_NAME', '')
libs_dir = param_data.get('OPEN_LIBS_DIR', '')
if not project_name or not libs_dir:
return False
# 检查关键库文件
key_libs = ['libtuyaapp.a', 'libtuyaos.a']
current_exe = os.path.join(build_dir, project_name)
if not os.path.exists(current_exe):
return True # 可执行文件不存在,需要构建
exe_mtime = os.path.getmtime(current_exe)
for lib_name in key_libs:
lib_path = os.path.join(libs_dir, lib_name)
if os.path.exists(lib_path):
lib_mtime = os.path.getmtime(lib_path)
if lib_mtime > exe_mtime:
print(f"Detected library file {lib_name} has been updated, rebuilding to avoid cache issues")
return True
return False
except Exception as e:
print(f"Error occurred during dependency check: {e}")
return True # Rebuild on error
def main():
if len(sys.argv) < 2:
print(f"Error: At least 2 parameters are needed {sys.argv}.")
build_param_path = sys.argv[1]
user_cmd = sys.argv[2]
root = os.path.dirname(os.path.abspath(__file__))
if "clean" == user_cmd:
clean(root)
sys.exit(0)
build_param_file = os.path.join(build_param_path, "build_param.json")
param_data = parser_para_file(build_param_file)
if not len(param_data):
sys.exit(1)
if not os.path.isfile("CMakeLists.txt"):
print("Error: CMakeLists.txt not found in the current directory.")
sys.exit(1)
# Check dependency changes, clean build directory if needed
if check_dependency_changes(build_param_path):
print("Detected application library updates, cleaning platform build cache...")
clean(root)
try:
os.makedirs("build", exist_ok=True)
cmake_command = [
"cmake", "-S", ".", "-B", "build", "-G", "Ninja",
f"-DBUILD_PARAM_PATH={build_param_path}"
]
subprocess.run(
cmake_command,
check=True,
)
subprocess.run(["cmake", "--build", "build"], check=True)
except subprocess.CalledProcessError as e:
print(f"CMake build process failed: {e}")
sys.exit(1)
source_file = os.path.join(os.getcwd(), "build", param_data['CONFIG_PROJECT_NAME'])
dst_dir = param_data["BIN_OUTPUT_DIR"]
if not os.path.exists(dst_dir):
os.makedirs(dst_dir, exist_ok=True)
destination_path = os.path.join(dst_dir, param_data['CONFIG_PROJECT_NAME'] + "_QIO_" + param_data['CONFIG_PROJECT_VERSION'] + ".bin")
shutil.copy(source_file, destination_path)
destination_path = os.path.join(dst_dir, param_data['CONFIG_PROJECT_NAME'] + "_" + param_data['CONFIG_PROJECT_VERSION'] + ".elf")
shutil.copy(source_file, destination_path)
sys.exit(0)
if __name__ == "__main__":
main()