# Copyright 2021 The CC Authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.

.PHONY:	ast.go build_all_targets clean cpu edit editor mem win64 win32 tmp test

ast.go:
	go generate
	pcregrep -nM 'panic|runtime|FAIL|TODO|<nil>|Output:\n}' ast_test.go || true

build_all_targets:
	GOOS=darwin GOARCH=amd64 go build -v ./...
	GOOS=darwin GOARCH=arm64 go build -v ./...
	GOOS=freebsd GOARCH=386 go build -v ./...
	GOOS=freebsd GOARCH=amd64 go build -v ./...
	GOOS=freebsd GOARCH=arm go build -v ./...
	GOOS=freebsd GOARCH=arm64 go build -v ./...
	GOOS=illumos GOARCH=amd64 go build -v ./...
	GOOS=linux GOARCH=386 go build -v ./...
	GOOS=linux GOARCH=amd64 go build -v ./...
	GOOS=linux GOARCH=arm go build -v ./...
	GOOS=linux GOARCH=arm64 go build -v ./...
	GOOS=linux GOARCH=loong64 go build -v ./...
	GOOS=linux GOARCH=ppc64le go build -v ./...
	GOOS=linux GOARCH=riscv64 go build -v ./...
	GOOS=linux GOARCH=s390x go build -v ./...
	GOOS=netbsd GOARCH=386 go build -v ./...
	GOOS=netbsd GOARCH=amd64 go build -v ./...
	GOOS=netbsd GOARCH=arm go build -v ./...
	GOOS=openbsd GOARCH=386 go build -v ./...
	GOOS=openbsd GOARCH=amd64 go build -v ./...
	GOOS=openbsd GOARCH=arm64 go build -v ./...
	GOOS=windows GOARCH=386 go build -v ./...
	GOOS=windows GOARCH=amd64 go build -v ./...
	GOOS=windows GOARCH=arm64 go build -v ./...

clean:
	rm -f log-* cpu.test mem.test *.out
	go clean

cpu:
	go test -c -o cpu.test
	./cpu.test -test.run TestTranslationPhase4 -re 20001226-1.c -test.cpuprofile cpu.out
	go tool pprof --lines cpu.test cpu.out

edit:
	@touch log
	@if [ -f "Session.vim" ]; then gvim -S & else gvim -p Makefile cc.go & fi

editor: # ast.go
	gofmt -l -s -w *.go
	go test -o /dev/null -c
	go build -o /dev/null fakecc.go
	go install 2>&1 | tee log-editor
	staticcheck
	rm -f ast.go.orig

mem:
	go test -c -o mem.test
	./mem.test -test.run TestParserBug -re 9.c -test.memprofile mem.out
	go tool pprof --lines --web --alloc_space mem.test mem.out

win32:
	# on linux/amd64
	CC_TEST_CPP=i686-w64-mingw32-cpp-win32 GOARCH=386 go test 2>&1 | tee log

win64:
	# on linux/amd64
	CC_TEST_CPP=x86_64-w64-mingw32-cpp go test 2>&1 | tee log

tmp:
	go test -v -run Translate 2>&1 | tee log
	grep -a '^ \+TODO' log | sort | uniq -c | sort -n
	grep -a 'TOTAL\|PASS' log || true

test:
	go test -v -timeout 4h -short 2>&1 | tee log-test
