[m-dev.] sourcing foreign code

Peter Wang novalazy at gmail.com
Wed Feb 26 14:27:26 AEDT 2014


On Wed, 26 Feb 2014 13:54:15 +1100, Paul Bone <paul at bone.id.au> wrote:
> On Wed, Feb 26, 2014 at 12:43:00PM +1100, Peter Wang wrote:
> > Hi,
> > 
> > Writing foreign code inside foreign_code is a bit painful (no syntax
> > highlighting, need to escape quotes and backslashes).  When the amount
> > of foreign code is significant, I would like to keep it in a separate
> > file and reference it from the Mercury file.  This is possible using the
> > C preprocessor but not ideal:
> > 
> >     * mmc --make does not know about the dependency so you must manually
> >       touch the relevant .m file.  Imagine if you switch branches in a
> >       workspace and the .c file is touched but not the .m file.
> > 
> >     * you must tell the C compiler where to find the .c file,
> >       e.g. with --c-include-dir in Mercury.options
> > 
> >     * only works for C backends
> > 
> > A solution might be to extend pragma foreign_decl/foreign_code like so:
> > 
> >     :- pragma foreign_code("C", source_file("<path>")).
> > 
> > where <path> is _relative to the .m file_, or else an absolute path.
> > 
> > I have not looked into how much work it would be to modify mmc --make,
> > but what do you think?
> 
> Does this embed the contents of the file in the current compilation unit
> (like #include in C)?

Yes.

> Or does this tell mmc --make to compile the external
> file separately using the C compiler and then link it with the other object
> files?

No, .m files is not the place for that.  I wouldn't want to add it to
mmc --make either.

Peter



More information about the developers mailing list