From 4e28fb9cfe22e46e314d0eff285f4629f9735b2d Mon Sep 17 00:00:00 2001
From: Andrea Shepard <andrea@persephoneslair.org>
Date: Fri, 11 Nov 2016 05:06:24 +0000
Subject: [PATCH] Pass LLVM includes and defines to python bindings

---
 SConstruct                     | 25 +++++++++++++++++++++++++
 src/bindings/python/SConscript |  9 ++++++++-
 2 files changed, 33 insertions(+), 1 deletion(-)

diff --git a/SConstruct b/SConstruct
index ee475784..43ba87ea 100644
--- a/SConstruct
+++ b/SConstruct
@@ -190,6 +190,29 @@ class LLVMConfigSanitizer:
         env.MergeFlags(filtered_cmd, unique)
 llvm_config_sanitizer = LLVMConfigSanitizer()
 
+# LLVM defines, which the python bindings need
+try:
+    llvm_config_cflags = subprocess.Popen('%s --cflags' % env["LLVM_CONFIG"], \
+                                          shell=True, \
+                                          stdin=subprocess.PIPE, stdout=subprocess.PIPE).communicate()
+    flags = llvm_config_cflags[0].split()
+    # get just the -D ones
+    p = re.compile("^-D(.*)$")
+    llvm_defines = [p.match(flag).group(1) for flag in flags if p.match(flag)]
+except:
+    print "%s failed. Make sure you have LLVM and clang installed." % env["LLVM_CONFIG"]
+    Exit(1)
+
+# Get the llvm includedir, which the python bindings need
+try:
+    llvm_config_includes = subprocess.Popen('%s --includedir' % env["LLVM_CONFIG"], \
+                                            shell=True, \
+                                            stdin=subprocess.PIPE, stdout=subprocess.PIPE).communicate()
+    llvm_includes = llvm_config_includes[0].splitlines()
+except:
+    print "%s failed. Make sure you have LLVM and clang installed." % env["LLVM_CONFIG"]
+    Exit(1)
+
 # 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()
@@ -229,6 +252,8 @@ Export('targets')
 Export('llvm_computed_shared_lib_name')
 Export('llvm_config_sanitizer')
 Export('llvm_config_version')
+Export('llvm_defines')
+Export('llvm_includes')
 Export('llvm_linkage_type_flag')
 Export('llvm_required_components')
 Export('llvm_system_libs_flag')
diff --git a/src/bindings/python/SConscript b/src/bindings/python/SConscript
index dac2d959..5619347c 100644
--- a/src/bindings/python/SConscript
+++ b/src/bindings/python/SConscript
@@ -1,13 +1,20 @@
 # -*- python -*-
 import os, os.path
 Import('env libhammer_shared testruns targets')
+Import('llvm_defines')
+Import('llvm_includes')
 
 pythonenv = env.Clone(IMPLICIT_COMMAND_DEPENDENCIES = 0)
 
 swig = pythonenv.Command("hammer.i", "../swig/hammer.i", Copy("$TARGET", "$SOURCE"))
 setup = ['setup.py']
 pydir = os.path.join(env['BUILD_BASE'], 'src/bindings/python')
-libhammer_python = pythonenv.Command(['hammer.py', 'hammer_wrap.c'], [swig, setup], 'python ' + os.path.join(pydir, 'setup.py') + ' build_ext --inplace')
+define_list = ','.join(llvm_defines)
+inc_list = ' '.join(['-I' + e for e in llvm_includes])
+swig_opt_list = '-DHAMMER_INTERNAL__NO_STDARG_H -I../../ ' + inc_list
+libhammer_python = pythonenv.Command(['hammer.py', 'hammer_wrap.c'], [swig, setup], \
+    'python ' + os.path.join(pydir, 'setup.py') + ' build_ext --inplace ' + inc_list + \
+    ' --define=\"' + define_list + '\" --swig-opts=\"' + swig_opt_list + '\"')
 Default(libhammer_python)
 
 pytestenv = pythonenv.Clone()
-- 
GitLab