John Basila
John Basila

Reputation: 163

Building kernel module with Bazel

I would like to use Bazel to build a Linux kernel module. I have sources that contain the logic of the module: logic.c The process I used so far:

  1. compile the 'logic.c' to a logic.o or logic.a
  2. execute the 'modpost' tool to generate the logic-modpost.c
  3. compile the output of the 'modpost' tool 'logic-modpost.c' to 'logic-modpost.o' or logic-modpost.a'
  4. link everything together with 'ld -r'

Before I start making this work with Skylark, I was wondering if there is a well known recipe that I don't know of and if it can be shared.

I also noticed that the cpp fragment does not expose the 'ld' tool, I was wondering why is that? I know I can use the 'gcc' with -Xlinker or -Wl, to achieve almost the same, but it would have been nice to get access to the ld.

-- John

Upvotes: 3

Views: 1759

Answers (1)

kris
kris

Reputation: 23592

AFAIK, there's no existing recipe. However, if you can link everything with g++ (instead of ld directly), you could do this as a macro like:

def mod(name, srcs, deps):
  cc_library(
      name = "%s-1" % name,
      srcs = srcs,
      deps = deps,
  )
  genrule(
      name = "%s-modpost" % name,
      srcs = ["%s-1.so" % name],
      tools = ["//path/to:modpost"],
      cmd = "$(location //path/to:modpost) $(location :%s-1.so) $@" % name,
      outs = ["%s-modpost.c"],
 )
 cc_library(
      name = "%s-2" % name,
      srcs = [":%s-modpost.c" % name],
      deps = deps,
 )
 genrule(
      name = "%s" % name,
      srcs = ["%s-2.a" % name],
      cmd = "$(CC) $(CCFLAGS) -Wlr $(location :%s-2.a) -o $@" % name,
      outs = ["%s.so" % name],
 )

If you want/need to use Skylark, I don't think there's any reason we can't expose ld, it just hasn't happened yet. You could file a bug or make a pull request adding a @SkylarkCallable annotation to getLdExecutable().

Upvotes: 3

Related Questions