v /
1CC ?= cc
2CFLAGS ?=
3LDFLAGS ?=
4TMPDIR ?= /tmp
5VROOT ?= .
6VC ?= ./vc
7V ?= ./v
8VCREPO ?= https://github.com/vlang/vc
9TCCREPO ?= https://github.com/vlang/tccbin
10
11VCFILE := v.c
12TMPTCC := $(VROOT)/thirdparty/tcc
13TCCOS := unknown
14TCCARCH := unknown
15GITCLEANPULL := git clean -xf && git pull --quiet
16GITFASTCLONE := git clone --depth 1 --quiet --single-branch
17
18#### Platform detections and overrides:
19_SYS := $(shell uname 2>/dev/null || echo Unknown)
20_SYS := $(patsubst MSYS%,MSYS,$(_SYS))
21_SYS := $(patsubst MINGW%,MinGW,$(_SYS))
22
23ifneq ($(filter $(_SYS),MSYS MinGW),)
24WIN32 := 1
25V:=./v.exe
26endif
27
28ifeq ($(_SYS),Linux)
29LINUX := 1
30TCCOS := linux
31endif
32
33ifeq ($(_SYS),Darwin)
34MAC := 1
35TCCOS := macos
36endif
37
38ifeq ($(_SYS),FreeBSD)
39TCCOS := freebsd
40LDFLAGS += -lexecinfo
41endif
42
43ifeq ($(_SYS),NetBSD)
44TCCOS := netbsd
45LDFLAGS += -lexecinfo
46endif
47
48ifdef ANDROID_ROOT
49ANDROID := 1
50undefine LINUX
51TCCOS := android
52endif
53#####
54
55ifdef WIN32
56TCCOS := windows
57VCFILE := v_win.c
58endif
59
60TCCARCH := $(shell uname -m 2>/dev/null || echo unknown)
61
62ifeq ($(TCCARCH),x86_64)
63 TCCARCH := amd64
64else
65ifneq ($(filter x86%,$(TCCARCH)),)
66 TCCARCH := i386
67else
68ifeq ($(TCCARCH),arm64)
69 TCCARCH := arm64
70else
71ifneq ($(filter arm%,$(TCCARCH)),)
72 TCCARCH := arm
73# otherwise, just use the arch name
74endif
75endif
76endif
77endif
78
79.PHONY: all clean fresh_vc fresh_tcc
80
81ifdef prod
82VFLAGS+=-prod
83endif
84
85all: latest_vc latest_tcc
86ifdef WIN32
87 $(CC) $(CFLAGS) -g -std=c99 -municode -w -o $(V) $(VC)/$(VCFILE) $(LDFLAGS)
88 $(V) -o v2.exe $(VFLAGS) cmd/v
89 move /y v2.exe v.exe
90else
91 $(CC) $(CFLAGS) -g -std=gnu99 -w -o $(V) $(VC)/$(VCFILE) -lm -lpthread $(LDFLAGS)
92 $(V) -o v2.exe $(VFLAGS) cmd/v
93 mv -f v2.exe v
94endif
95 @echo "V has been successfully built"
96 @$(V) -version
97
98clean:
99 rm -rf $(TMPTCC)
100 rm -rf $(VC)
101
102ifndef local
103latest_vc: $(VC)/.git/config
104 cd $(VC) && $(GITCLEANPULL)
105else
106latest_vc:
107 @echo "Using local vc"
108endif
109
110fresh_vc:
111 rm -rf $(VC)
112 $(GITFASTCLONE) $(VCREPO) $(VC)
113
114ifndef local
115latest_tcc: $(TMPTCC)/.git/config
116 cd $(TMPTCC) && $(GITCLEANPULL)
117else
118latest_tcc:
119 @echo "Using local tcc"
120endif
121
122fresh_tcc:
123 rm -rf $(TMPTCC)
124# Check wether a TCC branch exists for the user's system configuration.
125ifneq (,$(findstring thirdparty-$(TCCOS)-$(TCCARCH), $(shell git ls-remote --heads $(TCCREPO) | sed 's/^[a-z0-9]*\trefs.heads.//')))
126 $(GITFASTCLONE) --branch thirdparty-$(TCCOS)-$(TCCARCH) $(TCCREPO) $(TMPTCC)
127else
128 @echo 'Pre-built TCC not available for thirdparty-$(TCCOS)-$(TCCARCH) at $(TCCREPO), will use the system compiler: $(CC)'
129 $(GITFASTCLONE) --branch thirdparty-unknown-unknown $(TCCREPO) $(TMPTCC)
130endif
131
132$(TMPTCC)/.git/config:
133 $(MAKE) fresh_tcc
134
135$(VC)/.git/config:
136 $(MAKE) fresh_vc
137
138asan:
139 $(MAKE) all CFLAGS='-fsanitize=address,undefined'
140
141selfcompile:
142 $(V) -cg -o v cmd/v
143
144selfcompile-static:
145 $(V) -cg -cflags '--static' -o v-static cmd/v
146
147### NB: Please keep this Makefile and make.bat simple.
148install:
149 @echo 'Please use `sudo v symlink` instead.'