Skip to content

fix: rewrite cmake and pkg-config paths after Linux tarball reshape - #28147

Open
Rishi-Dave wants to merge 2 commits into
microsoft:mainfrom
Rishi-Dave:rishidave/fix/cmake-tarball-lib64-path
Open

fix: rewrite cmake and pkg-config paths after Linux tarball reshape#28147
Rishi-Dave wants to merge 2 commits into
microsoft:mainfrom
Rishi-Dave:rishidave/fix/cmake-tarball-lib64-path

Conversation

@Rishi-Dave

Copy link
Copy Markdown
Contributor

Summary

  • Patch stale lib64/ and include/onnxruntime/ paths in the generated cmake export and pkg-config files after the Linux tarball packaging script reshapes the directory layout.
  • Makes find_package(onnxruntime CONFIG) and pkg-config --libs onnxruntime work against the released Linux tarball.

Motivation

Fixes #23642.

On x86-64 Linux, GNUInstallDirs sets CMAKE_INSTALL_LIBDIR=lib64 and the build installs headers under include/onnxruntime/. The packaging script tools/ci_build/github/linux/copy_strip_binary.sh then reshapes the tarball:

  • mv $ARTIFACT_NAME/lib64 $ARTIFACT_NAME/lib
  • mv $ARTIFACT_NAME/include/onnxruntime/* $ARTIFACT_NAME/include && rmdir $ARTIFACT_NAME/include/onnxruntime

The files produced by install(EXPORT onnxruntimeTargets ...) (onnxruntimeTargets-release.cmake, etc.) and configure_file(libonnxruntime.pc.cmake.in ...) still reference the pre-reshape locations, so downstream consumers see:

  • IMPORTED_LOCATION_RELEASE "${_IMPORT_PREFIX}/lib64/libonnxruntime.so.X" — file does not exist
  • INTERFACE_INCLUDE_DIRECTORIES "${_IMPORT_PREFIX}/include/onnxruntime" — directory does not exist
  • libdir=${prefix}/lib64 / includedir=${prefix}/include/onnxruntime in libonnxruntime.pc

An earlier attempt (#26104) tried to eliminate the reshape itself and was reverted (#26767) because downstream release pipelines and example repos depended on the lib/ + flat include/ layout. This change takes the opposite approach: keep the existing tarball layout and fix the metadata to match it — fully self-contained in the packaging step.

Changes

tools/ci_build/github/linux/copy_strip_binary.sh — after the existing mv lib64 lib step, add two guarded sed blocks:

  • Rewrite /lib64//lib/ and /include/onnxruntime"/include" in every *.cmake under $ARTIFACT_NAME/lib/cmake (covers onnxruntimeConfig.cmake, onnxruntimeTargets.cmake, and per-config onnxruntimeTargets-*.cmake).
  • Rewrite libdir and includedir entries in $ARTIFACT_NAME/lib/pkgconfig/libonnxruntime.pc.

Patterns are anchored (trailing /, trailing ", or end-of-line) to avoid over-matching. Guarded with [ -d ] / [ -f ] so builds that don't install cmake exports or pkg-config files are unaffected. No cmake build-system changes; no effect on the macOS branch.

Test Plan

  • bash -n tools/ci_build/github/linux/copy_strip_binary.sh — syntax clean.
  • Mock tarball exercised locally: populated lib/cmake/onnxruntime/onnxruntimeTargets-release.cmake with ${_IMPORT_PREFIX}/lib64/libonnxruntime.so.X and ${_IMPORT_PREFIX}/include/onnxruntime, and lib/pkgconfig/libonnxruntime.pc with libdir=${prefix}/lib64 / includedir=${prefix}/include/onnxruntime. After running the new block, grep -r 'lib64\|include/onnxruntime' lib/ returns no matches; the rewritten IMPORTED_LOCATION_RELEASE and libdir point at the real, reshaped locations.
  • CI Linux packaging pipelines will exercise the end-to-end tarball consumption.

The Linux release packaging script `copy_strip_binary.sh` renames
`lib64/` to `lib/` and flattens `include/onnxruntime/*` into `include/`
after the cmake install step. However the files generated by
`install(EXPORT ...)` (`onnxruntimeTargets-release.cmake` etc.) and the
pkg-config file (`libonnxruntime.pc`) still contain the pre-reshape
paths, causing `find_package(onnxruntime CONFIG)` and `pkg-config
--libs onnxruntime` to fail for tarball consumers.

Patch the stale paths in place after the reshape steps using narrow
`sed` substitutions, guarded by existence checks so builds without
pkg-config or cmake exports are unaffected.

Fixes microsoft#23642

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR updates the Linux packaging reshape step so that the generated CMake package export files and pkg-config metadata match the post-reshape tarball layout (lib64 → lib and include/onnxruntime → include), restoring downstream usability of find_package(onnxruntime CONFIG) and pkg-config --libs onnxruntime.

Changes:

  • After reshaping lib64/ to lib/, rewrites stale /lib64/ and /include/onnxruntime references in installed *.cmake files under lib/cmake.
  • Rewrites libdir/includedir in lib/pkgconfig/libonnxruntime.pc to match the reshaped layout.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +46 to +58
# Fix stale paths baked into cmake config files by `install(EXPORT ...)`
# - lib64/ was renamed to lib/ above
# - include/onnxruntime/ was flattened into include/ above
if [ -d "$ARTIFACT_NAME/lib/cmake" ]; then
find "$ARTIFACT_NAME/lib/cmake" -type f -name "*.cmake" -exec \
sed -i -e 's|/lib64/|/lib/|g' -e 's|/include/onnxruntime"|/include"|g' {} +
fi
# Fix pkg-config file similarly
if [ -f "$ARTIFACT_NAME/lib/pkgconfig/libonnxruntime.pc" ]; then
sed -i -e 's|/lib64$|/lib|' -e 's|/lib64/|/lib/|g' \
-e 's|/include/onnxruntime$|/include|' -e 's|/include/onnxruntime/|/include/|g' \
"$ARTIFACT_NAME/lib/pkgconfig/libonnxruntime.pc"
fi
The cmake include-path rewrite (include/onnxruntime -> include) was
previously only applied in the Linux branch, but macOS tarballs also
flatten the include directory at line 21 and install cmake config files
under lib/cmake/. Hoist the cmake include-path rewrite to run after the
branch so it covers both Linux and macOS.

Additionally, replace `sed -i` with the portable `sed -i.bak ... && rm
*.bak` pattern throughout, since BSD sed (macOS) requires an explicit
backup extension argument while GNU sed (Linux) accepts an empty string.
The `-exec sh -c '...' _ {} \;` form processes one file at a time,
which is compatible with both implementations.
@Rishi-Dave

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Addressed both points in 1a1a32d:

  • Hoisted the include/onnxruntime"include" rewrite for *.cmake files out of the Linux branch so it now runs on both tarballs. The include/ flatten on line 21 is unconditional, so macOS needed the matching cmake-config fix-up too. The lib64lib rewrites stay Linux-only (only the Linux tarball hits the lib64 rename).
  • Switched all in-place edits to sed -i.bak '...' file && rm file.bak, which is the one sed -i form that behaves identically under GNU sed and BSD sed. No more relying on GNU's -i with no suffix.
  • .pc rewrite is still Linux-only since the dylib branch doesn't ship a pkg-config file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Build] Inconsistent naming of lib directories

2 participants