v
Raw file
		
1CC ?= cc
2CFLAGS ?=
3LDFLAGS ?=
4TMPDIR ?= /tmp
5VROOT ?= .
6VC ?= ./vc
7VEXE ?= ./v
8VCREPO ?= https://github.com/vlang/vc
9TCCREPO ?= https://github.com/vlang/tccbin
10LEGACYREPO ?= https://github.com/macports/macports-legacy-support
11
12VCFILE := v.c
13TMPTCC := $(VROOT)/thirdparty/tcc
14LEGACYLIBS := $(VROOT)/thirdparty/legacy
15TMPLEGACY := $(LEGACYLIBS)/source
16TCCOS := unknown
17TCCARCH := unknown
18GITCLEANPULL := git clean -xf && git pull --quiet
19GITFASTCLONE := git clone --depth 1 --quiet --single-branch
20
21#### Platform detections and overrides:
22_SYS := $(shell uname 2>/dev/null || echo Unknown)
23_SYS := $(patsubst MSYS%,MSYS,$(_SYS))
24_SYS := $(patsubst MINGW%,MinGW,$(_SYS))
25
26ifneq ($(filter $(_SYS),MSYS MinGW),)
27WIN32 := 1
28VEXE := ./v.exe
29endif
30
31ifeq ($(_SYS),Linux)
32LINUX := 1
33TCCOS := linux
34ifneq ($(shell ldd /bin/ls | grep musl),)
35TCCOS := linuxmusl
36endif
37endif
38
39ifeq ($(_SYS),Darwin)
40MAC := 1
41TCCOS := macos
42ifeq ($(shell expr $(shell uname -r | cut -d. -f1) \<= 15), 1)
43LEGACY := 1
44endif
45endif
46
47ifeq ($(_SYS),FreeBSD)
48TCCOS := freebsd
49LDFLAGS += -lexecinfo
50endif
51
52ifeq ($(_SYS),NetBSD)
53TCCOS := netbsd
54LDFLAGS += -lexecinfo
55endif
56
57ifdef ANDROID_ROOT
58ANDROID := 1
59undefine LINUX
60TCCOS := android
61endif
62#####
63
64ifdef WIN32
65TCCOS := windows
66VCFILE := v_win.c
67endif
68
69TCCARCH := $(shell uname -m 2>/dev/null || echo unknown)
70
71ifeq ($(TCCARCH),x86_64)
72 TCCARCH := amd64
73else
74ifneq ($(filter x86%,$(TCCARCH)),)
75 TCCARCH := i386
76else
77ifeq ($(TCCARCH),arm64)
78 TCCARCH := arm64
79else
80ifneq ($(filter arm%,$(TCCARCH)),)
81 TCCARCH := arm
82# otherwise, just use the arch name
83endif
84endif
85endif
86endif
87
88.PHONY: all clean check fresh_vc fresh_tcc fresh_legacy check_for_working_tcc
89
90ifdef prod
91VFLAGS+=-prod
92endif
93
94all: latest_vc latest_tcc latest_legacy
95ifdef WIN32
96 $(CC) $(CFLAGS) -std=c99 -municode -w -o v1.exe $(VC)/$(VCFILE) $(LDFLAGS)
97 v1.exe -no-parallel -o v2.exe $(VFLAGS) cmd/v
98 v2.exe -o $(VEXE) $(VFLAGS) cmd/v
99 del v1.exe
100 del v2.exe
101else
102ifdef LEGACY
103 $(MAKE) -C $(TMPLEGACY)
104 $(MAKE) -C $(TMPLEGACY) PREFIX=$(realpath $(LEGACYLIBS)) CFLAGS=$(CFLAGS) LDFLAGS=$(LDFLAGS) install
105 rm -rf $(TMPLEGACY)
106 $(eval override LDFLAGS+=-L$(realpath $(LEGACYLIBS))/lib -lMacportsLegacySupport)
107endif
108 $(CC) $(CFLAGS) -std=gnu99 -w -o v1.exe $(VC)/$(VCFILE) -lm -lpthread $(LDFLAGS)
109 ./v1.exe -no-parallel -o v2.exe $(VFLAGS) cmd/v
110 ./v2.exe -o $(VEXE) $(VFLAGS) cmd/v
111 rm -rf v1.exe v2.exe
112endif
113 @$(VEXE) run cmd/tools/detect_tcc.v
114 @echo "V has been successfully built"
115 @$(VEXE) -version
116
117clean:
118 rm -rf $(TMPTCC)
119 rm -rf $(LEGACYLIBS)
120 rm -rf $(VC)
121
122ifndef local
123latest_vc: $(VC)/.git/config
124 cd $(VC) && $(GITCLEANPULL)
125else
126latest_vc:
127 @echo "Using local vc"
128endif
129
130check_for_working_tcc:
131 @$(TMPTCC)/tcc.exe --version > /dev/null 2> /dev/null || echo "The executable '$(TMPTCC)/tcc.exe' does not work."
132
133fresh_vc:
134 rm -rf $(VC)
135 $(GITFASTCLONE) $(VCREPO) $(VC)
136
137ifndef local
138latest_tcc: $(TMPTCC)/.git/config
139 cd $(TMPTCC) && $(GITCLEANPULL)
140 @$(MAKE) --quiet check_for_working_tcc 2> /dev/null
141else
142latest_tcc:
143 @echo "Using local tcc"
144 @$(MAKE) --quiet check_for_working_tcc 2> /dev/null
145endif
146
147fresh_tcc:
148 rm -rf $(TMPTCC)
149ifndef local
150# Check wether a TCC branch exists for the user's system configuration.
151ifneq (,$(findstring thirdparty-$(TCCOS)-$(TCCARCH), $(shell git ls-remote --heads $(TCCREPO) | sed 's/^[a-z0-9]*\trefs.heads.//')))
152 $(GITFASTCLONE) --branch thirdparty-$(TCCOS)-$(TCCARCH) $(TCCREPO) $(TMPTCC)
153 @$(MAKE) --quiet check_for_working_tcc 2> /dev/null
154else
155 @echo 'Pre-built TCC not available for thirdparty-$(TCCOS)-$(TCCARCH) at $(TCCREPO), will use the system compiler: $(CC)'
156 $(GITFASTCLONE) --branch thirdparty-unknown-unknown $(TCCREPO) $(TMPTCC)
157 @$(MAKE) --quiet check_for_working_tcc 2> /dev/null
158endif
159else
160 @echo "Using local tccbin"
161 @$(MAKE) --quiet check_for_working_tcc 2> /dev/null
162endif
163
164ifndef local
165latest_legacy: $(TMPLEGACY)/.git/config
166ifdef LEGACY
167 cd $(TMPLEGACY) && $(GITCLEANPULL)
168endif
169else
170latest_legacy:
171ifdef LEGACY
172 @echo "Using local legacysupport"
173endif
174endif
175
176fresh_legacy:
177 rm -rf $(LEGACYLIBS)
178 $(GITFASTCLONE) $(LEGACYREPO) $(TMPLEGACY)
179
180$(TMPTCC)/.git/config:
181 $(MAKE) fresh_tcc
182
183$(VC)/.git/config:
184 $(MAKE) fresh_vc
185
186$(TMPLEGACY)/.git/config:
187ifdef LEGACY
188 $(MAKE) fresh_legacy
189endif
190
191asan:
192 $(MAKE) all CFLAGS='-fsanitize=address,undefined'
193
194selfcompile:
195 $(VEXE) -cg -o v cmd/v
196
197selfcompile-static:
198 $(VEXE) -cg -cflags '--static' -o v-static cmd/v
199
200### NB: Please keep this Makefile and make.bat simple.
201install:
202 @echo 'Please use `sudo ./v symlink` instead.'
203
204check:
205 $(VEXE) test-all