Closed mcleantom closed 1 month ago
A good example of what I would be trying to do could be tyring to include numpy in a cython project with setup-build-core
That's a fundamental CMake design question. If you are ok with minimum CMake 3.25, the recommended approach is:
# CMake boilerplate to define your project
cmake_minimum_required(VERSION 3.25...3.30)
project(My_Project)
# Define your targets that you are building
# Normally you have `add_library` here, but for python it is `python_add_library`
find_package(Python 3.9 REQUIRED COMPONENTS Development.Module)
python_add_library(_ta_lib)
# Find or fetch `talib` source code
include(FetchContent)
FetchContent_Declare(
ta-lib
GIT_REPOSITORY https://github.com/TA-Lib/ta-lib
GIT_TAG 0.4.0
# The next part is to support importing from the system
# it doesn't work for `ta-lib`, but it's good design to know and start using
FIND_PACKAGE_ARGS
)
FetchContent_MakeAvailable(ta-lib)
# Link to the library, see `ta-lib/CMakeLists.txt` for how the targets are defined
target_link_libraries(_ta_lib PRIVATE ta_lib)
Here I am only covering the CMake linking part, see the other resources for more details on how to cythonize and other stuff. Resources:
scikit-build-core
, python and CMake interface FIND_PACKAGE_ARGS
and find_package
though.@LecrisUT Thanks, I didnt realise I could do target_link_libraries(_ta_lib PRIVATE ta_lib)
as I wasnt sure what python_add_library(_ta_lib) was doing but that makes sense. Still struggling to get an example to work but I will try make a minimal reproducible example repository and if I cant get that to work i'll seek out some more help 👍
For Cython, cython-cmake
is more-or-less ready to be used, so you can try that if it's helpful. NumPy's likely quite different from TA-Lib, since you'd do that by adding it to pyproject-requires
, rather than trying to download it or put it in as a submodule, etc. For the link itself, yes, python_add_library makes a normal target.
@henryiii Thanks for the help, could you let me know if I have missed something in my work? I've added talib to my project using vcpkg:
{
"name": "ta-lib-easy",
"dependencies": [
"talib"
]
}
And I can see the header files at vcpkg_installed/vcpkg/x64-windows/include
and the library files at vcpkg_installed/vcpkg/x64-windows/lib
, so I tried to add the header files using this method:
cmake_minimum_required(VERSION 3.25...3.30)
project(${SKBUILD_PROJECT_NAME} LANGUAGES C)
set(VCPKG_ROOT "${CMAKE_SOURCE_DIR}/vcpkg/installed/x64-windows")
find_package(
Python
COMPONENTS Interpreter Development.Module
REQUIRED
)
find_package(Cython)
include(UseCython)
cython_transpile(
"${CMAKE_CURRENT_SOURCE_DIR}/src/ta_lib_easy/one.pyx"
LANGUAGE C
OUTPUT_VARIABLE one_c
CYTHON_ARGS -I "${VCPKG_ROOT}/include"
)
python_add_library(
one
MODULE
"${one_c}"
WITH_SOABI
)
target_link_libraries(
one
PUBLIC
"${VCPKG_ROOT}/lib/ta_abstract.lib"
"${VCPKG_ROOT}/lib/ta_common.lib"
"${VCPKG_ROOT}/lib/ta_func.lib"
"${VCPKG_ROOT}/lib/ta_libc.lib"
)
install(TARGETS one DESTINATION ta_lib_easy/)
however i'm getting the error:
C:\Users\tom.mclean\AppData\Local\Temp\tmpi1xr3lrl\build\src\ta_lib_easy\one.c(1222,10): fatal e
rror C1083: Cannot open include file: 'ta_defs.h': No such file or directory
Which ive just added in my one.pyx
file, importing some return code definitions:
# cython: language_level=3, embedsignature=True
cdef extern from "ta_defs.h":
ctypedef int TA_RetCode
TA_RetCode TA_SUCCESS = 0
TA_RetCode TA_LIB_NOT_INITIALIZE = 1
TA_RetCode TA_BAD_PARAM = 2
TA_RetCode TA_ALLOC_ERR = 3
TA_RetCode TA_GROUP_NOT_FOUND = 4
TA_RetCode TA_FUNC_NOT_FOUND = 5
TA_RetCode TA_INVALID_HANDLE = 6
TA_RetCode TA_INVALID_PARAM_HOLDER = 7
TA_RetCode TA_INVALID_PARAM_HOLDER_TYPE = 8
TA_RetCode TA_INVALID_PARAM_FUNCTION = 9
TA_RetCode TA_INPUT_NOT_ALL_INITIALIZE = 10
TA_RetCode TA_OUTPUT_NOT_ALL_INITIALIZE = 11
TA_RetCode TA_OUT_OF_RANGE_START_INDEX = 12
TA_RetCode TA_OUT_OF_RANGE_END_INDEX = 13
TA_RetCode TA_INVALID_LIST_TYPE = 14
TA_RetCode TA_BAD_OBJECT = 15
TA_RetCode TA_NOT_SUPPORTED = 16
TA_RetCode TA_INTERNAL_ERROR = 5000
TA_RetCode TA_UNKNOWN_ERR = 0xffff
cdef int one():
return 1
I am not sure how to fix this. If I could have any help to fix it it would be appreciated :)
(if its any help, my repository is https://github.com/mcleantom/ta-lib-easy)
You should add target_include_directories
as well. Should point out that talib
build system is horrible you should not be needing to use such an approach, but alas that is not an issue for you to resolve right now.
@LecrisUT Thanks for the help, adding target_include_directories
worked.
Yeah I noticed the build system is a bit dated but hopefully now it's working hopefully I can continue fairly easily, thanks for the help!
I am trying to make a version of this project, but using scikit-build-core to make it easier to install. The project links against the C library TA-Lib. I was wondering if its currently possible to link c libraries to the cython code using scikit_build_core from just pyproject.toml, or do I need to add a setup.py to my project and do something like:
And not use the CMakeLists.txt method?
If it is possible, would it be possible to have an example of how to link a library to the generated cython code from CMakeLists.txt?
Thanks!