2

For comparing Linux kernel compile times it would be very helpful if there would be a number for (actually) compiled lines of code afterwards. Doing a 'cloc' to kernel source previously to compiling is only part of a solution, because this value would be equal for every kernel configuration, no matter if configured, for example, as 'tinyconfig' or 'allyesconfig' or 'localmodconfig'.

Added request to gcc mailing list that might support this question
( https://gcc.gnu.org/ml/gcc-help/2019-03/msg00057.html )

Edit (from comment below answer from Mike Kinghan 03/09/2019):
"This number should be a normalizing factor for comparing compile time for different versions of linux kernels for previous versions and coming years."

beyondtime
  • 139
  • 1
  • 13

1 Answers1

2

It seems that you want to count lines of code consumed by the compiler after preprocessing. Exactly what is meant by that is debatable, but I assume a reasonable way of counting would be good enough for your purposes. A GNU Make makefile such as:

Makefile

%.o: %.c    # Cancel built-in rule
%.o: %.i
    $(CC) $(CFLAGS) -c $< -o $@
    @loc=$$(wc -l $<); echo "Compiled $${loc%% *} LOC from $<"

%.i: %.c
    $(CC) $(CPPFLAGS) -E -P $< > $@

illustrates a way of doing it:-

  • Preprocess the source file first.
  • Count the lines of preprocessed output.
  • Compile the preprocessed output.

The Makefile avoids the wastage of preprocessing the source code twice, because gcc recognises the extension .i as signifying a file of already-preprocessed C source and will not preprocess it again. It is also unncessary to clean up the intermediate .i files because GNU make recognises them as intermediate and auto-deletes them. Example:

$ cat foo.c
#ifdef FOO

/* A
 * B
 * C
 */
int foo(int x, int y)
{
    while(x < y) {
        if (++x == y) {
            return y;
        }
        --y;
    }
    while(y < x) {
        if (++y == x) {
            return x;
        }
        --x;
    }
    return y;
}

#else

/* D */
int bar(int x) {
    return x * x;
}

#endif

$ make foo.o
cc  -E -P foo.c > foo.i
cc  -c foo.i -o foo.o
Compiled 3 LOC from foo.i
rm foo.i

$ rm foo.o
$ make CPPFLAGS=-DFOO foo.o
cc -DFOO -E -P foo.c > foo.i
cc  -c foo.i -o foo.o
Compiled 16 LOC from foo.i
rm foo.i

You probably want to pass the -P option in the preprocessing step, as shown, to suppress the production of line-marked output that will inflate the line-count. E.g. your probably don't want foo.i to be:

$ cc -DFOO -E foo.c > foo.i
$ cat foo.i
# 1 "foo.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 1 "<command-line>" 2
# 1 "foo.c"






int foo(int x, int y)
{
    while(x < y) {
        if (++x == y) {
            return y;
        }
        --y;
    }
    while(y < x) {
        if (++y == x) {
            return x;
        }
        --x;
    }
    return y;
}

but rather want it to be:

$ cc -DFOO -E -P foo.c > foo.i
$ cat foo.i
int foo(int x, int y)
{
    while(x < y) {
        if (++x == y) {
            return y;
        }
        --y;
    }
    while(y < x) {
        if (++y == x) {
            return x;
        }
        --x;
    }
    return y;
}

Obviously, for a make-system that compiled many source files, you would devise more or different apparatus from adding up the per-file LOC reports.

Mike Kinghan
  • 55,740
  • 12
  • 153
  • 182
  • A very nicely thorough answer. – wallyk Mar 09 '19 at 16:56
  • Thx wallyk, Thx @Mike Kinghan : It does not matter that much when in compiling process (once) lines of processed code are measured/counted. This number should be a normalizing factor for comparing compile time for different versions of linux kernels for previous versions and coming years. This resulting into a real world (comparable, overall) hardware benchmark number (as long gcc stays on cpu not gpu https://stackoverflow.com/questions/8417053/is-it-possible-to-use-gpu-acceleration-on-compiling-multiple-programs-on-a-gcc-c) while doing some useful compiling on a stable reference source code. – beyondtime Mar 09 '19 at 19:26
  • Inspired by http://wiki.ant-computing.com/Choosing_a_processor_for_a_build_farm#Overview – beyondtime Mar 09 '19 at 19:32