OLD | NEW |
1 # Copyright 2009 The Go Authors. All rights reserved. | 1 # Copyright 2009 The Go Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style | 2 # Use of this source code is governed by a BSD-style |
3 # license that can be found in the LICENSE file. | 3 # license that can be found in the LICENSE file. |
4 | 4 |
5 all: package | 5 all: package |
6 package: _obj/$(TARG).a | 6 package: _obj/$(TARG).a |
7 testpackage: _test/$(TARG).a | 7 testpackage: _test/$(TARG).a |
8 | 8 |
| 9 # GNU Make syntax: |
| 10 ifndef GOBIN |
| 11 GOBIN=$(HOME)/bin |
| 12 endif |
| 13 |
| 14 # ugly hack to deal with whitespaces in $GOBIN |
| 15 nullstring := |
| 16 space := $(nullstring) # a space at the end |
| 17 QUOTED_GOBIN=$(subst $(space),\ ,$(GOBIN)) |
| 18 |
9 # GNU Make 3.80 has a bug in lastword | 19 # GNU Make 3.80 has a bug in lastword |
10 # elem=$(lastword $(subst /, ,$(TARG))) | 20 # elem=$(lastword $(subst /, ,$(TARG))) |
11 TARG_words=$(subst /, ,$(TARG)) | 21 TARG_words=$(subst /, ,$(TARG)) |
12 elem=$(word $(words $(TARG_words)),$(TARG_words)) | 22 elem=$(word $(words $(TARG_words)),$(TARG_words)) |
13 | 23 |
14 dir=$(patsubst %/$(elem),%,./$(TARG)) | 24 dir=$(patsubst %/$(elem),%,./$(TARG)) |
15 | 25 |
16 # ugly hack to deal with whitespaces in $GOROOT | 26 # ugly hack to deal with whitespaces in $GOROOT |
17 nullstring := | 27 nullstring := |
18 space := $(nullstring) # a space at the end | 28 space := $(nullstring) # a space at the end |
19 QUOTED_GOROOT=$(subst $(space),\ ,$(GOROOT)) | 29 QUOTED_GOROOT=$(subst $(space),\ ,$(GOROOT)) |
20 | 30 |
21 pkgdir=$(QUOTED_GOROOT)/pkg/$(GOOS)_$(GOARCH) | 31 pkgdir=$(QUOTED_GOROOT)/pkg/$(GOOS)_$(GOARCH) |
22 | 32 |
23 INSTALLFILES=$(pkgdir)/$(TARG).a | 33 INSTALLFILES=$(pkgdir)/$(TARG).a |
24 | 34 |
25 # The rest of the cgo rules are below, but these variable updates | 35 # The rest of the cgo rules are below, but these variable updates |
26 # must be done here so they apply to the main rules. | 36 # must be done here so they apply to the main rules. |
27 GOFILES+=$(patsubst %.go,%.cgo1.go,$(CGOFILES)) | 37 GOFILES+=$(patsubst %.go,%.cgo1.go,$(CGOFILES)) |
28 GOFILES+=$(patsubst %.go,%.cgo2.go,$(CGOFILES)) | 38 GOFILES+=$(patsubst %.go,%.cgo2.go,$(CGOFILES)) |
29 OFILES+=$(patsubst %.go,%.cgo3.$O,$(CGOFILES)) | 39 OFILES+=$(patsubst %.go,%.cgo3.$O,$(CGOFILES)) |
30 INSTALLFILES+=$(patsubst %.go,$(pkgdir)/$(dir)/$(elem)_%.so,$(CGOFILES)) | 40 INSTALLFILES+=$(patsubst %.go,$(pkgdir)/$(dir)/$(elem)_%.so,$(CGOFILES)) |
31 PREREQ+=$(patsubst %,%.make,$(DEPS)) | 41 PREREQ+=$(patsubst %,%.make,$(DEPS)) |
32 | 42 |
33 coverage: | 43 coverage: |
34 » gotest | 44 » $(QUOTED_GOBIN)/gotest |
35 » 6cov -g $(shell pwd) $O.out | grep -v '_test\.go:' | 45 » $(QUOTED_GOBIN)/6cov -g $(shell pwd) $O.out | grep -v '_test\.go:' |
36 | 46 |
37 clean: | 47 clean: |
38 rm -rf *.[$(OS)o] *.a [$(OS)].out *.cgo[12].go *.cgo[34].c *.so _obj _te
st _testmain.go $(CLEANFILES) | 48 rm -rf *.[$(OS)o] *.a [$(OS)].out *.cgo[12].go *.cgo[34].c *.so _obj _te
st _testmain.go $(CLEANFILES) |
39 | 49 |
40 test: | 50 test: |
41 » gotest | 51 » $(QUOTED_GOBIN)/gotest |
42 | 52 |
43 nuke: clean | 53 nuke: clean |
44 rm -f $(pkgdir)/$(TARG).a | 54 rm -f $(pkgdir)/$(TARG).a |
45 | 55 |
46 testpackage-clean: | 56 testpackage-clean: |
47 rm -f _test/$(TARG).a _gotest_.$O | 57 rm -f _test/$(TARG).a _gotest_.$O |
48 | 58 |
49 install: $(INSTALLFILES) | 59 install: $(INSTALLFILES) |
50 | 60 |
51 $(pkgdir)/$(TARG).a: package | 61 $(pkgdir)/$(TARG).a: package |
52 @test -d $(QUOTED_GOROOT)/pkg && mkdir -p $(pkgdir)/$(dir) | 62 @test -d $(QUOTED_GOROOT)/pkg && mkdir -p $(pkgdir)/$(dir) |
53 cp _obj/$(TARG).a "$@" | 63 cp _obj/$(TARG).a "$@" |
54 | 64 |
55 _go_.$O: $(GOFILES) $(PREREQ) | 65 _go_.$O: $(GOFILES) $(PREREQ) |
56 » $(GC) -o $@ $(GOFILES) | 66 » $(QUOTED_GOBIN)/$(GC) -o $@ $(GOFILES) |
57 | 67 |
58 _gotest_.$O: $(GOFILES) $(GOTESTFILES) $(PREREQ) | 68 _gotest_.$O: $(GOFILES) $(GOTESTFILES) $(PREREQ) |
59 » $(GC) -o $@ $(GOFILES) $(GOTESTFILES) | 69 » $(QUOTED_GOBIN)/$(GC) -o $@ $(GOFILES) $(GOTESTFILES) |
60 | 70 |
61 _obj/$(TARG).a: _go_.$O $(OFILES) | 71 _obj/$(TARG).a: _go_.$O $(OFILES) |
62 @mkdir -p _obj/$(dir) | 72 @mkdir -p _obj/$(dir) |
63 rm -f _obj/$(TARG).a | 73 rm -f _obj/$(TARG).a |
64 » gopack grc $@ _go_.$O $(OFILES) | 74 » $(QUOTED_GOBIN)/gopack grc $@ _go_.$O $(OFILES) |
65 | 75 |
66 _test/$(TARG).a: _gotest_.$O $(OFILES) | 76 _test/$(TARG).a: _gotest_.$O $(OFILES) |
67 @mkdir -p _test/$(dir) | 77 @mkdir -p _test/$(dir) |
68 rm -f _test/$(TARG).a | 78 rm -f _test/$(TARG).a |
69 » gopack grc $@ _gotest_.$O $(OFILES) | 79 » $(QUOTED_GOBIN)/gopack grc $@ _gotest_.$O $(OFILES) |
70 | 80 |
71 importpath: | 81 importpath: |
72 @echo $(TARG) | 82 @echo $(TARG) |
73 | 83 |
74 dir: | 84 dir: |
75 @echo $(dir) | 85 @echo $(dir) |
76 | 86 |
77 %.make: | 87 %.make: |
78 » (cd $* && gomake) | 88 » (cd $* && $(QUOTED_GOBIN)/gomake) |
79 | 89 |
80 # To use cgo in a Go package, add a line | 90 # To use cgo in a Go package, add a line |
81 # | 91 # |
82 # CGOFILES=x.go | 92 # CGOFILES=x.go |
83 # | 93 # |
84 # to the main Makefile. This signals that cgo should process x.go. | 94 # to the main Makefile. This signals that cgo should process x.go. |
85 # There are two optional variables to set, CGO_CFLAGS and CGO_LDFLAGS, | 95 # There are two optional variables to set, CGO_CFLAGS and CGO_LDFLAGS, |
86 # which specify compiler and linker flags to use when compiling | 96 # which specify compiler and linker flags to use when compiling |
87 # (using gcc) the C support for x.go. | 97 # (using gcc) the C support for x.go. |
88 | 98 |
89 # Cgo translates each x.go file listed in $(CGOFILES) into | 99 # Cgo translates each x.go file listed in $(CGOFILES) into |
90 # | 100 # |
91 # x.cgo1.go - basic translation of x.go | 101 # x.cgo1.go - basic translation of x.go |
92 # x.cgo2.go - declarations needed for x.cgo1.go; imports "unsafe" | 102 # x.cgo2.go - declarations needed for x.cgo1.go; imports "unsafe" |
93 # x.cgo3.c - C trampoline code to be compiled with 6c and linked into the
package | 103 # x.cgo3.c - C trampoline code to be compiled with 6c and linked into the
package |
94 # x.cgo4.c - C implementations compiled with gcc to create dynamic library | 104 # x.cgo4.c - C implementations compiled with gcc to create dynamic library |
95 # | 105 # |
96 %.cgo1.go %.cgo2.go %.cgo3.c %.cgo4.c: %.go | 106 %.cgo1.go %.cgo2.go %.cgo3.c %.cgo4.c: %.go |
97 » CGOPKGPATH=$(dir) cgo $(CGO_CFLAGS) $*.go | 107 » CGOPKGPATH=$(dir) $(QUOTED_GOBIN)/cgo $(CGO_CFLAGS) $*.go |
98 | 108 |
99 # The rules above added x.cgo1.go and x.cgo2.go to $(GOFILES), | 109 # The rules above added x.cgo1.go and x.cgo2.go to $(GOFILES), |
100 # added x.cgo3.$O to $OFILES, and added the installed copy of | 110 # added x.cgo3.$O to $OFILES, and added the installed copy of |
101 # package_x.so (built from x.cgo4.c) to $(INSTALLFILES). | 111 # package_x.so (built from x.cgo4.c) to $(INSTALLFILES). |
102 | 112 |
103 # Compile x.cgo3.c with 6c; needs access to the runtime headers. | 113 # Compile x.cgo3.c with 6c; needs access to the runtime headers. |
104 RUNTIME_CFLAGS_amd64=-D_64BIT | 114 RUNTIME_CFLAGS_amd64=-D_64BIT |
105 RUNTIME_CFLAGS=-I"$(GOROOT)/src/pkg/runtime" $(RUNTIME_CFLAGS_$(GOARCH)) | 115 RUNTIME_CFLAGS=-I"$(GOROOT)/src/pkg/runtime" $(RUNTIME_CFLAGS_$(GOARCH)) |
106 %.cgo3.$O: %.cgo3.c | 116 %.cgo3.$O: %.cgo3.c |
107 » $(CC) $(CFLAGS) $(RUNTIME_CFLAGS) $*.cgo3.c | 117 » $(QUOTED_GOBIN)/$(CC) $(CFLAGS) $(RUNTIME_CFLAGS) $*.cgo3.c |
108 | 118 |
109 # Have to run gcc with the right size argument on hybrid 32/64 machines. | 119 # Have to run gcc with the right size argument on hybrid 32/64 machines. |
110 _CGO_CFLAGS_386=-m32 | 120 _CGO_CFLAGS_386=-m32 |
111 _CGO_CFLAGS_amd64=-m64 | 121 _CGO_CFLAGS_amd64=-m64 |
112 _CGO_LDFLAGS_freebsd=-shared -lpthread -lm | 122 _CGO_LDFLAGS_freebsd=-shared -lpthread -lm |
113 _CGO_LDFLAGS_linux=-shared -lpthread -lm | 123 _CGO_LDFLAGS_linux=-shared -lpthread -lm |
114 _CGO_LDFLAGS_darwin=-dynamiclib -Wl,-undefined,dynamic_lookup | 124 _CGO_LDFLAGS_darwin=-dynamiclib -Wl,-undefined,dynamic_lookup |
115 | 125 |
116 | 126 |
117 # Compile x.cgo4.c with gcc to make package_x.so. | 127 # Compile x.cgo4.c with gcc to make package_x.so. |
118 %.cgo4.o: %.cgo4.c | 128 %.cgo4.o: %.cgo4.c |
119 gcc $(_CGO_CFLAGS_$(GOARCH)) -fPIC -O2 -o $@ -c $(CGO_CFLAGS) $*.cgo4.c | 129 gcc $(_CGO_CFLAGS_$(GOARCH)) -fPIC -O2 -o $@ -c $(CGO_CFLAGS) $*.cgo4.c |
120 | 130 |
121 $(elem)_%.so: %.cgo4.o | 131 $(elem)_%.so: %.cgo4.o |
122 gcc $(_CGO_CFLAGS_$(GOARCH)) -o $@ $*.cgo4.o $(CGO_LDFLAGS) $(_CGO_LDFL
AGS_$(GOOS)) | 132 gcc $(_CGO_CFLAGS_$(GOARCH)) -o $@ $*.cgo4.o $(CGO_LDFLAGS) $(_CGO_LDFL
AGS_$(GOOS)) |
123 | 133 |
124 $(pkgdir)/$(dir)/$(elem)_%.so: $(elem)_%.so | 134 $(pkgdir)/$(dir)/$(elem)_%.so: $(elem)_%.so |
125 @test -d $(QUOTED_GOROOT/pkg && mkdir -p $(pkgdir)/$(dir) | 135 @test -d $(QUOTED_GOROOT/pkg && mkdir -p $(pkgdir)/$(dir) |
126 cp $(elem)_$*.so "$@" | 136 cp $(elem)_$*.so "$@" |
127 | 137 |
128 # Generic build rules. | 138 # Generic build rules. |
129 # These come last so that the rules above can override them | 139 # These come last so that the rules above can override them |
130 # for more specific file names. | 140 # for more specific file names. |
131 %.$O: %.c | 141 %.$O: %.c |
132 » $(CC) $(CFLAGS) $*.c | 142 » $(QUOTED_GOBIN)/$(CC) $(CFLAGS) $*.c |
133 | 143 |
134 %.$O: %.s | 144 %.$O: %.s |
135 » $(AS) $*.s | 145 » $(QUOTED_GOBIN)/$(AS) $*.s |
136 | 146 |
137 %.$O: $(HFILES) | 147 %.$O: $(HFILES) |
138 | 148 |
OLD | NEW |