Skip to content

Commit

Permalink
python-bindings: Incremental building
Browse files Browse the repository at this point in the history
Can now do this:

$ nix develop .#python
$ cd python
$ make

Also reuses the common.sh/init.sh testing scripts
  • Loading branch information
infinisil committed Feb 7, 2023
1 parent f079036 commit cc11189
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 52 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ makefiles = \
misc/upstart/local.mk \
doc/manual/local.mk \
tests/local.mk \
tests/plugins/local.mk
tests/plugins/local.mk \
python/local.mk

-include Makefile.config

Expand Down
52 changes: 8 additions & 44 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -388,49 +388,10 @@
postUnpack = "sourceRoot=$sourceRoot/perl";
});

passthru.python-bindings =
let
python = final.python3;
in python.pkgs.buildPythonPackage {
name = "nix";
format = "other";

src = self;
postUnpack = "sourceRoot=$sourceRoot/python";

nativeBuildInputs = [
final.buildPackages.ninja
(final.buildPackages.meson.override { python3 = python; })
final.buildPackages.pkgconfig
final.buildPackages.nix
];

strictDeps = true;

buildInputs = [
final.nix
final.boost
];

installCheckPhase = ''
export TEST_ROOT=$(mktemp -d)
export NIX_STATE_DIR=$TEST_ROOT/var/nix
export NIX_STORE_DIR=$TEST_ROOT/store
export NIX_LOCALSTATE_DIR=$TEST_ROOT/var
export NIX_LOG_DIR=$TEST_ROOT/var/log/nix
export NIX_CONF_DIR=$TEST_ROOT/etc
mkdir "$NIX_CONF_DIR"
cat > "$NIX_CONF_DIR"/nix.conf <<EOF
sandbox = false
substituters =
EOF
export PYTHONPATH=$out/${python.sitePackages}
ninja test
'';
};
passthru.python-bindings = final.callPackage ./python {
inherit self system;
python = final.python3;
};

meta.platforms = systems;
};
Expand Down Expand Up @@ -768,7 +729,10 @@
'';
}
)
// { default = self.devShells.${system}.stdenv; }
// {
default = self.devShells.${system}.stdenv;
python = self.packages.${system}.nix.python-bindings.shell;
}
);

};
Expand Down
1 change: 1 addition & 0 deletions python/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.cache
build
9 changes: 9 additions & 0 deletions python/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
build:
$(MAKE) -C .. python/build
meson compile -C build

test:
$(MAKE) -C .. python/build
meson test -C build -v

.PHONY: build test
40 changes: 40 additions & 0 deletions python/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{ self, system, lib, python, ninja, meson, nix, mkShell }:
python.pkgs.buildPythonPackage {
name = "nix";
format = "other";

src = self;

strictDeps = true;

nativeBuildInputs = lib.optionals (nix != null) nix.nativeBuildInputs ++ [
ninja
(meson.override { python3 = python; })
nix
];

buildInputs = lib.optionals (nix != null) nix.buildInputs ++ [
nix
];

passthru.shell = mkShell {
inputsFrom = [
self.devShells.${system}.stdenv
(nix.python-bindings.override { nix = null; })
];
};

# We need to be able to generate tests/common.sh, which requires running
# `make`, which requires having run `autoreconf` and `./configure`.
# So we propagate `autoreconfHook` from nix.nativeBuildInputs for that to
# work, but after that we also need to cd into the python directory and run the
# meson configure phase for the python bindings
configurePhase = ''
./configure
cd python
mesonConfigurePhase
'';

doInstallCheck = true;
installCheckPhase = "meson test -v";
}
8 changes: 8 additions & 0 deletions python/local.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
pythonbuild=$(d)/build

clean-files += $(pythonbuild)

$(pythonbuild): src/libexpr/nix-expr.pc $(libdir)/libnixexpr.so src/libmain/nix-main.pc $(libdir)/libnixmain.so
rm -rf $(pythonbuild)
PKG_CONFIG_PATH=$(libdir)/pkgconfig:$$PKG_CONFIG_PATH meson setup $(pythonbuild) python
touch $(pythonbuild)
14 changes: 11 additions & 3 deletions python/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@ python_dep = dependency('python3', required : true)
nix_expr_dep = dependency('nix-expr', required: true)
nix_main_dep = dependency('nix-main', required: true)

python = python_mod.find_python()
test('python test', python, args : files('tests.py'))

subdir('src')

fs = import('fs')

nix_root = fs.parent(meson.project_source_root())
run_command('make', '-C', nix_root, 'tests/common.sh', check: true)

env = environment()
env.prepend('PYTHONPATH', fs.parent(pythonix.full_path()))
bash = find_program('bash')
test('python test', bash, args : files('test.sh'), env : env)

2 changes: 1 addition & 1 deletion python/src/internal/nix-to-python.hh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <Python.h>

#include <nix/config.h>
#include <config.h>

#include <eval.hh>

Expand Down
2 changes: 1 addition & 1 deletion python/src/internal/python-to-nix.hh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <Python.h>
#include <nix/config.h>
#include <config.h>

#include <eval.hh>
#include <optional>
Expand Down
2 changes: 1 addition & 1 deletion python/src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ src = [
'python-module.cc',
]

python_mod.extension_module('nix', src,
pythonix = python_mod.extension_module('nix', src,
dependencies : [python_dep, nix_expr_dep, nix_main_dep],
install: true,
install_dir: python_mod.sysconfig_path('platlib'),
Expand Down
2 changes: 1 addition & 1 deletion python/src/python-module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "internal/eval.hh"
#include "internal/ptr.hh"

#include <nix/config.h>
#include <config.h>

#include <eval.hh>
#include <globals.hh>
Expand Down
7 changes: 7 additions & 0 deletions python/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )

cd "$SCRIPT_DIR"/../tests

source init.sh

python "$SCRIPT_DIR"/tests.py

0 comments on commit cc11189

Please sign in to comment.