#  coq.moo.opt: library/legacy/coq.ma matitac.opt
 #          ./matitac.opt $(MATITA_FLAGS) $<
 
-ifeq ($(HAVE_OCAMLOPT),yes)
-
 CMXS = $(patsubst %.cmo,%.cmx,$(CMOS))
 CCMXS = $(patsubst %.cmo,%.cmx,$(CCMOS))
 MAINCMXS = $(patsubst %.cmo,%.cmx,$(MAINCMOS))
 LIBX_DEPS := $(shell $(OCAMLFIND) query -recursive -predicates "native" -format "%d/%a" $(MATITA_REQUIRES))
 CLIB_DEPS := $(shell $(OCAMLFIND) query -recursive -predicates "byte" -format "%d/%a" $(MATITA_CREQUIRES))
 CLIBX_DEPS := $(shell $(OCAMLFIND) query -recursive -predicates "native" -format "%d/%a" $(MATITA_CREQUIRES))
-.PHONY: opt
-opt: $(PROGRAMS_OPT) coq.moo.opt
-.PHONY: upx
-upx: $(PROGRAMS_UPX) coq.moo.opt
+opt: $(PROGRAMS_OPT)
+upx: $(PROGRAMS_UPX)
+.PHONY: opt upx
 
+ifeq ($(HAVE_OCAMLOPT),yes)
+world: all opt
 else
-
-opt:
-       @echo "Native code compilation is disabled"
-
+world: all
 endif
 
 matita: matita.ml $(LIB_DEPS) $(CMOS)
                $(PROGRAMS_UPX) \
                $(NULL)
 
+.PHONY: distclean
+distclean: clean
+       $(MAKE) -C dist/ clean
+       rm -f matitaGeneratedGui.ml matitaGeneratedGui.mli
+       rm -f buildTimeConf.ml
+       rm -f matita.glade.bak matita.gladep.bak
+       rm -f matita.conf.xml.sample
+       rm -rf .matita
+
 TEST_DIRS =                            \
        library                         \
        tests                           \
 
 # {{{ Distribution stuff
 
-ifeq ($(wildcard matitac.opt),matitac.opt)
+ifeq ($(HAVE_OCAMLOPT),yes)
 BEST=opt
+BEST_EXT=.opt
 else
 BEST=all
+BEST_EXT=
 endif
 
-stdlib:
-       @echo "MATITACLEAN all"
-       $(H)./matitaclean -system -conffile `pwd`/matita.conf.xml.build all
-       @echo "MATITAMAKE init"
+ifeq ($(DISTRIBUTED),yes)
+
+dist_library: dist_library@library
+dist_library_clean:
+       @echo "MATITACLEAN -system all"
+       $(H)./matitaclean$(BEST_EXT) \
+               -system -conffile `pwd`/matita.conf.xml.build all
+dist_library@%:
+       @echo "MATITAMAKE -system init"
        $(H)MATITA_RT_BASE_DIR=`pwd` \
-       MATITA_FLAGS="-system -conffile `pwd`/matita.conf.xml.build" \
-               ./matitamake -conffile `pwd`/matita.conf.xml.build \
-                       init build_stdlib `pwd`/library
-       @echo "MATITAMAKE build"
+               MATITA_FLAGS="-system -conffile `pwd`/matita.conf.xml.build" \
+               ./matitamake$(BEST_EXT) -conffile `pwd`/matita.conf.xml.build \
+                       init dist_$* `pwd`/$*
+       @echo "MATITAMAKE -system build"
        $(H)MATITA_RT_BASE_DIR=`pwd` \
-       MATITA_FLAGS="-system -conffile `pwd`/matita.conf.xml.build" \
-               ./matitamake -conffile `pwd`/matita.conf.xml.build \
-                       build build_stdlib
+               MATITA_FLAGS="-system -conffile `pwd`/matita.conf.xml.build" \
+               ./matitamake$(BEST_EXT) -conffile `pwd`/matita.conf.xml.build \
+                       build dist_$*
+       touch $@
 
-#          MATITA_RT_BASE_DIR=`pwd` \
-               $(MAKE) MATITA_FLAGS="-system -conffile `pwd`/matita.conf.xml.build" -C library/ $(BEST)
+endif
 
 DEST = @RT_BASE_DIR@
 INSTALL_STUFF =                        \
 PROGRAMS_UPX = $(patsubst %,%.upx,$(PROGRAMS_STATIC))
 
 ifeq ($(HAVE_OCAMLOPT),yes)
-static: $(STATIC_LINK) $(PROGRAMS_STATIC) coq.moo.opt
+static: $(STATIC_LINK) $(PROGRAMS_STATIC)
 else
 upx:
        @echo "Native code compilation is disabled"
 cicbrowser.opt.static.upx: matita.opt.static.upx
        @test -f $@ || ln -s $< $@
 
-.PHONY: distclean
-distclean: clean
-       $(MAKE) -C dist/ clean
-       rm -f matitaGeneratedGui.ml matitaGeneratedGui.mli
-       rm -f buildTimeConf.ml
-       rm -f matita.glade.bak matita.gladep.bak
-       rm -f matita.conf.xml.sample
-
 %.upx: %
        cp $< $@
        strip $@