2626# 2024-04-21 Bernard Add toolchain detection in sdk packages
2727# 2025-01-05 Bernard Add logging as Env['log']
2828# 2025-03-02 ZhaoCake Add MkDist_Strip
29+ # 2025-01-05 Assistant Refactor SCons PreProcessor patch to independent class
2930
3031import os
3132import sys
3940from utils import _make_path_relative
4041from mkdist import do_copy_file
4142from options import AddOptions
43+ from preprocessor import create_preprocessor_instance
44+ from win32spawn import Win32Spawn
4245
4346BuildOptions = {}
4447Projects = []
4548Rtt_Root = ''
4649Env = None
4750
48- # SCons PreProcessor patch
49- def start_handling_includes (self , t = None ):
50- """
51- Causes the PreProcessor object to start processing #import,
52- #include and #include_next lines.
53-
54- This method will be called when a #if, #ifdef, #ifndef or #elif
55- evaluates True, or when we reach the #else in a #if, #ifdef,
56- #ifndef or #elif block where a condition already evaluated
57- False.
58-
59- """
60- d = self .dispatch_table
61- p = self .stack [- 1 ] if self .stack else self .default_table
62-
63- for k in ('import' , 'include' , 'include_next' , 'define' ):
64- d [k ] = p [k ]
65-
66- def stop_handling_includes (self , t = None ):
67- """
68- Causes the PreProcessor object to stop processing #import,
69- #include and #include_next lines.
70-
71- This method will be called when a #if, #ifdef, #ifndef or #elif
72- evaluates False, or when we reach the #else in a #if, #ifdef,
73- #ifndef or #elif block where a condition already evaluated True.
74- """
75- d = self .dispatch_table
76- d ['import' ] = self .do_nothing
77- d ['include' ] = self .do_nothing
78- d ['include_next' ] = self .do_nothing
79- d ['define' ] = self .do_nothing
80-
81- PatchedPreProcessor = SCons .cpp .PreProcessor
82- PatchedPreProcessor .start_handling_includes = start_handling_includes
83- PatchedPreProcessor .stop_handling_includes = stop_handling_includes
84-
85- class Win32Spawn :
86- def spawn (self , sh , escape , cmd , args , env ):
87- # deal with the cmd build-in commands which cannot be used in
88- # subprocess.Popen
89- if cmd == 'del' :
90- for f in args [1 :]:
91- try :
92- os .remove (f )
93- except Exception as e :
94- print ('Error removing file: ' + e )
95- return - 1
96- return 0
97-
98- import subprocess
99-
100- newargs = ' ' .join (args [1 :])
101- cmdline = cmd + " " + newargs
102-
103- # Make sure the env is constructed by strings
104- _e = dict ([(k , str (v )) for k , v in env .items ()])
105-
106- # Windows(tm) CreateProcess does not use the env passed to it to find
107- # the executables. So we have to modify our own PATH to make Popen
108- # work.
109- old_path = os .environ ['PATH' ]
110- os .environ ['PATH' ] = _e ['PATH' ]
111-
112- try :
113- proc = subprocess .Popen (cmdline , env = _e , shell = False )
114- except Exception as e :
115- print ('Error in calling command:' + cmdline .split (' ' )[0 ])
116- print ('Exception: ' + os .strerror (e .errno ))
117- if (os .strerror (e .errno ) == "No such file or directory" ):
118- print ("\n Please check Toolchains PATH setting.\n " )
119-
120- return e .errno
121- finally :
122- os .environ ['PATH' ] = old_path
123-
124- return proc .wait ()
125-
12651def PrepareBuilding (env , root_directory , has_libcpu = False , remove_components = []):
12752
12853 global BuildOptions
@@ -294,7 +219,7 @@ def PrepareBuilding(env, root_directory, has_libcpu=False, remove_components = [
294219 Env .Append (BUILDERS = {'BuildLib' : bld })
295220
296221 # parse rtconfig.h to get used component
297- PreProcessor = PatchedPreProcessor ()
222+ PreProcessor = create_preprocessor_instance ()
298223 f = open ('rtconfig.h' , 'r' )
299224 contents = f .read ()
300225 f .close ()
@@ -433,7 +358,7 @@ def PrepareModuleBuilding(env, root_directory, bsp_directory):
433358 Rtt_Root = root_directory
434359
435360 # parse bsp rtconfig.h to get used component
436- PreProcessor = PatchedPreProcessor ()
361+ PreProcessor = create_preprocessor_instance ()
437362 f = open (bsp_directory + '/rtconfig.h' , 'r' )
438363 contents = f .read ()
439364 f .close ()
@@ -877,7 +802,7 @@ def local_group(group, objects):
877802def GenTargetProject (program = None ):
878803
879804 if GetOption ('target' ) in ['mdk' , 'mdk4' , 'mdk5' ]:
880- from keil import MDK2Project , MDK4Project , MDK5Project , ARMCC_Version
805+ from targets . keil import MDK2Project , MDK4Project , MDK5Project , ARMCC_Version
881806
882807 if os .path .isfile ('template.uvprojx' ) and GetOption ('target' ) not in ['mdk4' ]: # Keil5
883808 MDK5Project (GetOption ('project-name' ) + '.uvprojx' , Projects )
@@ -895,68 +820,68 @@ def GenTargetProject(program = None):
895820 print ("Keil-MDK project has generated successfully!" )
896821
897822 if GetOption ('target' ) == 'iar' :
898- from iar import IARProject , IARVersion
823+ from targets . iar import IARProject , IARVersion
899824 print ("IAR Version: " + IARVersion ())
900825 IARProject (GetOption ('project-name' ) + '.ewp' , Projects )
901826 print ("IAR project has generated successfully!" )
902827
903828 if GetOption ('target' ) == 'vs' :
904- from vs import VSProject
829+ from targets . vs import VSProject
905830 VSProject (GetOption ('project-name' ) + '.vcproj' , Projects , program )
906831
907832 if GetOption ('target' ) == 'vs2012' :
908- from vs2012 import VS2012Project
833+ from targets . vs2012 import VS2012Project
909834 VS2012Project (GetOption ('project-name' ) + '.vcxproj' , Projects , program )
910835
911836 if GetOption ('target' ) == 'cb' :
912- from codeblocks import CBProject
837+ from targets . codeblocks import CBProject
913838 CBProject (GetOption ('project-name' ) + '.cbp' , Projects , program )
914839
915840 if GetOption ('target' ) == 'ua' :
916- from ua import PrepareUA
841+ from targets . ua import PrepareUA
917842 PrepareUA (Projects , Rtt_Root , str (Dir ('#' )))
918843
919844 if GetOption ('target' ) == 'vsc' :
920- from vsc import GenerateVSCode
845+ from targets . vsc import GenerateVSCode
921846 GenerateVSCode (Env )
922847 if GetOption ('cmsispack' ):
923848 from vscpyocd import GenerateVSCodePyocdConfig
924849 GenerateVSCodePyocdConfig (GetOption ('cmsispack' ))
925850
926851 if GetOption ('target' ) == 'cdk' :
927- from cdk import CDKProject
852+ from targets . cdk import CDKProject
928853 CDKProject (GetOption ('project-name' ) + '.cdkproj' , Projects )
929854
930855 if GetOption ('target' ) == 'ses' :
931- from ses import SESProject
856+ from targets . ses import SESProject
932857 SESProject (Env )
933858
934859 if GetOption ('target' ) == 'makefile' :
935- from makefile import TargetMakefile
860+ from targets . makefile import TargetMakefile
936861 TargetMakefile (Env )
937862
938863 if GetOption ('target' ) == 'eclipse' :
939- from eclipse import TargetEclipse
864+ from targets . eclipse import TargetEclipse
940865 TargetEclipse (Env , GetOption ('reset-project-config' ), GetOption ('project-name' ))
941866
942867 if GetOption ('target' ) == 'codelite' :
943- from codelite import TargetCodelite
868+ from targets . codelite import TargetCodelite
944869 TargetCodelite (Projects , program )
945870
946871 if GetOption ('target' ) == 'cmake' or GetOption ('target' ) == 'cmake-armclang' :
947- from cmake import CMakeProject
872+ from targets . cmake import CMakeProject
948873 CMakeProject (Env , Projects , GetOption ('project-name' ))
949874
950875 if GetOption ('target' ) == 'xmake' :
951- from xmake import XMakeProject
876+ from targets . xmake import XMakeProject
952877 XMakeProject (Env , Projects )
953878
954879 if GetOption ('target' ) == 'esp-idf' :
955- from esp_idf import ESPIDFProject
880+ from targets . esp_idf import ESPIDFProject
956881 ESPIDFProject (Env , Projects )
957882
958883 if GetOption ('target' ) == 'zig' :
959- from zigbuild import ZigBuildProject
884+ from targets . zigbuild import ZigBuildProject
960885 ZigBuildProject (Env , Projects )
961886
962887def EndBuilding (target , program = None ):
@@ -1069,7 +994,7 @@ def GetVersion():
1069994 rtdef = os .path .join (Rtt_Root , 'include' , 'rtdef.h' )
1070995
1071996 # parse rtdef.h to get RT-Thread version
1072- prepcessor = PatchedPreProcessor ()
997+ prepcessor = create_preprocessor_instance ()
1073998 f = open (rtdef , 'r' )
1074999 contents = f .read ()
10751000 f .close ()
@@ -1109,4 +1034,3 @@ def PackageSConscript(package):
11091034 from package import BuildPackage
11101035
11111036 return BuildPackage (package )
1112-
0 commit comments