'Algorithm to recompile only necessary files with makefile given dependent source file should also recompile

make tool in UNIX only recompile those files that were changed after the most recent compilation, and any intermediate files in the compilation that depend on those that were changed.

A [Makefile] is typically composed of a list of source files that must be compiled.

`

Each of these source files is dependent on some of the other files which are listed. 
Thus a source file
must be recompiled if a file on which it depends is changed. 

    Assuming
    you have a list of which files have been recently changed, as well as a list
    for each source file of the files on which it depends

,

How to design an algorithm to recompile only those necessary source file.



Solution 1:[1]

What you are looking for is topological sorting which essentially is a depth-first search. You need to create the graph that reflects the dependencies between the files and then run the topological sorting algorithm on that graph. If a file changes you need to recompile all the files that are before that file in the topological order.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mushroomator