From f32aa8f296dc09ea8341d00702e61a4c0e3e9dbc Mon Sep 17 00:00:00 2001
From: Alexander Neundorf <neundorf@kde.org>
Date: Thu, 2 Aug 2007 15:48:51 -0400
Subject: ENH: make the python modules usable for C and C++ and only write the
 header if it has changed

Alex
---
 Modules/FindPythonLibs.cmake | 40 +++++++++++++++++++++++++++++++---------
 1 file changed, 31 insertions(+), 9 deletions(-)

diff --git a/Modules/FindPythonLibs.cmake b/Modules/FindPythonLibs.cmake
index 5e7da5a..e7eac31 100644
--- a/Modules/FindPythonLibs.cmake
+++ b/Modules/FindPythonLibs.cmake
@@ -117,22 +117,44 @@ MACRO(PYTHON_WRITE_MODULES_HEADER _filename)
   GET_FILENAME_COMPONENT(_name "${_filename}" NAME)
   STRING(REPLACE "." "_" _name "${_name}")
   STRING(TOUPPER ${_name} _name)
-  FILE(WRITE ${_filename} "/*Created by cmake, do not edit, changes will be lost*/\n")
-  FILE(APPEND ${_filename} "#ifndef ${_name}\n")
-  FILE(APPEND ${_filename} "#define ${_name}\n\n")
-  FILE(APPEND ${_filename} "#include <Python.h>\n\n")
+
+  SET(_filenameTmp "${_filename}.in")
+  FILE(WRITE ${_filenameTmp} "/*Created by cmake, do not edit, changes will be lost*/\n")
+  FILE(APPEND ${_filenameTmp} 
+"#ifndef ${_name}
+#define ${_name}
+
+#include <Python.h>
+
+#ifdef __cplusplus
+extern \"C\" {
+#endif /* __cplusplus */
+
+")
 
   FOREACH(_currentModule ${PY_STATIC_MODULES_LIST})
-    FILE(APPEND ${_filename} "extern void init${_currentModule}(void);\n\n")
+    FILE(APPEND ${_filenameTmp} "extern void init${PYTHON_MODULE_PREFIX}${_currentModule}(void);\n\n")
   ENDFOREACH(_currentModule ${PY_STATIC_MODULES_LIST})
 
+  FILE(APPEND ${_filenameTmp} 
+"#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+")
+
+
   FOREACH(_currentModule ${PY_STATIC_MODULES_LIST})
-    FILE(APPEND ${_filename} "int CMakeLoadPythonModule_${_currentModule}(void) \n{\n  char name[]=\"${PYTHON_MODULE_PREFIX}${_currentModule}\"; return PyImport_AppendInittab(name, init${PYTHON_MODULE_PREFIX}${_currentModule});\n}\n\n")
+    FILE(APPEND ${_filenameTmp} "int CMakeLoadPythonModule_${_currentModule}(void) \n{\n  char name[]=\"${PYTHON_MODULE_PREFIX}${_currentModule}\"; return PyImport_AppendInittab(name, init${PYTHON_MODULE_PREFIX}${_currentModule});\n}\n\n")
   ENDFOREACH(_currentModule ${PY_STATIC_MODULES_LIST})
 
-  FILE(APPEND ${_filename} "#ifndef EXCLUDE_LOAD_ALL_FUNCTION\nvoid CMakeLoadAllPythonModules(void)\n{\n")
+  FILE(APPEND ${_filenameTmp} "#ifndef EXCLUDE_LOAD_ALL_FUNCTION\nvoid CMakeLoadAllPythonModules(void)\n{\n")
   FOREACH(_currentModule ${PY_STATIC_MODULES_LIST})
-    FILE(APPEND ${_filename} "  CMakeLoadPythonModule_${_currentModule}();\n")
+    FILE(APPEND ${_filenameTmp} "  CMakeLoadPythonModule_${_currentModule}();\n")
   ENDFOREACH(_currentModule ${PY_STATIC_MODULES_LIST})
-  FILE(APPEND ${_filename} "}\n#endif\n\n#endif\n")
+  FILE(APPEND ${_filenameTmp} "}\n#endif\n\n#endif\n")
+  
+# with CONFIGURE_FILE() cmake complains that you may not use a file created using FILE(WRITE) as input file for CONFIGURE_FILE()
+  EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND} -E copy_if_different "${_filenameTmp}" "${_filename}" OUTPUT_QUIET ERROR_QUIET)
+
 ENDMACRO(PYTHON_WRITE_MODULES_HEADER)
-- 
cgit v0.12