-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathbuild_example.py
More file actions
executable file
·198 lines (163 loc) · 6.44 KB
/
build_example.py
File metadata and controls
executable file
·198 lines (163 loc) · 6.44 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/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
from tools.util import (
copy_file, need_settarget,
record_target, set_target,
execute_idf_commands
)
from tools.prepare import delete_temp_files
def clean(root):
delete_temp_files(root)
pass
def parser_para_file(json_file):
if not os.path.isfile(json_file):
print(f"Error: Not found [{json_file}].")
return {}
try:
f = open(json_file, 'r', encoding='utf-8')
json_data = json.load(f)
f.close()
except Exception as e:
print(f"Parser json error: [{str(e)}].")
return {}
return json_data
def set_environment(build_param_path, param_data):
os.environ["BUILD_PARAM_DIR"] = build_param_path
os.environ["TUYAOS_HEADER_DIR"] = param_data["OPEN_HEADER_DIR"]
os.environ["TUYAOS_LIBS_DIR"] = param_data["OPEN_LIBS_DIR"]
os.environ["TUYAOS_LIBS"] = param_data["PLATFORM_NEED_LIBS"]
open_root = param_data["OPEN_ROOT"]
board_path = os.path.join(open_root, "boards", "ESP32")
os.environ["TUYAOS_BOARD_PATH"] = board_path
pass
def set_partitions(root, param_data):
if param_data.get("CONFIG_PLATFORM_FLASHSIZE_16M", False):
flash = "16M"
elif param_data.get("CONFIG_PLATFORM_FLASHSIZE_8M", False):
flash = "8M"
else:
flash = "4M"
print(f"Set flash size {flash}")
tuya_path = os.path.join(root, "tuya_open_sdk")
source = os.path.join(tuya_path, f"partitions_{flash}.csv")
target = os.path.join(tuya_path, "partitions.csv")
copy_file(source, target)
pass
def merge_bin(root, chip, build_path, out_bin):
args_file = os.path.join(build_path, "flasher_args.json")
if not os.path.exists(args_file):
print(f"Error: Not found [{args_file}]")
return False
with open(args_file, 'r') as f:
args_data = json.load(f)
flash_files = args_data['flash_files']
cmd = f"python -m esptool --chip {chip} merge_bin -o {out_bin}"
for addr in flash_files:
bin_path = flash_files[addr]
cmd += f" {addr} {os.path.join(build_path, bin_path)}"
print("Merging bin ...")
if not execute_idf_commands(root, cmd, build_path):
print("Error: Build failed.")
return False
return True
def copy_assets(build_path, output_path, app_name, app_ver):
os.makedirs(output_path, exist_ok=True)
copy_file(os.path.join(build_path, f"{app_name}_QIO_{app_ver}.bin"),
os.path.join(output_path, f"{app_name}_QIO_{app_ver}.bin"))
copy_file(os.path.join(build_path, "bootloader", "bootloader.bin"),
os.path.join(output_path, "bootloader.bin"))
copy_file(os.path.join(build_path, "partition_table",
"partition-table.bin"),
os.path.join(output_path, "partition-table.bin"))
copy_file(os.path.join(build_path, "ota_data_initial.bin"),
os.path.join(output_path, "ota_data_initial.bin"))
copy_file(os.path.join(build_path, "tuya_open_sdk.bin"),
os.path.join(output_path, f"{app_name}.bin"))
copy_file(os.path.join(build_path, "tuya_open_sdk.bin"),
os.path.join(output_path, f"{app_name}_UA_{app_ver}.bin"))
copy_file(os.path.join(build_path, "tuya_open_sdk.bin"),
os.path.join(output_path, f"{app_name}_UG_{app_ver}.bin"))
copy_file(os.path.join(build_path, "tuya_open_sdk.elf"),
os.path.join(output_path, f"{app_name}_{app_ver}.elf"))
copy_file(os.path.join(build_path, "tuya_open_sdk.map"),
os.path.join(output_path, f"{app_name}_{app_ver}.map"))
srmodels_bin = os.path.join(build_path, "srmodels", "srmodels.bin")
if os.path.isfile(srmodels_bin):
copy_file(srmodels_bin,
os.path.join(output_path, "srmodels.bin"))
return True
def _suffix(param_data):
suffix = ""
chip = param_data["PLATFORM_CHIP"]
if "esp32s3" == chip:
# ENABLE_ESP32S3_USB_JTAG_ONLY
use_usb = param_data.get("CONFIG_ENABLE_ESP32S3_USB_JTAG_ONLY", False)
print(f"use_usb: {use_usb}")
if use_usb:
suffix = "_usb_jtag"
else:
suffix = "_uart"
return suffix
def main():
'''
1. 提前配置一些环境变量
1. 如果编译的项目变化,则清理现场并set-target
1. 如果target变化,则清理现场并set-target
1. 配置正确的partitions.csv
1. 调用idf.py build生成固件
1. 打包生成单独固件
1. 拷贝产物到输出路径中
'''
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)
# Set environment variables
set_environment(build_param_path, param_data)
# check app / check target
app_file = os.path.join(root, ".app")
target_file = os.path.join(root, ".target")
app_name = param_data["CONFIG_PROJECT_NAME"]
chip = param_data["PLATFORM_CHIP"]
if need_settarget(app_file, app_name) or need_settarget(target_file, chip):
clean(root)
suffix = _suffix(param_data)
if not set_target(root, chip, suffix):
print("Error: set-target failed.")
sys.exit(1)
record_target(app_file, app_name)
record_target(target_file, chip)
set_partitions(root, param_data)
cmd = "idf.py build"
directory = os.path.join(root, "tuya_open_sdk")
if not execute_idf_commands(root, cmd, directory):
print("Error: Build failed.")
sys.exit(1)
app_version = param_data["CONFIG_PROJECT_VERSION"]
idf_build_path = os.path.join(root, "tuya_open_sdk", "build")
out_bin = os.path.join(idf_build_path, f"{app_name}_QIO_{app_version}.bin")
print(f"Start generate {app_name}_QIO_{app_version}.bin")
if not merge_bin(root, chip, idf_build_path, out_bin):
print("Error: Merge bin.")
sys.exit(1)
output_path = param_data["BIN_OUTPUT_DIR"]
if not copy_assets(idf_build_path, output_path, app_name, app_version):
print("Error: copy assets.")
sys.exit(1)
sys.exit(0)
if __name__ == "__main__":
main()