How to include external header only librarys into gradle native builds with gradle's (5.3.1) new nativ eplugin?

581 views Asked by At

Evaluating gradle native i want to include a header only lib, {fmt}, to be used in a gradle native multiproject with the new gradle native plugin.

I've tried including it via flatDir and maven repositories. The Documentation is only mentioning benary dependencies. But even then it seems that foreign code has to be extended with a build.gradle file. Below you see my last try

build.gradle at root

allprojects {
    apply plugin: 'xcode'
    apply plugin: 'visual-studio'

    configurations {
        fmtLib
    }

    dependencies {
        fmtLib files(file("$rootDir/../fmt"))
    }
}

build.gradle at library which shall use {fmt}

plugins {
    id 'cpp-library'
}

library {
    linkage = [Linkage.SHARED]

    targetMachines = [
        machines.windows.x86_64,
        machines.macOS.x86_64,
        machines.linux.x86_64
    ]

    baseName = "greeter"
}

greeter.cpp which shall use {fmt}

#define FMT_HEADER_ONLY

#include <iostream>
#include "../public/greeter.hpp"
#include "include/fmt/format.h"

void Greeter::greet() {
    fmt::print("Hello, {}!", "world");
    std::cout << "Hello, " << name << ", your name has " << getNameLength() << " chars." << std::endl;
}

int Greeter::getNameLength() {
    return name.length();
}

The example above leads to a compiling error which is clear because unresolvable dependencies.

1

There are 1 answers

0
Ben1980 On BEST ANSWER

Have found the solution myself. Unfortunately gradle is not providing a better solution until 5.3.1 but a workaround can be done by

def fmtHeaders = file("$rootDir/../fmt/include")

components.main.binaries.whenElementFinalized { binary ->
    project.dependencies {
        if (binary.optimized) {
            add(binary.includePathConfiguration.name, files(fmtHeaders))
        } else {
            add(binary.includePathConfiguration.name, files(fmtHeaders))
        }
    }
}