[CMake] CmakeLists chain cannot find source added with add_custom_command

Eric Noulard eric.noulard at gmail.com
Sun Jul 2 06:57:57 EDT 2017


Ok,

I reproduced your issue and I can then confirm it.
I tried several thing including:
   - generating the file is the buildtree
   - refering to the same file with another target in the same dir
   - using absolute path in the sub2/add_executable

nothing works.
it looks like a generated file cannot be referred outside the directory
where the custom command generates it ???

Here comes an archive with an example similar to the one descrived by J.
Decker.
If you build it without option it wokrs (because I used an intermediate
object lib)
If you (try to) build it with

cmake -DBROKEN=1

then it'll fails with "Cannot find source file..."

This is definitely weird and I never encountered this after +10 years of
CMake usage!!
So I guess I have always used generated file locally to build exe or lib
and then refer to the lib.
If this is the intended behaviour then it should be documented more
explicitely.

Now re-reading the doc of add_custom_command:

"This defines a command to generate specified ``OUTPUT`` file(s).
A target created in the same directory (``CMakeLists.txt`` file)
that specifies any output of the custom command as a source file
is given a rule to generate the file using the command at build time."

So the "same directory" should be underlined!! Or may be another sentence
saying that in fact you CANNOT refer to outputed files outside the
directory...

In fact it is possible to have cross-directory reference to generated file
but this is
more like a workaround. You'll have to:

define your custom_command and an associated custom target in dir1
define your exe/lib in dir2 and explicitely:
   - flag the referred cross-dir generated source as GENERATED
   - make your exe/lib depend on the cross-dir custom target in order to
trigger the file generation.


The second zip implement that workaround. This is nasty, I wouldn't do that.
Shall we open a bug for this misleading behavior or shall we open a bug for
doc update?



2017-07-02 10:20 GMT+02:00 J Decker <d3ck0r at gmail.com>:

> It is not generated; because it is generated during a the build phase not
> the generate phase.  So only the logical idea of the file exists.
>
> On Sun, Jul 2, 2017 at 12:12 AM, Eric Noulard <eric.noulard at gmail.com>
> wrote:
>
>> Needless to say that your example should work but somehow CMake does not
>> see that the referred file is GENERATED.
>>
>> Le 2 juil. 2017 09:11, "Eric Noulard" <eric.noulard at gmail.com> a écrit :
>>
>>> Is the referred file created?
>>> Does your actual command creating the file doing it in the source tree
>>> and not in the build tree?
>>> Are you building in-source or out of source?
>>>
>>> Could you give us us a toy example which shows the issue?
>>>
>>> Le 2 juil. 2017 04:32, "J Decker" <d3ck0r at gmail.com> a écrit :
>>>
>>> Known issue?  Or just doesn't require a response?
>>>
>>> On Wed, Jun 28, 2017 at 6:05 AM, J Decker <d3ck0r at gmail.com> wrote:
>>>
>>>> I have this chain of makefiles.  It adds a custom rule to build a
>>>> source file, and then a subdirectory cannot reference that file.
>>>>
>>>> The first is added custom rule for M:/tmp/cmake-chain/sub/src/sack.c
>>>> and then later cannot find M:/tmp/cmake-chain/sub/src/sack.c
>>>> though it's really referencing it as M:/tmp/cmake-chain/sub/sub2
>>>> /../src/sack.c
>>>>
>>>> tested with 3.6.0-rc4 and 3.9.0-rc5
>>>>
>>>> if the add_custom_rule and add_executable are in the same makefile even
>>>> if there's a added path to make it not exactly the same source as the
>>>> 'referencing source' message logs; it works.
>>>>
>>>>
>>>> --- CMakeLists.txt ---
>>>> cmake_minimum_required(VERSION 2.8)
>>>>
>>>> message( "Adding custom rule for ${CMAKE_CURRENT_SOURCE_DIR}/src/sack.c"
>>>> )
>>>>   add_custom_command( OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/src/sack.c
>>>>   COMMAND cmake -E echo do something to make src/sack.c
>>>>   )
>>>> add_subdirectory( sub2 )
>>>> -------------------------------------
>>>>
>>>> --- sub2/CmakeLists.txt ---
>>>> message( "referencing source ${CMAKE_CURRENT_SOURCE_DIR}/../src/sack.c"
>>>> )
>>>> add_executable( something ${CMAKE_CURRENT_SOURCE_DIR}/../src/sack.c )
>>>>
>>>> -------------------------------------
>>>>
>>>> --- Truncated output log ------
>>>> Adding custom rule for M:/tmp/cmake-chain/sub/src/sack.c
>>>> referencing source M:/tmp/cmake-chain/sub/sub2/../src/sack.c
>>>> -- Configuring done
>>>> CMake Error at sub2/CMakeLists.txt:3 (add_executable):
>>>>   Cannot find source file:
>>>>
>>>>     M:/tmp/cmake-chain/sub/src/sack.c
>>>>
>>>>   Tried extensions .c .C .c++ .cc .cpp .cxx .m .M .mm .h .hh .h++ .hm
>>>> .hpp
>>>>   .hxx .in .txx
>>>> --------------------------------------
>>>>
>>>>
>>>> ------ Flat CmakeLists.txt which works ---------------
>>>> cmake_minimum_required(VERSION 2.8)
>>>>
>>>> message( "Adding custom rule for ${CMAKE_CURRENT_SOURCE_DIR}/src/sack.c"
>>>> )
>>>>   add_custom_command( OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/src/sack.c
>>>> COMMAND cmake -e echo make file.
>>>>   )
>>>>
>>>> message( "referencing source ${CMAKE_CURRENT_SOURCE_DIR}/src/sack.c" )
>>>> add_executable( something ${CMAKE_CURRENT_SOURCE_DIR}/sub2/../src/sack.c
>>>> )
>>>> --------------------------------------
>>>>
>>>> ----- flat cmakelists output.... -----------
>>>> Adding custom rule for M:/tmp/cmake-chain-flat/src/sack.c
>>>> referencing source M:/tmp/cmake-chain-flat/sub2/../src/sack.c
>>>> -- Configuring done
>>>> --------------------------------------
>>>>
>>>>
>>>
>>> --
>>>
>>> Powered by www.kitware.com
>>>
>>> Please keep messages on-topic and check the CMake FAQ at:
>>> http://www.cmake.org/Wiki/CMake_FAQ
>>>
>>> Kitware offers various services to support the CMake community. For more
>>> information on each offering, please visit:
>>>
>>> CMake Support: http://cmake.org/cmake/help/support.html
>>> CMake Consulting: http://cmake.org/cmake/help/consulting.html
>>> CMake Training Courses: http://cmake.org/cmake/help/training.html
>>>
>>> Visit other Kitware open-source projects at
>>> http://www.kitware.com/opensource/opensource.html
>>>
>>> Follow this link to subscribe/unsubscribe:
>>> http://public.kitware.com/mailman/listinfo/cmake
>>>
>>>
>>>
>
> --
>
> Powered by www.kitware.com
>
> Please keep messages on-topic and check the CMake FAQ at:
> http://www.cmake.org/Wiki/CMake_FAQ
>
> Kitware offers various services to support the CMake community. For more
> information on each offering, please visit:
>
> CMake Support: http://cmake.org/cmake/help/support.html
> CMake Consulting: http://cmake.org/cmake/help/consulting.html
> CMake Training Courses: http://cmake.org/cmake/help/training.html
>
> Visit other Kitware open-source projects at http://www.kitware.com/
> opensource/opensource.html
>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/cmake
>



-- 
Eric
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20170702/1b801e3f/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: GenFileReferredInSubdir.zip
Type: application/zip
Size: 1564 bytes
Desc: not available
URL: <http://public.kitware.com/pipermail/cmake/attachments/20170702/1b801e3f/attachment-0002.zip>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: GenFileReferredInSubdir-v2.zip
Type: application/zip
Size: 1651 bytes
Desc: not available
URL: <http://public.kitware.com/pipermail/cmake/attachments/20170702/1b801e3f/attachment-0003.zip>


More information about the CMake mailing list