Source file generated by add_custom_command() into subdirectory are not seen by subdirectory's target
It seems that source files generated by add_custom_command()
into a subdirectory are not seen by CMake targets in that subdirectory.
I've pushed a completely self-contained example here for replication. Building it yields these two errors (and a few subsequent spurious ones that can be ignored):
$ cd build && cmake ..
-- The CXX compiler identification is AppleClang 14.0.0.14000029
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
CMake Error at ACE_wrappers/TAO/tao/AnyTypeCode/CMakeLists.AnyTypeCode:32 (add_library):
Cannot find source file:
BooleanSeqA.cpp
Tried extensions .c .C .c++ .cc .cpp .cxx .cu .mpp .m .M .mm .ixx .cppm .h
.hh .h++ .hm .hpp .hxx .in .txx .f .F .for .f77 .f90 .f95 .f03 .hip .ispc
Call Stack (most recent call first):
ACE_wrappers/TAO/tao/AnyTypeCode/CMakeLists.txt:12 (include)
CMake Error at ACE_wrappers/TAO/tao/PortableServer/CMakeLists.PortableServer:32 (add_library):
Cannot find source file:
PolicyS.cpp
Tried extensions .c .C .c++ .cc .cpp .cxx .cu .mpp .m .M .mm .ixx .cppm .h
.hh .h++ .hm .hpp .hxx .in .txx .f .F .for .f77 .f90 .f95 .f03 .hip .ispc
Call Stack (most recent call first):
ACE_wrappers/TAO/tao/PortableServer/CMakeLists.txt:12 (include)
In both cases, the "missing" source files for these two targets (TAO_AnyTypeCode
and TAO_PortableServer
) are in fact generated by custom commands in ACE_wrappers/TAO/tao/CMakeLists.TAO_Core_idl
, which lies in their common parent directory.
(They're generated via the macro IDL_FILES_TARGET_SOURCES()
, defined in cmake/idl_files.cmake
, which calls tao_idl_command()
, defined in cmake/tao_idl_sources.cmake
, which outputs them into source directories/subdirectories).
Now this example has thousands of source files generated by IDL_FILES_TARGET_SOURCES()
, even other source files for those two targets. But they're almost always generated by CMakeLists
in the same directory as the targets that consume them, causing no errors. However, when source files are emitted into a subdirectory, as above, it seems like targets within that subdirectory don't see them.
Note that the macros use absolute file paths for OUTPUT
when calling add_custom_command
, to ensure outputs in the source directory structure.