[PATCH v1 0/5] Support MacOS
Hi, I love libguestfs and guestfs-tools. Here's a bunch of fixes to make it compile on MacOS. There's a homebrew formula which applies these patches before building libguests: https://github.com/quic/homebrew-quic/blob/main/Formula/libguestfs.rb I am not sure about `configure: Use -map in script flags`, as it probably needs some way to detect the current platform and use the right flag for the linker. Best regards, Antonio Caggiano Antonio Caggiano (5): build: Link libvirt-is-version with libgnu lib: Fix environ on MacOS configure: Use -map in script flags qemu: Add HVF to the accelerators list. ocaml: INSTALL_OCAMLLIB Makefile parameter lib/Makefile.am | 6 ++++-- lib/guestfs-internal.h | 5 +++++ lib/handle.c | 1 + lib/qemu.c | 4 ++-- m4/guestfs-c.m4 | 2 +- m4/ocaml.m4 | 3 +++ ocaml/Makefile.am | 14 +++++++------- 7 files changed, 23 insertions(+), 12 deletions(-) -- 2.45.1
This fixes building the tool on MacOS. Signed-off-by: Antonio Caggiano <quic_acaggian@quicinc.com> --- lib/Makefile.am | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/Makefile.am b/lib/Makefile.am index 4adf17ecd..0ed1576f8 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -178,10 +178,12 @@ libvirt_is_version_SOURCES = libvirt-is-version.c libvirt_is_version_LDADD = \ $(LIBVIRT_LIBS) \ - $(LTLIBINTL) + $(LTLIBINTL) \ + $(top_builddir)/gnulib/lib/.libs/libgnu.a libvirt_is_version_CPPFLAGS = \ - -DLOCALEBASEDIR=\""$(datadir)/locale"\" + -DLOCALEBASEDIR=\""$(datadir)/locale"\" \ + -I$(top_srcdir)/gnulib/lib -I$(top_builddir)/gnulib/lib libvirt_is_version_CFLAGS = \ $(WARN_CFLAGS) $(WERROR_CFLAGS) \ -- 2.45.1
On macOS, environ is not declared. Use the workaround suggested here: https://www.gnu.org/software/gnulib/manual/html_node/environ.html Signed-off-by: Antonio Caggiano <quic_acaggian@quicinc.com> --- lib/guestfs-internal.h | 5 +++++ lib/handle.c | 1 + 2 files changed, 6 insertions(+) diff --git a/lib/guestfs-internal.h b/lib/guestfs-internal.h index 57f0eb173..174ca135f 100644 --- a/lib/guestfs-internal.h +++ b/lib/guestfs-internal.h @@ -26,6 +26,11 @@ #ifndef GUESTFS_INTERNAL_H_ #define GUESTFS_INTERNAL_H_ +#ifdef __APPLE__ +#include <crt_externs.h> +#define environ (*_NSGetEnviron()) +#endif // __APPLE__ + #include <stdbool.h> #include <assert.h> diff --git a/lib/handle.c b/lib/handle.c index f1f33e737..df8e4284f 100644 --- a/lib/handle.c +++ b/lib/handle.c @@ -27,6 +27,7 @@ #include <stdlib.h> #include <string.h> #include <libintl.h> +#include <errno.h> #include <libxml/parser.h> #include <libxml/xmlversion.h> -- 2.45.1
-M is not a valid flag for MacOS ld. Signed-off-by: Antonio Caggiano <quic_acaggian@quicinc.com> --- m4/guestfs-c.m4 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/m4/guestfs-c.m4 b/m4/guestfs-c.m4 index c6d33183d..8035a9729 100644 --- a/m4/guestfs-c.m4 +++ b/m4/guestfs-c.m4 @@ -57,7 +57,7 @@ CFLAGS="$CFLAGS -fno-strict-overflow -Wno-strict-overflow" dnl Work out how to specify the linker script to the linker. VERSION_SCRIPT_FLAGS=-Wl,--version-script= `/usr/bin/ld --help 2>&1 | grep -- --version-script >/dev/null` || \ - VERSION_SCRIPT_FLAGS="-Wl,-M -Wl," + VERSION_SCRIPT_FLAGS="-Wl,-map -Wl," AC_SUBST(VERSION_SCRIPT_FLAGS) dnl Use -fvisibility=hidden by default in the library. -- 2.45.1
On Mon, Aug 12, 2024 at 11:41:20AM +0200, Antonio Caggiano wrote:
-M is not a valid flag for MacOS ld.
Signed-off-by: Antonio Caggiano <quic_acaggian@quicinc.com> --- m4/guestfs-c.m4 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/m4/guestfs-c.m4 b/m4/guestfs-c.m4 index c6d33183d..8035a9729 100644 --- a/m4/guestfs-c.m4 +++ b/m4/guestfs-c.m4 @@ -57,7 +57,7 @@ CFLAGS="$CFLAGS -fno-strict-overflow -Wno-strict-overflow" dnl Work out how to specify the linker script to the linker. VERSION_SCRIPT_FLAGS=-Wl,--version-script= `/usr/bin/ld --help 2>&1 | grep -- --version-script >/dev/null` || \ - VERSION_SCRIPT_FLAGS="-Wl,-M -Wl," + VERSION_SCRIPT_FLAGS="-Wl,-map -Wl," AC_SUBST(VERSION_SCRIPT_FLAGS)
Unfortunately -map isn't a flag which Linux's linker understands. The way to do this is to conditionalise the code based on the $host_os variable which probably contains "darwin*" for macOS. I'm sure there are many examples on line, but here are some from nbdkit: https://gitlab.com/nbdkit/nbdkit/-/blob/7a72d15a663cf12fdb74219f64275602369c... https://gitlab.com/nbdkit/nbdkit/-/blob/7a72d15a663cf12fdb74219f64275602369c... https://gitlab.com/nbdkit/nbdkit/-/blob/7a72d15a663cf12fdb74219f64275602369c... Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-p2v converts physical machines to virtual machines. Boot with a live CD or over the network (PXE) and turn machines into KVM guests. http://libguestfs.org/virt-v2v
This speeds up greatly QEMU when running on MacOS. Signed-off-by: Antonio Caggiano <quic_acaggian@quicinc.com> --- lib/qemu.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/qemu.c b/lib/qemu.c index 71115a27e..027790e4e 100644 --- a/lib/qemu.c +++ b/lib/qemu.c @@ -309,7 +309,7 @@ test_qemu_devices (guestfs_h *g, struct qemu_data *data) #ifdef MACHINE_TYPE MACHINE_TYPE "," #endif - "accel=kvm:tcg"); + "accel=kvm:hvf:tcg"); guestfs_int_cmd_add_arg (cmd, "-device"); guestfs_int_cmd_add_arg (cmd, "?"); guestfs_int_cmd_clear_capture_errors (cmd); @@ -574,7 +574,7 @@ generic_qmp_test (guestfs_h *g, struct qemu_data *data, #ifdef MACHINE_TYPE MACHINE_TYPE "," #endif - "accel=kvm:tcg"); + "accel=kvm:hvf:tcg"); guestfs_int_cmd_add_string_unquoted (cmd, " -qmp stdio"); guestfs_int_cmd_clear_capture_errors (cmd); -- 2.45.1
Add INSTALL_OCAMLLIB parameter for allowing ocaml install to a user defined path. If not defined, fallback to `ocamlc -where`. Signed-off-by: Antonio Caggiano <quic_acaggian@quicinc.com> --- m4/ocaml.m4 | 3 +++ ocaml/Makefile.am | 14 +++++++------- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/m4/ocaml.m4 b/m4/ocaml.m4 index fddd6a0c2..91896f386 100644 --- a/m4/ocaml.m4 +++ b/m4/ocaml.m4 @@ -17,6 +17,9 @@ AC_DEFUN([AC_PROG_OCAML], OCAMLVERSION=`$OCAMLC -v | sed -n -e 's|.*version* *\(.*\)$|\1|p'` AC_MSG_RESULT([OCaml version is $OCAMLVERSION]) OCAMLLIB=`$OCAMLC -where 2>/dev/null || $OCAMLC -v|tail -1|cut -d ' ' -f 4` + if test "x$INSTALL_OCAMLLIB" = "x"; then + INSTALL_OCAMLLIB=$OCAMLLIB + fi AC_MSG_RESULT([OCaml library path is $OCAMLLIB]) AC_SUBST([OCAMLVERSION]) diff --git a/ocaml/Makefile.am b/ocaml/Makefile.am index 63713ee68..f7621a8fa 100644 --- a/ocaml/Makefile.am +++ b/ocaml/Makefile.am @@ -185,16 +185,16 @@ data_hook_files += *.cmx *.cmxa endif install-data-hook: - mkdir -p $(DESTDIR)$(OCAMLLIB) - mkdir -p $(DESTDIR)$(OCAMLLIB)/stublibs - rm -rf $(DESTDIR)$(OCAMLLIB)/guestfs - rm -rf $(DESTDIR)$(OCAMLLIB)/stublibs/dllmlguestfs.so* + mkdir -p $(DESTDIR)$(INSTALL_OCAMLLIB) + mkdir -p $(DESTDIR)$(INSTALL_OCAMLLIB)/stublibs + rm -rf $(DESTDIR)$(INSTALL_OCAMLLIB)/guestfs + rm -rf $(DESTDIR)$(INSTALL_OCAMLLIB)/stublibs/dllmlguestfs.so* $(OCAMLFIND) install \ - -ldconf ignore -destdir $(DESTDIR)$(OCAMLLIB) \ + -ldconf ignore -destdir $(DESTDIR)$(INSTALL_OCAMLLIB) \ guestfs \ $(data_hook_files) - rm -f $(DESTDIR)$(OCAMLLIB)/guestfs/bindtests.* - rm $(DESTDIR)$(OCAMLLIB)/guestfs/libguestfsocaml.a + rm -f $(DESTDIR)$(INSTALL_OCAMLLIB)/guestfs/bindtests.* + rm $(DESTDIR)$(INSTALL_OCAMLLIB)/guestfs/libguestfsocaml.a CLEANFILES += $(noinst_DATA) $(check_DATA) -- 2.45.1
On Mon, Aug 12, 2024 at 11:41:22AM +0200, Antonio Caggiano wrote:
Add INSTALL_OCAMLLIB parameter for allowing ocaml install to a user defined path. If not defined, fallback to `ocamlc -where`.
Can't the usual destdir mechanism be used?
Signed-off-by: Antonio Caggiano <quic_acaggian@quicinc.com> --- m4/ocaml.m4 | 3 +++ ocaml/Makefile.am | 14 +++++++------- 2 files changed, 10 insertions(+), 7 deletions(-)
diff --git a/m4/ocaml.m4 b/m4/ocaml.m4 index fddd6a0c2..91896f386 100644 --- a/m4/ocaml.m4 +++ b/m4/ocaml.m4 @@ -17,6 +17,9 @@ AC_DEFUN([AC_PROG_OCAML], OCAMLVERSION=`$OCAMLC -v | sed -n -e 's|.*version* *\(.*\)$|\1|p'` AC_MSG_RESULT([OCaml version is $OCAMLVERSION]) OCAMLLIB=`$OCAMLC -where 2>/dev/null || $OCAMLC -v|tail -1|cut -d ' ' -f 4` + if test "x$INSTALL_OCAMLLIB" = "x"; then + INSTALL_OCAMLLIB=$OCAMLLIB + fi AC_MSG_RESULT([OCaml library path is $OCAMLLIB])
AC_SUBST([OCAMLVERSION])
Note this is a copy-in file from another project, I don't know if we can really modify it here. (Unfortunately I'm not sure of the upstream status ...) Eric, any idea about this?
diff --git a/ocaml/Makefile.am b/ocaml/Makefile.am index 63713ee68..f7621a8fa 100644 --- a/ocaml/Makefile.am +++ b/ocaml/Makefile.am @@ -185,16 +185,16 @@ data_hook_files += *.cmx *.cmxa endif
install-data-hook: - mkdir -p $(DESTDIR)$(OCAMLLIB) - mkdir -p $(DESTDIR)$(OCAMLLIB)/stublibs - rm -rf $(DESTDIR)$(OCAMLLIB)/guestfs - rm -rf $(DESTDIR)$(OCAMLLIB)/stublibs/dllmlguestfs.so* + mkdir -p $(DESTDIR)$(INSTALL_OCAMLLIB) + mkdir -p $(DESTDIR)$(INSTALL_OCAMLLIB)/stublibs + rm -rf $(DESTDIR)$(INSTALL_OCAMLLIB)/guestfs + rm -rf $(DESTDIR)$(INSTALL_OCAMLLIB)/stublibs/dllmlguestfs.so* $(OCAMLFIND) install \ - -ldconf ignore -destdir $(DESTDIR)$(OCAMLLIB) \ + -ldconf ignore -destdir $(DESTDIR)$(INSTALL_OCAMLLIB) \ guestfs \ $(data_hook_files) - rm -f $(DESTDIR)$(OCAMLLIB)/guestfs/bindtests.* - rm $(DESTDIR)$(OCAMLLIB)/guestfs/libguestfsocaml.a + rm -f $(DESTDIR)$(INSTALL_OCAMLLIB)/guestfs/bindtests.* + rm $(DESTDIR)$(INSTALL_OCAMLLIB)/guestfs/libguestfsocaml.a
CLEANFILES += $(noinst_DATA) $(check_DATA)
This part of the change seems reasonable. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-p2v converts physical machines to virtual machines. Boot with a live CD or over the network (PXE) and turn machines into KVM guests. http://libguestfs.org/virt-v2v
On Mon, Aug 12, 2024 at 11:31:34AM GMT, Richard W.M. Jones wrote:
On Mon, Aug 12, 2024 at 11:41:22AM +0200, Antonio Caggiano wrote:
Add INSTALL_OCAMLLIB parameter for allowing ocaml install to a user defined path. If not defined, fallback to `ocamlc -where`.
Can't the usual destdir mechanism be used?
Signed-off-by: Antonio Caggiano <quic_acaggian@quicinc.com> --- m4/ocaml.m4 | 3 +++ ocaml/Makefile.am | 14 +++++++------- 2 files changed, 10 insertions(+), 7 deletions(-)
diff --git a/m4/ocaml.m4 b/m4/ocaml.m4 index fddd6a0c2..91896f386 100644 --- a/m4/ocaml.m4 +++ b/m4/ocaml.m4 @@ -17,6 +17,9 @@ AC_DEFUN([AC_PROG_OCAML], OCAMLVERSION=`$OCAMLC -v | sed -n -e 's|.*version* *\(.*\)$|\1|p'` AC_MSG_RESULT([OCaml version is $OCAMLVERSION]) OCAMLLIB=`$OCAMLC -where 2>/dev/null || $OCAMLC -v|tail -1|cut -d ' ' -f 4` + if test "x$INSTALL_OCAMLLIB" = "x"; then + INSTALL_OCAMLLIB=$OCAMLLIB + fi AC_MSG_RESULT([OCaml library path is $OCAMLLIB])
AC_SUBST([OCAMLVERSION])
Note this is a copy-in file from another project, I don't know if we can really modify it here. (Unfortunately I'm not sure of the upstream status ...)
Eric, any idea about this?
Which upstream? The file was added in commit acf90002 in 2009, and the git log does not state what the upstream source would be (only that there is supposed to be an ocaml.m4 man page somewhere?) Okay, a quick google search found: https://manpages.ubuntu.com/manpages/mantic/man1/ocaml.m4.1.html which says upstream is: https://forge.ocamlcore.org/projects/ocaml-autoconf/ and sure enough, Fedora 40 has the ocaml-autoconf-1.1-40.fc40.noarch package available for use, and which appears newer than the copy we are using (I think it was copied from ocaml-autoconf-1.0), although upstream still carries only a 2009 copyright date, so it's hard to tell if upstream has any recent maintenance. The beauty of open source is that we can fork the file to meet our needs; the curse of open source is that if we fork and don't push the improvements upstream, no one else will benefit. But given how little upstream seems to change, I don't know that there are many users in the first place. It may also be possible to accomplish what you want by adding your code to configure.ac after the call to AC_PROG_OCAML, rather than modifying the imported file. -- Eric Blake, Principal Software Engineer Red Hat, Inc. Virtualization: qemu.org | libguestfs.org
On Mon, Aug 12, 2024 at 07:59:22AM -0500, Eric Blake wrote:
On Mon, Aug 12, 2024 at 11:31:34AM GMT, Richard W.M. Jones wrote:
On Mon, Aug 12, 2024 at 11:41:22AM +0200, Antonio Caggiano wrote:
Add INSTALL_OCAMLLIB parameter for allowing ocaml install to a user defined path. If not defined, fallback to `ocamlc -where`.
Can't the usual destdir mechanism be used?
Signed-off-by: Antonio Caggiano <quic_acaggian@quicinc.com> --- m4/ocaml.m4 | 3 +++ ocaml/Makefile.am | 14 +++++++------- 2 files changed, 10 insertions(+), 7 deletions(-)
diff --git a/m4/ocaml.m4 b/m4/ocaml.m4 index fddd6a0c2..91896f386 100644 --- a/m4/ocaml.m4 +++ b/m4/ocaml.m4 @@ -17,6 +17,9 @@ AC_DEFUN([AC_PROG_OCAML], OCAMLVERSION=`$OCAMLC -v | sed -n -e 's|.*version* *\(.*\)$|\1|p'` AC_MSG_RESULT([OCaml version is $OCAMLVERSION]) OCAMLLIB=`$OCAMLC -where 2>/dev/null || $OCAMLC -v|tail -1|cut -d ' ' -f 4` + if test "x$INSTALL_OCAMLLIB" = "x"; then + INSTALL_OCAMLLIB=$OCAMLLIB + fi AC_MSG_RESULT([OCaml library path is $OCAMLLIB])
AC_SUBST([OCAMLVERSION])
Note this is a copy-in file from another project, I don't know if we can really modify it here. (Unfortunately I'm not sure of the upstream status ...)
Eric, any idea about this?
Which upstream? The file was added in commit acf90002 in 2009, and the git log does not state what the upstream source would be (only that there is supposed to be an ocaml.m4 man page somewhere?)
Okay, a quick google search found: https://manpages.ubuntu.com/manpages/mantic/man1/ocaml.m4.1.html
which says upstream is: https://forge.ocamlcore.org/projects/ocaml-autoconf/
Yes that's the one. I don't think we've updated since 2009 on the basis that if it ain't broken, don't fix it. And while I was involved upstream two decades ago I haven't made any recent contributions upstream either so I'm not sure about the upstream status.
and sure enough, Fedora 40 has the ocaml-autoconf-1.1-40.fc40.noarch package available for use, and which appears newer than the copy we are using (I think it was copied from ocaml-autoconf-1.0), although upstream still carries only a 2009 copyright date, so it's hard to tell if upstream has any recent maintenance.
The beauty of open source is that we can fork the file to meet our needs; the curse of open source is that if we fork and don't push the improvements upstream, no one else will benefit. But given how little upstream seems to change, I don't know that there are many users in the first place.
It may also be possible to accomplish what you want by adding your code to configure.ac after the call to AC_PROG_OCAML, rather than modifying the imported file.
This is probably better in this case. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com Fedora Windows cross-compiler. Compile Windows programs, test, and build Windows installers. Over 100 libraries supported. http://fedoraproject.org/wiki/MinGW
On Mon, Aug 12, 2024 at 11:41:17AM +0200, Antonio Caggiano wrote:
Hi,
I love libguestfs and guestfs-tools. Here's a bunch of fixes to make it compile on MacOS. There's a homebrew formula which applies these patches before building libguests: https://github.com/quic/homebrew-quic/blob/main/Formula/libguestfs.rb
I am not sure about `configure: Use -map in script flags`, as it probably needs some way to detect the current platform and use the right flag for the linker.
Best regards, Antonio Caggiano
Antonio Caggiano (5): build: Link libvirt-is-version with libgnu lib: Fix environ on MacOS configure: Use -map in script flags qemu: Add HVF to the accelerators list. ocaml: INSTALL_OCAMLLIB Makefile parameter
lib/Makefile.am | 6 ++++-- lib/guestfs-internal.h | 5 +++++ lib/handle.c | 1 + lib/qemu.c | 4 ++-- m4/guestfs-c.m4 | 2 +- m4/ocaml.m4 | 3 +++ ocaml/Makefile.am | 14 +++++++------- 7 files changed, 23 insertions(+), 12 deletions(-)
Thanks, I pushed # 1, 2 & 4. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com nbdkit - Flexible, fast NBD server with plugins https://gitlab.com/nbdkit/nbdkit
participants (3)
-
Antonio Caggiano -
Eric Blake -
Richard W.M. Jones