Skip to content

Commit ee40529

Browse files
mario4tiermrjbq7
authored andcommitted
Add TA_LIB_SOURCE to build a local TA-Lib C source
Uses the same build as the pinned 0.8.1 download, just from a directory or tarball, so an unreleased TA-Lib C can be tested the way the wheels are built.
1 parent 637de1f commit ee40529

2 files changed

Lines changed: 34 additions & 4 deletions

File tree

‎DEVELOPMENT‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,11 @@ tools/generate_func.py,generate_stream.py
5050

5151
If you are interested in developing new indicator functions or whatnot on
5252
the underlying TA-Lib, you must install TA-Lib from git.
53+
54+
Alternatively, to build against a TA-Lib C checkout with the same static
55+
build the wheels use, set TA_LIB_SOURCE to its directory (or a source
56+
tarball) instead of installing it:
57+
58+
```
59+
$ TA_LIB_SOURCE=~/ta-lib make build
60+
```

‎setup.py‎

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,20 @@
2020
TALIB_C_SHA256 = 'ec59ccd88c0c77f618587d858787c8f9d06c40460a09d66751926f6fd670f985'
2121

2222

23-
def build_talib(directory, plat_name):
24-
"""Build the pinned C release locally, without installing it system-wide."""
23+
def talib_source(directory):
24+
"""Return the TA-Lib C source tree: TA_LIB_SOURCE if set, else the pinned release."""
25+
if 'TA_LIB_SOURCE' in os.environ:
26+
path = Path(os.environ['TA_LIB_SOURCE']).expanduser().resolve()
27+
if path.is_dir():
28+
return path
29+
with tarfile.open(path) as source:
30+
source.extractall(directory / 'source')
31+
trees = [p for p in (directory / 'source').iterdir()
32+
if (p / 'CMakeLists.txt').is_file()]
33+
if len(trees) != 1:
34+
raise RuntimeError(f'TA_LIB_SOURCE: no single TA-Lib C tree in {path}')
35+
return trees[0]
36+
2537
archive = directory / 'ta-lib.tar.gz'
2638
urllib.request.urlretrieve(
2739
f'https://github.com/TA-Lib/ta-lib/releases/download/v{TALIB_C_VERSION}/'
@@ -30,11 +42,16 @@ def build_talib(directory, plat_name):
3042
raise RuntimeError('TA-Lib C source checksum mismatch')
3143
with tarfile.open(archive) as source:
3244
source.extractall(directory)
45+
return directory / f'ta-lib-{TALIB_C_VERSION}'
3346

47+
48+
def build_talib(directory, plat_name):
49+
"""Build TA-Lib C locally, without installing it system-wide."""
50+
source = talib_source(directory)
3451
prefix = directory / 'install'
3552
build = directory / 'build'
3653
args = [
37-
'cmake', '-S', str(directory / f'ta-lib-{TALIB_C_VERSION}'),
54+
'cmake', '-S', str(source),
3855
'-B', str(build), '-DCMAKE_BUILD_TYPE=Release',
3956
f'-DCMAKE_INSTALL_PREFIX={prefix}',
4057
'-DBUILD_SHARED_LIBS=OFF', '-DBUILD_STATIC_LIBS=ON',
@@ -77,6 +94,9 @@ def build_extensions(self):
7794
Respect explicit paths, otherwise build and statically link TA-Lib C.
7895
"""
7996
if 'TA_LIBRARY_PATH' in os.environ or 'TA_INCLUDE_PATH' in os.environ:
97+
if 'TA_LIB_SOURCE' in os.environ:
98+
raise RuntimeError('TA_LIB_SOURCE cannot be combined with '
99+
'TA_INCLUDE_PATH or TA_LIBRARY_PATH')
80100
if sys.platform == 'win32':
81101
include_dirs = [r'c:\ta-lib\c\include',
82102
r'c:\Program Files\TA-Lib\include',
@@ -108,9 +128,11 @@ def build_extensions(self):
108128
with tempfile.TemporaryDirectory(prefix='ta-lib-') as temporary:
109129
prefix = build_talib(Path(temporary), self.plat_name)
110130
library = 'ta-lib-static.lib' if sys.platform == 'win32' else 'libta-lib.a'
131+
archive = str(prefix / 'lib' / library)
111132
for ext in self.extensions:
112133
ext.include_dirs += [str(prefix / 'include'), numpy.get_include()]
113-
ext.extra_objects = [str(prefix / 'lib' / library)]
134+
ext.extra_objects = [archive]
135+
ext.depends.append(archive)
114136
super().build_extensions()
115137

116138
cmdclass = {'build_ext': NumpyBuildExt}

0 commit comments

Comments
 (0)