Skip to contentSkip to conversation

Resolve link errors on Mac OS X

PATCH In review Christopher Friedt 4 participants 14 messages May 3, 2016 – May 6, 2016

lore
Share & tools
lore
#0 lore raw

cover Resolve link errors on Mac OS X

Hi list,

I recently tried to build Qemu on Mac and ran into a couple of trivial issues that I've provided patches for. I suppose that normally people just use 'brew install qemu', but there is really no reason that it can't be built from source, particularly for those modifying Qemu regularly.

In any case, the first change moves to using 'libtool -static' to create libraries on Mac OS X. If one attempts to use ar and ranlib, then the final link will fail with error messages resembling the following:

ld: warning: ignoring file libqemuutil.a, file was built for archive which is not the architecture being linked (x86_64): libqemuutil.a Undefined symbols for architecture x86_64:
...

Notice ld (Apple's ld64) presumes the static library is a relocatable with architecture 'archive' rather than x86_64, in this case.

A similar fix is required for dtc - again quite trivial, but I can provide that if necessary.

The second patch removes the preprocessor conditional around the function event_notifier_init_fd() in util/event_notifier-posix.c so that the link does not fail on systems where CONFIG_POSIX is defined but CONFIG_EVENTFD is not (such as under Mac OS X).

There is more information in each of the commits that follows.

Please feel free to comment.

Cheers,

C

Christopher Friedt (2):
Use libtool instead of ar to create static libraries on Darwin. Remove unnecessary CONFIG_EVENTFD preprocessor conditional to satisfy
link

 rules.mak                   | 4 ++++
 util/event_notifier-posix.c | 2 --
 2 files changed, 4 insertions(+), 2 deletions(-)
Signature · 1 line
2.6.4 (Apple Git-63)
#1 lore raw

PATCH 1/2 Use libtool instead of ar to create static libraries on Darwin.

Currently, at least on Mac OS X 10.11.4 (El Capitan), Qemu fails to build for a few reasons.

One of those reasons is that Apple's ld (at least ld64) does not properly process archive files created with ar (even Apple's ar).

After some RTFM'ing, I came upon this tidbit, which is unfortunate. Luckily, autotools packages are not broken.

"Libtool with -static is intended to replace ar(5) and ranlib."
http://www.manpages.info/macosx/libtool.1.html

In any case, this change takes Apple's recommendations into account and allows Qemu to build on Mac OS X El Capitan.

---
 rules.mak | 4 ++++
 1 file changed, 4 insertions(+)
rules.mak +4 -0
index d1ff311..44421af 100644
@@ -105,7 +105,11 @@ modules:
 	$(call LINK,$(filter %.o %.a %.mo, $^))
 
 %.a:
+ifeq ($(shell uname),Darwin)
+	$(call quiet-command,rm -f $@ && libtool -static -o $@ $^,"  libtool    $(TARGET_DIR)$@")
+else
 	$(call quiet-command,rm -f $@ && $(AR) rcs $@ $^,"  AR    $(TARGET_DIR)$@")
+endif
 
 quiet-command = $(if $(V),$1,$(if $(2),@echo $2 && $1, @$1))
 
#8 lore raw

PATCH 2/2 Remove unnecessary CONFIG_EVENTFD preprocessor conditional to satisfy link

In reply to Christopher Friedt

The file ivshmem.c unconditionally references event_notifier_init_fd() in util/event_notifier-posix.c, even if CONFIG_EVENTFD is not defined. On platforms where CONFIG_POSIX is defined, but CONFIG_EVENTFD is not defined, that results in an undefined symbol referenced from ivshmem.c and the link fails. That applies to Mac OS X, but possibly other BSD-based distros.

Note: there is nothing specific to eventfd inside and event_notifier_init() also fails unconditionally if CONFIG_EVENTFD is not defined.

---
 util/event_notifier-posix.c | 2 --
 1 file changed, 2 deletions(-)
util/event_notifier-posix.c +0 -2
index c1f0d79..c9bb34d 100644
@@ -21,7 +21,6 @@
 #include <sys/eventfd.h>
 #endif
 
-#ifdef CONFIG_EVENTFD
 /*
  * Initialize @e with existing file descriptor @fd.
  * @fd must be a genuine eventfd object, emulation with pipe won't do.
@@ -31,7 +30,6 @@ void event_notifier_init_fd(EventNotifier *e, int fd)
     e->rfd = fd;
     e->wfd = fd;
 }
-#endif
 
 int event_notifier_init(EventNotifier *e, int active)
 {
#2 lore raw

Re: [Qemu-devel] [PATCH 1/2] Use libtool instead of ar to create static libraries on Darwin.

In reply to Christopher Friedt
Quoted 3 lines · Christopher Friedt

Currently, at least on Mac OS X 10.11.4 (El Capitan), Qemu fails to build for a few reasons.

I guess this is a new-in-ElCapitan thing? I run Yosemite, which is fine.

thanks
-- PMM

#9 lore raw

Re: [Qemu-devel] [PATCH 2/2] Remove unnecessary CONFIG_EVENTFD preprocessor conditional to satisfy link

In reply to Christopher Friedt

[ccing somebody else who ran into this, since I've figured out why.]

Quoted 2 lines · Christopher Friedt

The file ivshmem.c unconditionally references event_notifier_init_fd() in util/event_notifier-posix.c, even if CONFIG_EVENTFD is not defined.

Yes, but ivshmem.c is only built if CONFIG_IVSHMEM, and CONFIG_IVSHMEM is set (in default-configs/pci.mak) to CONFIG_EVENTFD. So if CONFIG_EVENTFD is not defined then we should never build ivshmem.o.

The problem here is that you've run into a bug in QEMU's makefiles, where a change in an included .mak file in default-configs fails to cause the config-devices.mak file to be rebuilt. In commit 330b583 we changed pci.mak so that CONFIG_IVSHMEM is set to CONFIG_EVENTFD rather than CONFIG_POSIX, so if your config-devices.mak predates that commit then it will have incorrectly not been recreated and so your build will still try to build ivshmem.c.

You can check whether this is true by looking at (for instance) x86_64-softmmu/config-devices.mak in your build
tree: if it has a line "CONFIG_IVSHMEM=$(CONFIG_POSIX)" in it then it's the out of date version.

You should be able to fix this by deleting */config-devices.mak from your build tree (or by blowing away the build tree entirely and recreating it.) Then try rebuilding -- ivshmem.c should not be compiled.

We really must track down this dependency bug, it is very confusing when it bites people.

thanks
-- PMM

#4 lore raw

Re: [Qemu-devel] [PATCH 1/2] Use libtool instead of ar to create static libraries on Darwin.

In reply to Christopher Friedt

Cc'ing Peter Maydell, who must have better ideas than me on building on Mac.

Quoted 23 lines · Christopher Friedt

Currently, at least on Mac OS X 10.11.4 (El Capitan), Qemu fails to build for a few reasons.

One of those reasons is that Apple's ld (at least ld64) does not properly process archive files created with ar (even Apple's ar).

After some RTFM'ing, I came upon this tidbit, which is unfortunate. Luckily, autotools packages are not broken.

"Libtool with -static is intended to replace ar(5) and ranlib."
http://www.manpages.info/macosx/libtool.1.html

In any case, this change takes Apple's recommendations into account and allows Qemu to build on Mac OS X El Capitan.

---
 rules.mak | 4 ++++
 1 file changed, 4 insertions(+)
rules.mak +0 -0
index d1ff311..44421af 100644

rules.mak modules: · line 108

Quoted 4 lines
rules.mak +1 -0
@@ -105,7 +105,11 @@ modules:
 	$(call LINK,$(filter %.o %.a %.mo, $^))
 
 %.a:
+ifeq ($(shell uname),Darwin)

I think you can use "ifdef CONFIG_DARWIN" here.

rules.mak modules: · line 109

Quoted 1 line
rules.mak +1 -0
+	$(call quiet-command,rm -f $@ && libtool -static -o $@ $^,"  libtool    $(TARGET_DIR)$@")

Is libtool always available on Mac OS X? If not, we may need to add the detection to ./configure.

Fam

Quoted 6 lines
rules.mak +2 -0
+else
 	$(call quiet-command,rm -f $@ && $(AR) rcs $@ $^,"  AR    $(TARGET_DIR)$@")
+endif
 
 quiet-command = $(if $(V),$1,$(if $(2),@echo $2 && $1, @$1))
 
#3 lore raw

Re: [Qemu-devel] [PATCH 1/2] Use libtool instead of ar to create static libraries on Darwin.

In reply to Peter Maydell
Quoted 7 lines · Peter Maydell
Quoted 3 lines · Christopher Friedt

Currently, at least on Mac OS X 10.11.4 (El Capitan), Qemu fails to build for a few reasons.

I guess this is a new-in-ElCapitan thing? I run Yosemite, which is fine.

I think you're partially correct, although the information on the man page dates back to at least Snow Leopard (10.6.2) [1], so Apple has preferred the libtool method for a very long time.

It's possible that it only finally broke in El Capitan.

[1] http://www.unix.com/man-page/osx/1/libtool/

#5 lore raw

Re: [Qemu-devel] [PATCH 1/2] Use libtool instead of ar to create static libraries on Darwin.

In reply to Fam Zheng
Quoted 28 lines · Fam Zheng

Cc'ing Peter Maydell, who must have better ideas than me on building on Mac.

Quoted 25 lines · Christopher Friedt

Currently, at least on Mac OS X 10.11.4 (El Capitan), Qemu fails to build for a few reasons.

One of those reasons is that Apple's ld (at least ld64) does not properly process archive files created with ar (even Apple's ar).

After some RTFM'ing, I came upon this tidbit, which is unfortunate. Luckily, autotools packages are not broken.

"Libtool with -static is intended to replace ar(5) and ranlib."
http://www.manpages.info/macosx/libtool.1.html

In any case, this change takes Apple's recommendations into account and allows Qemu to build on Mac OS X El Capitan.

---
 rules.mak | 4 ++++
 1 file changed, 4 insertions(+)
rules.mak +0 -0
index d1ff311..44421af 100644
@@ -105,7 +105,11 @@ modules:
      $(call LINK,$(filter %.o %.a %.mo, $^))
 
Quoted 4 lines

rules.mak modules: · line 108

Quoted 2 lines
rules.mak +1 -0
 %.a:
+ifeq ($(shell uname),Darwin)

I think you can use "ifdef CONFIG_DARWIN" here.

Good suggestion. I missed that entirely.

Quoted 4 lines

rules.mak modules: · line 109

Quoted 1 line
rules.mak +1 -0
+     $(call quiet-command,rm -f $@ && libtool -static -o $@ $^,"  libtool    $(TARGET_DIR)$@")

Is libtool always available on Mac OS X? If not, we may need to add the detection to ./configure.

Apple's libtool should be installed with their command line tools, at least as far back as Snow Leopard.

#10 lore raw

Re: [Qemu-devel] [PATCH 2/2] Remove unnecessary CONFIG_EVENTFD preprocessor conditional to satisfy link

In reply to Peter Maydell
Quoted 11 lines · Peter Maydell

[ccing somebody else who ran into this, since I've figured out why.]

Quoted 2 lines · Christopher Friedt

The file ivshmem.c unconditionally references event_notifier_init_fd() in util/event_notifier-posix.c, even if CONFIG_EVENTFD is not defined.

Yes, but ivshmem.c is only built if CONFIG_IVSHMEM, and CONFIG_IVSHMEM is set (in default-configs/pci.mak) to CONFIG_EVENTFD. So if CONFIG_EVENTFD is not defined then we should never build ivshmem.o.

The problem here is that you've run into a bug in QEMU's makefiles,

That would explain things.

Quoted 11 lines

where a change in an included .mak file in default-configs fails to cause the config-devices.mak file to be rebuilt. In commit 330b583 we changed pci.mak so that CONFIG_IVSHMEM is set to CONFIG_EVENTFD rather than CONFIG_POSIX, so if your config-devices.mak predates that commit then it will have incorrectly not been recreated and so your build will still try to build ivshmem.c.

You can check whether this is true by looking at (for instance) x86_64-softmmu/config-devices.mak in your build
tree: if it has a line "CONFIG_IVSHMEM=$(CONFIG_POSIX)" in it then it's the out of date version.

$ grep "CONFIG_IVSHMEM" x86_64-softmmu/config-devices.mak
CONFIG_IVSHMEM=$(CONFIG_EVENTFD)

I have been re-configuring on every build, just to eliminate that potential error source.

Quoted 4 lines

You should be able to fix this by deleting */config-devices.mak from your build tree (or by blowing away the build tree entirely and recreating it.) Then try rebuilding -- ivshmem.c should not be compiled.

I'll give that a go anyway.

#11 lore raw

Re: [Qemu-devel] [PATCH 2/2] Remove unnecessary CONFIG_EVENTFD preprocessor conditional to satisfy link

Quoted 22 lines · Christopher Friedt
Quoted 11 lines · Peter Maydell

[ccing somebody else who ran into this, since I've figured out why.]

Quoted 2 lines · Christopher Friedt

The file ivshmem.c unconditionally references event_notifier_init_fd() in util/event_notifier-posix.c, even if CONFIG_EVENTFD is not defined.

Yes, but ivshmem.c is only built if CONFIG_IVSHMEM, and CONFIG_IVSHMEM is set (in default-configs/pci.mak) to CONFIG_EVENTFD. So if CONFIG_EVENTFD is not defined then we should never build ivshmem.o.

The problem here is that you've run into a bug in QEMU's makefiles,

That would explain things.

Quoted 7 lines

where a change in an included .mak file in default-configs fails to cause the config-devices.mak file to be rebuilt. In commit 330b583 we changed pci.mak so that CONFIG_IVSHMEM is set to CONFIG_EVENTFD rather than CONFIG_POSIX, so if your config-devices.mak predates that commit then it will have incorrectly not been recreated and so your build will still try to build ivshmem.c.

Quoted 7 lines
Quoted 4 lines

You can check whether this is true by looking at (for instance) x86_64-softmmu/config-devices.mak in your build
tree: if it has a line "CONFIG_IVSHMEM=$(CONFIG_POSIX)" in it then it's the out of date version.

$ grep "CONFIG_IVSHMEM" x86_64-softmmu/config-devices.mak
CONFIG_IVSHMEM=$(CONFIG_EVENTFD)

Hmm. (Is it set the same for every config-devices.mak for every target you're trying to build?)

Next things to check: is CONFIG_EVENTFD definitely not set in config-host.mak (it shouldn't be)? hw/misc/Makefile.objs ought to say "obj-$(CONFIG_IVSHMEM) += ivshmem.o", and that should mean that we don't try to build it...

thanks
-- PMM

#12 lore raw

Re: [Qemu-devel] [PATCH 2/2] Remove unnecessary CONFIG_EVENTFD preprocessor conditional to satisfy link

Quoted 12 lines · Peter Maydell
Quoted 11 lines · Christopher Friedt
Quoted 10 lines · Peter Maydell

[ccing somebody else who ran into this, since I've figured out why.]

Quoted 2 lines · Christopher Friedt

The file ivshmem.c unconditionally references event_notifier_init_fd() in util/event_notifier-posix.c, even if CONFIG_EVENTFD is not defined.

Yes, but ivshmem.c is only built if CONFIG_IVSHMEM, and CONFIG_IVSHMEM is set (in default-configs/pci.mak) to CONFIG_EVENTFD. So if CONFIG_EVENTFD is not defined then we should never build ivshmem.o.

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

The problem here is that you've run into a bug in QEMU's makefiles,

That would explain things.

Hmm. (Is it set the same for every config-devices.mak for every target you're trying to build?)

Ack - too late! I manually removed all of the *-softmmu/ directories after doing a make clean, because they were still hanging around. Rebuilding worked fine without PATCH 2/2.

Thanks!

#13 lore raw

Re: [Qemu-devel] [PATCH 2/2] Remove unnecessary CONFIG_EVENTFD preprocessor conditional to satisfy link

Quoted 3 lines · Christopher Friedt

Ack - too late! I manually removed all of the *-softmmu/ directories after doing a make clean, because they were still hanging around. Rebuilding worked fine without PATCH 2/2.

Oh well. If blowing away the build dirs fixed it then it seems pretty likely that the makefile bug is what you were running into.

thanks
-- PMM

#7 lore raw

Re: [Qemu-devel] [PATCH 1/2] Use libtool instead of ar to create static libraries on Darwin.

Quoted 1 line · Michael Tokarev

Hmm. We removed libtool support just two moths ago... :)

That was GNU libtool, which is a completely different thing from OSX libtool. They just have an unhelpful name clash.

thanks
-- PMM