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

Implement resolve-system-dependencies in C++ #1030

Merged
merged 11 commits into from
Aug 31, 2016
Merged
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ makefiles = \
src/nix-collect-garbage/local.mk \
src/nix-prefetch-url/local.mk \
src/buildenv/local.mk \
src/resolve-system-dependencies/local.mk \
perl/local.mk \
scripts/local.mk \
corepkgs/local.mk \
Expand Down
17 changes: 0 additions & 17 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -246,23 +246,6 @@ AC_MSG_RESULT(yes)
AC_SUBST(perlFlags)


# Check for otool, an optional dependency on Darwin.
AC_PATH_PROG(otool, otool)
AC_MSG_CHECKING([that otool works])
case $host_os in
darwin*)
if test -z "$otool" || ! $otool --version 2>/dev/null; then
AC_MSG_RESULT(no)
AC_MSG_ERROR([Can't get version from otool; do you need to install developer tools?])
fi
AC_MSG_RESULT(yes)
;;
*)
AC_MSG_RESULT(not needed)
;;
esac


# Whether to build the Perl bindings
AC_MSG_CHECKING([whether to build the Perl bindings])
AC_ARG_ENABLE(perl-bindings, AC_HELP_STRING([--enable-perl-bindings],
Expand Down
7 changes: 0 additions & 7 deletions scripts/local.mk
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,12 @@ nix_noinst_scripts := \
$(d)/nix-profile.sh \
$(d)/nix-reduce-build

ifeq ($(OS), Darwin)
nix_noinst_scripts += $(d)/resolve-system-dependencies.pl
endif

noinst-scripts += $(nix_noinst_scripts)

profiledir = $(sysconfdir)/profile.d

$(eval $(call install-file-as, $(d)/nix-profile.sh, $(profiledir)/nix.sh, 0644))
$(eval $(call install-program-in, $(d)/build-remote.pl, $(libexecdir)/nix))
ifeq ($(OS), Darwin)
$(eval $(call install-program-in, $(d)/resolve-system-dependencies.pl, $(libexecdir)/nix))
endif
$(eval $(call install-symlink, nix-build, $(bindir)/nix-shell))

clean-files += $(nix_bin_scripts) $(nix_noinst_scripts)
122 changes: 0 additions & 122 deletions scripts/resolve-system-dependencies.pl.in

This file was deleted.

2 changes: 1 addition & 1 deletion src/libstore/globals.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ void Settings::processEnvironment()

// should be set with the other config options, but depends on nixLibexecDir
#ifdef __APPLE__
preBuildHook = nixLibexecDir + "/nix/resolve-system-dependencies.pl";
preBuildHook = nixLibexecDir + "/nix/resolve-system-dependencies";
#endif
}

Expand Down
11 changes: 11 additions & 0 deletions src/resolve-system-dependencies/local.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
ifeq ($(OS), Darwin)
programs += resolve-system-dependencies
endif

resolve-system-dependencies_DIR := $(d)

resolve-system-dependencies_INSTALL_DIR := $(libexecdir)/nix

resolve-system-dependencies_LIBS := libstore libmain libutil libformat

resolve-system-dependencies_SOURCES := $(d)/resolve-system-dependencies.cc
202 changes: 202 additions & 0 deletions src/resolve-system-dependencies/resolve-system-dependencies.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
#include "derivations.hh"
#include "globals.hh"
#include "shared.hh"
#include "store-api.hh"
#include <sys/utsname.h>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <mach-o/loader.h>
#include <mach-o/swap.h>

using namespace nix;

typedef std::map<string, std::set<string>> SetMap;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't find any other reference to SetMap.


static auto cacheDir = Path{};

Path resolveCacheFile(const Path & lib) {
Path lib2 = Path(lib);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using a const reference parameter and doing explicit copying, you could directly accept the lib argument via value.

std::replace(lib2.begin(), lib2.end(), '/', '%');
return cacheDir + "/" + lib2;
}

std::set<string> readCacheFile(const Path & file) {
return tokenizeString<set<string>>(readFile(file), "\n");
}

void writeCacheFile(const Path & file, std::set<string> & deps) {
std::ofstream fp;
fp.open(file);
for (auto & d : deps) {
fp << d << "\n";
}
fp.close();
}

std::string find_dylib_name(FILE *obj_file, struct load_command cmd) {
fpos_t pos;
fgetpos(obj_file, &pos);
struct dylib_command dylc;
dylc.cmd = cmd.cmd;
dylc.cmdsize = cmd.cmdsize;
fread(&dylc.dylib, sizeof(struct dylib), 1, obj_file);

char *dylib_name = (char*)calloc(cmd.cmdsize, sizeof(char));
fseek(obj_file,
// offset is calculated from the beginning of the load command, which is two
// uint32_t's backwards
dylc.dylib.name.offset - (sizeof(uint32_t) * 2) + pos,
SEEK_SET);
fread(dylib_name, sizeof(char), cmd.cmdsize, obj_file);
fseek(obj_file, pos, SEEK_SET);
return std::string(dylib_name);
}

bool seek_mach64_blob(FILE *obj_file, enum NXByteOrder end) {
struct fat_header head;
fread(&head, sizeof(struct fat_header), 1, obj_file);
swap_fat_header(&head, end);
for(uint32_t narches = 0; narches < head.nfat_arch; narches++) {
struct fat_arch arch;
fread(&arch, sizeof(struct fat_arch), 1, obj_file);
swap_fat_arch(&arch, 1, end);
if(arch.cputype == CPU_TYPE_X86_64) {
fseek(obj_file, arch.offset, SEEK_SET);
return true;
}
}
return false;
}

std::set<std::string> runResolver(const Path & filename) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if there's a rationale behind it, but you seem to alternate between camelCase and underscore_case a fair amount in this file.

FILE *obj_file = fopen(filename.c_str(), "rb");
uint32_t magic;
fread(&magic, sizeof(uint32_t), 1, obj_file);
fseek(obj_file, 0, SEEK_SET);
enum NXByteOrder endianness;
if(magic == 0xBEBAFECA) {
endianness = NX_BigEndian;
if(!seek_mach64_blob(obj_file, endianness)) {
std::cerr << "Could not find any mach64 blobs in file " << filename << ", continuing..." << std::endl;
return std::set<string>();
}
}
struct mach_header_64 header;
fread(&header, sizeof(struct mach_header_64), 1, obj_file);
if(!(header.magic == MH_MAGIC_64 || header.magic == MH_CIGAM_64)) {
std::cerr << "Not a mach-o object file: " << filename << std::endl;
return std::set<string>();
}
std::set<string> libs;
for(uint32_t i = 0; i < header.ncmds; i++) {
struct load_command cmd;
fread(&cmd.cmd, sizeof(uint32_t), 1, obj_file);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feels slightly nicer to make the sizeof calls refer to the field you're loading.

fread(&cmd.cmdsize, sizeof(uint32_t), 1, obj_file);
switch(cmd.cmd) {
case LC_LOAD_DYLIB:
case LC_LOAD_UPWARD_DYLIB:
case LC_REEXPORT_DYLIB:
libs.insert(find_dylib_name(obj_file, cmd));
break;
}
fseek(obj_file, cmd.cmdsize - (sizeof(uint32_t) * 2), SEEK_CUR);
}
fclose(obj_file);
libs.erase(filename);
return libs;
}

bool isSymlink(const Path & path) {
struct stat st;
if(lstat(path.c_str(), &st))
throw SysError(format("getting attributes of path ‘%1%’") % path);

return S_ISLNK(st.st_mode);
}

Path resolveSymlink(const Path & path) {
char buf[PATH_MAX];
ssize_t len = readlink(path.c_str(), buf, sizeof(buf) - 1);
if(len != -1) {
buf[len] = 0;
return Path(buf);
} else {
throw SysError(format("readlink('%1%')") % path);
}
}

std::set<string> resolve_tree(const Path & path, PathSet & deps) {
std::set<string> results;
if(deps.find(path) != deps.end()) {
return std::set<string>();
}
deps.insert(path);
for (auto & lib : runResolver(path)) {
results.insert(lib);
for (auto & p : resolve_tree(lib, deps)) {
results.insert(p);
}
}
return results;
}

std::set<string> get_path(const Path & path) {
Path cacheFile = resolveCacheFile(path);
if(pathExists(cacheFile)) {
return readCacheFile(cacheFile);
}

std::set<string> deps;
std::set<string> paths;
paths.insert(path);

Path next_path = Path(path);
while(isSymlink(next_path)) {
next_path = resolveSymlink(next_path);
paths.insert(next_path);
}

for(auto & t : resolve_tree(next_path, deps)) {
paths.insert(t);
}

writeCacheFile(cacheFile, paths);

return paths;
}

int main(int argc, char ** argv) {
return handleExceptions(argv[0], [&]() {
initNix();

struct utsname _uname;

uname(&_uname);

cacheDir = (format("%1%/dependency-maps/%2%-%3%-%4%")
% settings.nixStateDir
% _uname.machine
% _uname.sysname
% _uname.release).str();

auto store = openStore();

auto drv = store->derivationFromPath(Path(argv[1]));
Strings impurePaths = tokenizeString<Strings>(get(drv.env, "__impureHostDeps"));

std::set<string> all_paths;

for (auto & path : impurePaths) {
for(auto & p : get_path(path)) {
all_paths.insert(p);
}
}

std::cout << "extra-chroot-dirs" << std::endl;
for(auto & path : all_paths) {
std::cout << path << std::endl;
}
std::cout << std::endl;
});
}