Skip to content

Commit

Permalink
Bundle r2 on macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
prodrigestivill committed Jan 12, 2025
1 parent a20f368 commit 3bea0b2
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 33 deletions.
22 changes: 11 additions & 11 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,20 +81,20 @@ jobs:
echo $(brew --prefix qt@5)/bin >> $GITHUB_PATH
pip3 install meson ninja
- name: install r2
working-directory: dist/macos
run: |
wget -q https://github.com/radareorg/radare2/releases/download/${{env.R2V}}/radare2-x64-${{env.R2V}}.pkg
sudo installer -pkg *.pkg -target /
curl -Lo radare2.pkg "https://github.com/radareorg/radare2/releases/download/${{env.R2V}}/radare2-x64-${{env.R2V}}.pkg"
sudo installer -pkg radare2.pkg -target /
- name: build iaito
run: |
./configure
make -j4
make -j4 QMAKE_FLAGS=IAITO_BUNDLE_R2_APPBUNDLE=true
- name: packaging
working-directory: build
run: macdeployqt iaito.app -dmg -verbose=2
run: make -C dist/macos
- uses: actions/upload-artifact@v4
with:
name: iaito-x64.dmg
path: build/iaito.dmg
path: dist/macos/iaito.dmg
acr-macos-arm64:
runs-on: macos-latest
steps:
Expand All @@ -113,20 +113,20 @@ jobs:
echo $(brew --prefix qt@5)/bin >> $GITHUB_PATH
pip3 install meson ninja
- name: install r2
working-directory: dist/macos
run: |
wget -q https://github.com/radareorg/radare2/releases/download/${{env.R2V}}/radare2-m1-${{env.R2V}}.pkg
sudo installer -pkg *.pkg -target /
curl -Lo radare2.pkg "https://github.com/radareorg/radare2/releases/download/${{env.R2V}}/radare2-m1-${{env.R2V}}.pkg"
sudo installer -pkg radare2.pkg -target /
- name: build iaito
run: |
./configure
make -j4
- name: packaging
working-directory: build
run: macdeployqt iaito.app -dmg -verbose=2
run: make -C dist/macos
- uses: actions/upload-artifact@v4
with:
name: iaito-arm64.dmg
path: build/iaito.dmg
path: dist/macos/iaito.dmg
meson:
runs-on: ${{ matrix.os }}
strategy:
Expand Down
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,10 @@ src/out
*.orig

# Translations
src/translations
src/translations

# macOS package
/dist/macos/radare2.pkg
/dist/macos/radare2-unpkg
/dist/macos/disk
/dist/macos/*.dmg
27 changes: 27 additions & 0 deletions dist/macos/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
.PHONY: all app clean mrproper

all: app

app: ../../build/iaito.app radare2-unpkg
mkdir disk
cp -a ../../build/iaito.app disk/
scripts/embed-radare2.sh radare2-unpkg disk/iaito.app
cd disk && macdeployqt iaito.app -dmg -verbose=2
mv disk/*.dmg .

radare2-unpkg: radare2.pkg
pkgutil --expand-full $< $@

radare2.pkg:
echo "Download first radare2.pkg from https://github.com/radareorg/radare2/releases"
@false

../../build/iaito.app:
echo "Building iaito..."
$(MAKE) -C ../.. QMAKE_FLAGS=IAITO_BUNDLE_R2_APPBUNDLE=true

clean:
rm -rf disk radare2-unpkg

mrproper: clean
rm -f radare2.pkg
16 changes: 16 additions & 0 deletions dist/macos/scripts/command.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/sh

CMD=$(basename "$0")
APPDIR=$(cd "$(dirname "$0")/../../../.."; pwd)

R2_BINDIR="${APPDIR}/Contents/Helpers"
R2_LIBDIR="${APPDIR}/Contents/Frameworks"
R2_LIBR_PLUGINS="${APPDIR}/Contents/PlugIns/radare2"
R2_PREFIX="${APPDIR}/Contents/Resources/radare2"

export R2_BINDIR
export R2_LIBDIR
export R2_LIBR_PLUGINS
export R2_PREFIX

exec "${R2_BINDIR}/${CMD}" "$@"
63 changes: 63 additions & 0 deletions dist/macos/scripts/embed-radare2.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/bin/sh -e

R2PKGDIR="$1"
APPDIR="$2"

SCRIPTS="$(dirname "$0")"
R2DIR="${R2PKGDIR}/Payload/usr/local"
R2V=$(readlink "${R2DIR}/lib/radare2/last")

fix_binary() {
echo "Change library paths for \"$1\"..."
ARGS=$(otool -L "$1" | awk '/\/usr\/local\/lib\/libr_/{dst=$1; sub(/\/usr\/local\/lib/,"@executable_path/../Frameworks", dst); print "-change "$1" "dst}')
[ -n "$ARGS" ] && install_name_tool $ARGS "$1"
}

mkdir -p \
"${APPDIR}/Contents/Helpers" \
"${APPDIR}/Contents/Frameworks" \
"${APPDIR}/Contents/PlugIns/radare2" \
"${APPDIR}/Contents/Resources/radare2/bin" \
"${APPDIR}/Contents/Resources/radare2/lib/radare2"

cp -a "${R2DIR}/bin/"* "${APPDIR}/Contents/Helpers/"
cp -a "${R2DIR}/lib/radare2/${R2V}/"* "${APPDIR}/Contents/PlugIns/radare2/"
cp -a "${R2DIR}/lib/"*.dylib "${APPDIR}/Contents/Frameworks/"
cp -a "${R2DIR}/include" "${APPDIR}/Contents/Resources/radare2/"
cp -a "${R2DIR}/share" "${APPDIR}/Contents/Resources/radare2/"
#cp -a "${R2DIR}/lib/pkgconfig" "${APPDIR}/Contents/Resources/radare2/lib/"
cp -p "${SCRIPTS}/command.sh" "${APPDIR}/Contents/Resources/radare2/bin/radare2"
cp -a "${R2DIR}/lib/radare2/last" "${APPDIR}/Contents/Resources/radare2/lib/radare2/"
ln -s "../../../../PlugIns/radare2" "${APPDIR}/Contents/Resources/radare2/lib/radare2/${R2V}"

(
cd "${APPDIR}/Contents/MacOS"
fix_binary "iaito"
)

(
cd "${APPDIR}/Contents/Helpers"
for c in *; do
[ -L "$c" ] || fix_binary "$c"
[ "$c" != "radare2" ] && ln -s radare2 "../Resources/radare2/bin/$c"
done
)

(
LIBS=$(cd "${R2DIR}/lib"; ls *.dylib)
cd "${APPDIR}/Contents/Frameworks"
for c in $LIBS; do
[ -L "$c" ] || fix_binary "$c"
c2=$c # Resolve upto 2 link levels
[ -L "$c2" ] && c2=$(readlink "$c2")
[ -L "$c2" ] && c2=$(readlink "$c2")
ln -s "../../../Frameworks/$c2" "../Resources/radare2/lib/$c"
done
)

(
cd "${APPDIR}/Contents/PlugIns/radare2"
for c in *; do
[ -L "$c" ] || fix_binary "$c"
done
)
8 changes: 5 additions & 3 deletions src/Iaito.pro
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ CONFIG+=app_bundle

CONFIG += sdk_no_version_check

unix:QMAKE_RPATHDIR += /usr/local/lib
unix:QMAKE_LFLAGS_RPATH=
unix:QMAKE_LFLAGS += "-Wl,-rpath,/usr/local/lib"
unix:!macx|macx:!IAITO_BUNDLE_R2_APPBUNDLE {
QMAKE_RPATHDIR += /usr/local/lib
QMAKE_LFLAGS_RPATH=
QMAKE_LFLAGS += "-Wl,-rpath,/usr/local/lib"
}

QMAKE_CXXFLAGS += $$(CXXFLAGS)
QMAKE_CFLAGS += $$(CFLAGS)
Expand Down
37 changes: 21 additions & 16 deletions src/IaitoApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,27 @@ IaitoApplication::IaitoApplication(int &argc, char **argv)
qputenv("R_ALT_SRC_DIR", "1");
#endif

#ifdef MACOS_R2_BUNDLED
{
auto appdir = QDir(QCoreApplication::applicationDirPath()); // Contents/MacOS
appdir.cdUp(); // Contents

auto r2prefix = appdir; // Contents
r2prefix.cd("Resources/radare2"); // Contents/Resources/radare2
qputenv("R2_PREFIX", r2prefix.absolutePath().toLocal8Bit());

auto r2bin = appdir; // Contents
r2bin.cd("Helpers"); // Contents/Helpers
auto paths = QStringList(QString::fromLocal8Bit(qgetenv("PATH")));
paths.prepend(r2bin.absolutePath());
qputenv("PATH", paths.join(QLatin1Char(':')).toLocal8Bit());

// auto sleighHome = appdir; // Contents
// sleighHome.cd("PlugIns/radare2/r2ghidra_sleigh"); // Contents/PlugIns/radare2/r2ghidra_sleigh
// qputenv("SLEIGHHOME", sleighHome.absolutePath().toLocal8Bit());
}
#endif

Core()->initialize(clOptions.enableR2Plugins);
Core()->setSettings();
Config()->loadInitial();
Expand Down Expand Up @@ -219,22 +240,6 @@ IaitoApplication::IaitoApplication(int &argc, char **argv)
}
#endif

#ifdef Q_OS_MACOS
{
auto r2prefix = QDir(QCoreApplication::applicationDirPath()); // Contents/MacOS
r2prefix.cdUp(); // Contents
r2prefix.cd("Resources/r2"); // Contents/Resources/r2

auto sleighHome = r2prefix;
sleighHome.cd("share/radare2/plugins/r2ghidra_sleigh"); // Contents/Resources/r2/share/radare2/plugins/r2ghidra_sleigh
Core()->setConfig("r2ghidra.sleighhome", sleighHome.absolutePath());

auto r2decHome = r2prefix;
r2decHome.cd("share/radare2/plugins/r2dec-js"); // Contents/Resources/r2/share/radare2/plugins/r2dec-js
qputenv("R2DEC_HOME", r2decHome.absolutePath().toLocal8Bit());
}
#endif

#ifdef IAITO_APPVEYOR_R2DEC
qputenv("R2DEC_HOME", "lib\\plugins\\r2dec-js");
#endif
Expand Down
4 changes: 2 additions & 2 deletions src/core/Iaito.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,10 @@ void IaitoCore::initialize(bool loadPlugins)
prefix.cdUp();
qInfo() << "Setting r2 prefix =" << prefix.absolutePath() << " for AppImage.";
#else // MACOS_R2_BUNDLED \
// Executable is in Contents/MacOS, prefix is Contents/Resources/r2
// Executable is in Contents/MacOS, prefix is Contents/Resources/radare2
prefix.cdUp();
prefix.cd("Resources");
prefix.cd("r2");
prefix.cd("radare2");
qInfo() << "Setting r2 prefix =" << prefix.absolutePath() << " for macOS Application Bundle.";
#endif
setConfig("dir.prefix", prefix.absolutePath());
Expand Down

0 comments on commit 3bea0b2

Please sign in to comment.