MantisBT - CMake
View Issue Details
0015729CMakeCMakepublic2015-09-06 04:362016-03-07 09:12
Stefan Bühler 
Stephen Kelly 
normalminoralways
closedfixed 
LinuxDebiantesting/unstable
CMake 3.3.1 
CMake 3.4 
0015729: ninja: TARGET_OBJECTS not working, ObjectDirectory and ObjectPathMax used before they are set
I converted a project to Qt5, and TARGET_OBJECTS are not working across directories anymore with ninja; traditional make is still working.

I pulled cmake from git (master 9e64fda6a77b49b394835dadba0e2951580eb86b), and got even stranger errors (but not everytime) that CMAKE_OBJECT_PATH_MAX was 0 and the "Object file ... cannot be safely placed under this directory.".

I added some debugging and got a backtrace like this:

#0 cmLocalGenerator::CreateSafeUniqueObjectFileName (this=0x12f2010, sin=..., dir_max=...) at ../Source/cmLocalGenerator.cxx:2660
#1 0x0000000000aeab84 in cmLocalGenerator::GetObjectFileNameWithoutTarget (this=0x12f2010, source=..., dir_max=..., hasSourceExtension=0x0) at ../Source/cmLocalGenerator.cxx:2810
0000002 0x00000000009223a5 in cmLocalNinjaGenerator::ComputeObjectFilenames (this=0x12f2010, mapping=..., gt=0x13ea920) at ../Source/cmLocalNinjaGenerator.cxx:269
0000003 0x0000000000a5ddec in cmGeneratorTarget::GetObjectSources (this=0x13ea920, data=..., config=...) at ../Source/cmGeneratorTarget.cxx:480
0000004 0x0000000000a4b820 in TargetObjectsNode::Evaluate (this=0xfdd3c0 <targetObjectsNode>, parameters=..., context=0x7fffffffb010, content=0x131d850) at ../Source/cmGeneratorExpressionNode.cxx:1285
0000005 0x0000000000a413f6 in GeneratorExpressionContent::Evaluate[abi:cxx11](cmGeneratorExpressionContext*, cmGeneratorExpressionDAGChecker*) const (this=0x131d850, context=0x7fffffffb010, dagChecker=0x7fffffffb720) at ../Source/cmGeneratorExpressionEvaluator.cxx:154
0000006 0x0000000000a5856c in cmCompiledGeneratorExpression::EvaluateWithContext (this=0x132cff0, context=..., dagChecker=0x7fffffffb720) at ../Source/cmGeneratorExpression.cxx:101
0000007 0x0000000000a58474 in cmCompiledGeneratorExpression::Evaluate (this=0x132cff0, mf=0x130f300, config=..., quiet=false, headTarget=0x1333028, currentTarget=0x1333028, dagChecker=0x7fffffffb720, language=...) at ../Source/cmGeneratorExpression.cxx:79
0000008 0x00000000008d140d in processSources (tgt=0x1333028, entries=..., srcs=..., uniqueSrcs=..., dagChecker=0x7fffffffb720, config=..., debugSources=false) at ../Source/cmTarget.cxx:557
#9 0x00000000008d21b7 in cmTarget::GetSourceFiles (this=0x1333028, files=..., config=...) at ../Source/cmTarget.cxx:700
0000010 0x00000000008d25e8 in cmTarget::GetSourceFiles (this=0x1333028, files=..., config=...) at ../Source/cmTarget.cxx:750
#11 0x0000000000a70b8a in cmGeneratorTarget::GetConfigCommonSourceFiles (this=0x13f0240, files=...) at ../Source/cmGeneratorTarget.cxx:4431
0000012 0x0000000000888ecd in cmQtAutoGenerators::InitializeAutogenTarget (this=0x7fffffffc6d0, lg=0x1312970, target=0x1333028) at ../Source/cmQtAutoGenerators.cxx:408
0000013 0x0000000000a99924 in cmGlobalGenerator::CreateQtAutoGeneratorsTargets (this=0x101ad40, autogens=...) at ../Source/cmGlobalGenerator.cxx:1465
0000014 0x0000000000a986c3 in cmGlobalGenerator::Compute (this=0x101ad40) at ../Source/cmGlobalGenerator.cxx:1272
0000015 0x0000000000900927 in cmake::Generate (this=0x7fffffffd7f0) at ../Source/cmake.cxx:1770
0000016 0x0000000000900783 in cmake::Run (this=0x7fffffffd7f0, args=..., noconfigure=false) at ../Source/cmake.cxx:1757
0000017 0x00000000008197d5 in do_cmake (ac=4, av=0x10111f0) at ../Source/cmakemain.cxx:330
0000018 0x0000000000818d8a in main (ac=4, av=0x10111f0) at ../Source/cmakemain.cxx:190

CreateQtAutoGeneratorsTargets gets called before ComputeObjectMaxPath in cmGlobalGenerator::Compute, and ObjectPathMax is not initialized, i.e. has a random value at that point (for me 0 or 1).

Also it turned out gt->ObjectDirectory is empty when TargetObjectsNode::Evaluate is called in a similar backtrace from CreateQtAutoGeneratorsTargets, which is why newer cmake version can't find the object files (older cmake versions simply drop the object, leading to undefined symbols during linking).

Moving the call to CreateQtAutoGeneratorsTargets below InitGeneratorTargets fixes it; calling ComputeTargetObjectDirectory and ComputeObjectMaxPath on demand and initializing ObjectPathMax to 0 fixes it too.
git clone -b v0.6.0 https://github.com/dosnut/nut [^]
cd nut
mkdir build
cd build
cmake -G Ninja ..
ninja cnut
No tags attached.
Issue History
2015-09-06 04:36Stefan BühlerNew Issue
2015-09-18 14:32Stephen KellyNote Added: 0039444
2015-09-18 15:21Stefan BühlerNote Added: 0039446
2015-09-18 18:30Stefan BühlerNote Added: 0039447
2015-09-20 10:12Stephen KellyNote Added: 0039450
2015-09-20 13:53Stefan BühlerNote Added: 0039452
2015-09-29 12:11Stephen KellyNote Added: 0039493
2015-09-29 12:11Stephen KellyStatusnew => resolved
2015-09-29 12:11Stephen KellyFixed in Version => CMake 3.4
2015-09-29 12:11Stephen KellyResolutionopen => fixed
2015-09-29 12:11Stephen KellyAssigned To => Stephen Kelly
2016-03-07 09:12Robert MaynardNote Added: 0040602
2016-03-07 09:12Robert MaynardStatusresolved => closed

Notes
(0039444)
Stephen Kelly   
2015-09-18 14:32   
Please provide an http://sscce.org/ [^] for this.
(0039446)
Stefan Bühler   
2015-09-18 15:21   
You could have said "it would be nice if someone could create a sscce for this" (because why does it have to be me?), and I might have tried to find some time to do this.

The initialization order bugs are clearly visible from the backtrace; so a) fix your initializations (it shouldn't be "undefined" in any case), and b) add checks to make sure certain functions can't be called before everything is setup they require.

And then you can try to fix the actual bug, and I might try to help you with it.

But you really shouldn't piss of people who report bugs in your software like this when you have all the details needed, and just don't want to put in the effort.
(0039447)
Stefan Bühler   
2015-09-18 18:30   
I just realized my instructions are broken; the git tag is "nut-v0.6.0" - sorry about that.

Also I now have a reduced test case:

CMakeLists.txt:
---
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)

set(CMAKE_AUTOMOC ON)
find_package(Qt5Core REQUIRED)

add_library(objectlib OBJECT
    lib.cpp
)

add_executable(main
    $<TARGET_OBJECTS:objectlib>
    main.cpp
)
---

lib and main are rather trivial:

lib.cpp:
---
void func() {
}
---

main.cpp:
---
void func();

int main() {
    func();
}
---

The classic makefile generator works fine, current cmake head with ninja gives:
---
$ ninja main
ninja: error: 'lib.cpp.o', needed by 'main', missing and no known rule to make it
---

cmake 3.2.2 (debian testing) gives:
---
$ ninja main
[5/5] Linking CXX executable main
FAILED: : && /usr/bin/c++ CMakeFiles/main.dir/main.cpp.o CMakeFiles/main.dir/main_automoc.cpp.o -o main -rdynamic && :
CMakeFiles/main.dir/main.cpp.o: In function `main':
main.cpp:(.text+0x5): undefined reference to `func()'
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
---
(0039450)
Stephen Kelly   
2015-09-20 10:12   
Sorry, I had no intention of pissing anyone off by asking for an sscce.

Thanks for the testcase. I have ideas of how to fix this, but it will not be in the next release because it requires refactoring.
(0039452)
Stefan Bühler   
2015-09-20 13:53   
Maybe you could add a check that fails with a helpful error message.
(0039493)
Stephen Kelly   
2015-09-29 12:11   
After some refactoring,

 cmLocalGenerator: Compute object max path on construction.
 https://cmake.org/gitweb?p=cmake.git;a=commitdiff;h=8bfff686 [^]

fixed the initialization of the ObjectPathMax.

After more refactoring,

 QtAutogen: Move Source initialization to prior loop.
 https://cmake.org/gitweb?p=cmake.git;a=commitdiff;h=64b78c14 [^]

properly computed the sources for all targets before attempting to initialize automoc for them.

Then,

 cmGlobalGenerator: Initialize generator targets on construction (0015729)
 https://cmake.org/gitweb?p=cmake.git;a=commitdiff;h=9bc6eb8e [^]

resolved the conflict between AUTOMOC and TARGET_OBJECTS, including the supplied testcase.

Thanks!
(0040602)
Robert Maynard   
2016-03-07 09:12   
Closing resolved issues that have not been updated in more than 4 months.