@@ -16,37 +16,40 @@ class MultipleCodeUnit(object):
1616 def __init__ (self , modulename , outdir ):
1717 self .modulename = modulename
1818 self .outdir = outdir
19- self .codeunits = {} # maps from a header to a SingleCodeUnit
19+ self .codeunits = {} # maps from a (filename, function) to a SingleCodeUnit
2020 self .functions = []
2121 self ._current = None
22+ self .all = SingleCodeUnit (None , None )
2223
2324
24- def _FunctionName (self , code_unit_name ):
25- return '_Export_ %s' % utils .makeid (code_unit_name )
25+ def _FunctionName (self , export_name ):
26+ return 'Export_ %s' % utils .makeid (export_name )
2627
2728
28- def _FileName (self , code_unit_name ):
29- filename = os .path .basename (code_unit_name )
29+ def _FileName (self , interface_file ):
30+ filename = os .path .basename (interface_file )
3031 filename = '_%s.cpp' % os .path .splitext (filename )[0 ]
3132 return os .path .join (self .outdir , filename )
3233
3334
34- def SetCurrent (self , code_unit_name ):
35+ def SetCurrent (self , interface_file , export_name ):
3536 'Changes the current code unit'
36- try :
37- if code_unit_name is not None :
38- codeunit = self .codeunits [code_unit_name ]
39- else :
40- codeunit = None
41- except KeyError :
42- filename = self ._FileName (code_unit_name )
43- function_name = self ._FunctionName (code_unit_name )
44- codeunit = SingleCodeUnit (None , filename )
45- codeunit .module_definition = 'void %s()' % function_name
46- self .codeunits [code_unit_name ] = codeunit
47- if code_unit_name != '__all__' :
48- self .functions .append (function_name )
49- self ._current = codeunit
37+ if export_name is None :
38+ self ._current = None
39+ elif export_name is '__all__' :
40+ self ._current = self .all
41+ else :
42+ filename = self ._FileName (interface_file )
43+ function = self ._FunctionName (export_name )
44+ try :
45+ codeunit = self .codeunits [(filename , function )]
46+ except KeyError :
47+ codeunit = SingleCodeUnit (None , filename )
48+ codeunit .module_definition = 'void %s()' % function
49+ self .codeunits [(filename , function )] = codeunit
50+ if function not in self .functions :
51+ self .functions .append (function )
52+ self ._current = codeunit
5053
5154
5255 def Current (self ):
@@ -74,16 +77,31 @@ def _CreateOutputDir(self):
7477 def Save (self ):
7578 # create the directory where all the files will go
7679 self ._CreateOutputDir ();
80+ # order all code units by filename, and merge them all
81+ codeunits = {} # filename => list of codeunits
82+ # the main_unit holds all the include, declaration and declaration-outside sections
83+ # to keep them all in the top of the source file
84+ main_unit = None
85+ for (filename , _ ), codeunit in self .codeunits .items ():
86+ if filename not in codeunits :
87+ codeunits [filename ] = [codeunit ]
88+ main_unit = codeunit
89+ main_unit .Merge (self .all )
90+ else :
91+ for section in ('include' , 'declaration' , 'declaration-outside' ):
92+ main_unit .code [section ] = main_unit .code [section ] + codeunit .code [section ]
93+ codeunit .code [section ] = ''
94+ codeunits [filename ].append (codeunit )
7795 # write all the codeunits, merging first the contents of
7896 # the special code unit named __all__
79- __all__ = self . codeunits .get ( '__all__' )
80- for name , codeunit in self . codeunits . items ():
81- if name != '__all__' :
82- if __all__ :
83- codeunit . Merge ( __all__ )
84- codeunit . Save ()
97+ for codeunits in codeunits .values ():
98+ append = False
99+ for codeunit in codeunits :
100+ codeunit . Save ( append )
101+ if not append :
102+ append = True
85103 # generate the main cpp
86- filename = os .path .join (self .outdir , self . modulename + ' .cpp' )
104+ filename = os .path .join (self .outdir , '_main .cpp' )
87105 fout = SmartFile (filename , 'w' )
88106 fout .write (utils .left_equals ('Include' ))
89107 fout .write ('#include <boost/python.hpp>\n \n ' )
0 commit comments