Lookup for libtorch.so and create buck2 target
Summary: `libtorch.so` is needed by custom ops as well as ATen mode. This diff adds a script to find the path of installed PyTorch directory in Python library and then look for `libtorch.so`. This script also copy header files into buck out so that they can be included by buck targets depending on the genrule. Note that this only works if user install pytorch properly. For example: ## libtorch.so My environment has `libtorch.so` installed in: ``` /opt/conda/envs/executorch/lib/python3.10/site-packages/torch/lib/libtorch.so ``` The script `link_torch.sh` creates a symbolic link in buck out: ``` lrwxrwxrwx 1 vscode vscode 77 Aug 10 07:43 /workspaces/executorch/buck-out/v2/gen/root/524f8da68ea2a374/third-party/__libtorch_gen__/out/libtorch.so -> /opt/conda/envs/executorch/lib/python3.10/site-packages/torch/lib/libtorch.so ``` Then we wrap it using a `prebuilt_cxx_library` macro. ## headers The script then go ahead and copy the headers under: ``` /opt/conda/envs/executorch/lib/python3.10/site-packages/torch/include ``` to buck-out: ``` /workspaces/executorch/buck-out/v2/gen/root/524f8da68ea2a374/third-party/__torch_headers_gen__/out ``` In the library definition we also need to add `-I<header_dir>` external preprocessor flags, so when we link `libtorch` we are also including those header directories. As a result, we will be able to link `//third-party:libtorch` to any C++ source file. We then are able to build `custom_ops_generated_lib` which registers custom ops using PyTorch C++ API. ## linker flag We need to add `-Wl,-rpath,$(location :libtorch_dir)` external linker flag, for any entity doing a `dlopen` to be able to find this shared library. Reviewed By: kimishpatel Differential Revision: D48221853 fbshipit-source-id: 0c7706c6f33f36d21212f965bd3c23aa9b2da0af
M
Mengwei Liu committed
015954e9bcb8dd8f636c8c0236781e61b6cedabd
Parent: 3b0533f
Committed by Facebook GitHub Bot <facebook-github-bot@users.noreply.github.com>
on 8/13/2023, 6:46:15 AM