From dc4159286b8571a3f3543e457fe1b51b9f5965b7 Mon Sep 17 00:00:00 2001 From: Orkun Tokdemir Date: Fri, 21 Jul 2023 16:41:48 +0200 Subject: qt6_wrap_cpp: Add .moc generation When a `.moc` file is included in a source file and that source passed to `qt_wrap_cpp`, Users should add the generated `.moc`s path to the target's include path. Since we don't share anything about the output path of generated files by `qt_wrap_cpp`, it makes sense to add in inside `qt_wrap_cpp`. And also, the generated `.moc` file is added to target's source to complete the dependency graph. Otherwise, Users need to get output variable and pass it to target's sources. * Update docs * Add test [ChangeLog][Build System] qt_wrap_cpp will accept .cpp files from now on. When .cpp a file is passed to qt_wrap_cpp, TARGET parameter becomes required. Generated .moc files are added to target's sources inside qt_wrap_cpp. That's why the output parameter will not contain generated .moc files. Fixes: QTBUG-113402 Change-Id: I54dd2b1ff8e5c9ba457b1eb4f73b0a8190d9c659 Reviewed-by: Alexandru Croitor Reviewed-by: Joerg Bornemann --- src/corelib/Qt6CoreMacros.cmake | 49 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) (limited to 'src/corelib/Qt6CoreMacros.cmake') diff --git a/src/corelib/Qt6CoreMacros.cmake b/src/corelib/Qt6CoreMacros.cmake index 84ffa6d2865..486957d7d7e 100644 --- a/src/corelib/Qt6CoreMacros.cmake +++ b/src/corelib/Qt6CoreMacros.cmake @@ -200,7 +200,49 @@ function(qt6_wrap_cpp outfiles ) foreach(it ${moc_files}) get_filename_component(it ${it} ABSOLUTE) - _qt_internal_make_output_file(${it} moc_ cpp outfile) + get_filename_component(it_ext ${it} EXT) + # remove the dot + string(SUBSTRING ${it_ext} 1 -1 it_ext) + set(HEADER_REGEX "(h|hh|h\\+\\+|hm|hpp|hxx|in|txx|inl)$") + + if(it_ext MATCHES "${HEADER_REGEX}") + _qt_internal_make_output_file("${it}" moc_ cpp outfile) + set(is_header_file TRUE) + else() + set(found_source_extension FALSE) + foreach(LANG C CXX OBJC OBJCXX CUDA) + list(FIND CMAKE_${LANG}_SOURCE_FILE_EXTENSIONS "${it_ext}" + index) + if(${index} GREATER -1) + set(found_extension TRUE) + break() + endif() + endforeach() + if(found_extension) + if(TARGET ${moc_target}) + _qt_internal_make_output_file(${it} "" moc outfile) + target_sources(${moc_target} PRIVATE "${outfile}") + target_include_directories("${moc_target}" PRIVATE + "${CMAKE_CURRENT_BINARY_DIR}") + else() + if("${moc_target}" STREQUAL "") + string(JOIN "" err_msg + "qt6_wrap_cpp: TARGET parameter is empty. " + "Since the file ${it} is a source file, " + "the TARGET option must be specified.") + else() + string(JOIN "" err_msg + "qt6_wrap_cpp: TARGET \"${moc_target}\" " + "not found.") + endif() + message(FATAL_ERROR "${err_msg}") + endif() + else() + string(JOIN "" err_msg "qt6_wrap_cpp: Unknown file extension: " + "\"\.${it_ext}\".") + message(FATAL_ERROR "${err_msg}") + endif() + endif() set(out_json_file_var "") if(_WRAP_CPP___QT_INTERNAL_OUTPUT_MOC_JSON_FILES) @@ -215,7 +257,10 @@ function(qt6_wrap_cpp outfiles ) list(APPEND metatypes_json_list "${${out_json_file_var}}") endif() endforeach() - set(${outfiles} ${${outfiles}} PARENT_SCOPE) + + if(is_header_file) + set(${outfiles} "${${outfiles}}" PARENT_SCOPE) + endif() if(metatypes_json_list) set(${_WRAP_CPP___QT_INTERNAL_OUTPUT_MOC_JSON_FILES} -- cgit v1.2.3