-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsetup.py
More file actions
70 lines (62 loc) · 2.41 KB
/
Copy pathsetup.py
File metadata and controls
70 lines (62 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python3
import importlib.util
import sys
from pathlib import Path
from setuptools import setup
from cffi import FFI
# --- Configuration ---
# Get the absolute path to the directory containing setup.py
here = Path(__file__).parent.resolve()
cffi_module_path = here / "cffi_module"
libmseed_path = cffi_module_path / "libmseed"
# --- CFFI Setup ---
# Import the CFFI definitions from the separate definitions file
spec = importlib.util.spec_from_file_location("cffi_defs", cffi_module_path / "cffi_defs.py")
cffi_defs = importlib.util.module_from_spec(spec)
spec.loader.exec_module(cffi_defs)
# Create FFI instance and configure it with the C definitions
ffi = FFI()
ffi.cdef(cffi_defs.LIBRARY_CDEF)
# Find all C source files.
#
# IMPORTANT: Paths must be explicitly relative to the 'setup.py' directory
# for setuptools to work correctly, especially with 'python -m build'.
# We use pathlib to find the absolute paths and then make them relative to 'here'.
c_sources = [str(p.relative_to(here)) for p in libmseed_path.glob("*.c")]
# --- Platform-specific compiler options ---
if sys.platform.startswith("win"):
# Windows-specific options
extra_compile_args = ["/O2"]
extra_link_args = []
define_macros = [("_CRT_SECURE_NO_WARNINGS", None)]
else:
# Unix-like systems (Linux, macOS)
extra_compile_args = ["-O2"]
extra_link_args = []
define_macros = []
# --- CFFI Source Configuration ---
# Configure the CFFI extension module
ffi.set_source(
"_libmseed_cffi",
# This is the C code that will be compiled. It includes the header
# which makes all the C functions available to the CFFI module.
'#include "libmseed.h"',
# Provide the list of all C source files to be compiled together.
sources=c_sources,
# Provide the include directory, also as a relative path.
include_dirs=[str(libmseed_path.relative_to(here))],
# Pass platform-specific compiler and linker arguments.
extra_compile_args=extra_compile_args,
extra_link_args=extra_link_args,
define_macros=define_macros,
)
# --- Setuptools Configuration ---
setup(
# This is where the magic happens: ffi.distutils_extension() creates
# the Extension object that setuptools will build.
ext_modules=[ffi.distutils_extension()],
# The compiled extension will be placed inside the 'pymseed' package.
ext_package="pymseed",
# Wheels with compiled extensions are not zip-safe.
zip_safe=False,
)