Simon Glass | b4cf5f1 | 2019-10-31 07:42:59 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Simon Glass | cf2064d | 2017-05-27 07:38:11 -0600 | [diff] [blame] | 2 | |
| 3 | """ |
| 4 | setup.py file for SWIG libfdt |
| 5 | Copyright (C) 2017 Google, Inc. |
| 6 | Written by Simon Glass <sjg@chromium.org> |
| 7 | |
| 8 | SPDX-License-Identifier: GPL-2.0+ BSD-2-Clause |
| 9 | |
| 10 | Files to be built into the extension are provided in SOURCES |
| 11 | C flags to use are provided in CPPFLAGS |
| 12 | Object file directory is provided in OBJDIR |
| 13 | Version is provided in VERSION |
| 14 | |
| 15 | If these variables are not given they are parsed from the Makefiles. This |
| 16 | allows this script to be run stand-alone, e.g.: |
| 17 | |
| 18 | ./pylibfdt/setup.py install [--prefix=...] |
| 19 | """ |
| 20 | |
| 21 | from distutils.core import setup, Extension |
| 22 | import os |
| 23 | import re |
| 24 | import sys |
| 25 | |
| 26 | # Decodes a Makefile assignment line into key and value (and plus for +=) |
| 27 | RE_KEY_VALUE = re.compile('(?P<key>\w+) *(?P<plus>[+])?= *(?P<value>.*)$') |
| 28 | |
| 29 | |
| 30 | def ParseMakefile(fname): |
| 31 | """Parse a Makefile to obtain its variables. |
| 32 | |
| 33 | This collects variable assigments of the form: |
| 34 | |
| 35 | VAR = value |
| 36 | VAR += more |
| 37 | |
| 38 | It does not pick out := assignments, as these are not needed here. It does |
| 39 | handle line continuation. |
| 40 | |
| 41 | Returns a dict: |
| 42 | key: Variable name (e.g. 'VAR') |
| 43 | value: Variable value (e.g. 'value more') |
| 44 | """ |
| 45 | makevars = {} |
| 46 | with open(fname) as fd: |
| 47 | prev_text = '' # Continuation text from previous line(s) |
| 48 | for line in fd.read().splitlines(): |
| 49 | if line and line[-1] == '\\': # Deal with line continuation |
| 50 | prev_text += line[:-1] |
| 51 | continue |
| 52 | elif prev_text: |
| 53 | line = prev_text + line |
| 54 | prev_text = '' # Continuation is now used up |
| 55 | m = RE_KEY_VALUE.match(line) |
| 56 | if m: |
| 57 | value = m.group('value') or '' |
| 58 | key = m.group('key') |
| 59 | |
| 60 | # Appending to a variable inserts a space beforehand |
| 61 | if 'plus' in m.groupdict() and key in makevars: |
| 62 | makevars[key] += ' ' + value |
| 63 | else: |
| 64 | makevars[key] = value |
| 65 | return makevars |
| 66 | |
| 67 | def GetEnvFromMakefiles(): |
| 68 | """Scan the Makefiles to obtain the settings we need. |
| 69 | |
| 70 | This assumes that this script is being run from the top-level directory, |
| 71 | not the pylibfdt directory. |
| 72 | |
| 73 | Returns: |
| 74 | Tuple with: |
| 75 | List of swig options |
| 76 | Version string |
| 77 | List of files to build |
| 78 | List of extra C preprocessor flags needed |
| 79 | Object directory to use (always '') |
| 80 | """ |
| 81 | basedir = os.path.dirname(os.path.dirname(os.path.abspath(sys.argv[0]))) |
| 82 | swig_opts = ['-I%s' % basedir] |
| 83 | makevars = ParseMakefile(os.path.join(basedir, 'Makefile')) |
| 84 | version = '%s.%s.%s' % (makevars['VERSION'], makevars['PATCHLEVEL'], |
| 85 | makevars['SUBLEVEL']) |
| 86 | makevars = ParseMakefile(os.path.join(basedir, 'libfdt', 'Makefile.libfdt')) |
| 87 | files = makevars['LIBFDT_SRCS'].split() |
| 88 | files = [os.path.join(basedir, 'libfdt', fname) for fname in files] |
| 89 | files.append('pylibfdt/libfdt.i') |
| 90 | cflags = ['-I%s' % basedir, '-I%s/libfdt' % basedir] |
| 91 | objdir = '' |
| 92 | return swig_opts, version, files, cflags, objdir |
| 93 | |
| 94 | |
| 95 | progname = sys.argv[0] |
| 96 | files = os.environ.get('SOURCES', '').split() |
| 97 | cflags = os.environ.get('CPPFLAGS', '').split() |
| 98 | objdir = os.environ.get('OBJDIR') |
| 99 | version = os.environ.get('VERSION') |
Simon Glass | 7e91b102 | 2017-05-27 07:38:16 -0600 | [diff] [blame] | 100 | swig_opts = os.environ.get('SWIG_OPTS', '').split() |
Simon Glass | cf2064d | 2017-05-27 07:38:11 -0600 | [diff] [blame] | 101 | |
| 102 | # If we were called directly rather than through our Makefile (which is often |
| 103 | # the case with Python module installation), read the settings from the |
| 104 | # Makefile. |
Simon Glass | 7e91b102 | 2017-05-27 07:38:16 -0600 | [diff] [blame] | 105 | if not all((swig_opts, version, files, cflags, objdir)): |
Simon Glass | cf2064d | 2017-05-27 07:38:11 -0600 | [diff] [blame] | 106 | swig_opts, version, files, cflags, objdir = GetEnvFromMakefiles() |
| 107 | |
| 108 | libfdt_module = Extension( |
| 109 | '_libfdt', |
| 110 | sources = files, |
| 111 | extra_compile_args = cflags, |
| 112 | swig_opts = swig_opts, |
| 113 | ) |
| 114 | |
| 115 | setup( |
| 116 | name='libfdt', |
| 117 | version= version, |
| 118 | author='Simon Glass <sjg@chromium.org>', |
| 119 | description='Python binding for libfdt', |
| 120 | ext_modules=[libfdt_module], |
| 121 | package_dir={'': objdir}, |
| 122 | py_modules=['pylibfdt/libfdt'], |
| 123 | ) |