Skip to content

Commit

Permalink
Get stripped down libnixstore and libnixfetchers building on Windows
Browse files Browse the repository at this point in the history
And their unit tests!

Co-Authored-By volth <[email protected]>
  • Loading branch information
Ericson2314 committed Nov 15, 2023
1 parent 41718d9 commit c9f5d8a
Show file tree
Hide file tree
Showing 53 changed files with 182 additions and 37 deletions.
13 changes: 5 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ ifeq ($(ENABLE_BUILD), yes)
makefiles = \
mk/precompiled-headers.mk \
local.mk \
src/libutil/local.mk
src/libutil/local.mk \
src/libstore/local.mk \
src/libfetchers/local.mk \
src/libexpr/local.mk

ifdef HOST_UNIX
makefiles += \
src/libstore/local.mk \
src/libfetchers/local.mk \
src/libmain/local.mk \
src/libexpr/local.mk \
src/libcmd/local.mk \
src/nix/local.mk \
src/resolve-system-dependencies/local.mk \
Expand All @@ -33,13 +33,10 @@ endif
ifeq ($(ENABLE_BUILD)_$(ENABLE_TESTS), yes_yes)
UNIT_TEST_ENV = _NIX_TEST_UNIT_DATA=unit-test-data
makefiles += \
src/libutil/tests/local.mk
ifdef HOST_UNIX
makefiles += \
src/libutil/tests/local.mk \
src/libstore/tests/local.mk \
src/libexpr/tests/local.mk
endif
endif

ifeq ($(ENABLE_TESTS), yes)
ifdef HOST_UNIX
Expand Down
6 changes: 5 additions & 1 deletion src/libexpr/eval-settings.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "users.hh"
#include "globals.hh"
#include "profiles.hh"
#ifndef _WIN32
# include "profiles.hh"
#endif
#include "eval.hh"
#include "eval-settings.hh"

Expand Down Expand Up @@ -65,8 +67,10 @@ Strings EvalSettings::getDefaultNixPath()

if (!evalSettings.restrictEval && !evalSettings.pureEval) {
add(getNixDefExpr() + "/channels");
#ifndef _WIN32
add(rootChannelsDir() + "/nixpkgs", "nixpkgs");
add(rootChannelsDir());
#endif
}

return res;
Expand Down
17 changes: 14 additions & 3 deletions src/libexpr/eval.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,32 @@
#include "eval-inline.hh"
#include "filetransfer.hh"
#include "function-trace.hh"
#include "profiles.hh"
#include "print.hh"
#include "fs-input-accessor.hh"
#include "memory-input-accessor.hh"
#include "signals.hh"

#ifndef _WIN32
# include "profiles.hh"
#endif

#include <algorithm>
#include <chrono>
#include <iostream>
#include <cstring>
#include <optional>
#include <unistd.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <iostream>
#include <fstream>
#include <functional>

#include <sys/resource.h>
#include <nlohmann/json.hpp>

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

#if HAVE_BOEHMGC

#define GC_INCLUDE_NEW
Expand Down Expand Up @@ -462,6 +467,8 @@ ErrorBuilder & ErrorBuilder::withFrame(const Env & env, const Expr & expr)
return *this;
}

// Don't want Windows function
#undef SearchPath

EvalState::EvalState(
const SearchPath & _searchPath,
Expand Down Expand Up @@ -2543,9 +2550,11 @@ void EvalState::maybePrintStats()

void EvalState::printStatistics()
{
#ifndef _WIN32
struct rusage buf;
getrusage(RUSAGE_SELF, &buf);
float cpuTime = buf.ru_utime.tv_sec + ((float) buf.ru_utime.tv_usec / 1000000);
#endif

uint64_t bEnvs = nrEnvs * sizeof(Env) + nrValuesInEnvs * sizeof(Value *);
uint64_t bLists = nrListElems * sizeof(Value *);
Expand All @@ -2562,7 +2571,9 @@ void EvalState::printStatistics()
if (outPath != "-")
fs.open(outPath, std::fstream::out);
json topObj = json::object();
#ifndef _WIN32
topObj["cpuTime"] = cpuTime;
#endif
topObj["envs"] = {
{"number", nrEnvs},
{"elements", nrValuesInEnvs},
Expand Down
6 changes: 3 additions & 3 deletions src/libexpr/lexer.l
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ else { return ELSE; }
assert { return ASSERT; }
with { return WITH; }
let { return LET; }
in { return IN; }
in { return INX; }
rec { return REC; }
inherit { return INHERIT; }
or { return OR_KW; }
Expand All @@ -158,7 +158,7 @@ or { return OR_KW; }
.errPos = data->state.positions[CUR_POS],
});
}
return INT;
return INTX;
}
{FLOAT} { errno = 0;
yylval->nf = strtod(yytext, 0);
Expand All @@ -167,7 +167,7 @@ or { return OR_KW; }
.msg = hintfmt("invalid float '%1%'", yytext),
.errPos = data->state.positions[CUR_POS],
});
return FLOAT;
return FLOATX;
}

\$\{ { PUSH_STATE(DEFAULT); return DOLLAR_CURLY; }
Expand Down
8 changes: 7 additions & 1 deletion src/libexpr/local.mk
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,18 @@ libexpr_SOURCES := \
$(wildcard $(d)/flake/*.cc) \
$(d)/lexer-tab.cc \
$(d)/parser-tab.cc
ifdef HOST_UNIX
libexpr_SOURCES += $(wildcard $(d)/unix/*.cc)
endif

libexpr_CXXFLAGS += $(INCLUDE_libutil) $(INCLUDE_libstore) -I src/libfetchers -I src/libmain -I src/libexpr

libexpr_LIBS = libutil libstore libfetchers

libexpr_LDFLAGS += -lboost_context -pthread
libexpr_LDFLAGS += -lboost_context
ifdef HOST_UNIX
libexpr_LDFLAGS += -pthread
endif
ifdef HOST_LINUX
libexpr_LDFLAGS += -ldl
endif
Expand Down
12 changes: 6 additions & 6 deletions src/libexpr/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -340,11 +340,11 @@ void yyerror(YYLTYPE * loc, yyscan_t scanner, ParseData * data, const char * err
%type <id> attr
%token <id> ID
%token <str> STR IND_STR
%token <n> INT
%token <nf> FLOAT
%token <n> INTX
%token <nf> FLOATX
%token <path> PATH HPATH SPATH PATH_END
%token <uri> URI
%token IF THEN ELSE ASSERT WITH LET IN REC INHERIT EQ NEQ AND OR IMPL OR_KW
%token IF THEN ELSE ASSERT WITH LET INX REC INHERIT EQ NEQ AND OR IMPL OR_KW
%token DOLLAR_CURLY /* == ${ */
%token IND_STRING_OPEN IND_STRING_CLOSE
%token ELLIPSIS
Expand Down Expand Up @@ -387,7 +387,7 @@ expr_function
{ $$ = new ExprAssert(CUR_POS, $2, $4); }
| WITH expr ';' expr_function
{ $$ = new ExprWith(CUR_POS, $2, $4); }
| LET binds IN expr_function
| LET binds INX expr_function
{ if (!$2->dynamicAttrs.empty())
throw ParseError({
.msg = hintfmt("dynamic attributes not allowed in let"),
Expand Down Expand Up @@ -457,8 +457,8 @@ expr_simple
else
$$ = new ExprVar(CUR_POS, data->symbols.create($1));
}
| INT { $$ = new ExprInt($1); }
| FLOAT { $$ = new ExprFloat($1); }
| INTX { $$ = new ExprInt($1); }
| FLOATX { $$ = new ExprFloat($1); }
| '"' string_parts '"' { $$ = $2; }
| IND_STRING_OPEN ind_string_parts IND_STRING_CLOSE {
$$ = stripIndentation(CUR_POS, data->symbols, std::move(*$2));
Expand Down
13 changes: 12 additions & 1 deletion src/libexpr/primops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@
#include <algorithm>
#include <cstring>
#include <regex>
#include <dlfcn.h>

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

#include <cmath>

Expand Down Expand Up @@ -327,6 +330,8 @@ static RegisterPrimOp primop_import({
}
});

#ifndef _WIN32

/* Want reasonable symbol names, so extern C */
/* !!! Should we pass the Pos or the file name too? */
extern "C" typedef void (*ValueInitializer)(EvalState & state, Value & v);
Expand Down Expand Up @@ -399,6 +404,8 @@ void prim_exec(EvalState & state, const PosIdx pos, Value * * args, Value & v)
}
}

#endif

/* Return a string representing the type of the expression. */
static void prim_typeOf(EvalState & state, const PosIdx pos, Value * * args, Value & v)
{
Expand Down Expand Up @@ -2223,7 +2230,9 @@ static void addPath(
arg2.mkString(
S_ISREG(st.st_mode) ? "regular" :
S_ISDIR(st.st_mode) ? "directory" :
#ifndef _WIN32
S_ISLNK(st.st_mode) ? "symlink" :
#endif
"unknown" /* not supported, will fail! */);

Value * args []{&arg1, &arg2};
Expand Down Expand Up @@ -4458,6 +4467,7 @@ void EvalState::createBaseEnv()
)",
});

#ifndef _WIN32
// Miscellaneous
if (evalSettings.enableNativeCode) {
addPrimOp({
Expand All @@ -4471,6 +4481,7 @@ void EvalState::createBaseEnv()
.fun = prim_exec,
});
}
#endif

addPrimOp({
.name = "__traceVerbose",
Expand Down
3 changes: 3 additions & 0 deletions src/libexpr/search-path.hh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

namespace nix {

// Do not want the windows macro (alias to `SearchPathA`)
#undef SearchPath

/**
* A "search path" is a list of ways look for something, used with
* `builtins.findFile` and `< >` lookup expressions.
Expand Down
2 changes: 2 additions & 0 deletions src/libexpr/tests/primops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,13 @@ namespace nix {
ASSERT_THAT(*p->value, IsIntEq(123));
}

#ifndef _WIN32
TEST_F(PrimOpTest, getEnv) {
setenv("_NIX_UNIT_TEST_ENV_VALUE", "test value", 1);
auto v = eval("builtins.getEnv \"_NIX_UNIT_TEST_ENV_VALUE\"");
ASSERT_THAT(v, IsStringEq("test value"));
}
#endif

TEST_F(PrimOpTest, seq) {
ASSERT_THROW(eval("let x = throw \"test\"; in builtins.seq x { }"), ThrownError);
Expand Down
4 changes: 3 additions & 1 deletion src/libfetchers/local.mk
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ libfetchers_SOURCES := $(wildcard $(d)/*.cc)

libfetchers_CXXFLAGS += $(INCLUDE_libutil) $(INCLUDE_libstore)

libfetchers_LDFLAGS += -pthread
ifdef HOST_UNIX
libfetchers_LDFLAGS += -pthread
endif

libfetchers_LIBS = libutil libstore
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit c9f5d8a

Please sign in to comment.