Trying and failing to build f2fs-tools from git

I modified the Makefile for f2fs-tools to build from git. I am hitting a failure to compile and wondering if anyone has some thoughts as to the cause.

Here is the complete build log and below is just the last few lines around the failure.

...
f2fs_io.c: In function 'do_fallocate':
f2fs_io.c:550:33: error: 'FALLOC_FL_COLLAPSE_RANGE' undeclared (first use in this function)
  550 |                         mode |= FALLOC_FL_COLLAPSE_RANGE;
      |                                 ^~~~~~~~~~~~~~~~~~~~~~~~
f2fs_io.c:550:33: note: each undeclared identifier is reported only once for each function it appears in
f2fs_io.c:553:33: error: 'FALLOC_FL_INSERT_RANGE' undeclared (first use in this function)
  553 |                         mode |= FALLOC_FL_INSERT_RANGE;
      |                                 ^~~~~~~~~~~~~~~~~~~~~~
f2fs_io.c:559:33: error: 'FALLOC_FL_ZERO_RANGE' undeclared (first use in this function)
  559 |                         mode |= FALLOC_FL_ZERO_RANGE;
      |                                 ^~~~~~~~~~~~~~~~~~~~
make[6]: *** [Makefile:419: f2fs_io.o] Error 1

The above was trying to building for bcm27xx/bcm2712 but I hit the same error if I try building for x86/64 also.

If I build the same commit from the git repo outside of the OpenWrt build system, I do not experience that error so I'm thinking the cause is something in it.

You need to define the _GNU_SOURCE macro, see: https://man7.org/linux/man-pages/man2/fallocate.2.html

Thanks for the reply. I hacked up the headers and got the build to work with this patch. How would I implement your suggestion?

You don't need this ugly hack, just pass the _GNU_SOURCE in the Makefile.

CFLAGS=-D_GNU_SOURCE

EDIT: Actually read this: https://openwrt.org/docs/guide-developer/packages.flags

I added this right below the following but still hit the error:

...
ifneq ($(CONFIG_USE_MUSL),)                                             
  CONFIGURE_VARS += ac_cv_func_lseek64=yes                              
endif                                                                   
                                                                        
CFLAGS += -D_GNU_SOURCE

See my edit above.

I read through that page but am unclear what variable I need to setup to get the build to work. I added:

CFLAGS += -D_GNU_SOURCE
TARGET_CFLAGS += -D_GNU_SOURCE

And I see it passed to the cross compiler (I think) but am still hitting that error:

...
aarch64-openwrt-linux-musl-gcc -DHAVE_CONFIG_H -I. -I../..  -I../../include -I/scratch/union/staging_dir/toolchain-aarch64_cortex-a76_gcc-14.2.0_musl/usr/include -I/scratch/union/staging_dir/toolchain-aarch64_cortex-a76_gcc-14.2.0_musl/include -I/scratch/union/staging_dir/toolchain-aarch64_cortex-a76_gcc-14.2.0_musl/include/fortify  -Wall -D_FILE_OFFSET_BITS=64 -mcpu=cortex-a76 -O3 -pipe -fno-caller-saves -fno-plt -fhonour-copts -fmacro-prefix-map=/scratch/union/build_dir/target-aarch64_cortex-a76_musl/f2fs-tools-default/f2fs-tools-1.16.20241111=f2fs-tools-1.16.20241111 -Wformat -Werror=format-security -fstack-protector -D_FORTIFY_SOURCE=1 -Wl,-z,now -Wl,-z,relro -D_GNU_SOURCE  -c -o f2fs_io.o f2fs_io.c
f2fs_io.c: In function 'do_fallocate':
f2fs_io.c:550:33: error: 'FALLOC_FL_COLLAPSE_RANGE' undeclared (first use in this function)
  550 |                         mode |= FALLOC_FL_COLLAPSE_RANGE;
      |                                 ^~~~~~~~~~~~~~~~~~~~~~~~
f2fs_io.c:550:33: note: each undeclared identifier is reported only once for each function it appears in
f2fs_io.c:553:33: error: 'FALLOC_FL_INSERT_RANGE' undeclared (first use in this function)
  553 |                         mode |= FALLOC_FL_INSERT_RANGE;
      |                                 ^~~~~~~~~~~~~~~~~~~~~~
f2fs_io.c:559:33: error: 'FALLOC_FL_ZERO_RANGE' undeclared (first use in this function)
  559 |                         mode |= FALLOC_FL_ZERO_RANGE;
      |                                 ^~~~~~~~~~~~~~~~~~~~
make[6]: *** [Makefile:419: f2fs_io.o] Error 1
make[6]: Leaving directory '/scratch/union/build_dir/target-aarch64_cortex-a76_musl/f2fs-tools-default/f2fs-tools-1.16.20241111/tools/f2fs_io'
make[5]: *** [Makefile:597: all-recursive] Error 1
make[5]: Leaving directory '/scratch/union/build_dir/target-aarch64_cortex-a76_musl/f2fs-tools-default/f2fs-tools-1.16.20241111/tools'
make[4]: *** [Makefile:419: all-recursive] Error 1
make[4]: Leaving directory '/scratch/union/build_dir/target-aarch64_cortex-a76_musl/f2fs-tools-default/f2fs-tools-1.16.20241111'
make[3]: *** [Makefile:351: all] Error 2
make[3]: Leaving directory '/scratch/union/build_dir/target-aarch64_cortex-a76_musl/f2fs-tools-default/f2fs-tools-1.16.20241111'
make[2]: *** [Makefile:161: /scratch/union/build_dir/target-aarch64_cortex-a76_musl/f2fs-tools-default/f2fs-tools-1.16.20241111/.built] Error 2
make[2]: Leaving directory '/scratch/union/package/utils/f2fs-tools'
time: package/utils/f2fs-tools/default/compile#12.66#0.66#10.09
    ERROR: package/utils/f2fs-tools failed to build (build variant: default).
make[1]: *** [package/Makefile:185: package/utils/f2fs-tools/compile] Error 1
make[1]: Leaving directory '/scratch/union'
make: *** [/scratch/union/include/toplevel.mk:233: package/f2fs-tools/compile] Error 2
make menuconfig

The build system allows for several entries under Advanced configuration options (for developers) > Kernel extra CFLAGS and Advanced configuration options (for developers) > Target Options which will pass the defined flags onto the kernel and packages respectively.

Ah! I added it but still got the error :smiley:

% grep D_GNU_SOURCE .config
CONFIG_TARGET_OPTIMIZATION="-mcpu=cortex-a76 -O3 -pipe -D_GNU_SOURCE"

Link to the f2fs-tools repository?

I'm not sure what you're asking... see: https://github.com/graysky2/openwrt/commit/c1b10ef62f043956f24565868627d2a1cd084a5f

This: https://git.kernel.org/pub/scm/linux/kernel/git/jaegeuk/f2fs-tools.git/tree/tools/f2fs_io/f2fs_io.c

The first few lines:

#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif

So, you shouldn't need to define it yourself. Gonna have to check the musl headers to see if the FALLOC_FL_* flags are there.

They are defined in include/linux/falloc.h. musl libc only defines two of them (in fcntl.h): FALLOC_FL_KEEP_SIZE and FALLOC_FL_PUNCH_HOLE.

Try patching tools/f2fs_io/f2fs_io.c with the following include: #include <linux/falloc.h>.