]> git.lyx.org Git - lyx.git/blob - development/cmake/modules/LyXMacros.cmake
FORMAT: add missing email addresses
[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 kde4uic.cmake
52       add_custom_command(OUTPUT ${_header}
53          COMMAND ${CMAKE_COMMAND}
54          ARGS
55          -DKDE4_HEADER:BOOL=ON
56          -DKDE_UIC_EXECUTABLE:FILEPATH=${QT_UIC_EXECUTABLE}
57          -DKDE_UIC_FILE:FILEPATH=${_tmp_FILE}
58          -DKDE_UIC_H_FILE:FILEPATH=${_header}
59          -DKDE_UIC_BASENAME:STRING=${_basename}
60          -P ${CMAKE_MODULE_PATH}/LyXuic.cmake
61          MAIN_DEPENDENCY ${_tmp_FILE}
62       )
63       set(${_ui} ${${_ui}} ${_header})
64    endforeach (_current_FILE)
65 endmacro(LYX_ADD_UI_FILES)
66
67
68
69 macro(LYX_AUTOMOC)
70    if(QT4_GET_MOC_INC_DIRS)
71       QT4_GET_MOC_INC_DIRS(_moc_INCS)
72    endif()
73
74    set(_matching_FILES )
75    foreach (_current_FILE ${ARGN})
76
77       get_filename_component(_abs_FILE ${_current_FILE} ABSOLUTE)
78       # if "SKIP_AUTOMOC" is set to true, we will not handle this file here.
79       # here. this is required to make bouic work correctly:
80       # we need to add generated .cpp files to the sources (to compile them),
81       # but we cannot let automoc handle them, as the .cpp files don't exist yet when
82       # cmake is run for the very first time on them -> however the .cpp files might
83       # exist at a later run. at that time we need to skip them, so that we don't add two
84       # different rules for the same moc file
85       get_source_file_property(_skip ${_abs_FILE} SKIP_AUTOMOC)
86
87       if (EXISTS ${_abs_FILE} AND NOT _skip)
88
89          file(READ ${_abs_FILE} _contents)
90
91          get_filename_component(_abs_PATH ${_abs_FILE} PATH)
92
93          string(REGEX MATCHALL "#include +[^ ]+_moc\\.cpp[\">]" _match "${_contents}")
94          if (_match)
95             foreach (_current_MOC_INC ${_match})
96                string(REGEX MATCH "[^ <\"]+_moc\\.cpp" _current_MOC "${_current_MOC_INC}")
97
98                get_filename_component(_basename ${_current_MOC} NAME_WE)               
99                         
100                                 string(LENGTH ${_basename} _length)
101                                 MATH(EXPR _mocless_length ${_length}-4)
102                                 STRING(SUBSTRING  ${_basename} 0 ${_mocless_length} _mocless_name )
103    
104           set(_header ${_abs_PATH}/${_mocless_name}.h)
105           
106           #message(STATUS "moc : ${_header}")
107
108                                          #set(_header ${CMAKE_CURRENT_SOURCE_DIR}/${_basename}.h)
109                                          #set(_header ${_abs_PATH}/${_basename}.h)
110                                          set(_moc    ${CMAKE_CURRENT_BINARY_DIR}/${_current_MOC})
111                
112    #set(_moc    ${_abs_PATH}/${_current_MOC})
113        add_custom_command(OUTPUT ${_moc}
114         COMMAND ${QT_MOC_EXECUTABLE}
115         ARGS ${_moc_INCS} ${_header} -o ${_moc}
116         MAIN_DEPENDENCY ${_header}
117      )
118                macro_add_file_dependencies(${_abs_FILE} ${_moc})
119
120             endforeach (_current_MOC_INC)
121          else()
122                 #message(STATUS "moc not found : ${_abs_FILE} ")
123          endif()
124
125       endif()
126    endforeach (_current_FILE)
127 endmacro (LYX_AUTOMOC)
128
129
130 macro(lyx_const_touched_files _allinone_name _list)
131    set(_file_list ${_allinone_name}_files)
132    set(_file_const ${CMAKE_CURRENT_BINARY_DIR}/${_allinone_name}_const.C)
133    set(_file_touched ${CMAKE_CURRENT_BINARY_DIR}/${_allinone_name}_touched.C)
134    
135    
136    # don't touch exisiting or non-empty file,
137    # so a cmake re-run doesn't touch all created files
138    set(_rebuild_file_const 0)
139    if (NOT EXISTS ${_file_const})
140       set(_rebuild_file_const 1)
141    else()
142       FILE(READ ${_file_const} _file_content)
143       if (NOT _file_content)
144          set(_rebuild_file_const 1)
145       endif()
146    endif()
147    
148    set(_rebuild_file_touched 0)
149    if (NOT EXISTS ${_file_touched})
150       set(_rebuild_file_touched 1)
151    else()
152       FILE(READ ${_file_touched} _file_content)
153       if (NOT _file_content)
154          set(_rebuild_file_touched 1)
155       endif()   
156    endif()
157    
158    if (merge_rebuild)
159       set(_rebuild_file_const 1)
160       set(_rebuild_file_touched 1)
161    endif()
162    
163    if (_rebuild_file_const)
164       file(WRITE  ${_file_const} "// autogenerated file \n//\n")
165       file(APPEND ${_file_const} "//    * clear or delete this file to build it again by cmake \n//\n\n")
166    endif()
167
168    if (_rebuild_file_touched)
169       file(WRITE  ${_file_touched} "// autogenerated file \n//\n")
170       file(APPEND ${_file_touched} "//    * clear or delete this file to build it again by cmake \n//\n")
171       file(APPEND ${_file_touched} "//    * don't touch this file \n//\n\n")
172       file(APPEND ${_file_touched} "#define DONT_INCLUDE_CONST_FILES\n")
173       file(APPEND ${_file_touched} "#include \"${_file_const}\"\n\n\n")
174    endif()
175    
176    set(${_file_list} ${_file_const} ${_file_touched})
177    
178    foreach (_current_FILE ${${_list}})
179       get_filename_component(_abs_FILE ${_current_FILE} ABSOLUTE)
180       # don't include any generated files in the final-file
181       # because then cmake will not know the dependencies
182       get_source_file_property(_isGenerated ${_abs_FILE} GENERATED)
183       if (_isGenerated)
184          list(APPEND ${_file_list} ${_abs_FILE})
185       else()
186         GET_FILENAME_COMPONENT(_file_name ${_abs_FILE} NAME_WE)
187         STRING(REGEX REPLACE "-" "_" _file_name "${_file_name}" )
188         set(__macro_name ${_file_name}___ASSUME_CONST)
189         
190         if (_rebuild_file_const)
191            file(APPEND ${_file_const}  "#define ${__macro_name}\n")
192            file(APPEND ${_file_const}  "#if defined(${__macro_name}) && !defined(DONT_INCLUDE_CONST_FILES)\n")
193            file(APPEND ${_file_const}  "#include \"${_abs_FILE}\"\n")
194            file(APPEND ${_file_const}  "#endif\n\n")
195         endif()
196         
197         if (_rebuild_file_touched)
198            file(APPEND ${_file_touched}  "#ifndef ${__macro_name}\n")
199            file(APPEND ${_file_touched}  "#include \"${_abs_FILE}\"\n")
200            file(APPEND ${_file_touched}  "#endif\n\n")
201         endif()
202       endif()
203    endforeach (_current_FILE)
204 endmacro(lyx_const_touched_files)
205
206