1616else :
1717 import io
1818
19+
1920@contextlib .contextmanager
2021def stdchannel_redirected (stdchannel ):
2122 """
@@ -30,13 +31,15 @@ def stdchannel_redirected(stdchannel):
3031 finally :
3132 setattr (sys , stdchannel , old )
3233
34+
3335# Subclass setuptools Extension to add a parameter specifying where the shared
3436# library should be placed after being compiled
3537class ImportCppExt (setuptools .Extension ):
3638 def __init__ (self , libdest , * args , ** kwargs ):
3739 self .libdest = libdest
3840 setuptools .Extension .__init__ (self , * args , ** kwargs )
3941
42+
4043# Subclass setuptools build_ext to put the compiled shared library in the
4144# appropriate place in the source tree.
4245class BuildImportCppExt (setuptools .command .build_ext .build_ext ):
@@ -48,15 +51,23 @@ def copy_extensions_to_source(self):
4851 dest_filename = os .path .join (ext .libdest , os .path .basename (filename ))
4952
5053 distutils .file_util .copy_file (
51- src_filename , dest_filename ,
52- verbose = self .verbose , dry_run = self .dry_run
54+ src_filename , dest_filename , verbose = self .verbose , dry_run = self .dry_run
5355 )
5456
57+
5558# Patch for parallel compilation with distutils
56- # From: http://stackoverflow.com/questions/11013851/speeding-up-build-process-with-distutils
57- def parallel_compile (self , sources , output_dir = None , macros = None ,
58- include_dirs = None , debug = 0 , extra_preargs = None , extra_postargs = None ,
59- depends = None ):
59+ # From: http://stackoverflow.com/questions/11013851/speeding-up-build-process-with-distutils # noqa: E501
60+ def parallel_compile (
61+ self ,
62+ sources ,
63+ output_dir = None ,
64+ macros = None ,
65+ include_dirs = None ,
66+ debug = 0 ,
67+ extra_preargs = None ,
68+ extra_postargs = None ,
69+ depends = None ,
70+ ):
6071
6172 # these lines are copied directly from distutils.ccompiler.CCompiler
6273 macros , objects , extra_postargs , pp_opts , build = self ._setup_compile (
@@ -70,6 +81,7 @@ def parallel_compile(self, sources, output_dir = None, macros = None,
7081 try :
7182 import multiprocessing
7283 import multiprocessing .pool
84+
7385 N = multiprocessing .cpu_count ()
7486 except (ImportError , NotImplementedError ):
7587 pass
@@ -86,68 +98,66 @@ def _single_compile(obj):
8698 # print("took " + str(end - start) + " to compile " + str(obj))
8799
88100 # imap is evaluated on demand, converting to list() forces execution
89- list (multiprocessing .pool .ThreadPool (N ).imap (_single_compile ,objects ))
101+ list (multiprocessing .pool .ThreadPool (N ).imap (_single_compile , objects ))
90102 return objects
91103
104+
92105def build_module (module_data ):
93106 build_path = tempfile .mkdtemp ()
94107
95- full_module_name = module_data [' fullname' ]
96- filepath = module_data [' filepath' ]
97- cfg = module_data [' cfg' ]
108+ full_module_name = module_data [" fullname" ]
109+ filepath = module_data [" filepath" ]
110+ cfg = module_data [" cfg" ]
98111
99- module_data [' abs_include_dirs' ] = [
100- make_absolute (module_data [' filedirname' ], d )
101- for d in cfg .get (' include_dirs' , [])
112+ module_data [" abs_include_dirs" ] = [
113+ make_absolute (module_data [" filedirname" ], d )
114+ for d in cfg .get (" include_dirs" , [])
102115 ] + [os .path .dirname (filepath )]
103- module_data [' abs_library_dirs' ] = [
104- make_absolute (module_data [' filedirname' ], d )
105- for d in cfg .get (' library_dirs' , [])
116+ module_data [" abs_library_dirs" ] = [
117+ make_absolute (module_data [" filedirname" ], d )
118+ for d in cfg .get (" library_dirs" , [])
106119 ]
107- module_data ['dependency_dirs' ] = (
108- module_data ['abs_include_dirs' ] + [module_data ['filedirname' ]]
109- )
110- module_data ['extra_source_filepaths' ] = [
111- make_absolute (module_data ['filedirname' ], s )
112- for s in cfg .get ('sources' , [])
120+ module_data ["dependency_dirs" ] = module_data ["abs_include_dirs" ] + [
121+ module_data ["filedirname" ]
122+ ]
123+ module_data ["extra_source_filepaths" ] = [
124+ make_absolute (module_data ["filedirname" ], s ) for s in cfg .get ("sources" , [])
113125 ]
114126
115127 ext = ImportCppExt (
116128 os .path .dirname (filepath ),
117129 full_module_name ,
118- language = ' c++' ,
119- sources = (
120- module_data [' extra_source_filepaths' ] +
121- [module_data [' rendered_src_filepath' ]]
130+ language = " c++" ,
131+ sources = (
132+ module_data [" extra_source_filepaths" ]
133+ + [module_data [" rendered_src_filepath" ]]
122134 ),
123- include_dirs = module_data [' abs_include_dirs' ],
124- extra_compile_args = cfg .get (' extra_compile_args' , []),
125- extra_link_args = cfg .get (' extra_link_args' , []),
126- library_dirs = module_data [' abs_library_dirs' ],
127- libraries = cfg .get (' libraries' , [])
135+ include_dirs = module_data [" abs_include_dirs" ],
136+ extra_compile_args = cfg .get (" extra_compile_args" , []),
137+ extra_link_args = cfg .get (" extra_link_args" , []),
138+ library_dirs = module_data [" abs_library_dirs" ],
139+ libraries = cfg .get (" libraries" , []),
128140 )
129141
130- args = [' build_ext' , ' --inplace' ]
131- args .append (' --build-temp=' + build_path )
132- args .append (' --build-lib=' + build_path )
142+ args = [" build_ext" , " --inplace" ]
143+ args .append (" --build-temp=" + build_path )
144+ args .append (" --build-lib=" + build_path )
133145
134146 if cppimport .config .quiet :
135- args .append ('-q' )
147+ args .append ("-q" )
136148 else :
137- args .append ('-v' )
149+ args .append ("-v" )
138150
139151 setuptools_args = dict (
140- name = full_module_name ,
141- ext_modules = [ext ],
142- script_args = args ,
143- cmdclass = {
144- 'build_ext' : BuildImportCppExt
145- }
152+ name = full_module_name ,
153+ ext_modules = [ext ],
154+ script_args = args ,
155+ cmdclass = {"build_ext" : BuildImportCppExt },
146156 )
147157
148158 # Monkey patch in the parallel compiler if requested.
149159 py33orgreater = sys .version_info [0 ] >= 3 and sys .version_info [1 ] >= 3
150- parallelize = cfg .get (' parallel' ) and py33orgreater
160+ parallelize = cfg .get (" parallel" ) and py33orgreater
151161 if parallelize :
152162 old_compile = distutils .ccompiler .CCompiler .compile
153163 distutils .ccompiler .CCompiler .compile = parallel_compile
0 commit comments