Skip to content

Commit

Permalink
Build the build hook (remote building) for Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Ericson2314 committed Jun 26, 2024
1 parent 5e4e334 commit 5c0e4e0
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 48 deletions.
2 changes: 1 addition & 1 deletion maintainers/flake-module.nix
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@
''^src/libstore/build/entry-points\.cc$''
''^src/libstore/build/goal\.cc$''
''^src/libstore/build/goal\.hh$''
''^src/libstore/unix/build/hook-instance\.cc$''
''^src/libstore/build/hook-instance\.cc$''
''^src/libstore/unix/build/local-derivation-goal\.cc$''
''^src/libstore/unix/build/local-derivation-goal\.hh$''
''^src/libstore/build/substitution-goal\.cc$''
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ void commonChildInit()
{
logger = makeSimpleLogger();

#ifndef _WIN32
const static std::string pathNullDevice = "/dev/null";
restoreProcessContext(false);

Expand All @@ -32,6 +33,7 @@ void commonChildInit()
if (dup2(fdDevNull, STDIN_FILENO) == -1)
throw SysError("cannot dup null device into stdin");
close(fdDevNull);
#endif
}

}
File renamed without changes.
43 changes: 12 additions & 31 deletions src/libstore/build/derivation-goal.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include "derivation-goal.hh"
#ifndef _WIN32 // TODO enable build hook on Windows
# include "hook-instance.hh"
#endif
#include "hook-instance.hh"
#include "processes.hh"
#include "config-global.hh"
#include "worker.hh"
Expand Down Expand Up @@ -98,9 +96,7 @@ std::string DerivationGoal::key()

void DerivationGoal::killChild()
{
#ifndef _WIN32 // TODO enable build hook on Windows
hook.reset();
#endif
}


Expand Down Expand Up @@ -640,17 +636,9 @@ void DerivationGoal::started()
buildMode == bmCheck ? "checking outputs of '%s'" :
"building '%s'", worker.store.printStorePath(drvPath));
fmt("building '%s'", worker.store.printStorePath(drvPath));
#ifndef _WIN32 // TODO enable build hook on Windows
if (hook) msg += fmt(" on '%s'", machineName);
#endif
act = std::make_unique<Activity>(*logger, lvlInfo, actBuild, msg,
Logger::Fields{worker.store.printStorePath(drvPath),
#ifndef _WIN32 // TODO enable build hook on Windows
hook ? machineName :
#endif
"",
1,
1});
Logger::Fields{worker.store.printStorePath(drvPath), hook ? machineName : "", 1, 1});
mcRunningBuilds = std::make_unique<MaintainCount<uint64_t>>(worker.runningBuilds);
worker.updateProgress();
}
Expand Down Expand Up @@ -831,20 +819,14 @@ void replaceValidPath(const Path & storePath, const Path & tmpPath)

int DerivationGoal::getChildStatus()
{
#ifndef _WIN32 // TODO enable build hook on Windows
return hook->pid.kill();
#else
return 0;
#endif
}


void DerivationGoal::closeReadPipes()
{
#ifndef _WIN32 // TODO enable build hook on Windows
hook->builderOut.readSide.close();
hook->fromHook.readSide.close();
#endif
}


Expand Down Expand Up @@ -1134,13 +1116,14 @@ void DerivationGoal::resolvedFinished()

HookReply DerivationGoal::tryBuildHook()
{
#ifdef _WIN32 // TODO enable build hook on Windows
return rpDecline;
#else
if (settings.buildHook.get().empty() || !worker.tryBuildHook || !useDerivation) return rpDecline;

if (!worker.hook)
worker.hook = std::make_unique<HookInstance>();
worker.hook = std::make_unique<HookInstance>(
#ifdef _WIN32
worker.ioport.get()
#endif
);

try {

Expand Down Expand Up @@ -1236,12 +1219,16 @@ HookReply DerivationGoal::tryBuildHook()
Path logFile = openLogFile();

std::set<MuxablePipePollState::CommChannel> fds;
#ifndef _WIN32
fds.insert(hook->fromHook.readSide.get());
fds.insert(hook->builderOut.readSide.get());
#else
fds.insert(&hook->fromHook);
fds.insert(&hook->builderOut);
#endif
worker.childStarted(shared_from_this(), fds, false, false);

return rpAccept;
#endif
}


Expand Down Expand Up @@ -1307,11 +1294,7 @@ void DerivationGoal::closeLogFile()

bool DerivationGoal::isReadDesc(Descriptor fd)
{
#ifdef _WIN32 // TODO enable build hook on Windows
return false;
#else
return fd == hook->builderOut.readSide.get();
#endif
}

void DerivationGoal::handleChildOutput(Descriptor fd, std::string_view data)
Expand Down Expand Up @@ -1344,7 +1327,6 @@ void DerivationGoal::handleChildOutput(Descriptor fd, std::string_view data)
if (logSink) (*logSink)(data);
}

#ifndef _WIN32 // TODO enable build hook on Windows
if (hook && fd == hook->fromHook.readSide.get()) {
for (auto c : data)
if (c == '\n') {
Expand Down Expand Up @@ -1379,7 +1361,6 @@ void DerivationGoal::handleChildOutput(Descriptor fd, std::string_view data)
} else
currentHookLine += c;
}
#endif
}


Expand Down
4 changes: 0 additions & 4 deletions src/libstore/build/derivation-goal.hh
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ namespace nix {

using std::map;

#ifndef _WIN32 // TODO enable build hook on Windows
struct HookInstance;
#endif

typedef enum {rpAccept, rpDecline, rpPostpone} HookReply;

Expand Down Expand Up @@ -182,12 +180,10 @@ struct DerivationGoal : public Goal

std::string currentHookLine;

#ifndef _WIN32 // TODO enable build hook on Windows
/**
* The build hook.
*/
std::unique_ptr<HookInstance> hook;
#endif

/**
* The sort of derivation we are building.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@

namespace nix {

HookInstance::HookInstance()
HookInstance::HookInstance(
#ifdef _WIN32
HANDLE ioport
#endif
)
{
debug("starting build hook '%s'", concatStringsSep(" ", settings.buildHook.get()));

Expand All @@ -27,14 +31,23 @@ HookInstance::HookInstance()
args.push_back(std::to_string(verbosity));

/* Create a pipe to get the output of the child. */
#ifndef _WIN32
fromHook.create();
#else
fromHook.createAsyncPipe(ioport);
#endif

/* Create the communication pipes. */
toHook.create();

/* Create a pipe to get the output of the builder. */
#ifndef _WIN32
builderOut.create();
#else
builderOut.createAsyncPipe(ioport);
#endif

#ifndef _WIN32
/* Fork the hook. */
pid = startProcess([&]() {

Expand Down Expand Up @@ -64,8 +77,11 @@ HookInstance::HookInstance()
});

pid.setSeparatePG(true);
fromHook.writeSide = -1;
toHook.readSide = -1;
#else
throw UnimplementedError("build remote hook not yet implemented on Windows.");
#endif
fromHook.writeSide.close();
toHook.readSide.close();

sink = FdSink(toHook.writeSide.get());
std::map<std::string, Config::SettingInfo> settings;
Expand All @@ -79,7 +95,7 @@ HookInstance::HookInstance()
HookInstance::~HookInstance()
{
try {
toHook.writeSide = -1;
toHook.writeSide.close();
if (pid != -1) pid.kill();
} catch (...) {
ignoreException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
#include "serialise.hh"
#include "processes.hh"

#ifdef _WIN32
# include "windows-async-pipe.hh"
#endif

namespace nix {

struct HookInstance
Expand All @@ -17,12 +21,22 @@ struct HookInstance
/**
* Pipe for the hook's standard output/error.
*/
Pipe fromHook;
#ifndef _WIN32
Pipe
#else
windows::AsyncPipe
#endif
fromHook;

/**
* Pipe for the builder's standard output/error.
*/
Pipe builderOut;
#ifndef _WIN32
Pipe
#else
windows::AsyncPipe
#endif
builderOut;

/**
* The process ID of the hook.
Expand All @@ -33,7 +47,11 @@ struct HookInstance

std::map<ActivityId, Activity> activities;

HookInstance();
HookInstance(
#ifdef _WIN32
HANDLE ioport
#endif
);

~HookInstance();
};
Expand Down
2 changes: 1 addition & 1 deletion src/libstore/build/worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
#include "derivation-goal.hh"
#ifndef _WIN32 // TODO Enable building on Windows
# include "local-derivation-goal.hh"
# include "hook-instance.hh"
#endif
#include "hook-instance.hh"
#include "signals.hh"

namespace nix {
Expand Down
4 changes: 0 additions & 4 deletions src/libstore/build/worker.hh
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,8 @@ struct Child
steady_time_point timeStarted;
};

#ifndef _WIN32 // TODO Enable building on Windows
/* Forward definition. */
struct HookInstance;
#endif

/**
* Coordinates one or more realisations and their interdependencies.
Expand Down Expand Up @@ -161,9 +159,7 @@ public:
Store & store;
Store & evalStore;

#ifndef _WIN32 // TODO Enable building on Windows
std::unique_ptr<HookInstance> hook;
#endif

uint64_t expectedBuilds = 0;
uint64_t doneBuilds = 0;
Expand Down

0 comments on commit 5c0e4e0

Please sign in to comment.