[extra-cmake-modules] /: Revert "Make sure we load translations on the main thread."

Alex Merry alex.merry at kde.org
Sun Nov 8 19:31:39 UTC 2015


Git commit 6745bd7e4796560959bb67e33b7c7f86f96a5a94 by Alex Merry.
Committed on 08/11/2015 at 19:29.
Pushed by alexmerry into branch 'master'.

Revert "Make sure we load translations on the main thread."

This broke the build for projects which used ecm_create_qm_loader in
unusual ways. A better approach is coming, but won't be in e-c-m 5.16.

This reverts commit 009c480413910e8c1a18f4d1420f4a517ea606e6.

CCBUG: 346188
CCMAIL: release-team at kde.org
CCMAIL: kde-buildsystem at kde.org

M  +2    -13   modules/ECMPoQmTools.cmake
M  +26   -62   modules/ECMQmLoader.cpp.in
M  +2    -2    tests/CMakeLists.txt
M  +5    -27   tests/ECMPoQmToolsTest/CMakeLists.txt
D  +0    -2    tests/ECMPoQmToolsTest/check_conf.cmake.in
R  +10   -30   tests/ECMPoQmToolsTest/check_tree.cmake.in [from: tests/ECMPoQmToolsTest/check.cmake.in - 052% similarity]
D  +0    -22   tests/ECMPoQmToolsTest/tr_test-po/en/catalog.po
D  +0    -22   tests/ECMPoQmToolsTest/tr_test-po/en_GB/catalog.po
D  +0    -16   tests/ECMPoQmToolsTest/tr_test.cpp
D  +0    -68   tests/ECMPoQmToolsTest/tr_thread_test.cpp
D  +0    -12   tests/ECMPoQmToolsTest/tr_thread_test_module.cpp

http://commits.kde.org/extra-cmake-modules/6745bd7e4796560959bb67e33b7c7f86f96a5a94

diff --git a/modules/ECMPoQmTools.cmake b/modules/ECMPoQmTools.cmake
index 22258dc..12bcf6b 100644
--- a/modules/ECMPoQmTools.cmake
+++ b/modules/ECMPoQmTools.cmake
@@ -99,20 +99,9 @@ endfunction()
 
 
 function(ecm_create_qm_loader out_var catalog_name)
-    set(loader_cpp ${CMAKE_CURRENT_BINARY_DIR}/ECMQmLoader.cpp)
-    set(loader_moc ${CMAKE_CURRENT_BINARY_DIR}/ECMQmLoader.moc)
-
     # catalog_name is used in ECMQmLoader.cpp.in
-    configure_file(${ECM_MODULE_DIR}/ECMQmLoader.cpp.in "${loader_cpp}" @ONLY)
-    set(${out_var} "${${out_var}}" "${loader_cpp}" "${loader_moc}" PARENT_SCOPE)
-
-    # can't assume target has AUTOMOC turned on
-    if(NOT Qt5Core_FOUND)
-        find_package(Qt5Core)
-    endif()
-    if(Qt5Core_FOUND)
-        qt5_generate_moc("${loader_cpp}" "${loader_moc}")
-    endif()
+    configure_file(${ECM_MODULE_DIR}/ECMQmLoader.cpp.in ECMQmLoader.cpp @ONLY)
+    set(${out_var} ${${out_var}} ${CMAKE_CURRENT_BINARY_DIR}/ECMQmLoader.cpp PARENT_SCOPE)
 endfunction()
 
 
diff --git a/modules/ECMQmLoader.cpp.in b/modules/ECMQmLoader.cpp.in
index fc667ba..423d1c9 100644
--- a/modules/ECMQmLoader.cpp.in
+++ b/modules/ECMQmLoader.cpp.in
@@ -32,77 +32,41 @@
  */
 #include <QCoreApplication>
 #include <QLocale>
-#include <QMetaObject>
 #include <QStandardPaths>
-#include <QThread>
 #include <QTranslator>
 
-namespace {
+#include <QDebug>
 
-    bool loadTranslation(const QString &localeDirName)
-    {
-        QString subPath = QStringLiteral("locale/") + localeDirName + QStringLiteral("/LC_MESSAGES/@catalog_name at .qm");
-        QString fullPath = QStandardPaths::locate(QStandardPaths::GenericDataLocation, subPath);
-        if (fullPath.isEmpty()) {
-            return false;
-        }
-        QTranslator *translator = new QTranslator(QCoreApplication::instance());
-        if (!translator->load(fullPath)) {
-            delete translator;
-            return false;
-        }
-        QCoreApplication::instance()->installTranslator(translator);
-        return true;
+static bool loadTranslation(const QString &localeDirName)
+{
+    QString subPath = QStringLiteral("locale/") + localeDirName + QStringLiteral("/LC_MESSAGES/@catalog_name at .qm");
+    QString fullPath = QStandardPaths::locate(QStandardPaths::GenericDataLocation, subPath);
+    if (fullPath.isEmpty()) {
+        return false;
     }
-
-    void load()
-    {
-        // The way Qt translation system handles plural forms makes it necessary to
-        // have a translation file which contains only plural forms for `en`. That's
-        // why we load the `en` translation unconditionally, then load the
-        // translation for the current locale to overload it.
-        loadTranslation(QStringLiteral("en"));
-
-        QLocale locale = QLocale::system();
-        if (locale.name() != QStringLiteral("en")) {
-            if (!loadTranslation(locale.name())) {
-                loadTranslation(locale.bcp47Name());
-            }
-        }
+    QTranslator *translator = new QTranslator(QCoreApplication::instance());
+    if (!translator->load(fullPath)) {
+        delete translator;
+        return false;
     }
+    QCoreApplication::instance()->installTranslator(translator);
+    return true;
+}
 
-    // helper to call load() on the correct thread
-    class Loader : public QObject
-    {
-        Q_OBJECT
-
-    public Q_SLOTS:
-        void callLoadAndDeleteSelf()
-        {
-            load();
-            this->deleteLater();
-        }
-    };
+static void load()
+{
+    // The way Qt translation system handles plural forms makes it necessary to
+    // have a translation file which contains only plural forms for `en`. That's
+    // why we load the `en` translation unconditionally, then load the
+    // translation for the current locale to overload it.
+    loadTranslation(QStringLiteral("en"));
 
-    void loadOnMainThread()
-    {
-        // If this library is loaded after the QCoreApplication instance is created
-        // (eg: because it is brought in by a plugin), there is no guarantee this
-        // function will be called on the main thread.
-        // QCoreApplication::installTranslator needs to be called on the main
-        // thread, because it uses QCoreApplication::sendEvent.
-        if (QThread::currentThread() == QCoreApplication::instance()->thread()) {
-            load();
-        } else {
-            // QObjects inherit their parent object's thread
-            Loader *loader = new Loader();
-            loader->moveToThread(QCoreApplication::instance()->thread());
-            QMetaObject::invokeMethod(loader, "callLoadAndDeleteSelf", Qt::AutoConnection);
+    QLocale locale = QLocale::system();
+    if (locale.name() != QStringLiteral("en")) {
+        if (!loadTranslation(locale.name())) {
+            loadTranslation(locale.bcp47Name());
         }
     }
-
 }
 
-Q_COREAPP_STARTUP_FUNCTION(loadOnMainThread)
-
-#include "ECMQmLoader.moc"
+Q_COREAPP_STARTUP_FUNCTION(load)
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 9e6de12..8a75ae6 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -101,13 +101,13 @@ add_test_macro(ECMInstallIconsTest
     ${CMAKE_COMMAND} -P "${CMAKE_CURRENT_BINARY_DIR}/ECMInstallIconsTest/check_tree.cmake"
 )
 
-if (Qt5Core_FOUND AND Qt5LinguistTools_FOUND)
+if (Qt5LinguistTools_FOUND)
     set(ECMPoQmToolsTest_EXTRA_OPTIONS
         --build-target install
         --build-options
             "-DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_CURRENT_BINARY_DIR}/ECMPoQmToolsTest/InstallDirectory"
     )
     add_test_macro(ECMPoQmToolsTest
-        ${CMAKE_COMMAND} -P "${CMAKE_CURRENT_BINARY_DIR}/ECMPoQmToolsTest/check.cmake"
+        ${CMAKE_COMMAND} -P "${CMAKE_CURRENT_BINARY_DIR}/ECMPoQmToolsTest/check_tree.cmake"
     )
 endif()
diff --git a/tests/ECMPoQmToolsTest/CMakeLists.txt b/tests/ECMPoQmToolsTest/CMakeLists.txt
index e08a2b8..15351d2 100644
--- a/tests/ECMPoQmToolsTest/CMakeLists.txt
+++ b/tests/ECMPoQmToolsTest/CMakeLists.txt
@@ -9,14 +9,9 @@ file(REMOVE_RECURSE "${CMAKE_INSTALL_PREFIX}")
 
 include(ECMPoQmTools)
 
-include(../test_helpers.cmake)
-
-unset(QMLOADER_FILES)
-ecm_create_qm_loader(QMLOADER_FILES catalog)
-assert_var_defined(QMLOADER_FILES)
-
-# These will be used to test the above-generated loader
-ecm_install_po_files_as_qm(tr_test-po)
+# Should create ${CMAKE_CURRENT_BINARY_DIR}/qmloader.cpp and set QMLOADER_PATH
+# to its path
+ecm_create_qm_loader(QMLOADER_PATH catalog)
 
 # Should create a process-and-install.qm file and install it
 ecm_process_po_files_as_qm(fr ALL
@@ -44,22 +39,5 @@ ecm_install_po_files_as_qm(po-custom-dir1)
 set(LOCALE_INSTALL_DIR custom-dir2)
 ecm_install_po_files_as_qm(po-custom-dir2)
 
-find_package(Qt5Core CONFIG REQUIRED)
-
-add_executable(tr_test tr_test.cpp ${QMLOADER_FILES})
-target_link_libraries(tr_test PRIVATE Qt5::Core)
-
-add_library(tr_thread_module MODULE tr_thread_test_module.cpp ${QMLOADER_FILES})
-target_link_libraries(tr_thread_module PRIVATE Qt5::Core)
-
-add_executable(tr_thread_test tr_thread_test.cpp)
-set_target_properties(tr_thread_test PROPERTIES AUTOMOC ON)
-target_include_directories(tr_thread_test PRIVATE "${CMAKE_CURRENT_BINARY_DIR}")
-target_compile_definitions(tr_thread_test PRIVATE "MODULE_PATH=\"$<TARGET_FILE:tr_thread_module>\"")
-target_link_libraries(tr_thread_test PRIVATE Qt5::Core)
-
-file(GENERATE
-    OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/check_conf.cmake"
-    INPUT "${CMAKE_CURRENT_SOURCE_DIR}/check_conf.cmake.in"
-)
-configure_file(check.cmake.in "${CMAKE_CURRENT_BINARY_DIR}/check.cmake" @ONLY)
+# this will be run by CTest
+configure_file(check_tree.cmake.in "${CMAKE_CURRENT_BINARY_DIR}/check_tree.cmake" @ONLY)
diff --git a/tests/ECMPoQmToolsTest/check_conf.cmake.in b/tests/ECMPoQmToolsTest/check_conf.cmake.in
deleted file mode 100644
index 0bbab1d..0000000
--- a/tests/ECMPoQmToolsTest/check_conf.cmake.in
+++ /dev/null
@@ -1,2 +0,0 @@
-set(TR_TEST_EXEC "$<TARGET_FILE:tr_test>")
-set(TR_THREAD_TEST_EXEC "$<TARGET_FILE:tr_thread_test>")
diff --git a/tests/ECMPoQmToolsTest/check.cmake.in b/tests/ECMPoQmToolsTest/check_tree.cmake.in
similarity index 52%
rename from tests/ECMPoQmToolsTest/check.cmake.in
rename to tests/ECMPoQmToolsTest/check_tree.cmake.in
index 2f0cc20..9f4f7c0 100644
--- a/tests/ECMPoQmToolsTest/check.cmake.in
+++ b/tests/ECMPoQmToolsTest/check_tree.cmake.in
@@ -1,6 +1,6 @@
 set(BINARY_DIR "@CMAKE_CURRENT_BINARY_DIR@")
 set(ACTUAL_TREE "@CMAKE_INSTALL_PREFIX@")
-include("${BINARY_DIR}/check_conf.cmake")
+set(QMLOADER_PATH "@QMLOADER_PATH@")
 
 set(fail OFF)
 
@@ -9,21 +9,27 @@ macro(mark_failed msg)
     set(fail ON)
 endmacro()
 
+macro(check_strequal var expected)
+    if (NOT "${${var}}" STREQUAL "${expected}")
+        mark_failed("${var} is:\n  \"${${var}}\"\nExpected:\n  \"${expected}\"")
+    endif()
+endmacro()
+
 macro(check_exists file)
-    message(STATUS "Checking for ${file}")
     if (NOT EXISTS ${file})
         mark_failed("File \"${file}\" does not exist")
     endif()
 endmacro()
 
+check_exists(${BINARY_DIR}/ECMQmLoader.cpp)
+check_strequal(QMLOADER_PATH "${BINARY_DIR}/ECMQmLoader.cpp")
+
 check_exists(${BINARY_DIR}/fr/only-process.qm)
 
 set(exp_files
     "share/locale/fr/LC_MESSAGES/process-and-install.qm"
     "share/locale/es/LC_MESSAGES/install-test.qm"
     "share/locale/fr/LC_MESSAGES/install-test.qm"
-    "share/locale/en/LC_MESSAGES/catalog.qm"
-    "share/locale/en_GB/LC_MESSAGES/catalog.qm"
     "custom-dir1/es/LC_MESSAGES/custom-dir1-install-test.qm"
     "custom-dir1/fr/LC_MESSAGES/custom-dir1-install-test.qm"
     "custom-dir2/es/LC_MESSAGES/custom-dir2-install-test.qm"
@@ -48,32 +54,6 @@ if(NOT exp_files STREQUAL actual_files)
             set(fail ON)
         endif()
     endforeach()
-else()
-    message(STATUS "Installed translations in expected locations")
-endif()
-
-# we know we can modify the executable environment on Linux
-if("@CMAKE_SYSTEM_NAME@" STREQUAL "Linux")
-    set(exp_output_en "english text:english plural form 5")
-    set(exp_output_en_GB "british english text:british english plural form 5")
-    # no french translation provided -> english fallback
-    set(exp_output_fr "${exp_output_en}")
-    foreach(exec TR_TEST TR_THREAD_TEST)
-        foreach(lang en en_GB fr)
-            execute_process(
-                COMMAND "${CMAKE_COMMAND}" -E env "XDG_DATA_DIRS=${ACTUAL_TREE}/share"
-                    LC_ALL=${lang} "${${exec}_EXEC}"
-                OUTPUT_VARIABLE output
-            )
-            string(STRIP "${output}" stripped_output)
-            if(NOT stripped_output STREQUAL exp_output_${lang})
-                message(WARNING "${exec}[${lang}] output was \"${stripped_output}\", but expected \"${exp_output_${lang}}\"")
-                set(fail ON)
-            else()
-                message(STATUS "${exec}[${lang}] output was \"${stripped_output}\", as expected")
-            endif()
-        endforeach()
-    endforeach()
 endif()
 
 if (fail)
diff --git a/tests/ECMPoQmToolsTest/tr_test-po/en/catalog.po b/tests/ECMPoQmToolsTest/tr_test-po/en/catalog.po
deleted file mode 100644
index 2a7b6d2..0000000
--- a/tests/ECMPoQmToolsTest/tr_test-po/en/catalog.po
+++ /dev/null
@@ -1,22 +0,0 @@
-msgid ""
-msgstr ""
-"MIME-Version: 1.0\n"
-"Content-Type: text/plain; charset=UTF-8\n"
-"Content-Transfer-Encoding: 8bit\n"
-"Plural-Forms: nplurals=2; plural=(n != 1);\n"
-"X-Language: en\n"
-"X-Qt-Contexts: true\n"
-
-#: main.cpp:12
-msgctxt "testcontext|"
-msgid "test string"
-msgstr "english text"
-
-#: main.cpp:13
-#, qt-format
-#| msgid "test plural"
-msgctxt "testcontext|"
-msgid "test plural %n"
-msgid_plural "test plural %n"
-msgstr[0] "english singular form %n"
-msgstr[1] "english plural form %n"
diff --git a/tests/ECMPoQmToolsTest/tr_test-po/en_GB/catalog.po b/tests/ECMPoQmToolsTest/tr_test-po/en_GB/catalog.po
deleted file mode 100644
index ec5ad85..0000000
--- a/tests/ECMPoQmToolsTest/tr_test-po/en_GB/catalog.po
+++ /dev/null
@@ -1,22 +0,0 @@
-msgid ""
-msgstr ""
-"MIME-Version: 1.0\n"
-"Content-Type: text/plain; charset=UTF-8\n"
-"Content-Transfer-Encoding: 8bit\n"
-"Plural-Forms: nplurals=2; plural=(n != 1);\n"
-"X-Language: en_GB\n"
-"X-Qt-Contexts: true\n"
-
-#: main.cpp:12
-msgctxt "testcontext|"
-msgid "test string"
-msgstr "british english text"
-
-#: main.cpp:13
-#, qt-format
-#| msgid "test plural"
-msgctxt "testcontext|"
-msgid "test plural %n"
-msgid_plural "test plural %n"
-msgstr[0] "british english singular form %n"
-msgstr[1] "british english plural form %n"
diff --git a/tests/ECMPoQmToolsTest/tr_test.cpp b/tests/ECMPoQmToolsTest/tr_test.cpp
deleted file mode 100644
index be5d342..0000000
--- a/tests/ECMPoQmToolsTest/tr_test.cpp
+++ /dev/null
@@ -1,16 +0,0 @@
-#include <QCoreApplication>
-#include <QTextStream>
-
-#include <stdio.h>
-
-int main(int argc, char** argv)
-{
-    QCoreApplication app(argc, argv);
-
-    QTextStream output(stdout);
-
-    output << QCoreApplication::translate("testcontext", "test string") << ":";
-    output << QCoreApplication::translate("testcontext", "test plural %n", 0, 5) << '\n';
-
-    return 0;
-}
diff --git a/tests/ECMPoQmToolsTest/tr_thread_test.cpp b/tests/ECMPoQmToolsTest/tr_thread_test.cpp
deleted file mode 100644
index 3ed30ee..0000000
--- a/tests/ECMPoQmToolsTest/tr_thread_test.cpp
+++ /dev/null
@@ -1,68 +0,0 @@
-#include <QCoreApplication>
-#include <QLibrary>
-#include <QMetaObject>
-#include <QThread>
-
-class Thread : public QThread
-{
-    Q_OBJECT
-
-    QLibrary *m_lib;
-
-public:
-    Thread()
-        : m_lib(0)
-    {}
-    ~Thread()
-    {
-        delete m_lib;
-    }
-
-Q_SIGNALS:
-    void libraryLoaded();
-
-public Q_SLOTS:
-    void printStrings()
-    {
-        // NB: this will run on the *main* event loop.
-        QFunctionPointer print_strings = m_lib->resolve("print_strings");
-        if (print_strings) {
-            print_strings();
-        } else {
-            qFatal("Could not resolve print_strings: %s", m_lib->errorString().toUtf8().data());
-        }
-
-        QCoreApplication::instance()->quit();
-    }
-protected:
-    void run()
-    {
-        m_lib = new QLibrary(MODULE_PATH);
-
-        if (!m_lib->load()) {
-            qFatal("Could not load module: %s", m_lib->errorString().toUtf8().data());
-        }
-
-        // Queue a call to printStrings() on the main event loop (giving the
-        // translations a chance to be loaded).
-        QMetaObject::invokeMethod(this, "printStrings", Qt::QueuedConnection);
-    }
-};
-
-int main(int argc, char** argv)
-{
-    QCoreApplication app(argc, argv);
-
-    Thread thread;
-
-    // Start the thread *after* QCoreApplication is started (otherwise the
-    // plugin's startup function won't be run on the Thread, and we won't test
-    // what we wanted to test).
-    QMetaObject::invokeMethod(&thread, "start", Qt::QueuedConnection);
-
-    app.exec();
-
-    return 0;
-}
-
-#include "tr_thread_test.moc"
diff --git a/tests/ECMPoQmToolsTest/tr_thread_test_module.cpp b/tests/ECMPoQmToolsTest/tr_thread_test_module.cpp
deleted file mode 100644
index b9000ff..0000000
--- a/tests/ECMPoQmToolsTest/tr_thread_test_module.cpp
+++ /dev/null
@@ -1,12 +0,0 @@
-#include <QCoreApplication>
-#include <QTextStream>
-
-#include <stdio.h>
-
-extern "C" Q_DECL_EXPORT void print_strings()
-{
-    QTextStream output(stdout);
-
-    output << QCoreApplication::translate("testcontext", "test string") << ":";
-    output << QCoreApplication::translate("testcontext", "test plural %n", 0, 5) << '\n';
-}


More information about the release-team mailing list