Skip to content

Commit a754b1d

Browse files
committed
Add option to build x86_64 only on macOS
1 parent 34515b7 commit a754b1d

File tree

4 files changed

+96
-6
lines changed

4 files changed

+96
-6
lines changed

cibuildwheel/macos.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
from .util import prepare_command, get_build_verbosity_extra_flags
1212

1313

14-
def get_python_configurations(build_selector):
14+
def get_python_configurations(build_selector, platform='intel'):
1515
PythonConfiguration = namedtuple('PythonConfiguration', ['version', 'identifier', 'url'])
1616
python_configurations = [
17-
PythonConfiguration(version='2.7', identifier='cp27-macosx_10_6_intel', url='https://www.python.org/ftp/python/2.7.17/python-2.7.17-macosx10.6.pkg'),
18-
PythonConfiguration(version='3.5', identifier='cp35-macosx_10_6_intel', url='https://www.python.org/ftp/python/3.5.4/python-3.5.4-macosx10.6.pkg'),
19-
PythonConfiguration(version='3.6', identifier='cp36-macosx_10_6_intel', url='https://www.python.org/ftp/python/3.6.8/python-3.6.8-macosx10.6.pkg'),
20-
PythonConfiguration(version='3.7', identifier='cp37-macosx_10_6_intel', url='https://www.python.org/ftp/python/3.7.5/python-3.7.5-macosx10.6.pkg'),
17+
PythonConfiguration(version='2.7', identifier='cp27-macosx_10_6_' + platform, url='https://www.python.org/ftp/python/2.7.17/python-2.7.17-macosx10.6.pkg'),
18+
PythonConfiguration(version='3.5', identifier='cp35-macosx_10_6_' + platform, url='https://www.python.org/ftp/python/3.5.4/python-3.5.4-macosx10.6.pkg'),
19+
PythonConfiguration(version='3.6', identifier='cp36-macosx_10_6_' + platform, url='https://www.python.org/ftp/python/3.6.8/python-3.6.8-macosx10.6.pkg'),
20+
PythonConfiguration(version='3.7', identifier='cp37-macosx_10_6_' + platform, url='https://www.python.org/ftp/python/3.7.5/python-3.7.5-macosx10.6.pkg'),
2121
PythonConfiguration(version='3.8', identifier='cp38-macosx_10_9_x86_64', url='https://www.python.org/ftp/python/3.8.0/python-3.8.0-macosx10.9.pkg'),
2222
]
2323

@@ -26,7 +26,9 @@ def get_python_configurations(build_selector):
2626

2727

2828
def build(project_dir, output_dir, test_command, test_requires, test_extras, before_build, build_verbosity, build_selector, environment):
29-
python_configurations = get_python_configurations(build_selector)
29+
platform = os.environ.get('CIBW_MACOS_PLATFORM', 'intel')
30+
assert platform in ['intel', 'x86_64']
31+
python_configurations = get_python_configurations(build_selector, platform)
3032
get_pip_url = 'https://bootstrap.pypa.io/get-pip.py'
3133
get_pip_script = '/tmp/get-pip.py'
3234

@@ -102,6 +104,15 @@ def call(args, env=None, cwd=None, shell=False):
102104
shutil.rmtree('/tmp/delocated_wheel')
103105
os.makedirs('/tmp/delocated_wheel')
104106

107+
# setup target platform
108+
current_platform = subprocess.check_output(['python', '-c', 'import distutils.util; print(distutils.util.get_platform())'], env=env)
109+
if sys.version_info[0] >= 3:
110+
current_platform = current_platform.decode('utf8')
111+
current_platform = current_platform.strip()
112+
if platform == 'x86_64' and current_platform.endswith('intel'):
113+
env['_PYTHON_HOST_PLATFORM'] = current_platform.replace('intel', 'x86_64') # cross-compilation platform override
114+
env['ARCHFLAGS'] = '-arch x86_64' # https://github.com/python/cpython/blob/a5ed2fe0eedefa1649aa93ee74a0bafc8e628a10/Lib/_osx_support.py#L260
115+
105116
# run the before_build command
106117
if before_build:
107118
before_build_prepared = prepare_command(before_build, project=abs_project_dir)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import os, pytest
2+
import utils
3+
4+
5+
def test():
6+
project_dir = os.path.dirname(__file__)
7+
8+
if utils.platform != 'macos':
9+
pytest.skip('the test is only relevant to the macOS build')
10+
11+
# build the wheels
12+
utils.cibuildwheel_run(project_dir, add_env={
13+
'CIBW_MACOS_PLATFORM': 'x86_64',
14+
})
15+
16+
# also check that we got the right wheels
17+
expected_wheels = [w.replace('intel', 'x86_64') for w in utils.expected_wheels('spam', '0.1.0')]
18+
actual_wheels = os.listdir('wheelhouse')
19+
assert set(actual_wheels) == set(expected_wheels)

test/09_macos_x86_64/setup.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import os
2+
3+
from setuptools import setup, Extension
4+
5+
if os.environ.get('CIBUILDWHEEL', '0') != '1':
6+
raise Exception('CIBUILDWHEEL environment variable is not set to 1')
7+
8+
setup(
9+
name="spam",
10+
ext_modules=[Extension('spam', sources=['spam.c'])],
11+
version="0.1.0",
12+
)

test/09_macos_x86_64/spam.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include <Python.h>
2+
3+
static PyObject *
4+
spam_system(PyObject *self, PyObject *args)
5+
{
6+
const char *command;
7+
int sts;
8+
9+
if (!PyArg_ParseTuple(args, "s", &command))
10+
return NULL;
11+
sts = system(command);
12+
return PyLong_FromLong(sts);
13+
}
14+
15+
/* Module initialization */
16+
17+
#if PY_MAJOR_VERSION >= 3
18+
#define MOD_INIT(name) PyMODINIT_FUNC PyInit_##name(void)
19+
#define MOD_DEF(m, name, doc, methods, module_state_size) \
20+
static struct PyModuleDef moduledef = { \
21+
PyModuleDef_HEAD_INIT, name, doc, module_state_size, methods, }; \
22+
m = PyModule_Create(&moduledef);
23+
#define MOD_RETURN(m) return m;
24+
#else
25+
#define MOD_INIT(name) PyMODINIT_FUNC init##name(void)
26+
#define MOD_DEF(m, name, doc, methods, module_state_size) \
27+
m = Py_InitModule3(name, methods, doc);
28+
#define MOD_RETURN(m) return;
29+
#endif
30+
31+
static PyMethodDef module_methods[] = {
32+
{"system", (PyCFunction)spam_system, METH_VARARGS,
33+
"Execute a shell command."},
34+
{NULL} /* Sentinel */
35+
};
36+
37+
MOD_INIT(spam)
38+
{
39+
PyObject* m;
40+
41+
MOD_DEF(m,
42+
"spam",
43+
"Example module",
44+
module_methods,
45+
-1)
46+
47+
MOD_RETURN(m)
48+
}

0 commit comments

Comments
 (0)