Russ Cox | eda49f3 | 2009-08-12 13:18:46 -0700 | [diff] [blame] | 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 | |
Russ Cox | 090efde | 2009-08-13 14:41:10 -0700 | [diff] [blame] | 5 | all: package |
Russ Cox | eda49f3 | 2009-08-12 13:18:46 -0700 | [diff] [blame] | 6 | package: _obj/$(TARG).a |
| 7 | testpackage: _test/$(TARG).a |
| 8 | |
Rob Pike | 14abacf | 2010-03-19 15:14:46 -0700 | [diff] [blame] | 9 | include $(QUOTED_GOROOT)/src/Make.common |
Devon H. O'Dell | 857d4cf | 2009-12-11 15:14:09 -0800 | [diff] [blame] | 10 | |
Russ Cox | 9f24d36 | 2010-08-26 20:21:34 -0400 | [diff] [blame] | 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 | |
Russ Cox | 3849325 | 2009-11-11 12:00:50 -0800 | [diff] [blame] | 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 | |
Russ Cox | c276d87 | 2009-12-21 08:31:02 -0800 | [diff] [blame] | 26 | ifeq ($(elem),$(TARG)) |
| 27 | dir= |
| 28 | else |
| 29 | dir=$(patsubst %/$(elem),%,$(TARG)) |
| 30 | endif |
Sergio Luis O. B. Correia | 6fc8207 | 2009-11-23 17:32:51 -0800 | [diff] [blame] | 31 | |
Sergio Luis O. B. Correia | 6fc8207 | 2009-11-23 17:32:51 -0800 | [diff] [blame] | 32 | pkgdir=$(QUOTED_GOROOT)/pkg/$(GOOS)_$(GOARCH) |
Russ Cox | eda49f3 | 2009-08-12 13:18:46 -0700 | [diff] [blame] | 33 | |
Ian Lance Taylor | 7c99dcb | 2010-02-24 17:00:25 -0800 | [diff] [blame] | 34 | INSTALLFILES+=$(pkgdir)/$(TARG).a |
Russ Cox | cce0111 | 2009-09-30 11:51:08 -0700 | [diff] [blame] | 35 | |
| 36 | # The rest of the cgo rules are below, but these variable updates |
| 37 | # must be done here so they apply to the main rules. |
Devon H. O'Dell | 9277b02 | 2009-12-17 13:20:56 -0800 | [diff] [blame] | 38 | ifdef CGOFILES |
Russ Cox | 14d677e | 2010-12-08 13:56:51 -0500 | [diff] [blame] | 39 | GOFILES+=$(patsubst %.go,%.cgo1.go,$(CGOFILES)) _cgo_gotypes.go |
Russ Cox | 0cd3475 | 2010-12-17 09:51:55 -0800 | [diff] [blame] | 40 | CGO_OFILES+=$(patsubst %.go,%.cgo2.o,$(CGOFILES)) _cgo_export.o |
| 41 | OFILES+=_cgo_defun.$O _cgo_import.$O $(CGO_OFILES) |
Devon H. O'Dell | 9277b02 | 2009-12-17 13:20:56 -0800 | [diff] [blame] | 42 | endif |
Russ Cox | cce0111 | 2009-09-30 11:51:08 -0700 | [diff] [blame] | 43 | |
Rob Pike | 14abacf | 2010-03-19 15:14:46 -0700 | [diff] [blame] | 44 | PREREQ+=$(patsubst %,%.make,$(DEPS)) |
| 45 | |
Russ Cox | eda49f3 | 2009-08-12 13:18:46 -0700 | [diff] [blame] | 46 | coverage: |
Russ Cox | aafe474e | 2010-08-24 20:00:33 -0400 | [diff] [blame] | 47 | gotest |
| 48 | 6cov -g $(shell pwd) $O.out | grep -v '_test\.go:' |
Russ Cox | eda49f3 | 2009-08-12 13:18:46 -0700 | [diff] [blame] | 49 | |
Russ Cox | 14d677e | 2010-12-08 13:56:51 -0500 | [diff] [blame] | 50 | CLEANFILES+=*.cgo1.go *.cgo2.c _cgo_defun.c _cgo_gotypes.go _cgo_export.* |
Gustavo Niemeyer | 4fd867b | 2011-02-01 08:44:18 -0500 | [diff] [blame] | 51 | CLEANFILES+=_cgo_.c _cgo_import.c _cgo_main.c _cgo_flags _cgo_run |
Russ Cox | 14d677e | 2010-12-08 13:56:51 -0500 | [diff] [blame] | 52 | CLEANFILES+=*.so _obj _test _testmain.go *.exe |
Russ Cox | eda49f3 | 2009-08-12 13:18:46 -0700 | [diff] [blame] | 53 | |
| 54 | test: |
Russ Cox | aafe474e | 2010-08-24 20:00:33 -0400 | [diff] [blame] | 55 | gotest |
Russ Cox | eda49f3 | 2009-08-12 13:18:46 -0700 | [diff] [blame] | 56 | |
Adam Langley | cbd3b2e | 2010-01-12 15:43:20 -0800 | [diff] [blame] | 57 | bench: |
Russ Cox | aafe474e | 2010-08-24 20:00:33 -0400 | [diff] [blame] | 58 | gotest -benchmarks=. -match="Do not run tests" |
Adam Langley | cbd3b2e | 2010-01-12 15:43:20 -0800 | [diff] [blame] | 59 | |
Russ Cox | eda49f3 | 2009-08-12 13:18:46 -0700 | [diff] [blame] | 60 | nuke: clean |
| 61 | rm -f $(pkgdir)/$(TARG).a |
| 62 | |
| 63 | testpackage-clean: |
| 64 | rm -f _test/$(TARG).a _gotest_.$O |
| 65 | |
Russ Cox | cce0111 | 2009-09-30 11:51:08 -0700 | [diff] [blame] | 66 | install: $(INSTALLFILES) |
Russ Cox | ff5618e | 2009-08-12 14:40:29 -0700 | [diff] [blame] | 67 | |
Russ Cox | 6962e2b | 2010-03-30 10:44:51 -0700 | [diff] [blame] | 68 | $(pkgdir)/$(TARG).a: _obj/$(TARG).a |
Sergio Luis O. B. Correia | 6fc8207 | 2009-11-23 17:32:51 -0800 | [diff] [blame] | 69 | @test -d $(QUOTED_GOROOT)/pkg && mkdir -p $(pkgdir)/$(dir) |
| 70 | cp _obj/$(TARG).a "$@" |
Russ Cox | eda49f3 | 2009-08-12 13:18:46 -0700 | [diff] [blame] | 71 | |
Russ Cox | bf991bb | 2009-11-01 05:43:26 -0800 | [diff] [blame] | 72 | _go_.$O: $(GOFILES) $(PREREQ) |
Russ Cox | aafe474e | 2010-08-24 20:00:33 -0400 | [diff] [blame] | 73 | $(GC) -o $@ $(GOFILES) |
Russ Cox | eda49f3 | 2009-08-12 13:18:46 -0700 | [diff] [blame] | 74 | |
Russ Cox | bf991bb | 2009-11-01 05:43:26 -0800 | [diff] [blame] | 75 | _gotest_.$O: $(GOFILES) $(GOTESTFILES) $(PREREQ) |
Russ Cox | aafe474e | 2010-08-24 20:00:33 -0400 | [diff] [blame] | 76 | $(GC) -o $@ $(GOFILES) $(GOTESTFILES) |
Russ Cox | eda49f3 | 2009-08-12 13:18:46 -0700 | [diff] [blame] | 77 | |
Russ Cox | eda49f3 | 2009-08-12 13:18:46 -0700 | [diff] [blame] | 78 | _obj/$(TARG).a: _go_.$O $(OFILES) |
Russ Cox | cce0111 | 2009-09-30 11:51:08 -0700 | [diff] [blame] | 79 | @mkdir -p _obj/$(dir) |
Russ Cox | eda49f3 | 2009-08-12 13:18:46 -0700 | [diff] [blame] | 80 | rm -f _obj/$(TARG).a |
Russ Cox | aafe474e | 2010-08-24 20:00:33 -0400 | [diff] [blame] | 81 | gopack grc $@ _go_.$O $(OFILES) |
Russ Cox | eda49f3 | 2009-08-12 13:18:46 -0700 | [diff] [blame] | 82 | |
| 83 | _test/$(TARG).a: _gotest_.$O $(OFILES) |
Russ Cox | cce0111 | 2009-09-30 11:51:08 -0700 | [diff] [blame] | 84 | @mkdir -p _test/$(dir) |
Russ Cox | eda49f3 | 2009-08-12 13:18:46 -0700 | [diff] [blame] | 85 | rm -f _test/$(TARG).a |
Russ Cox | aafe474e | 2010-08-24 20:00:33 -0400 | [diff] [blame] | 86 | gopack grc $@ _gotest_.$O $(OFILES) |
Russ Cox | eda49f3 | 2009-08-12 13:18:46 -0700 | [diff] [blame] | 87 | |
| 88 | importpath: |
| 89 | @echo $(TARG) |
| 90 | |
Russ Cox | 090efde | 2009-08-13 14:41:10 -0700 | [diff] [blame] | 91 | dir: |
| 92 | @echo $(dir) |
| 93 | |
Russ Cox | cce0111 | 2009-09-30 11:51:08 -0700 | [diff] [blame] | 94 | # To use cgo in a Go package, add a line |
| 95 | # |
Devon H. O'Dell | 9277b02 | 2009-12-17 13:20:56 -0800 | [diff] [blame] | 96 | # CGOFILES=x.go y.go |
Russ Cox | cce0111 | 2009-09-30 11:51:08 -0700 | [diff] [blame] | 97 | # |
Devon H. O'Dell | 9277b02 | 2009-12-17 13:20:56 -0800 | [diff] [blame] | 98 | # to the main Makefile. This signals that cgo should process x.go |
| 99 | # and y.go when building the package. |
Ian Lance Taylor | 12e7ce9 | 2010-04-09 13:31:05 -0700 | [diff] [blame] | 100 | # There are three optional variables to set, CGO_CFLAGS, CGO_LDFLAGS, |
| 101 | # and CGO_DEPS, which specify compiler flags, linker flags, and linker |
| 102 | # dependencies to use when compiling (using gcc) the C support for |
| 103 | # x.go and y.go. |
Russ Cox | cce0111 | 2009-09-30 11:51:08 -0700 | [diff] [blame] | 104 | |
Devon H. O'Dell | 9277b02 | 2009-12-17 13:20:56 -0800 | [diff] [blame] | 105 | # Cgo translates each x.go file listed in $(CGOFILES) into a basic |
| 106 | # translation of x.go, called x.cgo1.go. Additionally, three other |
| 107 | # files are created: |
Russ Cox | cce0111 | 2009-09-30 11:51:08 -0700 | [diff] [blame] | 108 | # |
Devon H. O'Dell | 9277b02 | 2009-12-17 13:20:56 -0800 | [diff] [blame] | 109 | # _cgo_gotypes.go - declarations needed for all .go files in the package; imports "unsafe" |
| 110 | # _cgo_defun.c - C trampoline code to be compiled with 6c and linked into the package |
| 111 | # x.cgo2.c - C implementations compiled with gcc to create a dynamic library |
Russ Cox | cce0111 | 2009-09-30 11:51:08 -0700 | [diff] [blame] | 112 | # |
Russ Cox | cce0111 | 2009-09-30 11:51:08 -0700 | [diff] [blame] | 113 | |
Russ Cox | 0cd3475 | 2010-12-17 09:51:55 -0800 | [diff] [blame] | 114 | ifdef CGOFILES |
Gustavo Niemeyer | 4fd867b | 2011-02-01 08:44:18 -0500 | [diff] [blame] | 115 | _cgo_run: $(CGOFILES) |
Russ Cox | aafe474e | 2010-08-24 20:00:33 -0400 | [diff] [blame] | 116 | CGOPKGPATH=$(dir) cgo -- $(CGO_CFLAGS) $(CGOFILES) |
Gustavo Niemeyer | 34336bd | 2011-02-02 22:37:40 -0500 | [diff] [blame] | 117 | touch _cgo_run |
Russ Cox | cce0111 | 2009-09-30 11:51:08 -0700 | [diff] [blame] | 118 | |
Gustavo Niemeyer | 4fd867b | 2011-02-01 08:44:18 -0500 | [diff] [blame] | 119 | # _CGO_CFLAGS and _CGO_LDFLAGS are defined via the evaluation of _cgo_flags. |
| 120 | # The include happens before the commands in the recipe run, |
| 121 | # so it cannot be done in the same recipe that runs cgo. |
Gustavo Niemeyer | b9183b9 | 2011-02-08 15:40:11 -0500 | [diff] [blame] | 122 | _load_cgo_flags: _cgo_run |
Gustavo Niemeyer | 4fd867b | 2011-02-01 08:44:18 -0500 | [diff] [blame] | 123 | $(eval include _cgo_flags) |
| 124 | |
| 125 | # Include any previous flags in case cgo files are up to date. |
| 126 | -include _cgo_flags |
| 127 | |
Russ Cox | 6c6d530 | 2010-12-17 11:37:11 -0800 | [diff] [blame] | 128 | # Ugly but necessary - cgo writes these files too. |
Gustavo Niemeyer | b9183b9 | 2011-02-08 15:40:11 -0500 | [diff] [blame] | 129 | _cgo_gotypes.go _cgo_export.c _cgo_export.h _cgo_main.c _cgo_defun.c: _load_cgo_flags |
Jukka-Pekka Kekkonen | 1cec72c | 2010-09-07 15:14:14 -0400 | [diff] [blame] | 130 | @true |
| 131 | |
| 132 | %.cgo1.go %.cgo2.c: _cgo_defun.c |
Devon H. O'Dell | 9277b02 | 2009-12-17 13:20:56 -0800 | [diff] [blame] | 133 | @true |
Russ Cox | 3832389 | 2010-12-17 13:22:20 -0800 | [diff] [blame] | 134 | endif |
Devon H. O'Dell | 9277b02 | 2009-12-17 13:20:56 -0800 | [diff] [blame] | 135 | |
Russ Cox | 14d677e | 2010-12-08 13:56:51 -0500 | [diff] [blame] | 136 | # Compile rules for gcc source files. |
Russ Cox | 0cd3475 | 2010-12-17 09:51:55 -0800 | [diff] [blame] | 137 | %.o: %.c |
Gustavo Niemeyer | 4fd867b | 2011-02-01 08:44:18 -0500 | [diff] [blame] | 138 | $(HOST_CC) $(_CGO_CFLAGS_$(GOARCH)) -g -fPIC -O2 -o $@ -c $(CGO_CFLAGS) $(_CGO_CFLAGS) $*.c |
Russ Cox | 14d677e | 2010-12-08 13:56:51 -0500 | [diff] [blame] | 139 | |
| 140 | # To find out which symbols are needed from external libraries |
| 141 | # and which libraries are needed, we build a simple a.out that |
| 142 | # links all the objects we just created and then use cgo -dynimport |
| 143 | # to inspect it. That is, we make gcc tell us which dynamic symbols |
| 144 | # and libraries are involved, instead of duplicating gcc's logic ourselves. |
Russ Cox | 0cd3475 | 2010-12-17 09:51:55 -0800 | [diff] [blame] | 145 | # After main we have to define all the symbols that will be provided |
| 146 | # by Go code. That's crosscall2 and any exported symbols. |
Russ Cox | 14d677e | 2010-12-08 13:56:51 -0500 | [diff] [blame] | 147 | |
| 148 | _cgo_main.o: _cgo_main.c |
Gustavo Niemeyer | 4fd867b | 2011-02-01 08:44:18 -0500 | [diff] [blame] | 149 | $(HOST_CC) $(_CGO_CFLAGS_$(GOARCH)) -g -fPIC -O2 -o $@ -c $(CGO_CFLAGS) $(_CGO_CFLAGS) _cgo_main.c |
Russ Cox | 14d677e | 2010-12-08 13:56:51 -0500 | [diff] [blame] | 150 | |
Russ Cox | 0cd3475 | 2010-12-17 09:51:55 -0800 | [diff] [blame] | 151 | _cgo1_.o: _cgo_main.o $(CGO_OFILES) |
Gustavo Niemeyer | 4fd867b | 2011-02-01 08:44:18 -0500 | [diff] [blame] | 152 | $(HOST_CC) $(_CGO_CFLAGS_$(GOARCH)) -g -fPIC -O2 -o $@ $^ $(CGO_LDFLAGS) $(_CGO_LDFLAGS) |
Russ Cox | 14d677e | 2010-12-08 13:56:51 -0500 | [diff] [blame] | 153 | |
| 154 | _cgo_import.c: _cgo1_.o |
| 155 | cgo -dynimport _cgo1_.o >_$@ && mv -f _$@ $@ |
Ian Lance Taylor | 12e7ce9 | 2010-04-09 13:31:05 -0700 | [diff] [blame] | 156 | |
Devon H. O'Dell | 9277b02 | 2009-12-17 13:20:56 -0800 | [diff] [blame] | 157 | # The rules above added x.cgo1.go and _cgo_gotypes.go to $(GOFILES), |
| 158 | # added _cgo_defun.$O to $OFILES, and added the installed copy of |
| 159 | # package_x.so (built from x.cgo2.c) to $(INSTALLFILES). |
| 160 | |
Russ Cox | e67a508 | 2009-09-30 13:47:15 -0700 | [diff] [blame] | 161 | # Have to run gcc with the right size argument on hybrid 32/64 machines. |
| 162 | _CGO_CFLAGS_386=-m32 |
| 163 | _CGO_CFLAGS_amd64=-m64 |
William Josephson | 72e7444 | 2009-11-18 09:18:51 -0800 | [diff] [blame] | 164 | _CGO_LDFLAGS_freebsd=-shared -lpthread -lm |
Russ Cox | 133a158 | 2009-10-03 10:37:12 -0700 | [diff] [blame] | 165 | _CGO_LDFLAGS_linux=-shared -lpthread -lm |
| 166 | _CGO_LDFLAGS_darwin=-dynamiclib -Wl,-undefined,dynamic_lookup |
Wei Guangjing | 035696c5 | 2010-11-01 17:52:26 -0400 | [diff] [blame] | 167 | _CGO_LDFLAGS_windows=-shared -lm -mthreads |
Russ Cox | 133a158 | 2009-10-03 10:37:12 -0700 | [diff] [blame] | 168 | |
Russ Cox | 14d677e | 2010-12-08 13:56:51 -0500 | [diff] [blame] | 169 | # Have to compile the runtime header. |
Christopher Nielsen | 31ccf19 | 2011-02-03 00:42:03 -0500 | [diff] [blame] | 170 | RUNTIME_CFLAGS=-I$(pkgdir) |
Russ Cox | cce0111 | 2009-09-30 11:51:08 -0700 | [diff] [blame] | 171 | |
Devon H. O'Dell | 9277b02 | 2009-12-17 13:20:56 -0800 | [diff] [blame] | 172 | # Compile _cgo_defun.c with 6c; needs access to the runtime headers. |
| 173 | _cgo_defun.$O: _cgo_defun.c |
Russ Cox | aafe474e | 2010-08-24 20:00:33 -0400 | [diff] [blame] | 174 | $(CC) $(CFLAGS) $(RUNTIME_CFLAGS) _cgo_defun.c |
Russ Cox | cce0111 | 2009-09-30 11:51:08 -0700 | [diff] [blame] | 175 | |
Russ Cox | cce0111 | 2009-09-30 11:51:08 -0700 | [diff] [blame] | 176 | # Generic build rules. |
| 177 | # These come last so that the rules above can override them |
| 178 | # for more specific file names. |
| 179 | %.$O: %.c |
Russ Cox | aafe474e | 2010-08-24 20:00:33 -0400 | [diff] [blame] | 180 | $(CC) $(CFLAGS) $*.c |
Russ Cox | cce0111 | 2009-09-30 11:51:08 -0700 | [diff] [blame] | 181 | |
| 182 | %.$O: %.s |
Russ Cox | aafe474e | 2010-08-24 20:00:33 -0400 | [diff] [blame] | 183 | $(AS) $*.s |
Russ Cox | cce0111 | 2009-09-30 11:51:08 -0700 | [diff] [blame] | 184 | |
| 185 | %.$O: $(HFILES) |
| 186 | |