Search code examples
buck

Java file generated by a genrule not included into compiling procedure


I face a problem in buck gen_rule usage.

I have a executable jar file, called SqlDelightBin, which could generate Java source files, during gradle build process, there will be a gradle task to run this jar file, and the generated Java files could be compiled properly.

But when I wrap this jar file with a gen_rule, named sqldelight_devDebug, and add it into my android_library’s deps list, although this gen_rule runs properly, but the generated Java files are not compiled in this compile round, so the generated class could not be found, when I run the buck build command again, it succeed.

My gen_rule is:

genrule(
    name = 'sqldelight_devDebug',
    srcs = glob([
        'src/*/sqldelight/**/*.sq',
    ]),
    out = 'out',
    bash = 'java -jar /Users/piasy/src/OkBuck/.okbuck/cache/c61171f7a8bee5d459102d49daecb0b6/SqlDelightBin-0.4.3.jar $SRCDIR /Users/piasy/src/OkBuck/app/build/okbuck/sqldelight && echo $SRCS > $OUT',
)

It run the jar file, which will compile SQL files in src/*/sqldelight/ to Java files in /Users/piasy/src/OkBuck/app/build/okbuck/sqldelight dir, and to make BUCK think this genrule succeed, we create the $OUT file with SQL file list.

My android_library is:

android_library(
    name = 'src_devDebug',
    srcs = glob([
        'src/main/java/**/*.java',
        'build/okbuck/sqldelight/**/*.java’, # the generated Java files
        'src/dev/java/**/*.java',
    ]),
    # other params
    deps = [
        # other deps
        ':sqldelight_devDebug',
    ],
    visibility = [
        'PUBLIC',
    ],
)

Solution

  • If you want to use the file generated by the sqldelight_devDebug target as a source, add that target name to srcs, like this:

    android_library(
        name = 'src_devDebug',
        srcs = [
            ':sqldelight_devDebug',
        ] + glob([
            'src/main/java/**/*.java',
            'src/dev/java/**/*.java',
        ]),
        # other params
        deps = [
            # other deps
        ],
        visibility = [
            'PUBLIC',
        ],
    )