From: Hilko Bengen <bengen@debian.org>
Date: Sat, 19 Mar 2016 22:31:25 +0100
Subject: Link system tsk (statically),
 talloc (dynamically) instead of embedding

Forwarded: not-needed

Note to future maintainers (or myself): This patch needs updating
every once in a while as upstream's setup.py is changed.

Upstream's general logic seems to be to have Sleuthkit sources
unpacked into a subdirectory sleuthkit/, run configure without any
extras, and build selected C or C++ files from sleuthkit/tsk as well
as the C or C++ files in the pytsk root directory. Also, talloc is
built from source.

We instead link libtsk statically (bot everything else dynamically),
by passing "-Wl,-Bstatic -ltsk -Wl,-Bdynamic" to the linker.

We want to dynamically link libtalloc as found on the system as well
as some libtsk dependencies, currently: libafflib, libewf, libvhdi,
libvmdk, libsqlite3, libz. (cf. output of "objdump -x libtsk.so | grep NEEDED").

Apparently, libtsk dependencies need to be passed as extra_link_args
after -ltsk.
---
 setup.py | 33 ++++++++++++++-------------------
 1 file changed, 14 insertions(+), 19 deletions(-)

diff --git a/setup.py b/setup.py
index a86ded0..9e9f02a 100755
--- a/setup.py
+++ b/setup.py
@@ -196,10 +196,11 @@ class BuildExtCommand(build_ext):
 
   def run(self):
     compiler = new_compiler(compiler=self.compiler)
-    # pylint: disable=attribute-defined-outside-init
-    self.configure_source(compiler)
 
-    libtsk_path = os.path.join("sleuthkit", "tsk")
+    self.define = [("HAVE_TSK_LIBTSK_H", "")]
+    self.libraries = ["stdc++"]
+
+    libtsk_path = "/usr/include/tsk"
 
     if not os.access("pytsk3.cpp", os.R_OK):
       # Generate the Python binding code (pytsk3.cpp).
@@ -221,7 +222,7 @@ class BuildExtCommand(build_ext):
 class SDistCommand(sdist):
   """Custom handler for generating source dist."""
   def run(self):
-    libtsk_path = os.path.join("sleuthkit", "tsk")
+    libtsk_path = "/usr/include/tsk"
 
     # sleuthkit submodule is not there, probably because this has been
     # freshly checked out.
@@ -342,7 +343,7 @@ class UpdateCommand(Command):
           line = "version = {0:s}\n".format(self.version)
         file_object.write(line)
 
-    libtsk_path = os.path.join("sleuthkit", "tsk")
+    libtsk_path = "/usr/include/tsk"
 
     # Generate the Python binding code (pytsk3.cpp).
     libtsk_header_files = [
@@ -374,29 +375,23 @@ class ProjectBuilder(object):
 
     # The args for the extension builder.
     self.extension_args = {
-        "include_dirs": ["talloc", self._libtsk_path, "sleuthkit", "."],
-        "library_dirs": []}
+        "include_dirs": ["."],
+        "library_dirs": [],
+        "libraries": [ "talloc" ],
+        "extra_link_args": [
+          "-Wl,-Bstatic", "-ltsk", "-Wl,-Bdynamic",
+          "-lafflib", "-lewf", "-lvhdi", "-lvmdk", "-lsqlite3", "-lz",
+        ]}
 
     # The sources to build.
     self._source_files = [
-        "class.cpp", "error.cpp", "tsk3.cpp", "pytsk3.cpp", "talloc/talloc.c"]
+        "class.cpp", "error.cpp", "tsk3.cpp", "pytsk3.cpp"]
 
     # Path to the top of the unpacked sleuthkit sources.
     self._sleuthkit_path = "sleuthkit"
 
   def build(self):
     """Build everything."""
-    # Fetch all c and cpp files from the subdirs to compile.
-    extension_file = os.path.join(
-        self._libtsk_path, "auto", "guid.cpp")
-    self._source_files.append(extension_file)
-
-    for library_name in self._sub_library_names:
-      for extension in ("*.c", "*.cpp"):
-        extension_glob = os.path.join(
-            self._libtsk_path, library_name, extension)
-        self._source_files.extend(glob.glob(extension_glob))
-
     # Sort the soure files to make sure they are in consistent order when
     # building.
     source_files = sorted(self._source_files)
