setup.py 3.8 KB

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