]> git.lyx.org Git - features.git/blob - development/cmake/modules/LyXMacros.cmake
Extend and use qt4_wrap_ui
[features.git] / development / cmake / modules / LyXMacros.cmake
1 #
2 #  Copyright (c) 2006-2011 Peter Kümmel, <syntheticpp@gmx.net>
3 #
4 #  Redistribution and use in source and binary forms, with or without
5 #  modification, are permitted provided that the following conditions
6 #  are met:
7 #
8 #  1. Redistributions of source code must retain the copyright
9 #         notice, this list of conditions and the following disclaimer.
10 #  2. Redistributions in binary form must reproduce the copyright
11 #         notice, this list of conditions and the following disclaimer in the
12 #         documentation and/or other materials provided with the distribution.
13 #  3. The name of the author may not be used to endorse or promote products
14 #         derived from this software without specific prior written permission.
15 #
16 #  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 #  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 #  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 #  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 #  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 #  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 #  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 #  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 #  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 #  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #
27
28 set(CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS true)
29
30 macro(lyx_add_path _list _prefix)
31         set(_tmp)
32         foreach(_current ${${_list}})
33                 set(_tmp ${_tmp} ${_prefix}/${_current})
34         endforeach(_current)
35         set(${_list} ${_tmp})
36 endmacro(lyx_add_path _out _prefix)
37
38
39 #create the implementation files from the ui files and add them
40 #to the list of sources
41 #usage: LYX_ADD_QT4_UI_FILES(foo_SRCS ${ui_files})
42 macro(LYX_ADD_UI_FILES _sources _ui)
43         foreach (_current_FILE ${ARGN})
44
45                 get_filename_component(_tmp_FILE ${_current_FILE} ABSOLUTE)
46                 get_filename_component(_basename ${_tmp_FILE} NAME_WE)
47                 set(_header ${CMAKE_CURRENT_BINARY_DIR}/ui_${_basename}.h)
48
49                 # we need to run uic and replace some things in the generated file
50                 # this is done by executing the cmake script LyXuic.cmake
51                 # ######
52                 # Latest test showed on linux and windows show no bad consequeces,
53                 # so we removed the call to LyXuic.cmake
54                 qt4_wrap_ui(${_header} ${_tmp_FILE} OPTIONS -tr lyx::qt_)
55                 set(${_ui} ${${_ui}} ${_header})
56         endforeach (_current_FILE)
57 endmacro(LYX_ADD_UI_FILES)
58
59
60
61 macro(LYX_AUTOMOC)
62         if (QT4_GET_MOC_INC_DIRS)
63                 QT4_GET_MOC_INC_DIRS(_moc_INCS)
64         endif()
65
66         set(_matching_FILES)
67         foreach (_current_FILE ${ARGN})
68
69                 get_filename_component(_abs_FILE ${_current_FILE} ABSOLUTE)
70                 # if "SKIP_AUTOMOC" is set to true, we will not handle this file here.
71                 # here. this is required to make bouic work correctly:
72                 # we need to add generated .cpp files to the sources (to compile them),
73                 # but we cannot let automoc handle them, as the .cpp files don't exist yet when
74                 # cmake is run for the very first time on them -> however the .cpp files might
75                 # exist at a later run. at that time we need to skip them, so that we don't add two
76                 # different rules for the same moc file
77                 get_source_file_property(_skip ${_abs_FILE} SKIP_AUTOMOC)
78
79                 if (EXISTS ${_abs_FILE} AND NOT _skip)
80
81                         file(READ ${_abs_FILE} _contents)
82
83                         get_filename_component(_abs_PATH ${_abs_FILE} PATH)
84
85                         string(REGEX MATCHALL "#include +[\"<]moc_[^ ]+\\.cpp[\">]" _match "${_contents}")
86                         if (_match)
87                                 foreach (_current_MOC_INC ${_match})
88                                         string(REGEX MATCH "moc_[^ <\"]+\\.cpp" _current_MOC "${_current_MOC_INC}")
89
90                                         get_filename_component(_basename ${_current_MOC} NAME_WE)
91
92                                         string(LENGTH ${_basename} _length)
93                                         MATH(EXPR _mocless_length ${_length}-4)
94                                         STRING(SUBSTRING  ${_basename} 4 ${_mocless_length} _mocless_name )
95
96                                         set(_header ${_abs_PATH}/${_mocless_name}.h)
97
98                                         #message(STATUS "moc : ${_header}")
99                                         #set(_header ${CMAKE_CURRENT_SOURCE_DIR}/${_basename}.h)
100                                         #set(_header ${_abs_PATH}/${_basename}.h)
101
102                                         set(_moc  ${CMAKE_CURRENT_BINARY_DIR}/${_current_MOC})
103                                         if (WIN32)
104                                                           set(_def -D_WIN32)
105                                         endif()
106                                         #set(_moc ${_abs_PATH}/${_current_MOC})
107                                         add_custom_command(OUTPUT ${_moc}
108                                                           COMMAND ${QT_MOC_EXECUTABLE}
109                                                           ARGS "-DQT_VERSION=${QT4_VERSION}" ${_def} ${_moc_INCS} ${_header} -o ${_moc}
110                                                           MAIN_DEPENDENCY ${_header})
111                                         macro_add_file_dependencies(${_abs_FILE} ${_moc})
112                                         SET_SOURCE_FILES_PROPERTIES(${_moc} GENERATED)
113                                 endforeach (_current_MOC_INC)
114                         else()
115                                 #message(STATUS "moc not found : ${_abs_FILE} ")
116                         endif()
117                 endif()
118         endforeach (_current_FILE)
119 endmacro (LYX_AUTOMOC)
120
121
122 macro(lyx_const_touched_files _allinone_name _list)
123         set(_file_list ${_allinone_name}_files)
124         set(_file_const ${CMAKE_CURRENT_BINARY_DIR}/${_allinone_name}_const.C)
125         set(_file_touched ${CMAKE_CURRENT_BINARY_DIR}/${_allinone_name}_touched.C)
126
127         # don't touch exisiting or non-empty file,
128         # so a cmake re-run doesn't touch all created files
129         set(_rebuild_file_const 0)
130         if (NOT EXISTS ${_file_const})
131                 set(_rebuild_file_const 1)
132         else()
133                 FILE(READ ${_file_const} _file_content)
134                 if (NOT _file_content)
135                         set(_rebuild_file_const 1)
136                 endif()
137         endif()
138
139         set(_rebuild_file_touched 0)
140         if (NOT EXISTS ${_file_touched})
141                 set(_rebuild_file_touched 1)
142         else()
143                 FILE(READ ${_file_touched} _file_content)
144                 if (NOT _file_content)
145                         set(_rebuild_file_touched 1)
146                 endif()
147         endif()
148
149         if (LYX_MERGE_REBUILD)
150                 #message(STATUS "Merge files build: rebuilding generated files")
151                 set(_rebuild_file_const 1)
152                 set(_rebuild_file_touched 1)
153         endif()
154
155         if (_rebuild_file_const)
156                 file(WRITE  ${_file_const} "// autogenerated file \n//\n")
157                 file(APPEND ${_file_const} "//   * clear or delete this file to build it again by cmake \n//\n\n")
158         endif()
159
160         if (_rebuild_file_touched)
161                 file(WRITE  ${_file_touched} "// autogenerated file \n//\n")
162                 file(APPEND ${_file_touched} "//         * clear or delete this file to build it again by cmake \n//\n")
163                 file(APPEND ${_file_touched} "//         * don't touch this file \n//\n\n")
164                 file(APPEND ${_file_touched} "#define DONT_INCLUDE_CONST_FILES\n")
165                 file(APPEND ${_file_touched} "#include \"${_file_const}\"\n\n\n")
166         endif()
167
168         #add merged files also to the project so they become editable
169                 if(${GROUP_CODE} MATCHES "flat")
170                 lyx_add_info_files_no_group(${${_list}})
171         else()
172                 lyx_add_info_files(MergedFiles ${${_list}})
173         endif()
174         
175         set(${_file_list} ${_file_const} ${_file_touched} ${lyx_${groupname}_info_files})
176
177         foreach (_current_FILE ${${_list}})
178                 get_filename_component(_abs_FILE ${_current_FILE} ABSOLUTE)
179                 # don't include any generated files in the final-file
180                 # because then cmake will not know the dependencies
181                 get_source_file_property(_isGenerated ${_abs_FILE} GENERATED)
182                 if (_isGenerated)
183                         list(APPEND ${_file_list} ${_abs_FILE})
184                 else()
185                   GET_FILENAME_COMPONENT(_file_name ${_abs_FILE} NAME_WE)
186                   STRING(REGEX REPLACE "-" "_" _file_name "${_file_name}")
187                   set(__macro_name ${_file_name}___ASSUME_CONST)
188
189                   if (_rebuild_file_const)
190                           file(APPEND ${_file_const}  "#define ${__macro_name}\n")
191                           file(APPEND ${_file_const}  "#if defined(${__macro_name}) && !defined(DONT_INCLUDE_CONST_FILES)\n")
192                           file(APPEND ${_file_const}  "#include \"${_abs_FILE}\"\n")
193                           file(APPEND ${_file_const}  "#endif\n\n")
194                   endif()
195
196                   if (_rebuild_file_touched)
197                           file(APPEND ${_file_touched}  "#ifndef ${__macro_name}\n")
198                           file(APPEND ${_file_touched}  "#include \"${_abs_FILE}\"\n")
199                           file(APPEND ${_file_touched}  "#endif\n\n")
200                   endif()
201                 endif()
202         endforeach (_current_FILE)
203 endmacro(lyx_const_touched_files)
204
205 macro(LYX_OPTION_INIT)
206         set(LYX_OPTIONS)
207 endmacro()
208
209
210 macro(LYX_OPTION _name _description _default _sys)
211         set(_msg OFF)
212         if(${_sys} MATCHES "GCC")
213                 set(_system CMAKE_COMPILER_IS_GNUCXX)
214         elseif(${_sys} MATCHES "MAC")
215                 set(_system APPLE)
216         else()
217                 set(_system ${_sys})
218         endif()
219         if(${_system} MATCHES "ALL")
220                 option(LYX_${_name} ${_description} ${_default})
221                 set(_msg ON)
222         else()
223                 if(${${_system}})
224                         option(LYX_${_name} ${_description} ${_default})
225                         set(_msg ON)
226                 endif()
227         endif()
228         list(APPEND LYX_OPTIONS LYX_${_name})
229         set(LYX_${_name}_description ${_description})
230         set(LYX_${_name}_show_message ${_msg})
231 endmacro()
232
233
234 macro(LYX_OPTION_LIST_ALL)
235         if(UNIX)
236                 set(run_cmake ${CMAKE_BINARY_DIR}/run_cmake.sh)
237                 file(WRITE ${run_cmake} "#!/bin/bash \n")
238                 execute_process(COMMAND chmod 755 ${run_cmake})
239                 set(cont "\\\n")
240         elseif(WIN32)
241                 set(run_cmake ${CMAKE_BINARY_DIR}/run_cmake.bat)
242                 file(WRITE ${run_cmake} "")
243                 set(cont "<nul ^\n")
244         endif()
245         file(APPEND ${run_cmake} "cmake ${CMAKE_SOURCE_DIR}  ${cont}")
246         file(APPEND ${run_cmake} " -G\"${CMAKE_GENERATOR}\"  ${cont}")
247         foreach(_option ${LYX_OPTIONS})
248                 if(${_option}_show_message OR ${ARGV0} STREQUAL "help")
249                         string(SUBSTRING "${_option}                            " 0 25 _var)
250                         if(${_option})
251                                 set(_isset ON)
252                         else()
253                                 set(_isset OFF)
254                         endif()
255                         string(SUBSTRING "${_isset}     " 0 4 _val)
256                         message(STATUS "${_var}= ${_val}   : ${${_option}_description}")
257                         file(APPEND ${run_cmake} " -D${_option}=${${_option}}  ${cont}")
258                 endif()
259         endforeach()
260         file(APPEND ${run_cmake} "\n")
261         message(STATUS)
262         message(STATUS "CMake command with options is available in shell script")
263         message(STATUS "    '${run_cmake}'")
264 endmacro()
265
266 macro(lyx_add_info_files group)
267         foreach(_it ${ARGN})
268                 if(NOT IS_DIRECTORY ${_it})
269                         get_filename_component(name ${_it} NAME)
270                         if(NOT ${_it} MATCHES "^/\\\\..*$;~$")
271                                 set_source_files_properties(${_it} PROPERTIES HEADER_FILE_ONLY TRUE)
272                                 set(lyx_${group}_info_files ${lyx_${group}_info_files} ${_it})
273                         endif()
274                 endif()
275         endforeach()
276         set(check_group ${group}) #cmake bug?
277         if(check_group)
278                 source_group(${group} FILES ${lyx_${group}_info_files})
279         endif()
280         set(lyx_info_files ${lyx_info_files} ${lyx_${group}_info_files})
281 endmacro()
282
283 macro(lyx_add_info_files_no_group)
284         lyx_add_info_files( "" ${ARGN})
285 endmacro()
286
287 macro(lyx_find_info_files group files)
288         file(GLOB _filelist ${files})
289         lyx_add_info_files(${group} ${_filelist})
290 endmacro()