Use dub to create source lib and executables
Steven Schveighoffer
schveiguy at gmail.com
Sat Mar 4 20:23:29 UTC 2023
On 3/4/23 1:33 PM, Chris Piker wrote:
> Hi D
>
> I normally work in a *nix environment, typically on server-side code.
> For many projects I have gnu makefiles that build a small lib along with
> command line utilities.
>
> Up to now I've been creating a dub.json file for just the sourceLibrary,
> and then putting embedded dub comments at the top of each of the utility
> programs. The utilities are built via `dub build --single` and a thin
> makefile triggers all the dub commands.
>
> This method is inefficient, and it won't work in a typical Windows dev
> environment, so today I'm trying to remove gnu make from the picture.
> Going back to basics, does anyone know of a clear example of how to
> convert this simplified makefile into 1-N dub files?:
>
> ```makefile
> # Build library + utility programs.
> # Lib sources are in "libsrc", 1-file programs in "utils"
>
> # Implicit rule to make a utility program
> outdir/%:utils/%.d outdir/mylib.a
> dmd -I libsrc -od=outdir -of=$@ $^
>
> # Top level build rule for everything
> build:outdir/mylib.a outdir/prog1 outdir/prog2 | outdir
>
> # Explicit rule to make output directory if not present
> outdir:
> @if [ ! -e outdir ]; then mkdir outdir; fi
>
> # Explicit build rule for the library
> outdir/mylib.a:libsrc/mod1.d libsrc/mod2.d
> dmd -lib -od=outdir -of=$@ $^
> ```
> This is a paired down example since `make test` and `make install`
> aren't present, but I hope the main ideas are apparent.
>
> I've been at it now for about four hours, with lots of web-searches
> (btw, thanks to schveiguy for getting me this far) but I haven't figured
> out what hierarchy of dub constructs I need to create to order to get
> the effect of the small make file above.
>
> Thanks for considering the question,
>
>
If you mean that you have multiple subprojects inside your main dub
project, my advice is to follow what other such projects do. I always
look at vibe for my example.
dub isn't great when it comes to the "one true way" to specify this. You
can actually create a dub subproject inside your dub file, or create dub
files inside all your other subproject folders, and tag them as
subprojects of the main project.
I've tried both ways, and I find the latter to be easier to deal with.
This is what vibe does.
-Steve
More information about the Digitalmars-d-learn
mailing list