Skip to content

Commit

Permalink
Add enough ifndefs to build nix{-build,-instantiate}.exe
Browse files Browse the repository at this point in the history
  • Loading branch information
puffnfresh authored and Ericson2314 committed Mar 22, 2024
1 parent b1fd529 commit 93ec39a
Show file tree
Hide file tree
Showing 31 changed files with 222 additions and 21 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ ifeq ($(ENABLE_BUILD), yes)
makefiles = \
mk/precompiled-headers.mk \
local.mk \
src/libmain/local.mk \
src/libcmd/local.mk \
src/nix/local.mk \
src/libutil/local.mk \
src/libstore/local.mk \
src/libfetchers/local.mk \
src/libexpr/local.mk

ifdef HOST_UNIX
makefiles += \
src/libmain/local.mk \
src/libcmd/local.mk \
src/nix/local.mk \
src/resolve-system-dependencies/local.mk \
scripts/local.mk \
misc/bash/local.mk \
Expand Down
6 changes: 6 additions & 0 deletions src/libcmd/command.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
#include "local-fs-store.hh"
#include "derivations.hh"
#include "nixexpr.hh"
#ifndef __WIN32
#include "profiles.hh"
#endif
#include "repl.hh"

#include <nlohmann/json.hpp>
Expand Down Expand Up @@ -235,6 +237,7 @@ void StorePathCommand::run(ref<Store> store, StorePaths && storePaths)
run(store, *storePaths.begin());
}

#ifndef __WIN32
MixProfile::MixProfile()
{
addFlag({
Expand Down Expand Up @@ -285,6 +288,7 @@ MixDefaultProfile::MixDefaultProfile()
{
profile = getDefaultProfile();
}
#endif

MixEnvironment::MixEnvironment() : ignoreEnvironment(false)
{
Expand Down Expand Up @@ -328,8 +332,10 @@ void MixEnvironment::setEnviron() {
if (!keep.empty())
throw UsageError("--keep does not make sense without --ignore-environment");

#ifndef __WIN32
for (const auto & var : unset)
unsetenv(var.c_str());
#endif
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/libcmd/command.hh
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ static RegisterCommand registerCommand2(std::vector<std::string> && name)
return RegisterCommand(std::move(name), [](){ return make_ref<T>(); });
}

#ifndef __WIN32
struct MixProfile : virtual StoreCommand
{
std::optional<Path> profile;
Expand All @@ -312,6 +313,7 @@ struct MixDefaultProfile : MixProfile
{
MixDefaultProfile();
};
#endif

struct MixEnvironment : virtual Args {

Expand Down
2 changes: 1 addition & 1 deletion src/libcmd/common-eval-args.cc
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ Bindings * MixEvalArgs::getAutoArgs(EvalState & state)
v->mkString(arg.s);
},
[&](const AutoArgFile & arg) {
v->mkString(readFile(arg.path));
v->mkString(readFile(arg.path.string()));
},
[&](const AutoArgStdin & arg) {
v->mkString(readFile(STDIN_FILENO));
Expand Down
2 changes: 1 addition & 1 deletion src/libcmd/markdown.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#include "finally.hh"
#include "terminal.hh"

#include <sys/queue.h>
#if HAVE_LOWDOWN
#include <sys/queue.h>
#include <lowdown.h>
#endif

Expand Down
4 changes: 4 additions & 0 deletions src/libcmd/repl-interacter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ static constexpr const char * promptForType(ReplPromptType promptType)

bool ReadlineLikeInteracter::getLine(std::string & input, ReplPromptType promptType)
{
#ifndef __WIN32
struct sigaction act, old;
sigset_t savedSignalMask, set;

Expand All @@ -161,9 +162,12 @@ bool ReadlineLikeInteracter::getLine(std::string & input, ReplPromptType promptT
};

setupSignals();
#endif
char * s = readline(promptForType(promptType));
Finally doFree([&]() { free(s); });
#ifndef __WIN32
restoreSignals();
#endif

if (g_signal_received) {
g_signal_received = 0;
Expand Down
10 changes: 10 additions & 0 deletions src/libcmd/repl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,14 @@ void runNix(Path program, const Strings & args,
auto subprocessEnv = getEnv();
subprocessEnv["NIX_CONFIG"] = globalConfig.toKeyValue();

#ifndef __WIN32
runProgram2(RunOptions {
.program = settings.nixBinDir+ "/" + program,
.args = args,
.environment = subprocessEnv,
.input = input,
});
#endif

return;
}
Expand Down Expand Up @@ -228,8 +230,10 @@ ReplExitStatus NixRepl::mainLoop()
printMsg(lvlError, e.msg());
} catch (Error & e) {
printMsg(lvlError, e.msg());
#ifndef __WIN32
} catch (Interrupted & e) {
printMsg(lvlError, e.msg());
#endif
}

// We handled the current input fully, so we should clear it
Expand Down Expand Up @@ -357,7 +361,9 @@ ProcessLineResult NixRepl::processLine(std::string line)
if (line.empty())
return ProcessLineResult::PromptAgain;

#ifndef __WIN32
_isInterrupted = false;
#endif

std::string command, arg;

Expand Down Expand Up @@ -480,6 +486,7 @@ ProcessLineResult NixRepl::processLine(std::string line)
reloadFiles();
}

#ifndef __WIN32
else if (command == ":e" || command == ":edit") {
Value v;
evalString(arg, v);
Expand Down Expand Up @@ -514,6 +521,7 @@ ProcessLineResult NixRepl::processLine(std::string line)
state->resetFileCache();
reloadFiles();
}
#endif

else if (command == ":t") {
Value v;
Expand Down Expand Up @@ -568,7 +576,9 @@ ProcessLineResult NixRepl::processLine(std::string line)
subs.push_front(state->store);

bool foundLog = false;
#ifndef __WIN32
RunPager pager;
#endif
for (auto & sub : subs) {
auto * logSubP = dynamic_cast<LogStore *>(&*sub);
if (!logSubP) {
Expand Down
4 changes: 4 additions & 0 deletions src/libmain/progress-bar.cc
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,11 @@ class ProgressBar : public Logger
}
}

#ifndef __WIN32
auto width = getWindowSize().second;
#else
auto width = 0;
#endif
if (width <= 0) width = std::numeric_limits<decltype(width)>::max();

writeToStderr("\r" + filterANSIEscapes(line, false, width) + ANSI_NORMAL + "\e[K");
Expand Down
16 changes: 15 additions & 1 deletion src/libmain/shared.cc
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ std::string getArg(const std::string & opt,
return *i;
}

#ifndef __WIN32
static void sigHandler(int signo) { }
#endif


void initNix()
Expand All @@ -121,6 +123,7 @@ void initNix()

initLibStore();

#ifndef __WIN32
startSignalHandlerThread();

/* Reset SIGCHLD to its default. */
Expand All @@ -135,6 +138,7 @@ void initNix()
/* Install a dummy SIGUSR1 handler for use with pthread_kill(). */
act.sa_handler = sigHandler;
if (sigaction(SIGUSR1, &act, 0)) throw SysError("handling SIGUSR1");
#endif

#if __APPLE__
/* HACK: on darwin, we need can’t use sigprocmask with SIGWINCH.
Expand All @@ -156,21 +160,26 @@ void initNix()
if (sigaction(SIGTRAP, &act, 0)) throw SysError("handling SIGTRAP");
#endif

#ifndef __WIN32
/* Register a SIGSEGV handler to detect stack overflows.
Why not initLibExpr()? initGC() is essentially that, but
detectStackOverflow is not an instance of the init function concept, as
it may have to be invoked more than once per process. */
detectStackOverflow();
#endif

/* There is no privacy in the Nix system ;-) At least not for
now. In particular, store objects should be readable by
everybody. */
umask(0022);

#ifndef __WIN32
/* Initialise the PRNG. */
struct timeval tv;
gettimeofday(&tv, 0);
srandom(tv.tv_usec);
#endif


}

Expand Down Expand Up @@ -308,7 +317,9 @@ void printVersion(const std::string & programName)
void showManPage(const std::string & name)
{
restoreProcessContext();
#ifndef __WIN32
setenv("MANPATH", settings.nixManDir.c_str(), 1);
#endif
execlp("man", "man", name.c_str(), nullptr);
throw SysError("command 'man %1%' failed", name.c_str());
}
Expand All @@ -329,7 +340,9 @@ int handleExceptions(const std::string & programName, std::function<void()> fun)
condition is discharged before we reach printMsg()
below, since otherwise it will throw an (uncaught)
exception. */
#ifndef __WIN32
setInterruptThrown();
#endif
throw;
}
} catch (Exit & e) {
Expand All @@ -352,7 +365,7 @@ int handleExceptions(const std::string & programName, std::function<void()> fun)
return 0;
}


#ifndef __WIN32
RunPager::RunPager()
{
if (!isatty(STDOUT_FILENO)) return;
Expand Down Expand Up @@ -398,6 +411,7 @@ RunPager::~RunPager()
ignoreException();
}
}
#endif


PrintFreed::~PrintFreed()
Expand Down
6 changes: 6 additions & 0 deletions src/libmain/shared.hh
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#pragma once
///@file

#ifndef __WIN32
#include "processes.hh"
#endif
#include "args.hh"
#include "args/root.hh"
#include "common-args.hh"
Expand Down Expand Up @@ -82,6 +84,7 @@ void showManPage(const std::string & name);
* terminal and $PAGER is set. Standard output is redirected to the
* pager.
*/
#ifndef __WIN32
class RunPager
{
public:
Expand All @@ -92,6 +95,7 @@ private:
Pid pid;
int std_out;
};
#endif

extern volatile ::sig_atomic_t blockInt;

Expand All @@ -112,6 +116,7 @@ struct PrintFreed
};


#ifndef __WIN32
/**
* Install a SIGSEGV handler to detect stack overflows.
*/
Expand Down Expand Up @@ -141,5 +146,6 @@ extern std::function<void(siginfo_t * info, void * ctx)> stackOverflowHandler;
* logger. Exits the process immediately after.
*/
void defaultStackOverflowHandler(siginfo_t * info, void * ctx);
#endif

}
3 changes: 2 additions & 1 deletion src/libmain/stack.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace nix {


#ifndef __WIN32
static void sigsegvHandler(int signo, siginfo_t * info, void * ctx)
{
/* Detect stack overflows by comparing the faulting address with
Expand Down Expand Up @@ -73,5 +73,6 @@ void defaultStackOverflowHandler(siginfo_t * info, void * ctx) {
[[gnu::unused]] auto res = write(2, msg, strlen(msg));
_exit(1); // maybe abort instead?
}
#endif

}
4 changes: 4 additions & 0 deletions src/nix-build/nix-build.cc
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,10 @@ static void main_nix_build(int argc, char * * argv)
left = {"default.nix"};
}

#ifndef __WIN32
if (runEnv)
setenv("IN_NIX_SHELL", pure ? "pure" : "impure", 1);
#endif

PackageInfos drvs;

Expand Down Expand Up @@ -571,8 +573,10 @@ static void main_nix_build(int argc, char * * argv)
"BASH=%5%; "
"set +e; "
R"s([ -n "$PS1" -a -z "$NIX_SHELL_PRESERVE_PROMPT" ] && )s" +
#ifndef __WIN32
(getuid() == 0 ? R"s(PS1='\n\[\033[1;31m\][nix-shell:\w]\$\[\033[0m\] '; )s"
: R"s(PS1='\n\[\033[1;32m\][nix-shell:\w]\$\[\033[0m\] '; )s") +
#endif
"if [ \"$(type -t runHook)\" = function ]; then runHook shellHook; fi; "
"unset NIX_ENFORCE_PURITY; "
"shopt -u nullglob; "
Expand Down
2 changes: 2 additions & 0 deletions src/nix-channel/nix-channel.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#ifndef __WIN32
#include "profiles.hh"
#endif
#include "shared.hh"
#include "globals.hh"
#include "filetransfer.hh"
Expand Down
2 changes: 1 addition & 1 deletion src/nix-instantiate/nix-instantiate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ static int main_nix_instantiate(int argc, char * * argv)
for (auto & i : files) {
auto p = state->findFile(i);
if (auto fn = p.getPhysicalPath())
std::cout << fn->native() << std::endl;
std::cout << fn->string() << std::endl;
else
throw Error("'%s' has no physical path", p);
}
Expand Down
7 changes: 6 additions & 1 deletion src/nix/build.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ static void createOutLinks(const Path& outLink, const std::vector<BuiltPathWithR
}
}

struct CmdBuild : InstallablesCommand, MixDryRun, MixJSON, MixProfile
struct CmdBuild : InstallablesCommand, MixDryRun, MixJSON
#ifndef __WIN32
, MixProfile
#endif
{
Path outLink = "result";
bool printOutputPaths = false;
Expand Down Expand Up @@ -158,10 +161,12 @@ struct CmdBuild : InstallablesCommand, MixDryRun, MixJSON, MixProfile
}
}

#ifndef __WIN32
BuiltPaths buildables2;
for (auto & b : buildables)
buildables2.push_back(b.path);
updateProfile(buildables2);
#endif
}
};

Expand Down
Loading

0 comments on commit 93ec39a

Please sign in to comment.