From 67ef2f1cc411ead96ca4ca8c537ea0d780482cd9 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 7 Apr 2020 22:36:54 +0800 Subject: improve qt.moc rule --- tests/projects/qt/widgetapp_private_slot/main.cpp | 11 +++++++ .../qt/widgetapp_private_slot/mainwindow.cpp | 25 ++++++++++++++++ .../qt/widgetapp_private_slot/mainwindow.h | 30 +++++++++++++++++++ .../qt/widgetapp_private_slot/mainwindow.ui | 24 +++++++++++++++ tests/projects/qt/widgetapp_private_slot/xmake.lua | 5 ++++ xmake/rules/qt/moc/xmake.lua | 34 ++++++++++++++++------ 6 files changed, 120 insertions(+), 9 deletions(-) create mode 100755 tests/projects/qt/widgetapp_private_slot/main.cpp create mode 100755 tests/projects/qt/widgetapp_private_slot/mainwindow.cpp create mode 100755 tests/projects/qt/widgetapp_private_slot/mainwindow.h create mode 100755 tests/projects/qt/widgetapp_private_slot/mainwindow.ui create mode 100755 tests/projects/qt/widgetapp_private_slot/xmake.lua diff --git a/tests/projects/qt/widgetapp_private_slot/main.cpp b/tests/projects/qt/widgetapp_private_slot/main.cpp new file mode 100755 index 000000000..aab39bb89 --- /dev/null +++ b/tests/projects/qt/widgetapp_private_slot/main.cpp @@ -0,0 +1,11 @@ +#include "mainwindow.h" +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + MainWindow w; + w.show(); + + return a.exec(); +} diff --git a/tests/projects/qt/widgetapp_private_slot/mainwindow.cpp b/tests/projects/qt/widgetapp_private_slot/mainwindow.cpp new file mode 100755 index 000000000..b27141bbe --- /dev/null +++ b/tests/projects/qt/widgetapp_private_slot/mainwindow.cpp @@ -0,0 +1,25 @@ +#include "mainwindow.h" +#include + +class MainWindowPrivate +{ + MainWindow *q_ptr; + Q_DECLARE_PUBLIC(MainWindow) +public: + MainWindowPrivate(){} + void mainWindow_slot(){qDebug()<<"mainWindow_slot";} +}; + +MainWindow::MainWindow(QWidget *parent) : + QMainWindow(parent) +{ + d_ptr = new MainWindowPrivate; + d_ptr->q_ptr = this; +} + +MainWindow::~MainWindow() +{ + +} + +#include "moc_mainwindow.cpp" diff --git a/tests/projects/qt/widgetapp_private_slot/mainwindow.h b/tests/projects/qt/widgetapp_private_slot/mainwindow.h new file mode 100755 index 000000000..2ede3584b --- /dev/null +++ b/tests/projects/qt/widgetapp_private_slot/mainwindow.h @@ -0,0 +1,30 @@ +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include + +#if QT_VERSION >= 0x040400 +QT_BEGIN_NAMESPACE +#endif + +class MainWindowPrivate; + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + explicit MainWindow(QWidget *parent = nullptr); + ~MainWindow(); + +private: + MainWindowPrivate *d_ptr; + Q_DECLARE_PRIVATE(MainWindow) + Q_DISABLE_COPY(MainWindow) + + Q_PRIVATE_SLOT(d_func(), void mainWindow_slot()) +}; +#if QT_VERSION >= 0x040400 +QT_END_NAMESPACE +#endif +#endif // MAINWINDOW_H diff --git a/tests/projects/qt/widgetapp_private_slot/mainwindow.ui b/tests/projects/qt/widgetapp_private_slot/mainwindow.ui new file mode 100755 index 000000000..7de574d7c --- /dev/null +++ b/tests/projects/qt/widgetapp_private_slot/mainwindow.ui @@ -0,0 +1,24 @@ + + MainWindow + + + + 0 + 0 + 400 + 300 + + + + MainWindow + + + + + + + + + + + diff --git a/tests/projects/qt/widgetapp_private_slot/xmake.lua b/tests/projects/qt/widgetapp_private_slot/xmake.lua new file mode 100755 index 000000000..bae92b3cf --- /dev/null +++ b/tests/projects/qt/widgetapp_private_slot/xmake.lua @@ -0,0 +1,5 @@ +target("test") + add_rules("qt.widgetapp") + add_files("mainwindow.h") + add_files("*.cpp") + add_frameworks("QtCore", "QtGui") diff --git a/xmake/rules/qt/moc/xmake.lua b/xmake/rules/qt/moc/xmake.lua index dc8984d43..8685643c0 100644 --- a/xmake/rules/qt/moc/xmake.lua +++ b/xmake/rules/qt/moc/xmake.lua @@ -38,8 +38,8 @@ rule("qt.moc") target:data_set("qt.moc", moc) end) - -- on build file - on_build_file(function (target, headerfile_moc, opt) + -- before build file (we need compile it first if exists Q_PRIVATE_SLOT) + before_build_file(function (target, headerfile_moc, opt) -- imports import("moc") @@ -85,14 +85,30 @@ rule("qt.moc") -- generate c++ source file for moc moc.generate(target, headerfile_moc, sourcefile_moc) - -- trace - if option.get("verbose") then - print(compinst:compcmd(sourcefile_moc, objectfile, {compflags = compflags})) - end - - -- compile c++ source file for moc + -- we need compile this moc_xxx.cpp file if exists Q_PRIVATE_SLOT, @see https://github.com/xmake-io/xmake/issues/750 dependinfo.files = {} - assert(compinst:compile(sourcefile_moc, objectfile, {dependinfo = dependinfo, compflags = compflags})) + local mocdata = io.readfile(headerfile_moc) + if mocdata and mocdata:find("Q_PRIVATE_SLOT") then + -- add includedirs of sourcefile_moc + target:add("includedirs", path.directory(sourcefile_moc)) + + -- remove the object file of sourcefile_moc + local objectfiles = target:objectfiles() + for idx, objectfile in ipairs(objectfiles) do + if objectfile == target:objectfile(sourcefile_moc) then + table.remove(objectfiles, idx) + break + end + end + else + -- trace + if option.get("verbose") then + print(compinst:compcmd(sourcefile_moc, objectfile, {compflags = compflags})) + end + + -- compile c++ source file for moc + assert(compinst:compile(sourcefile_moc, objectfile, {dependinfo = dependinfo, compflags = compflags})) + end -- update files and values to the dependent file dependinfo.values = depvalues -- cgit v1.3.1