Skip to content Skip to sidebar Skip to footer

Gradle Experimental Include File Directories Via SrcDir SrcFile Directive

Does anyone knows how the includes of srcDir works in gradle experimental files (cpp AND h)? This is kind of a '3-fold' question: 1°) how do the srcDir work? 1.a°) Does it includ

Solution 1:

I can at least answer my first point. After digging through the sources (at some point it becomes the best documentation), I found the implementation of the methods that retrieves sources:

public Set<File> getSrcDirs() {
    Set<File> dirs = new LinkedHashSet<File>();
    for (DirectoryTree tree : getSrcDirTrees()) {
        dirs.add(tree.getDir());
    }
    return dirs;
}

in DefaultSourceDirectorySet.java.

this method is used in the gradle-eperimental plugin to retrieve cpp AND c sources:

            languageSourceSets.create(
                    sourceSetName + "Cpp",
                    CppSourceSet.class,
                    new Action<CppSourceSet>() {
                        @Override
                        public void execute(CppSourceSet source) {
                            source.getSource().setSrcDirs(jni.getSource().getSrcDirs());
                            source.getSource().include("**/*.C");
                            source.getSource().include("**/*.CPP");
                            source.getSource().include("**/*.c++");
                            source.getSource().include("**/*.cc");
                            source.getSource().include("**/*.cp");
                            source.getSource().include("**/*.cpp");
                            source.getSource().include("**/*.cxx");
                            source.getSource().exclude(jni.getSource().getExcludes());
                            source.exportedHeaders(new Action<SourceDirectorySet>() {
                                @Override
                                public void execute(SourceDirectorySet files) {
                                    files.source(jni.getExportedHeaders());
                                }
                            });
                            configurePrebuiltDependency(source, jni);
                        }
                    });

in execute method from the NdkConfiguration.java file.

For the header, it's different, there is no such things as source.getSource().include("**/*.H");

, instead we have:

            for (LanguageSourceSet sourceSet : nativeBinary.getSources()) {
                if (sourceSet instanceof HeaderExportingSourceSet) {
                    HeaderExportingSourceSet source = (HeaderExportingSourceSet) sourceSet;
                    artifact.getExportedHeaderDirectories().addAll(
                            source.getExportedHeaders().getSrcDirs());
                }
            }

In the execute method of the NdkComponentModelPlugin.java. It uses directly the getSrcDirs method of the default gradle implementation, which include recursively all dir under the given one

I will keep on investigating the other points

EDIT: In summary:

1°) a°) srcDir includes all file matching the pattern: *.C, *.CPP, *.c++, *.cc, *.cp, *.cpp, *.cxx

b°) includeHeaders works, and only include the header at the given folder depth (so you have to give all the subpath to the header if you are including them like this: #include "test.h", instead of #include "dir1/dir2/dir3/test.h"

2°) it looks I was doing some things wrong in my includes, as they now do seem to work. However, you can't just include files. It is thus better to include top dir of source file, and then exclude every file that doesn't match a given pattern (as described in Alex Cohn answer)

3°) The include directive doesn't work


Solution 2:

For v. 0.4.0 srcDirs includes all subdirectories and exclude works on patterns. I don't know about plans to enable include or srcFile.

android.sources {
    main {
       jni.source {
            srcDirs = ["~/srcs/jni"]
            exclude "**/win.cpp"
        }
    }
}

See also https://stackoverflow.com/a/32640823/192373

Even for 0.6.0 the docs don't speak about exportedHeder, and generally speaking the gradle native docs are not relevant for Android plugin.


Post a Comment for "Gradle Experimental Include File Directories Via SrcDir SrcFile Directive"