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 21, 2023
1 parent db6d6f1 commit e17e095
Show file tree
Hide file tree
Showing 38 changed files with 810 additions and 344 deletions.
12 changes: 11 additions & 1 deletion 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,6 +28,7 @@ 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
Expand All @@ -33,12 +39,16 @@ makefiles += \
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
else
$(error Functional tests currently only run on Unix. Please disable tests)
endif
else
makefiles += \
mk/disable-tests.mk
Expand Down
17 changes: 14 additions & 3 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,20 @@ AC_ARG_ENABLE(build, AS_HELP_STRING([--disable-build],[Do not build nix]),
AC_SUBST(ENABLE_BUILD)
# Building without tests is useful for bootstrapping with a smaller footprint
# or running the tests in a separate derivation. Otherwise, we do compile and
# run them.
AC_ARG_ENABLE(tests, AS_HELP_STRING([--disable-tests],[Do not build the tests]),
ENABLE_TESTS=$enableval, ENABLE_TESTS=yes)
# run them if we can. On Windows we cannot.
AC_ARG_ENABLE(tests,
AS_HELP_STRING([--disable-tests],[Do not build the tests]),
[ENABLE_TESTS=$enableval],
[
case "$host_os" in
mingw*)
ENABLE_TESTS=no
;;
*)
ENABLE_TESTS=yes
;;
esac
])
AC_SUBST(ENABLE_TESTS)

# Building without API docs is the default as Nix' C++ interfaces are internal and unstable.
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
2 changes: 1 addition & 1 deletion 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,6 +13,7 @@
# include <mutex>
# include <sys/resource.h>
# include "cgroup.hh"
# include "namespaces.hh"
#endif

#include <sys/mount.h>
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);
}

}
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 e17e095

Please sign in to comment.