]> git.lyx.org Git - lyx.git/blob - development/cmake/modules/LyXMacros.cmake
816112eb544f9f54c3ad1dccd7f7ec3007be89f7
[lyx.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                 add_custom_command(OUTPUT ${_header}
55          COMMAND ${QT_UIC_EXECUTABLE} -tr lyx::qt_ ${_tmp_FILE} -o ${_header}
56                         MAIN_DEPENDENCY ${_tmp_FILE})
57                 set(${_ui} ${${_ui}} ${_header})
58         endforeach (_current_FILE)
59 endmacro(LYX_ADD_UI_FILES)
60
61
62
63 macro(LYX_AUTOMOC)
64         if (QT4_GET_MOC_INC_DIRS)
65                 QT4_GET_MOC_INC_DIRS(_moc_INCS)
66         endif()
67
68         set(_matching_FILES)
69         foreach (_current_FILE ${ARGN})
70
71                 get_filename_component(_abs_FILE ${_current_FILE} ABSOLUTE)
72                 # if "SKIP_AUTOMOC" is set to true, we will not handle this file here.
73                 # here. this is required to make bouic work correctly:
74                 # we need to add generated .cpp files to the sources (to compile them),
75                 # but we cannot let automoc handle them, as the .cpp files don't exist yet when
76                 # cmake is run for the very first time on them -> however the .cpp files might
77                 # exist at a later run. at that time we need to skip them, so that we don't add two
78                 # different rules for the same moc file
79                 get_source_file_property(_skip ${_abs_FILE} SKIP_AUTOMOC)
80
81                 if (EXISTS ${_abs_FILE} AND NOT _skip)
82
83                         file(READ ${_abs_FILE} _contents)
84
85                         get_filename_component(_abs_PATH ${_abs_FILE} PATH)
86
87                         string(REGEX MATCHALL "#include +[\"<]moc_[^ ]+\\.cpp[\">]" _match "${_contents}")
88                         if (_match)
89                                 foreach (_current_MOC_INC ${_match})
90                                         string(REGEX MATCH "moc_[^ <\"]+\\.cpp" _current_MOC "${_current_MOC_INC}")
91
92                                         get_filename_component(_basename ${_current_MOC} NAME_WE)
93
94                                         string(LENGTH ${_basename} _length)
95                                         MATH(EXPR _mocless_length ${_length}-4)
96                                         STRING(SUBSTRING  ${_basename} 4 ${_mocless_length} _mocless_name )
97
98                                         set(_header ${_abs_PATH}/${_mocless_name}.h)
99
100                                         #message(STATUS "moc : ${_header}")
101                                         #set(_header ${CMAKE_CURRENT_SOURCE_DIR}/${_basename}.h)
102                                         #set(_header ${_abs_PATH}/${_basename}.h)
103
104                                         set(_moc  ${CMAKE_CURRENT_BINARY_DIR}/${_current_MOC})
105                                         if (WIN32)
106                                                           set(_def -D_WIN32)
107                                         endif()
108                                         #set(_moc ${_abs_PATH}/${_current_MOC})
109                                         add_custom_command(OUTPUT ${_moc}
110                                                           COMMAND ${QT_MOC_EXECUTABLE}
111                                                           ARGS "-DQT_VERSION=${QT4_VERSION}" ${_def} ${_moc_INCS} ${_header} -o ${_moc}
112                                                           MAIN_DEPENDENCY ${_header})
113                                         macro_add_file_dependencies(${_abs_FILE} ${_moc})
114                                         SET_SOURCE_FILES_PROPERTIES(${_moc} GENERATED)
115                                 endforeach (_current_MOC_INC)
116                         else()
117                                 #message(STATUS "moc not found : ${_abs_FILE} ")
118                         endif()
119                 endif()
120         endforeach (_current_FILE)
121 endmacro (LYX_AUTOMOC)
122
123
124 macro(lyx_const_touched_files _allinone_name _list)
125         set(_file_list ${_allinone_name}_files)
126         set(_file_const ${CMAKE_CURRENT_BINARY_DIR}/${_allinone_name}_const.C)
127         set(_file_touched ${CMAKE_CURRENT_BINARY_DIR}/${_allinone_name}_touched.C)
128
129         # don't touch exisiting or non-empty file,
130         # so a cmake re-run doesn't touch all created files
131         set(_rebuild_file_const 0)
132         if (NOT EXISTS ${_file_const})
133                 set(_rebuild_file_const 1)
134         else()
135                 FILE(READ ${_file_const} _file_content)
136                 if (NOT _file_content)
137                         set(_rebuild_file_const 1)
138                 endif()
139         endif()
140
141         set(_rebuild_file_touched 0)
142         if (NOT EXISTS ${_file_touched})
143                 set(_rebuild_file_touched 1)
144         else()
145                 FILE(READ ${_file_touched} _file_content)
146                 if (NOT _file_content)
147                         set(_rebuild_file_touched 1)
148                 endif()
149         endif()
150
151         if (LYX_MERGE_REBUILD)
152                 #message(STATUS "Merge files build: rebuilding generated files")
153                 set(_rebuild_file_const 1)
154                 set(_rebuild_file_touched 1)
155         endif()
156
157         if (_rebuild_file_const)
158                 file(WRITE  ${_file_const} "// autogenerated file \n//\n")
159                 file(APPEND ${_file_const} "//   * clear or delete this file to build it again by cmake \n//\n\n")
160         endif()
161
162         if (_rebuild_file_touched)
163                 file(WRITE  ${_file_touched} "// autogenerated file \n//\n")
164                 file(APPEND ${_file_touched} "//         * clear or delete this file to build it again by cmake \n//\n")
165                 file(APPEND ${_file_touched} "//         * don't touch this file \n//\n\n")
166                 file(APPEND ${_file_touched} "#define DONT_INCLUDE_CONST_FILES\n")
167                 file(APPEND ${_file_touched} "#include \"${_file_const}\"\n\n\n")
168         endif()
169
170         #add merged files also to the project so they become editable
171                 if(${GROUP_CODE} MATCHES "flat")
172                 lyx_add_info_files_no_group(${${_list}})
173         else()
174                 lyx_add_info_files(MergedFiles ${${_list}})
175         endif()
176         
177         set(${_file_list} ${_file_const} ${_file_touched} ${lyx_${groupname}_info_files})
178
179         foreach (_current_FILE ${${_list}})
180                 get_filename_component(_abs_FILE ${_current_FILE} ABSOLUTE)
181                 # don't include any generated files in the final-file
182                 # because then cmake will not know the dependencies
183                 get_source_file_property(_isGenerated ${_abs_FILE} GENERATED)
184                 if (_isGenerated)
185                         list(APPEND ${_file_list} ${_abs_FILE})
186                 else()
187                   GET_FILENAME_COMPONENT(_file_name ${_abs_FILE} NAME_WE)
188                   STRING(REGEX REPLACE "-" "_" _file_name "${_file_name}")
189                   set(__macro_name ${_file_name}___ASSUME_CONST)
190
191                   if (_rebuild_file_const)
192                           file(APPEND ${_file_const}  "#define ${__macro_name}\n")
193                           file(APPEND ${_file_const}  "#if defined(${__macro_name}) && !defined(DONT_INCLUDE_CONST_FILES)\n")
194                           file(APPEND ${_file_const}  "#include \"${_abs_FILE}\"\n")
195                           file(APPEND ${_file_const}  "#endif\n\n")
196                   endif()
197
198                   if (_rebuild_file_touched)
199                           file(APPEND ${_file_touched}  "#ifndef ${__macro_name}\n")
200                           file(APPEND ${_file_touched}  "#include \"${_abs_FILE}\"\n")
201                           file(APPEND ${_file_touched}  "#endif\n\n")
202                   endif()
203                 endif()
204         endforeach (_current_FILE)
205 endmacro(lyx_const_touched_files)
206
207
208 macro(lyx_qt_resources_file _qrc_name _to_dir _list)
209         if (NOT EXISTS ${_qrc_name})
210                 set(_rebuild_file 1)
211         else()
212                 FILE(READ ${_qrc_name} _file_content)
213                 if (NOT _file_content)
214                         set(_rebuild_file 1)
215                 endif()
216         endif()
217
218         if (_rebuild_file)
219                 message(STATUS "Generating ${_qrc_name}")
220                 file(WRITE  ${_qrc_name} "<!DOCTYPE RCC><RCC version=\"1.0\">\n")
221                 file(APPEND  ${_qrc_name} "<qresource>\n")
222
223                 foreach (_current_FILE ${${_list}})
224                         get_filename_component(_abs_FILE ${_current_FILE} ABSOLUTE)
225                         string(REGEX REPLACE "${_to_dir}" "" _file_name ${_abs_FILE})
226                         file(APPEND  ${_qrc_name} "      <file alias=\"${_file_name}\">${_abs_FILE}</file>\n")
227                 endforeach (_current_FILE)
228
229                 file(APPEND  ${_qrc_name} "</qresource>\n")
230                 file(APPEND  ${_qrc_name} "</RCC>\n")
231         endif()
232         if(NOT WIN32)
233           add_custom_command(
234             OUTPUT ${_qrc_name}
235             COMMAND ${CMAKE_COMMAND} --build "${CMAKE_BINARY_DIR}" --target rebuild_cache
236             )
237         endif()
238
239 endmacro(lyx_qt_resources_file)
240
241
242 macro(LYX_OPTION_INIT)
243         set(LYX_OPTIONS)
244 endmacro()
245
246
247 macro(LYX_OPTION _name _description _default _sys)
248         set(_msg OFF)
249         if(${_sys} MATCHES "GCC")
250                 set(_system CMAKE_COMPILER_IS_GNUCXX)
251         elseif(${_sys} MATCHES "MAC")
252                 set(_system APPLE)
253         else()
254                 set(_system ${_sys})
255         endif()
256         if(${_system} MATCHES "ALL")
257                 option(LYX_${_name} ${_description} ${_default})
258                 set(_msg ON)
259         else()
260                 if(${${_system}})
261                         option(LYX_${_name} ${_description} ${_default})
262                         set(_msg ON)
263                 endif()
264         endif()
265         list(APPEND LYX_OPTIONS LYX_${_name})
266         set(LYX_${_name}_description ${_description})
267         set(LYX_${_name}_show_message ${_msg})
268 endmacro()
269
270
271 macro(LYX_OPTION_LIST_ALL)
272         if(UNIX)
273                 set(run_cmake ${CMAKE_BINARY_DIR}/run_cmake.sh)
274                 file(WRITE ${run_cmake} "#!/bin/bash \n")
275                 execute_process(COMMAND chmod 755 ${run_cmake})
276                 set(cont "\\\n")
277         elseif(WIN32)
278                 set(run_cmake ${CMAKE_BINARY_DIR}/run_cmake.bat)
279                 file(WRITE ${run_cmake} "")
280                 set(cont "<nul ^\n")
281         endif()
282         file(APPEND ${run_cmake} "cmake ${CMAKE_SOURCE_DIR}  ${cont}")
283         file(APPEND ${run_cmake} " -G\"${CMAKE_GENERATOR}\"  ${cont}")
284         foreach(_option ${LYX_OPTIONS})
285                 if(${_option}_show_message OR ${ARGV0} STREQUAL "help")
286                         string(SUBSTRING "${_option}                            " 0 25 _var)
287                         if(${_option})
288                                 set(_isset ON)
289                         else()
290                                 set(_isset OFF)
291                         endif()
292                         string(SUBSTRING "${_isset}     " 0 4 _val)
293                         message(STATUS "${_var}= ${_val}   : ${${_option}_description}")
294                         file(APPEND ${run_cmake} " -D${_option}=${${_option}}  ${cont}")
295                 endif()
296         endforeach()
297         file(APPEND ${run_cmake} "\n")
298         message(STATUS)
299         message(STATUS "CMake command with options is available in shell script")
300         message(STATUS "    '${run_cmake}'")
301 endmacro()
302
303 macro(lyx_add_info_files group)
304         foreach(_it ${ARGN})
305                 if(NOT IS_DIRECTORY ${_it})
306                         get_filename_component(name ${_it} NAME)
307                         if(NOT ${_it} MATCHES "^/\\\\..*$;~$")
308                                 set_source_files_properties(${_it} PROPERTIES HEADER_FILE_ONLY TRUE)
309                                 set(lyx_${group}_info_files ${lyx_${group}_info_files} ${_it})
310                         endif()
311                 endif()
312         endforeach()
313         set(check_group ${group}) #cmake bug?
314         if(check_group)
315                 source_group(${group} FILES ${lyx_${group}_info_files})
316         endif()
317         set(lyx_info_files ${lyx_info_files} ${lyx_${group}_info_files})
318 endmacro()
319
320 macro(lyx_add_info_files_no_group)
321         lyx_add_info_files( "" ${ARGN})
322 endmacro()
323
324 macro(lyx_find_info_files group files)
325         file(GLOB _filelist ${files})
326         lyx_add_info_files(${group} ${_filelist})
327 endmacro()