Skip to content

Commit

Permalink
build: Make ImHexbuildable for MacOS arm64 (#1414)
Browse files Browse the repository at this point in the history
  • Loading branch information
iTrooz authored and WerWolv committed Nov 11, 2023
1 parent 30ce4b6 commit f944a9d
Show file tree
Hide file tree
Showing 6 changed files with 270 additions and 5 deletions.
6 changes: 3 additions & 3 deletions cmake/build_helpers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ macro(configurePackingResources)
string(TIMESTAMP CURR_YEAR "%Y")
set(MACOSX_BUNDLE_COPYRIGHT "Copyright © 2020 - ${CURR_YEAR} WerWolv. All rights reserved." )
if ("${CMAKE_GENERATOR}" STREQUAL "Xcode")
set (IMHEX_BUNDLE_PATH "${CMAKE_BINARY_DIR}/${CMAKE_BUILD_TYPE}/ImHex.app")
set (IMHEX_BUNDLE_PATH "${CMAKE_BINARY_DIR}/${CMAKE_BUILD_TYPE}/imhex.app")
else ()
set (IMHEX_BUNDLE_PATH "${CMAKE_BINARY_DIR}/ImHex.app")
set (IMHEX_BUNDLE_PATH "${CMAKE_BINARY_DIR}/imhex.app")
endif()

set(PLUGINS_INSTALL_LOCATION "${IMHEX_BUNDLE_PATH}/Contents/MacOS/plugins")
Expand Down Expand Up @@ -259,7 +259,7 @@ macro(createPackage)
set(CPACK_GENERATOR "DragNDrop")

set (CPACK_BUNDLE_ICON "${CMAKE_SOURCE_DIR}/resources/dist/macos/AppIcon.icns" )
set (CPACK_BUNDLE_PLIST "${CMAKE_BINARY_DIR}/ImHex.app/Contents/Info.plist")
set (CPACK_BUNDLE_PLIST "${CMAKE_BINARY_DIR}/imhex.app/Contents/Info.plist")
else()
install(TARGETS main RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
if(WIN32) # Forwarder is only needed on Windows
Expand Down
6 changes: 6 additions & 0 deletions cmake/modules/PostprocessBundle.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ if(CMAKE_GENERATOR)
return()
endif()

# IMHEX PATCH BEGIN
# The function defined above doesn't keep in mind that if we are cross-compiling to MacOS, APPLE must be 1,
# so we force it here (where else would this script be run anyway ? This seems to be MacOS-specific code)
SET(APPLE 1)
# IMHEX PATCHE END

get_filename_component(BUNDLE_PATH "${BUNDLE_PATH}" ABSOLUTE)
message(STATUS "Fixing up application bundle: ${BUNDLE_PATH}")

Expand Down
163 changes: 163 additions & 0 deletions dist/macOS/arm64.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
# see arm64.crosscompile.Dockerfile
FROM crosscompile as build

ENV MACOSX_DEPLOYMENT_TARGET 12.1

# -- DOWNLOADING STUFF

## Install make
RUN --mount=type=cache,target=/var/lib/apt/lists/ apt update && apt install -y make

## fix environment
### add install_name_tool for cmake command that won't have the right env set (see PostprocessBundle.cmake function postprocess_bundle())
RUN cp /osxcross/build/cctools-port/cctools/misc/install_name_tool /usr/bin/install_name_tool
### a cmake thing wants 'otool' and not '' apparently
RUN cp /osxcross/target/bin/aarch64-apple-darwin23-otool /usr/bin/otool

## Clone glfw
RUN <<EOF
set -xe
if [ "$CUSTOM_GLFW" ]; then
git clone https://github.com/glfw/glfw /mnt/glfw
fi
EOF

## Assume the SDK has been removed from the image, and copy it again
COPY SDK /osxcross/target/SDK

## Download libmagic
### Clone libmagic
RUN git clone https://github.com/file/file /mnt/file
### Download libmagic dependencies
RUN --mount=type=cache,target=/var/lib/apt/lists/ apt install -y libtool autoconf

# -- DOWNLOADING + BUILDING STUFF
## Install libcurl dep
RUN vcpkg install --triplet=arm-osx-mytriplet curl
## Install mbedtls dep
RUN vcpkg install --triplet=arm-osx-mytriplet mbedtls
## Install freetype dep
RUN vcpkg install --triplet=arm-osx-mytriplet freetype
## Install jthread external library
RUN vcpkg install --triplet=arm-osx-mytriplet josuttis-jthread

## Install glfw3 dep
ARG CUSTOM_GLFW
RUN <<EOF
set -xe
if [ "$CUSTOM_GLFW" ]; then
echo "Flag confirmation: using custom GLFW for software rendering"
else
vcpkg install --triplet=arm-osx-mytriplet glfw3
fi
EOF

# -- BUILDING STUFF
ARG JOBS 1
ARG BUILD_TYPE Debug

## Build libmagic
RUN --mount=type=cache,target=/cache <<EOF
ccache -zs
set -xe

cd /mnt/file
autoreconf -is

# when cross-compiling, libmagic needs to have an the same version installed in the system.
# So we install it normally first
./configure --prefix /usr
make -j $JOBS install

# Now, we cross-compile it and install it in the libraries folder
CC=/osxcross/target/bin/aarch64-apple-darwin23-clang CXX=/osxcross/target/bin/aarch64-apple-darwin23-clang++ ./configure --prefix /vcpkg/installed/arm-osx-mytriplet --host $OSXCROSS_HOST
make -j $JOBS
make install

ccache -s

EOF

## Patch glfw
COPY --from=imhex /dist/macOS/0001-glfw-SW.patch /tmp
RUN <<EOF
set -xe
if [ "$CUSTOM_GLFW" ]; then
cd /mnt/glfw
git apply /tmp/0001-glfw-SW.patch
fi
EOF

## Build glfw
RUN --mount=type=cache,target=/cache <<EOF
set -xe
if [ "$CUSTOM_GLFW" ]; then
ccache -zs

cd /mnt/glfw
mkdir build
cd build
CC=o64-gcc CXX=o64-g++ cmake -G "Ninja" \
-DCMAKE_BUILD_TYPE=$BUILD_TYPE \
-DBUILD_SHARED_LIBS=ON \
-DCMAKE_C_COMPILER_LAUNCHER=ccache \
-DCMAKE_CXX_COMPILER_LAUNCHER=ccache \
-DCMAKE_OBJC_COMPILER_LAUNCHER=ccache \
-DCMAKE_OBJCXX_COMPILER_LAUNCHER=ccache \
-DCMAKE_INSTALL_PREFIX=/vcpkg/installed/arm-osx-mytriplet \
-DVCPKG_TARGET_TRIPLET=arm-osx-mytriplet -DCMAKE_TOOLCHAIN_FILE=/vcpkg/scripts/buildsystems/vcpkg.cmake -DVCPKG_CHAINLOAD_TOOLCHAIN_FILE=/osxcross/target/toolchain.cmake -DCMAKE_OSX_SYSROOT=/osxcross/target/SDK/MacOSX14.0.sdk -DCMAKE_OSX_DEPLOYMENT_TARGET=14.0 \
..
ninja -j $JOBS install

ccache -s
fi
EOF

# Build ImHex
## Copy ImHex
COPY --from=imhex / /mnt/ImHex
## Patch ImHex with hacks
# COPY toolchain.cmake.2 /osxcross/target/toolchain.cmake
# Configure ImHex build
RUN --mount=type=cache,target=/cache --mount=type=cache,target=/mnt/ImHex/build/_deps \
cd /mnt/ImHex && \
# compilers
CC=o64-clang CXX=o64-clang++ OBJC=/osxcross/target/bin/aarch64-apple-darwin23-clang OBJCXX=/osxcross/target/bin/aarch64-apple-darwin23-clang++ \
cmake -G "Ninja" \
`# ccache flags` \
-DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache -DCMAKE_OBJC_COMPILER_LAUNCHER=ccache -DCMAKE_OBJCXX_COMPILER_LAUNCHER=ccache \
`# MacOS cross-compiling flags` \
-DVCPKG_TARGET_TRIPLET=arm-osx-mytriplet -DCMAKE_TOOLCHAIN_FILE=/vcpkg/scripts/buildsystems/vcpkg.cmake -DVCPKG_CHAINLOAD_TOOLCHAIN_FILE=/osxcross/target/toolchain.cmake -DCMAKE_OSX_SYSROOT=/osxcross/target/SDK/MacOSX14.0.sdk -DCMAKE_OSX_DEPLOYMENT_TARGET=14.0 \
`# Override compilers for code generators` \
-DNATIVE_CMAKE_C_COMPILER=/usr/bin/clang -DNATIVE_CMAKE_CXX_COMPILER=/usr/bin/clang++ \
`# Normal ImHex flags` \
-DIMHEX_GENERATE_PACKAGE=ON -DCMAKE_BUILD_TYPE=$BUILD_TYPE \
`# other flags` \
-DIMHEX_STRICT_WARNINGS=OFF \
-B build
## Build ImHex
RUN --mount=type=cache,target=/cache --mount=type=cache,target=/mnt/ImHex/build/_deps <<EOF
ccache -zs
set -xe

cd /mnt/ImHex
cmake --build build --parallel $JOBS

ccache -s
EOF

# package ImHex
## install genisoimage
RUN --mount=type=cache,target=/var/lib/apt/lists/ apt install -y genisoimage
## Move everything that need to be packaged inside a directory
RUN <<EOF
set -xe
cd /mnt/ImHex/build
mkdir installDir
mv imhex.app installDir
EOF
## generate dmg file
RUN cd /mnt/ImHex/build && genisoimage -V imhex.app -D -R -apple -no-pad -o imhex.dmg installDir

FROM scratch
COPY --from=build /mnt/ImHex/build/imhex.dmg .
96 changes: 96 additions & 0 deletions dist/macOS/arm64.crosscompile.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# This image is is provided for reference, but a (potentially more up to date) image should be available at https://github.com/iTrooz/macos-crosscompile
FROM ubuntu:22.04

ENV PATH $PATH:/osxcross/target/bin
ENV LD_LIBRARY_PATH /osxcross/target/lib
ENV OSXCROSS_SDK /osxcross/target/SDK/MacOSX14.0.sdk
ENV OSXCROSS_TARGET darwin23
ENV OSXCROSS_TARGET_DIR /osxcross/target
ENV OSXCROSS_HOST aarch64-apple-darwin23

# -- DOWNLOADING STUFF

# Install common stuff
RUN --mount=type=cache,target=/var/lib/apt/lists/ export DEBIAN_FRONTEND=noninteractive &&\
export TZ=Etc/UTC &&\
dpkg --add-architecture i386 &&\
apt update &&\
apt -y install lsb-release build-essential python3 python3-pip git wget zip unzip pkg-config curl ninja-build software-properties-common gnupg libssl-dev ccache

# Install clang 17
RUN --mount=type=cache,target=/var/lib/apt/lists/ wget https://apt.llvm.org/llvm.sh && chmod +x llvm.sh && ./llvm.sh 17 &&\
ln -s /usr/bin/clang-17 /usr/bin/clang &&\
ln -s /usr/bin/clang++-17 /usr/bin/clang++

# Install vcpkg
RUN cd / &&\
git clone --depth 1 https://github.com/Microsoft/vcpkg.git vcpkg &&\
cd /vcpkg &&\
./bootstrap-vcpkg.sh -disableMetrics &&\
ln -s /vcpkg/vcpkg /usr/bin/ &&\
vcpkg install vcpkg-cmake &&\
ln -s /vcpkg/downloads/tools/cmake-*/cmake-*/bin/cmake /usr/bin/

RUN --mount=type=cache,target=/cache <<EOF
## Clone osxcross
set -xe
git clone https://github.com/tpoechtrager/osxcross /cache/osxcross || true
cd /cache/osxcross
git pull
cp -r /cache/osxcross /osxcross
EOF

RUN --mount=type=cache,target=/cache <<EOF
## Download SDK
set -xe
wget https://github.com/joseluisq/macosx-sdks/releases/download/14.0/MacOSX14.0.sdk.tar.xz -O /cache/MacOSX14.0.sdk.tar.xz -nc || true
cp /cache/MacOSX14.0.sdk.tar.xz /osxcross/tarballs
EOF

# Init stuff
## setup ccache dir
ENV CCACHE_DIR /cache/ccache

# Install triplet file
COPY arm-osx-mytriplet.cmake /vcpkg/triplets/community

# -- BUILDING STUFF
ARG JOBS 1

# Install osxcross
## Build cross-compiler clang-17
RUN --mount=type=cache,target=/cache <<EOF
set -xe
ccache -zs

cd /osxcross
UNATTENDED=1 CC=/usr/lib/ccache/clang-17 CXX=/usr/lib/ccache/clang++-17 ./build.sh

ccache -s
EOF
# Not needed, because we don't use gcc for cross-compiling anymore
# ## Install dependencies for gcc-13
# RUN apt install -y gcc g++ zlib1g-dev libmpc-dev libmpfr-dev libgmp-dev
# ## Build cross-compiler gcc-13
# RUN --mount=type=cache,target=/cache <<EOF
# set -xe
# ccache -zs

# cd /osxcross
# UNATTENDED=1 CC=/usr/lib/ccache/gcc CXX=/usr/lib/ccache/g++ GCC_VERSION=13.2.0 ./build_gcc.sh

# ccache -s
# EOF

ARG DELETE_SDK=1
RUN <<EOF
# Conditionally delete the SDK from the image
set -xe

if [ "$DELETE_SDK" ]; then
rm -r /osxcross/target/SDK
echo "Deleted the SDK from the image"
else
echo "NOT deleting the SDK from the image"
fi
EOF
2 changes: 1 addition & 1 deletion lib/external/imgui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ target_compile_options(imgui PRIVATE -Wno-unknown-warning-option)

target_include_directories(imgui PUBLIC include ${FREETYPE_INCLUDE_DIRS} ${GLFW_INCLUDE_DIRS} ${OpenGL_INCLUDE_DIRS})
target_link_directories(imgui PUBLIC ${GLFW_LIBRARY_DIRS} ${OpenGL_LIBRARY_DIRS})
target_link_libraries(imgui PUBLIC Freetype::Freetype ${GLFW_LIBRARIES} ${OPENGL_LIBRARIES})
target_link_libraries(imgui PUBLIC Freetype::Freetype ${FREETYPE_LIBRARIES} ${GLFW_LIBRARIES} ${OPENGL_LIBRARIES})
2 changes: 1 addition & 1 deletion lib/external/libromfs

0 comments on commit f944a9d

Please sign in to comment.