Add initial support for python bindings
[alexxy/gromacs.git] / src / pygromacs / cmake / modules / SIPMacros.cmake
1 # Macros for SIP
2 # ~~~~~~~~~~~~~~
3 # Copyright (c) 2007, Simon Edwards <simon@simonzone.com>
4 # Redistribution and use is allowed according to the terms of the BSD license.
5 # For details see the accompanying COPYING-CMAKE-SCRIPTS file.
6 #
7 # SIP website: http://www.riverbankcomputing.co.uk/sip/index.php
8 #
9 # This file defines the following macros:
10 #
11 # ADD_SIP_PYTHON_MODULE (MODULE_NAME MODULE_SIP [library1, libaray2, ...])
12 #     Specifies a SIP file to be built into a Python module and installed.
13 #     MODULE_NAME is the name of Python module including any path name. (e.g.
14 #     os.sys, Foo.bar etc). MODULE_SIP the path and filename of the .sip file
15 #     to process and compile. libraryN are libraries that the Python module,
16 #     which is typically a shared library, should be linked to. The built
17 #     module will also be install into Python's site-packages directory.
18 #
19 # The behaviour of the ADD_SIP_PYTHON_MODULE macro can be controlled by a
20 # number of variables:
21 #
22 # SIP_INCLUDES - List of directories which SIP will scan through when looking
23 #     for included .sip files. (Corresponds to the -I option for SIP.)
24 #
25 # SIP_TAGS - List of tags to define when running SIP. (Corresponds to the -t
26 #     option for SIP.)
27 #
28 # SIP_CONCAT_PARTS - An integer which defines the number of parts the C++ code
29 #     of each module should be split into. Defaults to 8. (Corresponds to the
30 #     -j option for SIP.)
31 #
32 # SIP_DISABLE_FEATURES - List of feature names which should be disabled
33 #     running SIP. (Corresponds to the -x option for SIP.)
34 #
35 # SIP_EXTRA_OPTIONS - Extra command line options which should be passed on to
36 #     SIP.
37
38 SET(SIP_INCLUDES)
39 SET(SIP_TAGS)
40 SET(SIP_CONCAT_PARTS 8)
41 SET(SIP_DISABLE_FEATURES)
42 SET(SIP_EXTRA_OPTIONS)
43
44 MACRO(ADD_SIP_PYTHON_MODULE MODULE_NAME MODULE_SIP)
45
46     SET(EXTRA_LINK_LIBRARIES ${ARGN})
47
48     STRING(REPLACE "." "/" _x ${MODULE_NAME})
49     GET_FILENAME_COMPONENT(_parent_module_path ${_x}  PATH)
50     GET_FILENAME_COMPONENT(_child_module_name ${_x} NAME)
51
52     GET_FILENAME_COMPONENT(_module_path ${MODULE_SIP} PATH)
53
54     if(_module_path STREQUAL "")
55         set(CMAKE_CURRENT_SIP_OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}")
56     else(_module_path STREQUAL "")
57         set(CMAKE_CURRENT_SIP_OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/${_module_path}")
58     endif(_module_path STREQUAL "")
59
60     GET_FILENAME_COMPONENT(_abs_module_sip ${MODULE_SIP} ABSOLUTE)
61
62     # We give this target a long logical target name.
63     # (This is to avoid having the library name clash with any already
64     # install library names. If that happens then cmake dependancy
65     # tracking get confused.)
66     STRING(REPLACE "." "_" _logical_name ${MODULE_NAME})
67     SET(_logical_name "python_module_${_logical_name}")
68
69     FILE(MAKE_DIRECTORY ${CMAKE_CURRENT_SIP_OUTPUT_DIR})    # Output goes in this dir.
70
71     SET(_sip_includes)
72     FOREACH (_inc ${SIP_INCLUDES})
73         GET_FILENAME_COMPONENT(_abs_inc ${_inc} ABSOLUTE)
74         LIST(APPEND _sip_includes -I ${_abs_inc})
75     ENDFOREACH (_inc )
76
77     SET(_sip_tags)
78     FOREACH (_tag ${SIP_TAGS})
79         LIST(APPEND _sip_tags -t ${_tag})
80     ENDFOREACH (_tag)
81
82     SET(_sip_x)
83     FOREACH (_x ${SIP_DISABLE_FEATURES})
84         LIST(APPEND _sip_x -x ${_x})
85     ENDFOREACH (_x ${SIP_DISABLE_FEATURES})
86
87     SET(_message "-DMESSAGE=Generating CPP code for module ${MODULE_NAME}")
88     SET(_sip_output_files)
89     FOREACH(CONCAT_NUM RANGE 0 ${SIP_CONCAT_PARTS} )
90         IF( ${CONCAT_NUM} LESS ${SIP_CONCAT_PARTS} )
91             SET(_sip_output_files ${_sip_output_files} ${CMAKE_CURRENT_SIP_OUTPUT_DIR}/sip${_child_module_name}part${CONCAT_NUM}.cpp )
92         ENDIF( ${CONCAT_NUM} LESS ${SIP_CONCAT_PARTS} )
93     ENDFOREACH(CONCAT_NUM RANGE 0 ${SIP_CONCAT_PARTS} )
94
95     IF(NOT WIN32)
96         SET(TOUCH_COMMAND touch)
97     ELSE(NOT WIN32)
98         SET(TOUCH_COMMAND echo)
99         # instead of a touch command, give out the name and append to the files
100         # this is basically what the touch command does.
101         FOREACH(filename ${_sip_output_files})
102             FILE(APPEND filename "")
103         ENDFOREACH(filename ${_sip_output_files})
104     ENDIF(NOT WIN32)
105     ADD_CUSTOM_COMMAND(
106         OUTPUT ${_sip_output_files}
107         COMMAND ${CMAKE_COMMAND} -E echo ${message}
108         COMMAND ${TOUCH_COMMAND} ${_sip_output_files}
109         COMMAND ${SIP_EXECUTABLE} ${_sip_tags} ${_sip_x} ${SIP_EXTRA_OPTIONS} -j ${SIP_CONCAT_PARTS} -c ${CMAKE_CURRENT_SIP_OUTPUT_DIR} ${_sip_includes} ${_abs_module_sip}
110         DEPENDS ${_abs_module_sip} ${SIP_EXTRA_FILES_DEPEND}
111     )
112     # not sure if type MODULE could be uses anywhere, limit to cygwin for now
113     IF (CYGWIN)
114         ADD_LIBRARY(${_logical_name} MODULE ${_sip_output_files} )
115     ELSE (CYGWIN)
116         ADD_LIBRARY(${_logical_name} SHARED ${_sip_output_files} )
117     ENDIF (CYGWIN)
118     TARGET_LINK_LIBRARIES(${_logical_name} ${PYTHON_LIBRARY})
119     TARGET_LINK_LIBRARIES(${_logical_name} ${EXTRA_LINK_LIBRARIES})
120     SET_TARGET_PROPERTIES(${_logical_name} PROPERTIES PREFIX "" OUTPUT_NAME ${_child_module_name})
121
122     INSTALL(TARGETS ${_logical_name} DESTINATION "${PYTHON_SITE_PACKAGES_INSTALL_DIR}/${_parent_module_path}")
123
124 ENDMACRO(ADD_SIP_PYTHON_MODULE)
125