13
13
from parse_boards import parse_file
14
14
15
15
parser = argparse .ArgumentParser ()
16
- parser .add_argument ("--cli" , "-x" , type = pathlib .Path , required = False , default = shutil .which ("arduino-cli" ), help = "path to arduino-cli" )
16
+ parser .add_argument (
17
+ "--cli" ,
18
+ "-x" ,
19
+ type = pathlib .Path ,
20
+ required = False ,
21
+ default = shutil .which ("arduino-cli" ),
22
+ help = "path to arduino-cli" ,
23
+ )
17
24
parser .add_argument ("--board" , "-b" , type = str , default = "" , help = "board name" )
18
- parser .add_argument ("--fire" , action = "store_true" , default = False , help = "launch the build immediately (use with caution!)" )
25
+ parser .add_argument (
26
+ "--fire" ,
27
+ action = "store_true" ,
28
+ default = False ,
29
+ help = "launch the build immediately (use with caution!)" ,
30
+ )
19
31
output_args = parser .add_mutually_exclusive_group (required = True )
20
- output_args .add_argument ("--output" , "-o" , type = pathlib .Path , help = "output file (CMake) with placeholders" )
21
- output_args .add_argument ("--sketch" , "-s" , type = pathlib .Path , help = "output file (CMake) filled given a sketch folder" )
32
+ output_args .add_argument (
33
+ "--output" , "-o" , type = pathlib .Path , help = "output file (CMake) with placeholders"
34
+ )
35
+ output_args .add_argument (
36
+ "--sketch" ,
37
+ "-s" ,
38
+ type = pathlib .Path ,
39
+ help = "output file (CMake) filled given a sketch folder" ,
40
+ )
22
41
23
42
shargs = parser .parse_args ()
24
43
shargs .board = shargs .board .upper ()
25
44
26
- if shargs .sketch and not shargs .board :
27
- print ("""
45
+ if shargs .sketch and not shargs .board :
46
+ print (
47
+ """
28
48
Warning: you did not specify which board you were targeting;
29
49
please review the generated CMakeLists.txt to remove the placeholder value before calling `cmake`.
30
- """ )
50
+ """
51
+ )
31
52
32
- if shargs .cli is None :
33
- print ("""
53
+ if shargs .cli is None :
54
+ print (
55
+ """
34
56
Error: `arduino-cli` not found in $PATH.
35
57
Please install arduino-cli and make it available from your system $PATH,
36
58
or give its location through the `--cli` argument.
37
- """ )
59
+ """
60
+ )
38
61
exit (- 1 )
39
62
40
- def arduino (* args ) :
63
+
64
+ def arduino (* args ):
41
65
# return (out.stdout, logfile)
42
66
# raises an exception if the command fails
43
67
handle , logfile = tempfile .mkstemp ()
@@ -50,47 +74,50 @@ def arduino(*args) :
50
74
).stdout
51
75
return (out , logfile )
52
76
53
- def get_log (fname ) :
54
- with open (fname ) as file :
55
- for line in file :
77
+
78
+ def get_log (fname ):
79
+ with open (fname ) as file :
80
+ for line in file :
56
81
yield json .loads (line )
57
82
58
- def get_boards (boardstxt ) :
83
+
84
+ def get_boards (boardstxt ):
59
85
60
86
# we "reject" everything because we don't care about the values, only the keys
61
- families = parse_file (boardstxt , lambda x :True )
87
+ families = parse_file (boardstxt , lambda x : True )
62
88
del families ["menu" ]
63
89
64
90
boards = set ()
65
- for fam , famcfg in families .items () :
91
+ for fam , famcfg in families .items ():
66
92
boards .update (famcfg .menu .pnum .keys ())
67
93
68
94
return boards
69
95
96
+
70
97
_ , logf = arduino ("lib" , "list" )
71
98
72
- allboards = get_boards (pathlib .Path (__file__ ).parent .parent .parent / "boards.txt" )
99
+ allboards = get_boards (pathlib .Path (__file__ ).parent .parent .parent / "boards.txt" )
73
100
74
101
75
- if shargs .board and shargs .board not in allboards :
102
+ if shargs .board and shargs .board not in allboards :
76
103
print (f"Unrecognized board name: '{ shargs .board } '" )
77
104
print ("Possible matches:" )
78
105
options = difflib .get_close_matches (shargs .board , allboards , n = 9 , cutoff = 0.0 )
79
106
print ("0. (keep as-is)" )
80
- for i , x in enumerate (options , start = 1 ) :
107
+ for i , x in enumerate (options , start = 1 ):
81
108
print (f"{ i } . { x } " )
82
109
choice = input ("Choice number: " )
83
- while not choice .isdecimal () :
110
+ while not choice .isdecimal ():
84
111
choice = input ("Invalid choice *number*. Select a board: " )
85
112
choice = int (choice )
86
- if choice != 0 :
113
+ if choice != 0 :
87
114
choice -= 1
88
115
shargs .board = options [choice ]
89
116
90
117
91
118
libpaths = dict ()
92
- for line in get_log (logf ) :
93
- if line ["msg" ] == "Adding libraries dir" :
119
+ for line in get_log (logf ):
120
+ if line ["msg" ] == "Adding libraries dir" :
94
121
libpaths [line ["location" ]] = pathlib .Path (line ["dir" ])
95
122
96
123
# platform lib path is already known, obviously, since that's where this script resides
@@ -99,18 +126,20 @@ def get_boards(boardstxt) :
99
126
corepath = pathlib .Path (__file__ ).parent .parent .parent .resolve ()
100
127
101
128
j2_env = Environment (
102
- loader = FileSystemLoader (str (corepath / "cmake" / "templates" )), trim_blocks = True , lstrip_blocks = True
129
+ loader = FileSystemLoader (str (corepath / "cmake" / "templates" )),
130
+ trim_blocks = True ,
131
+ lstrip_blocks = True ,
103
132
)
104
133
cmake_template = j2_env .get_template ("easy_cmake.cmake" )
105
134
106
135
107
136
userhome = pathlib .Path .home ()
108
- if userlibs .is_relative_to (userhome ) :
137
+ if userlibs .is_relative_to (userhome ):
109
138
userlibs = "~/" + str (userlibs .relative_to (userhome ))
110
- if corepath .is_relative_to (userhome ) :
139
+ if corepath .is_relative_to (userhome ):
111
140
corepath = "~/" + str (corepath .relative_to (userhome ))
112
141
113
- if shargs .sketch :
142
+ if shargs .sketch :
114
143
SOURCEFILE_EXTS = (".c" , ".cpp" , ".S" , ".ino" )
115
144
sources = {
116
145
file .relative_to (shargs .sketch )
@@ -123,37 +152,45 @@ def get_boards(boardstxt) :
123
152
if file .is_file () and file .suffix in SOURCEFILE_EXTS
124
153
}
125
154
tgtname = shargs .sketch .resolve ().name
126
- else :
155
+ else :
127
156
tgtname = ""
128
157
sources = set ()
129
158
130
159
scriptname = pathlib .Path (__file__ )
131
- if scriptname .is_relative_to (userhome ) :
160
+ if scriptname .is_relative_to (userhome ):
132
161
scriptname = "~/" + str (scriptname .relative_to (userhome ))
133
162
134
- with open (shargs .output or shargs .sketch / "CMakeLists.txt" , "w" ) as out :
135
- out .write (cmake_template .render (
136
- corepath = str (corepath ).replace ("\\ " , "\\ \\ " ), # escape backslashes for CMake
137
- userlibs = str (userlibs ).replace ("\\ " , "\\ \\ " ),
138
- libs = libs ,
139
- scriptfile = scriptname ,
140
- tgtname = tgtname ,
141
- scrcfiles = sources ,
142
- boardname = shargs .board ,
143
- ))
144
-
145
-
146
- print ("Generated" , shargs .output or shargs .sketch / "CMakeLists.txt" )
147
- print ("""
163
+ with open (shargs .output or shargs .sketch / "CMakeLists.txt" , "w" ) as out :
164
+ out .write (
165
+ cmake_template .render (
166
+ corepath = str (corepath ).replace (
167
+ "\\ " , "\\ \\ "
168
+ ), # escape backslashes for CMake
169
+ userlibs = str (userlibs ).replace ("\\ " , "\\ \\ " ),
170
+ libs = libs ,
171
+ scriptfile = scriptname ,
172
+ tgtname = tgtname ,
173
+ scrcfiles = sources ,
174
+ boardname = shargs .board ,
175
+ )
176
+ )
177
+
178
+
179
+ print ("Generated" , shargs .output or shargs .sketch / "CMakeLists.txt" )
180
+ print (
181
+ """
148
182
Unless you are building a very simple sketch with no library (e.g., Blink),
149
183
you are advised to edit this file to fill in any missing dependency relationship.
150
184
For details, please refer to
151
185
https://github.com/massonal/Arduino_Core_STM32/wiki/Arduino-(in)compatibility#library-management
152
- """ )
186
+ """
187
+ )
153
188
154
- if shargs .fire :
155
- if not (shargs .sketch and shargs .board ) :
156
- print ("There remains some placeholder in the output file; I won't build _that_." )
189
+ if shargs .fire :
190
+ if not (shargs .sketch and shargs .board ):
191
+ print (
192
+ "There remains some placeholder in the output file; I won't build _that_."
193
+ )
157
194
exit (1 )
158
- subprocess .run (("cmake" , "-S" , shargs .sketch , "-B" , shargs .sketch / "build" ))
159
- subprocess .run (("cmake" , "--build" , shargs .sketch / "build" ))
195
+ subprocess .run (("cmake" , "-S" , shargs .sketch , "-B" , shargs .sketch / "build" ))
196
+ subprocess .run (("cmake" , "--build" , shargs .sketch / "build" ))
0 commit comments