Page 1 of 1

Best way of doing Make library dependencies

Posted: Tue Sep 14, 2021 1:50 pm
by nexos
Hello,
I have been working on the build system of my operating system (for 2 months :roll: ). I am using non recursive GNU Make to build it. One question I have is what is the best way of making an executable depend on a library? At the moment, I'm doing it like this:

Code: Select all

$(BOOTEFI_OUTPUTNAME): $(BOOTEFI_OBJFILES) $(LIBK_OUTPUTNAME) Makefile
LIBK_OUTPUTNAME contains the name of the libk archive. One problem I perceived is the infinitesimally small chance that libk is still building when the executable is finished. Then, it will see an out of date libk (which is really in the process of being built), and then it will re-invoke the recipe. This would probably cause race conditions and other evils.

So, my question is: How can I make the executable wait for libk to come in date, instead of invoking the recipe itself? Is that even possible?
Thanks,
nexos

P.S. The full repo is in my signature

Re: Best way of doing Make library dependencies

Posted: Tue Sep 14, 2021 1:56 pm
by nullplan
If the makefile is non-recursive, then the make instance building the kernel is also the instance building the libk, and will not start the build process until libk is remade.

Re: Best way of doing Make library dependencies

Posted: Tue Sep 14, 2021 2:00 pm
by nexos
nullplan wrote:If the makefile is non-recursive, then the make instance building the kernel is also the instance building the libk, and will not start the build process until libk is remade.
I wondered that! Thanks!

Edit - on parallel make, are targets built in parallel? If so, then the above scenario might could still happen

Re: Best way of doing Make library dependencies

Posted: Tue Sep 14, 2021 2:28 pm
by nullplan
The whole deal with make is that it manages dependencies. That is the reason you write a Makefile instead of just a build script. On parallel make, it will only run independent builds in parallel. For the same reason, it can also not start to build your kernel while the object files are still in progress, but it can build the object files in parallel.