1*daae0a01SMasahiro Yamada#!/usr/bin/env python2 281536652SMasahiro Yamada 381536652SMasahiro Yamada""" 481536652SMasahiro Yamadasetup.py file for SWIG libfdt 581536652SMasahiro YamadaCopyright (C) 2017 Google, Inc. 681536652SMasahiro YamadaWritten by Simon Glass <sjg@chromium.org> 781536652SMasahiro Yamada 881536652SMasahiro YamadaSPDX-License-Identifier: GPL-2.0+ BSD-2-Clause 981536652SMasahiro Yamada 1081536652SMasahiro YamadaFiles to be built into the extension are provided in SOURCES 1181536652SMasahiro YamadaC flags to use are provided in CPPFLAGS 1281536652SMasahiro YamadaObject file directory is provided in OBJDIR 1381536652SMasahiro YamadaVersion is provided in VERSION 1481536652SMasahiro Yamada 1581536652SMasahiro YamadaIf these variables are not given they are parsed from the Makefiles. This 1681536652SMasahiro Yamadaallows this script to be run stand-alone, e.g.: 1781536652SMasahiro Yamada 1881536652SMasahiro Yamada ./pylibfdt/setup.py install [--prefix=...] 1981536652SMasahiro Yamada""" 2081536652SMasahiro Yamada 2181536652SMasahiro Yamadafrom distutils.core import setup, Extension 2281536652SMasahiro Yamadaimport os 2381536652SMasahiro Yamadaimport re 2481536652SMasahiro Yamadaimport sys 2581536652SMasahiro Yamada 2681536652SMasahiro Yamada# Decodes a Makefile assignment line into key and value (and plus for +=) 2781536652SMasahiro YamadaRE_KEY_VALUE = re.compile('(?P<key>\w+) *(?P<plus>[+])?= *(?P<value>.*)$') 2881536652SMasahiro Yamada 2981536652SMasahiro Yamada 3081536652SMasahiro Yamadadef ParseMakefile(fname): 3181536652SMasahiro Yamada """Parse a Makefile to obtain its variables. 3281536652SMasahiro Yamada 3381536652SMasahiro Yamada This collects variable assigments of the form: 3481536652SMasahiro Yamada 3581536652SMasahiro Yamada VAR = value 3681536652SMasahiro Yamada VAR += more 3781536652SMasahiro Yamada 3881536652SMasahiro Yamada It does not pick out := assignments, as these are not needed here. It does 3981536652SMasahiro Yamada handle line continuation. 4081536652SMasahiro Yamada 4181536652SMasahiro Yamada Returns a dict: 4281536652SMasahiro Yamada key: Variable name (e.g. 'VAR') 4381536652SMasahiro Yamada value: Variable value (e.g. 'value more') 4481536652SMasahiro Yamada """ 4581536652SMasahiro Yamada makevars = {} 4681536652SMasahiro Yamada with open(fname) as fd: 4781536652SMasahiro Yamada prev_text = '' # Continuation text from previous line(s) 4881536652SMasahiro Yamada for line in fd.read().splitlines(): 4981536652SMasahiro Yamada if line and line[-1] == '\\': # Deal with line continuation 5081536652SMasahiro Yamada prev_text += line[:-1] 5181536652SMasahiro Yamada continue 5281536652SMasahiro Yamada elif prev_text: 5381536652SMasahiro Yamada line = prev_text + line 5481536652SMasahiro Yamada prev_text = '' # Continuation is now used up 5581536652SMasahiro Yamada m = RE_KEY_VALUE.match(line) 5681536652SMasahiro Yamada if m: 5781536652SMasahiro Yamada value = m.group('value') or '' 5881536652SMasahiro Yamada key = m.group('key') 5981536652SMasahiro Yamada 6081536652SMasahiro Yamada # Appending to a variable inserts a space beforehand 6181536652SMasahiro Yamada if 'plus' in m.groupdict() and key in makevars: 6281536652SMasahiro Yamada makevars[key] += ' ' + value 6381536652SMasahiro Yamada else: 6481536652SMasahiro Yamada makevars[key] = value 6581536652SMasahiro Yamada return makevars 6681536652SMasahiro Yamada 6781536652SMasahiro Yamadadef GetEnvFromMakefiles(): 6881536652SMasahiro Yamada """Scan the Makefiles to obtain the settings we need. 6981536652SMasahiro Yamada 7081536652SMasahiro Yamada This assumes that this script is being run from the top-level directory, 7181536652SMasahiro Yamada not the pylibfdt directory. 7281536652SMasahiro Yamada 7381536652SMasahiro Yamada Returns: 7481536652SMasahiro Yamada Tuple with: 7581536652SMasahiro Yamada List of swig options 7681536652SMasahiro Yamada Version string 7781536652SMasahiro Yamada List of files to build 7881536652SMasahiro Yamada List of extra C preprocessor flags needed 7981536652SMasahiro Yamada Object directory to use (always '') 8081536652SMasahiro Yamada """ 8181536652SMasahiro Yamada basedir = os.path.dirname(os.path.dirname(os.path.abspath(sys.argv[0]))) 8281536652SMasahiro Yamada swig_opts = ['-I%s' % basedir] 8381536652SMasahiro Yamada makevars = ParseMakefile(os.path.join(basedir, 'Makefile')) 8481536652SMasahiro Yamada version = '%s.%s.%s' % (makevars['VERSION'], makevars['PATCHLEVEL'], 8581536652SMasahiro Yamada makevars['SUBLEVEL']) 8681536652SMasahiro Yamada makevars = ParseMakefile(os.path.join(basedir, 'libfdt', 'Makefile.libfdt')) 8781536652SMasahiro Yamada files = makevars['LIBFDT_SRCS'].split() 8881536652SMasahiro Yamada files = [os.path.join(basedir, 'libfdt', fname) for fname in files] 8981536652SMasahiro Yamada files.append('pylibfdt/libfdt.i') 9081536652SMasahiro Yamada cflags = ['-I%s' % basedir, '-I%s/libfdt' % basedir] 9181536652SMasahiro Yamada objdir = '' 9281536652SMasahiro Yamada return swig_opts, version, files, cflags, objdir 9381536652SMasahiro Yamada 9481536652SMasahiro Yamada 9581536652SMasahiro Yamadaprogname = sys.argv[0] 9681536652SMasahiro Yamadafiles = os.environ.get('SOURCES', '').split() 9781536652SMasahiro Yamadacflags = os.environ.get('CPPFLAGS', '').split() 9881536652SMasahiro Yamadaobjdir = os.environ.get('OBJDIR') 9981536652SMasahiro Yamadaversion = os.environ.get('VERSION') 10081536652SMasahiro Yamadaswig_opts = os.environ.get('SWIG_OPTS', '').split() 10181536652SMasahiro Yamada 10281536652SMasahiro Yamada# If we were called directly rather than through our Makefile (which is often 10381536652SMasahiro Yamada# the case with Python module installation), read the settings from the 10481536652SMasahiro Yamada# Makefile. 10581536652SMasahiro Yamadaif not all((swig_opts, version, files, cflags, objdir)): 10681536652SMasahiro Yamada swig_opts, version, files, cflags, objdir = GetEnvFromMakefiles() 10781536652SMasahiro Yamada 10881536652SMasahiro Yamadalibfdt_module = Extension( 10981536652SMasahiro Yamada '_libfdt', 11081536652SMasahiro Yamada sources = files, 11181536652SMasahiro Yamada extra_compile_args = cflags, 11281536652SMasahiro Yamada swig_opts = swig_opts, 11381536652SMasahiro Yamada) 11481536652SMasahiro Yamada 11581536652SMasahiro Yamadasetup( 11681536652SMasahiro Yamada name='libfdt', 11781536652SMasahiro Yamada version= version, 11881536652SMasahiro Yamada author='Simon Glass <sjg@chromium.org>', 11981536652SMasahiro Yamada description='Python binding for libfdt', 12081536652SMasahiro Yamada ext_modules=[libfdt_module], 12181536652SMasahiro Yamada package_dir={'': objdir}, 12281536652SMasahiro Yamada py_modules=['pylibfdt/libfdt'], 12381536652SMasahiro Yamada) 124