makefilelinux-kernelkernel-modulekbuildldflags

Difference between LDFLAGS and ldflags-y


The kbuild document says that:

ldflags-y... [applies] only to the kbuild makefile in which they are assigned....[is used] for all ld invocations happening during a recursive build.

while

LDFLAGS... [is used] for all invocations of the linker.

I cannot see any difference between them, given a Makefile m (and we type make m)

Is this interpretation correct?


Solution

  • [applies] only to the kbuild makefile in which they are assigned

    means that the link flags defined by that variable in a given makefile are used only in rules appearing in the same makefile. They are not inherited from a parent makefile, and they are not transmitted to sub-makes controlled by other makefiles.

    [is used] for all ld invocations happening during a recursive build.

    This is a statement about the facility in general, not about specific flags designated via any particular use of that facility. It says that you can use an ldflags-y variable in any kbuild makefile, and expect to see the behavior described in the docs -- linker invocations in rules appearing directly in the same makefile will use those flags, but they will not automatically be conveyed to sub-makes in other directories.

    On the other hand, this ...

    LDFLAGS... [is used] for all invocations of the linker.

    ... appears in the documentation for architecture makefiles. The point of these, as described in the docs, is to define flags appropriate for the overall build and (substantially) everything in it. This variable should be defined only in such a makefile, and the quotation says that the linker flags defined that way are used in all linker runs during the build, no matter in what makefile the corresponding rule appears (including, notably, in directories that are not in the same tree as the architecture makefile).

    The docs go on to say that ldflags-y can be used for further customization, which you should take to indicate that both are applied in linker runs. Reading a bit more closely between the lines, you should expect the (global) LDFLAGS to appear earlier in the linker command than the (local) ldflags-y.

    I cannot see any difference between them, given a Makefile m (and we type make m)

    • suppose LDFLAGS := x, because it applies for all invocations of the linker, then all ld invoked in any build session starting from make m has value x for LDFLAGS

    Yes and no. A general kbuild makefile m shouldn't provide a definition for LDFLAGS. That belongs only in the appropriate architecture makefile. It's unclear what would happen if a kbuild makefile did so anyway, but I suspect that it would cascade to sub-makes.

    Additionally, you shouldn't attempt to make a subtree directly if it is configured for kbuild. The system is not designed for that.

    • suppose ldflags-y := y , becaus if applies for all recursived invocations of ld, then all ld invoked (by any build session starting from make m) has also value y for LDFLAGS

    No. The ability to use an ldflags-y variable to provide local link-flag customizations applies to all kbuild makefiles. The specific flags provided that way by a particular makefile do not cascade to sub-makes.