vlang /
	
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 check_for_working_tcc
80
81ifdef prod
82VFLAGS+=-prod
83endif
84
85all: latest_vc latest_tcc
86ifdef WIN32
87 $(CC) $(CFLAGS) -std=c99 -municode -w -o v1.exe $(VC)/$(VCFILE) $(LDFLAGS)
88 v1.exe -no-parallel -o v2.exe $(VFLAGS) cmd/v
89 v2.exe -o $(V) $(VFLAGS) cmd/v
90 del v1.exe
91 del v2.exe
92else
93 $(CC) $(CFLAGS) -std=gnu99 -w -o v1.exe $(VC)/$(VCFILE) -lm -lpthread $(LDFLAGS)
94 ./v1.exe -no-parallel -o v2.exe $(VFLAGS) cmd/v
95 ./v2.exe -o $(V) $(VFLAGS) cmd/v
96 rm -rf v1.exe v2.exe
97endif
98 @$(V) run cmd/tools/detect_tcc.v
99 @echo "V has been successfully built"
100 @$(V) -version
101
102clean:
103 rm -rf $(TMPTCC)
104 rm -rf $(VC)
105
106ifndef local
107latest_vc: $(VC)/.git/config
108 cd $(VC) && $(GITCLEANPULL)
109else
110latest_vc:
111 @echo "Using local vc"
112endif
113
114check_for_working_tcc:
115 @$(TMPTCC)/tcc.exe --version > /dev/null 2> /dev/null || echo "The executable '$(TMPTCC)/tcc.exe' does not work."
116
117fresh_vc:
118 rm -rf $(VC)
119 $(GITFASTCLONE) $(VCREPO) $(VC)
120
121ifndef local
122latest_tcc: $(TMPTCC)/.git/config
123 cd $(TMPTCC) && $(GITCLEANPULL)
124 @$(MAKE) --quiet check_for_working_tcc 2> /dev/null
125else
126latest_tcc:
127 @echo "Using local tcc"
128 @$(MAKE) --quiet check_for_working_tcc 2> /dev/null
129endif
130
131fresh_tcc:
132 rm -rf $(TMPTCC)
133ifndef local
134# Check wether a TCC branch exists for the user's system configuration.
135ifneq (,$(findstring thirdparty-$(TCCOS)-$(TCCARCH), $(shell git ls-remote --heads $(TCCREPO) | sed 's/^[a-z0-9]*\trefs.heads.//')))
136 $(GITFASTCLONE) --branch thirdparty-$(TCCOS)-$(TCCARCH) $(TCCREPO) $(TMPTCC)
137 @$(MAKE) --quiet check_for_working_tcc 2> /dev/null
138else
139 @echo 'Pre-built TCC not available for thirdparty-$(TCCOS)-$(TCCARCH) at $(TCCREPO), will use the system compiler: $(CC)'
140 $(GITFASTCLONE) --branch thirdparty-unknown-unknown $(TCCREPO) $(TMPTCC)
141 @$(MAKE) --quiet check_for_working_tcc 2> /dev/null
142endif
143else
144 @echo "Using local tccbin"
145 @$(MAKE) --quiet check_for_working_tcc 2> /dev/null
146endif
147
148$(TMPTCC)/.git/config:
149 $(MAKE) fresh_tcc
150
151$(VC)/.git/config:
152 $(MAKE) fresh_vc
153
154asan:
155 $(MAKE) all CFLAGS='-fsanitize=address,undefined'
156
157selfcompile:
158 $(V) -cg -o v cmd/v
159
160selfcompile-static:
161 $(V) -cg -cflags '--static' -o v-static cmd/v
162
163### NB: Please keep this Makefile and make.bat simple.
164install:
165 @echo 'Please use `sudo ./v symlink` instead.'