VS: Csharp Improper reference to csharp files in build directory
Introduction
I have recently been using the SWIG bindings generator more and I want to incorporate the files generated by the SWIG compiler in to unit test to validate they were properly generated. This approach appeared to be working, but yesterday I noticed that the csproj files generated by my code wa malformed and could not properly compile. I've tested with 3.24 and 3.22 and it appears all versions that support msvc 2022 are affected. I have noticed two behaviors that may be caused by merge request from last year. I will try to describe below
Example Project: https://github.com/stevenawhite/introductions
Use Case
- Compile C/CPP Code
- add_swig_library which uses templates refencing C/CPP code. Instruct SWIG to place OUTPUT_DIR inside CMAKE_BINARY_DIR
- FILE(GLOB_RECURSE CONFIG_DEPEND) on generated code.
- add_executable for a in source unit test and the generated_code
Scenario 1:
If the generated_code inside the unit_text CMAKE_CURRENT_BINARY_DIR the files are created using the following template.
<Compile Include="%AbsolutePathToFile%">
<Link>%FileName%</Link
</Compile>
This results in a load warning and the files are not included in the project.
Severity Code Description Project File Line Suppression State
Warning The file '%AbsolutePathToFile%' could not be added to the project. Cannot add a link to the file %AbsolutePathToFile%^. This file is within the project directory tree. %Project%
Scenario 2
If the generated code is located in CMAKE_BINARY_DIR but out side of the CMAKE_CURRENT_BINARY_DIR sub tree then the files are referenced by the following type
<None Include="%AbsolutePathToFile%">
<Link>%FileName%</Link>
</None>
This causes the files to show up and in general not work. In this example the SWIG files specifically do not resolve while all the more complex files swig can generate seem to work. It is more obvious in the full BioGears project then the reduction problem I listed above.
Possible Fixes
- Mark all *.cs files as unless a source property is set maybe CSHARP_LINK_ONLY
- If %FILE% is relative to CMAKE_CURRENT_BINARY_DIR never include a Tag
- When a LINK tag is used respect SOURCE_GROUP property value. All of these CSharp projects I generate just have one giant flat list of binding files despite actual directory organization
I'm open to work arounds or suggestions. I just noticed this behavior last night and haven't tracked down what version of CMake/Visual Studio this setup used to work for, but I've been compiling these bindings for while, but just recently started seeing this behavior. I thought it was when I upgraded from 3.22 but testing this morning with the final 3.22 release seemed to behave identical to the current release. However, I haven't had time to grab the initial release. I'll try to test 3.21 against 2019 and 3.22.0 tonight.