I use the following macros to mimic qmake behavior in cmake and so far it works fine :
# Smarter versions of moc wrapping macros that mimic qmake behavior
function(qt4_wrap_hdrs _moc_srcs)
qt4_get_moc_flags(_moc_incs)
set(_mocs)
foreach(_current_file ${ARGN})
get_filename_component(_abs_file ${_current_file} ABSOLUTE)
if(EXISTS ${_abs_file})
file(READ ${_abs_file} _contents)
get_filename_component(_basename ${_abs_file} NAME_WE)
string(REGEX MATCH "Q_OBJECT" _match "${_contents}")
if(_match)
set(_moc "${CMAKE_CURRENT_BINARY_DIR}/moc_${_basename}.cpp")
qt4_create_moc_command(${_abs_file} ${_moc} "${_moc_incs}" "")
macro_add_file_dependencies(${_abs_file} ${_moc})
list(APPEND _mocs ${_moc})
endif(_match)
endif(EXISTS ${_abs_file})
endforeach (_current_file)
set(${_moc_srcs} ${_mocs} PARENT_SCOPE)
endfunction(qt4_wrap_hdrs)
function(qt4_wrap_srcs)
qt4_get_moc_flags(_moc_incs)
foreach(_current_file ${ARGN})
get_filename_component(_abs_file ${_current_file} ABSOLUTE)
if(EXISTS ${_abs_file})
file(READ ${_abs_file} _contents)
get_filename_component(_abs_path ${_abs_file} PATH)
get_filename_component(_basename ${_abs_file} NAME_WE)
string(REGEX MATCH "# *include +\"${_basename}\\.moc\"" _match "${_contents}")
if(_match)
string(REGEX MATCH "[^ <\"]+\\.moc" _moc "${_match}")
set(_moc "${CMAKE_CURRENT_BINARY_DIR}/${_moc}")
qt4_create_moc_command(${_abs_file} ${_moc} "${_moc_incs}" "")
macro_add_file_dependencies(${_abs_file} ${_moc})
endif(_match)
endif(EXISTS ${_abs_file})
endforeach(_current_file)
endfunction(qt4_wrap_srcs)
# Smarter versions of moc wrapping macros that mimic qmake behavior
function(qt4_wrap_hdrs _moc_srcs)
qt4_get_moc_flags(_moc_incs)
set(_mocs)
foreach(_current_file ${ARGN})
get_filename_component(_abs_file ${_current_file} ABSOLUTE)
if(EXISTS ${_abs_file})
file(READ ${_abs_file} _contents)
get_filename_component(_basename ${_abs_file} NAME_WE)
string(REGEX MATCH "Q_OBJECT" _match "${_contents}")
if(_match)
set(_moc "${CMAKE_CURRENT_BINARY_DIR}/moc_${_basename}.cpp")
qt4_create_moc_command(${_abs_file} ${_moc} "${_moc_incs}" "")
macro_add_file_dependencies(${_abs_file} ${_moc})
list(APPEND _mocs ${_moc})
endif(_match)
endif(EXISTS ${_abs_file})
endforeach (_current_file)
set(${_moc_srcs} ${_mocs} PARENT_SCOPE)
endfunction(qt4_wrap_hdrs)
function(qt4_wrap_srcs)
qt4_get_moc_flags(_moc_incs)
foreach(_current_file ${ARGN})
get_filename_component(_abs_file ${_current_file} ABSOLUTE)
if(EXISTS ${_abs_file})
file(READ ${_abs_file} _contents)
get_filename_component(_abs_path ${_abs_file} PATH)
get_filename_component(_basename ${_abs_file} NAME_WE)
string(REGEX MATCH "# *include +\"${_basename}\\.moc\"" _match "${_contents}")
if(_match)
string(REGEX MATCH "[^ <\"]+\\.moc" _moc "${_match}")
set(_moc "${CMAKE_CURRENT_BINARY_DIR}/${_moc}")
qt4_create_moc_command(${_abs_file} ${_moc} "${_moc_incs}" "")
macro_add_file_dependencies(${_abs_file} ${_moc})
endif(_match)
endif(EXISTS ${_abs_file})
endforeach(_current_file)
endfunction(qt4_wrap_srcs)
To copy to clipboard, switch view to plain text mode
Typical usage (assuming _hdrs, _srcs, _forms and _rsrcs variable are defined and hold respectively the HEADERS, SOURCES, FORMS and RESOURCES of your qmake project) :
qt4_wrap_hdrs(_moc_srcs ${_hdrs})
qt4_wrap_srcs(${_srcs})
qt4_wrap_ui(_forms_hdrs ${_forms})
qt4_add_resources(_rsrcs_srcs ${_rsrcs})
add_library(${_name} ${_srcs} ${_moc_srcs} ${_rsrcs_srcs})
qt4_wrap_hdrs(_moc_srcs ${_hdrs})
qt4_wrap_srcs(${_srcs})
qt4_wrap_ui(_forms_hdrs ${_forms})
qt4_add_resources(_rsrcs_srcs ${_rsrcs})
add_library(${_name} ${_srcs} ${_moc_srcs} ${_rsrcs_srcs})
To copy to clipboard, switch view to plain text mode
Bookmarks