Skip to content

Commit bab275f

Browse files
authored
Fix multi-file stuff on OS X (mypyc/mypyc#588)
Apparently on macOS it doesn't work (anymore?) to have the same symbol declared in multiple object files without initializing it. So instead be careful to declare them all as extern and then separately declare them in the c files. It worked in CI, too, so I'm pretty confused, but oh well. Also fix our attempts to keep it from doing 32-bit builds on OS X.
1 parent edeb5bb commit bab275f

File tree

6 files changed

+28
-17
lines changed

6 files changed

+28
-17
lines changed

mypyc/build.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ def setup_mypycify_vars() -> None:
7171
vars['LDSHARED'] = vars['LDSHARED'].replace('-bundle', '-dynamiclib')
7272
# Also disable building 32-bit binaries, since we generate too much code
7373
# for a 32-bit Mach-O object. There has to be a better way to do this.
74+
vars['LDSHARED'] = vars['LDSHARED'].replace('-arch i386', '')
7475
vars['LDFLAGS'] = vars['LDFLAGS'].replace('-arch i386', '')
7576
vars['CFLAGS'] = vars['CFLAGS'].replace('-arch i386', '')
7677

mypyc/emit.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@
2121

2222
class HeaderDeclaration:
2323
def __init__(self,
24-
dependencies: Set[str], decl: List[str], defn: Optional[List[str]]) -> None:
24+
dependencies: Set[str], decl: List[str], defn: Optional[List[str]],
25+
needs_extern: bool = False) -> None:
2526
self.dependencies = dependencies
2627
self.decl = decl
2728
self.defn = defn
29+
self.needs_extern = needs_extern
2830

2931

3032
class EmitterContext:

mypyc/emitclass.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,9 @@ def generate_slots(cl: ClassIR, table: SlotTable, emitter: Emitter) -> Dict[str,
7171
return fields
7272

7373

74-
def generate_class_type_decl(cl: ClassIR, emitter: Emitter) -> None:
75-
emitter.emit_line('PyTypeObject *{};'.format(emitter.type_struct_name(cl)))
74+
def generate_class_type_decl(cl: ClassIR, c_emitter: Emitter, emitter: Emitter) -> None:
75+
c_emitter.emit_line('PyTypeObject *{};'.format(emitter.type_struct_name(cl)))
76+
emitter.emit_line('extern PyTypeObject *{};'.format(emitter.type_struct_name(cl)))
7677
emitter.emit_line()
7778
generate_object_struct(cl, emitter)
7879
emitter.emit_line()

mypyc/emitmodule.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,12 +192,17 @@ def generate_c_for_modules(self) -> List[Tuple[str, str]]:
192192
declarations.emit_line()
193193

194194
for declaration in sorted_decls:
195-
declarations.emit_lines(*declaration.decl)
195+
if declaration.needs_extern:
196+
declarations.emit_lines(
197+
'extern {}'.format(declaration.decl[0]), *declaration.decl[1:])
198+
emitter.emit_lines(*declaration.decl)
199+
else:
200+
declarations.emit_lines(*declaration.decl)
196201

197202
for module_name, module in self.modules:
198203
self.declare_finals(module.final_names, declarations)
199204
for cl in module.classes:
200-
generate_class_type_decl(cl, declarations)
205+
generate_class_type_decl(cl, emitter, declarations)
201206
for fn in module.functions:
202207
generate_function_declaration(fn, declarations)
203208

@@ -400,6 +405,7 @@ def declare_global(self, type_spaced: str, name: str,
400405
set(),
401406
['{}{};'.format(type_spaced, name)],
402407
defn,
408+
needs_extern=True,
403409
)
404410

405411
def declare_internal_globals(self, module_name: str, emitter: Emitter) -> None:
@@ -427,7 +433,7 @@ def declare_imports(self, imps: Iterable[str], emitter: Emitter) -> None:
427433
def declare_finals(self, final_names: Iterable[Tuple[str, RType]], emitter: Emitter) -> None:
428434
for name, typ in final_names:
429435
static_name = emitter.static_name(name, 'final')
430-
emitter.emit_line('{}{};'.format(emitter.ctype_spaced(typ), static_name))
436+
emitter.emit_line('extern {}{};'.format(emitter.ctype_spaced(typ), static_name))
431437

432438
def define_finals(self, final_names: Iterable[Tuple[str, RType]], emitter: Emitter) -> None:
433439
for name, typ in final_names:

mypyc/test/test_run.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import platform
66
import subprocess
77
import contextlib
8-
import unittest
98
import sys
109
from typing import List, Iterator, Optional
1110

@@ -67,10 +66,6 @@ class TestRun(MypycDataSuite):
6766
multi_file = False
6867

6968
def run_case(self, testcase: DataDrivenTestCase) -> None:
70-
# FIXME: This is broken and I am investigating -sully
71-
if self.multi_file and sys.platform == 'darwin':
72-
pytest.skip("multifile tests are broken on macOS")
73-
7469
bench = testcase.config.getoption('--bench', False) and 'Benchmark' in testcase.name
7570

7671
# setup.py wants to be run from the root directory of the package, which we accommodate

test-data/module-output.test

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,18 +125,24 @@ int CPyGlobalsInit(void)
125125
CPyModule *CPyStatic_module_internal = NULL;
126126
CPyModule *CPyStatic_builtins_module_internal = NULL;
127127

128-
== __native.h ==
129-
#include <Python.h>
130-
#include <CPy.h>
131-
132-
int CPyGlobalsInit(void);
133-
134128
PyObject *CPyStatic_unicode_0;
135129
CPyModule *CPyStatic_module_internal;
136130
CPyModule *CPyStatic_module;
137131
PyObject *CPyStatic_globals;
138132
CPyModule *CPyStatic_builtins_module_internal;
139133
CPyModule *CPyStatic_builtins_module;
134+
== __native.h ==
135+
#include <Python.h>
136+
#include <CPy.h>
137+
138+
int CPyGlobalsInit(void);
139+
140+
extern PyObject *CPyStatic_unicode_0;
141+
extern CPyModule *CPyStatic_module_internal;
142+
extern CPyModule *CPyStatic_module;
143+
extern PyObject *CPyStatic_globals;
144+
extern CPyModule *CPyStatic_builtins_module_internal;
145+
extern CPyModule *CPyStatic_builtins_module;
140146
CPyTagged CPyDef_f(CPyTagged cpy_r_x);
141147
PyObject *CPyPy_f(PyObject *self, PyObject *args, PyObject *kw);
142148
char CPyDef___top_level__(void);

0 commit comments

Comments
 (0)