Repository: pytorch
Issue: caffe2_interface_library CMake macro prevents linking to LibTorch as a transitive dependency
Labels: module: cpp,triaged

### 🐛 Describe the bug

I have a CMake C++ library that uses LibTorch. It has a layout like this (if simplified):

* `src/CMakeLists.txt` downloads Torch, finds it with `find_package`, and creates the main library `lib` that publicly links to `${TORCH_LIBRARIES}`
* `tests/CMakeLists.txt` creates executable `tests` that links to `lib`
* Top-level `CMakeLists.txt` adds the above directories with `add_subdirectory` in the order they are written above

Here is the sample project: [project.zip](https://github.com/pytorch/pytorch/files/10010630/project.zip).

The expected behavior is for the project to build fine but in reality it fails to configure with a lot of errors when `lib` is linked to `tests` (the `lib` itself can be built without any errors if `tests` is removed):

```
CMake Error at tests/CMakeLists.txt:5 (target_link_libraries):
  Error evaluating generator expression:

    $<TARGET_PROPERTY:torch_cpu,INTERFACE_COMPILE_DEFINITIONS>

  Target "torch_cpu" not found.


CMake Error at tests/CMakeLists.txt:5 (target_link_libraries):
  Error evaluating generator expression:

    $<TARGET_PROPERTY:torch,INTERFACE_COMPILE_DEFINITIONS>

  Target "torch" not found.


CMake Error at tests/CMakeLists.txt:5 (target_link_libraries):
  Error evaluating generator expression:

    $<TARGET_PROPERTY:torch_cpu,INTERFACE_INCLUDE_DIRECTORIES>

  Target "torch_cpu" not found.


CMake Error at tests/CMakeLists.txt:5 (target_link_libraries):
  Error evaluating generator expression:

    $<TARGET_PROPERTY:torch,INTERFACE_INCLUDE_DIRECTORIES>

  Target "torch" not found.


CMake Error:
  Error evaluating generator expression:

    $<TARGET_PROPERTY:torch_cpu,INTERFACE_SYSTEM_INCLUDE_DIRECTORIES>

  Target "torch_cpu" not found.


CMake Error:
  Error evaluating generator expression:

    $<TARGET_PROPERTY:torch_cpu,INTERFACE_INCLUDE_DIRECTORIES>

  Target "torch_cpu" not found.


CMake Error:
  Error evaluating generator expression:

    $<TARGET_PROPERTY:torch,INTERFACE_SYSTEM_INCLUDE_DIRECTORIES>

  Target "torch" not found.


CMake Error:
  Error evaluating generator expression:

    $<TARGET_PROPERTY:torch,INTERFACE_INCLUDE_DIRECTORIES>

  Target "torch" not found.


CMake Error at tests/CMakeLists.txt:5 (target_link_libraries):
  Error evaluating generator expression:

    $<TARGET_PROPERTY:torch_cpu,INTERFACE_COMPILE_OPTIONS>

  Target "torch_cpu" not found.


CMake Error at tests/CMakeLists.txt:5 (target_link_libraries):
  Error evaluating generator expression:

    $<TARGET_PROPERTY:torch,INTERFACE_COMPILE_OPTIONS>

  Target "torch" not found.


CMake Error at tests/CMakeLists.txt:5 (target_link_libraries):
  Error evaluating generator expression:

    $<TARGET_PROPERTY:torch_cpu,INTERFACE_COMPILE_DEFINITIONS>

  Target "torch_cpu" not found.


CMake Error at tests/CMakeLists.txt:5 (target_link_libraries):
  Error evaluating generator expression:

    $<TARGET_PROPERTY:torch,INTERFACE_COMPILE_DEFINITIONS>

  Target "torch" not found.


CMake Error at tests/CMakeLists.txt:5 (target_link_libraries):
  Error evaluating generator expression:

    $<TARGET_PROPERTY:torch_cpu,INTERFACE_INCLUDE_DIRECTORIES>

  Target "torch_cpu" not found.


CMake Error at tests/CMakeLists.txt:5 (target_link_libraries):
  Error evaluating generator expression:

    $<TARGET_PROPERTY:torch,INTERFACE_INCLUDE_DIRECTORIES>

  Target "torch" not found.


CMake Error:
  Error evaluating generator expression:

    $<TARGET_PROPERTY:torch_cpu,INTERFACE_SYSTEM_INCLUDE_DIRECTORIES>

  Target "torch_cpu" not found.


CMake Error:
  Error evaluating generator expression:

    $<TARGET_PROPERTY:torch_cpu,INTERFACE_INCLUDE_DIRECTORIES>

  Target "torch_cpu" not found.


CMake Error:
  Error evaluating generator expression:

    $<TARGET_PROPERTY:torch,INTERFACE_SYSTEM_INCLUDE_DIRECTORIES>

  Target "torch" not found.


CMake Error:
  Error evaluating generator expression:

    $<TARGET_PROPERTY:torch,INTERFACE_INCLUDE_DIRECTORIES>

  Target "torch" not found.


CMake Error at tests/CMakeLists.txt:5 (target_link_libraries):
  Error evaluating generator expression:

    $<TARGET_PROPERTY:torch_cpu,INTERFACE_COMPILE_DEFINITIONS>

  Target "torch_cpu" not found.


CMake Error at tests/CMakeLists.txt:5 (target_link_libraries):
  Error evaluating generator expression:

    $<TARGET_PROPERTY:torch,INTERFACE_COMPILE_DEFINITIONS>

  Target "torch" not found.


CMake Error at tests/CMakeLists.txt:5 (target_link_libraries):
  Error evaluating generator expression:

    $<TARGET_PROPERTY:torch_cpu,INTERFACE_INCLUDE_DIRECTORIES>

  Target "torch_cpu" not found.


CMake Error at tests/CMakeLists.txt:5 (target_link_libraries):
  Error evaluating generator expression:

    $<TARGET_PROPERTY:torch,INTERFACE_INCLUDE_DIRECTORIES>

  Target "torch" not found.
```

I opened an issue at CMake's repo [here](https://gitlab.kitware.com/cmake/cmake/-/issues/24163) and one of the devs replied that this happened because [this caffe2_interface_library macro](https://github.com/pytorch/pytorch/blob/v1.13.0/cmake/public/utils.cmake#L195-L292) uses `$<TARGET_PROPERTY:tgt,prop>` instead of `target_link_libraries` (more details can be found in the reply itself). It seems like this is the expected behavior for CMake's implementation even though it is not properly documented.

### Versions

CMake version: version 3.22.1
Libc version: glibc-2.35

Python version: 3.10.6 (main, Nov  2 2022, 18:53:38) [GCC 11.3.0] (64-bit runtime)
Python platform: Linux-5.10.16.3-microsoft-standard-WSL2-x86_64-with-glibc2.35
Is CUDA available: N/A
CUDA runtime version: Could not collect
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: GPU 0: NVIDIA GeForce RTX 2060
Nvidia driver version: 526.86
cuDNN version: Could not collect
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: N/A

Versions of relevant libraries:
[pip3] No relevant packages
[conda] Could not collect

cc @jbschlosser