Skip to content

Commit

Permalink
Fix kernel version selection using build machine rather than target
Browse files Browse the repository at this point in the history
This fix uses the 'setup.cfg' file used by setuptools. It reads an
Adafruit-specific section that is ignored by setuptools.

The reason for using the 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, library build
systems can modify a config file instead of the source directly.

Fixes adafruit#256
  • Loading branch information
eigendude committed Apr 17, 2019
1 parent b867bbf commit 44e7c74
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 4 deletions.
80 changes: 80 additions & 0 deletions Adafruit_Config.py
@@ -0,0 +1,80 @@
# Adafruit Setuptools Configuration Module
# SPDX-License-Identifier: MIT

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

class Adafruit_Config:
"""
Class to obtain configuration provided by the setup.cfg configuration file
used by setuptools.
Adafruit-specific information is provided under an [adafruit] section.
The reason for this interface is that the command-line to the setup.py
script is out of our control and entirely up to the installer. The
setup.cfg file was added to setuptools as a useful middle-ground between
modifying the setup script and the command-line to the setup script.
"""

#############################################################################
# Parameters
#############################################################################

# Setuptools config file
SETUPTOOLS_CONFIG_FILE = 'setup.cfg'

# Section name in cfg file
SECTION_NAME = 'adafruit'

# Key name for specifying kernel >= 4.1.0 targets. Value must be 'true' to
# target the newer kernel interface or 'false' to target the older one.
KERNEL41_KEY = 'kernel41'

#############################################################################
# Interface
#############################################################################

def __init__(self):
"""
Initialize the configuration object.
"""
self._metadata = self.metadata_from_setupcfg(self.SECTION_NAME)

def is_kernel41(self):
"""
Return True if we are targeting a kernel >= 4.1.0, or False otherwise.
Defaults to True if unknown.
"""
return False if self.get_cfg_value(self.KERNEL41_KEY) == 'false' else True

#############################################################################
# Helpers
#############################################################################

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

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

return dict(cfgparser.items(section_name))

@classmethod
def get_cfg_fname(cls):
"""
Get the name of the setuptools cfg file.
"""
return cls.SETUPTOOLS_CONFIG_FILE
6 changes: 6 additions & 0 deletions setup.cfg
@@ -0,0 +1,6 @@
# Adafruit Setuptools Configuration Interface
[adafruit]

# Set this to 'true' if targeting a kernel >= 4.1.0 to use the new kernel
# interface, or 'false' to use the old interface.
kernel41 = true
8 changes: 4 additions & 4 deletions setup.py
@@ -1,3 +1,5 @@
from Adafruit_Config import Adafruit_Config

try:
from overlays import builder
builder.compile()
Expand All @@ -7,16 +9,14 @@

import distribute_setup
import io
import sys
import platform
distribute_setup.use_setuptools()
from setuptools import setup, Extension, find_packages

open_as_utf8 = lambda x: io.open(x, encoding='utf-8')

kernel = platform.release()
config = Adafruit_Config()

if kernel >= '4.1.0':
if config.is_kernel41():
kernel41 = [('BBBVERSION41', None)]
else:
kernel41 = None
Expand Down

0 comments on commit 44e7c74

Please sign in to comment.