<commit_before># Copyright 2017 syzkaller project authors. All rights reserved.
# Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.

# There are 4 OS/arch pairs:
#  - BUILDOS/BUILDARCH: the current machine's pair used for build.
#  - HOSTOS/HOSTARCH: pair where syz-manager will run.
#  - TARGETOS/TARGETVMARCH: pair of the target OS under test.
#  - TARGETOS/TARGETARCH: pair of the target test process.
#
# The last 2 differ for e.g. amd64 OS and 386 test processes (compat syscall testing).
# All pairs default to the current machine. All but BUILD can be overriden.
#
# For example, to test linux/amd64 on linux/amd64, you just run:
#    make
# To test linux/arm64 from darwin/amd64 host, run:
#    make HOSTOS=darwin HOSTARCH=amd64 TARGETOS=linux TARGETARCH=arm64
# To test x86 compat syscalls, run:
#    make TARGETVMARCH=amd64 TARGETARCH=386
#
# There is a special case for Android builds:
#    NDK=/path/to/android/ndk make TARGETOS=android TARGETARCH=arm64
# But you still need to specify "target": "linux/arm64" in syz-manager config.

BUILDOS := $(shell go env GOOS)
BUILDARCH := $(shell go env GOARCH)
HOSTOS ?= $(BUILDOS)
HOSTARCH ?= $(BUILDARCH)
TARGETOS ?= $(HOSTOS)
TARGETARCH ?= $(HOSTARCH)
TARGETVMARCH ?= $(TARGETARCH)

ifeq ("$(TARGETARCH)", "amd64")
	CC = "x86_64-linux-gnu-gcc"
else ifeq ("$(TARGETARCH)", "386")
	CC = "x86_64-linux-gnu-gcc"
	ADDCFLAGS = "-m32"
else ifeq ("$(TARGETARCH)", "arm64")
	CC = "aarch64-linux-gnu-gcc"
else ifeq ("$(TARGETARCH)", "arm")
	CC = "arm-linux-gnueabihf-gcc"
	ADDCFLAGS = "-march=armv6t2"
else ifeq ("$(TARGETARCH)", "ppc64le")
	CC = "powerpc64le-linux-gnu-gcc"
endif

ifeq ("$(TARGETOS)", "android")
	override TARGETOS = "linux"
	ANDROID_API = 24
	BUILDGCCARCH = ""
	ANDROIDARCH = ""
	TOOLCHAIN = ""
	GCCBIN = ""
	ifeq ("$(TARGETARCH)", "amd64")
		ANDROIDARCH = "x86_64"
		TOOLCHAIN = "x86_64-4.9"
		GCCBIN = "x86_64-linux-android-g++"
	else ifeq ("$(TARGETARCH)", "386")
		ANDROIDARCH = "x86"
		TOOLCHAIN = "x86-4.9"
		GCCBIN = "i686-linux-android-g++"
	else ifeq ("$(TARGETARCH)", "arm64")
		ANDROIDARCH = "arm64"
		TOOLCHAIN = "aarch64-linux-android-4.9"
		GCCBIN = "aarch64-linux-android-g++"
	else ifeq ("$(TARGETARCH)", "arm")
		ANDROIDARCH = "arm"
		TOOLCHAIN = "arm-linux-androideabi-4.9"
		GCCBIN = "arm-linux-androideabi-g++"
	endif
	ifeq ("$(BUILDARCH)", "amd64")
		BUILDGCCARCH = "x86_64"
	else ifeq ("$(BUILDARCH)", "arm64")
		BUILDGCCARCH = "aarch64"
	endif
	CC = $(NDK)/toolchains/$(TOOLCHAIN)/prebuilt/$(BUILDOS)-$(BUILDGCCARCH)/bin/$(GCCBIN)
	CFLAGS = -I $(NDK)/sources/cxx-stl/llvm-libc++/include --sysroot=$(NDK)/platforms/android-$(ANDROID_API)/arch-$(ANDROIDARCH) -O1 -g -Wall -static
endif

GITREV=$(shell git rev-parse HEAD)
ifeq ($(`git diff --shortstat`), "")
	REV=$(GITREV)
else
	REV=$(GITREV)+
endif

NOSTATIC ?= 0
ifeq ($(NOSTATIC), 0)
	ADDCFLAGS += -static
endif

# Don't generate symbol table and DWARF debug info.
# Reduces build time and binary sizes considerably.
# That's only needed if you use gdb or nm.
# If you need that, build manually without these flags.
GOFLAGS := "-ldflags=-s -w -X github.com/google/syzkaller/sys.GitRevision=$(REV)"
ifneq ("$(GOTAGS)", "")
	GOFLAGS += "-tags=$(GOTAGS)"
endif

.PHONY: all host target \
	manager fuzzer executor \
	ci hub \
	execprog mutate prog2c stress repro upgrade db \
	bin/syz-sysgen bin/syz-extract bin/syz-fmt \
	extract generate \
	format tidy test arch presubmit clean

all: host target

host:
	GOOS=$(HOSTOS) GOARCH=$(HOSTARCH) go install ./syz-manager
	env GOOS=$(HOSTOS) GOARCH=$(HOSTARCH) $(MAKE) manager repro mutate prog2c db upgrade

target:
	GOOS=$(TARGETOS) GOARCH=$(TARGETVMARCH) go install ./syz-fuzzer
	env GOOS=$(TARGETOS) GOARCH=$(TARGETVMARCH) $(MAKE) fuzzer execprog stress
	$(MAKE) executor

# executor uses stacks of limited size, so no jumbo frames.
executor:
	mkdir -p ./bin/$(TARGETOS)_$(TARGETARCH)
	$(CC) -o ./bin/$(TARGETOS)_$(TARGETARCH)/syz-executor executor/executor.cc \
		-pthread -Wall -Wframe-larger-than=8192 -Wparentheses -Werror -O1 -g \
		$(ADDCFLAGS) $(CFLAGS) -DGIT_REVISION=\"$(REV)\"

manager:
	go build $(GOFLAGS) -o ./bin/syz-manager github.com/google/syzkaller/syz-manager

fuzzer:
	go build $(GOFLAGS) -o ./bin/$(TARGETOS)_$(TARGETVMARCH)/syz-fuzzer github.com/google/syzkaller/syz-fuzzer

execprog:
	go build $(GOFLAGS) -o ./bin/$(TARGETOS)_$(TARGETVMARCH)/syz-execprog github.com/google/syzkaller/tools/syz-execprog

ci:
	go build $(GOFLAGS) -o ./bin/syz-ci github.com/google/syzkaller/syz-ci

hub:
	go build $(GOFLAGS) -o ./bin/syz-hub github.com/google/syzkaller/syz-hub

repro:
	go build $(GOFLAGS) -o ./bin/syz-repro github.com/google/syzkaller/tools/syz-repro

mutate:
	go build $(GOFLAGS) -o ./bin/syz-mutate github.com/google/syzkaller/tools/syz-mutate

prog2c:
	go build $(GOFLAGS) -o ./bin/syz-prog2c github.com/google/syzkaller/tools/syz-prog2c

stress:
	go build $(GOFLAGS) -o ./bin/$(TARGETOS)_$(TARGETVMARCH)/syz-stress github.com/google/syzkaller/tools/syz-stress

db:
	go build $(GOFLAGS) -o ./bin/syz-db github.com/google/syzkaller/tools/syz-db

upgrade:
	go build $(GOFLAGS) -o ./bin/syz-upgrade github.com/google/syzkaller/tools/syz-upgrade

extract: bin/syz-extract
	LINUX=$(LINUX) ./sys/linux/extract.sh
bin/syz-extract:
	go build $(GOFLAGS) -o $@ ./sys/syz-extract

generate: bin/syz-sysgen
	bin/syz-sysgen
	go generate ./pkg/csource ./executor ./pkg/ifuzz ./pkg/kernel
	$(MAKE) format
bin/syz-sysgen:
	go build $(GOFLAGS) -o $@ ./sys/syz-sysgen

format: bin/syz-fmt
	go fmt ./...
	clang-format --style=file -i executor/*.cc executor/*.h tools/kcovtrace/*.c
	bin/syz-fmt sys/linux
bin/syz-fmt:
	go build $(GOFLAGS) -o $@ ./tools/syz-fmt

tidy:
	# A single check is enabled for now. But it's always fixable and proved to be useful.<commit_msg>Makefile: respect target os/arch in individual targets<commit_after># Copyright 2017 syzkaller project authors. All rights reserved.
# Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.

# There are 4 OS/arch pairs:
#  - BUILDOS/BUILDARCH: the current machine's pair used for build.
#  - HOSTOS/HOSTARCH: pair where syz-manager will run.
#  - TARGETOS/TARGETVMARCH: pair of the target OS under test.
#  - TARGETOS/TARGETARCH: pair of the target test process.
#
# The last 2 differ for e.g. amd64 OS and 386 test processes (compat syscall testing).
# All pairs default to the current machine. All but BUILD can be overriden.
#
# For example, to test linux/amd64 on linux/amd64, you just run:
#    make
# To test linux/arm64 from darwin/amd64 host, run:
#    make HOSTOS=darwin HOSTARCH=amd64 TARGETOS=linux TARGETARCH=arm64
# To test x86 compat syscalls, run:
#    make TARGETVMARCH=amd64 TARGETARCH=386
#
# There is a special case for Android builds:
#    NDK=/path/to/android/ndk make TARGETOS=android TARGETARCH=arm64
# But you still need to specify "target": "linux/arm64" in syz-manager config.

BUILDOS := $(shell go env GOOS)
BUILDARCH := $(shell go env GOARCH)
HOSTOS ?= $(BUILDOS)
HOSTARCH ?= $(BUILDARCH)
TARGETOS ?= $(HOSTOS)
TARGETARCH ?= $(HOSTARCH)
TARGETVMARCH ?= $(TARGETARCH)

ifeq ("$(TARGETARCH)", "amd64")
	CC = "x86_64-linux-gnu-gcc"
else ifeq ("$(TARGETARCH)", "386")
	CC = "x86_64-linux-gnu-gcc"
	ADDCFLAGS = "-m32"
else ifeq ("$(TARGETARCH)", "arm64")
	CC = "aarch64-linux-gnu-gcc"
else ifeq ("$(TARGETARCH)", "arm")
	CC = "arm-linux-gnueabihf-gcc"
	ADDCFLAGS = "-march=armv6t2"
else ifeq ("$(TARGETARCH)", "ppc64le")
	CC = "powerpc64le-linux-gnu-gcc"
endif

ifeq ("$(TARGETOS)", "android")
	override TARGETOS = "linux"
	ANDROID_API = 24
	BUILDGCCARCH = ""
	ANDROIDARCH = ""
	TOOLCHAIN = ""
	GCCBIN = ""
	ifeq ("$(TARGETARCH)", "amd64")
		ANDROIDARCH = "x86_64"
		TOOLCHAIN = "x86_64-4.9"
		GCCBIN = "x86_64-linux-android-g++"
	else ifeq ("$(TARGETARCH)", "386")
		ANDROIDARCH = "x86"
		TOOLCHAIN = "x86-4.9"
		GCCBIN = "i686-linux-android-g++"
	else ifeq ("$(TARGETARCH)", "arm64")
		ANDROIDARCH = "arm64"
		TOOLCHAIN = "aarch64-linux-android-4.9"
		GCCBIN = "aarch64-linux-android-g++"
	else ifeq ("$(TARGETARCH)", "arm")
		ANDROIDARCH = "arm"
		TOOLCHAIN = "arm-linux-androideabi-4.9"
		GCCBIN = "arm-linux-androideabi-g++"
	endif
	ifeq ("$(BUILDARCH)", "amd64")
		BUILDGCCARCH = "x86_64"
	else ifeq ("$(BUILDARCH)", "arm64")
		BUILDGCCARCH = "aarch64"
	endif
	CC = $(NDK)/toolchains/$(TOOLCHAIN)/prebuilt/$(BUILDOS)-$(BUILDGCCARCH)/bin/$(GCCBIN)
	CFLAGS = -I $(NDK)/sources/cxx-stl/llvm-libc++/include --sysroot=$(NDK)/platforms/android-$(ANDROID_API)/arch-$(ANDROIDARCH) -O1 -g -Wall -static
endif

GITREV=$(shell git rev-parse HEAD)
ifeq ($(`git diff --shortstat`), "")
	REV=$(GITREV)
else
	REV=$(GITREV)+
endif

NOSTATIC ?= 0
ifeq ($(NOSTATIC), 0)
	ADDCFLAGS += -static
endif

# Don't generate symbol table and DWARF debug info.
# Reduces build time and binary sizes considerably.
# That's only needed if you use gdb or nm.
# If you need that, build manually without these flags.
GOFLAGS := "-ldflags=-s -w -X github.com/google/syzkaller/sys.GitRevision=$(REV)"
ifneq ("$(GOTAGS)", "")
	GOFLAGS += "-tags=$(GOTAGS)"
endif

.PHONY: all host target \
	manager fuzzer executor \
	ci hub \
	execprog mutate prog2c stress repro upgrade db \
	bin/syz-sysgen bin/syz-extract bin/syz-fmt \
	extract generate \
	format tidy test arch presubmit clean

all: host target

host:
	GOOS=$(HOSTOS) GOARCH=$(HOSTARCH) go install ./syz-manager
	$(MAKE) manager repro mutate prog2c db upgrade

target:
	GOOS=$(TARGETOS) GOARCH=$(TARGETVMARCH) go install ./syz-fuzzer
	$(MAKE) fuzzer execprog stress executor

# executor uses stacks of limited size, so no jumbo frames.
executor:
	mkdir -p ./bin/$(TARGETOS)_$(TARGETARCH)
	$(CC) -o ./bin/$(TARGETOS)_$(TARGETARCH)/syz-executor executor/executor.cc \
		-pthread -Wall -Wframe-larger-than=8192 -Wparentheses -Werror -O1 -g \
		$(ADDCFLAGS) $(CFLAGS) -DGIT_REVISION=\"$(REV)\"

manager:
	GOOS=$(HOSTOS) GOARCH=$(HOSTARCH) go build $(GOFLAGS) -o ./bin/syz-manager github.com/google/syzkaller/syz-manager

fuzzer:
	GOOS=$(TARGETOS) GOARCH=$(TARGETVMARCH) go build $(GOFLAGS) -o ./bin/$(TARGETOS)_$(TARGETVMARCH)/syz-fuzzer github.com/google/syzkaller/syz-fuzzer

execprog:
	GOOS=$(TARGETOS) GOARCH=$(TARGETVMARCH) go build $(GOFLAGS) -o ./bin/$(TARGETOS)_$(TARGETVMARCH)/syz-execprog github.com/google/syzkaller/tools/syz-execprog

ci:
	GOOS=$(HOSTOS) GOARCH=$(HOSTARCH) go build $(GOFLAGS) -o ./bin/syz-ci github.com/google/syzkaller/syz-ci

hub:
	GOOS=$(HOSTOS) GOARCH=$(HOSTARCH) go build $(GOFLAGS) -o ./bin/syz-hub github.com/google/syzkaller/syz-hub

repro:
	GOOS=$(HOSTOS) GOARCH=$(HOSTARCH) go build $(GOFLAGS) -o ./bin/syz-repro github.com/google/syzkaller/tools/syz-repro

mutate:
	GOOS=$(HOSTOS) GOARCH=$(HOSTARCH) go build $(GOFLAGS) -o ./bin/syz-mutate github.com/google/syzkaller/tools/syz-mutate

prog2c:
	GOOS=$(HOSTOS) GOARCH=$(HOSTARCH) go build $(GOFLAGS) -o ./bin/syz-prog2c github.com/google/syzkaller/tools/syz-prog2c

stress:
	GOOS=$(TARGETOS) GOARCH=$(TARGETVMARCH) go build $(GOFLAGS) -o ./bin/$(TARGETOS)_$(TARGETVMARCH)/syz-stress github.com/google/syzkaller/tools/syz-stress

db:
	GOOS=$(HOSTOS) GOARCH=$(HOSTARCH) go build $(GOFLAGS) -o ./bin/syz-db github.com/google/syzkaller/tools/syz-db

upgrade:
	GOOS=$(HOSTOS) GOARCH=$(HOSTARCH) go build $(GOFLAGS) -o ./bin/syz-upgrade github.com/google/syzkaller/tools/syz-upgrade

extract: bin/syz-extract
	LINUX=$(LINUX) ./sys/linux/extract.sh
bin/syz-extract:
	go build $(GOFLAGS) -o $@ ./sys/syz-extract

generate: bin/syz-sysgen
	bin/syz-sysgen
	go generate ./pkg/csource ./executor ./pkg/ifuzz ./pkg/kernel
	$(MAKE) format
bin/syz-sysgen:
	go build $(GOFLAGS) -o $@ ./sys/syz-sysgen

format: bin/syz-fmt
	go fmt ./...
	clang-format --style=file -i executor/*.cc executor/*.h tools/kcovtrace/*.c
	bin/syz-fmt sys/linux
bin/syz-fmt:
	go build $(GOFLAGS) -o $@ ./tools/syz-fmt

tidy:
	# A single check is enabled for now. But it's always fixable and proved to be useful.
{'fraction_non_alphanumeric': 0.15966175485599465, 'fraction_numerical': 0.020931011386470195, 'mean_word_length': 4.197998259355962, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2900993', 'n_tokens_mistral': 4878, 'n_tokens_neox': 4578, 'n_words': 1150}