Search code examples
makefilevhdlghdl

purpose of command in makefile


What does this code in Makefile exactly do and is there a way to avoid using it and still make the code compile successfully?

work-obj93.cf: sources.mk
$(RM) $@
(for i in $(SOURCES); do \
    $(GHDLC) -i $(GHDLFLAGS) $$i; \
done) || ($(RM) $@; exit 1)

Solution

  • work-obj93.cf: sources.mk

    This first line tells "make" that the file "work-obj93.cf" depends on "sources.mk". If you change the latter, the former needs to be rebuilt, as the following lines define. Most probably your Makefile includes "sources.mk", which defines SOURCES.

    $(RM) $@

    The first command to execute removes the target file. $(RM) inserts the appropriate real command for your system, probably rm on your OS. $@ is the placeholder for target file, an automatic make variable.

    The target file is the "library" for GHDL. To do a clean build if the set of sources changes, it seems as if this file shall be removed and rebuilt.

    (for i in $(SOURCES); do $(GHDLC) -i $(GHDLFLAGS) $$i; done) || ($(RM) $@; exit 1)

    The second rule calls GHDL with the "import option" to parse and add all sources to its library.

    The first part for i in $(SOURCES); do ...; done loops over all files given in SOURCES. The loop command $(GHDLC) -i $(GHDLFLAGS) $$i calls GHDL. If GHDL finds an error, it returns with a non-zero value that is used by the shell ((...) || ($(RM) $@; exit 1)) to remove the target file (the library) and to stop the build. EDIT: As HardcoreHenry pointed out, here is an error, because the loop runs through all sources; only if the last call of GHDL returns an error code, the reaction takes place.


    The decision about necessity of this rule is up to you. You might want to read GHDL's documentation, and do some experiments, if removing the rule still gives correct builds under all circumstances. Try to add a new source file in "sources.mk", to rename a source file, or to remove some source file, of course without introducing VHDL errors.