#!/bin/sh # Compute the dependencies (direct and transitive) of an object # This is used to collect all objects needed to link a program. # Bail out when .d files are missing test -f "${1%.o}.d" || exit 1 # Check whether $1 contains substring $2 contains(){ test "${1#*$2}" != "$1"; } # Walk the dependency graph (consinting of the set of all .d files) # and output the visited nodes (objects). walk_dag(){ while read -r line; do # skip the first line (file own name) contains "$line" ': ' && continue # only look up object files contains "$line" '.o ' || continue line="${line% \\}" contains "$visited" "$line" && continue visited="$line:$visited" printf '%s ' "$line" # whether the node is a leaf test "$(wc -l < "${line%.o}.d")" -gt 1 && walk_dag < "${line%.o}.d" done } printf '%s ' "$1" # the object itself walk_dag < "${1%.o}.d"