]> git.lyx.org Git - lyx.git/blob - development/cmake/modules/LyXMacros.cmake
c0bef8b25e7fa9861a82f42553c2157d2b5e1232
[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 macro(lyx_add_path _list _prefix)
29         set(_tmp)
30         foreach(_current ${${_list}})
31                 set(_tmp ${_tmp} ${_prefix}/${_current})
32                 #message( ${_prefix}/${_current})
33         endforeach(_current)     
34         set(${_list} ${_tmp})  
35 endmacro(lyx_add_path _out _prefix)
36
37
38 #create the implementation files from the ui files and add them to the list of sources
39 #usage: LYX_ADD_QT4_UI_FILES(foo_SRCS ${ui_files})
40 macro (LYX_ADD_UI_FILES _sources _ui)
41    foreach (_current_FILE ${ARGN})
42
43       get_filename_component(_tmp_FILE ${_current_FILE} ABSOLUTE)
44       get_filename_component(_basename ${_tmp_FILE} NAME_WE)
45       set(_header ${CMAKE_CURRENT_BINARY_DIR}/ui/${_basename}.h)
46
47       # we need to run uic and replace some things in the generated file
48       # this is done by executing the cmake script kde4uic.cmake
49       add_custom_command(OUTPUT ${_header}
50          COMMAND ${CMAKE_COMMAND}
51          ARGS
52          -DKDE4_HEADER:BOOL=ON
53          -DKDE_UIC_EXECUTABLE:FILEPATH=${QT_UIC_EXECUTABLE}
54          -DKDE_UIC_FILE:FILEPATH=${_tmp_FILE}
55          -DKDE_UIC_H_FILE:FILEPATH=${_header}
56          -DKDE_UIC_BASENAME:STRING=${_basename}
57          -P ${CMAKE_MODULE_PATH}/LyXuic.cmake
58          MAIN_DEPENDENCY ${_tmp_FILE}
59       )
60       set(${_ui} ${${_ui}} ${_header})
61    endforeach (_current_FILE)
62 endmacro (LYX_ADD_UI_FILES)
63
64
65
66 MACRO (LYX_AUTOMOC)
67    if(QT4_GET_MOC_INC_DIRS)
68       QT4_GET_MOC_INC_DIRS(_moc_INCS)
69    endif(QT4_GET_MOC_INC_DIRS)
70
71    set(_matching_FILES )
72    foreach (_current_FILE ${ARGN})
73
74       get_filename_component(_abs_FILE ${_current_FILE} ABSOLUTE)
75       # if "SKIP_AUTOMOC" is set to true, we will not handle this file here.
76       # here. this is required to make bouic work correctly:
77       # we need to add generated .cpp files to the sources (to compile them),
78       # but we cannot let automoc handle them, as the .cpp files don't exist yet when
79       # cmake is run for the very first time on them -> however the .cpp files might
80       # exist at a later run. at that time we need to skip them, so that we don't add two
81       # different rules for the same moc file
82       get_source_file_property(_skip ${_abs_FILE} SKIP_AUTOMOC)
83
84       if (EXISTS ${_abs_FILE} AND NOT _skip)
85
86          file(READ ${_abs_FILE} _contents)
87
88          get_filename_component(_abs_PATH ${_abs_FILE} PATH)
89
90          string(REGEX MATCHALL "#include +[^ ]+_moc\\.cpp[\">]" _match "${_contents}")
91          if (_match)
92             foreach (_current_MOC_INC ${_match})
93                string(REGEX MATCH "[^ <\"]+_moc\\.cpp" _current_MOC "${_current_MOC_INC}")
94
95                get_filename_component(_basename ${_current_MOC} NAME_WE)               
96                         
97                                 string(LENGTH ${_basename} _length)
98                                 MATH(EXPR _mocless_length ${_length}-4)
99                                 STRING(SUBSTRING  ${_basename} 0 ${_mocless_length} _mocless_name )
100    
101           set(_header ${_abs_PATH}/${_mocless_name}.h)
102           
103           #message(STATUS "moc : ${_header}")
104
105
106
107                #set(_header ${CMAKE_CURRENT_SOURCE_DIR}/${_basename}.h)
108                #set(_header ${_abs_PATH}/${_basename}.h)
109                set(_moc    ${CMAKE_CURRENT_BINARY_DIR}/${_current_MOC})
110                
111    #set(_moc    ${_abs_PATH}/${_current_MOC})
112        add_custom_command(OUTPUT ${_moc}
113         COMMAND ${QT_MOC_EXECUTABLE}
114         ARGS ${_moc_INCS} ${_header} -o ${_moc}
115         MAIN_DEPENDENCY ${_header}
116      )
117                macro_add_file_dependencies(${_abs_FILE} ${_moc})
118
119             endforeach (_current_MOC_INC)
120          else(_match)
121                 #message(STATUS "moc not found : ${_abs_FILE} ")
122          endif (_match)
123
124       endif (EXISTS ${_abs_FILE} AND NOT _skip)
125    endforeach (_current_FILE)
126 endmacro (LYX_AUTOMOC)
127
128
129 # For all C++ sources a big source file which includes all the files
130 # is created.
131 # This is not done for the C sources, they are just gathered in a separate list
132 # because they are usually not written by KDE and as such not intended to be
133 # compiled all-in-one.
134 macro (LYX_CREATE_FINAL_FILES _filenameCPP _filesExcludedFromFinalFile )
135    set(${_filesExcludedFromFinalFile})
136    file(WRITE ${_filenameCPP} "//autogenerated file\n")
137
138    set(_file_macros ${CMAKE_CURRENT_BINARY_DIR}/${_filenameCPP}.h)
139    set(_file_const ${CMAKE_CURRENT_BINARY_DIR}/${_filenameCPP}_const.C)
140    set(_file_touched ${CMAKE_CURRENT_BINARY_DIR}/${_filenameCPP}_touched.C)
141       
142    file(WRITE ${_file_macros} "// undef the macro when working on this file \n\n\n")
143    file(WRITE ${_file_const} "// const files\n#include \"${_file_macros}\" \n\n\n")
144    file(WRITE ${_file_touched} "// touched files\n#include \"${_file_macros}\"\n\n\n")
145    
146    foreach (_current_FILE ${ARGN})
147       get_filename_component(_abs_FILE ${_current_FILE} ABSOLUTE)
148       # don't include any generated files in the final-file
149       # because then cmake will not know the dependencies
150       get_source_file_property(_isGenerated ${_abs_FILE} GENERATED)
151       if (_isGenerated)
152          list(APPEND ${_filesExcludedFromFinalFile} ${_abs_FILE})
153       else (_isGenerated)
154          # don't include c-files in the final-file, because they usually come
155          # from a 3rd party and as such are not intended to be compiled all-in-one
156          string(REGEX MATCH ".+\\.c$" _isCFile ${_abs_FILE})
157          if (_isCFile)
158             list(APPEND ${_filesExcludedFromFinalFile} ${_abs_FILE})
159          else (_isCFile)
160             file(APPEND ${_filenameCPP} "#include \"${_abs_FILE}\"\n")
161             
162             GET_FILENAME_COMPONENT(_file_name ${_abs_FILE} NAME_WE)
163             STRING(REGEX REPLACE "-" "_" _file_name "${_file_name}" )
164             set(__macro_name ASSUME_CONST_____${_file_name})
165             file(APPEND ${_file_macros} "#define ${__macro_name}\n")
166             
167             file(APPEND ${_file_const}  "#ifdef ${__macro_name}\n")
168             file(APPEND ${_file_const}  "#include \"${_abs_FILE}\"\n")
169             file(APPEND ${_file_const}  "#endif\n\n")
170             
171             file(APPEND ${_file_touched}  "#ifndef ${__macro_name}\n")
172             file(APPEND ${_file_touched}  "#include \"${_abs_FILE}\"\n")
173             file(APPEND ${_file_touched}  "#endif\n\n")
174             
175          endif (_isCFile)
176       endif (_isGenerated)
177    endforeach (_current_FILE)
178
179 endmacro (LYX_CREATE_FINAL_FILES)
180
181
182 macro(lyx_merge_files _filename _list)
183         LYX_CREATE_FINAL_FILES(${_filename} _nix_nothing ${${_list}})
184 endmacro(lyx_merge_files _list _filename)
185