From b88ec09915a6b12d1aaec7e451ecf0e41a831030 Mon Sep 17 00:00:00 2001 From: Leon Matthes Date: Wed, 18 Dec 2024 08:51:14 +0100 Subject: [PATCH] Docs: "Cannot find source file" in common_issues As described in #580, this error may be encountered when using corrosion_add_cxxbridge, now that the generated headers are added as `PUBLIC` sources. --- doc/src/common_issues.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/doc/src/common_issues.md b/doc/src/common_issues.md index 62d3d3d9..c7af9a0b 100644 --- a/doc/src/common_issues.md +++ b/doc/src/common_issues.md @@ -6,6 +6,7 @@ - [Linking Rust static libraries into Debug C/C++ binaries fails on Windows MSVC targets](#linking-rust-static-libraries-into-debug-cc-binaries-fails-on-windows-msvc-targets) - [Missing `soname` on Linux for `cdylibs`](#missing-soname-on-linux-for-cdylibs) - [Missing `install_name` on MacOS for `ccdylibs` / Hardcoded references to the build-directory](#missing-installname-on-macos-for-ccdylibs--hardcoded-references-to-the-build-directory) +- [CMake Error (target_link_libraries): Cannot find source file](#cmake-error-target_link_libraries-cannot-find-source-file) ## Linking Debug C/C++ libraries into Rust fails on Windows MSVC targets @@ -86,3 +87,36 @@ When building binaries using this shared library, you should set the build rpath your shared library, e.g. by setting `set(CMAKE_BUILD_RPATH ${YOUR_CUSTOM_OUTPUT_DIRECTORY})` before adding executables. For a practical example, you may look at [Slint PR 2455](https://github.com/slint-ui/slint/pull/2455). + +## CMake Error (target_link_libraries): Cannot find source file + +When using `corrosion_add_cxxbridge`, you may encounter an error similar to this in targets that depend on the cxxbridge target: + +```diff +- CMake Error at ...../CMakeLists.txt:61 (target_link_libraries): +- Cannot find source file: +- +- ...../corrosion_generated/..../somefile.h +- +- Tried extensions .c .C .c++ .cc .cpp .cxx .cu .mpp .m .M .mm .ixx .cppm +- .ccm .cxxm .c++m .h .hh .h++ .hm .hpp .hxx .in .txx .f .F .for .f77 .f90 +- .f95 .f03 .hip .ispc +``` + +Where `somefile.h` should be generated by CXX via `corrosion_add_cxxbridge`. +In theory, CMake should already know that this is a generated file and just generate it when needed. + +However, in older versions of CMake the `GENERATED` property isn't correctly propagated. +See also: [https://gitlab.kitware.com/cmake/cmake/-/issues/18399](https://gitlab.kitware.com/cmake/cmake/-/issues/18399) + +This has since been fixed with CMake 3.20: [https://cmake.org/cmake/help/v3.20/policy/CMP0118.html](https://cmake.org/cmake/help/latest/command/cmake_policy.html#version) +However, the CMake policy CMP0118 must be enabled **in any dependent CMakeLists.txt** for the fix to work. + +The best fix is to call: +```cmake +cmake_minimium_required(VERSION 3.20 FATAL_ERROR) +# (or any other version above 3.20) +``` +As described [here](https://cmake.org/cmake/help/latest/command/cmake_policy.html#version), this implies a call to `cmake_policy` which enables CMP0118. + +Unfortunately this must be done in all (transitive) downstream dependencies that link to the bridge target, so cannot be done from within corrosion automatically.