0029571: Samples: build qt samples together with OCCT
[occt.git] / adm / cmake / qt_macro.cmake
1 #qt
2
3 macro (FIND_QT_PACKAGE PROJECT_LIBRARIES_DEBUG PROJECT_LIBRARIES_RELEASE PROJECT_INCLUDES)
4
5   if ("${3RDPARTY_QT_DIR}" STREQUAL "")
6     message (FATAL_ERROR "Empty Qt dir")
7   endif()
8
9   # Now set CMAKE_PREFIX_PATH to point to local Qt installation.
10   # Without this setting find_package() will not work
11   set(CMAKE_PREFIX_PATH ${3RDPARTY_QT_DIR})
12
13   # Now we can apply standard CMake finder for Qt5. We do this mostly
14   # to have qt5_wrap_cpp() function available
15   find_package(Qt5 QUIET COMPONENTS Widgets Quick PATHS ${3RDPARTY_QT_DIR} NO_DEFAULT_PATH)
16   if (${Qt5_FOUND})
17     #message (STATUS "Qt5 cmake configuration")
18
19     set(PROJECT_INCLUDES "${Qt5Widgets_INCLUDE_DIRS}" "${Qt5Quick_INCLUDE_DIRS}")
20     set(PROJECT_LIBRARIES_DEBUG "${Qt5Widgets_LIBRARIES}" "${Qt5Quick_LIBRARIES}")
21     set(PROJECT_LIBRARIES_RELEASE "${Qt5Widgets_LIBRARIES}" "${Qt5Quick_LIBRARIES}")
22
23     # processing *.ts files to generate *.qm
24     find_package(Qt5LinguistTools)
25     get_target_property(QT_LRELEASE_EXECUTABLE Qt5::lrelease LOCATION)
26     mark_as_advanced(QT_LRELEASE_EXECUTABLE)
27
28     GET_FILENAME_COMPONENT(QT_BINARY_DIR ${QT_LRELEASE_EXECUTABLE} DIRECTORY)
29     MARK_AS_ADVANCED(QT_BINARY_DIR)
30   else()
31     # Now we can apply standard CMake finder for Qt. We do this mostly
32     # to have qt4_wrap_cpp() function available
33     find_package(Qt4)
34
35     #message (STATUS "Qt4 cmake configuration")
36     set(PROJECT_INCLUDES ${QT_INCLUDES})
37     if (WIN32)
38       set(PROJECT_LIBRARIES_DEBUG "${3RDPARTY_QT_DIR}/lib/QtCored4.lib;${3RDPARTY_QT_DIR}/lib/QtGuid4.lib")
39       set(PROJECT_LIBRARIES_RELEASE "${3RDPARTY_QT_DIR}/lib/QtCore4.lib;${3RDPARTY_QT_DIR}/lib/QtGui4.lib")
40     else()
41       set(PROJECT_LIBRARIES_DEBUG "${3RDPARTY_QT_DIR}/lib/libQtCore.so;${3RDPARTY_QT_DIR}/lib/libQtGui.so")
42       set(PROJECT_LIBRARIES_RELEASE "${3RDPARTY_QT_DIR}/lib/libQtCore.so;${3RDPARTY_QT_DIR}/lib/libQtGui.so")
43     endif(WIN32)
44     find_program(QT_LRELEASE_EXECUTABLE lrelease)
45   endif()
46 endmacro()
47
48
49 macro (FIND_AND_WRAP_MOC_FILES HEADER_FILES GENERATED_MOC_FILES)
50   set (GENERATED_MOC_FILES "")
51   foreach (FILE ${HEADER_FILES})
52     # processing only files where Q_OBJECT exists
53     file(STRINGS "${FILE}" LINES REGEX "Q_OBJECT")
54     if(LINES)
55       unset (MOC_FILE)
56       if (${Qt5_FOUND})
57         qt5_wrap_cpp(MOC_FILE ${FILE})
58       else()
59         qt4_wrap_cpp(MOC_FILE ${FILE})
60       endif()
61       #message (STATUS "... Info: next MOC file ${MOC_FILE}")
62       list(APPEND ${GENERATED_MOC_FILES} ${MOC_FILE})
63      endif(LINES)
64   endforeach (FILE)
65
66 endmacro()
67
68 macro (FIND_AND_WRAP_RESOURCE_FILE RESOURCE_FILE_NAME RCC_FILES)
69   if(EXISTS "${RESOURCE_FILE_NAME}")
70     if (${Qt5_FOUND})
71       qt5_add_resources(RCC_FILES "${RESOURCE_FILE_NAME}")
72     else()
73       qt4_add_resources(RCC_FILES "${RESOURCE_FILE_NAME}")
74       # suppress some GCC warnings coming from source files generated from .qrc resources
75       if (CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX)
76         set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-variable")
77       endif()
78     endif()
79   endif()
80 endmacro()
81
82 macro (FIND_AND_INSTALL_TS_FILE RESOURCE_FILE_NAME TARGET_FOLDER QM_FILES)
83   if(EXISTS "${RESOURCE_FILE_NAME}")
84     GET_FILENAME_COMPONENT(_name ${RESOURCE_FILE_NAME} NAME_WE)
85     SET(_output ${CMAKE_CURRENT_BINARY_DIR}/${_name}.qm)
86     SET(_cmd_${_name} ${QT_LRELEASE_EXECUTABLE} ${RESOURCE_FILE_NAME} -qm ${_output})
87
88     set(TARGET_NAME ${_name}_resources)
89
90     if (NOT TARGET "${TARGET_NAME}")
91       add_custom_target(${TARGET_NAME} ALL COMMAND ${_cmd_${_name}} DEPENDS ${RESOURCE_FILE_NAME})
92       set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "${TARGET_FOLDER}")
93
94       list (APPEND ${QM_FILES} "${_output}")
95     endif()
96   endif()
97 endmacro()
98
99 macro (FIND_AND_INSTALL_QT_RESOURCES OCCT_PACKAGE RESOURCE_FILES)
100   file (STRINGS "${CMAKE_SOURCE_DIR}/${RELATIVE_SOURCES_DIR}/${OCCT_PACKAGE}/FILES"   TS_FILES   REGEX ".+[.]ts")
101   file (STRINGS "${CMAKE_SOURCE_DIR}/${RELATIVE_SOURCES_DIR}/${OCCT_PACKAGE}/FILES"   QRC_FILES  REGEX ".+[.]qrc")
102
103   string (FIND "${OCCT_PACKAGE}" "/" _index)
104   if (_index GREATER -1)
105     math (EXPR _index "${_index}")
106     string (SUBSTRING "${OCCT_PACKAGE}" 0 ${_index} OCCT_PACKAGE_NAME)
107   else()
108     set (OCCT_PACKAGE_NAME "${OCCT_PACKAGE}")
109   endif(_index GREATER -1)
110
111   #message("QRC files are: ${QRC_FILES} in ${OCCT_PACKAGE}")
112   foreach (QRC_FILE ${QRC_FILES})
113     set (QRC_FILE_RELATIVE "${CMAKE_SOURCE_DIR}/${RELATIVE_SOURCES_DIR}/${OCCT_PACKAGE}/${QRC_FILE}")
114     if (EXISTS ${QRC_FILE_RELATIVE})
115       FIND_AND_WRAP_RESOURCE_FILE(${QRC_FILE_RELATIVE} RCC_FILES)
116       list (APPEND ${RESOURCE_FILES} "${RCC_FILES}")
117     endif()
118   endforeach()
119
120   #message("TS files are: ${TS_FILES} in ${OCCT_PACKAGE}")
121   foreach (TS_FILE ${TS_FILES})
122     set (TS_FILE_RELATIVE "${CMAKE_SOURCE_DIR}/${RELATIVE_SOURCES_DIR}/${OCCT_PACKAGE}/${TS_FILE}")
123     FIND_AND_INSTALL_TS_FILE(${TS_FILE_RELATIVE} "${TARGET_FOLDER}/${CURRENT_MODULE}" QM_FILES)
124     if (EXISTS ${TS_FILE_RELATIVE})
125       list (APPEND ${RESOURCE_FILES} "${TS_FILE_RELATIVE}")
126     endif()
127   endforeach()
128
129   foreach (QM_FILE ${QM_FILES})
130     INSTALL(FILES ${QM_FILE} DESTINATION "${INSTALL_DIR_RESOURCE}/samples")
131     #message("install *.qm files (${QM_FILE}) to: ${INSTALL_DIR_RESOURCE}/samples")
132   endforeach (QM_FILE ${QM_FILES})
133 endmacro()
134