Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multi package makefile example for go

Tags:

I'm trying to setup a multi package go project something like

./main.go

./subpackage1/sub1_1.go

./subpackage1/sub1_2.go

./subpackage2/sub2_1.go

./subpackage2/sub2_2.go

where main.go imports both subpackage1 and subpackage2. And subpackage2 imports subpackage1.

Ive been looking around for go makefile examples but I can't find anything that supports this kind of set-up. Any idea?

like image 367
oftewel Avatar asked Nov 19 '09 21:11

oftewel


2 Answers

Install godag then run:

gd -o myapp

It will automatically build a Directed Acyclic Graph (DAG) of all the dependencies in your src/ directory, then compile and link each package in the proper order.

Much easier than manually maintaining a Makefile, especially since $(GOROOT)/src/Make.* has changed in recent versions of Go (there is no longer a Make.$(GOARCH)). Also useful:

gd clean removes object files.

gd -test runs your automated tests (see testing package).

gd -dot=myapp.dot generates a graph of your package imports you can visualize using GraphViz.

like image 189
Jeff Connelly Avatar answered Oct 14 '22 07:10

Jeff Connelly


Something like this should work

# Makefile include $(GOROOT)/src/Make.$(GOARCH) all:main main:main.$O     $(LD) -Lsubpackage1/_obj -Lsubpackage2/_obj -o $@ $^ %.$O:%.go  subpackage1 subpackage2     $(GC) -Isubpackage1/_obj -Isubpackage2/_obj -o $@ $^ subpackage1:     $(MAKE) -C subpackage1 subpackage2:     $(MAKE) -C subpackage2 .PHONY:subpackage1 subpackage2  # subpackage1/Makefile TARG=subpackage1 GOFILES=sub1_1.go sub1_2.go include $(GOROOT)/src/Make.$(GOARCH) include $(GOROOT)/src/Make.pkg  # subpackage2/Makefile TARG=subpackage2 GOFILES=sub2_1.go sub2_2.go include $(GOROOT)/src/Make.$(GOARCH) include $(GOROOT)/src/Make.pkg GC+=-I../subpackage1/_obj LD+=-L../subpackage1/_obj sub2_1.$O sub2_2.$O:subpackage1 subpackage1:     $(MAKE) -C ../subpackage1 .PHONY:subpackage1 

If you don't install the subpackages you need to explicitly set the include path. The supplied makefile Make.pkg is mainly to build packages, which is why it's only included in the subpackage makefile.

like image 22
Scott Wales Avatar answered Oct 14 '22 07:10

Scott Wales