187
187
import glob
188
188
import os
189
189
import platform
190
+ import traceback
190
191
import sys
191
192
import textwrap
192
193
import time
193
194
195
+ import locale
196
+
194
197
# Need to work on signature line used for match to avoid conflicts with
195
198
# existing embedded documentation methods.
196
199
build_opt_signature = "/*@create-file:build.opt@"
201
204
err_print_flag = False
202
205
msg_print_buf = ""
203
206
debug_enabled = False
207
+ default_encoding = None
204
208
205
209
# Issues trying to address through buffered printing
206
210
# 1. Arduino IDE 2.0 RC5 does not show stderr text in color. Text printed does
@@ -295,16 +299,16 @@ def copy_create_build_file(source_fqfn, build_target_fqfn):
295
299
pass
296
300
return True # file changed
297
301
298
-
299
302
def add_include_line (build_opt_fqfn , include_fqfn ):
303
+ global default_encoding
300
304
if not os .path .exists (include_fqfn ):
301
305
# If file is missing, we need an place holder
302
- with open (include_fqfn , 'w' , encoding = "utf-8" ):
306
+ with open (include_fqfn , 'w' , encoding = default_encoding ):
303
307
pass
304
- print ("add_include_line: Created " + include_fqfn )
305
- with open (build_opt_fqfn , 'a' , encoding = "utf-8" ) as build_opt :
306
- build_opt .write ('-include "' + include_fqfn .replace ('\\ ' , '\\ \\ ' ) + '"\n ' )
308
+ print_msg ("add_include_line: Created " + include_fqfn )
307
309
310
+ with open (build_opt_fqfn , 'a' , encoding = default_encoding ) as build_opt :
311
+ build_opt .write ('-include "' + include_fqfn .replace ('\\ ' , '\\ \\ ' ) + '"\n ' )
308
312
309
313
def extract_create_build_opt_file (globals_h_fqfn , file_name , build_opt_fqfn ):
310
314
"""
@@ -313,8 +317,9 @@ def extract_create_build_opt_file(globals_h_fqfn, file_name, build_opt_fqfn):
313
317
copy of Sketch.ino.globals.h.
314
318
"""
315
319
global build_opt_signature
320
+ global default_encoding
316
321
317
- build_opt = open (build_opt_fqfn , 'w' , encoding = "utf-8" )
322
+ build_opt = open (build_opt_fqfn , 'w' , encoding = default_encoding )
318
323
if not os .path .exists (globals_h_fqfn ) or (0 == os .path .getsize (globals_h_fqfn )):
319
324
build_opt .close ()
320
325
return False
@@ -605,12 +610,63 @@ def parse_args():
605
610
# ref nargs='*'', https://stackoverflow.com/a/4480202
606
611
# ref no '--n' parameter, https://stackoverflow.com/a/21998252
607
612
613
+
614
+ # retrieve *system* encoding, not the one used by python internally
615
+ if sys .version_info >= (3 , 11 ):
616
+ def get_encoding ():
617
+ return locale .getencoding ()
618
+ else :
619
+ def get_encoding ():
620
+ return locale .getdefaultlocale ()[1 ]
621
+
622
+
623
+ def show_value (desc , value ):
624
+ print_dbg (f'{ desc :<40} { value } ' )
625
+ return
626
+
627
+ def locale_dbg ():
628
+ show_value ("get_encoding()" , get_encoding ())
629
+ show_value ("locale.getdefaultlocale()" , locale .getdefaultlocale ())
630
+ show_value ('sys.getfilesystemencoding()' , sys .getfilesystemencoding ())
631
+ show_value ("sys.getdefaultencoding()" , sys .getdefaultencoding ())
632
+ show_value ("locale.getpreferredencoding(False)" , locale .getpreferredencoding (False ))
633
+ try :
634
+ show_value ("locale.getpreferredencoding()" , locale .getpreferredencoding ())
635
+ except :
636
+ pass
637
+ show_value ("sys.stdout.encoding" , sys .stdout .encoding )
638
+
639
+ # use current setting
640
+ show_value ("locale.setlocale(locale.LC_ALL, None)" , locale .setlocale (locale .LC_ALL , None ))
641
+ try :
642
+ show_value ("locale.getencoding()" , locale .getencoding ())
643
+ except :
644
+ pass
645
+ show_value ("locale.getlocale()" , locale .getlocale ())
646
+
647
+ # use user setting
648
+ show_value ("locale.setlocale(locale.LC_ALL, '')" , locale .setlocale (locale .LC_ALL , '' ))
649
+ # show_value("locale.getencoding()", locale.getencoding())
650
+ show_value ("locale.getlocale()" , locale .getlocale ())
651
+ return
652
+
653
+
608
654
def main ():
609
655
global build_opt_signature
610
656
global docs_url
611
657
global debug_enabled
658
+ global default_encoding
612
659
num_include_lines = 1
613
660
661
+ # Given that GCC will handle lines from an @file as if they were on
662
+ # the command line. I assume that the contents of @file need to be encoded
663
+ # to match that of the shell running GCC runs. I am not 100% sure this API
664
+ # gives me that, but it appears to work.
665
+ #
666
+ # However, elsewhere when dealing with source code we continue to use 'utf-8',
667
+ # ref. https://gcc.gnu.org/onlinedocs/cpp/Character-sets.html
668
+ default_encoding = get_encoding ()
669
+
614
670
args = parse_args ()
615
671
debug_enabled = args .debug
616
672
runtime_ide_path = os .path .normpath (args .runtime_ide_path )
@@ -623,6 +679,13 @@ def main():
623
679
build_path_core , build_opt_name = os .path .split (build_opt_fqfn )
624
680
globals_h_fqfn = os .path .join (build_path_core , globals_name )
625
681
682
+ if debug_enabled :
683
+ locale_dbg ()
684
+
685
+ default_locale = locale .getdefaultlocale ()
686
+ print_msg (f'default locale: { default_locale } ' )
687
+ print_msg (f'default_encoding: { default_encoding } ' )
688
+
626
689
print_dbg (f"runtime_ide_path: { runtime_ide_path } " )
627
690
print_dbg (f"runtime_ide_version: { args .runtime_ide_version } " )
628
691
print_dbg (f"build_path: { build_path } " )
@@ -655,6 +718,10 @@ def main():
655
718
print_dbg (f"first_time: { first_time } " )
656
719
print_dbg (f"use_aggressive_caching_workaround: { use_aggressive_caching_workaround } " )
657
720
721
+ if not os .path .exists (build_path_core ):
722
+ os .makedirs (build_path_core )
723
+ print_msg ("Clean build, created dir " + build_path_core )
724
+
658
725
if first_time or \
659
726
not use_aggressive_caching_workaround or \
660
727
not os .path .exists (commonhfile_fqfn ):
@@ -667,10 +734,6 @@ def main():
667
734
touch (commonhfile_fqfn )
668
735
print_err (f"Neutralized future timestamp on build file: { commonhfile_fqfn } " )
669
736
670
- if not os .path .exists (build_path_core ):
671
- os .makedirs (build_path_core )
672
- print_msg ("Clean build, created dir " + build_path_core )
673
-
674
737
if os .path .exists (source_globals_h_fqfn ):
675
738
print_msg ("Using global include from " + source_globals_h_fqfn )
676
739
@@ -750,4 +813,10 @@ def main():
750
813
handle_error (0 ) # commit print buffer
751
814
752
815
if __name__ == '__main__' :
753
- sys .exit (main ())
816
+ rc = 1
817
+ try :
818
+ rc = main ()
819
+ except :
820
+ print_err (traceback .format_exc ())
821
+ handle_error (0 )
822
+ sys .exit (rc )
0 commit comments