Skip to content

Commit af7750f

Browse files
committed
refactor: make code reusable
1 parent 25b59b6 commit af7750f

File tree

1 file changed

+17
-19
lines changed

1 file changed

+17
-19
lines changed

Makefile

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
CC := clang
22
AR := llvm-ar
33

4-
ifdef CFI
5-
$(info Info: build with CFI enabled)
6-
CLANG_VERSION := $(shell $(CC) --version | head -n1 | sed -n 's/.*version \([0-9]*\)\..*/\1/p')
7-
ifeq ($(shell test $(CLANG_VERSION) -ge 21; echo $$?),0)
8-
CFLAGS := --target=riscv64 -march=rv64imc_zba_zbb_zbc_zbs_zicfiss1p0_zicfilp1p0 -mabi=lp64
9-
CFLAGS += -menable-experimental-extensions -fcf-protection=full
10-
ifeq ($(CFI),unlabeled)
11-
CFLAGS += -mcf-branch-label-scheme=unlabeled
12-
else ifeq ($(CFI),func-sig)
13-
CFLAGS += -mcf-branch-label-scheme=func-sig
14-
else
15-
$(error Error: CFI is set to '$(CFI)' but expected 'unlabeled' or 'func-sig')
16-
endif
17-
else
18-
$(error Error: CFI requires clang version 21 or above, but found version $(CLANG_VERSION))
19-
endif
20-
else
21-
CFLAGS := --target=riscv64 -march=rv64imc_zba_zbb_zbc_zbs -mabi=lp64
22-
endif
4+
define get_cfi_cflags
5+
$(if $(1),\
6+
$(info Info: build with CFI enabled)\
7+
$(eval _CLANG_VERSION := $(shell $(CC) --version | head -n1 | sed -n 's/.*version \([0-9]*\)\..*/\1/p'))\
8+
$(if $(shell test $(_CLANG_VERSION) -ge 21 && echo 1),\
9+
--target=riscv64 -march=rv64imc_zba_zbb_zbc_zbs_zicfiss1p0_zicfilp1p0 -mabi=lp64 \
10+
-menable-experimental-extensions -fcf-protection=full \
11+
$(if $(filter unlabeled,$(1)),\
12+
-mcf-branch-label-scheme=unlabeled,\
13+
$(if $(filter func-sig,$(1)),\
14+
-mcf-branch-label-scheme=func-sig,\
15+
$(error Error: CFI is set to '$(1)' but expected 'unlabeled' or 'func-sig'))),\
16+
$(error Error: CFI requires clang version 21 or above, but found version $(_CLANG_VERSION))),\
17+
--target=riscv64 -march=rv64imc_zba_zbb_zbc_zbs -mabi=lp64)
18+
endef
19+
20+
CFLAGS := $(call get_cfi_cflags,$(CFI))
2321

2422
CFLAGS += -Os
2523
CFLAGS += -fdata-sections -ffunction-sections -fno-builtin -fvisibility=hidden -fomit-frame-pointer

0 commit comments

Comments
 (0)