Skip to content

Commit

Permalink
[cmake] Place export target file in the correct directory (#5677)
Browse files Browse the repository at this point in the history
## 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>
  • Loading branch information
ashay and jcwchen committed Oct 26, 2023
1 parent 49d84c7 commit 21bff4e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
18 changes: 17 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ else()
NO_DEFAULT_PATH)
find_package(Protobuf)
if (Protobuf_FOUND)
set(PROTOBUF_DIR "${_PROTOBUF_INSTALL_PREFIX}")
set(PROTOBUF_INCLUDE_DIR "${_PROTOBUF_INSTALL_PREFIX}/include")
set(Build_Protobuf OFF)
if ("${Protobuf_VERSION}" VERSION_GREATER_EQUAL "4.22.0")
# There are extra dependencies for protobuf.
Expand Down Expand Up @@ -265,6 +267,8 @@ else()
set(Protobuf_VERSION "4.22.3")
# Change back the BUILD_SHARED_LIBS to control the onnx project.
set(BUILD_SHARED_LIBS ${ONNX_BUILD_SHARED_LIBS})
set(PROTOBUF_DIR "${protobuf_BINARY_DIR}")
set(PROTOBUF_INCLUDE_DIR "${protobuf_SOURCE_DIR}/src")
endif()
message(STATUS "ONNX_PROTOC_EXECUTABLE: ${ONNX_PROTOC_EXECUTABLE}")
message(STATUS "Protobuf_VERSION: ${Protobuf_VERSION}")
Expand Down Expand Up @@ -717,7 +721,19 @@ install(FILES
${PROJECT_BINARY_DIR}/ONNXConfig.cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/ONNX
COMPONENT dev)
install(EXPORT ONNXTargets DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/ONNX")
install(EXPORT ONNXTargets
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/ONNX"
NAMESPACE ONNX::
)
if (NOT Build_Protobuf)
# If we fetched a Protobuf release (and didn't run its install step), then we
# don't have the export sets for protobuf and its dependencies, which
# prevents us from creating an export set for ONNXTargets.
export(EXPORT ONNXTargets
FILE "${PROJECT_BINARY_DIR}/ONNXTargets.cmake"
NAMESPACE ONNX::
)
endif()
install(TARGETS
onnx onnx_proto
EXPORT ONNXTargets DESTINATION ${CMAKE_INSTALL_LIBDIR})
Expand Down
4 changes: 4 additions & 0 deletions cmake/ONNXConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
# library version information
set(ONNX_VERSION "@ONNX_VERSION@")

list(APPEND CMAKE_PREFIX_PATH "@PROTOBUF_DIR@")
set(Protobuf_INCLUDE_DIR "@PROTOBUF_INCLUDE_DIR@")
find_package(Protobuf REQUIRED)

# import targets
include ("${CMAKE_CURRENT_LIST_DIR}/ONNXTargets.cmake")

Expand Down

0 comments on commit 21bff4e

Please sign in to comment.