ENH: make the python modules usable for C and C++ and only write the header

if it has changed

Alex
This commit is contained in:
Alexander Neundorf 2007-08-02 15:48:51 -04:00
parent 8dc897b60c
commit f32aa8f296
1 changed files with 31 additions and 9 deletions

View File

@ -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)