Say there is a source file a.c with some function func_a. I would like to call func_a based on some feature define and would also like a.c to be included in the module based on ideally same feature define.
so in my module source that is always present (say main.c) I could have something like
#ifdef FEATURE_A
func_a();
#endif
and in the kbuild for the module something like
obj-$(CONFIG_SAMPLE) += sample.o
sample-objs := main.o utils.o
only if FEATURE_A is defined, include a.c into the sample module
???
But this may not make sense since one is a pre-processor directive and the other a compiler/linker/build directive. Maybe the other way where the pre-processor directive uses some flag defined by the kbuild makes more sense? Is there some way to accomplish this?
Thanks.
I will answer your question by pointing out how sysctl support is conditionally included in the NFS module (I’m sure there are other examples, but this is what I’m familiar with):
The kernel config system maintains a file “include/linux/autoconf.h” that exposes your configuration options as C preprocessor macros. So the files listed above compile differently depending on whether you configured sysctl support.
If sysctl support is enabled: The header “include/linux/nfs_fs.h” checks the macro
CONFIG_SYSCTLand declares the C functionnfs_register_sysctl(). This function is called in “fs/nfs/super.c”. The Makefile (seeingnfs-y += sysctl.o) directs the build system to compile in the file “fs/nfs/sysctl.c” into the module, which defines the functionnfs_register_sysctl().If sysctl support is disabled: The header “include/linux/nfs_fs.h” checks the macro
CONFIG_SYSCTLand declares the preprocessor macronfs_register_sysctl()to be0. This macro is used in “fs/nfs/super.c” to bypass some (dead) error-handling code. The Makefile (seeingnfs-n += sysctl.o) does not compile or link “fs/nfs/sysctl.c”.