@@ -437,6 +437,11 @@ def pointerize(decl: str, name: str) -> str:
437
437
return decl .replace (name , '*{}' .format (name ))
438
438
439
439
440
+ def group_dir (group_name : str ) -> str :
441
+ """Given a group name, return the relative directory path for it. """
442
+ return os .sep .join (group_name .split ('.' )[:- 1 ])
443
+
444
+
440
445
class GroupGenerator :
441
446
def __init__ (self ,
442
447
literals : LiteralsMap ,
@@ -477,7 +482,11 @@ def __init__(self,
477
482
478
483
@property
479
484
def group_suffix (self ) -> str :
480
- return '_' + self .group_name if self .group_name else ''
485
+ return '_' + exported_name (self .group_name ) if self .group_name else ''
486
+
487
+ @property
488
+ def short_group_suffix (self ) -> str :
489
+ return '_' + exported_name (self .group_name .split ('.' )[- 1 ]) if self .group_name else ''
481
490
482
491
def generate_c_for_modules (self ) -> List [Tuple [str , str ]]:
483
492
file_contents = []
@@ -489,8 +498,8 @@ def generate_c_for_modules(self) -> List[Tuple[str, str]]:
489
498
if self .compiler_options .include_runtime_files :
490
499
base_emitter .emit_line ('#include "CPy.c"' )
491
500
base_emitter .emit_line ('#include "getargs.c"' )
492
- base_emitter .emit_line ('#include "__native{}.h"' .format (self .group_suffix ))
493
- base_emitter .emit_line ('#include "__native_internal{}.h"' .format (self .group_suffix ))
501
+ base_emitter .emit_line ('#include "__native{}.h"' .format (self .short_group_suffix ))
502
+ base_emitter .emit_line ('#include "__native_internal{}.h"' .format (self .short_group_suffix ))
494
503
emitter = base_emitter
495
504
496
505
for (_ , literal ), identifier in self .literals .items ():
@@ -503,8 +512,9 @@ def generate_c_for_modules(self) -> List[Tuple[str, str]]:
503
512
for module_name , module in self .modules :
504
513
if multi_file :
505
514
emitter = Emitter (self .context )
506
- emitter .emit_line ('#include "__native{}.h"' .format (self .group_suffix ))
507
- emitter .emit_line ('#include "__native_internal{}.h"' .format (self .group_suffix ))
515
+ emitter .emit_line ('#include "__native{}.h"' .format (self .short_group_suffix ))
516
+ emitter .emit_line (
517
+ '#include "__native_internal{}.h"' .format (self .short_group_suffix ))
508
518
509
519
self .declare_module (module_name , emitter )
510
520
self .declare_internal_globals (module_name , emitter )
@@ -544,7 +554,7 @@ def generate_c_for_modules(self) -> List[Tuple[str, str]]:
544
554
declarations .emit_line ('#define MYPYC_NATIVE_INTERNAL{}_H' .format (self .group_suffix ))
545
555
declarations .emit_line ('#include <Python.h>' )
546
556
declarations .emit_line ('#include <CPy.h>' )
547
- declarations .emit_line ('#include "__native{}.h"' .format (self .group_suffix ))
557
+ declarations .emit_line ('#include "__native{}.h"' .format (self .short_group_suffix ))
548
558
declarations .emit_line ()
549
559
declarations .emit_line ('int CPyGlobalsInit(void);' )
550
560
declarations .emit_line ()
@@ -557,9 +567,13 @@ def generate_c_for_modules(self) -> List[Tuple[str, str]]:
557
567
generate_function_declaration (fn , declarations )
558
568
559
569
for lib in sorted (self .context .group_deps ):
570
+ elib = exported_name (lib )
571
+ short_lib = exported_name (lib .split ('.' )[- 1 ])
560
572
declarations .emit_lines (
561
- '#include "__native_{}.h"' .format (lib ),
562
- 'struct export_table_{} exports_{};' .format (lib , lib )
573
+ '#include <{}>' .format (
574
+ os .path .join (group_dir (lib ), "__native_{}.h" .format (short_lib ))
575
+ ),
576
+ 'struct export_table_{} exports_{};' .format (elib , elib )
563
577
)
564
578
565
579
sorted_decls = self .toposort_declarations ()
@@ -591,13 +605,15 @@ def generate_c_for_modules(self) -> List[Tuple[str, str]]:
591
605
ext_declarations .emit_line ('#endif' )
592
606
declarations .emit_line ('#endif' )
593
607
594
- return file_contents + [('__native{}.c' .format (self .group_suffix ),
595
- '' .join (emitter .fragments )),
596
- ('__native_internal{}.h' .format (self .group_suffix ),
597
- '' .join (declarations .fragments )),
598
- ('__native{}.h' .format (self .group_suffix ),
599
- '' .join (ext_declarations .fragments )),
600
- ]
608
+ output_dir = group_dir (self .group_name ) if self .group_name else ''
609
+ return file_contents + [
610
+ (os .path .join (output_dir , '__native{}.c' .format (self .short_group_suffix )),
611
+ '' .join (emitter .fragments )),
612
+ (os .path .join (output_dir , '__native_internal{}.h' .format (self .short_group_suffix )),
613
+ '' .join (declarations .fragments )),
614
+ (os .path .join (output_dir , '__native{}.h' .format (self .short_group_suffix )),
615
+ '' .join (ext_declarations .fragments )),
616
+ ]
601
617
602
618
def generate_export_table (self , decl_emitter : Emitter , code_emitter : Emitter ) -> None :
603
619
"""Generate the declaration and definition of the group's export struct.
@@ -683,12 +699,14 @@ def generate_shared_lib_init(self, emitter: Emitter) -> None:
683
699
684
700
emitter .emit_line ()
685
701
emitter .emit_lines (
686
- 'PyMODINIT_FUNC PyInit_{}(void)' .format (shared_lib_name (self .group_name )),
702
+ 'PyMODINIT_FUNC PyInit_{}(void)' .format (
703
+ shared_lib_name (self .group_name ).split ('.' )[- 1 ]),
687
704
'{' ,
688
705
('static PyModuleDef def = {{ PyModuleDef_HEAD_INIT, "{}", NULL, -1, NULL, NULL }};'
689
- .format (self .group_name )),
706
+ .format (shared_lib_name ( self .group_name ) )),
690
707
'int res;' ,
691
708
'PyObject *capsule;' ,
709
+ 'PyObject *tmp;' ,
692
710
'static PyObject *module;' ,
693
711
'if (module) {' ,
694
712
'Py_INCREF(module);' ,
@@ -733,14 +751,17 @@ def generate_shared_lib_init(self, emitter: Emitter) -> None:
733
751
)
734
752
735
753
for group in sorted (self .context .group_deps ):
754
+ egroup = exported_name (group )
736
755
emitter .emit_lines (
756
+ 'tmp = PyImport_ImportModule("{}"); if (!tmp) goto fail; Py_DECREF(tmp);' .format (
757
+ shared_lib_name (group )),
737
758
'struct export_table_{} *pexports_{} = PyCapsule_Import("{}.exports", 0);' .format (
738
- group , group , shared_lib_name (group )),
739
- 'if (!pexports_{}) {{' .format (group ),
759
+ egroup , egroup , shared_lib_name (group )),
760
+ 'if (!pexports_{}) {{' .format (egroup ),
740
761
'goto fail;' ,
741
762
'}' ,
742
763
'memcpy(&exports_{group}, pexports_{group}, sizeof(exports_{group}));' .format (
743
- group = group ),
764
+ group = egroup ),
744
765
'' ,
745
766
)
746
767
0 commit comments