Salome HOME
Windows compatibility
[modules/gui.git] / adm_local / cmake_files / UsePyQt4.cmake
1 # Copyright (C) 2012-2015  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, or (at your option) any later version.
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
67  IF(NOT WIN32)
68
69   FOREACH(_input ${ARGN})
70     GET_FILENAME_COMPONENT(_input_name ${_input} NAME)
71     STRING(REPLACE ".ui" "_ui.py" _input_name ${_input_name})
72     SET(_output ${CMAKE_CURRENT_BINARY_DIR}/${_input_name})
73     ADD_CUSTOM_COMMAND(
74       OUTPUT ${_output}
75       COMMAND ${PYQT_PYUIC_PATH} -o ${_output} ${CMAKE_CURRENT_SOURCE_DIR}/${_input}
76       MAIN_DEPENDENCY ${_input}
77       )
78     SET(${outfiles} ${${outfiles}} ${_output})
79   ENDFOREACH()
80   _PYQT4_WRAP_GET_UNIQUE_TARGET_NAME(BUILD_UI_PY_FILES _uniqueTargetName)
81   ADD_CUSTOM_TARGET(${_uniqueTargetName} ALL DEPENDS ${${outfiles}})
82
83  ELSE(NOT WIN32)
84 ####
85 # ANA: Workaround for the Microsoft Visual Studio 2010. Seems there is a bug in 
86 # the Microsoft Visual Studio 2010 or CMake 2.8.10.2: custom target doesn't work 
87 # for the list of the dependencies. It works only for the first dependency in the 
88 # list. So generate separate target for the each input file. This problem will be 
89 #investigated in the future.
90 ####
91
92   SET_PROPERTY(GLOBAL PROPERTY USE_FOLDERS ON)
93   _PYQT4_WRAP_GET_UNIQUE_TARGET_NAME(BUILD_UI_PY_FILES _uniqueTargetName)
94   ADD_CUSTOM_TARGET(${_uniqueTargetName} ALL)
95   FOREACH(_input ${ARGN})
96     GET_FILENAME_COMPONENT(_input_name ${_input} NAME)
97     STRING(REPLACE ".ui" "_ui.py" _input_name ${_input_name})
98     SET(_output ${CMAKE_CURRENT_BINARY_DIR}/${_input_name})
99     _PYQT4_WRAP_GET_UNIQUE_TARGET_NAME(BUILD_UI_PY_FILES _TgName)
100     ADD_CUSTOM_TARGET(${_TgName} ${PYQT_PYUIC_PATH} -o ${_output} ${CMAKE_CURRENT_SOURCE_DIR}/${_input}
101       DEPENDS ${_input}
102       )
103     SET_TARGET_PROPERTIES(${_TgName} PROPERTIES FOLDER PYQT4_WRAP_UIC_TARGETS)
104     ADD_DEPENDENCIES(${_uniqueTargetName} DEPEND ${_TgName})
105     SET(${outfiles} ${${outfiles}} ${_output})
106   ENDFOREACH()
107  ENDIF(NOT WIN32)
108 ENDMACRO(PYQT4_WRAP_UIC)
109
110 ####################################################################
111 #
112 # PYQT4_WRAP_SIP macro
113 #
114 # Generate C++ wrappings for *.sip files by processing them with sip.
115 #
116 # USAGE: PYQT4_WRAP_SIP(output_files sip_files)
117 #
118 # ARGUMENTS:
119 #   output_files [out] variable where output file names are listed to
120 #   sip_files    [in]  list of *.sip files
121
122 # NOTES:
123 #   - Input files are considered relative to the current source directory.
124 #   - Output files are generated in the current build directory.
125 #   - This version of macro requires class(es) definition in the 
126 #     *.sip file to be started on a new line without any preceeding characters.
127
128 # WARNING:
129 #   - The macro does not properly processes sip features which are wrapped
130 #     with sip conditionals.
131 #   - The macro works only if one single sip module is processed
132 #     (there's only one %Module directive within all input sip files).
133 #
134 # TODO:
135 #   - Check if dependency of static sources on generated headers works properly:
136 #     if header is changed, dependant sources should be recompiled.
137 #   - Think how to properly process sip conditionals.
138 #   - Process several sip modules.
139
140 ####################################################################
141 MACRO(PYQT4_WRAP_SIP outfiles)
142   SET(_output)
143   SET(_module_input)
144   FOREACH(_input ${ARGN})
145     FILE(STRINGS ${_input} _sip_modules REGEX "%Module")
146     FILE(STRINGS ${_input} _sip_classes REGEX "^class ")
147     FOREACH(_sip_module ${_sip_modules})
148       STRING(REGEX MATCH ".*%Module *\\( *name=.*\\).*" _mod_name "${_sip_module}")
149       IF (_mod_name)
150         STRING(REGEX REPLACE ".*%Module *\\( *name=(.*).*\\).*" "\\1" _mod_name ${_sip_module})
151       ELSE()
152         STRING(REGEX REPLACE ".*%Module *(.*)" "\\1" _mod_name ${_sip_module})
153       ENDIF()
154       SET(_mod_header "sipAPI${_mod_name}.h")
155       SET(_mod_source "sip${_mod_name}cmodule${PYQT_CXX_EXT}")
156       LIST(APPEND _output ${CMAKE_CURRENT_BINARY_DIR}/${_mod_source})
157       SET(${outfiles} ${${outfiles}} ${CMAKE_CURRENT_BINARY_DIR}/${_mod_source})
158       SET(_module_input ${_input})
159     ENDFOREACH()
160     FOREACH(_sip_class ${_sip_classes})
161       STRING(REGEX MATCH ".*class +.* *:" _class_name "${_sip_class}")
162       IF (_class_name)
163         STRING(REGEX REPLACE ".*class +(.*) *:.*" "\\1" _class_name ${_sip_class})
164       ELSE()
165         STRING(REGEX REPLACE ".*class *(.*)" "\\1" _class_name ${_sip_class})
166       ENDIF()
167       STRING(STRIP ${_class_name} _class_name)
168       SET(_class_source "sip${_mod_name}${_class_name}${PYQT_CXX_EXT}")
169       LIST(APPEND _output ${CMAKE_CURRENT_BINARY_DIR}/${_class_source})
170       SET(${outfiles} ${${outfiles}} ${CMAKE_CURRENT_BINARY_DIR}/${_class_source})
171     ENDFOREACH()
172   ENDFOREACH()
173   ADD_CUSTOM_COMMAND(
174     OUTPUT ${_output}
175     COMMAND ${SIP_EXECUTABLE} ${PYQT_SIPFLAGS} ${CMAKE_CURRENT_SOURCE_DIR}/${_module_input}
176     MAIN_DEPENDENCY ${_module_input}
177     )
178 ENDMACRO(PYQT4_WRAP_SIP)
179
180
181 ####################################################################
182 #
183 # PYQT4_WRAP_QRC macro
184 #
185 # Generate Python wrappings for *.qrc files by processing them with pyrcc4.
186 #
187 # USAGE: PYQT4_WRAP_QRC(output_files qrc_files)
188 #
189 # ARGUMENTS:
190 #   output_files [out] variable where output file names are listed to
191 #   qrc_files  [in]  list of *.qrc files
192
193 # NOTES:
194 #   - Input files are considered relative to the current source directory.
195 #   - Output files are generated in the current build directory.
196 #   - Macro automatically adds custom build target to generate output files
197
198 ####################################################################
199
200 MACRO(PYQT4_WRAP_QRC outfiles)
201   FOREACH(_input ${ARGN})
202     GET_FILENAME_COMPONENT(_input_name ${_input} NAME)
203     STRING(REPLACE ".qrc" "_qrc.py" _input_name ${_input_name})
204     SET(_output ${CMAKE_CURRENT_BINARY_DIR}/${_input_name})
205     ADD_CUSTOM_COMMAND(
206       OUTPUT ${_output}
207       COMMAND ${PYQT_PYRCC_PATH} -o ${_output} ${CMAKE_CURRENT_SOURCE_DIR}/${_input}
208       MAIN_DEPENDENCY ${_input}
209       )
210     SET(${outfiles} ${${outfiles}} ${_output})
211   ENDFOREACH()
212   _PYQT4_WRAP_GET_UNIQUE_TARGET_NAME(BUILD_QRC_PY_FILES _uniqueTargetName)
213   ADD_CUSTOM_TARGET(${_uniqueTargetName} ALL DEPENDS ${${outfiles}})
214 ENDMACRO(PYQT4_WRAP_QRC)