mesonbuild / meson

The Meson Build System
http://mesonbuild.com
Apache License 2.0
5.58k stars 1.62k forks source link

jar() can't deal with generate source code under the build directory #3023

Open rib opened 6 years ago

rib commented 6 years ago

I just tried using configure_file() to process a GlimpseConfig.java.in in our project with a file like:

package com.impossible.glimpse;

public class GlimpseConfig
{
    public static final int USE_TANGO = @use_tango@;
}

This results in a file under our build directory like build/src/java/com/impossible/glimpse/GlimpseConfig.java

Passing 'com/impossible/glimpse/GlimpseConfig.java' as a source file to jar() doesn't currently work though.

rib commented 6 years ago

Actually this comes down to user error on my part, though it feels a little awkward...

Firstly, instead of using our top-level meson.build, I have a src/java/meson.build which I created due to the restriction jar() has that the package namespace directories for Java source code must be relative to the meson.build where jar() is used.

Then since configure_file() has a different restriction where the output filename can't include a subdirectory we have another meson.build file at src/java/com/impossible/glimpse/meson.build purely to handle our use of configure_file() to generate GlimpseConfig.java

The mistake was that I shouldn't pass the string literal 'com/impossible/glimpse/GlimpseConfig.java' to jar() (and I can see that it would be awkward for meson to match it up with the generated file). Instead we should do something like glimpse_config_file = configure_file(...) in the second meson.build and then pass that variable as source to jar() in the first meson.build.

I wonder if the restriction on configure_file's output being in a subdirectory could be relaxed (maybe asserting that it matches the subdirectory of the input file to restrict random access to the build directory) so we could keep these related things next to each other.

rib commented 6 years ago

Ah, no, it really doesn't work :-)

So after making the first mistake described above, and then taking a long time to realize I also needed to pass include_directories: include_directories('.') for my code to compile (due to class interdependencies), I thought all was well because my generated code also compiled. Unfortunately that was only true until my non-generated code needed to reference the GlimpseConfig class in the generated file.

The problem is that javac needs to have the location for the generated com/impossible/glimpse/ hierarchy, under the build directory, added to the given -sourcepath (./src/java in this case). Without that then if any non-generated code tries to reference the GlimpseConfig class then javac won't be able to automatically compile the dependency class as part of its default -implicit:class behaviour.

I've tested a patch to address this which I can open a pull request for.