Skip to content

dnaeon/makefile-graph

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

makefile-graph

Build Status Go Reference Go Report Card codecov

makefile-graph is a Go module and CLI application, which parses GNU Make's internal database and generates a graph representing the relationships between the discovered Makefile targets.

Requirements

Installation

You can install the CLI application using one of the following ways.

If you have cloned the repository you can build the CLI app using the provided Makefile target.

make build

The resulting binary will be located in bin/makefile-graph.

Install the CLI application using go install.

go install github.com/dnaeon/makefile-graph/cmd/makefile-graph@latest

In order to install the parser package and use it in your own Go code run the following command within your Go module.

go get -v github.com/dnaeon/makefile-graph/pkg/parser

Usage

Let's use the following example Makefile from GNU make's documentation.

edit : main.o kbd.o command.o display.o \
       insert.o search.o files.o utils.o
	cc -o edit main.o kbd.o command.o display.o \
		   insert.o search.o files.o utils.o

main.o : main.c defs.h
	cc -c main.c
kbd.o : kbd.c defs.h command.h
	cc -c kbd.c
command.o : command.c defs.h command.h
	cc -c command.c
display.o : display.c defs.h buffer.h
	cc -c display.c
insert.o : insert.c defs.h buffer.h
	cc -c insert.c
search.o : search.c defs.h buffer.h
	cc -c search.c
files.o : files.c defs.h buffer.h command.h
	cc -c files.c
utils.o : utils.c defs.h
	cc -c utils.c
clean :
	rm edit main.o kbd.o command.o display.o \
	   insert.o search.o files.o utils.o

You can also find this example Makefile in the examples directory of this repo.

Running the following command will generate the Dot representation for the Makefile targets and their dependencies from our example Makefile.

makefile-graph --makefile examples/Makefile --direction TB

In order to render the graph you can pipe it directly to the dot command, e.g.

makefile-graph --makefile examples/Makefile --direction TB | dot -Tsvg -o graph.svg

This is what the graph looks like when we render it using dot(1).

Example Makefile Graph

Sometimes when rendering large graphs it may not be obvious at first glance what are the dependencies for a specific target. In order to help with such situations makefile-graph supports a flag which allows you to highlight specific targets and their dependencies.

The following command will highlight the files.o target along with it's dependencies.

makefile-graph \
    --makefile examples/Makefile \
    --direction TB \
    --target files.o \
    --highlight \
    --highlight-color lightgreen

When we render the output from above command we will see this graph representation.

Example Makefile Graph Highlighted

If we want to focus on a specific target and it's dependencies only we can use the following command, which will generate a graph only for the target and it's dependencies.

makefile-graph \
    --makefile examples/Makefile \
    --direction TB \
    --target files.o \
    --related-only

This is what the resulting graph looks like.

Example Makefile Graph Related Only

The --direction option is used for specifying the direction of graph layout. You can set it to TB, BT, LR or RL.

The --format option is used for specifying the output format for the graph. By default it will produce the dot representation for the graph.

You can also view the topological order for a given target by setting the format to tsort, e.g.

makefile-graph \
    --makefile examples/Makefile \
    --target files.o \
    --related-only \
    --format tsort

Running above command produces the following output, which represents the topological order for the files.o target.

defs.h
buffer.h
command.h
files.c
files.o

Tests

Run the tests.

make test

Run test coverage.

make test-cover

Contributing

makefile-graph is hosted on Github. Please contribute by reporting issues, suggesting features or by sending patches using pull requests.

License

makefile-graph is Open Source and licensed under the BSD License.