Salome HOME
CMake build procedure improvements
[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_UIC macro
24 #
25 # Create Python modules by processing input *.ui (Qt designer) files with
26 # PyQt4 pyuic4 tool.
27 #
28 # USAGE: PYQT4_WRAP_UIC(output_files pyuic_files)
29 #
30 # ARGUMENTS:
31 #   output_files [out] variable where output file names are listed to
32 #   pyuic_files  [in]  list of *.ui files
33
34 # NOTES:
35 #   - Input files are considered relative to the current source directory.
36 #   - Output files are generated in the current build directory.
37 #   - Macro automatically adds custom build target to generate output files
38
39 ####################################################################
40 MACRO(PYQT4_WRAP_UIC outfiles)
41   FOREACH(_input ${ARGN})
42     GET_FILENAME_COMPONENT(_input_name ${_input} NAME)
43     STRING(REPLACE ".ui" "_ui.py" _input_name ${_input_name})
44     SET(_output ${CMAKE_CURRENT_BINARY_DIR}/${_input_name})
45     ADD_CUSTOM_COMMAND(
46       OUTPUT ${_output}
47       COMMAND ${PYQT_PYUIC_EXECUTABLE} -o ${_output} ${CMAKE_CURRENT_SOURCE_DIR}/${_input}
48       MAIN_DEPENDENCY ${_input}
49       )
50     SET(${outfiles} ${${outfiles}} ${_output})
51   ENDFOREACH()
52   ADD_CUSTOM_TARGET(BUILD_UI_PY_FILES ALL DEPENDS ${${outfiles}})
53 ENDMACRO(PYQT4_WRAP_UIC)
54
55 ####################################################################
56 #
57 # PYQT4_WRAP_SIP macro
58 #
59 # Generate C++ wrappings for *.sip files by processing them with sip.
60 #
61 # USAGE: PYQT4_WRAP_SIP(output_files sip_files)
62 #
63 # ARGUMENTS:
64 #   output_files [out] variable where output file names are listed to
65 #   sip_files    [in]  list of *.sip files
66
67 # NOTES:
68 #   - Input files are considered relative to the current source directory.
69 #   - Output files are generated in the current build directory.
70 #   - This version of macro requires class(es) definition in the 
71 #     *.sip file to be started on a new line without any preceeding characters.
72 #
73 # TODO:
74 #   - Check if dependency of static sources on generated headers works properly:
75 #     if header is changed, dependant sources should be recompiled.
76
77 ####################################################################
78 MACRO(PYQT4_WRAP_SIP outfiles)
79   FOREACH(_input ${ARGN})
80     FILE(STRINGS ${_input} _sip_modules REGEX "%Module")
81     FILE(STRINGS ${_input} _sip_classes REGEX "^class ")
82     SET(_output)
83     FOREACH(_sip_module ${_sip_modules})
84       STRING(REGEX MATCH ".*%Module *\\( *name=.*\\).*" _mod_name "${_sip_module}")
85       IF (_mod_name)
86         STRING(REGEX REPLACE ".*%Module *\\( *name=(.*).*\\).*" "\\1" _mod_name ${_sip_module})
87       ELSE()
88         STRING(REGEX REPLACE ".*%Module *(.*)" "\\1" _mod_name ${_sip_module})
89       ENDIF()
90       SET(_mod_header "sipAPI${_mod_name}.h")
91       SET(_mod_source "sip${_mod_name}cmodule${PYQT_CXX_EXT}")
92       LIST(APPEND _output ${CMAKE_CURRENT_BINARY_DIR}/${_mod_source})
93       SET(${outfiles} ${${outfiles}} ${CMAKE_CURRENT_BINARY_DIR}/${_mod_source})
94     ENDFOREACH()
95     FOREACH(_sip_class ${_sip_classes})
96       STRING(REGEX MATCH ".*class +.* *:" _class_name "${_sip_class}")
97       IF (_class_name)
98         STRING(REGEX REPLACE ".*class +(.*) *:.*" "\\1" _class_name ${_sip_class})
99       ELSE()
100         STRING(REGEX REPLACE ".*class *(.*)" "\\1" _class_name ${_sip_class})
101       ENDIF()
102       STRING(STRIP ${_class_name} _class_name)
103       SET(_class_source "sip${_mod_name}${_class_name}${PYQT_CXX_EXT}")
104       LIST(APPEND _output ${CMAKE_CURRENT_BINARY_DIR}/${_class_source})
105       SET(${outfiles} ${${outfiles}} ${CMAKE_CURRENT_BINARY_DIR}/${_class_source})
106     ENDFOREACH()
107     ADD_CUSTOM_COMMAND(
108       OUTPUT ${_output}
109       COMMAND ${SIP_EXECUTABLE} ${PYQT_SIPFLAGS} ${CMAKE_CURRENT_SOURCE_DIR}/${_input}
110       MAIN_DEPENDENCY ${_input}
111       )
112   ENDFOREACH()
113 ENDMACRO(PYQT4_WRAP_SIP)