26
26
# 2024-04-21 Bernard Add toolchain detection in sdk packages
27
27
# 2025-01-05 Bernard Add logging as Env['log']
28
28
# 2025-03-02 ZhaoCake Add MkDist_Strip
29
+ # 2025-01-05 Assistant Refactor SCons PreProcessor patch to independent class
29
30
30
31
import os
31
32
import sys
39
40
from utils import _make_path_relative
40
41
from mkdist import do_copy_file
41
42
from options import AddOptions
43
+ from preprocessor import create_preprocessor_instance
44
+ from win32spawn import Win32Spawn
42
45
43
46
BuildOptions = {}
44
47
Projects = []
45
48
Rtt_Root = ''
46
49
Env = None
47
50
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
-
126
51
def PrepareBuilding (env , root_directory , has_libcpu = False , remove_components = []):
127
52
128
53
global BuildOptions
@@ -294,7 +219,7 @@ def PrepareBuilding(env, root_directory, has_libcpu=False, remove_components = [
294
219
Env .Append (BUILDERS = {'BuildLib' : bld })
295
220
296
221
# parse rtconfig.h to get used component
297
- PreProcessor = PatchedPreProcessor ()
222
+ PreProcessor = create_preprocessor_instance ()
298
223
f = open ('rtconfig.h' , 'r' )
299
224
contents = f .read ()
300
225
f .close ()
@@ -433,7 +358,7 @@ def PrepareModuleBuilding(env, root_directory, bsp_directory):
433
358
Rtt_Root = root_directory
434
359
435
360
# parse bsp rtconfig.h to get used component
436
- PreProcessor = PatchedPreProcessor ()
361
+ PreProcessor = create_preprocessor_instance ()
437
362
f = open (bsp_directory + '/rtconfig.h' , 'r' )
438
363
contents = f .read ()
439
364
f .close ()
@@ -877,7 +802,7 @@ def local_group(group, objects):
877
802
def GenTargetProject (program = None ):
878
803
879
804
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
881
806
882
807
if os .path .isfile ('template.uvprojx' ) and GetOption ('target' ) not in ['mdk4' ]: # Keil5
883
808
MDK5Project (GetOption ('project-name' ) + '.uvprojx' , Projects )
@@ -895,68 +820,68 @@ def GenTargetProject(program = None):
895
820
print ("Keil-MDK project has generated successfully!" )
896
821
897
822
if GetOption ('target' ) == 'iar' :
898
- from iar import IARProject , IARVersion
823
+ from targets . iar import IARProject , IARVersion
899
824
print ("IAR Version: " + IARVersion ())
900
825
IARProject (GetOption ('project-name' ) + '.ewp' , Projects )
901
826
print ("IAR project has generated successfully!" )
902
827
903
828
if GetOption ('target' ) == 'vs' :
904
- from vs import VSProject
829
+ from targets . vs import VSProject
905
830
VSProject (GetOption ('project-name' ) + '.vcproj' , Projects , program )
906
831
907
832
if GetOption ('target' ) == 'vs2012' :
908
- from vs2012 import VS2012Project
833
+ from targets . vs2012 import VS2012Project
909
834
VS2012Project (GetOption ('project-name' ) + '.vcxproj' , Projects , program )
910
835
911
836
if GetOption ('target' ) == 'cb' :
912
- from codeblocks import CBProject
837
+ from targets . codeblocks import CBProject
913
838
CBProject (GetOption ('project-name' ) + '.cbp' , Projects , program )
914
839
915
840
if GetOption ('target' ) == 'ua' :
916
- from ua import PrepareUA
841
+ from targets . ua import PrepareUA
917
842
PrepareUA (Projects , Rtt_Root , str (Dir ('#' )))
918
843
919
844
if GetOption ('target' ) == 'vsc' :
920
- from vsc import GenerateVSCode
845
+ from targets . vsc import GenerateVSCode
921
846
GenerateVSCode (Env )
922
847
if GetOption ('cmsispack' ):
923
848
from vscpyocd import GenerateVSCodePyocdConfig
924
849
GenerateVSCodePyocdConfig (GetOption ('cmsispack' ))
925
850
926
851
if GetOption ('target' ) == 'cdk' :
927
- from cdk import CDKProject
852
+ from targets . cdk import CDKProject
928
853
CDKProject (GetOption ('project-name' ) + '.cdkproj' , Projects )
929
854
930
855
if GetOption ('target' ) == 'ses' :
931
- from ses import SESProject
856
+ from targets . ses import SESProject
932
857
SESProject (Env )
933
858
934
859
if GetOption ('target' ) == 'makefile' :
935
- from makefile import TargetMakefile
860
+ from targets . makefile import TargetMakefile
936
861
TargetMakefile (Env )
937
862
938
863
if GetOption ('target' ) == 'eclipse' :
939
- from eclipse import TargetEclipse
864
+ from targets . eclipse import TargetEclipse
940
865
TargetEclipse (Env , GetOption ('reset-project-config' ), GetOption ('project-name' ))
941
866
942
867
if GetOption ('target' ) == 'codelite' :
943
- from codelite import TargetCodelite
868
+ from targets . codelite import TargetCodelite
944
869
TargetCodelite (Projects , program )
945
870
946
871
if GetOption ('target' ) == 'cmake' or GetOption ('target' ) == 'cmake-armclang' :
947
- from cmake import CMakeProject
872
+ from targets . cmake import CMakeProject
948
873
CMakeProject (Env , Projects , GetOption ('project-name' ))
949
874
950
875
if GetOption ('target' ) == 'xmake' :
951
- from xmake import XMakeProject
876
+ from targets . xmake import XMakeProject
952
877
XMakeProject (Env , Projects )
953
878
954
879
if GetOption ('target' ) == 'esp-idf' :
955
- from esp_idf import ESPIDFProject
880
+ from targets . esp_idf import ESPIDFProject
956
881
ESPIDFProject (Env , Projects )
957
882
958
883
if GetOption ('target' ) == 'zig' :
959
- from zigbuild import ZigBuildProject
884
+ from targets . zigbuild import ZigBuildProject
960
885
ZigBuildProject (Env , Projects )
961
886
962
887
def EndBuilding (target , program = None ):
@@ -1069,7 +994,7 @@ def GetVersion():
1069
994
rtdef = os .path .join (Rtt_Root , 'include' , 'rtdef.h' )
1070
995
1071
996
# parse rtdef.h to get RT-Thread version
1072
- prepcessor = PatchedPreProcessor ()
997
+ prepcessor = create_preprocessor_instance ()
1073
998
f = open (rtdef , 'r' )
1074
999
contents = f .read ()
1075
1000
f .close ()
@@ -1109,4 +1034,3 @@ def PackageSConscript(package):
1109
1034
from package import BuildPackage
1110
1035
1111
1036
return BuildPackage (package )
1112
-
0 commit comments