An error occurred fetching the project authors.
  1. 16 Jul, 2024 1 commit
  2. 09 May, 2024 1 commit
    • Masahiro Yamada's avatar
      kbuild: use $(src) instead of $(srctree)/$(src) for source directory · b1992c37
      Masahiro Yamada authored
      Kbuild conventionally uses $(obj)/ for generated files, and $(src)/ for
      checked-in source files. It is merely a convention without any functional
      difference. In fact, $(obj) and $(src) are exactly the same, as defined
      in scripts/Makefile.build:
      
          src := $(obj)
      
      When the kernel is built in a separate output directory, $(src) does
      not accurately reflect the source directory location. While Kbuild
      resolves this discrepancy by specifying VPATH=$(srctree) to search for
      source files, it does not cover all cases. For example, when adding a
      header search path for local headers, -I$(srctree)/$(src) is typically
      passed to the compiler.
      
      This introduces inconsistency between upstream and downstream Makefiles
      because $(src) is used instead of $(srctree)/$(src) for the latter.
      
      To address this inconsistency, this commit changes the semantics of
      $(src) so that it always points to the directory in the source tree.
      
      Going forward, the variables used in Makefiles will have the following
      meanings:
      
        $(obj)     - directory in the object tree
        $(src)     - directory in the source tree  (changed by this commit)
        $(objtree) - the top of the kernel object tree
        $(srctree) - the top of the kernel source tree
      
      Consequently, $(srctree)/$(src) in upstream Makefiles need to be replaced
      with $(src).
      Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      Reviewed-by: default avatarNicolas Schier <nicolas@fjasle.eu>
      b1992c37
  3. 19 Feb, 2024 1 commit
  4. 22 Jan, 2023 5 commits
  5. 13 Dec, 2022 3 commits
    • Masahiro Yamada's avatar
      kbuild: use .NOTINTERMEDIATE for future GNU Make versions · 875ef1a5
      Masahiro Yamada authored
      In Kbuild, some files are generated by chains of pattern/implicit rules.
      For example, *.dtb.o files in drivers/of/unittest-data/Makefile are
      generated by the chain of 3 pattern rules, like this:
      
        %.dts  ->  %.dtb  ->  %.dtb.S  ->  %.dtb.o
      
      Here, %.dts is the real source, %.dtb.o is the final target.
      %.dtb and %.dtb.S are called "intermediate files".
      
      As GNU Make manual [1] says, intermediate files are treated differently
      in two ways:
      
       (a) The first difference is what happens if the intermediate file does
         not exist. If an ordinary file 'b' does not exist, and make considers
         a target that depends on 'b', it invariably creates 'b' and then
         updates the target from 'b'. But if 'b' is an intermediate file, then
         make can leave well enough alone: it won't create 'b' unless one of
         its prerequisites is out of date. This means the target depending
         on 'b' won't be rebuilt either, unless there is some other reason
         to update that target: for example the target doesn't exist or a
         different prerequisite is newer than the target.
      
       (b) The second difference is that if make does create 'b' in order to
         update something else, it deletes 'b' later on after it is no longer
         needed. Therefore, an intermediate file which did not exist before
         make also does not exist after make. make reports the deletion to
         you by printing a 'rm' command showing which file it is deleting.
      
      The combination of these is problematic for Kbuild because most of the
      build rules depend on FORCE and the if_changed* macros really determine
      if the target should be updated. So, all missing files, whether they
      are intermediate or not, are always rebuilt.
      
      To see the problem, delete ".SECONDARY:" from scripts/Kbuild.include,
      and repeat this command:
      
        $ make allmodconfig drivers/of/unittest-data/
      
      The intermediate files will be deleted, which results in rebuilding
      intermediate and final objects in the next run of make.
      
      In the old days, people suppressed (b) in inconsistent ways.
      As commit 54a702f7 ("kbuild: mark $(targets) as .SECONDARY and
      remove .PRECIOUS markers") noted, you should not use .PRECIOUS because
      .PRECIOUS has the following behavior (c), which is not likely what you
      want.
      
       (c) If make is killed or interrupted during the execution of their
         recipes, the target is not deleted. Also, the target is not deleted
         on error even if .DELETE_ON_ERROR is specified.
      
      .SECONDARY is a much better way to disable (b), but a small problem
      is that .SECONDARY enables (a), which gives a side-effect to $?;
      prerequisites marked as .SECONDARY do not appear in $?. This is a
      drawback for Kbuild.
      
      I thought it was a bug and opened a bug report. As Paul, the GNU Make
      maintainer, concluded in [2], this is not a bug.
      
      A good news is that, GNU Make 4.4 added the perfect solution,
      .NOTINTERMEDIATE, which cancels both (a) and (b).
      
      For clarificaton, my understanding of .INTERMEDIATE, .SECONDARY,
      .PRECIOUS and .NOTINTERMEDIATE are as follows:
      
                              (a)         (b)         (c)
        .INTERMEDIATE        enable      enable      disable
        .SECONDARY           enable      disable     disable
        .PRECIOUS            disable     disable     enable
        .NOTINTERMEDIATE     disable     disable     disable
      
      However, GNU Make 4.4 has a bug for the global .NOTINTERMEDIATE. [3]
      It was fixed by commit 6164608900ad ("[SV 63417] Ensure global
      .NOTINTERMEDIATE disables all intermediates"), and will be available
      in the next release of GNU Make.
      
      The following is the gain for .NOTINTERMEDIATE:
      
        [Current Make]
      
            $ make allnoconfig vmlinux
                [ full build ]
            $ rm include/linux/device.h
            $ make vmlinux
              CALL    scripts/checksyscalls.sh
      
        Make does not notice the removal of <linux/device.h>.
      
        [Future Make]
      
            $ make-latest allnoconfig vmlinux
                [ full build ]
            $ rm include/linux/device.h
            $ make-latest vmlinux
              CC      arch/x86/kernel/asm-offsets.s
            In file included from ./include/linux/writeback.h:13,
                             from ./include/linux/memcontrol.h:22,
                             from ./include/linux/swap.h:9,
                             from ./include/linux/suspend.h:5,
                             from arch/x86/kernel/asm-offsets.c:13:
            ./include/linux/blk_types.h:11:10: fatal error: linux/device.h: No such file or directory
               11 | #include <linux/device.h>
                  |          ^~~~~~~~~~~~~~~~
            compilation terminated.
            make-latest[1]: *** [scripts/Makefile.build:114: arch/x86/kernel/asm-offsets.s] Error 1
            make-latest: *** [Makefile:1282: prepare0] Error 2
      
        Make notices the removal of <linux/device.h>, and rebuilds objects
        that depended on <linux/device.h>. There exists a source file that
        includes <linux/device.h>, and it raises an error.
      
      To see detailed background information, refer to commit 2d3b1b8f
      ("kbuild: drop $(wildcard $^) check in if_changed* for faster rebuild").
      
      [1]: https://www.gnu.org/software/make/manual/make.html#Chained-Rules
      [2]: https://savannah.gnu.org/bugs/?55532
      [3]: https://savannah.gnu.org/bugs/?63417Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      875ef1a5
    • Masahiro Yamada's avatar
      kbuild: add read-file macro · 6768fa4b
      Masahiro Yamada authored
      Since GNU Make 4.2, $(file ...) supports the read operater '<', which
      is useful to read a file without forking a new process. No warning is
      shown even if the input file is missing.
      
      For older Make versions, it falls back to the cat command.
      Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      Reviewed-by: default avatarNicolas Schier <nicolas@fjasle.eu>
      Reviewed-by: default avatarAlexander Lobakin <alexandr.lobakin@intel.com>
      Tested-by: default avatarAlexander Lobakin <alexandr.lobakin@intel.com>
      6768fa4b
    • Masahiro Yamada's avatar
      kbuild: add test-{ge,gt,le,lt} macros · fccb3d3e
      Masahiro Yamada authored
      GNU Make 4.4 introduced $(intcmp ...), which is useful to compare two
      integers without forking a new process.
      
      Add test-{ge,gt,le,lt} macros, which work more efficiently with GNU
      Make >= 4.4. For older Make versions, they fall back to the 'test'
      shell command.
      
      The first two parameters to $(intcmp ...) must not be empty. To avoid
      the syntax error, I appended '0' to them. Fortunately, '00' is treated
      as '0'. This is needed because CONFIG options may expand to an empty
      string when the kernel configuration is not included.
      Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      Acked-by: Palmer Dabbelt <palmer@rivosinc.com> # RISC-V
      Reviewed-by: default avatarNathan Chancellor <nathan@kernel.org>
      Reviewed-by: default avatarNicolas Schier <nicolas@fjasle.eu>
      fccb3d3e
  6. 22 Nov, 2022 1 commit
  7. 28 Sep, 2022 1 commit
    • Masahiro Yamada's avatar
      kbuild: remove the target in signal traps when interrupted · a7f3257d
      Masahiro Yamada authored
      When receiving some signal, GNU Make automatically deletes the target if
      it has already been changed by the interrupted recipe.
      
      If the target is possibly incomplete due to interruption, it must be
      deleted so that it will be remade from scratch on the next run of make.
      Otherwise, the target would remain corrupted permanently because its
      timestamp had already been updated.
      
      Thanks to this behavior of Make, you can stop the build any time by
      pressing Ctrl-C, and just run 'make' to resume it.
      
      Kbuild also relies on this feature, but it is equivalently important
      for any build systems that make decisions based on timestamps (if you
      want to support Ctrl-C reliably).
      
      However, this does not always work as claimed; Make immediately dies
      with Ctrl-C if its stderr goes into a pipe.
      
        [Test Makefile]
      
          foo:
                  echo hello > $@
                  sleep 3
                  echo world >> $@
      
        [Test Result]
      
          $ make                         # hit Ctrl-C
          echo hello > foo
          sleep 3
          ^Cmake: *** Deleting file 'foo'
          make: *** [Makefile:3: foo] Interrupt
      
          $ make 2>&1 | cat              # hit Ctrl-C
          echo hello > foo
          sleep 3
          ^C$                            # 'foo' is often left-over
      
      The reason is because SIGINT is sent to the entire process group.
      In this example, SIGINT kills 'cat', and 'make' writes the message to
      the closed pipe, then dies with SIGPIPE before cleaning the target.
      
      A typical bad scenario (as reported by [1], [2]) is to save build log
      by using the 'tee' command:
      
          $ make 2>&1 | tee log
      
      This can be problematic for any build systems based on Make, so I hope
      it will be fixed in GNU Make. The maintainer of GNU Make stated this is
      a long-standing issue and difficult to fix [3]. It has not been fixed
      yet as of writing.
      
      So, we cannot rely on Make cleaning the target. We can do it by
      ourselves, in signal traps.
      
      As far as I understand, Make takes care of SIGHUP, SIGINT, SIGQUIT, and
      SITERM for the target removal. I added the traps for them, and also for
      SIGPIPE just in case cmd_* rule prints something to stdout or stderr
      (but I did not observe an actual case where SIGPIPE was triggered).
      
      [Note 1]
      
      The trap handler might be worth explaining.
      
          rm -f $@; trap - $(sig); kill -s $(sig) $$
      
      This lets the shell kill itself by the signal it caught, so the parent
      process can tell the child has exited on the signal. Generally, this is
      a proper manner for handling signals, in case the calling program (like
      Bash) may monitor WIFSIGNALED() and WTERMSIG() for WCE although this may
      not be a big deal here because GNU Make handles SIGHUP, SIGINT, SIGQUIT
      in WUE and SIGTERM in IUE.
      
        IUE - Immediate Unconditional Exit
        WUE - Wait and Unconditional Exit
        WCE - Wait and Cooperative Exit
      
      For details, see "Proper handling of SIGINT/SIGQUIT" [4].
      
      [Note 2]
      
      Reverting 392885ee ("kbuild: let fixdep directly write to .*.cmd
      files") would directly address [1], but it only saves if_changed_dep.
      As reported in [2], all commands that use redirection can potentially
      leave an empty (i.e. broken) target.
      
      [Note 3]
      
      Another (even safer) approach might be to always write to a temporary
      file, and rename it to $@ at the end of the recipe.
      
         <command>  > $(tmp-target)
         mv $(tmp-target) $@
      
      It would require a lot of Makefile changes, and result in ugly code,
      so I did not take it.
      
      [Note 4]
      
      A little more thoughts about a pattern rule with multiple targets (or
      a grouped target).
      
          %.x %.y: %.z
                  <recipe>
      
      When interrupted, GNU Make deletes both %.x and %.y, while this solution
      only deletes $@. Probably, this is not a big deal. The next run of make
      will execute the rule again to create $@ along with the other files.
      
      [1]: https://lore.kernel.org/all/YLeot94yAaM4xbMY@gmail.com/
      [2]: https://lore.kernel.org/all/20220510221333.2770571-1-robh@kernel.org/
      [3]: https://lists.gnu.org/archive/html/help-make/2021-06/msg00001.html
      [4]: https://www.cons.org/cracauer/sigint.html
      
      Fixes: 392885ee ("kbuild: let fixdep directly write to .*.cmd files")
      Reported-by: default avatarIngo Molnar <mingo@kernel.org>
      Reported-by: default avatarRob Herring <robh@kernel.org>
      Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      Tested-by: default avatarIngo Molnar <mingo@kernel.org>
      Reviewed-by: default avatarNicolas Schier <nicolas@fjasle.eu>
      a7f3257d
  8. 01 Jun, 2022 1 commit
  9. 29 May, 2022 1 commit
    • Masahiro Yamada's avatar
      kbuild: do not create *.prelink.o for Clang LTO or IBT · c25e1c55
      Masahiro Yamada authored
      When CONFIG_LTO_CLANG=y, additional intermediate *.prelink.o is created
      for each module. Also, objtool is postponed until LLVM IR is converted
      to ELF.
      
      CONFIG_X86_KERNEL_IBT works in a similar way to postpone objtool until
      objects are merged together.
      
      This commit stops generating *.prelink.o, so the build flow will look
      similar with/without LTO.
      
      The following figures show how the LTO build currently works, and
      how this commit is changing it.
      
      Current build flow
      ==================
      
       [1] single-object module
      
                                            $(LD)
                 $(CC)                     +objtool              $(LD)
          foo.c --------------------> foo.o -----> foo.prelink.o -----> foo.ko
                                    (LLVM IR)          (ELF)       |    (ELF)
                                                                   |
                                                       foo.mod.o --/
                                                       (LLVM IR)
      
       [2] multi-object module
                                            $(LD)
                 $(CC)         $(AR)       +objtool               $(LD)
          foo1.c -----> foo1.o -----> foo.o -----> foo.prelink.o -----> foo.ko
                                 |  (archive)          (ELF)       |    (ELF)
          foo2.c -----> foo2.o --/                                 |
                       (LLVM IR)                       foo.mod.o --/
                                                       (LLVM IR)
      
        One confusion is that foo.o in multi-object module is an archive
        despite of its suffix.
      
      New build flow
      ==============
      
       [1] single-object module
      
        Since there is only one object, there is no need to keep the LLVM IR.
        Use $(CC)+$(LD) to generate an ELF object in one build rule. When LTO
        is disabled, $(LD) is unneeded because $(CC) produces an ELF object.
      
                     $(CC)+$(LD)+objtool              $(LD)
          foo.c ----------------------------> foo.o ---------> foo.ko
                                              (ELF)     |      (ELF)
                                                        |
                                            foo.mod.o --/
                                            (LLVM IR)
      
       [2] multi-object module
      
        Previously, $(AR) was used to combine LLVM IR files into an archive,
        but there was no technical reason to do so. Use $(LD) to merge them
        into a single ELF object.
      
                                     $(LD)
                   $(CC)            +objtool          $(LD)
          foo1.c ---------> foo1.o ---------> foo.o ---------> foo.ko
                                       |      (ELF)     |      (ELF)
          foo2.c ---------> foo2.o ----/                |
                           (LLVM IR)        foo.mod.o --/
                                            (LLVM IR)
      Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      Reviewed-by: default avatarNicolas Schier <nicolas@fjasle.eu>
      Tested-by: default avatarNathan Chancellor <nathan@kernel.org>
      Reviewed-by: default avatarSami Tolvanen <samitolvanen@google.com>
      Tested-by: Sedat Dilek <sedat.dilek@gmail.com> # LLVM-14 (x86-64)
      Acked-by: default avatarJosh Poimboeuf <jpoimboe@kernel.org>
      c25e1c55
  10. 08 Jan, 2022 1 commit
    • Masahiro Yamada's avatar
      certs: simplify $(srctree)/ handling and remove config_filename macro · b8c96a6b
      Masahiro Yamada authored
      The complex macro, config_filename, was introduced to do:
      
       [1] drop double-quotes from the string value
       [2] add $(srctree)/ prefix in case the file is not found in $(objtree)
       [3] escape spaces and more
      
      [1] will be more generally handled by Kconfig later.
      
      As for [2], Kbuild uses VPATH to search for files in $(objtree),
      $(srctree) in this order. GNU Make can natively handle it.
      
      As for [3], converting $(space) to $(space_escape) back and forth looks
      questionable to me. It is well-known that GNU Make cannot handle file
      paths with spaces in the first place.
      
      Instead of using the complex macro, use $< so it will be expanded to
      the file path of the key.
      
      Remove config_filename, finally.
      Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      b8c96a6b
  11. 02 Sep, 2021 2 commits
  12. 26 May, 2021 1 commit
    • Masahiro Yamada's avatar
      kbuild: sink stdout from cmd for silent build · 174a1dcc
      Masahiro Yamada authored
      When building with 'make -s', no output to stdout should be printed.
      
      As Arnd Bergmann reported [1], mkimage shows the detailed information
      of the generated images.
      
      I think this should be suppressed by the 'cmd' macro instead of by
      individual scripts.
      
      Insert 'exec >/dev/null;' in order to redirect stdout to /dev/null for
      silent builds.
      
      [Note about this implementation]
      
      'exec >/dev/null;' may look somewhat tricky, but this has a reason.
      
      Appending '>/dev/null' at the end of command line is a common way for
      redirection, so I first tried this:
      
        cmd = @set -e; $(echo-cmd) $(cmd_$(1)) >/dev/null
      
      ... but it would not work if $(cmd_$(1)) itself contains a redirection.
      
      For example, cmd_wrap in scripts/Makefile.asm-generic redirects the
      output from the 'echo' command into the target file.
      
      It would be expanded into:
      
        echo "#include <asm-generic/$*.h>" > $@ >/dev/null
      
      Then, the target file gets empty because the string will go to /dev/null
      instead of $@.
      
      Next, I tried this:
      
        cmd = @set -e; $(echo-cmd) { $(cmd_$(1)); } >/dev/null
      
      The form above would be expanded into:
      
        { echo "#include <asm-generic/$*.h>" > $@; } >/dev/null
      
      This works as expected. However, it would be a syntax error if
      $(cmd_$(1)) is empty.
      
      When CONFIG_TRIM_UNUSED_KSYMS is disabled, $(call cmd,gen_ksymdeps) in
      scripts/Makefile.build would be expanded into:
      
        set -e;  { ; } >/dev/null
      
      ..., which causes an syntax error.
      
      I also tried this:
      
        cmd = @set -e; $(echo-cmd) ( $(cmd_$(1)) ) >/dev/null
      
      ... but this causes a syntax error for the same reason.
      
      So, finally I adopted:
      
        cmd = @set -e; $(echo-cmd) exec >/dev/null; $(cmd_$(1))
      
      [1]: https://lore.kernel.org/lkml/20210514135752.2910387-1-arnd@kernel.org/Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      174a1dcc
  13. 24 Apr, 2021 1 commit
  14. 21 Feb, 2021 1 commit
  15. 20 Oct, 2020 1 commit
  16. 09 Aug, 2020 1 commit
  17. 17 Jun, 2020 1 commit
    • Masahiro Yamada's avatar
      kbuild: improve cc-option to clean up all temporary files · f2f02ebd
      Masahiro Yamada authored
      When cc-option and friends evaluate compiler flags, the temporary file
      $$TMP is created as an output object, and automatically cleaned up.
      The actual file path of $$TMP is .<pid>.tmp, here <pid> is the process
      ID of $(shell ...) invoked from cc-option. (Please note $$$$ is the
      escape sequence of $$).
      
      Such garbage files are cleaned up in most cases, but some compiler flags
      create additional output files.
      
      For example, -gsplit-dwarf creates a .dwo file.
      
      When CONFIG_DEBUG_INFO_SPLIT=y, you will see a bunch of .<pid>.dwo files
      left in the top of build directories. You may not notice them unless you
      do 'ls -a', but the garbage files will increase every time you run 'make'.
      
      This commit changes the temporary object path to .tmp_<pid>/tmp, and
      removes .tmp_<pid> directory when exiting. Separate build artifacts such
      as *.dwo will be cleaned up all together because their file paths are
      usually determined based on the base name of the object.
      
      Another example is -ftest-coverage, which outputs the coverage data into
      <base-name-of-object>.gcno
      Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      f2f02ebd
  18. 12 May, 2020 1 commit
    • Masahiro Yamada's avatar
      kbuild: use -MMD instead of -MD to exclude system headers from dependency · 30a77297
      Masahiro Yamada authored
      This omits system headers from the generated header dependency.
      
      System headers are not updated unless you upgrade the compiler. Nor do
      they contain CONFIG options, so fixdep does not need to parse them.
      
      Having said that, the effect of this optimization will be quite small
      because the kernel code generally does not include system headers
      except <stdarg.h>. Host programs include a lot of system headers,
      but there are not so many in the kernel tree.
      
      At first, keeping system headers in .*.cmd files might be useful to
      detect the compiler update, but there is no guarantee that <stdarg.h>
      is included from every file. So, I implemented a more reliable way in
      the previous commit.
      Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      30a77297
  19. 15 Jan, 2020 1 commit
    • Masahiro Yamada's avatar
      kbuild: remove *.tmp file when filechk fails · 88fe89a4
      Masahiro Yamada authored
      Bartosz Golaszewski reports that when "make {menu,n,g,x}config" fails
      due to missing packages, a temporary file is left over, which is not
      ignored by git.
      
      For example, if GTK+ is not installed:
      
        $ make gconfig
        *
        * Unable to find the GTK+ installation. Please make sure that
        * the GTK+ 2.0 development package is correctly installed.
        * You need gtk+-2.0 gmodule-2.0 libglade-2.0
        *
        scripts/kconfig/Makefile:208: recipe for target 'scripts/kconfig/gconf-cfg' failed
        make[1]: *** [scripts/kconfig/gconf-cfg] Error 1
        Makefile:567: recipe for target 'gconfig' failed
        make: *** [gconfig] Error 2
        $ git status
        HEAD detached at v5.4
        Untracked files:
          (use "git add <file>..." to include in what will be committed)
      
                scripts/kconfig/gconf-cfg.tmp
      
        nothing added to commit but untracked files present (use "git add" to track)
      
      This is because the check scripts are run with filechk, which misses
      to clean up the temporary file on failure.
      
      When the line
      
        { $(filechk_$(1)); } > $@.tmp;
      
      ... fails, it exits immediately due to the 'set -e'. Use trap to make
      sure to delete the temporary file on exit.
      
      For extra safety, I replaced $@.tmp with $(dot-target).tmp to make it
      a hidden file.
      Reported-by: default avatarBartosz Golaszewski <bgolaszewski@baylibre.com>
      Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      88fe89a4
  20. 06 Jan, 2020 2 commits
    • Masahiro Yamada's avatar
      kbuild: create modules.builtin without Makefile.modbuiltin or tristate.conf · 8b41fc44
      Masahiro Yamada authored
      Commit bc081dd6 ("kbuild: generate modules.builtin") added
      infrastructure to generate modules.builtin, the list of all
      builtin modules.
      
      Basically, it works like this:
      
        - Kconfig generates include/config/tristate.conf, the list of
          tristate CONFIG options with a value in a capital letter.
      
        - scripts/Makefile.modbuiltin makes Kbuild descend into
          directories to collect the information of builtin modules.
      
      I am not a big fan of it because Kbuild ends up with traversing
      the source tree twice.
      
      I am not sure how perfectly it should work, but this approach cannot
      avoid false positives; even if the relevant CONFIG option is tristate,
      some Makefiles forces obj-m to obj-y.
      
      Some examples are:
      
        arch/powerpc/platforms/powermac/Makefile:
          obj-$(CONFIG_NVRAM:m=y)         += nvram.o
      
        net/ipv6/Makefile:
          obj-$(subst m,y,$(CONFIG_IPV6)) += inet6_hashtables.o
      
        net/netlabel/Makefile:
          obj-$(subst m,y,$(CONFIG_IPV6)) += netlabel_calipso.o
      
      Nobody has complained about (or noticed) it, so it is probably fine to
      have false positives in modules.builtin.
      
      This commit simplifies the implementation. Let's exploit the fact
      that every module has MODULE_LICENSE(). (modpost shows a warning if
      MODULE_LICENSE is missing. If so, 0-day bot would already have blocked
      such a module.)
      
      I added MODULE_FILE to <linux/module.h>. When the code is being compiled
      as builtin, it will be filled with the file path of the module, and
      collected into modules.builtin.info. Then, scripts/link-vmlinux.sh
      extracts the list of builtin modules out of it.
      
      This new approach fixes the false-positives above, but adds another
      type of false-positives; non-modular code may have MODULE_LICENSE()
      by mistake. This is not a big deal, it is just the code is always
      orphan. We can clean it up if we like. You can see cleanup examples by:
      
        $ git log --grep='make.* explicitly non-modular'
      
      To sum up, this commits deletes lots of code, but still produces almost
      equivalent results. Please note it does not increase the vmlinux size at
      all. As you can see in include/asm-generic/vmlinux.lds.h, the .modinfo
      section is discarded in the link stage.
      Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      8b41fc44
    • Masahiro Yamada's avatar
      kbuild: add stringify helper to quote a string passed to C files · 7e826c44
      Masahiro Yamada authored
      Make $(squote)$(quote)...$(quote)$(squote) a helper macro.
      I will reuse it in the next commit.
      Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      7e826c44
  21. 11 Nov, 2019 2 commits
    • Masahiro Yamada's avatar
      kbuild: rename any-prereq to newer-prereqs · eba19032
      Masahiro Yamada authored
      GNU Make manual says:
      
        $?
            The names of all the prerequisites that are newer than the target,
            with spaces between them.
      
      To reflect this, rename any-prereq to newer-prereqs, which is clearer
      and more intuitive.
      Signed-off-by: default avatarMasahiro Yamada <yamada.masahiro@socionext.com>
      eba19032
    • Masahiro Yamada's avatar
      kbuild: drop $(wildcard $^) check in if_changed* for faster rebuild · 2d3b1b8f
      Masahiro Yamada authored
      The incremental build of Linux kernel is pretty slow when lots of
      objects are compiled. The rebuild of allmodconfig may take a few
      minutes even when none of the objects needs to be rebuilt.
      
      The time-consuming part in the incremental build is the evaluation of
      if_changed* macros since they are used in the recipes to compile C and
      assembly source files into objects.
      
      I notice the following code in if_changed* is expensive:
      
        $(filter-out $(PHONY) $(wildcard $^),$^)
      
      In the incremental build, every object has its .*.cmd file, which
      contains the auto-generated list of included headers. So, $^ are
      expanded into the long list of the source file + included headers,
      and $(wildcard $^) checks whether they exist.
      
      It may not be clear why this check exists there.
      
      Here is the record of my research.
      
      [1] The first code addition into Kbuild
      
      This code dates back to 2002. It is the pre-git era. So, I copy-pasted
      it from the historical git tree.
      
      | commit 4a6db079 (HEAD)
      | Author: Kai Germaschewski <kai@tp1.ruhr-uni-bochum.de>
      | Date:   Mon Jun 17 00:22:37 2002 -0500
      |
      |     kbuild: Handle removed headers
      |
      |     New and old way to handle dependencies would choke when a file
      |     #include'd by other files was removed, since the dependency on it was
      |     still recorded, but since it was gone, make has no idea what to do about
      |     it (and would complain with "No rule to make <file> ...")
      |
      |     We now add targets for all the previously included files, so make will
      |     just ignore them if they disappear.
      |
      | diff --git a/Rules.make b/Rules.make
      | index 6ef827d3df39..7db5301ea7db 100644
      | --- a/Rules.make
      | +++ b/Rules.make
      | @@ -446,7 +446,7 @@ if_changed = $(if $(strip $? \
      |  # execute the command and also postprocess generated .d dependencies
      |  # file
      |
      | -if_changed_dep = $(if $(strip $? \
      | +if_changed_dep = $(if $(strip $? $(filter-out FORCE $(wildcard $^),$^)\
      |                           $(filter-out $(cmd_$(1)),$(cmd_$@))\
      |                           $(filter-out $(cmd_$@),$(cmd_$(1)))),\
      |         @set -e; \
      | diff --git a/scripts/fixdep.c b/scripts/fixdep.c
      | index b5d7bee8efc7..db45bd1888c0 100644
      | --- a/scripts/fixdep.c
      | +++ b/scripts/fixdep.c
      | @@ -292,7 +292,7 @@ void parse_dep_file(void *map, size_t len)
      |                 exit(1);
      |         }
      |         memcpy(s, m, p-m); s[p-m] = 0;
      | -       printf("%s: \\\n", target);
      | +       printf("deps_%s := \\\n", target);
      |         m = p+1;
      |
      |         clear_config();
      | @@ -314,7 +314,8 @@ void parse_dep_file(void *map, size_t len)
      |                 }
      |                 m = p + 1;
      |         }
      | -       printf("\n");
      | +       printf("\n%s: $(deps_%s)\n\n", target, target);
      | +       printf("$(deps_%s):\n", target);
      |  }
      |
      |  void print_deps(void)
      
      The "No rule to make <file> ..." error can be solved by passing -MP to
      the compiler, but I think the detection of header removal is a good
      feature. When a header is removed, all source files that previously
      included it should be re-compiled. This makes sure we has correctly
      got rid of #include directives of it.
      
      This is also related with the behavior of $?. The GNU Make manual says:
      
        $?
            The names of all the prerequisites that are newer than the target,
            with spaces between them.
      
      This does not explain whether a non-existent prerequisite is considered
      to be newer than the target.
      
      At this point of time, GNU Make 3.7x was used, where the $? did not
      include non-existent prerequisites. Therefore,
      
        $(filter-out FORCE $(wildcard $^),$^)
      
      was useful to detect the header removal, and to rebuild the related
      objects if it is the case.
      
      [2] Change of $? behavior
      
      Later, the behavior of $? was changed (fixed) to include prerequisites
      that did not exist.
      
      First, GNU Make commit 64e16d6c00a5 ("Various changes getting ready for
      the release of 3.81.") changed it, but in the release test of 3.81, it
      turned out to break the kernel build.
      
      See these:
      
       - http://lists.gnu.org/archive/html/bug-make/2006-03/msg00003.html
       - https://savannah.gnu.org/bugs/?16002
       - https://savannah.gnu.org/bugs/?16051
      
      Then, GNU Make commit 6d8d9b74d9c5 ("Numerous updates to tests for
      issues found on Cygwin and Windows.") reverted it for the 3.81 release
      to give Linux kernel time to adjust to the new behavior.
      
      After the 3.81 release, GNU Make commit 7595f38f62af ("Fixed a number
      of documentation bugs, plus some build/install issues:") re-added it.
      
      [3] Adjustment to the new $? behavior on Kbuild side
      
      Meanwhile, the kernel build was changed by commit 4f193362 ("kbuild:
      change kbuild to not rely on incorrect GNU make behavior") to adjust to
      the new $? behavior.
      
      [4] GNU Make 3.82 released in 2010
      
      GNU Make 3.82 was the first release that integrated the correct $?
      behavior. At this point, Kbuild dealt with GNU Make versions with
      different $? behaviors.
      
       3.81 or older:
          $? does not contain any non-existent prerequisite.
          $(filter-out $(PHONY) $(wildcard $^),$^) was useful to detect
          removed include headers.
      
       3.82 or newer:
          $? contains non-existent prerequisites. When a header is removed,
          it appears in $?. $(filter-out $(PHONY) $(wildcard $^),$^) became
          a redundant check.
      
      With the correct $? behavior, we could have dropped the expensive
      check for 3.82 or later, but we did not. (Maybe nobody noticed this
      optimization.)
      
      [5] The .SECONDARY special target trips up $?
      
      Some time later, I noticed $? did not work as expected under some
      circumstances. As above, $? should contain non-existent prerequisites,
      but the ones specified as SECONDARY do not appear in $?.
      
      I asked this in GNU Make ML, and it seems a bug:
      
        https://lists.gnu.org/archive/html/bug-make/2019-01/msg00001.html
      
      Since commit 8e9b61b2 ("kbuild: move .SECONDARY special target to
      Kbuild.include"), all files, including headers listed in .*.cmd files,
      are treated as secondary.
      
      So, we are back into the incorrect $? behavior.
      
      If we Kbuild want to react to the header removal, we need to keep
      $(filter-out $(PHONY) $(wildcard $^),$^) but this makes the rebuild
      so slow.
      
      [Summary]
      
       - I believe noticing the header removal and recompiling related objects
         is a nice feature for the build system.
      
       - If $? worked correctly, $(filter-out $(PHONY),$?) would be enough
         to detect the header removal.
      
       - Currently, $? does not work correctly when used with .SECONDARY,
         and Kbuild is hit by this bug.
      
       - I filed a bug report for this, but not fixed yet as of writing.
      
       - Currently, the header removal is detected by the following expensive
         code:
      
          $(filter-out $(PHONY) $(wildcard $^),$^)
      
       - I do not want to revert commit 8e9b61b2 ("kbuild: move
         .SECONDARY special target to Kbuild.include"). Specifying
         .SECONDARY globally is clean, and it matches to the Kbuild policy.
      
      This commit proactively removes the expensive check since it makes the
      incremental build faster. A downside is Kbuild will no longer be able
      to notice the header removal.
      
      You can confirm it by the full-build followed by a header removal, and
      then re-build.
      
        $ make defconfig all
          [ full build ]
        $ rm include/linux/device.h
        $ make
          CALL    scripts/checksyscalls.sh
          CALL    scripts/atomic/check-atomics.sh
          DESCEND  objtool
          CHK     include/generated/compile.h
        Kernel: arch/x86/boot/bzImage is ready  (#11)
          Building modules, stage 2.
          MODPOST 12 modules
      
      Previously, Kbuild noticed a missing header and emits a build error.
      Now, Kbuild is fine with it. This is an unusual corner-case, not a big
      deal. Once the $? bug is fixed in GNU Make, everything will work fine.
      Signed-off-by: default avatarMasahiro Yamada <yamada.masahiro@socionext.com>
      2d3b1b8f
  22. 01 Oct, 2019 1 commit
  23. 27 Jul, 2019 1 commit
  24. 17 Jul, 2019 1 commit
  25. 11 Jul, 2019 1 commit
    • Masahiro Yamada's avatar
      kbuild: use -- separater intead of $(filter-out ...) for cc-cross-prefix · d4a74bbf
      Masahiro Yamada authored
      arch/mips/Makefile passes prefixes that start with '-' to cc-cross-prefix
      when $(tool-archpref) evaluates to the empty string.
      
      They are filtered-out before the $(shell ...) invocation. Otherwise,
      'command -v' would be confused.
      
        $ command -v -linux-gcc
        bash: command: -l: invalid option
        command: usage: command [-pVv] command [arg ...]
      
      Since commit 913ab978 ("kbuild: use more portable 'command -v' for
      cc-cross-prefix"), cc-cross-prefix throws away the stderr output, so
      the console is not polluted in any way.
      
      This is not a big deal in practice, but I see a slightly better taste
      in adding '--' to teach it that '-linux-gcc' is an argument instead of
      a command option.
      
      This will cause extra forking of subshell, but it will not be noticeable
      performance regression.
      Signed-off-by: default avatarMasahiro Yamada <yamada.masahiro@socionext.com>
      d4a74bbf
  26. 01 Jul, 2019 3 commits
    • Masahiro Yamada's avatar
      kbuild: save $(strip ...) for calling if_changed and friends · c2341e2a
      Masahiro Yamada authored
      The string returned by $(filter-out ...) does not contain any leading
      or trailing spaces.
      
      With the previous commit, 'any-prereq' no longer contains any
      excessive spaces.
      
      Nor does 'cmd-check' since it expands to a $(filter-out ...) call.
      
      So, only the space that matters is the one between 'any-prereq'
      and 'cmd-check'.
      
      By removing it from the code, we can save $(strip ...) evaluation.
      This refactoring is possible because $(any-prereq)$(cmd-check) is only
      passed to the first argument of $(if ...), so we are only interested
      in whether or not it is empty.
      Signed-off-by: default avatarMasahiro Yamada <yamada.masahiro@socionext.com>
      c2341e2a
    • Masahiro Yamada's avatar
      kbuild: save $(strip ...) for calling any-prepreq · 93f31bbd
      Masahiro Yamada authored
      The string returned by $(filter-out ...) does not contain any leading
      or trailing spaces.
      
      So, only the space that matters is the one between
      
        $(filter-out $(PHONY),$?)
      
      and
      
        $(filter-out $(PHONY) $(wildcard $^),$^)
      
      By removing it from the code, we can save $(strip ...) evaluation.
      This refactoring is possible because $(any-prereq) is only passed to
      the first argument of $(if ...), so we are only interested in whether
      or not it is empty.
      
      This is also the prerequisite for the next commit.
      Signed-off-by: default avatarMasahiro Yamada <yamada.masahiro@socionext.com>
      93f31bbd
    • Masahiro Yamada's avatar
      kbuild: rename arg-check to cmd-check · 50bcca6a
      Masahiro Yamada authored
      I prefer 'cmd-check' for consistency.
      
      We have 'echo-cmd', 'cmd', 'cmd_and_fixdep', etc. in this file.
      Signed-off-by: default avatarMasahiro Yamada <yamada.masahiro@socionext.com>
      50bcca6a
  27. 15 Jun, 2019 1 commit
  28. 14 Jun, 2019 1 commit
    • Mauro Carvalho Chehab's avatar
      docs: kbuild: convert docs to ReST and rename to *.rst · cd238eff
      Mauro Carvalho Chehab authored
      The kbuild documentation clearly shows that the documents
      there are written at different times: some use markdown,
      some use their own peculiar logic to split sections.
      
      Convert everything to ReST without affecting too much
      the author's style and avoiding adding uneeded markups.
      
      The conversion is actually:
        - add blank lines and identation in order to identify paragraphs;
        - fix tables markups;
        - add some lists markups;
        - mark literal blocks;
        - adjust title markups.
      
      At its new index.rst, let's add a :orphan: while this is not linked to
      the main index.rst file, in order to avoid build warnings.
      Signed-off-by: default avatarMauro Carvalho Chehab <mchehab+samsung@kernel.org>
      Signed-off-by: default avatarJonathan Corbet <corbet@lwn.net>
      cd238eff
  29. 07 Jun, 2019 1 commit
    • Masahiro Yamada's avatar
      kbuild: use more portable 'command -v' for cc-cross-prefix · 913ab978
      Masahiro Yamada authored
      To print the pathname that will be used by shell in the current
      environment, 'command -v' is a standardized way. [1]
      
      'which' is also often used in scripts, but it is less portable.
      
      When I worked on commit bd55f96f ("kbuild: refactor cc-cross-prefix
      implementation"), I was eager to use 'command -v' but it did not work.
      (The reason is explained below.)
      
      I kept 'which' as before but got rid of '> /dev/null 2>&1' as I
      thought it was no longer needed. Sorry, I was wrong.
      
      It works well on my Ubuntu machine, but Alexey Brodkin reports noisy
      warnings on CentOS7 when 'which' fails to find the given command in
      the PATH environment.
      
        $ which foo
        which: no foo in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin)
      
      Given that behavior of 'which' depends on system (and it may not be
      installed by default), I want to try 'command -v' once again.
      
      The specification [1] clearly describes the behavior of 'command -v'
      when the given command is not found:
      
        Otherwise, no output shall be written and the exit status shall reflect
        that the name was not found.
      
      However, we need a little magic to use 'command -v' from Make.
      
      $(shell ...) passes the argument to a subshell for execution, and
      returns the standard output of the command.
      
      Here is a trick. GNU Make may optimize this by executing the command
      directly instead of forking a subshell, if no shell special characters
      are found in the command and omitting the subshell will not change the
      behavior.
      
      In this case, no shell special character is used. So, Make will try
      to run it directly. However, 'command' is a shell-builtin command,
      then Make would fail to find it in the PATH environment:
      
        $ make ARCH=m68k defconfig
        make: command: Command not found
        make: command: Command not found
        make: command: Command not found
      
      In fact, Make has a table of shell-builtin commands because it must
      ask the shell to execute them.
      
      Until recently, 'command' was missing in the table.
      
      This issue was fixed by the following commit:
      
      | commit 1af314465e5dfe3e8baa839a32a72e83c04f26ef
      | Author: Paul Smith <psmith@gnu.org>
      | Date:   Sun Nov 12 18:10:28 2017 -0500
      |
      |     * job.c: Add "command" as a known shell built-in.
      |
      |     This is not a POSIX shell built-in but it's common in UNIX shells.
      |     Reported by Nick Bowler <nbowler@draconx.ca>.
      
      Because the latest release is GNU Make 4.2.1 in 2016, this commit is
      not included in any released versions. (But some distributions may
      have back-ported it.)
      
      We need to trick Make to spawn a subshell. There are various ways to
      do so:
      
       1) Use a shell special character '~' as dummy
      
          $(shell : ~; command -v $(c)gcc)
      
       2) Use a variable reference that always expands to the empty string
          (suggested by David Laight)
      
          $(shell command$${x:+} -v $(c)gcc)
      
       3) Use redirect
      
          $(shell command -v $(c)gcc 2>/dev/null)
      
      I chose 3) to not confuse people. The stderr would not be polluted
      anyway, but it will provide extra safety, and is easy to understand.
      
      Tested on Make 3.81, 3.82, 4.0, 4.1, 4.2, 4.2.1
      
      [1] http://pubs.opengroup.org/onlinepubs/9699919799/utilities/command.html
      
      Fixes: bd55f96f ("kbuild: refactor cc-cross-prefix implementation")
      Cc: linux-stable <stable@vger.kernel.org> # 5.1
      Reported-by: default avatarAlexey Brodkin <abrodkin@synopsys.com>
      Signed-off-by: default avatarMasahiro Yamada <yamada.masahiro@socionext.com>
      Tested-by: default avatarAlexey Brodkin <abrodkin@synopsys.com>
      913ab978