Salome HOME
Win32 compatibility
[modules/gui.git] / adm_local / cmake_files / UsePyQt4.cmake
1 # Copyright (C) 2012-2014  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(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       COMMENT ANA:${_output}
102       DEPENDS ${_input}
103       )
104     SET_TARGET_PROPERTIES(${_TgName} PROPERTIES FOLDER PYQT4_WRAP_UIC_TARGETS)
105     ADD_DEPENDENCIES(${_uniqueTargetName} DEPEND ${_TgName})
106     SET(${outfiles} ${${outfiles}} ${_output})
107   ENDFOREACH()
108  ENDIF(WIN32)
109 ENDMACRO(PYQT4_WRAP_UIC)
110
111 ####################################################################
112 #
113 # PYQT4_WRAP_SIP macro
114 #
115 # Generate C++ wrappings for *.sip files by processing them with sip.
116 #
117 # USAGE: PYQT4_WRAP_SIP(output_files sip_files)
118 #
119 # ARGUMENTS:
120 #   output_files [out] variable where output file names are listed to
121 #   sip_files    [in]  list of *.sip files
122
123 # NOTES:
124 #   - Input files are considered relative to the current source directory.
125 #   - Output files are generated in the current build directory.
126 #   - This version of macro requires class(es) definition in the 
127 #     *.sip file to be started on a new line without any preceeding characters.
128 #
129 # TODO:
130 #   - Check if dependency of static sources on generated headers works properly:
131 #     if header is changed, dependant sources should be recompiled.
132
133 ####################################################################
134 MACRO(PYQT4_WRAP_SIP outfiles)
135   FOREACH(_input ${ARGN})
136     FILE(STRINGS ${_input} _sip_modules REGEX "%Module")
137     FILE(STRINGS ${_input} _sip_classes REGEX "^class ")
138     SET(_output)
139     FOREACH(_sip_module ${_sip_modules})
140       STRING(REGEX MATCH ".*%Module *\\( *name=.*\\).*" _mod_name "${_sip_module}")
141       IF (_mod_name)
142         STRING(REGEX REPLACE ".*%Module *\\( *name=(.*).*\\).*" "\\1" _mod_name ${_sip_module})
143       ELSE()
144         STRING(REGEX REPLACE ".*%Module *(.*)" "\\1" _mod_name ${_sip_module})
145       ENDIF()
146       SET(_mod_header "sipAPI${_mod_name}.h")
147       SET(_mod_source "sip${_mod_name}cmodule${PYQT_CXX_EXT}")
148       LIST(APPEND _output ${CMAKE_CURRENT_BINARY_DIR}/${_mod_source})
149       SET(${outfiles} ${${outfiles}} ${CMAKE_CURRENT_BINARY_DIR}/${_mod_source})
150     ENDFOREACH()
151     FOREACH(_sip_class ${_sip_classes})
152       STRING(REGEX MATCH ".*class +.* *:" _class_name "${_sip_class}")
153       IF (_class_name)
154         STRING(REGEX REPLACE ".*class +(.*) *:.*" "\\1" _class_name ${_sip_class})
155       ELSE()
156         STRING(REGEX REPLACE ".*class *(.*)" "\\1" _class_name ${_sip_class})
157       ENDIF()
158       STRING(STRIP ${_class_name} _class_name)
159       SET(_class_source "sip${_mod_name}${_class_name}${PYQT_CXX_EXT}")
160       LIST(APPEND _output ${CMAKE_CURRENT_BINARY_DIR}/${_class_source})
161       SET(${outfiles} ${${outfiles}} ${CMAKE_CURRENT_BINARY_DIR}/${_class_source})
162     ENDFOREACH()
163     ADD_CUSTOM_COMMAND(
164       OUTPUT ${_output}
165       COMMAND ${SIP_EXECUTABLE} ${PYQT_SIPFLAGS} ${CMAKE_CURRENT_SOURCE_DIR}/${_input}
166       MAIN_DEPENDENCY ${_input}
167       )
168   ENDFOREACH()
169 ENDMACRO(PYQT4_WRAP_SIP)
170
171
172 ####################################################################
173 #
174 # PYQT4_WRAP_QRC macro
175 #
176 # Generate Python wrappings for *.qrc files by processing them with pyrcc4.
177 #
178 # USAGE: PYQT4_WRAP_QRC(output_files qrc_files)
179 #
180 # ARGUMENTS:
181 #   output_files [out] variable where output file names are listed to
182 #   qrc_files  [in]  list of *.qrc files
183
184 # NOTES:
185 #   - Input files are considered relative to the current source directory.
186 #   - Output files are generated in the current build directory.
187 #   - Macro automatically adds custom build target to generate output files
188
189 ####################################################################
190
191 MACRO(PYQT4_WRAP_QRC outfiles)
192   FOREACH(_input ${ARGN})
193     GET_FILENAME_COMPONENT(_input_name ${_input} NAME)
194     STRING(REPLACE ".qrc" "_qrc.py" _input_name ${_input_name})
195     SET(_output ${CMAKE_CURRENT_BINARY_DIR}/${_input_name})
196     ADD_CUSTOM_COMMAND(
197       OUTPUT ${_output}
198       COMMAND ${PYQT_PYRCC_PATH} -o ${_output} ${CMAKE_CURRENT_SOURCE_DIR}/${_input}
199       MAIN_DEPENDENCY ${_input}
200       )
201     SET(${outfiles} ${${outfiles}} ${_output})
202   ENDFOREACH()
203   _PYQT4_WRAP_GET_UNIQUE_TARGET_NAME(BUILD_QRC_PY_FILES _uniqueTargetName)
204   ADD_CUSTOM_TARGET(${_uniqueTargetName} ALL DEPENDS ${${outfiles}})
205 ENDMACRO(PYQT4_WRAP_QRC)