Skip to content

Commit

Permalink
Allow architecture selection
Browse files Browse the repository at this point in the history
  • Loading branch information
asfaltboy committed Dec 16, 2020
1 parent 90a14b9 commit b023416
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 11 deletions.
26 changes: 23 additions & 3 deletions cibuildwheel/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
parse_environment,
)
from cibuildwheel.util import (
Architecture,
BuildOptions,
BuildSelector,
DependencyConstraints,
Expand Down Expand Up @@ -67,6 +68,22 @@ def main() -> None:
run in Windows, and it will build and test for all versions of
Python. Default: auto.
''')

parser.add_argument(
'-a',
'--architectures',
choices=[arch.value for arch in Architecture],
default=[],
help='''
Comma-separated list of CPU architectures to build for.
If unspecified, builds the architectures natively supported
on this machine. Set this option to build an architecture
via emulation, for example, using binfmt_misc and qemu.
''',
action="extend",
nargs="+",
type=Architecture,
)
parser.add_argument('--output-dir',
default=os.environ.get('CIBW_OUTPUT_DIR', 'wheelhouse'),
help='Destination folder for the wheels.')
Expand Down Expand Up @@ -169,7 +186,7 @@ def main() -> None:
exit(2)

if args.print_build_identifiers:
print_build_identifiers(platform, build_selector)
print_build_identifiers(platform, build_selector, args.architectures)
exit(0)

manylinux_images: Optional[Dict[str, str]] = None
Expand Down Expand Up @@ -202,6 +219,7 @@ def main() -> None:
manylinux_images[build_platform] = image

build_options = BuildOptions(
architectures=args.architectures,
package_dir=package_dir,
output_dir=output_dir,
test_command=test_command,
Expand Down Expand Up @@ -284,10 +302,12 @@ def print_preamble(platform: str, build_options: BuildOptions) -> None:
print('\nHere we go!\n')


def print_build_identifiers(platform: str, build_selector: BuildSelector) -> None:
def print_build_identifiers(
platform: str, build_selector: BuildSelector, architectures: List[Architecture]
) -> None:
python_configurations: List[Any] = []
if platform == 'linux':
python_configurations = cibuildwheel.linux.get_python_configurations(build_selector)
python_configurations = cibuildwheel.linux.get_python_configurations(build_selector, architectures)
elif platform == 'windows':
python_configurations = cibuildwheel.windows.get_python_configurations(build_selector)
elif platform == 'macos':
Expand Down
22 changes: 14 additions & 8 deletions cibuildwheel/linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@

from .docker_container import DockerContainer
from .logger import log
from .util import (BuildOptions, BuildSelector, NonPlatformWheelError,
get_build_verbosity_extra_flags, prepare_command)
from .util import (
Architecture, BuildOptions, BuildSelector, NonPlatformWheelError,
get_build_verbosity_extra_flags, prepare_command,
)


re_pattern = re.compile(r'[cp]p\d{2}-manylinux_(\w*)')


def matches_platform(identifier: str, supported_platforms: List[str]) -> bool:
def matches_platform(identifier: str, supported_platforms: List[Architecture]) -> bool:
matched_architecture = re_pattern.search(identifier)
id_architecture = matched_architecture.group(1) if matched_architecture else ''
# x86_64 machines can run i686 docker containers
id_architecture = id_architecture if id_architecture == 'i686' else 'x86_64'
return id_architecture in supported_platforms


Expand All @@ -34,7 +34,9 @@ def path(self):
return PurePath(self.path_str)


def get_python_configurations(build_selector: BuildSelector) -> List[PythonConfiguration]:
def get_python_configurations(
build_selector: BuildSelector, architectures: List[Architecture]
) -> List[PythonConfiguration]:
python_configurations = [
PythonConfiguration(version='2.7', identifier='cp27-manylinux_x86_64', path_str='/opt/python/cp27-cp27m'),
PythonConfiguration(version='2.7', identifier='cp27-manylinux_x86_64', path_str='/opt/python/cp27-cp27mu'),
Expand Down Expand Up @@ -71,7 +73,11 @@ def get_python_configurations(build_selector: BuildSelector) -> List[PythonConfi
]

# skip builds as required
target_archs = [platform.machine()]
target_archs = architectures or [Architecture(platform.machine())]
# x86_64 machines can run i686 docker containers
if Architecture.i686 not in target_archs and Architecture.x86_64 in target_archs:
target_archs.append(Architecture.i686)
print("matching platforms to architectures: ", target_archs)
return [
c for c in python_configurations
if matches_platform(c.identifier, target_archs)
Expand All @@ -90,7 +96,7 @@ def build(options: BuildOptions) -> None:
exit(2)

assert options.manylinux_images is not None
python_configurations = get_python_configurations(options.build_selector)
python_configurations = get_python_configurations(options.build_selector, options.architectures)
platforms = [
('cp', 'manylinux_x86_64', options.manylinux_images['x86_64']),
('cp', 'manylinux_i686', options.manylinux_images['i686']),
Expand Down
9 changes: 9 additions & 0 deletions cibuildwheel/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,16 @@ def __repr__(self):
return f'{self.__class__.__name__}{self.base_file_path!r})'


class Architecture(str, Enum):
x86_64 = 'x86_64'
i686 = 'i686'
aarch64 = 'aarch64'
ppc64le = 'ppc64le'
s390x = 's390x'


class BuildOptions(NamedTuple):
architectures: List[Architecture]
package_dir: Path
output_dir: Path
build_selector: BuildSelector
Expand Down

0 comments on commit b023416

Please sign in to comment.