1313from parse_boards import parse_file
1414
1515parser = 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+ )
1724parser .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+ )
1931output_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+ )
2241
2342shargs = parser .parse_args ()
2443shargs .board = shargs .board .upper ()
2544
26- if shargs .sketch and not shargs .board :
27- print ("""
45+ if shargs .sketch and not shargs .board :
46+ print (
47+ """
2848 Warning: you did not specify which board you were targeting;
2949 please review the generated CMakeLists.txt to remove the placeholder value before calling `cmake`.
30- """ )
50+ """
51+ )
3152
32- if shargs .cli is None :
33- print ("""
53+ if shargs .cli is None :
54+ print (
55+ """
3456 Error: `arduino-cli` not found in $PATH.
3557 Please install arduino-cli and make it available from your system $PATH,
3658 or give its location through the `--cli` argument.
37- """ )
59+ """
60+ )
3861 exit (- 1 )
3962
40- def arduino (* args ) :
63+
64+ def arduino (* args ):
4165 # return (out.stdout, logfile)
4266 # raises an exception if the command fails
4367 handle , logfile = tempfile .mkstemp ()
@@ -50,47 +74,50 @@ def arduino(*args) :
5074 ).stdout
5175 return (out , logfile )
5276
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 :
5681 yield json .loads (line )
5782
58- def get_boards (boardstxt ) :
83+
84+ def get_boards (boardstxt ):
5985
6086 # 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 )
6288 del families ["menu" ]
6389
6490 boards = set ()
65- for fam , famcfg in families .items () :
91+ for fam , famcfg in families .items ():
6692 boards .update (famcfg .menu .pnum .keys ())
6793
6894 return boards
6995
96+
7097_ , logf = arduino ("lib" , "list" )
7198
72- allboards = get_boards (pathlib .Path (__file__ ).parent .parent .parent / "boards.txt" )
99+ allboards = get_boards (pathlib .Path (__file__ ).parent .parent .parent / "boards.txt" )
73100
74101
75- if shargs .board and shargs .board not in allboards :
102+ if shargs .board and shargs .board not in allboards :
76103 print (f"Unrecognized board name: '{ shargs .board } '" )
77104 print ("Possible matches:" )
78105 options = difflib .get_close_matches (shargs .board , allboards , n = 9 , cutoff = 0.0 )
79106 print ("0. (keep as-is)" )
80- for i , x in enumerate (options , start = 1 ) :
107+ for i , x in enumerate (options , start = 1 ):
81108 print (f"{ i } . { x } " )
82109 choice = input ("Choice number: " )
83- while not choice .isdecimal () :
110+ while not choice .isdecimal ():
84111 choice = input ("Invalid choice *number*. Select a board: " )
85112 choice = int (choice )
86- if choice != 0 :
113+ if choice != 0 :
87114 choice -= 1
88115 shargs .board = options [choice ]
89116
90117
91118libpaths = 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" :
94121 libpaths [line ["location" ]] = pathlib .Path (line ["dir" ])
95122
96123# platform lib path is already known, obviously, since that's where this script resides
@@ -99,18 +126,20 @@ def get_boards(boardstxt) :
99126corepath = pathlib .Path (__file__ ).parent .parent .parent .resolve ()
100127
101128j2_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 ,
103132)
104133cmake_template = j2_env .get_template ("easy_cmake.cmake" )
105134
106135
107136userhome = pathlib .Path .home ()
108- if userlibs .is_relative_to (userhome ) :
137+ if userlibs .is_relative_to (userhome ):
109138 userlibs = "~/" + str (userlibs .relative_to (userhome ))
110- if corepath .is_relative_to (userhome ) :
139+ if corepath .is_relative_to (userhome ):
111140 corepath = "~/" + str (corepath .relative_to (userhome ))
112141
113- if shargs .sketch :
142+ if shargs .sketch :
114143 SOURCEFILE_EXTS = (".c" , ".cpp" , ".S" , ".ino" )
115144 sources = {
116145 file .relative_to (shargs .sketch )
@@ -123,37 +152,45 @@ def get_boards(boardstxt) :
123152 if file .is_file () and file .suffix in SOURCEFILE_EXTS
124153 }
125154 tgtname = shargs .sketch .resolve ().name
126- else :
155+ else :
127156 tgtname = ""
128157 sources = set ()
129158
130159scriptname = pathlib .Path (__file__ )
131- if scriptname .is_relative_to (userhome ) :
160+ if scriptname .is_relative_to (userhome ):
132161 scriptname = "~/" + str (scriptname .relative_to (userhome ))
133162
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+ """
148182Unless you are building a very simple sketch with no library (e.g., Blink),
149183you are advised to edit this file to fill in any missing dependency relationship.
150184For details, please refer to
151185https://github.com/massonal/Arduino_Core_STM32/wiki/Arduino-(in)compatibility#library-management
152- """ )
186+ """
187+ )
153188
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+ )
157194 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