proposal: improved import declaration

Frits van Bommel fvbommel at REMwOVExCAPSs.nl
Wed May 30 07:23:02 PDT 2007


Chris Nicholson-Sauls wrote:
> Something like this:
> 
> macro myImport (pkg, mods...) {
>   foreach (m; mods) {
>     import pkg.m ;
>   }
> }
> 
> myImport(tango.io, Console, Conduit, FilePath, FileSystem, Stdout);
> 
> And seeing as rebuild is based around the compiler's own frontend, where 
> such macros are defined and expanded, I imagine it should adapt to them 
> quite readily.  Other build utilities, on the other hand, might have 
> some issue.  (Maybe it really is time to provide at least simple support 
> for this in the compiler...)

Other build utilities should be able to easily support this sort of 
thing by parsing the output of "dmd -v -o-". (For GDC, use either "gdmd 
-v -o-" or "gdc -fsyntax-only -fd-verbose" instead)

I have the following clause in one of my makefiles:
---
# Generate dependency information
%.d$(DEP_SUFFIX): Makefile
	@echo Processing $*.d
	@dmd -v -o- $*.d $(DFLAGS) \
		| grep '^import ' \
		| sed -e 's/.*(\(.*\))/\1/' \
		| xargs echo "$*.d$(OBJ_SUFFIX) $@ : $*.d " \
		> $@
---
The first (relevant) line invokes DMD as above. -v shows lots of info 
about the file being processed, -o- suppresses actual compilation.
The grep filters down to just lines about imports.
The sed command extracts the bit between parentheses (the file name of 
the imported module).
The xargs line prepends some information for Make to it, converting it 
to a dependency rule; the object file and the dependency file depend on 
the original file + all imported modules.
The output is saved to the dependency file. All dependency files are 
included into the makefile.

This works quite nicely. I would imagine it'd work even better in build 
utilities that can use the complete power of D (or another high-level 
language) to parse the output instead of being restricted to 
(interpreted) shell commands. (And it wouldn't need all of the above 
steps, since they don't need to generate make-compatible syntax)



More information about the Digitalmars-d mailing list