Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[cmake] Place export target file in the correct directory (onnx#5677)
## Description Prior to this patch, the ONNXConfig.cmake script (generated from the ONNXConfig.cmake.in file) tries to read the export target file (ONNXTargets.cmake) from the same directory where ONNXConfig.cmake is located, but the two files are not in the same directory. Specifically, CMake places the export target file (ONNXTargets.cmake) in a directory whose name contains the build hash, whereas the ONNXConfig.cmake script is placed in the ${PROJECT_BINARY_DIR}/ONNXConfig.cmake, as specified in the top-level CMakeLists.txt directory. This patch reconciles the difference by explicitly instructing CMake to place the export target file (ONNXTargets.cmake) in the same directory as the ONNXConfig.cmake script during the build step. This patch also makes two ancillary changes. First, it adds the `ONNX::` namespace to the target names, so that targets built by this project don't interfere with targets built by other projects. Second, this patch adds Protobuf to the list of packages that consumers of ONNX must have available, since protobuf libraries are linked publicly while building ONNX. <!-- copilot:all --> ### <samp>🤖 Generated by Copilot at 5173943</samp> ### Summary 🛠️🚀🌐 <!-- 1. 🛠️ - This emoji represents the addition of the commands to the `cmake/ONNXConfig.cmake.in` file, which are tools for improving the ONNX CMake configuration and making the Protobuf library findable. 2. 🚀 - This emoji represents the improvement of the ONNX CMake configuration by setting the Protobuf variables and adding the namespace option, which are enhancements that make the ONNX project more robust and portable. 3. 🌐 - This emoji represents the conditional export command for the Protobuf library, which is a feature that makes the ONNX project more compatible with different platforms and environments. --> This pull request enhances the ONNX CMake configuration to make the Protobuf library more accessible and consistent for the ONNX library and its users. It modifies the `CMakeLists.txt` and `cmake/ONNXConfig.cmake.in` files to set and export the relevant Protobuf variables and commands. > _Sing, O Muse, of the valiant ONNX project, the source of many models_ > _That with `CMake` configures its code, and to other projects unfolds_ > _Its mighty `Protobuf` dependency, that it finds and exports with care_ > _Like Zeus who wields his thunderbolt, and his power with the gods shares_ ### Walkthrough * Add `find_package(Protobuf REQUIRED)` command and set `CMAKE_PREFIX_PATH` and `Protobuf_INCLUDE_DIR` variables to make Protobuf library findable by ONNXConfig.cmake file ([link](https://github.com/onnx/onnx/pull/5677/files?diff=unified&w=0#diff-67d7a4f3d6c006f4fd85472c38c422e6f1ad75e026c69c3657c6ca1ec29e6673R9-R12)) * Set `PROTOBUF_DIR` and `PROTOBUF_INCLUDE_DIR` variables to Protobuf installation prefix or binary and source directories depending on whether Protobuf was fetched and built by ONNX or not ([link](https://github.com/onnx/onnx/pull/5677/files?diff=unified&w=0#diff-1e7de1ae2d059d21e1dd75d5812d5a34b0222cef273b7c3a2af62eb747f9d20aR179-R180), [link](https://github.com/onnx/onnx/pull/5677/files?diff=unified&w=0#diff-1e7de1ae2d059d21e1dd75d5812d5a34b0222cef273b7c3a2af62eb747f9d20aR270-R271)) * Add `NAMESPACE ONNX::` option to `install(EXPORT ONNXTargets ...)` and `export(EXPORT ONNXTargets ...)` commands to follow CMake best practices and avoid name clashes ([link](https://github.com/onnx/onnx/pull/5677/files?diff=unified&w=0#diff-1e7de1ae2d059d21e1dd75d5812d5a34b0222cef273b7c3a2af62eb747f9d20aL720-R736)) * Add conditional `export(EXPORT ONNXTargets ...)` command to workaround Protobuf export issue when it is built as part of ONNX ([link](https://github.com/onnx/onnx/pull/5677/files?diff=unified&w=0#diff-1e7de1ae2d059d21e1dd75d5812d5a34b0222cef273b7c3a2af62eb747f9d20aL720-R736)) Signed-off-by: Ashay Rane <[email protected]> Co-authored-by: Chun-Wei Chen <[email protected]>
- Loading branch information