Salome HOME
Merge branch 'master' into ngr/python3_dev
[tools/configuration.git] / cmake / UsePyQt.cmake
1 # Copyright (C) 2012-2016  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 INCLUDE(UseSIP)
22
23 ####################################################################
24 #
25 # _PYQT_WRAP_GET_UNIQUE_TARGET_NAME: internal function
26
27 # Used to generate unique custom target name for usage in
28 # PYQT_WRAP_UIC macro.
29 #
30 # USAGE: _PYQT_WRAP_GET_UNIQUE_TARGET_NAME(prefix unique_name)
31 #
32 # ARGUMENTS:
33 #   prefix [in] prefix for the name
34 #   unique_name [out] unique name generated by function
35 #
36 ####################################################################
37 FUNCTION(_PYQT_WRAP_GET_UNIQUE_TARGET_NAME name unique_name)
38    SET(_propertyName "_PYQT_WRAP_UNIQUE_COUNTER_${name}")
39    GET_PROPERTY(_currentCounter GLOBAL PROPERTY "${_propertyName}")
40    IF(NOT _currentCounter)
41       SET(_currentCounter 1)
42    ENDIF()
43    SET(${unique_name} "${name}_${_currentCounter}" PARENT_SCOPE)
44    MATH(EXPR _currentCounter "${_currentCounter} + 1")
45    SET_PROPERTY(GLOBAL PROPERTY ${_propertyName} ${_currentCounter} )
46 ENDFUNCTION()
47
48 ####################################################################
49 #
50 # PYQT_WRAP_UIC macro
51 #
52 # Create Python modules by processing input *.ui (Qt designer) files with
53 # PyQt pyuic tool.
54 #
55 # USAGE: PYQT_WRAP_UIC(output_files pyuic_files)
56 #
57 # ARGUMENTS:
58 #   output_files [out] variable where output file names are listed to
59 #   pyuic_files  [in]  list of *.ui files
60 #   options      [in]  additional options to be specified to pyuic
61
62 # NOTES:
63 #   - Input files are considered relative to the current source directory.
64 #   - Output files are generated in the current build directory.
65 #   - Macro automatically adds custom build target to generate output files
66
67 ####################################################################
68 MACRO(PYQT_WRAP_UIC outfiles)
69   SET(_output)
70   SET(_options)
71   SET(_pyuic_files)
72   SET(_get_options "0")
73   FOREACH(_input ${ARGN})
74     IF(${_input} STREQUAL "OPTIONS")
75       SET(_get_options "1")
76     ELSE()
77       IF(${_get_options} STREQUAL "1")
78         SET(_options ${_options} ${_input})
79       ELSE()
80         SET(_pyuic_files ${_pyuic_files} ${_input})
81       ENDIF()
82   ENDIF()
83   ENDFOREACH()
84
85  IF(NOT WIN32)
86
87   FOREACH(_input ${_pyuic_files})
88     GET_FILENAME_COMPONENT(_input_name ${_input} NAME)
89     STRING(REPLACE ".ui" "_ui.py" _input_name ${_input_name})
90     SET(_output ${CMAKE_CURRENT_BINARY_DIR}/${_input_name})
91     ADD_CUSTOM_COMMAND(
92       OUTPUT ${_output}
93       COMMAND ${PYQT_PYUIC_PATH} ${_options} -o ${_output} ${CMAKE_CURRENT_SOURCE_DIR}/${_input}
94       MAIN_DEPENDENCY ${_input}
95       )
96     SET(${outfiles} ${${outfiles}} ${_output})
97   ENDFOREACH()
98   _PYQT_WRAP_GET_UNIQUE_TARGET_NAME(BUILD_UI_PY_FILES _uniqueTargetName)
99   ADD_CUSTOM_TARGET(${_uniqueTargetName} ALL DEPENDS ${${outfiles}})
100
101  ELSE(NOT WIN32)
102
103 ####
104 # ANA: Workaround for the Microsoft Visual Studio 2010. Seems there is a bug in 
105 # the Microsoft Visual Studio 2010 or CMake 2.8.10.2: custom target doesn't work 
106 # for the list of the dependencies. It works only for the first dependency in the 
107 # list. So generate separate target for the each input file. This problem will be 
108 #investigated in the future.
109 ####
110
111   SET_PROPERTY(GLOBAL PROPERTY USE_FOLDERS ON)
112   _PYQT_WRAP_GET_UNIQUE_TARGET_NAME(BUILD_UI_PY_FILES _uniqueTargetName)
113   ADD_CUSTOM_TARGET(${_uniqueTargetName} ALL)
114   SET_TARGET_PROPERTIES(${_uniqueTargetName} PROPERTIES FOLDER PYQT_WRAP_UIC_TARGETS)
115   FOREACH(_input ${ARGN})
116     GET_FILENAME_COMPONENT(_input_name ${_input} NAME)
117     STRING(REPLACE ".ui" "_ui.py" _input_name ${_input_name})
118     SET(_output ${CMAKE_CURRENT_BINARY_DIR}/${_input_name})
119     _PYQT_WRAP_GET_UNIQUE_TARGET_NAME(BUILD_UI_PY_FILES _TgName)
120     ADD_CUSTOM_TARGET(${_TgName} ${PYQT_PYUIC_PATH} -o ${_output} ${CMAKE_CURRENT_SOURCE_DIR}/${_input}
121       DEPENDS ${_input}
122       )
123     SET_TARGET_PROPERTIES(${_TgName} PROPERTIES FOLDER PYQT_WRAP_UIC_TARGETS)
124     ADD_DEPENDENCIES(${_uniqueTargetName} ${_TgName})
125     SET(${outfiles} ${${outfiles}} ${_output})
126   ENDFOREACH()
127  ENDIF(NOT WIN32)
128 ENDMACRO(PYQT_WRAP_UIC)
129
130 ####################################################################
131 #
132 # PYQT_WRAP_SIP macro
133 #
134 # Generate C++ wrappings for *.sip files by processing them with sip.
135 #
136 # USAGE: PYQT_WRAP_SIP(output_files sip_file [sip_file...] [OPTIONS options] [SOURCES sources])
137 #
138 # NOTES: See SIP_WRAP_SIP macro from UseSIP.cmake for the usage description.
139
140 ####################################################################
141 MACRO(PYQT_WRAP_SIP outfiles)
142   SIP_WRAP_SIP(${outfiles} ${ARGN} OPTIONS ${PYQT_SIPFLAGS})
143 ENDMACRO(PYQT_WRAP_SIP)
144
145 ####################################################################
146 #
147 # PYQT_WRAP_QRC macro
148 #
149 # Generate Python wrappings for *.qrc files by processing them with pyrcc5.
150 #
151 # USAGE: PYQT_WRAP_QRC(output_files qrc_files)
152 #
153 # ARGUMENTS:
154 #   output_files [out] variable where output file names are listed to
155 #   qrc_files  [in]  list of *.qrc files
156
157 # NOTES:
158 #   - Input files are considered relative to the current source directory.
159 #   - Output files are generated in the current build directory.
160 #   - Macro automatically adds custom build target to generate output files
161
162 ####################################################################
163
164 MACRO(PYQT_WRAP_QRC outfiles)
165   FOREACH(_input ${ARGN})
166     GET_FILENAME_COMPONENT(_input_name ${_input} NAME)
167     STRING(REPLACE ".qrc" "_qrc.py" _input_name ${_input_name})
168     SET(_output ${CMAKE_CURRENT_BINARY_DIR}/${_input_name})
169     ADD_CUSTOM_COMMAND(
170       OUTPUT ${_output}
171       COMMAND ${PYQT_PYRCC_PATH} -o ${_output} ${CMAKE_CURRENT_SOURCE_DIR}/${_input}
172       MAIN_DEPENDENCY ${_input}
173       )
174     SET(${outfiles} ${${outfiles}} ${_output})
175   ENDFOREACH()
176   _PYQT_WRAP_GET_UNIQUE_TARGET_NAME(BUILD_QRC_PY_FILES _uniqueTargetName)
177   ADD_CUSTOM_TARGET(${_uniqueTargetName} ALL DEPENDS ${${outfiles}})
178 ENDMACRO(PYQT_WRAP_QRC)