setup.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #!/usr/bin/env python3
  2. # SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
  3. """
  4. setup.py file for SWIG libfdt
  5. Copyright (C) 2017 Google, Inc.
  6. Written by Simon Glass <sjg@chromium.org>
  7. This script is modified from the upstream version, to fit in with the U-Boot
  8. build system.
  9. Files to be built into the extension are provided in SOURCES
  10. C flags to use are provided in CPPFLAGS
  11. Object file directory is provided in OBJDIR
  12. Version is provided in VERSION
  13. If these variables are not given they are parsed from the Makefiles. This
  14. allows this script to be run stand-alone, e.g.:
  15. ./pylibfdt/setup.py install [--prefix=...]
  16. """
  17. from setuptools import setup, Extension
  18. from setuptools.command.build_py import build_py as _build_py
  19. import os
  20. import re
  21. import sys
  22. try:
  23. from setuptools import sic
  24. except ImportError:
  25. pass
  26. srcdir = os.path.dirname(__file__)
  27. with open(os.path.join(srcdir, "../README"), "r") as fh:
  28. long_description = fh.read()
  29. # Decodes a Makefile assignment line into key and value (and plus for +=)
  30. RE_KEY_VALUE = re.compile('(?P<key>\w+) *(?P<plus>[+])?= *(?P<value>.*)$')
  31. def get_top_builddir():
  32. if '--top-builddir' in sys.argv:
  33. index = sys.argv.index('--top-builddir')
  34. sys.argv.pop(index)
  35. return sys.argv.pop(index)
  36. else:
  37. return os.path.join(srcdir, '..')
  38. top_builddir = get_top_builddir()
  39. def ParseMakefile(fname):
  40. """Parse a Makefile to obtain its variables.
  41. This collects variable assigments of the form:
  42. VAR = value
  43. VAR += more
  44. It does not pick out := assignments, as these are not needed here. It does
  45. handle line continuation.
  46. Returns a dict:
  47. key: Variable name (e.g. 'VAR')
  48. value: Variable value (e.g. 'value more')
  49. """
  50. makevars = {}
  51. with open(fname) as fd:
  52. prev_text = '' # Continuation text from previous line(s)
  53. for line in fd.read().splitlines():
  54. if line and line[-1] == '\\': # Deal with line continuation
  55. prev_text += line[:-1]
  56. continue
  57. elif prev_text:
  58. line = prev_text + line
  59. prev_text = '' # Continuation is now used up
  60. m = RE_KEY_VALUE.match(line)
  61. if m:
  62. value = m.group('value') or ''
  63. key = m.group('key')
  64. # Appending to a variable inserts a space beforehand
  65. if 'plus' in m.groupdict() and key in makevars:
  66. makevars[key] += ' ' + value
  67. else:
  68. makevars[key] = value
  69. return makevars
  70. def GetEnvFromMakefiles():
  71. """Scan the Makefiles to obtain the settings we need.
  72. This assumes that this script is being run from the top-level directory,
  73. not the pylibfdt directory.
  74. Returns:
  75. Tuple with:
  76. List of swig options
  77. Version string
  78. List of files to build
  79. List of extra C preprocessor flags needed
  80. Object directory to use (always '')
  81. """
  82. basedir = os.path.dirname(os.path.dirname(os.path.abspath(sys.argv[0])))
  83. swig_opts = ['-I%s' % basedir]
  84. makevars = ParseMakefile(os.path.join(basedir, 'Makefile'))
  85. version = '%s.%s.%s' % (makevars['VERSION'], makevars['PATCHLEVEL'],
  86. makevars['SUBLEVEL'])
  87. makevars = ParseMakefile(os.path.join(basedir, 'libfdt', 'Makefile.libfdt'))
  88. files = makevars['LIBFDT_SRCS'].split()
  89. files = [os.path.join(basedir, 'libfdt', fname) for fname in files]
  90. files.append('libfdt.i')
  91. cflags = ['-I%s' % basedir, '-I%s/libfdt' % basedir]
  92. objdir = ''
  93. return swig_opts, version, files, cflags, objdir
  94. progname = sys.argv[0]
  95. files = os.environ.get('SOURCES', '').split()
  96. cflags = os.environ.get('CPPFLAGS', '').split()
  97. objdir = os.environ.get('OBJDIR')
  98. try:
  99. version = sic(os.environ.get('VERSION'))
  100. except:
  101. version = os.environ.get('VERSION')
  102. swig_opts = os.environ.get('SWIG_OPTS', '').split()
  103. # If we were called directly rather than through our Makefile (which is often
  104. # the case with Python module installation), read the settings from the
  105. # Makefile.
  106. if not all((swig_opts, version, files, cflags, objdir)):
  107. swig_opts, version, files, cflags, objdir = GetEnvFromMakefiles()
  108. libfdt_module = Extension(
  109. '_libfdt',
  110. sources=files,
  111. include_dirs=[os.path.join(srcdir, 'libfdt')],
  112. library_dirs=[os.path.join(top_builddir, 'libfdt')],
  113. swig_opts=swig_opts,
  114. )
  115. class build_py(_build_py):
  116. def run(self):
  117. self.run_command("build_ext")
  118. return super().run()
  119. setup(
  120. name='libfdt',
  121. version=version,
  122. cmdclass = {'build_py' : build_py},
  123. author='Simon Glass',
  124. author_email='sjg@chromium.org',
  125. description='Python binding for libfdt',
  126. ext_modules=[libfdt_module],
  127. package_dir={'': objdir},
  128. py_modules=['libfdt'],
  129. long_description=long_description,
  130. long_description_content_type="text/plain",
  131. url="https://git.kernel.org/pub/scm/utils/dtc/dtc.git",
  132. license="BSD",
  133. license_files=["GPL", "BSD-2-Clause"],
  134. classifiers=[
  135. "Programming Language :: Python :: 3",
  136. "License :: OSI Approved :: BSD License",
  137. "License :: OSI Approved :: GNU General Public License v2 or later (GPLv2+)",
  138. "Operating System :: OS Independent",
  139. ],
  140. )