Skip to content

Instantly share code, notes, and snippets.

@anjannath
Last active May 13, 2022 04:33
Show Gist options
  • Save anjannath/cea4dab84e375a57364bf55df9ba9a15 to your computer and use it in GitHub Desktop.
Save anjannath/cea4dab84e375a57364bf55df9ba9a15 to your computer and use it in GitHub Desktop.
Instructions for building qemu on macOS
install the dependencies as seen on the brew formulae here: https://github.com/Homebrew/homebrew-core/blob/master/Formula/qemu.rb
export LIBRARY_PATH=/tmp/anjan/libs
export CPATH=/tmp/anjan/libs
LDFLAGS=-L/tmp/anjan/libs/lib
CPPFLAGS=-I/tmp/anjan/libs/include
./configure
make install
## Cp out the shared library objects the qemu binary is linked to
otool -L /tmp/anjan/qemu/bin/qemu-system-x86_64 | grep homebrew | awk '{print $1}' | xargs -t -I % cp % /tmp/anjan/qemu/libs/
## Change the path of the dylibs in the qemu-system-* binary
otool -L /tmp/anjan/qemu/bin/qemu-system-x86_64 | grep homebrew | awk '{print $1}' | xargs -t -I % sh -c 'install_name_tool -change % /tmp/anjan/qemu/libs/$(basename %) /tmp/anjan/qemu/bin/qemu-system-x86_64'
## Build gettext
$ ./configure --disable-dependency-tracking --disable-silent-rules --disable-debug --prefix=/tmp/anjan/libs --with-included-glib --with-included-libcroco --with-included-libunistring --with-included-libxml --with-emacs --with-lispdir=/tmp/anjan/libs/share/emacs/site-lisp/gettext --disable-java --disable-csharp --without-git --without-cvs --without-xz --with-included-gettext
## Build libffi
$ ./configure --disable-debug --disable-dependency-tracking --prefix=/tmp/anjan/libs --libdir=/tmp/anjan/libs/lib
$ make install
## Build PCRE
## "--enable-jit" if MacOS.version >= :sierra && !Hardware::CPU.arm?
$ ./configure --disable-dependency-tracking --enable-utf8 --enable-pcre8 --enable-pcre16 --enable-pcre32 --enable-unicode-properties --enable-pcregrep-libz --enable-pcregrep-libbz2 --prefix=/tmp/anjan/libs
$ make; make test; make install
## Build glib (https://raw.githubusercontent.com/Homebrew/formula-patches/43467fd8dfc0e8954892ecc08fab131242dca025/glib/hardcoded-paths.diff)
$ mkdir build; cd build
$ meson --prefix=/tmp/anjan/libs --libdir=/tmp/anjan/libs/lib --buildtype=release --wrap-mode=nofallback --default-library=both --localstatedir=/var -Diconv=auto -Dgio_module_dir=/tmp/anjan/libs/lib/gio/modules -Dbsymbolic_functions=false -Ddtrace=false ..
$ ninja -v
$ ninja install -v
$ #need to rewrite some python #! inside /tmp/anjan/libs/bin/(file *)
## Build jpeg
$ ./configure --disable-dependency-tracking --disable-silent-rules --prefix=/tmp/anjan/libs
$ make install
## Build libpng
$ ./configure --disable-dependency-tracking --disable-silent-rules --prefix=/tmp/anjan/libs
$ make; make test; make install
## Build libslirp (still linking to glib from homebrew, see pkg-config modifications needed
## std_meson_args = --prefix=/tmp/anjan/libs --libdir=/tmp/anjan/libs/lib --buildtype=release --wrap-mode=nofallback
$ meson build -Ddefault_library=both --prefix=/tmp/anjan/libs --libdir=/tmp/anjan/libs/lib --buildtype=release --wrap-mode=nofallback
$ ninja -C build install all
## Build libusb
$ ./configure --disable-dependency-tracking --prefix=/tmp/anjan/libs
$ make install
## Build lzo
$ ./configure --disable-dependency-tracking --prefix=/tmp/anjan/libs --enable-shared
$ make; make check; make install
## Build gmp (https://github.com/Homebrew/homebrew-core/blob/master/Formula/gmp.rb) (depends on kernel major version)
## std_configure_args = --disable-debug --disable-dependency-tracking --prefix=/tmp/anjan/libs --libdir=/tmp/anjan/libs/lib
$ ./configure --prefix=/tmp/anjan/libs --libdir=/tmp/anjan/libs/lib --enable-cxx --with-pic --build=aarch64-apple-darwin21
$ make; make check; make install
## Build nettle
$ ./configure --build=aarch64-apple-darwin21 --disable-dependency-tracking --prefix=/tmp/anjan/libs --enable-shared
$ make; make check; make install
## Build pixman
$ wget --content-disposition https://cairographics.org/releases/pixman-0.40.0.tar.gz
$ ./configure --disable-dependency-tracking --disable-gtk --disable-silent-rules --prefix=/tmp/anjan/libs
$ make install
## Build snappy (cmake)
## cmake std args = -DBUILD_SHARED_LIBS=ON -DCMAKE_INSTALL_PREFIX=/tmp/anjan/libs -DCMAKE_INSTALL_LIBDIR=/tmp/anjan/libs/lib -DCMAKE_BUILD_TYPE=Release -DCMAKE_VERBOSE_MAKEFILE=ON -Wno-dev -DBUILD_TESTING=OFF
$ wget https://github.com/google/snappy/archive/1.1.9.tar.gz
$ cmake . -DSNAPPY_BUILD_TESTS=OFF -DSNAPPY_BUILD_BENCHMARKS=OFF -DBUILD_SHARED_LIBS=ON -DCMAKE_INSTALL_PREFIX=/tmp/anjan/libs -DCMAKE_INSTALL_LIBDIR=/tmp/anjan/libs/lib -DCMAKE_BUILD_TYPE=Release -DCMAKE_VERBOSE_MAKEFILE=ON -Wno-dev -DBUILD_TESTING=OFF
$ make install
## Build vde
## sourceforge download
$ ./configure --prefix=/tmp/anjan/libs/ --disable-python
$ make install
## gnutls
$ ./configure --disable-dependency-tracking --disable-silent-rules --prefix=/tmp/anjan/libs --sysconfdir=/tmp/anjan/libs/etc --with-default-trust-store-file=/opt/homebrew/Cellar/ca-certificates/2022-04-26/share/ca-certificates/cacert.pem --disable-heartbeat-support --with-p11-kit --with-included-unistring
=== tough nuts ===
## Build libssh
## Build libncurses
@anjannath
Copy link
Author

After the above steps to build, there are libs that are referring to other libs and those cannot be found, so we have to build the dependecy libs as well

$ /tmp/anjan/qemu/bin/qemu-system-x86_64 -m 512 -nic user -boot d -cdrom alpine-virt-3.15.4-x86.iso -hda alpine.qcow2 -display cocoa
dyld[52736]: Library not loaded: /opt/homebrew/opt/nettle/lib/libnettle.8.dylib
  Referenced from: /private/tmp/anjan/qemu/libs/libgnutls.30.dylib
  Reason: tried: '/opt/homebrew/opt/nettle/lib/libnettle.8.dylib' (no such file), '/usr/local/lib/libnettle.8.dylib' (no such file), '/usr/lib/libnettle.8.dylib' (no such file)Library not loaded: /opt/homebrew/Cellar/glib/2.72.0/lib/libglib-2.0.0.dylib
  Referenced from: /private/tmp/anjan/qemu/libs/libgio-2.0.0.dylib
  Reason: tried: '/opt/homebrew/Cellar/glib/2.72.0/lib/libglib-2.0.0.dylib' (no such file), '/usr/local/lib/libglib-2.0.0.dylib' (no such file), '/usr/lib/libglib-2.0.0.dylib' (no such file)Library not loaded: /opt/homebrew/Cellar/glib/2.72.0/lib/libglib-2.0.0.dylib
  Referenced from: /private/tmp/anjan/qemu/libs/libgobject-2.0.0.dylib
  Reason: tried: '/opt/homebrew/Cellar/glib/2.72.0/lib/libglib-2.0.0.dylib' (no such file), '/usr/local/lib/libglib-2.0.0.dylib' (no such file), '/usr/lib/libglib-2.0.0.dylib' (no such file)Library not loaded: /opt/homebrew/Cellar/glib/2.72.0/lib/libglib-2.0.0.dylib
  Referenced from: /private/tmp/anjan/qemu/libs/libgmodule-2.0.0.dylib
  Reason: tried: '/opt/homebrew/Cellar/glib/2.72.0/lib/libglib-2.0.0.dylib' (no such file), '/usr/local/lib/libglib-2.0.0.dylib' (no such file), '/usr/lib/libglib-2.0.0.dylib' (no such file)
Abort trap: 6

@anjannath
Copy link
Author

anjannath commented Apr 22, 2022

$ otool -L /tmp/anjan/libs/lib/libslirp.dylib
/tmp/anjan/libs/lib/libslirp.dylib:
	/tmp/anjan/libs/lib/libslirp.0.dylib (compatibility version 0.0.0, current version 0.0.0)
	/opt/homebrew/opt/glib/lib/libglib-2.0.0.dylib (compatibility version 7201.0.0, current version 7201.0.0)
	/usr/lib/libresolv.9.dylib (compatibility version 1.0.0, current version 1.0.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1311.100.3)

@anjannath
Copy link
Author

admin@admins-Virtual-Machine qemu-macos-build % brew list
==> Formulae
autoconf	gcc		isl		libunistring	meson		[email protected]	readline	wget
automake	gdbm		libidn2		libyaml		mpdecimal	pkg-config	ruby		xz
ca-certificates	gettext		libmpc		m4		mpfr		[email protected]	ruby-build	zstd
cmake		gmp		libtool		make		ninja		rbenv		sqlite

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