diff --git a/CMakeLists.txt b/CMakeLists.txt index c4b3ce4..d5dffc2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,8 +1,87 @@ -cmake_minimum_required(VERSION 3.15) -project(${SKBUILD_PROJECT_NAME} LANGUAGES CXX) +message(STATUS "") +message(STATUS "------------------------------------------------------------------------") +message(STATUS " Project Cytnx (core), A Cross-section of Python & C++,Tensor network library ") +message(STATUS "------------------------------------------------------------------------") +message(STATUS "") +# ##################################################################### +# ## CMAKE and CXX VERSION +# ##################################################################### +cmake_minimum_required(VERSION 3.20) + +include(cmake/target_sources_local.cmake) +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) + +message(STATUS " Generator: ${CMAKE_GENERATOR}") +message(STATUS " Build Target: ${CMAKE_SYSTEM_NAME}-${CMAKE_SYSTEM_PROCESSOR}") +message(STATUS " Installation Prefix: ${CMAKE_INSTALL_PREFIX}") +set(CMAKE_POSITION_INDEPENDENT_CODE ON) + +# ##################################################################### +# Version information +# ##################################################################### +include(version.cmake) +set(CYTNX_CORE_VERSION + ${CYTNX_CORE_VERSION_MAJOR}.${CYTNX_CORE_VERSION_MINOR}.${CYTNX_CORE_VERSION_PATCH} +) + +## Project: +project(CYTNX_CORE VERSION ${CYTNX_CORE_VERSION} LANGUAGES CXX C) +add_library(cytnx_core STATIC) +set_property(TARGET cytnx_core PROPERTY C_VISIBILITY_PRESET hidden) +set_property(TARGET cytnx_core PROPERTY VISIBILITY_INLINES_HIDDEN ON) + +message(STATUS " Current source dir: ${CMAKE_CURRENT_SOURCE_DIR}") +target_include_directories(cytnx_core + PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/src + PUBLIC + $ + $ +) +# cpp source directory +add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/src) +target_compile_definitions(cytnx_core PUBLIC _LIBCPP_DISABLE_AVAILABILITY) +target_compile_definitions(cytnx_core PUBLIC _LIBCPP_ENABLE_CXX17_REMOVED_UNARY_BINARY_FUNCTION) +target_compile_options(cytnx_core PUBLIC -Wformat=0 -w -fsized-deallocation) +target_compile_features(cytnx_core PUBLIC cxx_std_17) + + +## install +include(GNUInstallDirs) + +message(STATUS " install libdir: ${CMAKE_INSTALL_LIBDIR}") +message(STATUS " install includedir: ${CMAKE_INSTALL_INCLUDEDIR}") +set(INSTALL_CONFIGDIR ${CMAKE_INSTALL_LIBDIR}/cmake/cytnx_core) +INSTALL(TARGETS cytnx_core EXPORT cytnx_targets + LIBRARY + DESTINATION ${CMAKE_INSTALL_LIBDIR} + COMPONENT libraries + ARCHIVE + DESTINATION ${CMAKE_INSTALL_LIBDIR} + COMPONENT libraries + PUBLIC_HEADER + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} + COMPONENT Development +) +install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/include/ + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} + COMPONENT headers + FILES_MATCHING PATTERN "*.h*") + +export(EXPORT cytnx_targets FILE ${CMAKE_CURRENT_BINARY_DIR}/CytnxTargets.cmake NAMESPACE cytnx_core::) +export(PACKAGE cytnx_core) + +# ##################################################################### +# Pybind +# ##################################################################### set(PYBIND11_FINDPYTHON ON) find_package(pybind11 CONFIG REQUIRED) -pybind11_add_module(_core MODULE src/main.cpp) -install(TARGETS _core DESTINATION ${SKBUILD_PROJECT_NAME}) +pybind11_add_module(pycytnx MODULE src/cpp/pybind/main.cpp) +target_link_libraries(pycytnx PUBLIC cytnx_core) +#install(TARGETS pycytnx DESTINATION ${SKBUILD_PROJECT_NAME}) +install(TARGETS pycytnx DESTINATION ${CMAKE_INSTALL_PREFIX}/cytnx_core) +message(STATUS " skbuild Installation Prefix: ${SKBUILD_PROJECT_NAME}") diff --git a/cmake/target_sources_local.cmake b/cmake/target_sources_local.cmake new file mode 100644 index 0000000..b048471 --- /dev/null +++ b/cmake/target_sources_local.cmake @@ -0,0 +1,30 @@ +# NOTE: This helper function assumes no generator expressions are used +# for the source files +function(target_sources_local target) + if(POLICY CMP0076) + # New behavior is available, so just forward to it by ensuring + # that we have the policy set to request the new behavior, but + # don't change the policy setting for the calling scope + cmake_policy(PUSH) + cmake_policy(SET CMP0076 NEW) + target_sources(${target} ${ARGN}) + cmake_policy(POP) + return() + endif() + + # Must be using CMake 3.12 or earlier, so simulate the new behavior + unset(_srcList) + get_target_property(_targetSourceDir ${target} SOURCE_DIR) + + foreach(src ${ARGN}) + if(NOT src STREQUAL "PRIVATE" AND + NOT src STREQUAL "PUBLIC" AND + NOT src STREQUAL "INTERFACE" AND + NOT IS_ABSOLUTE "${src}") + # Relative path to source, prepend relative to where target was defined + file(RELATIVE_PATH src "${_targetSourceDir}" "${CMAKE_CURRENT_LIST_DIR}/${src}") + endif() + list(APPEND _srcList ${src}) + endforeach() + target_sources(${target} ${_srcList}) +endfunction() diff --git a/src/cpp/include/test.hpp b/src/cpp/include/test.hpp new file mode 100644 index 0000000..e8345bc --- /dev/null +++ b/src/cpp/include/test.hpp @@ -0,0 +1,9 @@ +#pragma once + +#include + +namespace cytnx_core { + +void test(); + +} diff --git a/src/main.cpp b/src/cpp/pybind/main.cpp similarity index 70% rename from src/main.cpp rename to src/cpp/pybind/main.cpp index fe93813..9a57ecd 100644 --- a/src/main.cpp +++ b/src/cpp/pybind/main.cpp @@ -1,10 +1,10 @@ #include -std::string hello_from_bin() { return "Hello from!"; } +std::string hello_from_bin() { return "Hello from Kai!"; } namespace py = pybind11; -PYBIND11_MODULE(_core, m) { +PYBIND11_MODULE(pycytnx, m) { m.doc() = "pybind11 hello module"; m.def("hello_from_bin", &hello_from_bin, R"pbdoc( diff --git a/src/cpp/src/CMakeLists.txt b/src/cpp/src/CMakeLists.txt new file mode 100644 index 0000000..7c96618 --- /dev/null +++ b/src/cpp/src/CMakeLists.txt @@ -0,0 +1,9 @@ +target_sources_local(cytnx_core + PRIVATE + + # put cpp files here + test.cpp + +) + +#add_subdirectory(storage) diff --git a/src/cpp/src/test.cpp b/src/cpp/src/test.cpp new file mode 100644 index 0000000..c3e8fcb --- /dev/null +++ b/src/cpp/src/test.cpp @@ -0,0 +1,4 @@ +#include "test.hpp" +#include + +void test() { std::cout << "Hello from test" << std::endl; } diff --git a/src/cytnx_core/__init__.py b/src/cytnx_core/__init__.py index e581d39..b919c0f 100644 --- a/src/cytnx_core/__init__.py +++ b/src/cytnx_core/__init__.py @@ -1,4 +1,4 @@ -from cytnx_core._core import hello_from_bin +from cytnx_core.pycytnx import hello_from_bin def hello() -> str: diff --git a/src/cytnx_core/_core.pyi b/src/cytnx_core/pycytnx.pyi similarity index 100% rename from src/cytnx_core/_core.pyi rename to src/cytnx_core/pycytnx.pyi diff --git a/sync.sh b/sync.sh new file mode 100644 index 0000000..263b975 --- /dev/null +++ b/sync.sh @@ -0,0 +1 @@ +uv sync --default-index https://pypi.org/simple diff --git a/version.cmake b/version.cmake new file mode 100644 index 0000000..0f76fd2 --- /dev/null +++ b/version.cmake @@ -0,0 +1,3 @@ +set(CYTNX_CORE_VERSION_MAJOR 0) +set(CYTNX_CORE_VERSION_MINOR 1) +set(CYTNX_CORE_VERSION_PATCH 0)