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 <volth@volth.com>
  • Loading branch information
Ericson2314 committed Jan 8, 2024
1 parent fc2fadf commit c304aa2
Show file tree
Hide file tree
Showing 56 changed files with 216 additions and 36 deletions.
13 changes: 5 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,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,15 +33,12 @@ endif
ifeq ($(ENABLE_UNIT_TESTS), yes)
makefiles += \
tests/unit/libutil/local.mk \
tests/unit/libutil-support/local.mk
ifdef HOST_UNIX
makefiles += \
tests/unit/libutil-support/local.mk \
tests/unit/libstore/local.mk \
tests/unit/libstore-support/local.mk \
tests/unit/libexpr/local.mk \
tests/unit/libexpr-support/local.mk
endif
endif

ifeq ($(ENABLE_FUNCTIONAL_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 @@ -11,7 +11,6 @@
#include "eval-inline.hh"
#include "filetransfer.hh"
#include "function-trace.hh"
#include "profiles.hh"
#include "print.hh"
#include "fs-input-accessor.hh"
#include "filtering-input-accessor.hh"
Expand All @@ -20,22 +19,28 @@
#include "gc-small-vector.hh"
#include "url.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>
#include <boost/container/small_vector.hpp>

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

#if HAVE_BOEHMGC

#define GC_INCLUDE_NEW
Expand Down Expand Up @@ -467,6 +472,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 @@ -2593,9 +2600,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 @@ -2612,7 +2621,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 @@ -130,7 +130,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 @@ -156,7 +156,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 @@ -165,7 +165,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
15 changes: 14 additions & 1 deletion src/libexpr/local.mk
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,25 @@ 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
ifdef HOST_WINDOWS
# TODO not good
libexpr_LDFLAGS += -Wl,--export-all-symbols
endif

# The dependency on libgc must be propagated (i.e. meaning that
# programs/libraries that use libexpr must explicitly pass -lgc),
Expand Down Expand Up @@ -52,3 +62,6 @@ $(buildprefix)src/libexpr/primops/fromTOML.o: ERROR_SWITCH_ENUM =
# Not for libnixexpr itself, but for downstream libraries using libnixexpr

INCLUDE_libexpr := -I $(d)
ifdef HOST_UNIX
INCLUDE_libexpr += -I $(d)/unix
endif
12 changes: 6 additions & 6 deletions src/libexpr/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -365,11 +365,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 @@ -412,7 +412,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 @@ -482,8 +482,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
11 changes: 10 additions & 1 deletion src/libexpr/primops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@
#include <algorithm>
#include <cstring>
#include <regex>
#include <dlfcn.h>

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

#include <cmath>

Expand Down Expand Up @@ -326,6 +329,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 @@ -398,6 +403,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 @@ -4473,6 +4480,7 @@ void EvalState::createBaseEnv()
)",
});

#ifndef _WIN32
// Miscellaneous
if (evalSettings.enableNativeCode) {
addPrimOp({
Expand All @@ -4486,6 +4494,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
12 changes: 12 additions & 0 deletions src/libfetchers/git-utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,11 @@ struct GitRepoImpl : GitRepo, std::enable_shared_from_this<GitRepoImpl>
{
auto act = (Activity *) payload;
act->result(resFetchStatus, trim(std::string_view(str, len)));
#ifndef _WIN32
return _isInterrupted ? -1 : 0;
#else
return 0;
#endif
}

static int transferProgressCallback(const git_indexer_progress * stats, void * payload)
Expand All @@ -359,7 +363,11 @@ struct GitRepoImpl : GitRepo, std::enable_shared_from_this<GitRepoImpl>
stats->indexed_deltas,
stats->total_deltas,
stats->received_bytes / (1024.0 * 1024.0)));
#ifndef _WIN32
return _isInterrupted ? -1 : 0;
#else
return 0;
#endif
}

void fetch(
Expand Down Expand Up @@ -418,6 +426,7 @@ struct GitRepoImpl : GitRepo, std::enable_shared_from_this<GitRepoImpl>
}
writeFile(allowedSignersFile, allowedSigners);

#ifndef _WIN32
// Run verification command
auto [status, output] = runProgram(RunOptions {
.program = "git",
Expand Down Expand Up @@ -448,6 +457,9 @@ struct GitRepoImpl : GitRepo, std::enable_shared_from_this<GitRepoImpl>
printTalkative("Signature verification on commit %s succeeded.", rev.gitRev());
else
throw Error("Commit signature verification on commit %s failed: %s", rev.gitRev(), output);
#else
throw Error("Commit signature verification not implemented on Windows yet");
#endif
}
};

Expand Down
15 changes: 14 additions & 1 deletion src/libfetchers/local.mk
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,26 @@ libfetchers_NAME = libnixfetchers
libfetchers_DIR := $(d)

libfetchers_SOURCES := $(wildcard $(d)/*.cc)
ifdef HOST_UNIX
libstore_SOURCES += $(wildcard $(d)/unix/*.cc)
endif

libfetchers_CXXFLAGS += $(INCLUDE_libutil) $(INCLUDE_libstore)

libfetchers_LDFLAGS += -pthread $(LIBGIT2_LIBS) -larchive
libfetchers_LDFLAGS += $(LIBGIT2_LIBS) -larchive
ifdef HOST_UNIX
libfetchers_LDFLAGS += -pthread
endif
ifdef HOST_WINDOWS
# TODO not good
libfetchers_LDFLAGS += -Wl,--export-all-symbols
endif

libfetchers_LIBS = libutil libstore

# Not for libnixfetchers itself, but for downstream libraries using libnixfetchers

INCLUDE_libfetchers := -I $(d)
ifdef HOST_UNIX
INCLUDE_libfetchers += -I $(d)/unix
endif
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit c304aa2

Please sign in to comment.