+# Unfortunately setup.py (or actually the Python distutil implementation)
+# puts files into the same directory as the .i file. We cannot touch the source
+# directory, so we copy the .i file into the tools/ build subdirectory before
+# calling setup. This directory is safe to write to. This ensures that we get
+# all three files in $(obj)/tools: _libfdt.so, libfdt.py and libfdt_wrap.c
+# The latter is a temporary file which we could actually remove.
+tools/_libfdt.so: $(LIBFDT_SRCS) $(LIBFDT_SWIG)
+ cp $(LIBFDT_SWIG) tools/.
+ unset CC; \
+ unset CROSS_COMPILE; \
+ LDFLAGS="$(HOSTLDFLAGS)" CFLAGS= VERSION="u-boot-$(UBOOTVERSION)" \
+ CPPFLAGS="$(_hostc_flags)" OBJDIR=tools \
+ SOURCES="$(LIBFDT_SRCS) tools/libfdt.i" \
+ SWIG_OPTS="-I$(srctree)/lib/libfdt -I$(srctree)/lib" \
+ $(libfdt_tree)/pylibfdt/setup.py --quiet build_ext \
+ --build-lib tools
+
+# Build a libfdt Python module if swig is available
+# Use 'sudo apt-get install swig libpython-dev' to enable this
+hostprogs-y += \
+ $(if $(shell which swig 2> /dev/null),_libfdt_legacy.so)
+_libfdt_legacy.so-sharedobjs += $(LIBFDT_OBJS)
+libfdt:
+
+tools/_libfdt_legacy.so: $(patsubst %.o,%.c,$(LIBFDT_OBJS)) \
+ tools/libfdt_legacy_wrap.c
+ LDFLAGS="$(HOSTLDFLAGS)" CFLAGS= ${PYTHON} $(srctree)/lib/libfdt/setup.py \
+ "$(_hostc_flags)" $^
+ mv _libfdt_legacy.so $@
+
+tools/libfdt_legacy_wrap.c: $(srctree)/lib/libfdt/libfdt_legacy.swig
+ swig -python -o $@ $<
+