Fix multiple find_package(Torch) calls (#8407)
While debugging the build issue on https://github.com/pytorch/executorch/pull/8322 w.r.t mkl, I undercover a complex interaction between https://github.com/pytorch/executorch/pull/8322, https://github.com/pytorch/executorch/pull/8248 (to install mkl), and https://github.com/pytorch/pytorch/blob/main/cmake/public/mkl.cmake from PyTorch. The error is as follows: ``` CMake Error at /opt/conda/envs/py_3.10/lib/cmake/mkl/MKLConfig.cmake:744 (add_library): <-- This file comes from conda mkl add_library cannot create imported target "MKL::MKL" because another target with the same name already exists. Call Stack (most recent call first): /opt/conda/envs/py_3.10/lib/python3.10/site-packages/torch/share/cmake/Caffe2/public/mkl.cmake:1 (find_package) <-- this is from PyTorch /opt/conda/envs/py_3.10/lib/python3.10/site-packages/torch/share/cmake/Caffe2/Caffe2Config.cmake:106 (include) /opt/conda/envs/py_3.10/lib/python3.10/site-packages/torch/share/cmake/Torch/TorchConfig.cmake:68 (find_package) CMakeLists.txt:753 (find_package) ``` The conclusion is that, with mkl installed, there should be just one `find_package(Torch)` call because the mkl target is defined globally. The `torch` target, on the other hand, is only defined locally. So, this change adds `if(NOT TARGET torch)` check to only call `find_package(Torch)` if needed. ### Testing The change on top of https://github.com/pytorch/executorch/pull/8322 looks like this https://github.com/pytorch/executorch/pull/8399/commits/f705b01a51486e1cbe318d142015da35a83e2578 https://github.com/pytorch/executorch/actions/runs/13278590926?pr=8399
H
Huy Do committed
1308d4d89da7d98a27e5ff3346ac14f66e6d8143
Parent: 012b5e9
Committed by GitHub <noreply@github.com>
on 2/12/2025, 5:27:37 PM