|
10 | 10 | Author Tim Molteno. tim@molteno.net |
11 | 11 | """ |
12 | 12 |
|
| 13 | +import datetime |
13 | 14 | import os |
14 | 15 | from glob import glob |
15 | 16 |
|
16 | 17 | from setuptools import Extension, setup |
17 | 18 |
|
| 19 | +# --------------------------------------------------------------------------- |
| 20 | +# Generate config.h (normally done by CMake, but needed for pip installs and |
| 21 | +# PEP 517 isolated builds where CMake is not available). |
| 22 | +# --------------------------------------------------------------------------- |
| 23 | +BUILD_DIR = "necpp_src/build" |
| 24 | +os.makedirs(BUILD_DIR, exist_ok=True) |
| 25 | + |
| 26 | +# Try to read the version from pyproject.toml; fall back to a known default. |
| 27 | +_version = "2.2.4" |
| 28 | +try: |
| 29 | + import tomllib # Python 3.11+ |
| 30 | +except ImportError: |
| 31 | + try: |
| 32 | + import tomli as tomllib |
| 33 | + except ImportError: |
| 34 | + pass |
| 35 | + else: |
| 36 | + with open("pyproject.toml", "rb") as _f: |
| 37 | + _version = tomllib.load(_f)["project"]["version"] |
| 38 | +else: |
| 39 | + with open("pyproject.toml", "rb") as _f: |
| 40 | + _version = tomllib.load(_f)["project"]["version"] |
| 41 | + |
| 42 | +_build_date = datetime.date.today().strftime("%Y-%m-%d") |
| 43 | + |
| 44 | +_config_h = os.path.join(BUILD_DIR, "config.h") |
| 45 | +with open(_config_h, "w") as _f: |
| 46 | + _f.write(f"""#ifndef CONFIG_H |
| 47 | +#define CONFIG_H |
| 48 | +
|
| 49 | +#define NECPP_VERSION "{_version}" |
| 50 | +#define NECPP_BUILD_DATE "{_build_date}" |
| 51 | +
|
| 52 | +#endif /* CONFIG_H */ |
| 53 | +""") |
| 54 | + |
18 | 55 | # numpy is declared in pyproject.toml build-system.requires for PEP 517 isolated builds. |
19 | 56 | # Guard against the case where setup.py is evaluated for metadata before build deps are installed. |
20 | 57 | try: |
|
37 | 74 | ) |
38 | 75 | nec_sources.extend(glob("PyNEC_wrap.cxx")) |
39 | 76 |
|
40 | | -# config.h is generated by CMake into necpp_src/build/ (run build.sh first). |
| 77 | +# config.h is auto-generated above from pyproject.toml metadata. |
41 | 78 | # |
42 | 79 | if np is not None: |
43 | 80 | include_dirs = [np.get_include(), "necpp_src/src", "necpp_src/", "necpp_src/build/", "necpp_src/src/eigen"] |
|
0 commit comments