Please consider this patch. It adds support for obtaining file basename via __FILE_BASENAME__. This would be used by GNU GRUB. I'll provide some explanation on why this is very necessary for us: - Bootloader code is very critical; a bug can render system unbootable. Additionally, it needs to support a wide range of hardware and firmware. Because of this, we put extensive debugging support into GRUB. Debug statements currently rely on __FILE__. - We want to support $srcdir != $objdir build setups, and continue supporting them. - Some parts of bootstrap code tend to be very size constrained. Every byte counts, and each debug statement is taking unnecessary space because accessing a file in $srcdir requires either an absolute path or at least a number of '../' components (in Debian, that would be two of them). Because of this, I would find it very useful if GCC provided __FILE_BASENAME__, or at least an equivalent facility.
Created attachment 19445 [details] __FILE_BASENAME__ Attaching the patch. Also sent to gcc-patches.
__BASE_FILE__ could be enough for what you want.
(In reply to comment #2) > __BASE_FILE__ could be enough for what you want. Hi, I tried already, but __BASE_FILE__ seems to be something unrelated. It's like __FILE__ but when used in an #include'd source it picks the includer rather than the included file.
(In reply to comment #0) > Please consider this patch. It adds support for obtaining file basename via > __FILE_BASENAME__. Please do! What would also be really nice would be to have similar functionality for C++, with __CLASS__ and __METHOD__ macros.
We have a similar issue for embedded devices with debugging. We even implemented it with the same exact name __FILE_BASENAME__ and when I was going to submit a it in bugzilla we realized that it was already reported.
We tried it in 4.7 and it works perfectly. By now we will have to apply this patch for our development with 4.7.1.
Please send patches to the gcc-patches mailing list for review. Patches in bugzilla are easily overlooked or forgotten. https://gcc.gnu.org/contribute.html
*** Bug 91998 has been marked as a duplicate of this bug. ***
The master branch has been updated by Christophe Lyon <clyon@gcc.gnu.org>: https://gcc.gnu.org/g:1a9b3f04c11eb467a8dc504a37dad57a371a0d4c commit r12-938-g1a9b3f04c11eb467a8dc504a37dad57a371a0d4c Author: Christophe Lyon <christophe.lyon@linaro.org> Date: Thu May 20 08:10:50 2021 +0000 c: Add support for __FILE_NAME__ macro (PR c/42579) The toolchain provided by ST for stm32 has had support for __FILENAME__ for a while, but clang/llvm has recently implemented support for __FILE_NAME__, so it seems better to use the same macro name in GCC. It happens that the ST patch is similar to the one proposed in PR c/42579. Given these input files: :::::::::::::: mydir/myinc.h :::::::::::::: char* mystringh_file = __FILE__; char* mystringh_filename = __FILE_NAME__; char* mystringh_base_file = __BASE_FILE__; :::::::::::::: mydir/mysrc.c :::::::::::::: char* mystring_file = __FILE__; char* mystring_filename = __FILE_NAME__; char* mystring_base_file = __BASE_FILE__; we produce: $ gcc mydir/mysrc.c -I . -E char* mystringh_file = "./mydir/myinc.h"; char* mystringh_filename = "myinc.h"; char* mystringh_base_file = "mydir/mysrc.c"; char* mystring_file = "mydir/mysrc.c"; char* mystring_filename = "mysrc.c"; char* mystring_base_file = "mydir/mysrc.c"; 2021-05-20 Christophe Lyon <christophe.lyon@linaro.org> Torbjörn Svensson <torbjorn.svensson@st.com> PR c/42579 libcpp/ * include/cpplib.h (cpp_builtin_type): Add BT_FILE_NAME entry. * init.c (builtin_array): Likewise. * macro.c (_cpp_builtin_macro_text): Add support for BT_FILE_NAME. gcc/ * doc/cpp.texi (Common Predefined Macros): Document __FILE_NAME__. gcc/testsuite/ * c-c++-common/spellcheck-reserved.c: Add tests for __FILE_NAME__. * c-c++-common/cpp/file-name-1.c: New test.
Fixed on trunk: we now support __FILE_NAME__, like clang.
What is the target GCC version for __FILE_NAME__? GCC 12.1?
*** Bug 77488 has been marked as a duplicate of this bug. ***
Yes, this will be part of gcc-12.