Simon Glass | 7ccca83 | 2019-10-31 07:42:59 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Simon Glass | 85feff3 | 2022-07-30 20:57:11 -0600 | [diff] [blame] | 2 | # SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause) |
Simon Glass | a13d902 | 2017-05-27 07:38:11 -0600 | [diff] [blame] | 3 | |
| 4 | """ |
| 5 | setup.py file for SWIG libfdt |
| 6 | Copyright (C) 2017 Google, Inc. |
| 7 | Written by Simon Glass <sjg@chromium.org> |
| 8 | |
Simon Glass | 85feff3 | 2022-07-30 20:57:11 -0600 | [diff] [blame] | 9 | This script is modified from the upstream version, to fit in with the U-Boot |
| 10 | build system. |
Simon Glass | a13d902 | 2017-05-27 07:38:11 -0600 | [diff] [blame] | 11 | |
| 12 | Files to be built into the extension are provided in SOURCES |
| 13 | C flags to use are provided in CPPFLAGS |
| 14 | Object file directory is provided in OBJDIR |
| 15 | Version is provided in VERSION |
| 16 | |
| 17 | If these variables are not given they are parsed from the Makefiles. This |
| 18 | allows this script to be run stand-alone, e.g.: |
| 19 | |
| 20 | ./pylibfdt/setup.py install [--prefix=...] |
| 21 | """ |
| 22 | |
Simon Glass | 85feff3 | 2022-07-30 20:57:11 -0600 | [diff] [blame] | 23 | from setuptools import setup, Extension |
| 24 | from setuptools.command.build_py import build_py as _build_py |
Marek Vasut | 9675f80 | 2022-12-15 23:37:59 +0100 | [diff] [blame] | 25 | from setuptools.extern.packaging import version |
Simon Glass | a13d902 | 2017-05-27 07:38:11 -0600 | [diff] [blame] | 26 | import os |
| 27 | import re |
| 28 | import sys |
| 29 | |
Marek Vasut | 9675f80 | 2022-12-15 23:37:59 +0100 | [diff] [blame] | 30 | # Disable version normalization |
| 31 | version.Version = version.LegacyVersion |
| 32 | |
Simon Glass | 85feff3 | 2022-07-30 20:57:11 -0600 | [diff] [blame] | 33 | srcdir = os.path.dirname(__file__) |
| 34 | |
| 35 | with open(os.path.join(srcdir, "../README"), "r") as fh: |
| 36 | long_description = fh.read() |
| 37 | |
Simon Glass | a13d902 | 2017-05-27 07:38:11 -0600 | [diff] [blame] | 38 | # Decodes a Makefile assignment line into key and value (and plus for +=) |
| 39 | RE_KEY_VALUE = re.compile('(?P<key>\w+) *(?P<plus>[+])?= *(?P<value>.*)$') |
| 40 | |
Simon Glass | 85feff3 | 2022-07-30 20:57:11 -0600 | [diff] [blame] | 41 | def get_top_builddir(): |
| 42 | if '--top-builddir' in sys.argv: |
| 43 | index = sys.argv.index('--top-builddir') |
| 44 | sys.argv.pop(index) |
| 45 | return sys.argv.pop(index) |
| 46 | else: |
| 47 | return os.path.join(srcdir, '..') |
| 48 | |
| 49 | top_builddir = get_top_builddir() |
Simon Glass | a13d902 | 2017-05-27 07:38:11 -0600 | [diff] [blame] | 50 | |
| 51 | def ParseMakefile(fname): |
| 52 | """Parse a Makefile to obtain its variables. |
| 53 | |
| 54 | This collects variable assigments of the form: |
| 55 | |
| 56 | VAR = value |
| 57 | VAR += more |
| 58 | |
| 59 | It does not pick out := assignments, as these are not needed here. It does |
| 60 | handle line continuation. |
| 61 | |
| 62 | Returns a dict: |
| 63 | key: Variable name (e.g. 'VAR') |
| 64 | value: Variable value (e.g. 'value more') |
| 65 | """ |
| 66 | makevars = {} |
| 67 | with open(fname) as fd: |
| 68 | prev_text = '' # Continuation text from previous line(s) |
| 69 | for line in fd.read().splitlines(): |
| 70 | if line and line[-1] == '\\': # Deal with line continuation |
| 71 | prev_text += line[:-1] |
| 72 | continue |
| 73 | elif prev_text: |
| 74 | line = prev_text + line |
| 75 | prev_text = '' # Continuation is now used up |
| 76 | m = RE_KEY_VALUE.match(line) |
| 77 | if m: |
| 78 | value = m.group('value') or '' |
| 79 | key = m.group('key') |
| 80 | |
| 81 | # Appending to a variable inserts a space beforehand |
| 82 | if 'plus' in m.groupdict() and key in makevars: |
| 83 | makevars[key] += ' ' + value |
| 84 | else: |
| 85 | makevars[key] = value |
| 86 | return makevars |
| 87 | |
| 88 | def GetEnvFromMakefiles(): |
| 89 | """Scan the Makefiles to obtain the settings we need. |
| 90 | |
| 91 | This assumes that this script is being run from the top-level directory, |
| 92 | not the pylibfdt directory. |
| 93 | |
| 94 | Returns: |
| 95 | Tuple with: |
| 96 | List of swig options |
| 97 | Version string |
| 98 | List of files to build |
| 99 | List of extra C preprocessor flags needed |
| 100 | Object directory to use (always '') |
| 101 | """ |
| 102 | basedir = os.path.dirname(os.path.dirname(os.path.abspath(sys.argv[0]))) |
| 103 | swig_opts = ['-I%s' % basedir] |
| 104 | makevars = ParseMakefile(os.path.join(basedir, 'Makefile')) |
| 105 | version = '%s.%s.%s' % (makevars['VERSION'], makevars['PATCHLEVEL'], |
| 106 | makevars['SUBLEVEL']) |
| 107 | makevars = ParseMakefile(os.path.join(basedir, 'libfdt', 'Makefile.libfdt')) |
| 108 | files = makevars['LIBFDT_SRCS'].split() |
| 109 | files = [os.path.join(basedir, 'libfdt', fname) for fname in files] |
Simon Glass | 85feff3 | 2022-07-30 20:57:11 -0600 | [diff] [blame] | 110 | files.append('libfdt.i') |
Simon Glass | a13d902 | 2017-05-27 07:38:11 -0600 | [diff] [blame] | 111 | cflags = ['-I%s' % basedir, '-I%s/libfdt' % basedir] |
| 112 | objdir = '' |
| 113 | return swig_opts, version, files, cflags, objdir |
| 114 | |
| 115 | |
| 116 | progname = sys.argv[0] |
| 117 | files = os.environ.get('SOURCES', '').split() |
| 118 | cflags = os.environ.get('CPPFLAGS', '').split() |
| 119 | objdir = os.environ.get('OBJDIR') |
| 120 | version = os.environ.get('VERSION') |
Simon Glass | 3d0b999 | 2017-05-27 07:38:16 -0600 | [diff] [blame] | 121 | swig_opts = os.environ.get('SWIG_OPTS', '').split() |
Simon Glass | a13d902 | 2017-05-27 07:38:11 -0600 | [diff] [blame] | 122 | |
| 123 | # If we were called directly rather than through our Makefile (which is often |
| 124 | # the case with Python module installation), read the settings from the |
| 125 | # Makefile. |
Simon Glass | 3d0b999 | 2017-05-27 07:38:16 -0600 | [diff] [blame] | 126 | if not all((swig_opts, version, files, cflags, objdir)): |
Simon Glass | a13d902 | 2017-05-27 07:38:11 -0600 | [diff] [blame] | 127 | swig_opts, version, files, cflags, objdir = GetEnvFromMakefiles() |
| 128 | |
| 129 | libfdt_module = Extension( |
| 130 | '_libfdt', |
Simon Glass | 85feff3 | 2022-07-30 20:57:11 -0600 | [diff] [blame] | 131 | sources=files, |
| 132 | include_dirs=[os.path.join(srcdir, 'libfdt')], |
| 133 | library_dirs=[os.path.join(top_builddir, 'libfdt')], |
| 134 | swig_opts=swig_opts, |
Simon Glass | a13d902 | 2017-05-27 07:38:11 -0600 | [diff] [blame] | 135 | ) |
| 136 | |
Simon Glass | 85feff3 | 2022-07-30 20:57:11 -0600 | [diff] [blame] | 137 | class build_py(_build_py): |
| 138 | def run(self): |
| 139 | self.run_command("build_ext") |
| 140 | return super().run() |
| 141 | |
Simon Glass | a13d902 | 2017-05-27 07:38:11 -0600 | [diff] [blame] | 142 | setup( |
| 143 | name='libfdt', |
Simon Glass | 85feff3 | 2022-07-30 20:57:11 -0600 | [diff] [blame] | 144 | version=version, |
| 145 | cmdclass = {'build_py' : build_py}, |
| 146 | author='Simon Glass', |
| 147 | author_email='sjg@chromium.org', |
Simon Glass | a13d902 | 2017-05-27 07:38:11 -0600 | [diff] [blame] | 148 | description='Python binding for libfdt', |
| 149 | ext_modules=[libfdt_module], |
| 150 | package_dir={'': objdir}, |
Simon Glass | 85feff3 | 2022-07-30 20:57:11 -0600 | [diff] [blame] | 151 | py_modules=['libfdt'], |
| 152 | |
| 153 | long_description=long_description, |
| 154 | long_description_content_type="text/plain", |
| 155 | url="https://git.kernel.org/pub/scm/utils/dtc/dtc.git", |
| 156 | license="BSD", |
| 157 | license_files=["GPL", "BSD-2-Clause"], |
| 158 | |
| 159 | classifiers=[ |
| 160 | "Programming Language :: Python :: 3", |
| 161 | "License :: OSI Approved :: BSD License", |
| 162 | "License :: OSI Approved :: GNU General Public License v2 or later (GPLv2+)", |
| 163 | "Operating System :: OS Independent", |
| 164 | ], |
| 165 | |
Simon Glass | a13d902 | 2017-05-27 07:38:11 -0600 | [diff] [blame] | 166 | ) |