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