1 # SPDX-License-Identifier: GPL-2.0
2 # Copyright (c) 2022 Meta Platforms, Inc. and affiliates.
3 include ..
/build
/Build.
include
4 include ..
/scripts
/Makefile.arch
5 include ..
/scripts
/Makefile.
include
10 ifneq ($(filter %/,$(LLVM
)),)
11 LLVM_PREFIX
:= $(LLVM
)
12 else ifneq ($(filter -%,$(LLVM
)),)
13 LLVM_SUFFIX
:= $(LLVM
)
16 CLANG_TARGET_FLAGS_arm
:= arm-linux-gnueabi
17 CLANG_TARGET_FLAGS_arm64
:= aarch64-linux-gnu
18 CLANG_TARGET_FLAGS_hexagon
:= hexagon-linux-musl
19 CLANG_TARGET_FLAGS_m68k
:= m68k-linux-gnu
20 CLANG_TARGET_FLAGS_mips
:= mipsel-linux-gnu
21 CLANG_TARGET_FLAGS_powerpc
:= powerpc64le-linux-gnu
22 CLANG_TARGET_FLAGS_riscv
:= riscv64-linux-gnu
23 CLANG_TARGET_FLAGS_s390
:= s390x-linux-gnu
24 CLANG_TARGET_FLAGS_x86
:= x86_64-linux-gnu
25 CLANG_TARGET_FLAGS
:= $(CLANG_TARGET_FLAGS_
$(ARCH
))
27 ifeq ($(CROSS_COMPILE
),)
28 ifeq ($(CLANG_TARGET_FLAGS
),)
29 $(error Specify CROSS_COMPILE or add
'--target=' option to lib.mk
)
31 CLANG_FLAGS
+= --target
=$(CLANG_TARGET_FLAGS
)
32 endif # CLANG_TARGET_FLAGS
34 CLANG_FLAGS
+= --target
=$(notdir $(CROSS_COMPILE
:%-=%))
37 CC
:= $(LLVM_PREFIX
)clang
$(LLVM_SUFFIX
) $(CLANG_FLAGS
) -fintegrated-as
39 CC
:= $(CROSS_COMPILE
)gcc
42 CURDIR
:= $(abspath .
)
43 TOOLSDIR
:= $(abspath ..
)
44 LIBDIR
:= $(TOOLSDIR
)/lib
45 BPFDIR
:= $(LIBDIR
)/bpf
46 TOOLSINCDIR
:= $(TOOLSDIR
)/include
47 BPFTOOLDIR
:= $(TOOLSDIR
)/bpf
/bpftool
48 APIDIR
:= $(TOOLSINCDIR
)/uapi
49 GENDIR
:= $(abspath ..
/..
/include/generated
)
50 GENHDR
:= $(GENDIR
)/autoconf.h
53 OUTPUT_DIR
:= $(CURDIR
)/build
55 OUTPUT_DIR
:= $(O
)/build
57 OBJ_DIR
:= $(OUTPUT_DIR
)/obj
58 INCLUDE_DIR
:= $(OUTPUT_DIR
)/include
59 BPFOBJ_DIR
:= $(OBJ_DIR
)/libbpf
60 SCXOBJ_DIR
:= $(OBJ_DIR
)/sched_ext
61 BINDIR
:= $(OUTPUT_DIR
)/bin
62 BPFOBJ
:= $(BPFOBJ_DIR
)/libbpf.a
63 ifneq ($(CROSS_COMPILE
),)
64 HOST_BUILD_DIR
:= $(OBJ_DIR
)/host
65 HOST_OUTPUT_DIR
:= host-tools
66 HOST_INCLUDE_DIR
:= $(HOST_OUTPUT_DIR
)/include
68 HOST_BUILD_DIR
:= $(OBJ_DIR
)
69 HOST_OUTPUT_DIR
:= $(OUTPUT_DIR
)
70 HOST_INCLUDE_DIR
:= $(INCLUDE_DIR
)
72 HOST_BPFOBJ
:= $(HOST_BUILD_DIR
)/libbpf
/libbpf.a
73 RESOLVE_BTFIDS
:= $(HOST_BUILD_DIR
)/resolve_btfids
/resolve_btfids
74 DEFAULT_BPFTOOL
:= $(HOST_OUTPUT_DIR
)/sbin
/bpftool
76 VMLINUX_BTF_PATHS ?
= $(if
$(O
),$(O
)/vmlinux
) \
77 $(if
$(KBUILD_OUTPUT
),$(KBUILD_OUTPUT
)/vmlinux
) \
79 /sys
/kernel
/btf
/vmlinux \
80 /boot
/vmlinux-
$(shell uname
-r
)
81 VMLINUX_BTF ?
= $(abspath
$(firstword $(wildcard $(VMLINUX_BTF_PATHS
))))
82 ifeq ($(VMLINUX_BTF
),)
83 $(error Cannot find a vmlinux for VMLINUX_BTF at any of
"$(VMLINUX_BTF_PATHS)")
86 BPFTOOL ?
= $(DEFAULT_BPFTOOL
)
88 ifneq ($(wildcard $(GENHDR
)),)
89 GENFLAGS
:= -DHAVE_GENHDR
92 CFLAGS
+= -g
-O2
-rdynamic
-pthread
-Wall
-Werror
$(GENFLAGS
) \
93 -I
$(INCLUDE_DIR
) -I
$(GENDIR
) -I
$(LIBDIR
) \
94 -I
$(TOOLSINCDIR
) -I
$(APIDIR
) -I
$(CURDIR
)/include
96 # Silence some warnings when compiled with clang
98 CFLAGS
+= -Wno-unused-command-line-argument
101 LDFLAGS
= -lelf
-lz
-lpthread
103 IS_LITTLE_ENDIAN
= $(shell $(CC
) -dM
-E
- </dev
/null | \
104 grep
'define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__')
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.
110 # Use '-idirafter': Don't interfere with include mechanics except where the
111 # build would have failed anyways.
112 define 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)}')
118 BPF_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
) \
123 $(call get_sys_includes
,$(CLANG
)) \
124 -Wall
-Wno-compare-distinct-pointer-types \
127 # sort removes libbpf duplicates when not cross-building
128 MAKE_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
))
133 $(call msg
,MKDIR
,,$@
)
136 $(BPFOBJ
): $(wildcard $(BPFDIR
)/*.
[ch
] $(BPFDIR
)/Makefile
) \
137 $(APIDIR
)/linux
/bpf.h \
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
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
153 $(INCLUDE_DIR
)/vmlinux.h
: $(VMLINUX_BTF
) $(BPFTOOL
) |
$(INCLUDE_DIR
)
156 $(Q
)$(BPFTOOL
) btf dump file
$(VMLINUX_BTF
) format c
> $@
159 $(Q
)cp
"$(VMLINUX_H)" $@
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
$@
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
)
177 SCX_COMMON_DEPS
:= include/scx
/common.h
include/scx
/user_exit_info.h |
$(BINDIR
)
179 c-sched-targets
= scx_simple scx_qmap scx_central scx_flatcg
181 $(addprefix $(BINDIR
)/,$(c-sched-targets
)): \
183 $(filter-out %.bpf.c
,%.c
) \
184 $(INCLUDE_DIR
)/%.bpf.skel.h \
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
)
190 $(c-sched-targets
): %: $(BINDIR
)/%
193 $(Q
)mkdir
-p
$(DESTDIR
)/usr
/local
/bin
/
194 $(Q
)cp
$(BINDIR
)/* $(DESTDIR
)/usr
/local
/bin
/
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
)
202 @echo
'Building targets'
203 @echo
'================'
205 @echo
' all - Compile all schedulers'
207 @echo
'Alternatively, you may compile individual schedulers:'
209 @printf
' %s\n' $(c-sched-targets
)
211 @echo
'For any scheduler build target, you may specify an alternative'
212 @echo
'build output path with the O= environment variable. For example:'
214 @echo
' O=/tmp/sched_ext make all'
216 @echo
'will compile all schedulers, and emit the build artifacts to'
217 @echo
'/tmp/sched_ext/build.'
220 @echo
'Installing targets'
221 @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:'
227 @echo
' DESTDIR=/tmp/sched_ext make install'
229 @echo
' will build the schedulers in CWD/build, and'
230 @echo
' install the schedulers to /tmp/sched_ext/usr/bin.'
233 @echo
'Cleaning targets'
234 @echo
'================'
236 @echo
' clean - Remove all generated files'
238 all_targets
: $(c-sched-targets
)
240 .PHONY
: all all_targets
$(c-sched-targets
) clean help
242 # delete failed targets
245 # keep intermediate (.bpf.skel.h, .bpf.o, etc) targets