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

can't cross build x86_64 mpv on a arm64 macOS #15562

Open
5 tasks done
eko5624 opened this issue Dec 23, 2024 · 14 comments
Open
5 tasks done

can't cross build x86_64 mpv on a arm64 macOS #15562

eko5624 opened this issue Dec 23, 2024 · 14 comments

Comments

@eko5624
Copy link

eko5624 commented Dec 23, 2024

Important Information

- mpv version: git master
- Platform version: arm64 mac
- Meson version: latest

Reproduction Steps

My repo: https://github.com/eko5624/mpv-mac/tree/cross

failed log:
https://github.com/eko5624/mpv-mac/actions/runs/12462323821/job/34787841907

sucessed log:
https://github.com/eko5624/mpv-mac/actions/runs/12464492354/job/34788650939

workaround:

cd mpv
ln -s xx/include/libplacebo libplacebo
ln -s xx/include/libavcodec libavcodec
ln -s xx/include/libavdevice libavdevice
ln -s xx/include/libavfilter libavfilter
ln -s xx/include/libavformat libavformat
ln -s xx/include/libavutil libavutil
ln -s xx/include/libpostproc libpostproc
ln -s xx/include/libswresample libswresample
ln -s xx/include/libswscale libswscale
ln -s xx/include/vulkan vulkan
ln -s xx/include/vk_video vk_video

Expected Behavior

build without workaround

Actual Behavior

This is my env setting.

build env:

#!/hint/bash

DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
TOOLS="$DIR/tools"
PACKAGES="$DIR/packages"
WORKSPACE="$DIR/workspace"
mkdir -p "$TOOLS"
mkdir -p "$PACKAGES"
mkdir -p "$WORKSPACE"
PATH="${TOOLS}/bin:$PATH"
 
if [ "$ARCHS" == "x86_64" ]; then
  ARCH="x86_64"
  if [ "$(uname -m)" != "x86_64" ]; then
    BUILD_HOST="--host=x86_64-apple-darwin --target=x86_64-apple-macos11.0"
  else
    BUILD_HOST=""  
  fi  
elif [ "$ARCHS" == "arm64" ]; then
  ARCH="aarch64"
  if [ "$(uname -m)" != "arm64" ]; then
    BUILD_HOST="--host=aarch64-apple-darwin --target=arm64-apple-macos11.0"
    X265_CMAKEFLAGS="-DENABLE_NEON=OFF" #fix cross build x265 arm64 library on x86_64 macOS
  else  
    BUILD_HOST=""
  fi
fi

CC="clang"
CXX="clang++"
CPP="clang -E"
CXXCPP="clang++ -E"
MACOSX_TARGET="11.0"
MACOSX_DEPLOYMENT_TARGET="11.0"
SWIFT_FLAGS="-target $ARCHS-apple-macosx11.0"
SDKROOT="/Applications/Xcode_15.2.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk"
XCTOOLCHAIN="/Applications/Xcode_15.2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/"
CFLAGS="-arch $ARCHS -target $ARCHS-apple-macos11.0 -isysroot $SDKROOT -mmacosx-version-min=11.0 -I$WORKSPACE/include -Wno-int-conversion"
LDFLAGS="-arch $ARCHS -target $ARCHS-apple-macos11.0 -isysroot $SDKROOT -mmacosx-version-min=11.0 -L$WORKSPACE/lib -Wl,-ld_classic -framework CoreFoundation"
CXXFLAGS="$CFLAGS"
CPPFLAGS="$CFLAGS"
EXTRALIBS="-ldl -lpthread -lm -lz -lc++"

PKG_CONFIG="pkgconf --static"
PKG_CONFIG_PATH="$WORKSPACE/lib/pkgconfig"

LIBRARY_PATH="$WORKSPACE/lib"
C_INCLUDE_PATH="$WORKSPACE/include"
CPLUS_INCLUDE_PATH="${C_INCLUDE_PATH}"

CURL_RETRIES="--connect-timeout 60 --retry 5 --retry-delay 5"

# Speed up the process
# Env Var NUMJOBS overrides automatic detection
if [[ -n "$NUMJOBS" ]]; then
  MJOBS="$NUMJOBS"
elif [[ -f /proc/cpuinfo ]]; then
  MJOBS=$(grep -c processor /proc/cpuinfo)
elif [[ "$OSTYPE" == "darwin"* ]]; then
  MJOBS=$(sysctl -n machdep.cpu.thread_count)
  MACOS_LIBTOOL="$(which libtool)" # gnu libtool is installed in this script and need to avoid name conflict
else
  MJOBS=3
fi

messon_cross_x86_64.txt

[constants]
sdkroot = '/Applications/Xcode_15.2.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk'

[host_machine]
system = 'darwin'
cpu_family = 'x86_64'
cpu = 'x86_64'
endian = 'little'

[binaries]
c = ['clang']
cpp = ['clang++']
objc = ['clang']
objcpp = ['clang++']
nm = ['nm']
ar = ['ar']
strings = ['strings']
ranlib = ['ranlib']
strip = ['strip']
pkg-config = ['pkgconf']

[built-in options]
c_args = ['-arch', 'x86_64', '-isysroot', sdkroot, '-mmacosx-version-min=11.0']
cpp_args = ['-arch', 'x86_64', '-isysroot', sdkroot, '-mmacosx-version-min=11.0', '-stdlib=libc++']
objc_args = ['-arch', 'x86_64', '-isysroot', sdkroot, '-mmacosx-version-min=11.0']
objcpp_args = ['-arch', 'x86_64', '-isysroot', sdkroot, '-mmacosx-version-min=11.0', '-stdlib=libc++']
c_link_args = ['-arch', 'x86_64', '-isysroot', sdkroot, '-mmacosx-version-min=11.0']
cpp_link_args = ['-arch', 'x86_64', '-isysroot', sdkroot, '-mmacosx-version-min=11.0']
objc_link_args = ['-arch', 'x86_64', '-isysroot', sdkroot, '-mmacosx-version-min=11.0']
objcpp_link_args = ['-arch', 'x86_64', '-isysroot', sdkroot, '-mmacosx-version-min=11.0']

mpv.sh

#!/bin/bash
set -e

cd "$(dirname "$0")" && cd ..
set -a; source build.env; source ver.sh; set +a

cd $PACKAGES
git clone https://github.com/mpv-player/mpv.git

export CFLAGS="$CFLAGS -Wno-error=deprecated -Wno-error=deprecated-declarations"
export LDFLAGS="$LDFLAGS -Wl,-no_compact_unwind"
cd mpv
#git reset --hard 23843b4aa594dc8c885575f3d237cde3c29398a2
#export TOOLCHAINS=$(/usr/libexec/PlistBuddy -c "Print :CFBundleIdentifier" /Library/Developer/Toolchains/swift-latest.xctoolchain/Info.plist)
if [[ ("$(uname -m)" == "arm64") && ("$ARCHS" == "x86_64") ]] || [[ ("$(uname -m)" == "x86_64") && ("$ARCHS" == "arm64") ]]; then
  ln -s $WORKSPACE/include/libplacebo libplacebo
  ln -s $WORKSPACE/include/libavcodec libavcodec
  ln -s $WORKSPACE/include/libavdevice libavdevice
  ln -s $WORKSPACE/include/libavfilter libavfilter
  ln -s $WORKSPACE/include/libavformat libavformat
  ln -s $WORKSPACE/include/libavutil libavutil
  ln -s $WORKSPACE/include/libpostproc libpostproc
  ln -s $WORKSPACE/include/libswresample libswresample
  ln -s $WORKSPACE/include/libswscale libswscale
  ln -s $WORKSPACE/include/vulkan vulkan
  ln -s $WORKSPACE/include/vk_video vk_video
fi  
meson setup build \
  --buildtype=release \
  --cross-file="$DIR/meson_$ARCHS.txt" \
  -Dwrap_mode=nodownload \
  -Db_lto=true \
  -Db_lto_mode=thin \
  -Dobjc_args="-Wno-error=deprecated -Wno-error=deprecated-declarations" \
  -Dlibmpv=true \
  -Dvulkan=enabled \
  -Diconv=enabled \
  -Dmanpage-build=disabled \
  -Dswift-flags="${SWIFT_FLAGS}"
meson compile -C build
#meson compile -C build macos-bundle

# get latest commit sha
short_sha=$(git rev-parse --short HEAD)
echo $short_sha > build/SHORT_SHA

# bundle mpv
cp -r TOOLS/osxbundle/mpv.app build
cp build/mpv build/mpv.app/Contents/MacOS
cp $WORKSPACE/lib/libluajit-5.1.2.dylib build/mpv.app/Contents/MacOS/lib
#cp $WORKSPACE/lib/libvapoursynth-script.0.dylib build/mpv.app/Contents/MacOS/lib
mkdir -p build/mpv.app/Contents/Frameworks
mkdir -p build/mpv.app/Contents/Resources/vulkan/icd.d
cp $WORKSPACE/lib/libMoltenVK.dylib build/mpv.app/Contents/Frameworks
cp $WORKSPACE/share/vulkan/icd.d/MoltenVK_icd.json build/mpv.app/Contents/Resources/vulkan/icd.d
sed -i "" 's|../../../lib/libMoltenVK.dylib|../../../Frameworks/libMoltenVK.dylib|g' build/mpv.app/Contents/Resources/vulkan/icd.d/MoltenVK_icd.json

mpv_deps=($(otool -L $PACKAGES/mpv/build/mpv.app/Contents/MacOS/mpv | grep -e '\t' | grep -Ev "\/usr\/lib|\/System|@rpath" | awk '{ print $1 }'))
for f in "${mpv_deps[@]}"; do
  sudo install_name_tool -id "@executable_path/lib/$(basename $f)" "build/mpv.app/Contents/MacOS/lib/$(basename $f)"
  sudo install_name_tool -change "$f" "@executable_path/lib/$(basename $f)" build/mpv.app/Contents/MacOS/mpv
done

# setting rpath
rpaths=($(otool -l build/mpv.app/Contents/MacOS/mpv | grep -A2 LC_RPATH | grep path | awk '{ print $2 }'))
for f in "${rpaths[@]}"; do
  sudo install_name_tool -delete_rpath $f build/mpv.app/Contents/MacOS/mpv
done
sudo install_name_tool -add_rpath @executable_path/lib build/mpv.app/Contents/MacOS/mpv


# Codesign mpv.app
codesign --deep -fs - build/mpv.app

cd $DIR
# Zip mpv.app and mpv config files
mkdir mpv
git clone https://github.com/eko5624/mpv-config.git
mv mpv-config/macos_config mpv
cp -r $PACKAGES/mpv/build/mpv.app mpv
cp $PACKAGES/mpv/build/SHORT_SHA mpv
zip -r mpv-$ARCHS-git-$short_sha.zip mpv/*

# Zip libmpv
mkdir -p libmpv/include
cp $PACKAGES/mpv/build/libmpv.2.dylib libmpv
cp $PACKAGES/mpv/build/mpv.app/Contents/MacOS/lib/*.dylib libmpv
cp $PACKAGES/mpv/libmpv/client.h libmpv/include
cp $PACKAGES/mpv/libmpv/stream_cb.h libmpv/include
cp $PACKAGES/mpv/libmpv/render.h libmpv/include
cp $PACKAGES/mpv/libmpv/render_gl.h libmpv/include
zip -r libmpv-$ARCHS-$short_sha.zip libmpv/*

# Zip ffmpeg
mkdir ffmpeg
cp $WORKSPACE/bin/ffmpeg ffmpeg
mv $WORKSPACE/SHORT_SHA ffmpeg
ffmpeg_sha=$(cat ffmpeg/SHORT_SHA)
zip -r ffmpeg-$ARCHS-$ffmpeg_sha.zip ffmpeg/*

Log File

logs_32369201623.zip

I carefully read all instruction and confirm that I did the following:

  • I tested with the latest mpv version to validate that the issue is not already fixed.
  • I provided all required information including system and mpv version.
  • I produced the log file with the exact same set of parameters, and conditions used in "Reproduction Steps".
  • I produced the log file while the behaviors described in "Actual Behavior" were actively observed.
  • I attached the full, untruncated log file.
@Akemi
Copy link
Member

Akemi commented Dec 23, 2024

which version/commit broke this?

just a guess but this might not work properly? what does libplacebo.get_variable('includedir', default_value: source_root / 'subprojects' / 'libplacebo' / 'src' / 'include') output? as another workaround you could try passing -I/PATH/TO/LIBPLACEBO/INCLUDES manually to the swift flags like the target.

@eko5624
Copy link
Author

eko5624 commented Dec 23, 2024

as another workaround you could try passing -I/PATH/TO/LIBPLACEBO/INCLUDES manually to the swift flags like the target.

This doesn't work either. Here's the log:
https://github.com/eko5624/mpv-mac/actions/runs/12466282314/job/34793527535

@kasper93
Copy link
Contributor

../osdep/mac/presentation.swift:22:59: error: cannot find 'mp_raw_time_ns_from_mach' in scope

This implies that HAVE_DARWIN is not defined for .switft build. Wouldn't this be fixed by #15557 ? Not sure how it works without currently, though.

@Akemi
Copy link
Member

Akemi commented Dec 23, 2024

This implies that HAVE_DARWIN is not defined for .switft build. Wouldn't this be fixed by #15557 ? Not sure how it works without currently, though.

because HAVE_DARWIN is not necessary/used in swift, so that PR won't change anything. it's only used in the c parts of our code.

@kasper93
Copy link
Contributor

kasper93 commented Dec 23, 2024

This implies that HAVE_DARWIN is not defined for .switft build. Wouldn't this be fixed by #15557 ? Not sure how it works without currently, though.

because HAVE_DARWIN is not necessary/used in swift, so that PR won't change anything. it's only used in the c parts of our code.

presentation.swift references mp_raw_time_ns_from_mach, which is guarded behind HAVE_DARWIN in timer.h and not declared in any other way. So the error: cannot find 'mp_raw_time_ns_from_mach' in scope seems like a reasonable failure. (unless it is generated in build time and this part is failing for some reason)

@Akemi
Copy link
Member

Akemi commented Dec 23, 2024

but that is a compile time guard of c and not swift. those -D flags are there because swift can't just include our config.h (it doesn't import those #defines as we need them). the c parts swift is including are still handled by config.h includes.

eg time.h includes config.h.

@Akemi
Copy link
Member

Akemi commented Dec 23, 2024

/Applications/Xcode_15.2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc -c -emit-library -static -sdk /Applications/Xcode_15.2.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.2.sdk -emit-objc-header -parse-as-library -O -D HAVE_MACOS_10_15_4_FEATURES -D HAVE_MACOS_11_FEATURES -D HAVE_MACOS_12_FEATURES -D HAVE_MACOS_COCOA_CB -D HAVE_MACOS_TOUCHBAR -D HAVE_MACOS_MEDIA_PLAYER -target x86_64-apple-macosx11.0 -I/Users/runner/work/mpv-mac/mpv-mac/workspace/include/libplacebo -module-name swift -emit-module-path osdep/mac/swift.swiftmodule -import-objc-header /Users/runner/work/mpv-mac/mpv-mac/packages/mpv/osdep/mac/app_bridge_objc.h -emit-objc-header-path osdep/mac/swift.h -o osdep/mac/swift.o ../osdep/mac/application.swift ../osdep/mac/app_hub.swift ../osdep/mac/event_helper.swift ../osdep/mac/input_helper.swift ../osdep/mac/libmpv_helper.swift ../osdep/mac/log_helper.swift ../osdep/mac/menu_bar.swift ../osdep/mac/option_helper.swift ../osdep/mac/precise_timer.swift ../osdep/mac/presentation.swift ../osdep/mac/swift_compat.swift ../osdep/mac/swift_extensions.swift ../osdep/mac/type_helper.swift ../video/out/mac/common.swift ../video/out/mac/title_bar.swift ../video/out/mac/view.swift ../video/out/mac/window.swift ../video/out/cocoa_cb_common.swift ../video/out/mac/gl_layer.swift ../video/out/mac_common.swift ../video/out/mac/metal_layer.swift ../osdep/mac/remote_command_center.swift ../osdep/mac/touch_bar.swift -I. -I/Users/runner/work/mpv-mac/mpv-mac/packages/mpv -I/Users/runner/work/mpv-mac/mpv-mac/workspace/include -I/Users/runner/work/mpv-mac/mpv-mac/workspace/include

just a copy of the build call.
the include paths from our declarations:
-I.
-I/Users/runner/work/mpv-mac/mpv-mac/packages/mpv (source_root)
-I/Users/runner/work/mpv-mac/mpv-mac/workspace/include (libplacebo)
-I/Users/runner/work/mpv-mac/mpv-mac/workspace/include (libavutil)

corresponding build declarations:
-I.
-I + source_root
-I + libplacebo.get_variable('includedir', default_value: source_root / 'subprojects' / 'libplacebo' / 'src' / 'include')
-I + libavutil.get_variable('includedir')

your added include path:
-I/Users/runner/work/mpv-mac/mpv-mac/workspace/include/libplacebo

your included seems to be one level too much. though /Users/runner/work/mpv-mac/mpv-mac/workspace/include is already defined properly.

the new error and the old might just hide the underlying problem.

look at your 'success' log, it's riddled with linking errors. i think something isn't correct yet on your end:

ld: warning: ignoring file libmpv.2.dylib.p/osdep_path-mac.m.o, lto file was built for unknown bitcode architecture which is not the architecture being linked (x86_64): libmpv.2.dylib.p/osdep_path-mac.m.o
ld: warning: ignoring file libmpv.2.dylib.p/osdep_mac_app_bridge.m.o, lto file was built for unknown bitcode architecture which is not the architecture being linked (x86_64): libmpv.2.dylib.p/osdep_mac_app_bridge.m.o
ld: warning: ignoring file libmpv.2.dylib.p/audio_out_ao_avfoundation.m.o, lto file was built for unknown bitcode architecture which is not the architecture being linked (x86_64): libmpv.2.dylib.p/audio_out_ao_avfoundation.m.o
ld: warning: ignoring file libmpv.2.dylib.p/video_out_hwdec_hwdec_vt_pl.m.o, lto file was built for unknown bitcode architecture which is not the architecture being linked (x86_64): libmpv.2.dylib.p/video_out_hwdec_hwdec_vt_pl.m.o
ld: warning: ignoring file libmpv.2.dylib.p/video_out_vulkan_context_mac.m.o, lto file was built for unknown bitcode architecture which is not the architecture being linked (x86_64): libmpv.2.dylib.p/video_out_vulkan_context_mac.m.o
ld: warning: ignoring file /Users/runner/work/mpv-mac/mpv-mac/workspace/lib/libmujs.a, building for macOS-x86_64 but attempting to link with file built for macOS-arm64
[249/249] Linking target mpv
ld: warning: ignoring file libmpv.2.dylib.p/osdep_path-mac.m.o, lto file was built for unknown bitcode architecture which is not the architecture being linked (x86_64): libmpv.2.dylib.p/osdep_path-mac.m.o
ld: warning: ignoring file libmpv.2.dylib.p/audio_out_ao_avfoundation.m.o, lto file was built for unknown bitcode architecture which is not the architecture being linked (x86_64): libmpv.2.dylib.p/audio_out_ao_avfoundation.m.o
ld: warning: ignoring file libmpv.2.dylib.p/osdep_mac_app_bridge.m.o, lto file was built for unknown bitcode architecture which is not the architecture being linked (x86_64): libmpv.2.dylib.p/osdep_mac_app_bridge.m.o
ld: warning: ignoring file libmpv.2.dylib.p/video_out_hwdec_hwdec_vt_pl.m.o, lto file was built for unknown bitcode architecture which is not the architecture being linked (x86_64): libmpv.2.dylib.p/video_out_hwdec_hwdec_vt_pl.m.o
ld: warning: ignoring file libmpv.2.dylib.p/video_out_vulkan_context_mac.m.o, lto file was built for unknown bitcode architecture which is not the architecture being linked (x86_64): libmpv.2.dylib.p/video_out_vulkan_context_mac.m.o
ld: warning: ignoring file /Users/runner/work/mpv-mac/mpv-mac/workspace/lib/libmujs.a, building for macOS-x86_64 but attempting to link with file built for macOS-arm64

@Akemi
Copy link
Member

Akemi commented Dec 23, 2024

also still this question, which version/commit broke this? or didn't you try to cross build before?

@eko5624
Copy link
Author

eko5624 commented Dec 23, 2024

which version/commit broke this?

I have no clue. I think I have something wrong in my end, I'm digging.

didn't you try to cross build before?

On mac, never.

@Akemi
Copy link
Member

Akemi commented Dec 23, 2024

okay. it's possible that there is something wrong with or build system still. it worked at some point though i believe that was still when we used waf.

@kasper93
Copy link
Contributor

We could add cross build to our CI. I think it would be good given that GHA runners are only ARM for newer macOS versions.

@eko5624
Copy link
Author

eko5624 commented Dec 27, 2024

I did more tests. Below workaround also works. Maybe there's something wrong with the meson build system.
The succeed log: https://github.com/eko5624/mpv-mac/actions/runs/12514134718/job/34909783049

cd mpv
ln -s /PATH/TO/include/libplacebo libplacebo
ln -s /PATH/TO/include/libavutil libavutil
ln -s /PATH/TO/include/vulkan vulkan
ln -s /PATH/TO/include/vk_video vk_video

@Akemi
Copy link
Member

Akemi commented Dec 29, 2024

just wondering if it is related to our swiftc change, does it work when reverting this 8d20b72 or using the commit previous to the linked one?

@eko5624
Copy link
Author

eko5624 commented Dec 29, 2024

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

No branches or pull requests

3 participants