18
18
# along with this program. If not, see <https://www.gnu.org/licenses/>.
19
19
20
20
import argparse
21
- import os
21
+ import pathlib
22
22
import subprocess
23
+ import sys
23
24
25
+ from typing import Optional , TextIO
24
26
25
- def generate (path , platform_path , version = "unspecified" , release = False ):
26
- def git (* args ):
27
- cmd = ["git" , "-C" , platform_path ]
28
- cmd .extend (args )
29
- proc = subprocess .Popen (cmd , stdout = subprocess .PIPE , universal_newlines = True , stderr = subprocess .DEVNULL )
30
- return proc .stdout .readlines ()[0 ].strip ()
31
27
32
- text = ""
28
+ PWD = pathlib .Path (__file__ ).parent .absolute ()
29
+
30
+ VERSION_UNSPECIFIED = "unspecified"
31
+ VERSION_DEFAULT = ("0" , "0" , "0" )
32
+
33
+
34
+ def escape_version (v : str ) -> str :
35
+ return v .replace ("-" , "_" ).replace ("." , "_" )
36
+
37
+
38
+ def check_git (* args : str , cwd : Optional [str ]):
39
+ cmd = ["git" ]
40
+ if cwd :
41
+ cmd .extend (["-C" , cwd ])
42
+ cmd .extend (args )
43
+
44
+ with subprocess .Popen (
45
+ cmd ,
46
+ stdout = subprocess .PIPE ,
47
+ universal_newlines = True ,
48
+ stderr = subprocess .DEVNULL ,
49
+ ) as proc :
50
+ if proc .stdout :
51
+ return proc .stdout .readlines ()[0 ].strip ()
52
+
53
+ return ""
54
+
33
55
34
- git_ver = "00000000"
56
+ def generate (
57
+ out : TextIO ,
58
+ * ,
59
+ git_root : pathlib .Path ,
60
+ hash_length : int = 8 ,
61
+ release : bool ,
62
+ version : str ,
63
+ ):
64
+ git_root = git_root .absolute ()
65
+ git_cwd = git_root .as_posix ()
66
+
67
+ def git (* args ):
68
+ return check_git (* args , cwd = git_cwd )
69
+
70
+ git_ver = "0" * hash_length
35
71
try :
36
- git_ver = git ("rev-parse" , "--short=8 " , "HEAD" )
72
+ git_ver = git ("rev-parse" , f "--short={ hash_length } " , "HEAD" )
37
73
except Exception :
38
- pass
39
- text = "#define ARDUINO_ESP8266_GIT_VER 0x{}\n " .format (git_ver )
74
+ raise
40
75
41
76
# version is
42
77
# - using Arduino-CLI:
@@ -50,70 +85,101 @@ def git(*args):
50
85
# in any case, get a better version when git is around
51
86
git_desc = git ("describe" , "--tags" )
52
87
except Exception :
88
+ raise
89
+
90
+ if version == VERSION_UNSPECIFIED :
91
+ version = git_desc
92
+
93
+ version_triple = list (VERSION_DEFAULT )
94
+
95
+ try :
96
+ version_triple = version .split ("." , 2 )
97
+ except ValueError :
53
98
pass
54
99
55
- text += "#define ARDUINO_ESP8266_GIT_DESC {}\n " .format (git_desc )
56
- text += "#define ARDUINO_ESP8266_VERSION {}\n " .format (version )
57
- text += "\n "
100
+ major , minor , patch = version_triple
58
101
59
- version_split = version .split ("." )
60
- # major: if present, skip "unix-" in "unix-3"
61
- text += "#define ARDUINO_ESP8266_MAJOR {}\n " .format (version_split [0 ].split ("-" )[- 1 ])
62
- text += "#define ARDUINO_ESP8266_MINOR {}\n " .format (version_split [1 ])
63
- # revision can be ".n" or ".n-dev" or ".n-42-g00d1e5"
64
- revision = version_split [2 ].split ("-" )
65
- text += "#define ARDUINO_ESP8266_REVISION {}\n " .format (revision [0 ])
66
- text += "\n "
102
+ major = major .split ("-" )[- 1 ]
103
+ revision = patch .split ("-" )[0 ]
67
104
68
- # release or dev ?
105
+ text = rf"""// ! ! ! DO NOT EDIT, AUTOMATICALLY GENERATED ! ! !
106
+ #define ARDUINO_ESP8266_GIT_VER 0x{ git_ver }
107
+ #define ARDUINO_ESP8266_GIT_DESC { git_desc }
108
+ #define ARDUINO_ESP8266_VERSION { version }
109
+
110
+ #define ARDUINO_ESP8266_MAJOR { major }
111
+ #define ARDUINO_ESP8266_MINOR { minor }
112
+ #define ARDUINO_ESP8266_REVISION { revision }
113
+ """
69
114
if release :
70
- text += "#define ARDUINO_ESP8266_RELEASE \" {}\" \n " .format (git_desc )
71
- text += "#define ARDUINO_ESP8266_RELEASE_{}\n " .format (git_desc .replace ("-" ,"_" ).replace ("." ,"_" ))
115
+ if version != VERSION_UNSPECIFIED :
116
+ release_version = version
117
+ else :
118
+ release_version = git_desc
119
+
120
+ text += rf"""
121
+ #define ARDUINO_ESP8266_RELEASE \"{ release_version } \"
122
+ #define ARDUINO_ESP8266_RELEASE_{ escape_version (release_version )}
123
+ """
72
124
else :
73
- text += "#define ARDUINO_ESP8266_DEV 1 // development version\n "
74
-
75
- try :
76
- with open (path , "r" ) as inp :
77
- old_text = inp .read ()
78
- if old_text == text :
79
- return
80
- except Exception :
81
- pass
125
+ text += """
126
+ #define ARDUINO_ESP8266_DEV 1 // development version
127
+ """
82
128
83
- with open (path , "w" ) as out :
84
- out .write (text )
129
+ out .write (text )
85
130
86
131
87
132
if __name__ == "__main__" :
88
133
parser = argparse .ArgumentParser (description = "Generate core_version.h" )
89
134
90
135
parser .add_argument (
91
- "-b" , "--build_path" , action = "store" , required = True , help = "build.path variable"
136
+ "--git-root" ,
137
+ action = "store" ,
138
+ help = "ESP8266 Core Git root. In platform.txt, this is {platform.path}" ,
139
+ type = pathlib .Path ,
140
+ default = PWD / ".." ,
141
+ )
142
+ parser .add_argument (
143
+ "--hash-length" ,
144
+ default = 8 ,
145
+ type = int ,
146
+ help = "Used in git rev-parse --short=..." ,
92
147
)
93
148
parser .add_argument (
94
- "-p" ,
95
- "--platform_path" ,
149
+ "--version" ,
96
150
action = "store" ,
97
- required = True ,
98
- help = "platform.path variable " ,
151
+ default = VERSION_UNSPECIFIED ,
152
+ help = "ESP8266 Core version string. In platform.txt, this is {version} " ,
99
153
)
100
154
parser .add_argument (
101
- "-v" , "--version" , action = "store" , required = True , help = "version variable"
155
+ "--release" ,
156
+ action = "store_true" ,
157
+ default = False ,
158
+ help = "In addition to numeric version, also provide ARDUINO_ESP8266_RELEASE{,_...} definitions" ,
159
+ )
160
+ parser .add_argument (
161
+ "output" ,
162
+ nargs = "?" ,
163
+ type = str ,
164
+ default = "" ,
102
165
)
103
- parser .add_argument ("-i" , "--include_dir" , default = "core" )
104
- parser .add_argument ("-r" , "--release" , action = "store_true" , default = False )
105
166
106
167
args = parser .parse_args ()
107
168
108
- include_dir = os .path .join (args .build_path , args .include_dir )
109
- try :
110
- os .makedirs (include_dir )
111
- except Exception :
112
- pass
169
+ def select_output (s : str ) -> TextIO :
170
+ if not s :
171
+ return sys .stdout
113
172
114
- generate (
115
- os .path .join (include_dir , "core_version.h" ),
116
- args .platform_path ,
117
- version = args .version ,
118
- release = args .release
119
- )
173
+ out = pathlib .Path (s )
174
+ out .parent .mkdir (parents = True , exist_ok = True )
175
+
176
+ return out .open ("r" , encoding = "utf-8" )
177
+
178
+ with select_output (args .output ) as out :
179
+ generate (
180
+ out ,
181
+ git_root = args .git_root ,
182
+ hash_length = args .hash_length ,
183
+ release = args .release ,
184
+ version = args .version ,
185
+ )
0 commit comments