]> git.lyx.org Git - lyx.git/blob - development/cmake/modules/LyXMacros.cmake
cmake: more cleanup
[lyx.git] / development / cmake / modules / LyXMacros.cmake
1 #
2 #  Copyright (c) 2006, 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       #message(${_prefix}/${_current})
35    endforeach(_current)
36    set(${_list} ${_tmp})
37 endmacro(lyx_add_path _out _prefix)
38
39
40 #create the implementation files from the ui files and add them
41 # to the list of sources
42 #usage: LYX_ADD_QT4_UI_FILES(foo_SRCS ${ui_files})
43 macro(LYX_ADD_UI_FILES _sources _ui)
44    foreach (_current_FILE ${ARGN})
45
46       get_filename_component(_tmp_FILE ${_current_FILE} ABSOLUTE)
47       get_filename_component(_basename ${_tmp_FILE} NAME_WE)
48       set(_header ${CMAKE_CURRENT_BINARY_DIR}/ui_${_basename}.h)
49
50       # we need to run uic and replace some things in the generated file
51       # this is done by executing the cmake script LyXuic.cmake
52       # ######
53       # Latest test showed on linux and windows show no bad consequeces,
54       # so we removed the call to LyXuic.cmake
55       add_custom_command(OUTPUT ${_header}
56          COMMAND ${QT_UIC_EXECUTABLE} -tr lyx::qt_ ${_tmp_FILE} -o ${_header}
57          MAIN_DEPENDENCY ${_tmp_FILE}
58 )
59       set(${_ui} ${${_ui}} ${_header})
60    endforeach (_current_FILE)
61 endmacro(LYX_ADD_UI_FILES)
62
63
64
65 macro(LYX_AUTOMOC)
66    if (QT4_GET_MOC_INC_DIRS)
67       QT4_GET_MOC_INC_DIRS(_moc_INCS)
68    endif()
69
70    set(_matching_FILES)
71    foreach (_current_FILE ${ARGN})
72
73       get_filename_component(_abs_FILE ${_current_FILE} ABSOLUTE)
74       # if "SKIP_AUTOMOC" is set to true, we will not handle this file here.
75       # here. this is required to make bouic work correctly:
76       # we need to add generated .cpp files to the sources (to compile them),
77       # but we cannot let automoc handle them, as the .cpp files don't exist yet when
78       # cmake is run for the very first time on them -> however the .cpp files might
79       # exist at a later run. at that time we need to skip them, so that we don't add two
80       # different rules for the same moc file
81       get_source_file_property(_skip ${_abs_FILE} SKIP_AUTOMOC)
82
83       if (EXISTS ${_abs_FILE} AND NOT _skip)
84
85          file(READ ${_abs_FILE} _contents)
86
87          get_filename_component(_abs_PATH ${_abs_FILE} PATH)
88
89          string(REGEX MATCHALL "#include +[\"<]moc_[^ ]+\\.cpp[\">]" _match "${_contents}")
90          if (_match)
91             foreach (_current_MOC_INC ${_match})
92                string(REGEX MATCH "moc_[^ <\"]+\\.cpp" _current_MOC "${_current_MOC_INC}")
93
94                get_filename_component(_basename ${_current_MOC} NAME_WE)
95
96                string(LENGTH ${_basename} _length)
97                MATH(EXPR _mocless_length ${_length}-4)
98                STRING(SUBSTRING  ${_basename} 4 ${_mocless_length} _mocless_name )
99
100                set(_header ${_abs_PATH}/${_mocless_name}.h)
101
102                #message(STATUS "moc : ${_header}")
103                #set(_header ${CMAKE_CURRENT_SOURCE_DIR}/${_basename}.h)
104                #set(_header ${_abs_PATH}/${_basename}.h)
105
106                set(_moc  ${CMAKE_CURRENT_BINARY_DIR}/${_current_MOC})
107                if (WIN32)
108                        set(_def -D_WIN32)
109                endif()
110                #set(_moc ${_abs_PATH}/${_current_MOC})
111                add_custom_command(OUTPUT ${_moc}
112                        COMMAND ${QT_MOC_EXECUTABLE}
113                        ARGS ${_def} ${_moc_INCS} ${_header} -o ${_moc}
114                        MAIN_DEPENDENCY ${_header})
115                macro_add_file_dependencies(${_abs_FILE} ${_moc})
116             endforeach (_current_MOC_INC)
117          else()
118             #message(STATUS "moc not found : ${_abs_FILE} ")
119          endif()
120
121       endif()
122    endforeach (_current_FILE)
123 endmacro (LYX_AUTOMOC)
124
125
126 macro(lyx_const_touched_files _allinone_name _list)
127    set(_file_list ${_allinone_name}_files)
128    set(_file_const ${CMAKE_CURRENT_BINARY_DIR}/${_allinone_name}_const.C)
129    set(_file_touched ${CMAKE_CURRENT_BINARY_DIR}/${_allinone_name}_touched.C)
130
131
132    # don't touch exisiting or non-empty file,
133    # so a cmake re-run doesn't touch all created files
134    set(_rebuild_file_const 0)
135    if (NOT EXISTS ${_file_const})
136       set(_rebuild_file_const 1)
137    else()
138       FILE(READ ${_file_const} _file_content)
139       if (NOT _file_content)
140          set(_rebuild_file_const 1)
141       endif()
142    endif()
143
144    set(_rebuild_file_touched 0)
145    if (NOT EXISTS ${_file_touched})
146       set(_rebuild_file_touched 1)
147    else()
148       FILE(READ ${_file_touched} _file_content)
149       if (NOT _file_content)
150          set(_rebuild_file_touched 1)
151       endif()
152    endif()
153
154    if (merge_rebuild)
155       set(_rebuild_file_const 1)
156       set(_rebuild_file_touched 1)
157    endif()
158
159    if (_rebuild_file_const)
160       file(WRITE  ${_file_const} "// autogenerated file \n//\n")
161       file(APPEND ${_file_const} "//    * clear or delete this file to build it again by cmake \n//\n\n")
162    endif()
163
164    if (_rebuild_file_touched)
165       file(WRITE  ${_file_touched} "// autogenerated file \n//\n")
166       file(APPEND ${_file_touched} "//    * clear or delete this file to build it again by cmake \n//\n")
167       file(APPEND ${_file_touched} "//    * don't touch this file \n//\n\n")
168       file(APPEND ${_file_touched} "#define DONT_INCLUDE_CONST_FILES\n")
169       file(APPEND ${_file_touched} "#include \"${_file_const}\"\n\n\n")
170    endif()
171
172    set(${_file_list} ${_file_const} ${_file_touched})
173
174    foreach (_current_FILE ${${_list}})
175       get_filename_component(_abs_FILE ${_current_FILE} ABSOLUTE)
176       # don't include any generated files in the final-file
177       # because then cmake will not know the dependencies
178       get_source_file_property(_isGenerated ${_abs_FILE} GENERATED)
179       if (_isGenerated)
180          list(APPEND ${_file_list} ${_abs_FILE})
181       else()
182         GET_FILENAME_COMPONENT(_file_name ${_abs_FILE} NAME_WE)
183         STRING(REGEX REPLACE "-" "_" _file_name "${_file_name}")
184         set(__macro_name ${_file_name}___ASSUME_CONST)
185
186         if (_rebuild_file_const)
187            file(APPEND ${_file_const}  "#define ${__macro_name}\n")
188            file(APPEND ${_file_const}  "#if defined(${__macro_name}) && !defined(DONT_INCLUDE_CONST_FILES)\n")
189            file(APPEND ${_file_const}  "#include \"${_abs_FILE}\"\n")
190            file(APPEND ${_file_const}  "#endif\n\n")
191         endif()
192
193         if (_rebuild_file_touched)
194            file(APPEND ${_file_touched}  "#ifndef ${__macro_name}\n")
195            file(APPEND ${_file_touched}  "#include \"${_abs_FILE}\"\n")
196            file(APPEND ${_file_touched}  "#endif\n\n")
197         endif()
198       endif()
199    endforeach (_current_FILE)
200 endmacro(lyx_const_touched_files)
201
202
203 macro(lyx_qt_resources_file _qrc_name _to_dir _list)
204    if (NOT EXISTS ${_qrc_name})
205       set(_rebuild_file 1)
206    else()
207       FILE(READ ${_qrc_name} _file_content)
208       if (NOT _file_content)
209          set(_rebuild_file 1)
210       endif()
211    endif()
212
213    if (_rebuild_file)
214       message(STATUS "Generating ${_qrc_name}")
215       file(WRITE  ${_qrc_name} "<!DOCTYPE RCC><RCC version=\"1.0\">\n")
216       file(APPEND  ${_qrc_name} "<qresource>\n")
217
218       foreach (_current_FILE ${${_list}})
219          get_filename_component(_abs_FILE ${_current_FILE} ABSOLUTE)
220          string(REGEX REPLACE "${_to_dir}" "" _file_name ${_abs_FILE})
221          file(APPEND  ${_qrc_name} "    <file alias=\"${_file_name}\">${_abs_FILE}</file>\n")
222       endforeach (_current_FILE)
223
224       file(APPEND  ${_qrc_name} "</qresource>\n")
225       file(APPEND  ${_qrc_name} "</RCC>\n")
226    endif()
227 endmacro(lyx_qt_resources_file)
228
229
230 macro(LYX_OPTION _name _description _default _sys)
231         set(_msg OFF)
232         if(${_sys} MATCHES "GCC")
233                 set(_system CMAKE_COMPILER_IS_GNUCXX)
234         else()
235                 set(_system ${_sys})
236         endif()
237         if(${_system} MATCHES "ALL")
238                 option(LYX_${_name} ${_description} ${_default})
239                 set(_msg ON)
240         else()
241                 if(${${_system}})
242                         option(LYX_${_name} ${_description} ${_default})
243                         set(_msg ON)
244                 endif()
245         endif()
246         if(_msg)
247                 string(SUBSTRING "LYX_${_name}                            " 0 25 _var)
248                 string(SUBSTRING "${LYX_${_name}}     " 0 4 _val)
249                 message(STATUS "${_var}: ${_val}   (${_description})")
250         endif()
251 endmacro()
252