4 setup.py file for SWIG libfdt
5 Copyright (C) 2017 Google, Inc.
8 SPDX-License-Identifier: GPL-2.0+ BSD-2-Clause
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
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.:
18 ./pylibfdt/setup.py install [--prefix=...]
21 from distutils.core import setup, Extension
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>.*)$')
30 def ParseMakefile(fname):
31 """Parse a Makefile to obtain its variables.
33 This collects variable assigments of the form:
38 It does not pick out := assignments, as these are not needed here. It does
39 handle line continuation.
42 key: Variable name (e.g. 'VAR')
43 value: Variable value (e.g. 'value more')
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]
53 line = prev_text + line
54 prev_text = '' # Continuation is now used up
55 m = RE_KEY_VALUE.match(line)
57 value = m.group('value') or ''
60 # Appending to a variable inserts a space beforehand
61 if 'plus' in m.groupdict() and key in makevars:
62 makevars[key] += ' ' + value
67 def GetEnvFromMakefiles():
68 """Scan the Makefiles to obtain the settings we need.
70 This assumes that this script is being run from the top-level directory,
71 not the pylibfdt directory.
77 List of files to build
78 List of extra C preprocessor flags needed
79 Object directory to use (always '')
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'],
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]
92 return swig_opts, version, files, cflags, objdir
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')
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
105 if not all((version, files, cflags, objdir)):
106 swig_opts, version, files, cflags, objdir = GetEnvFromMakefiles()
108 libfdt_module = Extension(
111 extra_compile_args = cflags,
112 swig_opts = swig_opts,
119 description='Python binding for libfdt',
120 ext_modules=[libfdt_module],
121 package_dir={'': objdir},
122 py_modules=['pylibfdt/libfdt'],