Skip to content

Commit c667b05

Browse files
committed
Simplify Python setup process
1 parent a6475ba commit c667b05

File tree

3 files changed

+40
-20
lines changed

3 files changed

+40
-20
lines changed

python/README.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,11 @@ Install the project with pip
108108
109109
pip install .
110110
111+
If you already have a ``s-dftd3`` installation, *e.g.* from conda-forge, you can build the Python extension module directly without cloning this repository
112+
113+
.. code:: sh
114+
pip install "https://github.com/awvwgk/simple-dftd3/archive/refs/heads/main.zip#egg=dftd3-python&subdirectory=python"
115+
111116
112117
113118
Using meson

python/build.py

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,16 @@
3232
import os
3333
import cffi
3434

35+
library = "s-dftd3"
36+
include_header = '#include "dftd3.h"'
37+
prefix_var = "SDFTD3_PREFIX"
38+
if prefix_var not in os.environ:
39+
prefix_var = "CONDA_PREFIX"
40+
3541
if __name__ == "__main__":
3642
import sys
3743

38-
kwargs = dict(libraries=["s-dftd3"])
44+
kwargs = dict(libraries=[library])
3945

4046
header_file = sys.argv[1]
4147
module_name = sys.argv[2]
@@ -44,22 +50,33 @@
4450
cdefs = f.read()
4551
else:
4652
import subprocess
47-
import pkgconfig
48-
49-
if not pkgconfig.exists("s-dftd3"):
50-
raise Exception("Unable to find pkg-config package 's-dftd3'")
51-
if pkgconfig.installed("s-dftd3", "< 0.4"):
52-
raise Exception(
53-
"Installed 's-dftd3' version is too old, 0.4 or newer is required"
54-
)
55-
56-
kwargs = pkgconfig.parse("s-dftd3")
57-
58-
if "CC" not in os.environ:
59-
raise Exception("This build script requires to set a C compiler in CC")
60-
cc = os.environ["CC"]
6153

62-
cflags = pkgconfig.cflags("s-dftd3").split()
54+
try:
55+
import pkgconfig
56+
57+
if not pkgconfig.exists(library):
58+
raise ModuleNotFoundError("Unable to find pkg-config package 's-dftd3'")
59+
if pkgconfig.installed(library, "< 0.4"):
60+
raise Exception(
61+
"Installed 's-dftd3' version is too old, 0.4 or newer is required"
62+
)
63+
64+
kwargs = pkgconfig.parse(library)
65+
cflags = pkgconfig.cflags(library).split()
66+
67+
except ModuleNotFoundError:
68+
kwargs = dict(libraries=[library])
69+
cflags = []
70+
if prefix_var in os.environ:
71+
prefix = os.environ[prefix_var]
72+
kwargs.update(
73+
include_dirs=[os.path.join(prefix, "include")],
74+
library_dirs=[os.path.join(prefix, "lib")],
75+
runtime_library_dirs=[os.path.join(prefix, "lib")],
76+
)
77+
cflags.append("-I" + os.path.join(prefix, "include"))
78+
79+
cc = os.environ["CC"] if "CC" in os.environ else "cc"
6380

6481
module_name = "dftd3._libdftd3"
6582

@@ -69,12 +86,12 @@
6986
stdout=subprocess.PIPE,
7087
stderr=subprocess.PIPE,
7188
)
72-
out, err = p.communicate(b'#include "s-dftd3.h"')
89+
out, err = p.communicate(include_header.encode())
7390

7491
cdefs = out.decode()
7592

7693
ffibuilder = cffi.FFI()
77-
ffibuilder.set_source(module_name, '#include "s-dftd3.h"', **kwargs)
94+
ffibuilder.set_source(module_name, include_header, **kwargs)
7895
ffibuilder.cdef(cdefs)
7996

8097
if __name__ == "__main__":

python/setup.cfg

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ classifiers =
2626

2727
[options]
2828
packages = find:
29-
setup_requires =
30-
pkgconfig
3129
install_requires =
3230
cffi
3331
numpy

0 commit comments

Comments
 (0)