Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1bba31d

Browse files
committed
Refactor compile() method implementations.
Always use _setup_compile() to do the grunt work of processing arguments, figuring out which files to compile, and emitting debug messages for files that are up-to-date. Use _get_cc_args() when possible.
1 parent 6864d30 commit 1bba31d

5 files changed

Lines changed: 178 additions & 272 deletions

File tree

Lib/distutils/bcppcompiler.py

Lines changed: 44 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -80,74 +80,61 @@ def __init__ (self,
8080

8181
# -- Worker methods ------------------------------------------------
8282

83-
def compile (self,
84-
sources,
85-
output_dir=None,
86-
macros=None,
87-
include_dirs=None,
88-
debug=0,
89-
extra_preargs=None,
90-
extra_postargs=None):
91-
92-
(output_dir, macros, include_dirs) = \
93-
self._fix_compile_args (output_dir, macros, include_dirs)
94-
(objects, skip_sources) = self._prep_compile (sources, output_dir)
95-
96-
if extra_postargs is None:
97-
extra_postargs = []
98-
99-
pp_opts = gen_preprocess_options (macros, include_dirs)
83+
def compile(self, sources,
84+
output_dir=None, macros=None, include_dirs=None, debug=0,
85+
extra_preargs=None, extra_postargs=None, depends=None):
86+
87+
macros, objects, extra_postargs, pp_opts, build = \
88+
self._setup_compile(output_dir, macros, include_dirs, sources,
89+
depends, extra_postargs)
10090
compile_opts = extra_preargs or []
10191
compile_opts.append ('-c')
10292
if debug:
10393
compile_opts.extend (self.compile_options_debug)
10494
else:
10595
compile_opts.extend (self.compile_options)
10696

107-
for i in range (len (sources)):
108-
src = sources[i] ; obj = objects[i]
109-
ext = (os.path.splitext (src))[1]
97+
for obj, (src, ext) in build.items():
98+
# XXX why do the normpath here?
99+
src = os.path.normpath(src)
100+
obj = os.path.normpath(obj)
101+
# XXX _setup_compile() did a mkpath() too but before the normpath.
102+
# Is it possible to skip the normpath?
103+
self.mkpath(os.path.dirname(obj))
110104

111-
if skip_sources[src]:
112-
log.debug("skipping %s (%s up-to-date)", src, obj)
113-
else:
114-
src = os.path.normpath(src)
115-
obj = os.path.normpath(obj)
116-
self.mkpath(os.path.dirname(obj))
117-
118-
if ext == '.res':
119-
# This is already a binary file -- skip it.
120-
continue # the 'for' loop
121-
if ext == '.rc':
122-
# This needs to be compiled to a .res file -- do it now.
123-
try:
124-
self.spawn (["brcc32", "-fo", obj, src])
125-
except DistutilsExecError, msg:
126-
raise CompileError, msg
127-
continue # the 'for' loop
128-
129-
# The next two are both for the real compiler.
130-
if ext in self._c_extensions:
131-
input_opt = ""
132-
elif ext in self._cpp_extensions:
133-
input_opt = "-P"
134-
else:
135-
# Unknown file type -- no extra options. The compiler
136-
# will probably fail, but let it just in case this is a
137-
# file the compiler recognizes even if we don't.
138-
input_opt = ""
139-
140-
output_opt = "-o" + obj
141-
142-
# Compiler command line syntax is: "bcc32 [options] file(s)".
143-
# Note that the source file names must appear at the end of
144-
# the command line.
105+
if ext == '.res':
106+
# This is already a binary file -- skip it.
107+
continue # the 'for' loop
108+
if ext == '.rc':
109+
# This needs to be compiled to a .res file -- do it now.
145110
try:
146-
self.spawn ([self.cc] + compile_opts + pp_opts +
147-
[input_opt, output_opt] +
148-
extra_postargs + [src])
111+
self.spawn (["brcc32", "-fo", obj, src])
149112
except DistutilsExecError, msg:
150113
raise CompileError, msg
114+
continue # the 'for' loop
115+
116+
# The next two are both for the real compiler.
117+
if ext in self._c_extensions:
118+
input_opt = ""
119+
elif ext in self._cpp_extensions:
120+
input_opt = "-P"
121+
else:
122+
# Unknown file type -- no extra options. The compiler
123+
# will probably fail, but let it just in case this is a
124+
# file the compiler recognizes even if we don't.
125+
input_opt = ""
126+
127+
output_opt = "-o" + obj
128+
129+
# Compiler command line syntax is: "bcc32 [options] file(s)".
130+
# Note that the source file names must appear at the end of
131+
# the command line.
132+
try:
133+
self.spawn ([self.cc] + compile_opts + pp_opts +
134+
[input_opt, output_opt] +
135+
extra_postargs + [src])
136+
except DistutilsExecError, msg:
137+
raise CompileError, msg
151138

152139
return objects
153140

Lib/distutils/cygwinccompiler.py

Lines changed: 29 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,7 @@ class CygwinCCompiler (UnixCCompiler):
6262
shared_lib_format = "%s%s"
6363
exe_extension = ".exe"
6464

65-
def __init__ (self,
66-
verbose=0,
67-
dry_run=0,
68-
force=0):
65+
def __init__ (self, verbose=0, dry_run=0, force=0):
6966

7067
UnixCCompiler.__init__ (self, verbose, dry_run, force)
7168

@@ -74,11 +71,12 @@ def __init__ (self,
7471
(status, details))
7572
if status is not CONFIG_H_OK:
7673
self.warn(
77-
"Python's pyconfig.h doesn't seem to support your compiler. " +
78-
("Reason: %s." % details) +
79-
"Compiling may fail because of undefined preprocessor macros.")
74+
"Python's pyconfig.h doesn't seem to support your compiler. "
75+
"Reason: %s. "
76+
"Compiling may fail because of undefined preprocessor macros."
77+
% details)
8078

81-
(self.gcc_version, self.ld_version, self.dllwrap_version) = \
79+
self.gcc_version, self.ld_version, self.dllwrap_version = \
8280
get_versions()
8381
self.debug_print(self.compiler_type + ": gcc %s, ld %s, dllwrap %s\n" %
8482
(self.gcc_version,
@@ -120,58 +118,33 @@ def __init__ (self,
120118
# we put here a adapted version of it.
121119
# (If we would call compile() in the base class, it would do some
122120
# initializations a second time, this is why all is done here.)
123-
def compile (self,
124-
sources,
125-
output_dir=None,
126-
macros=None,
127-
include_dirs=None,
128-
debug=0,
129-
extra_preargs=None,
130-
extra_postargs=None):
131-
132-
(output_dir, macros, include_dirs) = \
133-
self._fix_compile_args (output_dir, macros, include_dirs)
134-
(objects, skip_sources) = self._prep_compile (sources, output_dir)
135-
136-
# Figure out the options for the compiler command line.
137-
pp_opts = gen_preprocess_options (macros, include_dirs)
138-
cc_args = pp_opts + ['-c']
139-
if debug:
140-
cc_args[:0] = ['-g']
141-
if extra_preargs:
142-
cc_args[:0] = extra_preargs
143-
if extra_postargs is None:
144-
extra_postargs = []
145-
146-
# Compile all source files that weren't eliminated by
147-
# '_prep_compile()'.
148-
for i in range (len (sources)):
149-
src = sources[i] ; obj = objects[i]
150-
ext = (os.path.splitext (src))[1]
151-
if skip_sources[src]:
152-
log.debug("skipping %s (%s up-to-date)", src, obj)
153-
else:
154-
self.mkpath (os.path.dirname (obj))
155-
if ext == '.rc' or ext == '.res':
156-
# gcc needs '.res' and '.rc' compiled to object files !!!
157-
try:
158-
self.spawn (["windres","-i",src,"-o",obj])
159-
except DistutilsExecError, msg:
160-
raise CompileError, msg
161-
else: # for other files use the C-compiler
162-
try:
163-
self.spawn (self.compiler_so + cc_args +
164-
[src, '-o', obj] +
165-
extra_postargs)
166-
except DistutilsExecError, msg:
167-
raise CompileError, msg
121+
def compile(self, sources,
122+
output_dir=None, macros=None, include_dirs=None, debug=0,
123+
extra_preargs=None, extra_postargs=None, depends=None):
124+
125+
macros, objects, extra_postargs, pp_opts, build = \
126+
self._setup_compile(output_dir, macros, include_dirs, sources,
127+
depends, extra_postargs)
128+
cc_args = self._get_cc_args(pp_opts, debug, extra_preargs)
129+
130+
for obj, (src, ext) in build.items():
131+
if ext == '.rc' or ext == '.res':
132+
# gcc needs '.res' and '.rc' compiled to object files !!!
133+
try:
134+
self.spawn (["windres","-i",src,"-o",obj])
135+
except DistutilsExecError, msg:
136+
raise CompileError, msg
137+
else: # for other files use the C-compiler
138+
try:
139+
self.spawn (self.compiler_so + cc_args +
140+
[src, '-o', obj] +
141+
extra_postargs)
142+
except DistutilsExecError, msg:
143+
raise CompileError, msg
168144

169145
# Return *all* object filenames, not just the ones we just built.
170146
return objects
171147

172-
# compile ()
173-
174-
175148
def link (self,
176149
target_desc,
177150
objects,

Lib/distutils/emxccompiler.py

Lines changed: 24 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -81,51 +81,30 @@ def __init__ (self,
8181
# we put here a adapted version of it.
8282
# (If we would call compile() in the base class, it would do some
8383
# initializations a second time, this is why all is done here.)
84-
def compile (self,
85-
sources,
86-
output_dir=None,
87-
macros=None,
88-
include_dirs=None,
89-
debug=0,
90-
extra_preargs=None,
91-
extra_postargs=None):
92-
93-
(output_dir, macros, include_dirs) = \
94-
self._fix_compile_args (output_dir, macros, include_dirs)
95-
(objects, skip_sources) = self._prep_compile (sources, output_dir)
96-
97-
# Figure out the options for the compiler command line.
98-
pp_opts = gen_preprocess_options (macros, include_dirs)
99-
cc_args = pp_opts + ['-c']
100-
if debug:
101-
cc_args[:0] = ['-g']
102-
if extra_preargs:
103-
cc_args[:0] = extra_preargs
104-
if extra_postargs is None:
105-
extra_postargs = []
106-
107-
# Compile all source files that weren't eliminated by
108-
# '_prep_compile()'.
109-
for i in range (len (sources)):
110-
src = sources[i] ; obj = objects[i]
111-
ext = (os.path.splitext (src))[1]
112-
if skip_sources[src]:
113-
log.debug("skipping %s (%s up-to-date)", src, obj)
114-
else:
115-
self.mkpath (os.path.dirname (obj))
116-
if ext == '.rc':
117-
# gcc requires '.rc' compiled to binary ('.res') files !!!
118-
try:
119-
self.spawn (["rc","-r",src])
120-
except DistutilsExecError, msg:
121-
raise CompileError, msg
122-
else: # for other files use the C-compiler
123-
try:
124-
self.spawn (self.compiler_so + cc_args +
125-
[src, '-o', obj] +
126-
extra_postargs)
127-
except DistutilsExecError, msg:
128-
raise CompileError, msg
84+
85+
def compile(self, sources,
86+
output_dir=None, macros=None, include_dirs=None, debug=0,
87+
extra_preargs=None, extra_postargs=None, depends=None):
88+
89+
macros, objects, extra_postargs, pp_opts, build = \
90+
self._setup_compile(output_dir, macros, include_dirs, sources,
91+
depends, extra_postargs)
92+
cc_args = self._get_cc_args(pp_opts, debug, extra_preargs)
93+
94+
for obj, (src, ext) in build.items():
95+
if ext == '.rc':
96+
# gcc requires '.rc' compiled to binary ('.res') files !!!
97+
try:
98+
self.spawn (["rc","-r",src])
99+
except DistutilsExecError, msg:
100+
raise CompileError, msg
101+
else: # for other files use the C-compiler
102+
try:
103+
self.spawn (self.compiler_so + cc_args +
104+
[src, '-o', obj] +
105+
extra_postargs)
106+
except DistutilsExecError, msg:
107+
raise CompileError, msg
129108

130109
# Return *all* object filenames, not just the ones we just built.
131110
return objects

0 commit comments

Comments
 (0)