Skip to content

Commit 6e6f12a

Browse files
authored
Merge pull request #1952 from ArdaFu/master
[Tools] Modify buliding.py and gcc.py for work with python 3.
2 parents ddd7343 + e7ca31c commit 6e6f12a

File tree

2 files changed

+65
-66
lines changed

2 files changed

+65
-66
lines changed

tools/building.py

Lines changed: 49 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,13 @@ def spawn(self, sh, escape, cmd, args, env):
8383
try:
8484
os.remove(f)
8585
except Exception as e:
86-
print 'Error removing file: %s' % e
86+
print ('Error removing file: ' + e)
8787
return -1
8888
return 0
8989

9090
import subprocess
9191

92-
newargs = string.join(args[1:], ' ')
92+
newargs = ' '.join(args[1:])
9393
cmdline = cmd + " " + newargs
9494

9595
# Make sure the env is constructed by strings
@@ -104,8 +104,8 @@ def spawn(self, sh, escape, cmd, args, env):
104104
try:
105105
proc = subprocess.Popen(cmdline, env=_e, shell=False)
106106
except Exception as e:
107-
print 'Error in calling:\n%s' % cmdline
108-
print 'Exception: %s: %s' % (e, os.strerror(e.errno))
107+
print ('Error in calling:\n' + cmdline)
108+
print ('Exception: ' + e + ': ' + os.strerror(e.errno))
109109
return e.errno
110110
finally:
111111
os.environ['PATH'] = old_path
@@ -124,7 +124,7 @@ def GenCconfigFile(env, BuildOptions):
124124

125125
# try again
126126
if os.path.isfile('cconfig.h'):
127-
f = file('cconfig.h', 'r')
127+
f = open('cconfig.h', 'r')
128128
if f:
129129
contents = f.read()
130130
f.close();
@@ -225,7 +225,7 @@ def PrepareBuilding(env, root_directory, has_libcpu=False, remove_components = [
225225
# --target will change the toolchain settings which clang-analyzer is
226226
# depend on
227227
if GetOption('clang-analyzer'):
228-
print '--clang-analyzer cannot be used with --target'
228+
print ('--clang-analyzer cannot be used with --target')
229229
sys.exit(1)
230230

231231
SetOption('no_exec', 1)
@@ -235,8 +235,7 @@ def PrepareBuilding(env, root_directory, has_libcpu=False, remove_components = [
235235
os.environ['RTT_CC'] = rtconfig.CROSS_TOOL
236236
reload(rtconfig)
237237
except KeyError:
238-
print 'Unknow target: %s. Avaible targets: %s' % \
239-
(tgt_name, ', '.join(tgt_dict.keys()))
238+
print ('Unknow target: '+ tgt_name+'. Avaible targets: ' +', '.join(tgt_dict.keys()))
240239
sys.exit(1)
241240
elif (GetDepend('RT_USING_NEWLIB') == False and GetDepend('RT_USING_NOLIBC') == False) \
242241
and rtconfig.PLATFORM == 'gcc':
@@ -287,7 +286,7 @@ def PrepareBuilding(env, root_directory, has_libcpu=False, remove_components = [
287286

288287
# parse rtconfig.h to get used component
289288
PreProcessor = PatchedPreProcessor()
290-
f = file('rtconfig.h', 'r')
289+
f = open('rtconfig.h', 'r')
291290
contents = f.read()
292291
f.close()
293292
PreProcessor.process_contents(contents)
@@ -438,7 +437,7 @@ def GetConfigValue(name):
438437
def GetDepend(depend):
439438
building = True
440439
if type(depend) == type('str'):
441-
if not BuildOptions.has_key(depend) or BuildOptions[depend] == 0:
440+
if not depend in BuildOptions or BuildOptions[depend] == 0:
442441
building = False
443442
elif BuildOptions[depend] != '':
444443
return BuildOptions[depend]
@@ -448,7 +447,7 @@ def GetDepend(depend):
448447
# for list type depend
449448
for item in depend:
450449
if item != '':
451-
if not BuildOptions.has_key(item) or BuildOptions[item] == 0:
450+
if not item in BuildOptions or BuildOptions[item] == 0:
452451
building = False
453452

454453
return building
@@ -471,7 +470,7 @@ def LocalOptions(config_filename):
471470
def GetLocalDepend(options, depend):
472471
building = True
473472
if type(depend) == type('str'):
474-
if not options.has_key(depend) or options[depend] == 0:
473+
if not depend in options or options[depend] == 0:
475474
building = False
476475
elif options[depend] != '':
477476
return options[depend]
@@ -481,7 +480,7 @@ def GetLocalDepend(options, depend):
481480
# for list type depend
482481
for item in depend:
483482
if item != '':
484-
if not options.has_key(item) or options[item] == 0:
483+
if not item in options or options[item] == 0:
485484
building = False
486485

487486
return building
@@ -491,61 +490,61 @@ def AddDepend(option):
491490

492491
def MergeGroup(src_group, group):
493492
src_group['src'] = src_group['src'] + group['src']
494-
if group.has_key('CCFLAGS'):
495-
if src_group.has_key('CCFLAGS'):
493+
if 'CCFLAGS' in group:
494+
if 'CCFLAGS' in src_group:
496495
src_group['CCFLAGS'] = src_group['CCFLAGS'] + group['CCFLAGS']
497496
else:
498497
src_group['CCFLAGS'] = group['CCFLAGS']
499-
if group.has_key('CPPPATH'):
500-
if src_group.has_key('CPPPATH'):
498+
if 'CPPPATH' in group:
499+
if 'CPPPATH' in src_group:
501500
src_group['CPPPATH'] = src_group['CPPPATH'] + group['CPPPATH']
502501
else:
503502
src_group['CPPPATH'] = group['CPPPATH']
504-
if group.has_key('CPPDEFINES'):
505-
if src_group.has_key('CPPDEFINES'):
503+
if 'CPPDEFINES' in group:
504+
if 'CPPDEFINES' in src_group:
506505
src_group['CPPDEFINES'] = src_group['CPPDEFINES'] + group['CPPDEFINES']
507506
else:
508507
src_group['CPPDEFINES'] = group['CPPDEFINES']
509-
if group.has_key('ASFLAGS'):
510-
if src_group.has_key('ASFLAGS'):
508+
if 'ASFLAGS' in group:
509+
if 'ASFLAGS' in src_group:
511510
src_group['ASFLAGS'] = src_group['ASFLAGS'] + group['ASFLAGS']
512511
else:
513512
src_group['ASFLAGS'] = group['ASFLAGS']
514513

515514
# for local CCFLAGS/CPPPATH/CPPDEFINES
516-
if group.has_key('LOCAL_CCFLAGS'):
517-
if src_group.has_key('LOCAL_CCFLAGS'):
515+
if 'LOCAL_CCFLAGS' in group:
516+
if 'LOCAL_CCFLAGS' in src_group:
518517
src_group['LOCAL_CCFLAGS'] = src_group['LOCAL_CCFLAGS'] + group['LOCAL_CCFLAGS']
519518
else:
520519
src_group['LOCAL_CCFLAGS'] = group['LOCAL_CCFLAGS']
521-
if group.has_key('LOCAL_CPPPATH'):
522-
if src_group.has_key('LOCAL_CPPPATH'):
520+
if 'LOCAL_CPPPATH' in group:
521+
if 'LOCAL_CPPPATH' in src_group:
523522
src_group['LOCAL_CPPPATH'] = src_group['LOCAL_CPPPATH'] + group['LOCAL_CPPPATH']
524523
else:
525524
src_group['LOCAL_CPPPATH'] = group['LOCAL_CPPPATH']
526-
if group.has_key('LOCAL_CPPDEFINES'):
527-
if src_group.has_key('LOCAL_CPPDEFINES'):
525+
if 'LOCAL_CPPDEFINES' in group:
526+
if 'LOCAL_CPPDEFINES' in src_group:
528527
src_group['LOCAL_CPPDEFINES'] = src_group['LOCAL_CPPDEFINES'] + group['LOCAL_CPPDEFINES']
529528
else:
530529
src_group['LOCAL_CPPDEFINES'] = group['LOCAL_CPPDEFINES']
531530

532-
if group.has_key('LINKFLAGS'):
533-
if src_group.has_key('LINKFLAGS'):
531+
if 'LINKFLAGS' in group:
532+
if 'LINKFLAGS' in src_group:
534533
src_group['LINKFLAGS'] = src_group['LINKFLAGS'] + group['LINKFLAGS']
535534
else:
536535
src_group['LINKFLAGS'] = group['LINKFLAGS']
537-
if group.has_key('LIBS'):
538-
if src_group.has_key('LIBS'):
536+
if 'LIBS' in group:
537+
if 'LIBS' in src_group:
539538
src_group['LIBS'] = src_group['LIBS'] + group['LIBS']
540539
else:
541540
src_group['LIBS'] = group['LIBS']
542-
if group.has_key('LIBPATH'):
543-
if src_group.has_key('LIBPATH'):
541+
if 'LIBPATH' in group:
542+
if 'LIBPATH' in src_group:
544543
src_group['LIBPATH'] = src_group['LIBPATH'] + group['LIBPATH']
545544
else:
546545
src_group['LIBPATH'] = group['LIBPATH']
547-
if group.has_key('LOCAL_ASFLAGS'):
548-
if src_group.has_key('LOCAL_ASFLAGS'):
546+
if 'LOCAL_ASFLAGS' in group:
547+
if 'LOCAL_ASFLAGS' in src_group:
549548
src_group['LOCAL_ASFLAGS'] = src_group['LOCAL_ASFLAGS'] + group['LOCAL_ASFLAGS']
550549
else:
551550
src_group['LOCAL_ASFLAGS'] = group['LOCAL_ASFLAGS']
@@ -571,32 +570,32 @@ def DefineGroup(name, src, depend, **parameters):
571570
else:
572571
group['src'] = src
573572

574-
if group.has_key('CCFLAGS'):
573+
if 'CCFLAGS' in group:
575574
Env.AppendUnique(CCFLAGS = group['CCFLAGS'])
576-
if group.has_key('CPPPATH'):
575+
if 'CPPPATH' in group:
577576
Env.AppendUnique(CPPPATH = group['CPPPATH'])
578-
if group.has_key('CPPDEFINES'):
577+
if 'CPPDEFINES' in group:
579578
Env.AppendUnique(CPPDEFINES = group['CPPDEFINES'])
580-
if group.has_key('LINKFLAGS'):
579+
if 'LINKFLAGS' in group:
581580
Env.AppendUnique(LINKFLAGS = group['LINKFLAGS'])
582-
if group.has_key('ASFLAGS'):
581+
if 'ASFLAGS' in group:
583582
Env.AppendUnique(ASFLAGS = group['ASFLAGS'])
584583

585584
# check whether to clean up library
586585
if GetOption('cleanlib') and os.path.exists(os.path.join(group['path'], GroupLibFullName(name, Env))):
587586
if group['src'] != []:
588-
print 'Remove library:', GroupLibFullName(name, Env)
587+
print ('Remove library:'+ GroupLibFullName(name, Env))
589588
fn = os.path.join(group['path'], GroupLibFullName(name, Env))
590589
if os.path.exists(fn):
591590
os.unlink(fn)
592591

593-
if group.has_key('LIBS'):
592+
if 'LIBS' in group:
594593
Env.AppendUnique(LIBS = group['LIBS'])
595-
if group.has_key('LIBPATH'):
594+
if 'LIBPATH' in group:
596595
Env.AppendUnique(LIBPATH = group['LIBPATH'])
597596

598597
# check whether to build group library
599-
if group.has_key('LIBRARY'):
598+
if 'LIBRARY' in group:
600599
objs = Env.Library(name, group['src'])
601600
else:
602601
# only add source
@@ -650,7 +649,7 @@ def BuildLibInstallAction(target, source, env):
650649
if Group['name'] == lib_name:
651650
lib_name = GroupLibFullName(Group['name'], env)
652651
dst_name = os.path.join(Group['path'], lib_name)
653-
print 'Copy %s => %s' % (lib_name, dst_name)
652+
print ('Copy '+lib_name+' => ' +dst_name)
654653
do_copy_file(lib_name, dst_name)
655654
break
656655

@@ -668,7 +667,7 @@ def one_list(l):
668667

669668
# handle local group
670669
def local_group(group, objects):
671-
if group.has_key('LOCAL_CCFLAGS') or group.has_key('LOCAL_CPPPATH') or group.has_key('LOCAL_CPPDEFINES') or group.has_key('LOCAL_ASFLAGS'):
670+
if 'LOCAL_CCFLAGS' in group or 'LOCAL_CPPPATH' in group or 'LOCAL_CPPDEFINES' in group or 'LOCAL_ASFLAGS' in group:
672671
CCFLAGS = Env.get('CCFLAGS', '') + group.get('LOCAL_CCFLAGS', '')
673672
CPPPATH = Env.get('CPPPATH', ['']) + group.get('LOCAL_CPPPATH', [''])
674673
CPPDEFINES = Env.get('CPPDEFINES', ['']) + group.get('LOCAL_CPPDEFINES', [''])
@@ -705,7 +704,7 @@ def local_group(group, objects):
705704
else:
706705
# remove source files with local flags setting
707706
for group in Projects:
708-
if group.has_key('LOCAL_CCFLAGS') or group.has_key('LOCAL_CPPPATH') or group.has_key('LOCAL_CPPDEFINES'):
707+
if 'LOCAL_CCFLAGS' in group or 'LOCAL_CPPPATH' in group or 'LOCAL_CPPDEFINES' in group:
709708
for source in group['src']:
710709
for obj in objects:
711710
if source.abspath == obj.abspath or (len(obj.sources) > 0 and source.abspath == obj.sources[0].abspath):
@@ -738,7 +737,7 @@ def GenTargetProject(program = None):
738737
if template:
739738
MDK5Project('project.uvprojx', Projects)
740739
else:
741-
print 'No template project file found.'
740+
print ('No template project file found.')
742741

743742
if GetOption('target') == 'mdk4':
744743
from keil import MDK4Project
@@ -807,7 +806,7 @@ def EndBuilding(target, program = None):
807806

808807
if not GetOption('help') and not GetOption('target'):
809808
if not os.path.exists(rtconfig.EXEC_PATH):
810-
print "Error: the toolchain path (%s) is not exist, please check 'EXEC_PATH' in path or rtconfig.py." % rtconfig.EXEC_PATH
809+
print ("Error: the toolchain path (" + rtconfig.EXEC_PATH + ") is not exist, please check 'EXEC_PATH' in path or rtconfig.py.")
811810
need_exit = True
812811

813812
if need_exit:
@@ -873,7 +872,7 @@ def GetVersion():
873872
version = int(filter(lambda ch: ch in '0123456789.', def_ns['RT_VERSION']))
874873
subversion = int(filter(lambda ch: ch in '0123456789.', def_ns['RT_SUBVERSION']))
875874

876-
if def_ns.has_key('RT_REVISION'):
875+
if 'RT_REVISION' in def_ns:
877876
revision = int(filter(lambda ch: ch in '0123456789.', def_ns['RT_REVISION']))
878877
return '%d.%d.%d' % (version, subversion, revision)
879878

tools/gcc.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,19 @@ def GetNewLibVersion(rtconfig):
5050
root = GetGCCRoot(rtconfig)
5151

5252
if CheckHeader(rtconfig, '_newlib_version.h'): # get version from _newlib_version.h file
53-
f = file(os.path.join(root, 'include', '_newlib_version.h'))
53+
f = open(os.path.join(root, 'include', '_newlib_version.h'), 'r')
5454
if f:
5555
for line in f:
5656
if line.find('_NEWLIB_VERSION') != -1 and line.find('"') != -1:
5757
version = re.search(r'\"([^"]+)\"', line).groups()[0]
58+
f.close()
5859
elif CheckHeader(rtconfig, 'newlib.h'): # get version from newlib.h
59-
f = file(os.path.join(root, 'include', 'newlib.h'))
60+
f = open(os.path.join(root, 'include', 'newlib.h'), 'r')
6061
if f:
6162
for line in f:
6263
if line.find('_NEWLIB_VERSION') != -1 and line.find('"') != -1:
6364
version = re.search(r'\"([^"]+)\"', line).groups()[0]
64-
65+
f.close()
6566
return version
6667

6768
def GCCResult(rtconfig, str):
@@ -77,7 +78,7 @@ def checkAndGetResult(pattern, string):
7778
gcc_cmd = os.path.join(rtconfig.EXEC_PATH, rtconfig.CC)
7879

7980
# use temp file to get more information
80-
f = file('__tmp.c', 'w')
81+
f = open('__tmp.c', 'w')
8182
if f:
8283
f.write(str)
8384
f.close()
@@ -103,27 +104,27 @@ def checkAndGetResult(pattern, string):
103104
stdc = '1989'
104105
posix_thread = 0
105106

106-
for line in stdout.split('\n'):
107-
if re.search('fd_set', line):
107+
for line in stdout.split(b'\n'):
108+
if re.search(b'fd_set', line):
108109
have_fdset = 1
109110

110111
# check for sigal
111-
if re.search('struct[ \t]+sigaction', line):
112+
if re.search(b'struct[ \t]+sigaction', line):
112113
have_sigaction = 1
113-
if re.search('struct[ \t]+sigevent', line):
114+
if re.search(b'struct[ \t]+sigevent', line):
114115
have_sigevent = 1
115-
if re.search('siginfo_t', line):
116+
if re.search(b'siginfo_t', line):
116117
have_siginfo = 1
117-
if re.search('union[ \t]+sigval', line):
118+
if re.search(b'union[ \t]+sigval', line):
118119
have_sigval = 1
119120

120-
if re.search('char\* version', line):
121-
version = re.search(r'\"([^"]+)\"', line).groups()[0]
121+
if re.search(b'char\* version', line):
122+
version = re.search(br'\"([^"]+)\"', line).groups()[0]
122123

123-
if re.findall('iso_c_visible = [\d]+', line):
124+
if re.findall(b'iso_c_visible = [\d]+', line):
124125
stdc = re.findall('[\d]+', line)[0]
125126

126-
if re.findall('pthread_create', line):
127+
if re.findall(b'pthread_create', line):
127128
posix_thread = 1
128129

129130
if have_fdset:
@@ -147,7 +148,6 @@ def checkAndGetResult(pattern, string):
147148
result += '#define LIBC_POSIX_THREADS 1\n'
148149

149150
os.remove('__tmp.c')
150-
151151
return result
152152

153153
def GenerateGCCConfig(rtconfig):
@@ -187,7 +187,7 @@ def GenerateGCCConfig(rtconfig):
187187
cc_header += GCCResult(rtconfig, str)
188188
cc_header += '\n#endif\n'
189189

190-
cc_file = file('cconfig.h', 'w')
190+
cc_file = open('cconfig.h', 'w')
191191
if cc_file:
192192
cc_file.write(cc_header)
193193
cc_file.close()

0 commit comments

Comments
 (0)