MantisBT - CMake
View Issue Details
0015608CMakeCMakepublic2015-06-11 02:142016-02-01 09:10
Hendrik Sattler 
Brad King 
normalmajoralways
closedfixed 
CMake 3.2.2 
CMake 3.4CMake 3.4 
0015608: automoc fails when enabling autorcc
Automoc does not work after initial cmake run when autorcc is enabled.

I created a small example that shows the problem. I created it from
scratch and it instantly fails for me. I get:
error C3861: 'onTst2': identifier not found (moc_main.cpp)

This file is auto-generated and thus was not updated. -> Should not
happen.

I also tried to manually run cmake, it won't help. However, commenting mainx.qrc helps! Or using qt4_add_resources() and disabling CMAKE_AUTORCC!

Suddenly, the bugtest_automoc target is gone in VS and it compiles
again, even after adding/removing Qt slot functions.
BTW, why is this target called bugtest_automoc and not bugtest_autogen?

The whole thing looks strange as bugtest target still has a
qrc_mainx.cpp.rule but bugtest_automoc also has such a rule.
The bugtest.automoc.rule file properties show no real command:
-----------------
setlocal
cd D:\bugtest\cmake-autogen\build
if %errorlevel% neq 0 goto :cmEnd
D:
if %errorlevel% neq 0 goto :cmEnd
:cmEnd
endlocal & call :cmErrorLevel %errorlevel% & goto :cmDone
:cmErrorLevel
exit /b %1
:cmDone
if %errorlevel% neq 0 goto :VCEnd
-----------------
Is that supposed to actually do something?

I am using Visual Studio 2010. CMake is 3.2.2 but also I tried 3.3-rc1.

You need to create a foo.png to get the qrc to actually create the
source file (actually 2nd bug that CMake doesn't warn about a missing file).

First run, comment out the onTst2 slot function (both h and cpp file),
it should build.
Then comment it before 2nd run.
CMakeLists.txt:
----------------------------------------------
cmake_minimum_required ( VERSION 3.0.2 )
cmake_policy ( VERSION 3.0.2 )

project ( bugtest CXX )

find_package ( Qt4 )

set ( CMAKE_INCLUDE_CURRENT_DIR ON )
set ( CMAKE_AUTOMOC ON )
set ( CMAKE_AUTORCC ON )

add_executable ( bugtest
  main.cpp
  mainx.qrc
)
target_link_libraries ( bugtest Qt4::QtCore )
----------------------------------------------

main.h:
----------------------------------------------
#include <QObject>

class tst1 : public QObject
{
  Q_OBJECT

 public slots:
   void onTst1();
   //void onTst2();
};
----------------------------------------------

main.cpp:
----------------------------------------------
#include "main.h"

void tst1::onTst1()
{
}

// void tst1::onTst2()
// {
// }

int main( int argc, char **argv)
{
  return 0;
}
----------------------------------------------

mainx.qrc:
----------------------------------------------
<!DOCTYPE RCC><RCC version="1.0">
<qresource>
  <file>foo.png</file>
</qresource>
</RCC>
----------------------------------------------
No tags attached.
related to 0015074closed Stephen Kelly Qt 5: resources aren't being updated when CMAKE_AUTORCC is used 
Issue History
2015-06-11 02:14Hendrik SattlerNew Issue
2015-06-16 12:54Brad KingNote Added: 0038935
2015-06-16 15:41Hendrik SattlerNote Added: 0038936
2015-06-16 16:20Brad KingNote Added: 0038937
2015-06-16 16:21Brad KingNote Edited: 0038937bug_revision_view_page.php?bugnote_id=38937#r1808
2015-06-16 16:32Brad KingNote Added: 0038938
2015-06-16 16:36Brad KingNote Added: 0038939
2015-06-16 16:37Brad KingStatusnew => resolved
2015-06-16 16:37Brad KingResolutionopen => no change required
2015-06-16 16:50Hendrik SattlerNote Added: 0038940
2015-06-17 08:21Hendrik SattlerNote Added: 0038942
2015-06-17 08:21Hendrik SattlerStatusresolved => feedback
2015-06-17 08:21Hendrik SattlerResolutionno change required => reopened
2015-06-18 08:33Brad KingStatusfeedback => backlog
2015-06-18 08:33Brad KingResolutionreopened => open
2015-06-18 08:35Brad KingNote Added: 0038943
2015-06-19 09:50Brad KingRelationship addedrelated to 0015074
2015-06-19 09:51Brad KingNote Added: 0038955
2015-06-19 10:36Brad KingNote Added: 0038956
2015-06-19 10:36Brad KingAssigned To => Brad King
2015-06-19 10:36Brad KingStatusbacklog => resolved
2015-06-19 10:36Brad KingResolutionopen => fixed
2015-06-19 10:36Brad KingFixed in Version => CMake 3.4
2015-06-19 10:36Brad KingTarget Version => CMake 3.4
2016-02-01 09:10Robert MaynardNote Added: 0040403
2016-02-01 09:10Robert MaynardStatusresolved => closed

Notes
(0038935)
Brad King   
2015-06-16 12:54   
I cannot reproduce this. Here is a session:

$ cat CMakeLists.txt
cmake_minimum_required(VERSION 3.0.2)
project(Issue15608 CXX)

find_package(Qt4)

set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

add_executable(bugtest
  main.cpp
  mainx.qrc
  )
target_link_libraries(bugtest Qt4::QtCore)

$ cat main.h
#include <QObject>

class tst1 : public QObject
{
  Q_OBJECT

 public slots:
   void onTst1();
   void onTst2();
};

$ cat main.cpp
#include "main.h"

void tst1::onTst1()
{
}

void tst1::onTst2()
{
}

int main( int argc, char **argv)
{
  return 0;
}

$ cat mainx.qrc
<!DOCTYPE RCC><RCC version="1.0">
<qresource>
  <file>foo.png</file>
</qresource>
</RCC>

$ test -f foo.png && echo exists
exists

$ cmake --version
cmake version 3.3.0-rc2

$ mkdir build
$ cd build

$ cmake .. -G "Visual Studio 10 2010" -DQT_QMAKE_EXECUTABLE=c:/.../bin/qmake
$ cmake --build . --config Debug
...
Build succeeded.
    0 Warning(s)
    0 Error(s)
(0038936)
Hendrik Sattler   
2015-06-16 15:41   
Hi Brad,

you forgot the second part where the slot function onTst2() is delete from .h and .cpp file. Then try to recompile.

Additionally, I only tried with Visual Studio 2010 GUI, not command line build. The above will use MSBuild, the GUI uses devenv AFAIK.

I'll try again, tommorrow with 3.3-rc2 but I'm sure that it still breaks.
(0038937)
Brad King   
2015-06-16 16:20   
(edited on: 2015-06-16 16:21)
Re 0015608:0038936: Thanks. I was confused by "comment out" in "First run, comment out the onTst2 slot function".

I now continued the session in 0015608:0038935 by removing onTst2 from the .h and .cpp files and then could see the error:

$ cmake --build . --config Debug
...\build\moc_main.cpp(50): error C2039: 'onTst2' : is not a member of 'tst1'


(0038938)
Brad King   
2015-06-16 16:32   
One must set the AUTOGEN_TARGET_DEPENDS target property for this case. See property documentation for why:

  http://www.cmake.org/cmake/help/v3.3/prop_tgt/AUTOGEN_TARGET_DEPENDS.html [^]

I added the line

  set_property(TARGET bugtest PROPERTY AUTOGEN_TARGET_DEPENDS main.cpp mainx.qrc)

to the end of the example CMakeLists.txt file and now it works.
(0038939)
Brad King   
2015-06-16 16:36   
Re 0015608:0038938: This is documented in the cmake-qt(7) manual:

 http://www.cmake.org/cmake/help/v3.3/manual/cmake-qt.7.html#qt-build-tools [^]
 "The tools are executed as part of a synthesized custom target generated by
  CMake. Target dependencies may be added to that custom target by adding
  them to the AUTOGEN_TARGET_DEPENDS target property."
(0038940)
Hendrik Sattler   
2015-06-16 16:50   
Then why does automoc work without that line when not using autorcc?
So everyone needs to add all files that potentially need moc or rcc? For most projects this will be all source and all header files. That's really strange...
(0038942)
Hendrik Sattler   
2015-06-17 08:21   
After looking into it, I don't think that AUTOGET_TARGET_DEPENDS is the right solution here.

Why?
Because when _NOT_ using AUTORCC, it is not needed!
Enabling both is what causes the wrong behaviour for AUTOMOC.

The solution cannot be adding all sources of a target AUTOGEN_TARGET_DEPENDS.
I always understood that as dependencies needed when moc runs (e.g. generated header files included into the moc'd sources) but not the sources for moc to run on.

Aditionally, the first build should also fail, not just after the first incompatible mofification.
(0038943)
Brad King   
2015-06-18 08:35   
Steve, Clinton?
(0038955)
Brad King   
2015-06-19 09:51   
I think the change made for issue 0015074 broke this by causing automoc to not always rerun when autorcc is enabled.
(0038956)
Brad King   
2015-06-19 10:36   
Here is a fix and a test case:

 QtAutogen: Always run autogen step even when rcc is enabled
 http://cmake.org/gitweb?p=cmake.git;a=commitdiff;h=0e346427 [^]

It also allows this simplification:

 QtAutogen: Use PRE_BUILD in Visual Studio generators even with rcc
 http://cmake.org/gitweb?p=cmake.git;a=commitdiff;h=12c3fcde [^]
(0040403)
Robert Maynard   
2016-02-01 09:10   
Closing resolved issues that have not been updated in more than 4 months.