xref: /openbmc/u-boot/scripts/dtc/pylibfdt/setup.py (revision 12308b12)
1#!/usr/bin/env python2
2
3"""
4setup.py file for SWIG libfdt
5Copyright (C) 2017 Google, Inc.
6Written by Simon Glass <sjg@chromium.org>
7
8SPDX-License-Identifier:	GPL-2.0+ BSD-2-Clause
9
10Files to be built into the extension are provided in SOURCES
11C flags to use are provided in CPPFLAGS
12Object file directory is provided in OBJDIR
13Version is provided in VERSION
14
15If these variables are not given they are parsed from the Makefiles. This
16allows this script to be run stand-alone, e.g.:
17
18    ./pylibfdt/setup.py install [--prefix=...]
19"""
20
21from distutils.core import setup, Extension
22import os
23import re
24import sys
25
26# Decodes a Makefile assignment line into key and value (and plus for +=)
27RE_KEY_VALUE = re.compile('(?P<key>\w+) *(?P<plus>[+])?= *(?P<value>.*)$')
28
29
30def 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
67def 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
95progname = sys.argv[0]
96files = os.environ.get('SOURCES', '').split()
97cflags = os.environ.get('CPPFLAGS', '').split()
98objdir = os.environ.get('OBJDIR')
99version = os.environ.get('VERSION')
100swig_opts = os.environ.get('SWIG_OPTS', '').split()
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.
105if not all((swig_opts, version, files, cflags, objdir)):
106    swig_opts, version, files, cflags, objdir = GetEnvFromMakefiles()
107
108libfdt_module = Extension(
109    '_libfdt',
110    sources = files,
111    extra_compile_args = cflags,
112    swig_opts = swig_opts,
113)
114
115setup(
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)
124