setup.py: use a simplified custom version of CCompiler.has_function
authorAlexis S. L. Carvalho <alexis@cecm.usp.br>
Fri, 14 Mar 2008 01:45:17 -0300
changeset 6251 87053f0b0fad
parent 6250 bf0dd23f55fa
child 6252 f21f6de16d41
setup.py: use a simplified custom version of CCompiler.has_function The original one doesn't remove its temporary files and even creates a temporary file in the CWD.
setup.py
--- a/setup.py	Thu Mar 13 23:45:36 2008 +0100
+++ b/setup.py	Fri Mar 14 01:45:17 2008 -0300
@@ -10,6 +10,8 @@
     raise SystemExit, "Mercurial requires python 2.3 or later."
 
 import os
+import shutil
+import tempfile
 from distutils.core import setup, Extension
 from distutils.command.install_data import install_data
 from distutils.ccompiler import new_compiler
@@ -18,6 +20,26 @@
 
 extra = {}
 
+# simplified version of distutils.ccompiler.CCompiler.has_function
+# that actually removes its temporary files.
+def has_function(cc, funcname):
+    tmpdir = tempfile.mkdtemp(prefix='hg-install-')
+    try:
+        fname = os.path.join(tmpdir, 'funcname.c')
+        f = open(fname, 'w')
+        f.write('int main(void) {\n')
+        f.write('    %s();\n' % funcname)
+        f.write('}\n')
+        f.close()
+        try:
+            objects = cc.compile([fname])
+            cc.link_executable(objects, os.path.join(tmpdir, "a.out"))
+        except:
+            return False
+        return True
+    finally:
+        shutil.rmtree(tmpdir)
+
 # py2exe needs to be installed to work
 try:
     import py2exe
@@ -70,7 +92,7 @@
         # The inotify extension is only usable with Linux 2.6 kernels.
         # You also need a reasonably recent C library.
         cc = new_compiler()
-        if cc.has_function('inotify_add_watch'):
+        if has_function(cc, 'inotify_add_watch'):
             ext_modules.append(Extension('hgext.inotify.linux._inotify',
                                          ['hgext/inotify/linux/_inotify.c']))
             packages.extend(['hgext.inotify', 'hgext.inotify.linux'])