1
+ # This file is expected to be present in ${COMPONENT_DIR}
2
+ # accessed from components/esp_insights/CMakeLists.txt
3
+ # Used in:
4
+ # 1. Project ESP Insights build package tar file
5
+
6
+ #from __future__ import unicode_literals
7
+ import os
8
+ import sys
9
+ import json
10
+ import subprocess
11
+ from builtins import range , str
12
+ from io import open
13
+
14
+ # Input project directory from CMakeLists.txt
15
+ PROJ_DIR = sys .argv [1 ]
16
+ # Input project name
17
+ PROJ_NAME = sys .argv [2 ]
18
+ # Input project version
19
+ PROJ_VER = sys .argv [3 ]
20
+ # Input custom config filename from CMakeLists.txt
21
+ FILENAME = sys .argv [4 ]
22
+ # Input IDF_PATH from CMakeLists.txt
23
+ IDF_PATH = sys .argv [5 ]
24
+ # Input target
25
+ TARGET = sys .argv [6 ]
26
+
27
+ NEWLINE = "\n "
28
+
29
+ CONFIG = {}
30
+
31
+ # Set Config
32
+
33
+ # Set current directory i.e Set ${COMPONENT_DIR} as current directory
34
+ CURR_DIR = os .getcwd ()
35
+
36
+ def _change_dir (dirname ):
37
+ # Change directory
38
+ os .chdir (dirname )
39
+
40
+
41
+ def _set_submodule_cfg (submodules , repo_name ):
42
+ # Set config for submodules
43
+ CFG_TITLE = "submodules"
44
+ NAME_STR = "name"
45
+ VERSION_STR = "version"
46
+ CONFIG [repo_name ][CFG_TITLE ] = []
47
+
48
+ if submodules :
49
+ # Get the submodule name and version
50
+ submodules_list = submodules .strip ().split (NEWLINE )
51
+ for i in range (0 , len (submodules_list ), 2 ):
52
+ name = submodules_list [i ].split ('\' ' )[1 ]
53
+ version = submodules_list [i + 1 ]
54
+ submodule_json = { NAME_STR : name , VERSION_STR : version }
55
+ CONFIG [repo_name ][CFG_TITLE ].append (submodule_json )
56
+
57
+
58
+ def run_cmd (command , get_basename = False ):
59
+ try :
60
+ resp = subprocess .check_output (command , shell = True ).strip ().decode ('utf-8' )
61
+ if get_basename :
62
+ resp = os .path .basename (resp )
63
+ return resp
64
+ except subprocess .CalledProcessError :
65
+ raise Exception ("ERROR: Please check command : {}" .format (command ))
66
+
67
+ def set_cfg (config_name ):
68
+ # Set config for ESP-IDF Repo
69
+ if config_name == "esp-idf" :
70
+ # Get repo name (for IDF repo)
71
+ REPO_CMD = 'git rev-parse --show-toplevel'
72
+ repo_name = run_cmd (REPO_CMD , get_basename = True )
73
+ CONFIG [repo_name ] = {}
74
+
75
+ # Get commit HEAD
76
+ GITHEAD_STR = "HEAD"
77
+ HEAD = 'git describe --always --tags --dirty'
78
+ head_ver = run_cmd (HEAD )
79
+ CONFIG [repo_name ][GITHEAD_STR ] = head_ver
80
+
81
+ # Get submodule latest refs
82
+ SUBMODULE = 'git submodule foreach git describe --always --tags --dirty'
83
+ submodules = run_cmd (SUBMODULE )
84
+ _set_submodule_cfg (submodules , repo_name )
85
+ elif config_name == "toolchain" :
86
+ # Set config for Toolchain Version
87
+ arch_target = "xtensa-" + TARGET
88
+ if TARGET == "esp32c3" or TARGET == "esp32c2" or TARGET == "esp32h2" or TARGET == "esp32c6" :
89
+ arch_target = "riscv32-esp"
90
+ # Get toolchain version
91
+ TOOLCHAIN_STR = "toolchain"
92
+ TOOLCHAIN = arch_target + '-elf-gcc --version'
93
+ toolchain = run_cmd (TOOLCHAIN )
94
+ CONFIG [TOOLCHAIN_STR ] = toolchain .strip ().split (NEWLINE )[0 ]
95
+
96
+ # Set project details - name and version
97
+ def set_project_details ():
98
+ # Set project name and version
99
+ CONFIG ['project' ] = {}
100
+ CONFIG ['project' ]['name' ] = PROJ_NAME
101
+ CONFIG ['project' ]['version' ] = PROJ_VER
102
+
103
+ try :
104
+ with open (FILENAME , "w+" , encoding = "utf-8" ) as output_file :
105
+ # ESP-IDF REPO CONFIG
106
+ # Change to ESP-IDF Directory
107
+ _change_dir (IDF_PATH )
108
+ set_cfg ("esp-idf" )
109
+
110
+ # Change back to ${COMPONENT_DIR}
111
+ _change_dir (CURR_DIR )
112
+
113
+ # Set project name and version
114
+ set_project_details ()
115
+
116
+ # GET TOOLCHAIN VERSION
117
+ set_cfg ("toolchain" )
118
+
119
+ output_file .write (str (json .dumps (CONFIG , indent = 4 , sort_keys = True )))
120
+
121
+ except Exception as e :
122
+ # Remove config file created if error occurs
123
+ os .system ("rm " + FILENAME )
124
+ sys .exit (e )
0 commit comments