[mercury-users] Importing modules from a different directory (MCFLAGS=-I../other_dir/)

Fergus Henderson fjh at cs.mu.OZ.AU
Wed Jan 30 21:24:12 AEDT 2002


On 30-Jan-2002, Ondrej Bojar <oboj7042 at ss1000.ms.mff.cuni.cz> wrote:
> I'm trying to split my project into separate directories:
> 
> dirA/moduleA.m
> dirB/mainmodule.m
>
> where mainmodule.m imports moduleA
> (:- import_module moduleA.)
...
> Is mmake ready for working in more than one directory? And how about with
> autorebuilding when sources in the other directory change?

Mmake will work fine for projects that are spread over more multiple
sub-directories, so long as there are no cyclic dependencies between
different subdirectories.  You just need to run mmake in each directory
in turn.  This can be easily automated using an Mmakefile in the top-level
directory:

	# This part is specific to your example
	SUBDIRS = dirA dirB
	dirB: dirA

	# The rest is a generic template...
	SUBDIR_DEPENDS = $(SUBDIRS:%=%.depend)
	.PHONY: $(SUBDIRS) $(SUBDIR_DEPENDS)
	main_target: $(SUBDIRS)
	depend: $(SUBDIR_DEPENDS)
	$(SUBDIRS): %:
		cd $* && mmake
	$(SUBDIR_DEPENDS): %.depend:
		cd $* && mmake depend

You also need to set the appropriate variables in dirB/Mmakefile to ensure
that the `moduleA' library from the `dirA' directory will get linked in
when building the main module in dirB:

	EXTRA_INT_DIRS = ../dirA
	EXTRA_INIT_DIRS = ../dirA
	EXTRA_LIB_DIRS = ../dirA
	EXTRA_C_LIB_DIRS = ../dirA
	EXTRA_LIBRARIES = moduleA

See the attached files for an complete example.
To build, just do `mmake depend' and then `mmake' in the top-level directory.

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
The University of Melbourne         |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.
-------------- next part --------------
# This part is specific to your example
SUBDIRS = dirA dirB
dirB: dirA

# The rest is a generic template...
SUBDIR_DEPENDS = $(SUBDIRS:%=%.depend)
.PHONY: $(SUBDIRS) $(SUBDIR_DEPENDS)
main_target: $(SUBDIRS)
depend: $(SUBDIR_DEPENDS)
$(SUBDIRS): %:
	cd $* && mmake
$(SUBDIR_DEPENDS): %.depend:
	cd $* && mmake depend

-------------- next part --------------
depend: moduleA.depend
main_target: libmoduleA
-------------- next part --------------
:- module moduleA.
:- interface.
:- func foo = int.
:- implementation.
foo = 42.
-------------- next part --------------
EXTRA_INT_DIRS = ../dirA
EXTRA_INIT_DIRS = ../dirA
EXTRA_LIB_DIRS = ../dirA
EXTRA_C_LIB_DIRS = ../dirA
EXTRA_LIBRARIES = moduleA
depend: mainmodule.depend
main_target: mainmodule
-------------- next part --------------
:- module mainmodule.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.

:- implementation.
:- import_module moduleA.
main --> print(foo), nl.


More information about the users mailing list