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 Oct 11, 2024
1 parent dbcd4cd commit e212f87
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 @@ -229,7 +229,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 @@ -106,9 +104,7 @@ std::string DerivationGoal::key()

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


Expand Down Expand Up @@ -632,17 +628,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 @@ -825,20 +813,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 @@ -1127,13 +1109,14 @@ Goal::Co 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 @@ -1229,12 +1212,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 @@ -1300,11 +1287,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 @@ -1339,7 +1322,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 @@ -1374,7 +1356,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 @@ -8,7 +8,11 @@

namespace nix {

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

Expand Down Expand Up @@ -36,14 +40,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 @@ -73,8 +86,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 @@ -88,7 +104,7 @@ HookInstance::HookInstance()
HookInstance::~HookInstance()
{
try {
toHook.writeSide = -1;
toHook.writeSide.close();
if (pid != -1) pid.kill();
} catch (...) {
ignoreExceptionInDestructor();
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 e212f87

Please sign in to comment.