Skip to content

Commit

Permalink
Build libnixutil with MinGW
Browse files Browse the repository at this point in the history
Co-Authored-By volth <[email protected]>
  • Loading branch information
Ericson2314 committed Oct 22, 2023
1 parent 937cd65 commit 1aaf36b
Show file tree
Hide file tree
Showing 49 changed files with 1,860 additions and 758 deletions.
15 changes: 13 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
-include Makefile.config
clean-files += Makefile.config

include mk/platform.mk

ifeq ($(ENABLE_BUILD), yes)
makefiles = \
mk/precompiled-headers.mk \
local.mk \
src/libutil/local.mk \
src/libutil/local.mk

ifdef HOST_UNIX
makefiles += \
src/libstore/local.mk \
src/libfetchers/local.mk \
src/libmain/local.mk \
Expand All @@ -23,22 +28,28 @@ makefiles = \
doc/manual/local.mk \
doc/internal-api/local.mk
endif
endif

ifeq ($(ENABLE_BUILD)_$(ENABLE_TESTS), yes_yes)
UNIT_TEST_ENV = _NIX_TEST_UNIT_DATA=unit-test-data
makefiles += \
src/libutil/tests/local.mk \
src/libutil/tests/local.mk
ifdef HOST_UNIX
makefiles += \
src/libstore/tests/local.mk \
src/libexpr/tests/local.mk
endif
endif

ifeq ($(ENABLE_TESTS), yes)
ifdef HOST_UNIX
makefiles += \
tests/functional/local.mk \
tests/functional/ca/local.mk \
tests/functional/dyn-drv/local.mk \
tests/functional/test-libstoreconsumer/local.mk \
tests/functional/plugins/local.mk
endif
else
makefiles += \
mk/disable-tests.mk
Expand Down
33 changes: 1 addition & 32 deletions mk/lib.mk
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,7 @@ man-pages :=
install-tests :=
install-tests-groups :=

ifdef HOST_OS
HOST_KERNEL = $(firstword $(subst -, ,$(HOST_OS)))
ifeq ($(patsubst mingw%,,$(HOST_KERNEL)),)
HOST_MINGW = 1
HOST_WINDOWS = 1
endif
ifeq ($(HOST_KERNEL), cygwin)
HOST_CYGWIN = 1
HOST_WINDOWS = 1
HOST_UNIX = 1
endif
ifeq ($(patsubst darwin%,,$(HOST_KERNEL)),)
HOST_DARWIN = 1
HOST_UNIX = 1
endif
ifeq ($(patsubst freebsd%,,$(HOST_KERNEL)),)
HOST_FREEBSD = 1
HOST_UNIX = 1
endif
ifeq ($(patsubst netbsd%,,$(HOST_KERNEL)),)
HOST_NETBSD = 1
HOST_UNIX = 1
endif
ifeq ($(HOST_KERNEL), linux)
HOST_LINUX = 1
HOST_UNIX = 1
endif
ifeq ($(patsubst solaris%,,$(HOST_KERNEL)),)
HOST_SOLARIS = 1
HOST_UNIX = 1
endif
endif
include mk/platform.mk

# Hack to define a literal space.
space :=
Expand Down
32 changes: 32 additions & 0 deletions mk/platform.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
ifdef HOST_OS
HOST_KERNEL = $(firstword $(subst -, ,$(HOST_OS)))
ifeq ($(patsubst mingw%,,$(HOST_KERNEL)),)
HOST_MINGW = 1
HOST_WINDOWS = 1
endif
ifeq ($(HOST_KERNEL), cygwin)
HOST_CYGWIN = 1
HOST_WINDOWS = 1
HOST_UNIX = 1
endif
ifeq ($(patsubst darwin%,,$(HOST_KERNEL)),)
HOST_DARWIN = 1
HOST_UNIX = 1
endif
ifeq ($(patsubst freebsd%,,$(HOST_KERNEL)),)
HOST_FREEBSD = 1
HOST_UNIX = 1
endif
ifeq ($(patsubst netbsd%,,$(HOST_KERNEL)),)
HOST_NETBSD = 1
HOST_UNIX = 1
endif
ifeq ($(HOST_KERNEL), linux)
HOST_LINUX = 1
HOST_UNIX = 1
endif
ifeq ($(patsubst solaris%,,$(HOST_KERNEL)),)
HOST_SOLARIS = 1
HOST_UNIX = 1
endif
endif
10 changes: 8 additions & 2 deletions src/libutil/ambient-authority.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "ambient-authority.hh"
#include "namespaces.hh"
#include "util.hh"
#include "finally.hh"
#include "file-system.hh"
Expand All @@ -14,9 +13,12 @@
# include <mutex>
# include <sys/resource.h>
# include "cgroup.hh"
# include "namespaces.hh"
#endif

#include <sys/mount.h>
#ifndef _WIN32
# include <sys/mount.h>
#endif

namespace nix {

Expand Down Expand Up @@ -70,9 +72,13 @@ void setStackSize(size_t stackSize)

void restoreProcessContext(bool restoreMounts)
{
#ifndef _WIN32
restoreSignals();
#endif
if (restoreMounts) {
#if __linux__
restoreMountNamespace();
#endif
}

#if __linux__
Expand Down
20 changes: 18 additions & 2 deletions src/libutil/archive.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
#include "file-system.hh"
#include "signals.hh"

#if _WIN32
# include <fileapi.h>
# include "windows-file-path.hh"
# include "windows-error.hh"
#endif

namespace nix {

struct ArchiveSettings : Config
Expand All @@ -42,8 +48,14 @@ static void dumpContents(const Path & path, off_t size,
{
sink << "contents" << size;

AutoCloseFD fd = open(path.c_str(), O_RDONLY | O_CLOEXEC);
if (!fd) throw SysError("opening file '%1%'", path);
AutoCloseFD fd =
#ifdef _WIN32
CreateFileW(pathW(path).c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)
#else
open(path.c_str(), O_RDONLY | O_CLOEXEC)
#endif
;
if (!fd) throw NativeSysError("opening file '%1%'", path);

std::vector<char> buf(65536);
size_t left = size;
Expand Down Expand Up @@ -107,8 +119,10 @@ static time_t dump(const Path & path, Sink & sink, PathFilter & filter)
}
}

#ifndef _WIN32
else if (S_ISLNK(st.st_mode))
sink << "type" << "symlink" << "target" << readLink(path);
#endif

else throw Error("file '%1%' has an unsupported type", path);

Expand Down Expand Up @@ -232,11 +246,13 @@ static void parse(ParseSink & sink, Source & source, const Path & path)
sink.closeRegularFile();
}

#ifndef _WIN32
else if (s == "executable" && type == tpRegular) {
auto s = readString(source);
if (s != "") throw badArchive("executable marker has non-empty value");
sink.isExecutable();
}
#endif

else if (s == "entry" && type == tpDirectory) {
std::string name, prevName;
Expand Down
6 changes: 5 additions & 1 deletion src/libutil/args.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
#include "users.hh"
#include "json-utils.hh"

#include <glob.h>
#ifndef _WIN32
# include <glob.h>
#endif

namespace nix {

Expand Down Expand Up @@ -309,6 +311,7 @@ Args::Flag Args::Flag::mkHashTypeOptFlag(std::string && longName, std::optional<

static void _completePath(std::string_view prefix, bool onlyDirs)
{
#ifndef _WIN32
completionType = ctFilenames;
glob_t globbuf;
int flags = GLOB_NOESCAPE;
Expand All @@ -327,6 +330,7 @@ static void _completePath(std::string_view prefix, bool onlyDirs)
}
}
globfree(&globbuf);
#endif
}

void completePath(size_t, std::string_view prefix)
Expand Down
14 changes: 0 additions & 14 deletions src/libutil/environment-variables.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,4 @@ std::map<std::string, std::string> getEnv()
return env;
}


void clearEnv()
{
for (auto & name : getEnv())
unsetenv(name.first.c_str());
}

void replaceEnv(const std::map<std::string, std::string> & newEnv)
{
clearEnv();
for (auto & newEnvVar : newEnv)
setenv(newEnvVar.first.c_str(), newEnvVar.second.c_str(), 1);
}

}
2 changes: 1 addition & 1 deletion src/libutil/error.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void BaseError::addTrace(std::shared_ptr<AbstractPos> && e, hintformat hint, boo

void throwExceptionSelfCheck(){
// This is meant to be caught in initLibUtil()
throw SysError("C++ exception handling is broken. This would appear to be a problem with the way Nix was compiled and/or linked and/or loaded.");
throw Error("C++ exception handling is broken. This would appear to be a problem with the way Nix was compiled and/or linked and/or loaded.");
}

// c++ std::exception descendants must have a 'const char* what()' function.
Expand Down
51 changes: 45 additions & 6 deletions src/libutil/error.hh
Original file line number Diff line number Diff line change
Expand Up @@ -200,27 +200,66 @@ MakeError(Error, BaseError);
MakeError(UsageError, Error);
MakeError(UnimplementedError, Error);

/**
* To use in catch-blocks.
*/
class SysError : public Error
{
public:
int errNo;
/**
* Has to be big enough for all platforms:
*
* - Unix: `int` (perhaps `int32_t`)
* - Windows: `DWORD` (which is `uint32_t`)
*
* The smallest type which contains all valeus of both is `int64_t`.
*/
int64_t errNo;

template<typename... Args>
SysError(int errNo_, const Args & ... args)
: Error("")
SysError(int64_t errNo, const Args & ... args)
: Error(args...), errNo(errNo)
{ }
};

/**
* To throw. Don't catch this in portable code! Catch `SysError`
* instead.
*/
class PosixError : public SysError
{
public:
template<typename... Args>
PosixError(int errNo, const Args & ... args)
: SysError(errNo, "")
{
errNo = errNo_;
auto hf = hintfmt(args...);
err.msg = hintfmt("%1%: %2%", normaltxt(hf.str()), strerror(errNo));
}

template<typename... Args>
SysError(const Args & ... args)
: SysError(errno, args ...)
PosixError(const Args & ... args)
: PosixError(errno, args ...)
{
}
};

#ifdef _WIN32
class WinError;
#endif

/**
* Convenience alias for when we use a `errno`-based error handling
* function on Unix, and a Win32 on on Windows.
*/
typedef
#ifdef _WIN32
WinError
#else
PosixError
#endif
NativeSysError;

/** Throw an exception for the purpose of checking that exception handling works; see 'initLibUtil()'.
*/
void throwExceptionSelfCheck();
Expand Down
Loading

0 comments on commit 1aaf36b

Please sign in to comment.