Subject: [UFC-dev] Patch for pleasing mac buildbot

From: Johan Hake <[email-masked]>
Date: Tue, 13 Jan 2009 20:50:47 +0100

Attached is a patch that hopefully will please the mac buildbot.

I parsed the output from pkg-config boost to figure out the installation of 
boost. If no boost is installed will the extension module be compiled without 
shared_ptr support.

Johan
# HG changeset patch
# User "Johan Hake <[email-masked]>"
# Date 1231875999 -3600
# Node ID 1d17bc37be4b35fafe6bbe9983c0cf913cbe5371
# Parent  49d6988c66c81d455353771e39dae57b98f7321f
Make the compilation of the extenstion module more robust.
 - Now includes the directory of the boost installation while compiling
   extension module
 - If no boost installation is found compile the extension module without
   shared_ptr support.

diff -r 49d6988c66c8 -r 1d17bc37be4b setup.py
--- a/setup.py	Tue Jan 13 11:44:38 2009 +0100
+++ b/setup.py	Tue Jan 13 20:46:39 2009 +0100
@@ -5,6 +5,15 @@
 from os.path import join as pjoin
 from os.path import pardir
 import sys, os
+
+# Taken from http://ivory.idyll.org/blog/mar-07/replacing-commands-with-subprocess
+from subprocess import Popen, PIPE, STDOUT
+def get_status_output(cmd, input=None, cwd=None, env=None):
+    pipe = Popen(cmd, shell=True, cwd=cwd, env=env, stdout=PIPE, stderr=STDOUT)
+    (output, errout) = pipe.communicate(input=input)
+    assert not errout
+    status = pipe.returncode
+    return (status, output)
 
 # Version number
 major = 1
@@ -31,10 +40,37 @@
 # FIXME: better way for this? ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 file.close()
 
+# Checking for boost installation uses pkg-config
+# If no boost installation is found compiled the ufc extension module
+# without shared_ptr support
+
+# Set pkg-config to always include system flags
+env = os.environ.copy()
+try:
+    assert env["PKG_CONFIG_ALLOW_SYSTEM_CFLAGS"] == "0"
+except:
+    env["PKG_CONFIG_ALLOW_SYSTEM_CFLAGS"] = "1"
+
+# Check if pkg-config is installed
+result, output = get_status_output("pkg-config --version ")
+if result != 0: 
+    raise OSError("The pkg-config package is not installed on the system.")
+
+# Check if boost is installed
+result, output = get_status_output("pkg-config --exists boost")
+if result == 0:
+    boost_include_dir = [get_status_output("pkg-config --cflags-only-I boost",env=env)[1][2:]]
+    swig_cmd = "swig -python -c++ -fcompact -O -I. -small ufc.i"
+else:
+    print "*** Warning: Boost was not found. Python extension module compiled without shared_ptr support"
+    boost_include_dir = []
+    swig_cmd = "swig -python -c++ -fcompact -O -I. -DNO_SHARED_PTR -small ufc.i"
+    
 # Run swig on the ufc.i file
-# FIXME: Add a way to set NO_SHARED_PTR in swig command
 os.chdir(pjoin("src","ufc"))
-os.system("swig -python -c++ -fcompact -O -I. -small ufc.i")
+print "Running swig command"
+print swig_cmd
+os.system(swig_cmd)
 os.chdir(os.pardir); os.chdir(os.pardir)
 sources = [pjoin("src","ufc","ufc_wrap.cxx")]
 
@@ -52,5 +88,5 @@
                     (pjoin("include","swig"),[pjoin("src","ufc","ufc.i")])],
       ext_modules = [Extension("_ufc",
                      sources,
-                     include_dirs=[pjoin("src","ufc")]
+                     include_dirs=[pjoin("src","ufc")] + boost_include_dir
                                )])
diff -r 49d6988c66c8 -r 1d17bc37be4b src/ufc/ufc.i
--- a/src/ufc/ufc.i	Tue Jan 13 11:44:38 2009 +0100
+++ b/src/ufc/ufc.i	Tue Jan 13 20:46:39 2009 +0100
@@ -2,13 +2,21 @@
 
 %{
 #include <ufc.h>
+%}
+
+#ifndef NO_SHARED_PTR
+// Handle shared_ptr only available for swig version >= 1.3.34
+#if SWIG_VERSION >= 0x010334
+%{
 // If using std::tr1::shared_ptr comment out this line
 #include <boost/shared_ptr.hpp>
 // Un comment these lines to use std::tr1, only works with swig version  >= 1.3.37
 //#include <tr1/memory>
 %}
+#endif
+#endif
 
-
+#ifndef NO_SHARED_PTR
 // Handle shared_ptr only available for swig version >= 1.3.34
 #if SWIG_VERSION >= 0x010334
 // Un comment these lines to use std::tr1, only works with swig version  >= 1.3.37
@@ -22,7 +30,8 @@
 SWIG_SHARED_PTR(cell_integral,ufc::cell_integral)
 SWIG_SHARED_PTR(exterior_facet_integral,ufc::exterior_facet_integral)
 SWIG_SHARED_PTR(interior_facet_integral,ufc::interior_facet_integral)
+#endif
+#endif
 
 %include ufc.h
 
-#endif