From ee17697cb25a51ec50492e1d198b6d10cb843b98 Mon Sep 17 00:00:00 2001
From: Andrea Shepard <andrea@persephoneslair.org>
Date: Fri, 11 Nov 2016 03:35:29 +0000
Subject: [PATCH] Fix pkg-config and add LLVM dependencies to it

---
 SConstruct      | 62 ++++++++++++++++++++++++++++++++++++++-----------
 libhammer.pc.in |  3 ++-
 src/SConscript  |  1 -
 3 files changed, 50 insertions(+), 16 deletions(-)

diff --git a/SConstruct b/SConstruct
index dc220c1d..ee475784 100644
--- a/SConstruct
+++ b/SConstruct
@@ -11,6 +11,8 @@ import subprocess
 vars = Variables(None, ARGUMENTS)
 vars.Add(PathVariable('DESTDIR', "Root directory to install in (useful for packaging scripts)", None, PathVariable.PathIsDirCreate))
 vars.Add(PathVariable('prefix', "Where to install in the FHS", "/usr/local", PathVariable.PathAccept))
+vars.Add(PathVariable('libdir', "Where to install libraries", None, PathVariable.PathAccept))
+vars.Add(PathVariable('includedir', "Where to install headers", None, PathVariable.PathAccept))
 vars.Add(ListVariable('bindings', 'Language bindings to build', 'none', ['cpp', 'dotnet', 'perl', 'php', 'python', 'ruby']))
 
 tools = ['default', 'scanreplace']
@@ -45,12 +47,20 @@ if 'DESTDIR' in env:
         print >>sys.stderr, "--!!--    %s" % (calcInstallPath("$prefix"),)
 
 env['LLVM_CONFIG'] = "llvm-config"
-env['libpath'] = calcInstallPath("$prefix", "lib")
-env['incpath'] = calcInstallPath("$prefix", "include", "hammer")
-env['parsersincpath'] = calcInstallPath("$prefix", "include", "hammer", "parsers")
-env['backendsincpath'] = calcInstallPath("$prefix", "include", "hammer", "backends")
-env['pkgconfigpath'] = calcInstallPath("$prefix", "lib", "pkgconfig")
-env.ScanReplace('libhammer.pc.in')
+if 'includedir' in env:
+    env['incpath'] = calcInstallPath("$includedir", "hammer")
+else:
+    env['includedir'] = os.path.abspath(os.path.join(*map(env.subst, ["$prefix", "include"])))
+    env['incpath'] = calcInstallPath("$prefix", "include", "hammer")
+if 'libdir' in env:
+    env['libpath'] = calcInstallPath("$libdir")
+    env['pkgconfigpath'] = calcInstallPath("$libdir", "pkgconfig")
+else:
+    env['libpath'] = calcInstallPath("$prefix", "lib")
+    env['pkgconfigpath'] = calcInstallPath("$prefix", "lib", "pkgconfig")
+    env['libdir'] = os.path.abspath(os.path.join(*map(env.subst, ["$prefix", "lib"])))
+env['parsersincpath'] = calcInstallPath("$includedir", "hammer", "parsers")
+env['backendsincpath'] = calcInstallPath("$includedir", "hammer", "backends")
 
 env.MergeFlags("-std=gnu11 -Wno-unused-parameter -Wno-attributes -Wno-unused-variable -Wall -Wextra -Werror")
 
@@ -116,14 +126,6 @@ env["ENV"].update(x for x in os.environ.items() if x[0].startswith("CCC_"))
 #rootpath = env['ROOTPATH'] = os.path.abspath('.')
 #env.Append(CPPPATH=os.path.join('#', "hammer"))
 
-testruns = []
-
-targets = ["$libpath",
-           "$incpath",
-           "$parsersincpath",
-           "$backendsincpath",
-           "$pkgconfigpath"]
-
 # Set up LLVM config stuff to export
 
 # some llvm versions are old and will not work; some require --system-libs
@@ -188,6 +190,38 @@ class LLVMConfigSanitizer:
         env.MergeFlags(filtered_cmd, unique)
 llvm_config_sanitizer = LLVMConfigSanitizer()
 
+# This goes here so we already know all the LLVM crap
+# Make a fresh environment to parse the config into, to read out just LLVM stuff
+llvm_dummy_env = Environment()
+# Get LLVM stuff into LIBS/LDFLAGS
+llvm_dummy_env.ParseConfig('%s --ldflags %s %s %s' % \
+                           (env["LLVM_CONFIG"], llvm_system_libs_flag, llvm_linkage_type_flag, \
+                            llvm_required_components), \
+                           function=llvm_config_sanitizer.sanitize)
+# Get the right -l lines in
+if llvm_use_shared:
+    if llvm_use_computed_shared_lib_name:
+        llvm_dummy_env.Append(LIBS=[llvm_computed_shared_lib_name, ])
+    else:
+        llvm_dummy_env.ParseConfig('%s %s --libs %s' % \
+                                   (env["LLVM_CONFIG"], llvm_linkage_type_flag, llvm_required_components), \
+                                   function=llvm_config_sanitizer.sanitize)
+llvm_dummy_env.Append(LIBS=['stdc++', ], )
+
+env['llvm_libdir_flags'] = llvm_dummy_env.subst('$_LIBDIRFLAGS')
+env['llvm_lib_flags'] = llvm_dummy_env.subst('$_LIBFLAGS')
+pkgconfig = env.ScanReplace('libhammer.pc.in')
+Default(pkgconfig)
+env.Install("$pkgconfigpath", pkgconfig)
+
+testruns = []
+
+targets = ["$libpath",
+           "$incpath",
+           "$parsersincpath",
+           "$backendsincpath",
+           "$pkgconfigpath"]
+
 Export('env')
 Export('testruns')
 Export('targets')
diff --git a/libhammer.pc.in b/libhammer.pc.in
index c1e3c862..8d751308 100644
--- a/libhammer.pc.in
+++ b/libhammer.pc.in
@@ -1,10 +1,11 @@
 prefix=${prefix}
 exec_prefix=${prefix}
 includedir=${prefix}/include
-libdir=${exec_prefix}/lib
+libdir=${libdir}
 
 Name: libhammer
 Description: The Hammer parsing library
 Version: 0.9.0
 Cflags: -I${includedir}
 Libs: -L${libdir} -lhammer
+Libs.private: ${llvm_libdir_flags} ${llvm_lib_flags}
diff --git a/src/SConscript b/src/SConscript
index d40e6525..80d96bf4 100644
--- a/src/SConscript
+++ b/src/SConscript
@@ -120,7 +120,6 @@ env.Install("$libpath", [libhammer_static, libhammer_shared])
 env.Install("$incpath", dist_headers)
 env.Install("$parsersincpath", parsers_headers)
 env.Install("$backendsincpath", backends_headers)
-env.Install("$pkgconfigpath", "../../../libhammer.pc")
 
 testenv = env.Clone()
 testenv.Append(LIBS=['hammer'], LIBPATH=['.'])
-- 
GitLab