Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cmake] Place export target file in the correct directory #5677

Merged
merged 3 commits into from Oct 26, 2023
Merged

[cmake] Place export target file in the correct directory #5677

merged 3 commits into from Oct 26, 2023

Conversation

ashay
Copy link
Contributor

@ashay ashay commented Oct 16, 2023

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.

🤖 Generated by Copilot at 5173943

Summary

🛠️🚀🌐

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)
  • 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, link)
  • Add NAMESPACE ONNX:: option to install(EXPORT ONNXTargets ...) and export(EXPORT ONNXTargets ...) commands to follow CMake best practices and avoid name clashes (link)
  • Add conditional export(EXPORT ONNXTargets ...) command to workaround Protobuf export issue when it is built as part of ONNX (link)

@ashay ashay requested a review from a team as a code owner October 16, 2023 17:16
@codecov
Copy link

codecov bot commented Oct 16, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

📢 Thoughts on this report? Let us know!.

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.

Signed-off-by: Ashay Rane <ashay@users.noreply.github.com>
@jcwchen jcwchen added the run release CIs Use this label to trigger release tests in CI label Oct 17, 2023
Copy link
Member

@jcwchen jcwchen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please ignore the failure from Windows release CI, which will be fixed by #5678.

@justinchuby justinchuby added the build Issues related to ONNX builds and packages label Oct 17, 2023
@justinchuby justinchuby added this pull request to the merge queue Oct 26, 2023
Merged via the queue into onnx:main with commit 21bff4e Oct 26, 2023
56 checks passed
@ashay ashay deleted the ashay/cmake-export-filename branch October 26, 2023 15:13
isdanni pushed a commit to isdanni/onnx that referenced this pull request Nov 2, 2023
## 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 <ashay@users.noreply.github.com>
Co-authored-by: Chun-Wei Chen <jacky82226@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
build Issues related to ONNX builds and packages run release CIs Use this label to trigger release tests in CI
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

None yet

3 participants