Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support conditional generation of sources #1857

Open
veelo opened this issue Jan 14, 2020 · 0 comments
Open

Support conditional generation of sources #1857

veelo opened this issue Jan 14, 2020 · 0 comments

Comments

@veelo
Copy link
Contributor

veelo commented Jan 14, 2020

Dub supports generation of sources using preGenerateCommands (disregarding issue #1474). However, the advertised way causes sources to always be (re)generated, causing unnecessary recompilation. This will of course escalate if there are sources that depend on the generated source, and/or other projects that depend on this project. It is possible to prevent this, but it requires jumping through additional hoops and boiler plate code.

This could be made possible with an interface like this:

"addBuildRule": {
    "input": [ "usesCinclude.dpp" ],
    "output": [ "usesCinclude.d" ],
    "command": "cd $PACKAGE_DIR/source && dub run dpp -- --preprocess-only usesCinclude.dpp"
}

The way to do this currently:

"excludedSourceFiles": [
    "source/make.d"
],
"preGenerateCommands": [
    "cd $PACKAGE_DIR/source && rdmd make.d"
]
// soure/make.d
void main()
{
  make(["usesCinclude.dpp"], ["usesCinclude.d"], ["dub", "run", "dpp", "--", "--preprocess-only", "usesCinclude.dpp"]);
}

void make(string[] input, string[] output, string[] args)
{
    import std.file, std.datetime.systime, std.process, std.stdio, std.algorithm.iteration, std.exception;
    input ~= __FILE__;
    if ((){
            foreach (outp; output)
            {
                immutable outpTime = timeLastModified(outp, SysTime.min);
                foreach (inp; input)
                    if (timeLastModified(inp) > outpTime)
                        return true;
            }
            return false;
        }())
    {
        stderr.writeln(args.joiner(" "));
        auto dmd = execute(args);
        enforce(dmd.status == 0, "Compilation failed:\n" ~ dmd.output);
    }
}

Dead ends

I have tried integrating d code directly into the recipe file, but this is currently limited to one-liners like

"preBuildCommands": [
    "rdmd --eval=\"writeln(`Started $BUILD_TYPE build at `, Clock.currTime, ` for $PACKAGE_DIR on $PLATFORM`)\""
]

I have tried hijacking Dub. Although interestingly possible:

// build.d in the package root dir, to be called like
// dub build.d <command>
#!/usr/bin/env dub
/+ dub.json: {
    "name": "build",
    "dependencies": { "dub": "~>1.19.0" }
}+/
import dub.commandline;
int main(string[] args)
{
    return runDubCommandLine(args); // Or do something special instead!
}

... this is unlikely to be a good idea because

  • Special actions don't turn up in dub describe, like preGenerateCommands do.
  • Special actions may not be forward compatible with future dub versions.
  • Can break building package dependencies.
  • Breaking into the code flow of dub is not easy at all.
  • Actually invoking the hijacked version when the package is compiled as a dependency is an unsolved problem.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant