Skip to content

Commit

Permalink
Support cross-compiling in the libmagic vcpkg port overlay. (#5333)
Browse files Browse the repository at this point in the history
Fixes TileDB-Inc/conda-forge-nightly-controller#145.

This PR adds support for cross-compiling our custom vcpkg port overlay
for libmagic. This is accomplished by installing libmagic as a host
dependency, and using the host's `file.exe` to build `magic.mgc` in
cross-compiled builds.

Validated locally by successfully cross compiling the Core for
Windows-ARM64 (with cloud storage and serialization disabled because
these dependencies are not yet compatible).

---
TYPE: BUILD
DESC: Fixed cross-compiling support in the libmagic vcpkg port overlay.
  • Loading branch information
teo-tsirpanis authored Oct 3, 2024
1 parent c44fb49 commit f47efff
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
3 changes: 2 additions & 1 deletion ports/libmagic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ unset(CONFIGURE_AC_CONTENT)
project(file VERSION ${CMAKE_MATCH_1})

option(FILE_TESTS "Enable file tests" OFF)
set(FILE_COMMAND "file" CACHE PATH "Path to the file command to generate magic.mgc. Should be set for cross-compiling scenarios.")

# Get library directory for multiarch linux distros
include(GNUInstallDirs)
Expand Down Expand Up @@ -133,7 +134,7 @@ foreach(MAGIC_FRAGMENT ${MAGIC_FRAGMENTS})
endforeach()

add_custom_command(OUTPUT magic.mgc
COMMAND file -C -m magic
COMMAND ${FILE_COMMAND} -C -m magic
COMMENT "Compiling magic file"
)

Expand Down
16 changes: 9 additions & 7 deletions ports/libmagic/portfile.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,19 @@ 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)
set(FILE_COMMAND_OPT "-DFILE_COMMAND=${CURRENT_HOST_INSTALLED_DIR}/tools/libmagic/file${VCPKG_HOST_EXECUTABLE_SUFFIX}")
elseif(VCPKG_TARGET_IS_WINDOWS AND VCPKG_LIBRARY_LINKAGE STREQUAL dynamic)
set(EXTRA_ARGS "ADD_BIN_TO_PATH")
endif()

vcpkg_cmake_configure(
SOURCE_PATH ${SOURCE_PATH}
OPTIONS
${FILE_COMMAND_OPT}
)

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 +55,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 @@ -5,6 +5,10 @@
"description": "This library can be used to classify files according to magic number tests.",
"homepage": "https://github.com/file/file",
"dependencies": [
{
"name": "libmagic",
"host": true
},
{
"name": "vcpkg-cmake",
"host": true
Expand Down

0 comments on commit f47efff

Please sign in to comment.