Patchwork [RFC,v2,3/6] Makefile: introduce common-obj-m and block-obj-m for DSO

login
register
mail settings
Submitter Fam Zheng
Date Sept. 6, 2013, 7:28 a.m.
Message ID <1378452491-20467-4-git-send-email-famz@redhat.com>
Download mbox | patch
Permalink /patch/273109/
State New
Headers show

Comments

Fam Zheng - Sept. 6, 2013, 7:28 a.m.
Add necessary rules and flags for shared object generation.
$(common-obj-m) will include $(block-obj-m), like $(common-obj-y) does
for $(block-obj-y). The new rules introduced here are:

0) For all %.so compiling:

    QEMU_CFLAGS += -shared -fPIC

1) %.o in $(common-obj-m) is compiled to %.o, with "QEMU_CFLAGS +=
-shared -fPIC". Then linked to %.so.

2) %.mo in $(common-obj-m) is the placeholder for %.so for pattern
matching in Makefile. It's linked to "-shared" with all its dependencies
(multiple *.o) as input. Which means the list of depended objects must
be ruled out in each sub-Makefile.objs with an variable:

    $(obj)/foo.mo-obj := $(addprefix $(obj)/,bar.o baz.o qux.o)

Notice that $(obj)/ is required for both target and dependency in the
rule. DSO suffix (.so) is configure variable (.dll for Windows).

Signed-off-by: Fam Zheng <famz@redhat.com>
---
 Makefile      | 32 +++++++++++++++++++++++++++++---
 Makefile.objs | 14 +++++++++++++-
 configure     |  3 +++
 rules.mak     | 10 ++++++++++
 4 files changed, 55 insertions(+), 4 deletions(-)
Paolo Bonzini - Sept. 6, 2013, 10:09 a.m.
Il 06/09/2013 09:28, Fam Zheng ha scritto:
> Add necessary rules and flags for shared object generation.
> $(common-obj-m) will include $(block-obj-m), like $(common-obj-y) does
> for $(block-obj-y). The new rules introduced here are:
> 
> 0) For all %.so compiling:
> 
>     QEMU_CFLAGS += -shared -fPIC
> 
> 1) %.o in $(common-obj-m) is compiled to %.o, with "QEMU_CFLAGS +=
> -shared -fPIC". Then linked to %.so.
> 
> 2) %.mo in $(common-obj-m) is the placeholder for %.so for pattern
> matching in Makefile. It's linked to "-shared" with all its dependencies
> (multiple *.o) as input. Which means the list of depended objects must
> be ruled out in each sub-Makefile.objs with an variable:
> 
>     $(obj)/foo.mo-obj := $(addprefix $(obj)/,bar.o baz.o qux.o)
> 
> Notice that $(obj)/ is required for both target and dependency in the
> rule. DSO suffix (.so) is configure variable (.dll for Windows).

Some kinks to iron, but this is really well-architected.  You took all
the best parts of mjt's patches and fixed almost all the ugly ones.  Kudos!

> Signed-off-by: Fam Zheng <famz@redhat.com>
> ---
>  Makefile      | 32 +++++++++++++++++++++++++++++---
>  Makefile.objs | 14 +++++++++++++-
>  configure     |  3 +++
>  rules.mak     | 10 ++++++++++
>  4 files changed, 55 insertions(+), 4 deletions(-)
> 
> diff --git a/Makefile b/Makefile
> index 806946e..cf47ea9 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -56,7 +56,7 @@ Makefile: ;
>  configure: ;
>  
>  .PHONY: all clean cscope distclean dvi html info install install-doc \
> -	pdf recurse-all speed test dist
> +	pdf recurse-all speed test dist modules
>  
>  $(call set-vpath, $(SRC_PATH))
>  
> @@ -121,7 +121,30 @@ ifeq ($(CONFIG_SMARTCARD_NSS),y)
>  include $(SRC_PATH)/libcacard/Makefile
>  endif
>  
> -all: $(DOCS) $(TOOLS) $(HELPERS-y) recurse-all
> +all: $(DOCS) $(TOOLS) $(HELPERS-y) recurse-all modules
> +
> +mod-obj-m = $(patsubst %.o,%$(DSOSUF),$(filter %.o,$(common-obj-m))) \
> +            $(patsubst %.mo,%$(DSOSUF),$(filter %.mo,$(common-obj-m)))


Why common-obj-m only, what about block-obj-m?

Perhaps adding to mod-obj-m (better name: modules-m) can be done in
unnest-vars?

> +# Generate rules for single file modules (%.so: %.o).
> +$(foreach o,$(filter %.o,$(common-obj-m)),$(eval \
> +	$(patsubst %.o,%.so,$o): $o ))

If you subst %.o to %.mo, you can use the same set of rules for both cases.

> +# For multi file modules, dependencies should be listed explicitly in
> +# Makefile.objs as
> +#     $(obj)/foo.mo-obj := $(obj)/bar.o $(obj)/biz.o

Why not

$(obj)/foo.mo: $(obj)/bar.o $(obj)/biz.o

?

> +$(foreach o,$(filter %.mo,$(mod-obj-m)),$(eval \
> +	$o: $($o-obj)))
> +
> +%.mo:
> +	$(if $(BUILD_DYNAMIC), \
> +		$(call quiet-command,$(CC) $(sort $^) -shared -o $@,"  LD[M] $(TARGET_DIR)$@"), \
> +		$(call quiet-command,$(AR) rcs $@ $(sort $^),"  AR    $(TARGET_DIR)$@"))

I think we can always build modules dynamically.  If the
module/no-module configure option decides whether an object moves
between *-obj-y and *-obj-m, statically-linked modules will just go on
the linker command line with no need for $(AR) or
--whole-archive/--no-whole-archive.

I'm missing where is the .so built (or I guess it can just be
hard-linked?) from the .mo file.

IIRC -shared is not portable to Darwin.  Darwin has separate file
formats for dynamic libraries (.dylib) and loadable modules (.so), so it
needs "-bundle" instad.  All this screams "just use libtool"...

> +
> +
> +modules: $(mod-obj-m)
> +modules: BUILD_DYNAMIC = 1
> +modules: QEMU_CFLAGS += -shared -fPIC

-shared is not needed here, only -fPIC.  Again, libtool would abstract
this nicely.  But it's fine if you prefer to have v3 still without
libtool and only working on ELF systems.

In fact, you probably don't need a special modules target.  You can just
add $(mod-obj-m) to "all".

>  
>  config-host.h: config-host.h-timestamp
>  config-host.h-timestamp: config-host.mak
> @@ -155,7 +178,7 @@ subdir-dtc:dtc/libfdt dtc/tests
>  dtc/%:
>  	mkdir -p $@
>  
> -$(SUBDIR_RULES): libqemuutil.a libqemustub.a $(common-obj-y)
> +$(SUBDIR_RULES): libqemuutil.a libqemustub.a $(common-obj-y) $(common-obj-m)
>  
>  ROMSUBDIR_RULES=$(patsubst %,romsubdir-%, $(ROMS))
>  romsubdir-%:
> @@ -235,6 +258,9 @@ clean:
>  	rm -f qemu-options.def
>  	find . -name '*.[oda]' -type f -exec rm -f {} +
>  	find . -name '*.l[oa]' -type f -exec rm -f {} +
> +	find . -name '*'$(DSOSUF) -type f -exec rm -f {} +
> +	find . -name '*.mo' -type f -exec rm -f {} +
> +
>  	rm -f $(TOOLS) $(HELPERS-y) qemu-ga TAGS cscope.* *.pod *~ */*~
>  	rm -Rf .libs
>  	rm -f qemu-img-cmds.h
> diff --git a/Makefile.objs b/Makefile.objs
> index f46a4cd..8984a20 100644
> --- a/Makefile.objs
> +++ b/Makefile.objs
> @@ -19,6 +19,8 @@ block-obj-y += qemu-coroutine.o qemu-coroutine-lock.o qemu-coroutine-io.o
>  block-obj-y += qemu-coroutine-sleep.o
>  block-obj-y += coroutine-$(CONFIG_COROUTINE_BACKEND).o
>  
> +block-obj-m = block/
> +
>  ifeq ($(CONFIG_VIRTIO)$(CONFIG_VIRTFS)$(CONFIG_PCI),yyy)
>  # Lots of the fsdev/9pcode is pulled in by vl.c via qemu_fsdev_add.
>  # only pull in the actual virtio-9p device if we also enabled virtio.
> @@ -83,6 +85,9 @@ common-obj-$(CONFIG_SMARTCARD_NSS) += $(libcacard-y)
>  
>  common-obj-y += qmp-marshal.o
>  common-obj-y += qmp.o hmp.o
> +
> +common-obj-m = $(block-obj-m)
> +
>  endif
>  
>  ######################################################################
> @@ -121,5 +126,12 @@ nested-vars += \
>  	util-obj-y \
>  	qga-obj-y \
>  	block-obj-y \
> -	common-obj-y
> +	block-obj-m \
> +	common-obj-y \
> +	common-obj-m
> +
>  dummy := $(call unnest-vars)
> +
> +# static linked mods are expanded to .o list
> +dummy := $(call expand-mod-obj,common-obj-y)
> +dummy := $(call expand-mod-obj,block-obj-y)
> diff --git a/configure b/configure
> index af6b048..75abb87 100755
> --- a/configure
> +++ b/configure
> @@ -190,6 +190,7 @@ mingw32="no"
>  gcov="no"
>  gcov_tool="gcov"
>  EXESUF=""
> +DSOSUF=".so"
>  prefix="/usr/local"
>  mandir="\${prefix}/share/man"
>  datadir="\${prefix}/share"
> @@ -584,6 +585,7 @@ fi
>  
>  if test "$mingw32" = "yes" ; then
>    EXESUF=".exe"
> +  DSOSUF=".dll"
>    QEMU_CFLAGS="-DWIN32_LEAN_AND_MEAN -DWINVER=0x501 $QEMU_CFLAGS"
>    # enable C99/POSIX format strings (needs mingw32-runtime 3.15 or later)
>    QEMU_CFLAGS="-D__USE_MINGW_ANSI_STDIO=1 $QEMU_CFLAGS"
> @@ -4175,6 +4177,7 @@ echo "LIBTOOLFLAGS=$LIBTOOLFLAGS" >> $config_host_mak
>  echo "LIBS+=$LIBS" >> $config_host_mak
>  echo "LIBS_TOOLS+=$libs_tools" >> $config_host_mak
>  echo "EXESUF=$EXESUF" >> $config_host_mak
> +echo "DSOSUF=$DSOSUF" >> $config_host_mak
>  echo "LIBS_QGA+=$libs_qga" >> $config_host_mak
>  echo "POD2MAN=$POD2MAN" >> $config_host_mak
>  echo "TRANSLATE_OPT_CFLAGS=$TRANSLATE_OPT_CFLAGS" >> $config_host_mak
> diff --git a/rules.mak b/rules.mak
> index e581d55..0a39499 100644
> --- a/rules.mak
> +++ b/rules.mak
> @@ -58,6 +58,10 @@ endif
>  %.o: %.dtrace
>  	$(call quiet-command,dtrace -o $@ -G -s $<, "  GEN   $(TARGET_DIR)$@")
>  
> +%$(DSOSUF): QEMU_CLFAGS += -shared -fPIC
> +%$(DSOSUF): %.o
> +	$(call quiet-command,$(LD) $< -o $@ -shared,"  LD[M] $(TARGET_DIR)$@")
> +

As mentioned above, I think these four lines are not needed and you can
always go through the "modules" or $(mod-obj-m) targets.  In fact,
CLFAGS sounds dubious. :)

Paolo

>  %$(EXESUF): %.o
>  	$(call LINK,$^)
>  
> @@ -145,3 +149,9 @@ $(shell mkdir -p $(sort $(foreach var,$(nested-vars),$(dir $($(var))))))
>  $(foreach var,$(nested-vars), $(eval \
>    -include $(addsuffix *.d, $(sort $(dir $($(var)))))))
>  endef
> +
> +define expand-mod-obj
> +$(eval pref = $(if $(obj-base),$(obj-base)/,))
> +$(eval t = $(foreach o,$($1),$(if $($(pref)$o-obj),$($(pref)$o-obj),$o)))
> +$(eval $1 = $t)
> +endef
>
Fam Zheng - Sept. 6, 2013, 11:47 a.m.
On Fri, 09/06 12:09, Paolo Bonzini wrote:
> Il 06/09/2013 09:28, Fam Zheng ha scritto:
> > Add necessary rules and flags for shared object generation.
> > $(common-obj-m) will include $(block-obj-m), like $(common-obj-y) does
> > for $(block-obj-y). The new rules introduced here are:
> > 
> > 0) For all %.so compiling:
> > 
> >     QEMU_CFLAGS += -shared -fPIC
> > 
> > 1) %.o in $(common-obj-m) is compiled to %.o, with "QEMU_CFLAGS +=
> > -shared -fPIC". Then linked to %.so.
> > 
> > 2) %.mo in $(common-obj-m) is the placeholder for %.so for pattern
> > matching in Makefile. It's linked to "-shared" with all its dependencies
> > (multiple *.o) as input. Which means the list of depended objects must
> > be ruled out in each sub-Makefile.objs with an variable:
> > 
> >     $(obj)/foo.mo-obj := $(addprefix $(obj)/,bar.o baz.o qux.o)
> > 
> > Notice that $(obj)/ is required for both target and dependency in the
> > rule. DSO suffix (.so) is configure variable (.dll for Windows).
> 
> Some kinks to iron, but this is really well-architected.  You took all
> the best parts of mjt's patches and fixed almost all the ugly ones.  Kudos!
> 
> > Signed-off-by: Fam Zheng <famz@redhat.com>
> > ---
> >  Makefile      | 32 +++++++++++++++++++++++++++++---
> >  Makefile.objs | 14 +++++++++++++-
> >  configure     |  3 +++
> >  rules.mak     | 10 ++++++++++
> >  4 files changed, 55 insertions(+), 4 deletions(-)
> > 
> > diff --git a/Makefile b/Makefile
> > index 806946e..cf47ea9 100644
> > --- a/Makefile
> > +++ b/Makefile
> > @@ -56,7 +56,7 @@ Makefile: ;
> >  configure: ;
> >  
> >  .PHONY: all clean cscope distclean dvi html info install install-doc \
> > -	pdf recurse-all speed test dist
> > +	pdf recurse-all speed test dist modules
> >  
> >  $(call set-vpath, $(SRC_PATH))
> >  
> > @@ -121,7 +121,30 @@ ifeq ($(CONFIG_SMARTCARD_NSS),y)
> >  include $(SRC_PATH)/libcacard/Makefile
> >  endif
> >  
> > -all: $(DOCS) $(TOOLS) $(HELPERS-y) recurse-all
> > +all: $(DOCS) $(TOOLS) $(HELPERS-y) recurse-all modules
> > +
> > +mod-obj-m = $(patsubst %.o,%$(DSOSUF),$(filter %.o,$(common-obj-m))) \
> > +            $(patsubst %.mo,%$(DSOSUF),$(filter %.mo,$(common-obj-m)))
> 
> 
> Why common-obj-m only, what about block-obj-m?
> 
Because common-obj-m = $(block-obj-m), in Makefile.objs below.

> Perhaps adding to mod-obj-m (better name: modules-m) can be done in
> unnest-vars?

It looks more straightforward for me to do here, and it's used very locally.

> 
> > +# Generate rules for single file modules (%.so: %.o).
> > +$(foreach o,$(filter %.o,$(common-obj-m)),$(eval \
> > +	$(patsubst %.o,%.so,$o): $o ))
> 
> If you subst %.o to %.mo, you can use the same set of rules for both cases.
> 
> > +# For multi file modules, dependencies should be listed explicitly in
> > +# Makefile.objs as
> > +#     $(obj)/foo.mo-obj := $(obj)/bar.o $(obj)/biz.o
> 
> Why not
> 
> $(obj)/foo.mo: $(obj)/bar.o $(obj)/biz.o
> 
> ?
> 
Because I don't know how to expand foo.mo to bar.o and biz.o: I expand "foo.mo"
to "bar.o biz.o" in the link command line, to skip creating to foo.mo, or
libfoo.a, if it's to be linked static:

    block-obj-y += foo.mo

That's for avoiding ar and ln -r.

> > +$(foreach o,$(filter %.mo,$(mod-obj-m)),$(eval \
> > +	$o: $($o-obj)))
> > +
> > +%.mo:
> > +	$(if $(BUILD_DYNAMIC), \
> > +		$(call quiet-command,$(CC) $(sort $^) -shared -o $@,"  LD[M] $(TARGET_DIR)$@"), \
> > +		$(call quiet-command,$(AR) rcs $@ $(sort $^),"  AR    $(TARGET_DIR)$@"))
> 
> I think we can always build modules dynamically.  If the
> module/no-module configure option decides whether an object moves
> between *-obj-y and *-obj-m, statically-linked modules will just go on
> the linker command line with no need for $(AR) or
> --whole-archive/--no-whole-archive.
> 
I don't understand, do you mean a "cc -shared" output can be statically linked
into a executable?

> I'm missing where is the .so built (or I guess it can just be
> hard-linked?) from the .mo file.
> 
> IIRC -shared is not portable to Darwin.  Darwin has separate file
> formats for dynamic libraries (.dylib) and loadable modules (.so), so it
> needs "-bundle" instad.  All this screams "just use libtool"...
> 
> > +
> > +
> > +modules: $(mod-obj-m)
> > +modules: BUILD_DYNAMIC = 1
> > +modules: QEMU_CFLAGS += -shared -fPIC
> 
> -shared is not needed here, only -fPIC.  Again, libtool would abstract
> this nicely.  But it's fine if you prefer to have v3 still without
> libtool and only working on ELF systems.
> 
> In fact, you probably don't need a special modules target.  You can just
> add $(mod-obj-m) to "all".
> 
> >  
> >  config-host.h: config-host.h-timestamp
> >  config-host.h-timestamp: config-host.mak
> > @@ -155,7 +178,7 @@ subdir-dtc:dtc/libfdt dtc/tests
> >  dtc/%:
> >  	mkdir -p $@
> >  
> > -$(SUBDIR_RULES): libqemuutil.a libqemustub.a $(common-obj-y)
> > +$(SUBDIR_RULES): libqemuutil.a libqemustub.a $(common-obj-y) $(common-obj-m)
> >  
> >  ROMSUBDIR_RULES=$(patsubst %,romsubdir-%, $(ROMS))
> >  romsubdir-%:
> > @@ -235,6 +258,9 @@ clean:
> >  	rm -f qemu-options.def
> >  	find . -name '*.[oda]' -type f -exec rm -f {} +
> >  	find . -name '*.l[oa]' -type f -exec rm -f {} +
> > +	find . -name '*'$(DSOSUF) -type f -exec rm -f {} +
> > +	find . -name '*.mo' -type f -exec rm -f {} +
> > +
> >  	rm -f $(TOOLS) $(HELPERS-y) qemu-ga TAGS cscope.* *.pod *~ */*~
> >  	rm -Rf .libs
> >  	rm -f qemu-img-cmds.h
> > diff --git a/Makefile.objs b/Makefile.objs
> > index f46a4cd..8984a20 100644
> > --- a/Makefile.objs
> > +++ b/Makefile.objs
> > @@ -19,6 +19,8 @@ block-obj-y += qemu-coroutine.o qemu-coroutine-lock.o qemu-coroutine-io.o
> >  block-obj-y += qemu-coroutine-sleep.o
> >  block-obj-y += coroutine-$(CONFIG_COROUTINE_BACKEND).o
> >  
> > +block-obj-m = block/
> > +
> >  ifeq ($(CONFIG_VIRTIO)$(CONFIG_VIRTFS)$(CONFIG_PCI),yyy)
> >  # Lots of the fsdev/9pcode is pulled in by vl.c via qemu_fsdev_add.
> >  # only pull in the actual virtio-9p device if we also enabled virtio.
> > @@ -83,6 +85,9 @@ common-obj-$(CONFIG_SMARTCARD_NSS) += $(libcacard-y)
> >  
> >  common-obj-y += qmp-marshal.o
> >  common-obj-y += qmp.o hmp.o
> > +
> > +common-obj-m = $(block-obj-m)
> > +
> >  endif
> >  
> >  ######################################################################
> > @@ -121,5 +126,12 @@ nested-vars += \
> >  	util-obj-y \
> >  	qga-obj-y \
> >  	block-obj-y \
> > -	common-obj-y
> > +	block-obj-m \
> > +	common-obj-y \
> > +	common-obj-m
> > +
> >  dummy := $(call unnest-vars)
> > +
> > +# static linked mods are expanded to .o list
> > +dummy := $(call expand-mod-obj,common-obj-y)
> > +dummy := $(call expand-mod-obj,block-obj-y)
> > diff --git a/configure b/configure
> > index af6b048..75abb87 100755
> > --- a/configure
> > +++ b/configure
> > @@ -190,6 +190,7 @@ mingw32="no"
> >  gcov="no"
> >  gcov_tool="gcov"
> >  EXESUF=""
> > +DSOSUF=".so"
> >  prefix="/usr/local"
> >  mandir="\${prefix}/share/man"
> >  datadir="\${prefix}/share"
> > @@ -584,6 +585,7 @@ fi
> >  
> >  if test "$mingw32" = "yes" ; then
> >    EXESUF=".exe"
> > +  DSOSUF=".dll"
> >    QEMU_CFLAGS="-DWIN32_LEAN_AND_MEAN -DWINVER=0x501 $QEMU_CFLAGS"
> >    # enable C99/POSIX format strings (needs mingw32-runtime 3.15 or later)
> >    QEMU_CFLAGS="-D__USE_MINGW_ANSI_STDIO=1 $QEMU_CFLAGS"
> > @@ -4175,6 +4177,7 @@ echo "LIBTOOLFLAGS=$LIBTOOLFLAGS" >> $config_host_mak
> >  echo "LIBS+=$LIBS" >> $config_host_mak
> >  echo "LIBS_TOOLS+=$libs_tools" >> $config_host_mak
> >  echo "EXESUF=$EXESUF" >> $config_host_mak
> > +echo "DSOSUF=$DSOSUF" >> $config_host_mak
> >  echo "LIBS_QGA+=$libs_qga" >> $config_host_mak
> >  echo "POD2MAN=$POD2MAN" >> $config_host_mak
> >  echo "TRANSLATE_OPT_CFLAGS=$TRANSLATE_OPT_CFLAGS" >> $config_host_mak
> > diff --git a/rules.mak b/rules.mak
> > index e581d55..0a39499 100644
> > --- a/rules.mak
> > +++ b/rules.mak
> > @@ -58,6 +58,10 @@ endif
> >  %.o: %.dtrace
> >  	$(call quiet-command,dtrace -o $@ -G -s $<, "  GEN   $(TARGET_DIR)$@")
> >  
> > +%$(DSOSUF): QEMU_CLFAGS += -shared -fPIC
> > +%$(DSOSUF): %.o
> > +	$(call quiet-command,$(LD) $< -o $@ -shared,"  LD[M] $(TARGET_DIR)$@")
> > +
> 
> As mentioned above, I think these four lines are not needed and you can
> always go through the "modules" or $(mod-obj-m) targets.  In fact,
> CLFAGS sounds dubious. :)
> 
> Paolo
> 
> >  %$(EXESUF): %.o
> >  	$(call LINK,$^)
> >  
> > @@ -145,3 +149,9 @@ $(shell mkdir -p $(sort $(foreach var,$(nested-vars),$(dir $($(var))))))
> >  $(foreach var,$(nested-vars), $(eval \
> >    -include $(addsuffix *.d, $(sort $(dir $($(var)))))))
> >  endef
> > +
> > +define expand-mod-obj
> > +$(eval pref = $(if $(obj-base),$(obj-base)/,))
> > +$(eval t = $(foreach o,$($1),$(if $($(pref)$o-obj),$($(pref)$o-obj),$o)))
> > +$(eval $1 = $t)
> > +endef
> > 
>
Paolo Bonzini - Sept. 6, 2013, 12:11 p.m.
Il 06/09/2013 13:47, Fam Zheng ha scritto:
> On Fri, 09/06 12:09, Paolo Bonzini wrote:
>> Il 06/09/2013 09:28, Fam Zheng ha scritto:
>>> Add necessary rules and flags for shared object generation.
>>> $(common-obj-m) will include $(block-obj-m), like $(common-obj-y) does
>>> for $(block-obj-y). The new rules introduced here are:
>>>
>>> 0) For all %.so compiling:
>>>
>>>     QEMU_CFLAGS += -shared -fPIC
>>>
>>> 1) %.o in $(common-obj-m) is compiled to %.o, with "QEMU_CFLAGS +=
>>> -shared -fPIC". Then linked to %.so.
>>>
>>> 2) %.mo in $(common-obj-m) is the placeholder for %.so for pattern
>>> matching in Makefile. It's linked to "-shared" with all its dependencies
>>> (multiple *.o) as input. Which means the list of depended objects must
>>> be ruled out in each sub-Makefile.objs with an variable:
>>>
>>>     $(obj)/foo.mo-obj := $(addprefix $(obj)/,bar.o baz.o qux.o)
>>>
>>> Notice that $(obj)/ is required for both target and dependency in the
>>> rule. DSO suffix (.so) is configure variable (.dll for Windows).
>>
>> Some kinks to iron, but this is really well-architected.  You took all
>> the best parts of mjt's patches and fixed almost all the ugly ones.  Kudos!
>>
>>> Signed-off-by: Fam Zheng <famz@redhat.com>
>>> ---
>>>  Makefile      | 32 +++++++++++++++++++++++++++++---
>>>  Makefile.objs | 14 +++++++++++++-
>>>  configure     |  3 +++
>>>  rules.mak     | 10 ++++++++++
>>>  4 files changed, 55 insertions(+), 4 deletions(-)
>>>
>>> diff --git a/Makefile b/Makefile
>>> index 806946e..cf47ea9 100644
>>> --- a/Makefile
>>> +++ b/Makefile
>>> @@ -56,7 +56,7 @@ Makefile: ;
>>>  configure: ;
>>>  
>>>  .PHONY: all clean cscope distclean dvi html info install install-doc \
>>> -	pdf recurse-all speed test dist
>>> +	pdf recurse-all speed test dist modules
>>>  
>>>  $(call set-vpath, $(SRC_PATH))
>>>  
>>> @@ -121,7 +121,30 @@ ifeq ($(CONFIG_SMARTCARD_NSS),y)
>>>  include $(SRC_PATH)/libcacard/Makefile
>>>  endif
>>>  
>>> -all: $(DOCS) $(TOOLS) $(HELPERS-y) recurse-all
>>> +all: $(DOCS) $(TOOLS) $(HELPERS-y) recurse-all modules
>>> +
>>> +mod-obj-m = $(patsubst %.o,%$(DSOSUF),$(filter %.o,$(common-obj-m))) \
>>> +            $(patsubst %.mo,%$(DSOSUF),$(filter %.mo,$(common-obj-m)))
>>
>>
>> Why common-obj-m only, what about block-obj-m?
>>
> Because common-obj-m = $(block-obj-m), in Makefile.objs below.
> 
>> Perhaps adding to mod-obj-m (better name: modules-m) can be done in
>> unnest-vars?
> 
> It looks more straightforward for me to do here, and it's used very locally.

But it doesn't scale too well, does it?  There's no particular reason 
why all -m variables would be included in common-obj-m.  In fact, block-
obj-y/block-obj-m are the only remaining case of one variable including 
another.  In another review I even proposed eliminating this exception.

>>> +# Generate rules for single file modules (%.so: %.o).
>>> +$(foreach o,$(filter %.o,$(common-obj-m)),$(eval \
>>> +	$(patsubst %.o,%.so,$o): $o ))
>>
>> If you subst %.o to %.mo, you can use the same set of rules for both cases.
>>
>>> +# For multi file modules, dependencies should be listed explicitly in
>>> +# Makefile.objs as
>>> +#     $(obj)/foo.mo-obj := $(obj)/bar.o $(obj)/biz.o
>>
>> Why not
>>
>> $(obj)/foo.mo: $(obj)/bar.o $(obj)/biz.o
>>
>> ?
>>
> Because I don't know how to expand foo.mo to bar.o and biz.o: I expand "foo.mo"
> to "bar.o biz.o" in the link command line, to skip creating to foo.mo, or
> libfoo.a, if it's to be linked static:
> 
>     block-obj-y += foo.mo
> 
> That's for avoiding ar and ln -r.

Ah, I see now.  See below for an idea (if it works).

>>> +$(foreach o,$(filter %.mo,$(mod-obj-m)),$(eval \
>>> +	$o: $($o-obj)))
>>> +
>>> +%.mo:
>>> +	$(if $(BUILD_DYNAMIC), \
>>> +		$(call quiet-command,$(CC) $(sort $^) -shared -o $@,"  LD[M] $(TARGET_DIR)$@"), \
>>> +		$(call quiet-command,$(AR) rcs $@ $(sort $^),"  AR    $(TARGET_DIR)$@"))
>>
>> I think we can always build modules dynamically.  If the
>> module/no-module configure option decides whether an object moves
>> between *-obj-y and *-obj-m, statically-linked modules will just go on
>> the linker command line with no need for $(AR) or
>> --whole-archive/--no-whole-archive.
>>
> I don't understand, do you mean a "cc -shared" output can be statically linked
> into a executable?

So if I understand correctly the .mo file is never used for a 
statically-linked module?  So the "ar" doesn't matter as things stand

Perhaps you could just use .mo file as a placeholder that holds the
module's list of dependencies, no matter if it is shared or static:

%.mo:
	$(call quiet-command,echo $(sort $^) > $@,"  GEN    $(TARGET_DIR_$@"))

In the LINK rule include the list from all .mo files:

	$(sort $(filter %.o, $1)) \
	$(shell cat $(filter %.mo,$^)) \
	$(filter-out %.o %.mo,$^))

And the rule for shared objects would be simply:

%.$(DSOSUF): QEMU_LDFLAGS += -shared
%.$(DSOSUF): %.mo
	$(call LINK,$^)

This would remove the need for the $i-obj variables.

Paolo
Fam Zheng - Sept. 9, 2013, 2:26 a.m.
On Fri, 09/06 14:11, Paolo Bonzini wrote:
> Il 06/09/2013 13:47, Fam Zheng ha scritto:
> > On Fri, 09/06 12:09, Paolo Bonzini wrote:
> >> Il 06/09/2013 09:28, Fam Zheng ha scritto:
> >>> Add necessary rules and flags for shared object generation.
> >>> $(common-obj-m) will include $(block-obj-m), like $(common-obj-y) does
> >>> for $(block-obj-y). The new rules introduced here are:
> >>>
> >>> 0) For all %.so compiling:
> >>>
> >>>     QEMU_CFLAGS += -shared -fPIC
> >>>
> >>> 1) %.o in $(common-obj-m) is compiled to %.o, with "QEMU_CFLAGS +=
> >>> -shared -fPIC". Then linked to %.so.
> >>>
> >>> 2) %.mo in $(common-obj-m) is the placeholder for %.so for pattern
> >>> matching in Makefile. It's linked to "-shared" with all its dependencies
> >>> (multiple *.o) as input. Which means the list of depended objects must
> >>> be ruled out in each sub-Makefile.objs with an variable:
> >>>
> >>>     $(obj)/foo.mo-obj := $(addprefix $(obj)/,bar.o baz.o qux.o)
> >>>
> >>> Notice that $(obj)/ is required for both target and dependency in the
> >>> rule. DSO suffix (.so) is configure variable (.dll for Windows).
> >>
> >> Some kinks to iron, but this is really well-architected.  You took all
> >> the best parts of mjt's patches and fixed almost all the ugly ones.  Kudos!
> >>
> >>> Signed-off-by: Fam Zheng <famz@redhat.com>
> >>> ---
> >>>  Makefile      | 32 +++++++++++++++++++++++++++++---
> >>>  Makefile.objs | 14 +++++++++++++-
> >>>  configure     |  3 +++
> >>>  rules.mak     | 10 ++++++++++
> >>>  4 files changed, 55 insertions(+), 4 deletions(-)
> >>>
> >>> diff --git a/Makefile b/Makefile
> >>> index 806946e..cf47ea9 100644
> >>> --- a/Makefile
> >>> +++ b/Makefile
> >>> @@ -56,7 +56,7 @@ Makefile: ;
> >>>  configure: ;
> >>>  
> >>>  .PHONY: all clean cscope distclean dvi html info install install-doc \
> >>> -	pdf recurse-all speed test dist
> >>> +	pdf recurse-all speed test dist modules
> >>>  
> >>>  $(call set-vpath, $(SRC_PATH))
> >>>  
> >>> @@ -121,7 +121,30 @@ ifeq ($(CONFIG_SMARTCARD_NSS),y)
> >>>  include $(SRC_PATH)/libcacard/Makefile
> >>>  endif
> >>>  
> >>> -all: $(DOCS) $(TOOLS) $(HELPERS-y) recurse-all
> >>> +all: $(DOCS) $(TOOLS) $(HELPERS-y) recurse-all modules
> >>> +
> >>> +mod-obj-m = $(patsubst %.o,%$(DSOSUF),$(filter %.o,$(common-obj-m))) \
> >>> +            $(patsubst %.mo,%$(DSOSUF),$(filter %.mo,$(common-obj-m)))
> >>
> >>
> >> Why common-obj-m only, what about block-obj-m?
> >>
> > Because common-obj-m = $(block-obj-m), in Makefile.objs below.
> > 
> >> Perhaps adding to mod-obj-m (better name: modules-m) can be done in
> >> unnest-vars?
> > 
> > It looks more straightforward for me to do here, and it's used very locally.
> 
> But it doesn't scale too well, does it?  There's no particular reason 
> why all -m variables would be included in common-obj-m.  In fact, block-
> obj-y/block-obj-m are the only remaining case of one variable including 
> another.  In another review I even proposed eliminating this exception.
> 
> >>> +# Generate rules for single file modules (%.so: %.o).
> >>> +$(foreach o,$(filter %.o,$(common-obj-m)),$(eval \
> >>> +	$(patsubst %.o,%.so,$o): $o ))
> >>
> >> If you subst %.o to %.mo, you can use the same set of rules for both cases.
> >>
> >>> +# For multi file modules, dependencies should be listed explicitly in
> >>> +# Makefile.objs as
> >>> +#     $(obj)/foo.mo-obj := $(obj)/bar.o $(obj)/biz.o
> >>
> >> Why not
> >>
> >> $(obj)/foo.mo: $(obj)/bar.o $(obj)/biz.o
> >>
> >> ?
> >>
> > Because I don't know how to expand foo.mo to bar.o and biz.o: I expand "foo.mo"
> > to "bar.o biz.o" in the link command line, to skip creating to foo.mo, or
> > libfoo.a, if it's to be linked static:
> > 
> >     block-obj-y += foo.mo
> > 
> > That's for avoiding ar and ln -r.
> 
> Ah, I see now.  See below for an idea (if it works).
> 
> >>> +$(foreach o,$(filter %.mo,$(mod-obj-m)),$(eval \
> >>> +	$o: $($o-obj)))
> >>> +
> >>> +%.mo:
> >>> +	$(if $(BUILD_DYNAMIC), \
> >>> +		$(call quiet-command,$(CC) $(sort $^) -shared -o $@,"  LD[M] $(TARGET_DIR)$@"), \
> >>> +		$(call quiet-command,$(AR) rcs $@ $(sort $^),"  AR    $(TARGET_DIR)$@"))
> >>
> >> I think we can always build modules dynamically.  If the
> >> module/no-module configure option decides whether an object moves
> >> between *-obj-y and *-obj-m, statically-linked modules will just go on
> >> the linker command line with no need for $(AR) or
> >> --whole-archive/--no-whole-archive.
> >>
> > I don't understand, do you mean a "cc -shared" output can be statically linked
> > into a executable?
> 
> So if I understand correctly the .mo file is never used for a 
> statically-linked module?  So the "ar" doesn't matter as things stand
> 
> Perhaps you could just use .mo file as a placeholder that holds the
> module's list of dependencies, no matter if it is shared or static:
> 
> %.mo:
> 	$(call quiet-command,echo $(sort $^) > $@,"  GEN    $(TARGET_DIR_$@"))
> 
> In the LINK rule include the list from all .mo files:
> 
> 	$(sort $(filter %.o, $1)) \
> 	$(shell cat $(filter %.mo,$^)) \
> 	$(filter-out %.o %.mo,$^))
> 
> And the rule for shared objects would be simply:
> 
> %.$(DSOSUF): QEMU_LDFLAGS += -shared
> %.$(DSOSUF): %.mo
> 	$(call LINK,$^)
> 
> This would remove the need for the $i-obj variables.
> 
Actually I would like a try to keep $i-{obj,cflags,libs}:

    foo-obj := bar.o biz.o
    foo-libs := $(FOO_LIBS)
    foo-cflags := $(FOO_CFLAGS)

getting rid of all the $(obj)/'s and expand them in the toplevel unnest magic
(because we know foo.mo is contained in local $(block-obj-y) so it's all
trackable).

If this idea can't work out, I'll use your suggested way.

Thanks,

Fam
Paolo Bonzini - Sept. 9, 2013, 10:43 a.m.
Il 09/09/2013 04:26, Fam Zheng ha scritto:
> Actually I would like a try to keep $i-{obj,cflags,libs}:
> 
>     foo-obj := bar.o biz.o
>     foo-libs := $(FOO_LIBS)
>     foo-cflags := $(FOO_CFLAGS)
> 
> getting rid of all the $(obj)/'s and expand them in the toplevel unnest magic
> (because we know foo.mo is contained in local $(block-obj-y) so it's all
> trackable).
> 
> If this idea can't work out, I'll use your suggested way.

Yes, either way works (i.e. as long as it's consistent).

Paolo

Patch

diff --git a/Makefile b/Makefile
index 806946e..cf47ea9 100644
--- a/Makefile
+++ b/Makefile
@@ -56,7 +56,7 @@  Makefile: ;
 configure: ;
 
 .PHONY: all clean cscope distclean dvi html info install install-doc \
-	pdf recurse-all speed test dist
+	pdf recurse-all speed test dist modules
 
 $(call set-vpath, $(SRC_PATH))
 
@@ -121,7 +121,30 @@  ifeq ($(CONFIG_SMARTCARD_NSS),y)
 include $(SRC_PATH)/libcacard/Makefile
 endif
 
-all: $(DOCS) $(TOOLS) $(HELPERS-y) recurse-all
+all: $(DOCS) $(TOOLS) $(HELPERS-y) recurse-all modules
+
+mod-obj-m = $(patsubst %.o,%$(DSOSUF),$(filter %.o,$(common-obj-m))) \
+            $(patsubst %.mo,%$(DSOSUF),$(filter %.mo,$(common-obj-m)))
+
+# Generate rules for single file modules (%.so: %.o).
+$(foreach o,$(filter %.o,$(common-obj-m)),$(eval \
+	$(patsubst %.o,%.so,$o): $o ))
+
+# For multi file modules, dependencies should be listed explicitly in
+# Makefile.objs as
+#     $(obj)/foo.mo-obj := $(obj)/bar.o $(obj)/biz.o
+$(foreach o,$(filter %.mo,$(mod-obj-m)),$(eval \
+	$o: $($o-obj)))
+
+%.mo:
+	$(if $(BUILD_DYNAMIC), \
+		$(call quiet-command,$(CC) $(sort $^) -shared -o $@,"  LD[M] $(TARGET_DIR)$@"), \
+		$(call quiet-command,$(AR) rcs $@ $(sort $^),"  AR    $(TARGET_DIR)$@"))
+
+
+modules: $(mod-obj-m)
+modules: BUILD_DYNAMIC = 1
+modules: QEMU_CFLAGS += -shared -fPIC
 
 config-host.h: config-host.h-timestamp
 config-host.h-timestamp: config-host.mak
@@ -155,7 +178,7 @@  subdir-dtc:dtc/libfdt dtc/tests
 dtc/%:
 	mkdir -p $@
 
-$(SUBDIR_RULES): libqemuutil.a libqemustub.a $(common-obj-y)
+$(SUBDIR_RULES): libqemuutil.a libqemustub.a $(common-obj-y) $(common-obj-m)
 
 ROMSUBDIR_RULES=$(patsubst %,romsubdir-%, $(ROMS))
 romsubdir-%:
@@ -235,6 +258,9 @@  clean:
 	rm -f qemu-options.def
 	find . -name '*.[oda]' -type f -exec rm -f {} +
 	find . -name '*.l[oa]' -type f -exec rm -f {} +
+	find . -name '*'$(DSOSUF) -type f -exec rm -f {} +
+	find . -name '*.mo' -type f -exec rm -f {} +
+
 	rm -f $(TOOLS) $(HELPERS-y) qemu-ga TAGS cscope.* *.pod *~ */*~
 	rm -Rf .libs
 	rm -f qemu-img-cmds.h
diff --git a/Makefile.objs b/Makefile.objs
index f46a4cd..8984a20 100644
--- a/Makefile.objs
+++ b/Makefile.objs
@@ -19,6 +19,8 @@  block-obj-y += qemu-coroutine.o qemu-coroutine-lock.o qemu-coroutine-io.o
 block-obj-y += qemu-coroutine-sleep.o
 block-obj-y += coroutine-$(CONFIG_COROUTINE_BACKEND).o
 
+block-obj-m = block/
+
 ifeq ($(CONFIG_VIRTIO)$(CONFIG_VIRTFS)$(CONFIG_PCI),yyy)
 # Lots of the fsdev/9pcode is pulled in by vl.c via qemu_fsdev_add.
 # only pull in the actual virtio-9p device if we also enabled virtio.
@@ -83,6 +85,9 @@  common-obj-$(CONFIG_SMARTCARD_NSS) += $(libcacard-y)
 
 common-obj-y += qmp-marshal.o
 common-obj-y += qmp.o hmp.o
+
+common-obj-m = $(block-obj-m)
+
 endif
 
 ######################################################################
@@ -121,5 +126,12 @@  nested-vars += \
 	util-obj-y \
 	qga-obj-y \
 	block-obj-y \
-	common-obj-y
+	block-obj-m \
+	common-obj-y \
+	common-obj-m
+
 dummy := $(call unnest-vars)
+
+# static linked mods are expanded to .o list
+dummy := $(call expand-mod-obj,common-obj-y)
+dummy := $(call expand-mod-obj,block-obj-y)
diff --git a/configure b/configure
index af6b048..75abb87 100755
--- a/configure
+++ b/configure
@@ -190,6 +190,7 @@  mingw32="no"
 gcov="no"
 gcov_tool="gcov"
 EXESUF=""
+DSOSUF=".so"
 prefix="/usr/local"
 mandir="\${prefix}/share/man"
 datadir="\${prefix}/share"
@@ -584,6 +585,7 @@  fi
 
 if test "$mingw32" = "yes" ; then
   EXESUF=".exe"
+  DSOSUF=".dll"
   QEMU_CFLAGS="-DWIN32_LEAN_AND_MEAN -DWINVER=0x501 $QEMU_CFLAGS"
   # enable C99/POSIX format strings (needs mingw32-runtime 3.15 or later)
   QEMU_CFLAGS="-D__USE_MINGW_ANSI_STDIO=1 $QEMU_CFLAGS"
@@ -4175,6 +4177,7 @@  echo "LIBTOOLFLAGS=$LIBTOOLFLAGS" >> $config_host_mak
 echo "LIBS+=$LIBS" >> $config_host_mak
 echo "LIBS_TOOLS+=$libs_tools" >> $config_host_mak
 echo "EXESUF=$EXESUF" >> $config_host_mak
+echo "DSOSUF=$DSOSUF" >> $config_host_mak
 echo "LIBS_QGA+=$libs_qga" >> $config_host_mak
 echo "POD2MAN=$POD2MAN" >> $config_host_mak
 echo "TRANSLATE_OPT_CFLAGS=$TRANSLATE_OPT_CFLAGS" >> $config_host_mak
diff --git a/rules.mak b/rules.mak
index e581d55..0a39499 100644
--- a/rules.mak
+++ b/rules.mak
@@ -58,6 +58,10 @@  endif
 %.o: %.dtrace
 	$(call quiet-command,dtrace -o $@ -G -s $<, "  GEN   $(TARGET_DIR)$@")
 
+%$(DSOSUF): QEMU_CLFAGS += -shared -fPIC
+%$(DSOSUF): %.o
+	$(call quiet-command,$(LD) $< -o $@ -shared,"  LD[M] $(TARGET_DIR)$@")
+
 %$(EXESUF): %.o
 	$(call LINK,$^)
 
@@ -145,3 +149,9 @@  $(shell mkdir -p $(sort $(foreach var,$(nested-vars),$(dir $($(var))))))
 $(foreach var,$(nested-vars), $(eval \
   -include $(addsuffix *.d, $(sort $(dir $($(var)))))))
 endef
+
+define expand-mod-obj
+$(eval pref = $(if $(obj-base),$(obj-base)/,))
+$(eval t = $(foreach o,$($1),$(if $($(pref)$o-obj),$($(pref)$o-obj),$o)))
+$(eval $1 = $t)
+endef