From 3de44d86d89a99bae42e7a154b8d2a948ff6b19c Mon Sep 17 00:00:00 2001 From: Garrett Brown Date: Tue, 14 May 2019 21:44:53 -0700 Subject: [PATCH] Fix using build machine compiler flags rather than target 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. --- config.py | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ setup.cfg | 6 +++++ setup.py | 11 ++++++++- 3 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 config.py create mode 100644 setup.cfg diff --git a/config.py b/config.py new file mode 100644 index 0000000..aa03969 --- /dev/null +++ b/config.py @@ -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 diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..a17db43 --- /dev/null +++ b/setup.cfg @@ -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 diff --git a/setup.py b/setup.py index 506dd02..0232b6f 100644 --- a/setup.py +++ b/setup.py @@ -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 @@ -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