2 # Copyright (c) 2006-2011 Peter Kümmel, <syntheticpp@gmx.net>
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions
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.
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.
28 include (MacroAddFileDependencies)
30 set(CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS true)
32 macro(lyx_add_path _list _prefix)
34 foreach(_current ${${_list}})
35 set(_tmp ${_tmp} ${_prefix}/${_current})
38 endmacro(lyx_add_path _out _prefix)
41 #create the implementation files from the ui files and add them
42 #to the list of sources
43 #usage: LYX_ADD_QT4_UI_FILES(foo_SRCS ${ui_files})
44 macro(LYX_ADD_UI_FILES _sources _ui_files)
46 foreach (_current_FILE ${ARGN})
47 get_filename_component(_tmp_FILE ${_current_FILE} ABSOLUTE)
48 get_filename_component(_basename ${_tmp_FILE} NAME_WE)
49 set(_header ${CMAKE_CURRENT_BINARY_DIR}/ui_${_basename}.h)
51 # we need to run uic and replace some things in the generated file
52 # this is done by executing the cmake script LyXuic.cmake
54 # Latest test showed on linux and windows show no bad consequeces,
55 # so we removed the call to LyXuic.cmake
56 qt_wrap_uifiles(${_header} ${_tmp_FILE} OPTIONS -tr lyx::qt_)
57 list(APPEND uifiles ${_header})
59 set(${_ui_files} ${uifiles})
60 endmacro(LYX_ADD_UI_FILES)
65 if (QT4_GET_MOC_INC_DIRS)
66 QT4_GET_MOC_INC_DIRS(_moc_INCS)
70 foreach (_current_FILE ${ARGN})
72 get_filename_component(_abs_FILE ${_current_FILE} ABSOLUTE)
73 # if "SKIP_AUTOMOC" is set to true, we will not handle this file here.
74 # here. this is required to make bouic work correctly:
75 # we need to add generated .cpp files to the sources (to compile them),
76 # but we cannot let automoc handle them, as the .cpp files don't exist yet when
77 # cmake is run for the very first time on them -> however the .cpp files might
78 # exist at a later run. at that time we need to skip them, so that we don't add two
79 # different rules for the same moc file
80 get_source_file_property(_skip ${_abs_FILE} SKIP_AUTOMOC)
82 if (EXISTS ${_abs_FILE} AND NOT _skip)
84 file(READ ${_abs_FILE} _contents)
86 get_filename_component(_abs_PATH ${_abs_FILE} PATH)
88 string(REGEX MATCHALL "#include +[\"<]moc_[^ ]+\\.cpp[\">]" _match "${_contents}")
90 foreach (_current_MOC_INC ${_match})
91 string(REGEX MATCH "moc_[^ <\"]+\\.cpp" _current_MOC "${_current_MOC_INC}")
93 get_filename_component(_basename ${_current_MOC} NAME_WE)
95 string(LENGTH ${_basename} _length)
96 MATH(EXPR _mocless_length ${_length}-4)
97 STRING(SUBSTRING ${_basename} 4 ${_mocless_length} _mocless_name )
99 set(_header ${_abs_PATH}/${_mocless_name}.h)
101 #message(STATUS "moc : ${_header}")
102 #set(_header ${CMAKE_CURRENT_SOURCE_DIR}/${_basename}.h)
103 #set(_header ${_abs_PATH}/${_basename}.h)
105 set(_moc ${CMAKE_CURRENT_BINARY_DIR}/${_current_MOC})
109 #set(_moc ${_abs_PATH}/${_current_MOC})
110 add_custom_command(OUTPUT ${_moc}
111 COMMAND ${QT_MOC_EXECUTABLE}
112 ARGS "-DQT_VERSION=${QT4_VERSION}" ${_def} ${_moc_INCS} ${_header} -o ${_moc}
113 MAIN_DEPENDENCY ${_header})
114 macro_add_file_dependencies(${_abs_FILE} ${_moc})
115 SET_SOURCE_FILES_PROPERTIES(${_moc} GENERATED)
116 endforeach (_current_MOC_INC)
118 #message(STATUS "moc not found : ${_abs_FILE} ")
121 endforeach (_current_FILE)
122 endmacro (LYX_AUTOMOC)
125 macro(lyx_const_touched_files _allinone_name _list)
126 set(_file_list ${_allinone_name}_files)
127 set(_file_const ${CMAKE_CURRENT_BINARY_DIR}/${_allinone_name}_const.C)
128 set(_file_touched ${CMAKE_CURRENT_BINARY_DIR}/${_allinone_name}_touched.C)
130 # don't touch exisiting or non-empty file,
131 # so a cmake re-run doesn't touch all created files
132 set(_rebuild_file_const 0)
133 if (NOT EXISTS ${_file_const})
134 set(_rebuild_file_const 1)
136 FILE(READ ${_file_const} _file_content)
137 if (NOT _file_content)
138 set(_rebuild_file_const 1)
142 set(_rebuild_file_touched 0)
143 if (NOT EXISTS ${_file_touched})
144 set(_rebuild_file_touched 1)
146 FILE(READ ${_file_touched} _file_content)
147 if (NOT _file_content)
148 set(_rebuild_file_touched 1)
152 if (LYX_MERGE_REBUILD)
153 #message(STATUS "Merge files build: rebuilding generated files")
154 set(_rebuild_file_const 1)
155 set(_rebuild_file_touched 1)
158 if (_rebuild_file_const)
159 file(WRITE ${_file_const} "// autogenerated file \n//\n")
160 file(APPEND ${_file_const} "// * clear or delete this file to build it again by cmake \n//\n\n")
163 if (_rebuild_file_touched)
164 file(WRITE ${_file_touched} "// autogenerated file \n//\n")
165 file(APPEND ${_file_touched} "// * clear or delete this file to build it again by cmake \n//\n")
166 file(APPEND ${_file_touched} "// * don't touch this file \n//\n\n")
167 file(APPEND ${_file_touched} "#define DONT_INCLUDE_CONST_FILES\n")
168 file(APPEND ${_file_touched} "#include \"${_file_const}\"\n\n\n")
171 #add merged files also to the project so they become editable
172 if(${GROUP_CODE} MATCHES "flat")
173 lyx_add_info_files_no_group(${${_list}})
175 lyx_add_info_files(MergedFiles ${${_list}})
178 set(${_file_list} ${_file_const} ${_file_touched} ${lyx_${groupname}_info_files})
180 foreach (_current_FILE ${${_list}})
181 get_filename_component(_abs_FILE ${_current_FILE} ABSOLUTE)
182 # don't include any generated files in the final-file
183 # because then cmake will not know the dependencies
184 get_source_file_property(_isGenerated ${_abs_FILE} GENERATED)
186 list(APPEND ${_file_list} ${_abs_FILE})
188 GET_FILENAME_COMPONENT(_file_name ${_abs_FILE} NAME_WE)
189 STRING(REGEX REPLACE "-" "_" _file_name "${_file_name}")
190 set(__macro_name ${_file_name}___ASSUME_CONST)
192 if (_rebuild_file_const)
193 file(APPEND ${_file_const} "#define ${__macro_name}\n")
194 file(APPEND ${_file_const} "#if defined(${__macro_name}) && !defined(DONT_INCLUDE_CONST_FILES)\n")
195 file(APPEND ${_file_const} "#include \"${_abs_FILE}\"\n")
196 file(APPEND ${_file_const} "#endif\n\n")
199 if (_rebuild_file_touched)
200 file(APPEND ${_file_touched} "#ifndef ${__macro_name}\n")
201 file(APPEND ${_file_touched} "#include \"${_abs_FILE}\"\n")
202 file(APPEND ${_file_touched} "#endif\n\n")
205 endforeach (_current_FILE)
206 endmacro(lyx_const_touched_files)
208 macro(LYX_OPTION_INIT)
213 macro(LYX_OPTION _name _description _default _sys)
215 if(${_sys} MATCHES "GCC")
216 set(_system CMAKE_COMPILER_IS_GNUCXX)
217 elseif(${_sys} MATCHES "MAC")
222 if(${_system} MATCHES "ALL")
223 option(LYX_${_name} ${_description} ${_default})
227 option(LYX_${_name} ${_description} ${_default})
231 list(APPEND LYX_OPTIONS LYX_${_name})
232 set(LYX_${_name}_description ${_description})
233 set(LYX_${_name}_show_message ${_msg})
236 macro(LYX_COMBO _name _description _default)
237 set(_lyx_name "LYX_${_name}")
238 set(${_lyx_name} ${_default} CACHE STRING "${_description}")
239 set(_combo_list ${_default} ${ARGN})
240 set_property(CACHE ${_lyx_name} PROPERTY STRINGS ${_combo_list})
241 list(APPEND LYX_OPTIONS ${_lyx_name})
242 set(${_lyx_name}_show_message ON)
243 string(REGEX REPLACE ";" " " _use_list "${_combo_list}")
244 set(${_lyx_name}_description "${_description} (${_use_list})")
245 # Now check the value
246 list(FIND _combo_list ${${_lyx_name}} _idx)
248 message(FATAL_ERROR "${_lyx_name} set to \"${${_lyx_name}}\", but has to be only one of (${_use_list})")
252 macro(LYX_OPTION_LIST_ALL)
254 set(run_cmake ${CMAKE_BINARY_DIR}/run_cmake.sh)
255 file(WRITE ${run_cmake} "#!/bin/bash \n")
256 execute_process(COMMAND chmod 755 ${run_cmake})
259 set(run_cmake ${CMAKE_BINARY_DIR}/run_cmake.bat)
260 file(WRITE ${run_cmake} "")
263 file(APPEND ${run_cmake} "cmake ${CMAKE_SOURCE_DIR} ${cont}")
264 file(APPEND ${run_cmake} " -G\"${CMAKE_GENERATOR}\" ${cont}")
265 foreach(_option ${LYX_OPTIONS})
266 if(${_option}_show_message OR ${ARGV0} STREQUAL "help")
267 string(SUBSTRING "${_option} " 0 25 _var)
268 get_property(_prop CACHE ${_option} PROPERTY STRINGS)
270 set(_isset ${${_option}})
276 string(SUBSTRING "${_isset} " 0 4 _val)
277 message(STATUS "${_var}= ${_val} : ${${_option}_description}")
278 file(APPEND ${run_cmake} " -D${_option}=${${_option}} ${cont}")
281 file(APPEND ${run_cmake} "\n")
283 message(STATUS "CMake command with options is available in shell script")
284 message(STATUS " '${run_cmake}'")
287 macro(lyx_add_info_files group)
289 if(NOT IS_DIRECTORY ${_it})
290 get_filename_component(name ${_it} NAME)
291 if(NOT ${_it} MATCHES "^/\\\\..*$;~$")
292 set_source_files_properties(${_it} PROPERTIES HEADER_FILE_ONLY TRUE)
293 set(lyx_${group}_info_files ${lyx_${group}_info_files} ${_it})
297 set(check_group ${group}) #cmake bug?
299 source_group(${group} FILES ${lyx_${group}_info_files})
301 set(lyx_info_files ${lyx_info_files} ${lyx_${group}_info_files})
304 macro(lyx_add_info_files_no_group)
305 lyx_add_info_files( "" ${ARGN})
308 macro(lyx_find_info_files group files)
309 file(GLOB _filelist ${files})
310 lyx_add_info_files(${group} ${_filelist})
313 macro(settestlabel testname)
314 get_property(_lab_list TEST ${testname} PROPERTY LABELS)
316 list(APPEND _lab_list "${ARGN}")
318 set(_lab_list "${ARGN}")
320 list(REMOVE_DUPLICATES _lab_list)
321 set_tests_properties(${testname} PROPERTIES LABELS "${_lab_list}")
324 macro(setmarkedtestlabel testname reverted)
326 settestlabel(${testname} "reverted" ${ARGN})
328 settestlabel(${testname} ${ARGN})