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

Support cross-compiling in the libmagic vcpkg port overlay, attempt 2. #5338

Merged
merged 1 commit into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions ports/libmagic/portfile.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,17 @@ file(COPY "${CMAKE_CURRENT_LIST_DIR}/unofficial-libmagic-config.cmake.in" DESTIN
file(COPY "${CMAKE_CURRENT_LIST_DIR}/magic.def" DESTINATION "${SOURCE_PATH}/src")
file(COPY "${CMAKE_CURRENT_LIST_DIR}/config.h" DESTINATION "${SOURCE_PATH}/src")

if(VCPKG_CROSSCOMPILING)
vcpkg_add_to_path(PREPEND "${CURRENT_HOST_INSTALLED_DIR}/tools/libmagic")
elseif(VCPKG_TARGET_IS_WINDOWS AND VCPKG_LIBRARY_LINKAGE STREQUAL dynamic)
set(EXTRA_ARGS "ADD_BIN_TO_PATH")
endif()
Comment on lines +34 to +38
Copy link
Member Author

Choose a reason for hiding this comment

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

We move this block before the configure and install steps, so that libmagic's build system can find the file command in the path we built it for the host.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Well, not quite sure what this does, but have you tested this with cross compilation? (If it works for x86_64 and crosscompilation x86_64->aarch64)

Copy link
Member Author

Choose a reason for hiding this comment

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

It's tested as outlined in #5338 (comment).

As for what this does, in libmagic's CMakeLists.txt we have this command:

add_custom_command(OUTPUT magic.mgc
  COMMAND file -C -m magic
  COMMENT "Compiling magic file"
)

When not cross-compiling, CMake resolves file to the file executable we built, but when cross-compiling file will be passed to the shell as-is and resolved from PATH. The vcpkg_add_to_path command ensures the host's file we built gets found.

The elseif part makes sure libmagic.dll gets found on Windows if we are using dynamic linkage for the dependencies (which we don't currently do).

This block exists in the upstream port BTW.

https://github.com/microsoft/vcpkg/blob/b8d688b979bd54f0269ea1c6101e6a977613175f/ports/libmagic/portfile.cmake#L61-L67

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks a lot for the explanation!


vcpkg_cmake_configure(
SOURCE_PATH ${SOURCE_PATH}
)

vcpkg_cmake_install()
vcpkg_cmake_install(${EXTRA_ARGS})
vcpkg_copy_pdbs()
vcpkg_fixup_pkgconfig()
vcpkg_copy_tools(TOOL_NAMES file AUTO_CLEAN)
Expand All @@ -47,12 +53,6 @@ file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/debug/include")
file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/debug/share")
file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/share/${PORT}/man5")

if(VCPKG_CROSSCOMPILING)
vcpkg_add_to_path(PREPEND "${CURRENT_HOST_INSTALLED_DIR}/tools/libmagic/bin")
elseif(VCPKG_TARGET_IS_WINDOWS AND VCPKG_LIBRARY_LINKAGE STREQUAL dynamic)
set(EXTRA_ARGS "ADD_BIN_TO_PATH")
endif()

if(VCPKG_LIBRARY_LINKAGE STREQUAL static)
file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/bin" "${CURRENT_PACKAGES_DIR}/debug/bin")
endif()
Expand Down
4 changes: 4 additions & 0 deletions ports/libmagic/vcpkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
"name": "vcpkg-cmake-config",
"host": true
},
{
"name": "libmagic",
"host": true
},
{
"name": "dirent",
"platform": "windows"
Expand Down
Loading