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

Add enough ifndefs to build nix{-build,-instantiate}.exe #30

Merged
merged 3 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/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
12 changes: 12 additions & 0 deletions src/libcmd/repl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,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 @@ -345,8 +347,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 All @@ -359,6 +363,7 @@ ReplExitStatus NixRepl::mainLoop()

bool NixRepl::getLine(std::string & input, const std::string & prompt)
{
#ifndef __WIN32
struct sigaction act, old;
sigset_t savedSignalMask, set;

Expand All @@ -383,10 +388,13 @@ bool NixRepl::getLine(std::string & input, const std::string & prompt)
};

setupSignals();
#endif
Finally resetTerminal([&]() { rl_deprep_terminal(); });
char * s = readline(prompt.c_str());
Finally doFree([&]() { free(s); });
#ifndef __WIN32
restoreSignals();
#endif

if (g_signal_received) {
g_signal_received = 0;
Expand Down Expand Up @@ -520,7 +528,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 @@ -642,6 +652,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 @@ -676,6 +687,7 @@ ProcessLineResult NixRepl::processLine(std::string line)
state->resetFileCache();
reloadFiles();
}
#endif

else if (command == ":t") {
Value v;
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
17 changes: 17 additions & 0 deletions 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 Down Expand Up @@ -362,6 +375,7 @@ RunPager::RunPager()

stopProgressBar();

#ifndef __WIN32
Pipe toPager;
toPager.create();

Expand All @@ -383,11 +397,13 @@ RunPager::RunPager()
std_out = fcntl(STDOUT_FILENO, F_DUPFD_CLOEXEC, 0);
if (dup2(toPager.writeSide.get(), STDOUT_FILENO) == -1)
throw SysError("dupping standard output");
#endif
}


RunPager::~RunPager()
{
#ifndef __WIN32
try {
if (pid != -1) {
std::cout.flush();
Expand All @@ -397,6 +413,7 @@ RunPager::~RunPager()
} catch (...) {
ignoreException();
}
#endif
}


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 @@ -89,8 +91,10 @@ public:
~RunPager();

private:
#ifndef __WIN32
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

}
2 changes: 2 additions & 0 deletions src/libmain/stack.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,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 +74,6 @@ void defaultStackOverflowHandler(siginfo_t * info, void * ctx) {
[[gnu::unused]] auto res = write(2, msg, strlen(msg));
_exit(1); // maybe abort instead?
}
#endif

}
13 changes: 11 additions & 2 deletions src/libutil/file-descriptor.hh
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#include "error.hh"

#ifdef _WIN32
# include <handleapi.h>
# include <io.h>
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
#endif

namespace nix {
Expand Down Expand Up @@ -97,6 +97,15 @@ void drainFD(
#endif
);

[[gnu::always_inline]]
inline Descriptor getStandardOut() {
#ifndef __WIN32
return STDOUT_FILENO;
#else
return GetStdHandle(STD_OUTPUT_HANDLE);
#endif
}

/**
* Automatic cleanup of resources.
*/
Expand Down
14 changes: 1 addition & 13 deletions src/libutil/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@
#include <nlohmann/json.hpp>
#include <iostream>

#ifdef _WIN32
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
# include <processthreadsapi.h>
#endif

namespace nix {

LoggerSettings loggerSettings;
Expand All @@ -43,13 +37,7 @@ void Logger::warn(const std::string & msg)

void Logger::writeToStdout(std::string_view s)
{
Descriptor standard_out =
#ifdef _WIN32
GetStdHandle(STD_OUTPUT_HANDLE)
#else
STDOUT_FILENO
#endif
;
Descriptor standard_out = getStandardOut();
writeFull(standard_out, s);
writeFull(standard_out, "\n");
}
Expand Down
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
Loading
Loading