Skip to contentSkip to conversation

Makefile: fix support checks that use grep

PATCH Applied Trevor Gamblin 2 participants 5 messages Sep 23 – Sep 28 Kbuild

lore
Share & tools
lore
#0 lore raw

v2 cover Makefile: fix support checks that use grep

When building OpenSBI in environments where extensions from the ISA are used to generate build paths (e.g. Yocto Project), the Makefile checks which use grep to look for patterns can incorrectly match against the build paths, rather than the command's exit status. This means a workaround is needed to handle checking for extensions like zicsr and zifencei. Instead, make the following variable assignments use the same exit status pattern as OPENSBI_LD_PIE:

CC_SUPPORT_ZICSR_ZIFENCEI
CC_SUPPORT_STRICT_ALIGN
CC_SUPPORT_SAVE_RESTORE

Tested by building a full image in Yocto including OpenSBI and booting it in QEMU.

Note that only the first patch (for CC_SUPPORT_ZICSR_ZIFENCEI) is needed to fix the Yocto QEMU case, but I've submitted the remaining two for consistency. Also since submitting the first version of this series two additional uses of grep have appeared in the Makefile, but I've opted not to change them since they don't appear to result in the same issue observed in Yocto.

See also: https://bugzilla.yoctoproject.org/show_bug.cgi?id=15897

As an example, in my Yocto build environment, values like CC for OpenSBI are set like:

|export CC="riscv64-poky-linux-gcc -march=rv64imafdc_zicsr_zifencei -mabi=lp64d -Wl,-O1 -Wl,--hash-style=gnu -Wl,--as-needed -Wl,-z,relro,-z,now -fstack-protector-strong -O2 -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=format-security --sysroot=/home/tgamblin/workspace/ypbuilds/poky-qemuriscv64/build/tmp/work/riscv64imafdc_zicsr_zifencei-poky-linux/opensbi/1.9/recipe-sysroot"

and the build error:

| /tmp/ccnpajj3.s: Assembler messages:
| /home/tgamblin/workspace/ypbuilds/poky-qemuriscv64/build/tmp/work/riscv64imafdc_zicsr_zifencei-poky-linux/opensbi/1.9/sources/opensbi-1.9/lib/sbi/sbi_tlb.c:176:
| Error: unrecognized opcode `fence.i', extension `zifencei' required

Checking the exit status directly avoids a positive match as happens with grep.

---
Changes in v2:
- Rebased onto latest master, removed old CC (their changes merged
already), no functional changes
- Link to v1: https://patch.msgid.link/[email protected]

---
Trevor Gamblin (3):
Makefile: don't grep when setting CC_SUPPORT_ZICSR_ZIFENCEI
Makefile: don't grep when setting CC_SUPPORT_STRICT_ALIGN
Makefile: don't grep when setting CC_SUPPORT_SAVE_RESTORE

 Makefile | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

---
base-commit: 5a175017261af97e9122fcb9d9012481cdb5aa41
change-id: 20260909-makefile_extension_grep-4f9fcea894cd

Best regards,

Signature · 7 lines
Trevor Gamblin <[email protected]>


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#1 lore raw

PATCH v2 1/3 Makefile: don't grep when setting CC_SUPPORT_ZICSR_ZIFENCEI

Check exit status from the compiler/linker check directly (similar to OPENSBI_LD_PIE), rather than using grep to search for a pattern. This avoids potential unrelated matches in the command.

---
 Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Makefile +1 -1
index 1b3333f3..d704b0b6 100644
@@ -204,7 +204,7 @@ CC_SUPPORT_SAVE_RESTORE := $(shell $(CC) $(CLANG_TARGET) $(RELAX_FLAG) -nostdlib
 CC_SUPPORT_STRICT_ALIGN := $(shell $(CC) $(CLANG_TARGET) $(RELAX_FLAG) -nostdlib -mstrict-align -x c /dev/null -o /dev/null 2>&1 | grep -e "-mstrict-align" -e "-mno-unaligned-access" >/dev/null && echo n || echo y)
 
 # Check whether the assembler and the compiler support the Zicsr and Zifencei extensions
-CC_SUPPORT_ZICSR_ZIFENCEI := $(shell $(CC) $(CLANG_TARGET) $(RELAX_FLAG) -nostdlib -march=rv$(OPENSBI_CC_XLEN)imafd_zicsr_zifencei -x c /dev/null -o /dev/null 2>&1 | grep -e "zicsr" -e "zifencei" > /dev/null && echo n || echo y)
+CC_SUPPORT_ZICSR_ZIFENCEI := $(shell $(CC) $(CLANG_TARGET) $(RELAX_FLAG) -nostdlib -march=rv$(OPENSBI_CC_XLEN)imafd_zicsr_zifencei -x c /dev/null -o /dev/null >/dev/null 2>&1 && echo y || echo n)
 
 # Check whether the assembler and the compiler support the Vector extension
 CC_SUPPORT_VECTOR := $(shell $(CC) $(CLANG_TARGET) $(RELAX_FLAG) -nostdlib -march=rv$(OPENSBI_CC_XLEN)gv -dM -E -x c /dev/null 2>&1 | grep -q riscv.*vector && echo y || echo n)
Signature · 7 lines
2.55.0


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#2 lore raw

PATCH v2 2/3 Makefile: don't grep when setting CC_SUPPORT_STRICT_ALIGN

In reply to Trevor Gamblin

Check exit status from the compiler/linker check directly (similar to OPENSBI_LD_PIE), rather than using grep to search for a pattern. This avoids potential unrelated matches in the command.

---
 Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Makefile +1 -1
index d704b0b6..b0a23ce0 100644
@@ -201,7 +201,7 @@ OPENSBI_LD_EXCLUDE_LIBS := $(shell $(CC) $(CLANG_TARGET) $(RELAX_FLAG) $(USE_LD_
 CC_SUPPORT_SAVE_RESTORE := $(shell $(CC) $(CLANG_TARGET) $(RELAX_FLAG) -nostdlib -mno-save-restore -x c /dev/null -o /dev/null 2>&1 | grep -e "-save-restore" >/dev/null && echo n || echo y)
 
 # Check whether the compiler supports -m(no-)strict-align
-CC_SUPPORT_STRICT_ALIGN := $(shell $(CC) $(CLANG_TARGET) $(RELAX_FLAG) -nostdlib -mstrict-align -x c /dev/null -o /dev/null 2>&1 | grep -e "-mstrict-align" -e "-mno-unaligned-access" >/dev/null && echo n || echo y)
+CC_SUPPORT_STRICT_ALIGN := $(shell $(CC) $(CLANG_TARGET) $(RELAX_FLAG) -nostdlib -mstrict-align -x c /dev/null -o /dev/null 2>&1 && echo y || echo n)
 
 # Check whether the assembler and the compiler support the Zicsr and Zifencei extensions
 CC_SUPPORT_ZICSR_ZIFENCEI := $(shell $(CC) $(CLANG_TARGET) $(RELAX_FLAG) -nostdlib -march=rv$(OPENSBI_CC_XLEN)imafd_zicsr_zifencei -x c /dev/null -o /dev/null >/dev/null 2>&1 && echo y || echo n)
Signature · 7 lines
2.55.0


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#3 lore raw

PATCH v2 3/3 Makefile: don't grep when setting CC_SUPPORT_SAVE_RESTORE

In reply to Trevor Gamblin

Check exit status from the compiler/linker check directly (similar to OPENSBI_LD_PIE), rather than using grep to search for a pattern. This avoids potential unrelated matches in the command.

---
 Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Makefile +1 -1
index b0a23ce0..90bf7d5d 100644
@@ -198,7 +198,7 @@ OPENSBI_LD_PIE := $(shell $(CC) $(CLANG_TARGET) $(RELAX_FLAG) $(USE_LD_FLAG) -fP
 OPENSBI_LD_EXCLUDE_LIBS := $(shell $(CC) $(CLANG_TARGET) $(RELAX_FLAG) $(USE_LD_FLAG) "-Wl,--exclude-libs,ALL" -x c /dev/null -o /dev/null >/dev/null 2>&1 && echo y || echo n)
 
 # Check whether the compiler supports -m(no-)save-restore
-CC_SUPPORT_SAVE_RESTORE := $(shell $(CC) $(CLANG_TARGET) $(RELAX_FLAG) -nostdlib -mno-save-restore -x c /dev/null -o /dev/null 2>&1 | grep -e "-save-restore" >/dev/null && echo n || echo y)
+CC_SUPPORT_SAVE_RESTORE := $(shell $(CC) $(CLANG_TARGET) $(RELAX_FLAG) -nostdlib -mno-save-restore -x c /dev/null -o /dev/null 2>&1 && echo y || echo n)
 
 # Check whether the compiler supports -m(no-)strict-align
 CC_SUPPORT_STRICT_ALIGN := $(shell $(CC) $(CLANG_TARGET) $(RELAX_FLAG) -nostdlib -mstrict-align -x c /dev/null -o /dev/null 2>&1 && echo y || echo n)
Signature · 7 lines
2.55.0


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
Reviewed-by on the v2 series Applied
#4 lore raw
In reply to Trevor Gamblin
Quoted 47 lines · Trevor Gamblin

When building OpenSBI in environments where extensions from the ISA are used to generate build paths (e.g. Yocto Project), the Makefile checks which use grep to look for patterns can incorrectly match against the build paths, rather than the command's exit status. This means a workaround is needed to handle checking for extensions like zicsr and zifencei. Instead, make the following variable assignments use the same exit status pattern as OPENSBI_LD_PIE:

CC_SUPPORT_ZICSR_ZIFENCEI
CC_SUPPORT_STRICT_ALIGN
CC_SUPPORT_SAVE_RESTORE

Tested by building a full image in Yocto including OpenSBI and booting it in QEMU.

Note that only the first patch (for CC_SUPPORT_ZICSR_ZIFENCEI) is needed to fix the Yocto QEMU case, but I've submitted the remaining two for consistency. Also since submitting the first version of this series two additional uses of grep have appeared in the Makefile, but I've opted not to change them since they don't appear to result in the same issue observed in Yocto.

See also: https://bugzilla.yoctoproject.org/show_bug.cgi?id=15897

As an example, in my Yocto build environment, values like CC for OpenSBI are set like:

|export CC="riscv64-poky-linux-gcc -march=rv64imafdc_zicsr_zifencei -mabi=lp64d -Wl,-O1 -Wl,--hash-style=gnu -Wl,--as-needed -Wl,-z,relro,-z,now -fstack-protector-strong -O2 -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=format-security --sysroot=/home/tgamblin/workspace/ypbuilds/poky-qemuriscv64/build/tmp/work/riscv64imafdc_zicsr_zifencei-poky-linux/opensbi/1.9/recipe-sysroot"

and the build error:

| /tmp/ccnpajj3.s: Assembler messages:
| /home/tgamblin/workspace/ypbuilds/poky-qemuriscv64/build/tmp/work/riscv64imafdc_zicsr_zifencei-poky-linux/opensbi/1.9/sources/opensbi-1.9/lib/sbi/sbi_tlb.c:176:
| Error: unrecognized opcode `fence.i', extension `zifencei' required

Checking the exit status directly avoids a positive match as happens with grep.

---
Changes in v2:
- Rebased onto latest master, removed old CC (their changes merged
already), no functional changes
- Link to v1: https://patch.msgid.link/[email protected]

Quoted 5 lines

---
Trevor Gamblin (3):
Makefile: don't grep when setting CC_SUPPORT_ZICSR_ZIFENCEI
Makefile: don't grep when setting CC_SUPPORT_STRICT_ALIGN
Makefile: don't grep when setting CC_SUPPORT_SAVE_RESTORE

LGTM. You may also want to update CC_SUPPORT_VECTOR and CC_SUPPORT_WTHREAD_SAFETY_POINTER in top-level Makefile.

Applied this series to the riscv/opensbi repo.

Thanks,
Anup

Quoted 11 lines
 Makefile | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

---
base-commit: 5a175017261af97e9122fcb9d9012481cdb5aa41
change-id: 20260909-makefile_extension_grep-4f9fcea894cd

Best regards,
--
Trevor Gamblin <[email protected]>

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi