Salome HOME
fix error of previous change
[modules/gui.git] / adm_local / cmake_files / UsePyQt4.cmake
1 # Copyright (C) 2012-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 #
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License.
7 #
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 # Lesser General Public License for more details.
12 #
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 #
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 #
19 # Author: Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
20
21 ####################################################################
22 #
23 # _PYQT4_WRAP_GET_UNIQUE_TARGET_NAME: internal function
24
25 # Used to generate unique custom target name for usage in
26 # PYQT4_WRAP_UIC macro.
27 #
28 # USAGE: _PYQT4_WRAP_GET_UNIQUE_TARGET_NAME(prefix unique_name)
29 #
30 # ARGUMENTS:
31 #   prefix [in] prefix for the name
32 #   unique_name [out] unique name generated by function
33 #
34 ####################################################################
35 FUNCTION(_PYQT4_WRAP_GET_UNIQUE_TARGET_NAME name unique_name)
36    SET(_propertyName "_PYQT4_WRAP_UNIQUE_COUNTER_${name}")
37    GET_PROPERTY(_currentCounter GLOBAL PROPERTY "${_propertyName}")
38    IF(NOT _currentCounter)
39       SET(_currentCounter 1)
40    ENDIF()
41    SET(${unique_name} "${name}_${_currentCounter}" PARENT_SCOPE)
42    MATH(EXPR _currentCounter "${_currentCounter} + 1")
43    SET_PROPERTY(GLOBAL PROPERTY ${_propertyName} ${_currentCounter} )
44 ENDFUNCTION()
45
46 ####################################################################
47 #
48 # PYQT4_WRAP_UIC macro
49 #
50 # Create Python modules by processing input *.ui (Qt designer) files with
51 # PyQt4 pyuic4 tool.
52 #
53 # USAGE: PYQT4_WRAP_UIC(output_files pyuic_files)
54 #
55 # ARGUMENTS:
56 #   output_files [out] variable where output file names are listed to
57 #   pyuic_files  [in]  list of *.ui files
58
59 # NOTES:
60 #   - Input files are considered relative to the current source directory.
61 #   - Output files are generated in the current build directory.
62 #   - Macro automatically adds custom build target to generate output files
63
64 ####################################################################
65 MACRO(PYQT4_WRAP_UIC outfiles)
66   FOREACH(_input ${ARGN})
67     GET_FILENAME_COMPONENT(_input_name ${_input} NAME)
68     STRING(REPLACE ".ui" "_ui.py" _input_name ${_input_name})
69     SET(_output ${CMAKE_CURRENT_BINARY_DIR}/${_input_name})
70     ADD_CUSTOM_COMMAND(
71       OUTPUT ${_output}
72       COMMAND ${PYQT_PYUIC_PATH} -o ${_output} ${CMAKE_CURRENT_SOURCE_DIR}/${_input}
73       MAIN_DEPENDENCY ${_input}
74       )
75     SET(${outfiles} ${${outfiles}} ${_output})
76   ENDFOREACH()
77   _PYQT4_WRAP_GET_UNIQUE_TARGET_NAME(BUILD_UI_PY_FILES _uniqueTargetName)
78   ADD_CUSTOM_TARGET(${_uniqueTargetName} ALL DEPENDS ${${outfiles}})
79 ENDMACRO(PYQT4_WRAP_UIC)
80
81 ####################################################################
82 #
83 # PYQT4_WRAP_SIP macro
84 #
85 # Generate C++ wrappings for *.sip files by processing them with sip.
86 #
87 # USAGE: PYQT4_WRAP_SIP(output_files sip_files)
88 #
89 # ARGUMENTS:
90 #   output_files [out] variable where output file names are listed to
91 #   sip_files    [in]  list of *.sip files
92
93 # NOTES:
94 #   - Input files are considered relative to the current source directory.
95 #   - Output files are generated in the current build directory.
96 #   - This version of macro requires class(es) definition in the 
97 #     *.sip file to be started on a new line without any preceeding characters.
98 #
99 # TODO:
100 #   - Check if dependency of static sources on generated headers works properly:
101 #     if header is changed, dependant sources should be recompiled.
102
103 ####################################################################
104 MACRO(PYQT4_WRAP_SIP outfiles)
105   FOREACH(_input ${ARGN})
106     FILE(STRINGS ${_input} _sip_modules REGEX "%Module")
107     FILE(STRINGS ${_input} _sip_classes REGEX "^class ")
108     SET(_output)
109     FOREACH(_sip_module ${_sip_modules})
110       STRING(REGEX MATCH ".*%Module *\\( *name=.*\\).*" _mod_name "${_sip_module}")
111       IF (_mod_name)
112         STRING(REGEX REPLACE ".*%Module *\\( *name=(.*).*\\).*" "\\1" _mod_name ${_sip_module})
113       ELSE()
114         STRING(REGEX REPLACE ".*%Module *(.*)" "\\1" _mod_name ${_sip_module})
115       ENDIF()
116       SET(_mod_header "sipAPI${_mod_name}.h")
117       SET(_mod_source "sip${_mod_name}cmodule${PYQT_CXX_EXT}")
118       LIST(APPEND _output ${CMAKE_CURRENT_BINARY_DIR}/${_mod_source})
119       SET(${outfiles} ${${outfiles}} ${CMAKE_CURRENT_BINARY_DIR}/${_mod_source})
120     ENDFOREACH()
121     FOREACH(_sip_class ${_sip_classes})
122       STRING(REGEX MATCH ".*class +.* *:" _class_name "${_sip_class}")
123       IF (_class_name)
124         STRING(REGEX REPLACE ".*class +(.*) *:.*" "\\1" _class_name ${_sip_class})
125       ELSE()
126         STRING(REGEX REPLACE ".*class *(.*)" "\\1" _class_name ${_sip_class})
127       ENDIF()
128       STRING(STRIP ${_class_name} _class_name)
129       SET(_class_source "sip${_mod_name}${_class_name}${PYQT_CXX_EXT}")
130       LIST(APPEND _output ${CMAKE_CURRENT_BINARY_DIR}/${_class_source})
131       SET(${outfiles} ${${outfiles}} ${CMAKE_CURRENT_BINARY_DIR}/${_class_source})
132     ENDFOREACH()
133     ADD_CUSTOM_COMMAND(
134       OUTPUT ${_output}
135       COMMAND ${SIP_EXECUTABLE} ${PYQT_SIPFLAGS} ${CMAKE_CURRENT_SOURCE_DIR}/${_input}
136       MAIN_DEPENDENCY ${_input}
137       )
138   ENDFOREACH()
139 ENDMACRO(PYQT4_WRAP_SIP)
140
141
142 ####################################################################
143 #
144 # PYQT4_WRAP_QRC macro
145 #
146 # Generate Python wrappings for *.qrc files by processing them with pyrcc4.
147 #
148 # USAGE: PYQT4_WRAP_QRC(output_files qrc_files)
149 #
150 # ARGUMENTS:
151 #   output_files [out] variable where output file names are listed to
152 #   qrc_files  [in]  list of *.qrc files
153
154 # NOTES:
155 #   - Input files are considered relative to the current source directory.
156 #   - Output files are generated in the current build directory.
157 #   - Macro automatically adds custom build target to generate output files
158
159 ####################################################################
160
161 MACRO(PYQT4_WRAP_QRC outfiles)
162   FOREACH(_input ${ARGN})
163     GET_FILENAME_COMPONENT(_input_name ${_input} NAME)
164     STRING(REPLACE ".qrc" "_qrc.py" _input_name ${_input_name})
165     SET(_output ${CMAKE_CURRENT_BINARY_DIR}/${_input_name})
166     ADD_CUSTOM_COMMAND(
167       OUTPUT ${_output}
168       COMMAND ${PYQT_PYRCC_PATH} -o ${_output} ${CMAKE_CURRENT_SOURCE_DIR}/${_input}
169       MAIN_DEPENDENCY ${_input}
170       )
171     SET(${outfiles} ${${outfiles}} ${_output})
172   ENDFOREACH()
173   _PYQT4_WRAP_GET_UNIQUE_TARGET_NAME(BUILD_QRC_PY_FILES _uniqueTargetName)
174   ADD_CUSTOM_TARGET(${_uniqueTargetName} ALL DEPENDS ${${outfiles}})
175 ENDMACRO(PYQT4_WRAP_QRC)