Skip to content

Commit

Permalink
finished setting up repo
Browse files Browse the repository at this point in the history
  • Loading branch information
kaihsin committed Nov 29, 2024
1 parent 9546876 commit c5281fd
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 7 deletions.
87 changes: 83 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
$<INSTALL_INTERFACE:include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/include>
)
# 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}")
30 changes: 30 additions & 0 deletions cmake/target_sources_local.cmake
Original file line number Diff line number Diff line change
@@ -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()
9 changes: 9 additions & 0 deletions src/cpp/include/test.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#include <cstring>

namespace cytnx_core {

void test();

}
4 changes: 2 additions & 2 deletions src/main.cpp → src/cpp/pybind/main.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include <pybind11/pybind11.h>

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(
Expand Down
9 changes: 9 additions & 0 deletions src/cpp/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
target_sources_local(cytnx_core
PRIVATE

# put cpp files here
test.cpp

)

#add_subdirectory(storage)
4 changes: 4 additions & 0 deletions src/cpp/src/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#include "test.hpp"
#include <iostream>

void test() { std::cout << "Hello from test" << std::endl; }
2 changes: 1 addition & 1 deletion src/cytnx_core/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from cytnx_core._core import hello_from_bin
from cytnx_core.pycytnx import hello_from_bin


def hello() -> str:
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions sync.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
uv sync --default-index https://pypi.org/simple
3 changes: 3 additions & 0 deletions version.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
set(CYTNX_CORE_VERSION_MAJOR 0)
set(CYTNX_CORE_VERSION_MINOR 1)
set(CYTNX_CORE_VERSION_PATCH 0)

0 comments on commit c5281fd

Please sign in to comment.