1# SPDX-License-Identifier: GPL-2.0
2# Copyright (c) 2022 Meta Platforms, Inc. and affiliates.
3include ../build/Build.include
4include ../scripts/Makefile.arch
5include ../scripts/Makefile.include
6
7all: all_targets
8
9ifneq ($(LLVM),)
10ifneq ($(filter %/,$(LLVM)),)
11LLVM_PREFIX := $(LLVM)
12else ifneq ($(filter -%,$(LLVM)),)
13LLVM_SUFFIX := $(LLVM)
14endif
15
16CLANG_TARGET_FLAGS_arm          := arm-linux-gnueabi
17CLANG_TARGET_FLAGS_arm64        := aarch64-linux-gnu
18CLANG_TARGET_FLAGS_hexagon      := hexagon-linux-musl
19CLANG_TARGET_FLAGS_m68k         := m68k-linux-gnu
20CLANG_TARGET_FLAGS_mips         := mipsel-linux-gnu
21CLANG_TARGET_FLAGS_powerpc      := powerpc64le-linux-gnu
22CLANG_TARGET_FLAGS_riscv        := riscv64-linux-gnu
23CLANG_TARGET_FLAGS_s390         := s390x-linux-gnu
24CLANG_TARGET_FLAGS_x86          := x86_64-linux-gnu
25CLANG_TARGET_FLAGS              := $(CLANG_TARGET_FLAGS_$(ARCH))
26
27ifeq ($(CROSS_COMPILE),)
28ifeq ($(CLANG_TARGET_FLAGS),)
29$(error Specify CROSS_COMPILE or add '--target=' option to lib.mk)
30else
31CLANG_FLAGS     += --target=$(CLANG_TARGET_FLAGS)
32endif # CLANG_TARGET_FLAGS
33else
34CLANG_FLAGS     += --target=$(notdir $(CROSS_COMPILE:%-=%))
35endif # CROSS_COMPILE
36
37CC := $(LLVM_PREFIX)clang$(LLVM_SUFFIX) $(CLANG_FLAGS) -fintegrated-as
38else
39CC := $(CROSS_COMPILE)gcc
40endif # LLVM
41
42CURDIR := $(abspath .)
43TOOLSDIR := $(abspath ..)
44LIBDIR := $(TOOLSDIR)/lib
45BPFDIR := $(LIBDIR)/bpf
46TOOLSINCDIR := $(TOOLSDIR)/include
47BPFTOOLDIR := $(TOOLSDIR)/bpf/bpftool
48APIDIR := $(TOOLSINCDIR)/uapi
49GENDIR := $(abspath ../../include/generated)
50GENHDR := $(GENDIR)/autoconf.h
51
52ifeq ($(O),)
53OUTPUT_DIR := $(CURDIR)/build
54else
55OUTPUT_DIR := $(O)/build
56endif # O
57OBJ_DIR := $(OUTPUT_DIR)/obj
58INCLUDE_DIR := $(OUTPUT_DIR)/include
59BPFOBJ_DIR := $(OBJ_DIR)/libbpf
60SCXOBJ_DIR := $(OBJ_DIR)/sched_ext
61BINDIR := $(OUTPUT_DIR)/bin
62BPFOBJ := $(BPFOBJ_DIR)/libbpf.a
63ifneq ($(CROSS_COMPILE),)
64HOST_BUILD_DIR		:= $(OBJ_DIR)/host
65HOST_OUTPUT_DIR	:= host-tools
66HOST_INCLUDE_DIR	:= $(HOST_OUTPUT_DIR)/include
67else
68HOST_BUILD_DIR		:= $(OBJ_DIR)
69HOST_OUTPUT_DIR	:= $(OUTPUT_DIR)
70HOST_INCLUDE_DIR	:= $(INCLUDE_DIR)
71endif
72HOST_BPFOBJ := $(HOST_BUILD_DIR)/libbpf/libbpf.a
73RESOLVE_BTFIDS := $(HOST_BUILD_DIR)/resolve_btfids/resolve_btfids
74DEFAULT_BPFTOOL := $(HOST_OUTPUT_DIR)/sbin/bpftool
75
76VMLINUX_BTF_PATHS ?= $(if $(O),$(O)/vmlinux)					\
77		     $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux)		\
78		     ../../vmlinux						\
79		     /sys/kernel/btf/vmlinux					\
80		     /boot/vmlinux-$(shell uname -r)
81VMLINUX_BTF ?= $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS))))
82ifeq ($(VMLINUX_BTF),)
83$(error Cannot find a vmlinux for VMLINUX_BTF at any of "$(VMLINUX_BTF_PATHS)")
84endif
85
86BPFTOOL ?= $(DEFAULT_BPFTOOL)
87
88ifneq ($(wildcard $(GENHDR)),)
89  GENFLAGS := -DHAVE_GENHDR
90endif
91
92CFLAGS += -g -O2 -rdynamic -pthread -Wall -Werror $(GENFLAGS)			\
93	  -I$(INCLUDE_DIR) -I$(GENDIR) -I$(LIBDIR)				\
94	  -I$(TOOLSINCDIR) -I$(APIDIR) -I$(CURDIR)/include
95
96# Silence some warnings when compiled with clang
97ifneq ($(LLVM),)
98CFLAGS += -Wno-unused-command-line-argument
99endif
100
101LDFLAGS = -lelf -lz -lpthread
102
103IS_LITTLE_ENDIAN = $(shell $(CC) -dM -E - </dev/null |				\
104			grep 'define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__')
105
106# Get Clang's default includes on this system, as opposed to those seen by
107# '-target bpf'. This fixes "missing" files on some architectures/distros,
108# such as asm/byteorder.h, asm/socket.h, asm/sockios.h, sys/cdefs.h etc.
109#
110# Use '-idirafter': Don't interfere with include mechanics except where the
111# build would have failed anyways.
112define get_sys_includes
113$(shell $(1) -v -E - </dev/null 2>&1 \
114	| sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }') \
115$(shell $(1) -dM -E - </dev/null | grep '__riscv_xlen ' | awk '{printf("-D__riscv_xlen=%d -D__BITS_PER_LONG=%d", $$3, $$3)}')
116endef
117
118BPF_CFLAGS = -g -D__TARGET_ARCH_$(SRCARCH)					\
119	     $(if $(IS_LITTLE_ENDIAN),-mlittle-endian,-mbig-endian)		\
120	     -I$(CURDIR)/include -I$(CURDIR)/include/bpf-compat			\
121	     -I$(INCLUDE_DIR) -I$(APIDIR)					\
122	     -I../../include							\
123	     $(call get_sys_includes,$(CLANG))					\
124	     -Wall -Wno-compare-distinct-pointer-types				\
125	     -O2 -mcpu=v3
126
127# sort removes libbpf duplicates when not cross-building
128MAKE_DIRS := $(sort $(OBJ_DIR)/libbpf $(HOST_BUILD_DIR)/libbpf			\
129	       $(HOST_BUILD_DIR)/bpftool $(HOST_BUILD_DIR)/resolve_btfids	\
130	       $(INCLUDE_DIR) $(SCXOBJ_DIR) $(BINDIR))
131
132$(MAKE_DIRS):
133	$(call msg,MKDIR,,$@)
134	$(Q)mkdir -p $@
135
136$(BPFOBJ): $(wildcard $(BPFDIR)/*.[ch] $(BPFDIR)/Makefile)			\
137	   $(APIDIR)/linux/bpf.h						\
138	   | $(OBJ_DIR)/libbpf
139	$(Q)$(MAKE) $(submake_extras) -C $(BPFDIR) OUTPUT=$(OBJ_DIR)/libbpf/	\
140		    EXTRA_CFLAGS='-g -O0 -fPIC'					\
141		    DESTDIR=$(OUTPUT_DIR) prefix= all install_headers
142
143$(DEFAULT_BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile)	\
144		    $(HOST_BPFOBJ) | $(HOST_BUILD_DIR)/bpftool
145	$(Q)$(MAKE) $(submake_extras)  -C $(BPFTOOLDIR)				\
146		    ARCH= CROSS_COMPILE= CC=$(HOSTCC) LD=$(HOSTLD)		\
147		    EXTRA_CFLAGS='-g -O0'					\
148		    OUTPUT=$(HOST_BUILD_DIR)/bpftool/				\
149		    LIBBPF_OUTPUT=$(HOST_BUILD_DIR)/libbpf/			\
150		    LIBBPF_DESTDIR=$(HOST_OUTPUT_DIR)/				\
151		    prefix= DESTDIR=$(HOST_OUTPUT_DIR)/ install-bin
152
153$(INCLUDE_DIR)/vmlinux.h: $(VMLINUX_BTF) $(BPFTOOL) | $(INCLUDE_DIR)
154ifeq ($(VMLINUX_H),)
155	$(call msg,GEN,,$@)
156	$(Q)$(BPFTOOL) btf dump file $(VMLINUX_BTF) format c > $@
157else
158	$(call msg,CP,,$@)
159	$(Q)cp "$(VMLINUX_H)" $@
160endif
161
162$(SCXOBJ_DIR)/%.bpf.o: %.bpf.c $(INCLUDE_DIR)/vmlinux.h include/scx/*.h		\
163		       | $(BPFOBJ) $(SCXOBJ_DIR)
164	$(call msg,CLNG-BPF,,$(notdir $@))
165	$(Q)$(CLANG) $(BPF_CFLAGS) -target bpf -c $< -o $@
166
167$(INCLUDE_DIR)/%.bpf.skel.h: $(SCXOBJ_DIR)/%.bpf.o $(INCLUDE_DIR)/vmlinux.h $(BPFTOOL)
168	$(eval sched=$(notdir $@))
169	$(call msg,GEN-SKEL,,$(sched))
170	$(Q)$(BPFTOOL) gen object $(<:.o=.linked1.o) $<
171	$(Q)$(BPFTOOL) gen object $(<:.o=.linked2.o) $(<:.o=.linked1.o)
172	$(Q)$(BPFTOOL) gen object $(<:.o=.linked3.o) $(<:.o=.linked2.o)
173	$(Q)diff $(<:.o=.linked2.o) $(<:.o=.linked3.o)
174	$(Q)$(BPFTOOL) gen skeleton $(<:.o=.linked3.o) name $(subst .bpf.skel.h,,$(sched)) > $@
175	$(Q)$(BPFTOOL) gen subskeleton $(<:.o=.linked3.o) name $(subst .bpf.skel.h,,$(sched)) > $(@:.skel.h=.subskel.h)
176
177SCX_COMMON_DEPS := include/scx/common.h include/scx/user_exit_info.h | $(BINDIR)
178
179c-sched-targets = scx_simple scx_qmap scx_central scx_flatcg
180
181$(addprefix $(BINDIR)/,$(c-sched-targets)): \
182	$(BINDIR)/%: \
183		$(filter-out %.bpf.c,%.c) \
184		$(INCLUDE_DIR)/%.bpf.skel.h \
185		$(SCX_COMMON_DEPS)
186	$(eval sched=$(notdir $@))
187	$(CC) $(CFLAGS) -c $(sched).c -o $(SCXOBJ_DIR)/$(sched).o
188	$(CC) -o $@ $(SCXOBJ_DIR)/$(sched).o $(HOST_BPFOBJ) $(LDFLAGS)
189
190$(c-sched-targets): %: $(BINDIR)/%
191
192install: all
193	$(Q)mkdir -p $(DESTDIR)/usr/local/bin/
194	$(Q)cp $(BINDIR)/* $(DESTDIR)/usr/local/bin/
195
196clean:
197	rm -rf $(OUTPUT_DIR) $(HOST_OUTPUT_DIR)
198	rm -f *.o *.bpf.o *.bpf.skel.h *.bpf.subskel.h
199	rm -f $(c-sched-targets)
200
201help:
202	@echo   'Building targets'
203	@echo   '================'
204	@echo   ''
205	@echo   '  all		  - Compile all schedulers'
206	@echo   ''
207	@echo   'Alternatively, you may compile individual schedulers:'
208	@echo   ''
209	@printf '  %s\n' $(c-sched-targets)
210	@echo   ''
211	@echo   'For any scheduler build target, you may specify an alternative'
212	@echo   'build output path with the O= environment variable. For example:'
213	@echo   ''
214	@echo   '   O=/tmp/sched_ext make all'
215	@echo   ''
216	@echo   'will compile all schedulers, and emit the build artifacts to'
217	@echo   '/tmp/sched_ext/build.'
218	@echo   ''
219	@echo   ''
220	@echo   'Installing targets'
221	@echo   '=================='
222	@echo   ''
223	@echo   '  install	  - Compile and install all schedulers to /usr/bin.'
224	@echo   '		    You may specify the DESTDIR= environment variable'
225	@echo   '		    to indicate a prefix for /usr/bin. For example:'
226	@echo   ''
227	@echo   '                     DESTDIR=/tmp/sched_ext make install'
228	@echo   ''
229	@echo   '		    will build the schedulers in CWD/build, and'
230	@echo   '		    install the schedulers to /tmp/sched_ext/usr/bin.'
231	@echo   ''
232	@echo   ''
233	@echo   'Cleaning targets'
234	@echo   '================'
235	@echo   ''
236	@echo   '  clean		  - Remove all generated files'
237
238all_targets: $(c-sched-targets)
239
240.PHONY: all all_targets $(c-sched-targets) clean help
241
242# delete failed targets
243.DELETE_ON_ERROR:
244
245# keep intermediate (.bpf.skel.h, .bpf.o, etc) targets
246.SECONDARY:
247