1 # Copyright 2009 The Go Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style
3 # license that can be found in the LICENSE file.
4
5 all: package
6 package: _obj/$(TARG).a
7 testpackage: _test/$(TARG).a
8
9 include $(QUOTED_GOROOT)/src/Make.common
10
11 # The quietgcc wrapper is for our own source code
12 # while building the libraries, not arbitrary source code
13 # as encountered by cgo.
14 ifeq ($(HOST_CC),quietgcc)
15 HOST_CC:=gcc
16 endif
17 ifeq ($(HOST_LD),quietgcc)
18 HOST_LD:=gcc
19 endif
20
21 # GNU Make 3.80 has a bug in lastword
22 # elem=$(lastword $(subst /, ,$(TARG)))
23 TARG_words=$(subst /, ,$(TARG))
24 elem=$(word $(words $(TARG_words)),$(TARG_words))
25
26 ifeq ($(elem),$(TARG))
27 dir=
28 else
29 dir=$(patsubst %/$(elem),%,$(TARG))
30 endif
31
32 pkgdir=$(QUOTED_GOROOT)/pkg/$(GOOS)_$(GOARCH)
33
34 ifeq ($(TARGDIR),)
35 TARGDIR:=$(pkgdir)
36 endif
37
38 INSTALLFILES+=$(TARGDIR)/$(TARG).a
39
40 # The rest of the cgo rules are below, but these variable updates
41 # must be done here so they apply to the main rules.
42 ifdef CGOFILES
43 GOFILES+=$(patsubst %.go,_obj/%.cgo1.go,$(CGOFILES)) _obj/_cgo_gotypes.go
44 CGO_OFILES+=$(patsubst %.go,%.cgo2.o,$(CGOFILES)) _cgo_export.o
45 OFILES+=_cgo_defun.$O _cgo_import.$O $(CGO_OFILES)
46 endif
47
48 ifdef SWIGFILES
49 GOFILES+=$(patsubst %.swig,_obj/%.go,$(patsubst %.swigcxx,%.swig,$(SWIGFILES)))
50 OFILES+=$(patsubst %.swig,_obj/%_gc.$O,$(patsubst %.swigcxx,%.swig,$(SWIGFILES)))
51 SWIG_PREFIX=$(subst /,-,$(TARG))
52 SWIG_SOS+=$(patsubst %.swig,_obj/$(SWIG_PREFIX)-%.so,$(patsubst %.swigcxx,%.swig,$(SWIGFILES)))
53 INSTALLFILES+=$(patsubst %.swig,$(TARGDIR)/swig/$(SWIG_PREFIX)-%.so,$(patsubst %.swigcxx,%.swig,$(SWIGFILES)))
54 endif
55
56 PREREQ+=$(patsubst %,%.make,$(DEPS))
57
58 coverage:
59 gotest
60 6cov -g $(shell pwd) $O.out | grep -v '_test\.go:'
61
62 CLEANFILES+=*.so _obj _test _testmain.go *.exe _cgo* test.out build.out
63
64 test:
65 gotest
66
67 testshort:
68 gotest -test.short -test.timeout=120
69
70 bench:
71 gotest -test.bench=. -test.run="Do not run tests"
72
73 nuke: clean
74 rm -f $(TARGDIR)/$(TARG).a
75
76 testpackage-clean:
77 rm -f _test/$(TARG).a
78
79 install: $(INSTALLFILES)
80
81 $(TARGDIR)/$(TARG).a: _obj/$(TARG).a
82 @mkdir -p $(TARGDIR)/$(dir)
83 cp _obj/$(TARG).a "$@"
84
85 _go_.$O: $(GOFILES) $(PREREQ)
86 $(GC) $(GCIMPORTS) -o $@ $(GOFILES)
87
88 _gotest_.$O: $(GOFILES) $(GOTESTFILES) $(PREREQ)
89 $(GC) $(GCIMPORTS) -o $@ $(GOFILES) $(GOTESTFILES)
90
91 _obj/$(TARG).a: _go_.$O $(OFILES)
92 @mkdir -p _obj/$(dir)
93 rm -f _obj/$(TARG).a
94 gopack grc $@ _go_.$O $(OFILES)
95
96 _test/$(TARG).a: _gotest_.$O $(OFILES)
97 @mkdir -p _test/$(dir)
98 rm -f _test/$(TARG).a
99 gopack grc $@ _gotest_.$O $(OFILES)
100
101 importpath:
102 @echo $(TARG)
103
104 dir:
105 @echo $(dir)
106
107 # To use cgo in a Go package, add a line
108 #
109 # CGOFILES=x.go y.go
110 #
111 # to the main Makefile. This signals that cgo should process x.go
112 # and y.go when building the package.
113 # There are three optional variables to set, CGO_CFLAGS, CGO_LDFLAGS,
114 # and CGO_DEPS, which specify compiler flags, linker flags, and linker
115 # dependencies to use when compiling (using gcc) the C support for
116 # x.go and y.go.
117
118 # Cgo translates each x.go file listed in $(CGOFILES) into a basic
119 # translation of x.go, called _obj/x.cgo1.go. Additionally, three other
120 # files are created:
121 #
122 # _obj/_cgo_gotypes.go - declarations needed for all .go files in the package; imports "unsafe"
123 # _obj/_cgo_defun.c - C trampoline code to be compiled with 6c and linked into the package
124 # _obj/x.cgo2.c - C implementations compiled with gcc to create a dynamic library
125 #
126
127 ifdef CGOFILES
128 _obj/_cgo_run: $(CGOFILES)
129 @mkdir -p _obj
130 CGOPKGPATH=$(dir) cgo -- $(CGO_CFLAGS) $(CGOFILES)
131 touch _obj/_cgo_run
132
133 # _CGO_CFLAGS and _CGO_LDFLAGS are defined via the evaluation of _cgo_flags.
134 # The include happens before the commands in the recipe run,
135 # so it cannot be done in the same recipe that runs cgo.
136 _obj/_load_cgo_flags: _obj/_cgo_run
137 $(eval include _obj/_cgo_flags)
138
139 # Include any previous flags in case cgo files are up to date.
140 -include _obj/_cgo_flags
141
142 # Ugly but necessary - cgo writes these files too.
143 _obj/_cgo_gotypes.go _obj/_cgo_export.c _obj/_cgo_export.h _obj/_cgo_main.c _obj/_cgo_defun.c: _obj/_load_cgo_flags
144 @true
145
146 _obj/%.cgo1.go _obj/%.cgo2.c: _obj/_cgo_defun.c
147 @true
148 endif
149
150 # Compile rules for gcc source files.
151 %.o: %.c
152 $(HOST_CC) $(_CGO_CFLAGS_$(GOARCH)) -g -fPIC -O2 -o $@ -c $(CGO_CFLAGS) $(_CGO_CFLAGS) $*.c
153
154 %.o: _obj/%.c
155 $(HOST_CC) $(_CGO_CFLAGS_$(GOARCH)) -I . -g -fPIC -O2 -o $@ -c $(CGO_CFLAGS) $(_CGO_CFLAGS) $^
156
157 # To find out which symbols are needed from external libraries
158 # and which libraries are needed, we build a simple a.out that
159 # links all the objects we just created and then use cgo -dynimport
160 # to inspect it. That is, we make gcc tell us which dynamic symbols
161 # and libraries are involved, instead of duplicating gcc's logic ourselves.
162 # After main we have to define all the symbols that will be provided
163 # by Go code. That's crosscall2 and any exported symbols.
164
165 _cgo1_.o: _cgo_main.o $(CGO_OFILES)
166 $(HOST_CC) $(_CGO_CFLAGS_$(GOARCH)) -g -fPIC -O2 -o $@ $^ $(CGO_LDFLAGS) $(_CGO_LDFLAGS)
167
168 _obj/_cgo_import.c: _cgo1_.o
169 @mkdir -p _obj
170 cgo -dynimport _cgo1_.o >$@_ && mv -f $@_ $@
171
172 # The rules above added x.cgo1.go and _cgo_gotypes.go to $(GOFILES),
173 # added _cgo_defun.$O to $OFILES, and added the installed copy of
174 # package_x.so (built from x.cgo2.c) to $(INSTALLFILES).
175
176 # Have to run gcc with the right size argument on hybrid 32/64 machines.
177 _CGO_CFLAGS_386=-m32
178 _CGO_CFLAGS_amd64=-m64
179 _CGO_LDFLAGS_freebsd=-shared -lpthread -lm
180 _CGO_LDFLAGS_linux=-shared -lpthread -lm
181 _CGO_LDFLAGS_darwin=-dynamiclib -Wl,-undefined,dynamic_lookup
182 _CGO_LDFLAGS_windows=-shared -lm -mthreads
183
184 # Have to compile the runtime header.
185 RUNTIME_CFLAGS=-I$(pkgdir)
186
187 # Compile _cgo_defun.c with 6c; needs access to the runtime headers.
188 _cgo_defun.$O: _obj/_cgo_defun.c
189 $(CC) $(CFLAGS) $(RUNTIME_CFLAGS) -I . -o "$@" _obj/_cgo_defun.c
190
191 # To use swig in a Go package, add a line
192 #
193 # SWIGFILES=x.swig
194 #
195 # to the main Makefile. This signals that SWIG should process the
196 #.swig file when building the package.
197 #
198 # To wrap C code, use an extension of .swig. To wrap C++ code, use an
199 # extension of .swigcxx.
200 #
201 # SWIGFILES=myclib.swig mycxxlib.swigcxx
202
203 ifdef SWIGFILES
204 _obj/%._swig_run _obj/%.go _obj/%_gc.c _obj/%_wrap.c: %.swig
205 @mkdir -p _obj
206 swig -go -module $* -soname $(SWIG_PREFIX)-$*.so -o _obj/$*_wrap.c -outdir _obj $<
207
208 _obj/%._swig_run _obj/%.go _obj/%_gc.c _obj/%_wrap.cxx: %.swigcxx
209 @mkdir -p _obj
210 swig -go -c++ -module $* -soname $(SWIG_PREFIX)-$*.so -o _obj/$*_wrap.cxx -outdir _obj $<
211
212 _obj/%_gc.$O: _obj/%_gc.c
213 $(CC) $(CFLAGS) -I . -I$(pkgdir) -o "$@" _obj/$*_gc.c
214
215 _obj/%_wrap.o: _obj/%_wrap.c
216 $(HOST_CC) $(_CGO_CFLAGS_$(GOARCH)) -I . -g -fPIC -O2 -o $@ -c $^ $(SWIG_CFLAGS)
217
218 HOST_CXX=g++
219
220 _obj/%_wrapcxx.o: _obj/%_wrap.cxx
221 $(HOST_CXX) $(_CGO_CFLAGS_$(GOARCH)) -I . -g -fPIC -O2 -o $@ -c $^ $(SWIG_CXXFLAGS)
222
223 _obj/$(SWIG_PREFIX)-%.so: _obj/%_wrap.o
224 $(HOST_CC) $(_CGO_CFLAGS_$(GOARCH)) -o $@ $^ $(SWIG_LDFLAGS) $(_CGO_LDFLAGS_$(GOOS)) $(_SWIG_LDFLAGS_$(GOOS))
225
226 _obj/$(SWIG_PREFIX)-%.so: _obj/%_wrapcxx.o
227 $(HOST_CXX) $(_CGO_CFLAGS_$(GOARCH)) -o $@ $^ $(SWIG_LDFLAGS) $(_CGO_LDFLAGS_$(GOOS)) $(_SWIG_LDFLAGS_$(GOOS))
228
229 $(TARGDIR)/swig/$(SWIG_PREFIX)-%.so: _obj/$(SWIG_PREFIX)-%.so
230 @mkdir -p $(TARGDIR)/swig
231 cp $< "$@"
232
233 all: $(SWIG_SOS)
234
235 SWIG_RPATH=-r $(TARGDIR)/swig
236
237 endif
238
239 # Generic build rules.
240 # These come last so that the rules above can override them
241 # for more specific file names.
242 %.$O: %.c $(HFILES)
243 $(CC) $(CFLAGS) -o "$@" $*.c
244
245 %.$O: _obj/%.c $(HFILES)
246 $(CC) $(CFLAGS) -I . -o "$@" _obj/$*.c
247
248 %.$O: %.s
249 $(AS) $*.s