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