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