I am trying to generate and update Qt translation files, using CMake.
My problem is that when I invoke make clean
, my .ts
file gets removed.
I can reproduce the problem easily. Here is the CMakeLists.txt
:
cmake_minimum_required(VERSION 3.7)
find_package(Qt5LinguistTools)
add_custom_command(OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/tr.ts
COMMAND ${Qt5_LUPDATE_EXECUTABLE} -target-language en main.cpp -ts tr.ts
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
DEPENDS main.cpp
)
add_custom_target(foo DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tr.ts)
And the translation file tr.ts
:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1" language="en">
<context>
<name>QObject</name>
<message>
<location filename="main.cpp" line="8"/>
<source>%_foo</source>
<translation>Foo</translation>
</message>
</context>
</TS>
My source file main.cpp
:
#include <QCoreApplication>
#include <QString>
#include <iostream>
int main(int argc, char* argv[])
{
QCoreApplication app(argc, argv);
QString what(QObject::tr("%_foo"));
std::cout << what.toStdString() << std::endl;
return app.exec();
}
Simply configure the build directory and invoke the clean
target:
mkdir build && cd build
cmake ..
make clean
I thought about copying the translation file in the build directory and work on this copy, but this means that any new translation found in the sources will be put in the copy, but not the initial file. I would have to copy the copy in the source directory manually.
This solution says to call set_directory_properties(PROPERTIES CLEAN_NO_CUSTOM 1)
, which works, but only for Makefile generators. Also, the file is not present in the property ADDITIONAL_MAKE_CLEAN_FILES
.
How come the file gets removed in the first and how can I avoid this behavior without disabling cleaning custom files?
Thanks,