Skip to content

Commit

Permalink
Fix using build machine compiler flags rather than target
Browse files Browse the repository at this point in the history
This fix uses the 'setup.cfg' file used by setuptools to define parameters
for cross-compiling.

The reason for using this config file is that command-line parameters can't
be passed to setup.py, as the command-line is ideally opaque to the
installer. 'setup.cfg' was added to setuptools as a useful middle-ground
between modifying 'setup.py' and the command line. Now, build systems can
modify a config file instead of the source directly.
  • Loading branch information
eigendude committed May 19, 2019
1 parent e0d8456 commit 3de44d8
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 1 deletion.
70 changes: 70 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#
# ICRAR - International Centre for Radio Astronomy Research
# (c) UWA - The University of Western Australia, 2014
# Copyright by UWA (in the framework of the ICRAR)
# All rights reserved
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307 USA
#

try:
import configparser
except ImportError:
# Python 2 compatibility
import ConfigParser as configparser

class setup_config(object):
"""
Class to obtain configuration provided by the setup.cfg file used by
setuptools.
"""

# Setuptools configuration file
SETUPTOOLS_CONFIG_FILE = 'setup.cfg'

# Cross-compiling section
BUILD_SECTION_NAME = 'build_info'

# Key name for specifying the target machine
MACHINE_KEY = 'machine'

def __init__(self):
"""Initialize the configuration object"""
self._metadata = self._metadata_from_setupcfg(self.BUILD_SECTION_NAME)

def machine(self):
"""Return the target machine name, or None if unknown"""
return self._get_cfg_value(self.MACHINE_KEY)

def _get_cfg_value(self, key):
"""
Get a value associated with a key in the build section. Returns the
value, or None if unknown.
"""
return self._metadata.get(key, None)

@classmethod
def _metadata_from_setupcfg(cls, section_name):
"""Read the section of setup.cfg and return it as a dict"""
cfgparser = configparser.ConfigParser()
cfgparser.read(cls._get_cfg_filename())

return dict(cfgparser.items(section_name))

@classmethod
def _get_cfg_filename(cls):
"""Get the file name of the setuptools cfg file"""
return cls.SETUPTOOLS_CONFIG_FILE
6 changes: 6 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Cross-compiling configuration interface
[build_info]

# If cross-compiling, uncomment this and set it to the target machine name
# (e.g. output of uname -m). If unset, the host machine type will be detected.
#machine = x86_64
11 changes: 10 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
# Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307 USA
#
from config import setup_config

import glob
import platform

Expand All @@ -33,7 +35,14 @@
language='c',
sources=['_crc32c.c', 'crc32c_sw.c'],
include_dirs=['.'])
is_intel = platform.machine() in ['x86_64', 'AMD64']

config = setup_config()

machine = config.machine()
if not machine:
machine = platform.machine()

is_intel = machine in ['x86_64', 'AMD64']

def get_extra_compile_args():
# msvc is treated specially; otherwise we assume it's a unix compiler
Expand Down

0 comments on commit 3de44d8

Please sign in to comment.