Skylark to write to gendir instead of bin?

205 views
Skip to first unread message

alpha....@gmail.com

unread,
Mar 30, 2016, 3:31:48 PM3/30/16
to bazel-discuss
Hi again,

I wonder if there's a flag to tell a skylark rule to write to genfiles directory instead of bin?

The reason for this is it generates JNI header files and I would like to include the headers. If they are generated in genfiles then I can use this to include them:

cc_libary(
  name = "something",
  includes = [ "." ],
)

Any suggestions would be great. After this exercise I think I should write up a quick example for JNI..

Thanks in advance!

Alpha

Alex Humesky

unread,
Mar 30, 2016, 3:33:58 PM3/30/16
to alpha....@gmail.com, bazel-discuss
I think output_to_genfiles is what you're looking for:

--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/f3ef983c-9cd0-4f9c-aa9a-774a82eb82d4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

alpha....@gmail.com

unread,
Mar 30, 2016, 4:12:26 PM3/30/16
to bazel-discuss, alpha....@gmail.com
I have this skylark rule that works for me. Thanks Alex.

=== jni_library.bzl ===
def genjni_impl(ctx):
  cmd = (
    '%s ' % ctx.executable._javah.path +
    '-cp %s ' % ':'.join([jar.class_jar.path for jar in ctx.attr.lib.java.outputs.jars]) +
    '-d %s ' % ctx.outputs.header.dirname +
    '%s' % ctx.attr.classname
  )
  ctx.action(
      inputs = [ ctx.file.lib, ctx.executable._javah ],
      outputs = [ ctx.outputs.header ],
      mnemonic = 'Javah',
      command = cmd,
      use_default_shell_env = True)
  return struct()

genjni_internal = rule(genjni_impl,
   attrs = {
       "lib": attr.label(mandatory=True, single_file=True),
       "classname": attr.string(mandatory=True),
       "_javah": attr.label(
           default=Label("@local_jdk//:javah"),
           executable=True),
   },
   outputs = {"header": "%{name}.h"},
   output_to_genfiles = True,
)

def jni_library(name, lib, classes):
  hdrs = []
  for c in classes:
    header = c.replace('.', '_')
    genjni_internal(
      name=header,
      lib=lib,
      classname=c)
    hdrs = hdrs + [ ':' + header + '.h' ]
  native.cc_library(
    name=name,
    hdrs=hdrs,
    includes=["."])

=== BUILD ===

jni_library(
  name = "something_jni",
  lib = ":java",
  classes = [ "com.Something" ] ,
)

cc_library(
  name = "vfs_jni",
  srcs = [
    "Something.c",
  ],
  deps = [
    ":something_jni",
    "@local_jdk//:jdk_include",
  ],
)
Reply all
Reply to author
Forward
0 new messages