From c461afa50c4497302d8a2b4c9e65647598d06b4a Mon Sep 17 00:00:00 2001
From: "Xuefeng Ding (IHEP)" <dingxf@ihep.ac.cn>
Date: Wed, 13 Nov 2024 01:49:34 +0800
Subject: [PATCH] good template now

---
 CMakeLists.txt                                |  7 ++---
 README.md                                     | 31 ++++++++++++++++++-
 cpp/CMakeLists.txt                            | 11 +++----
 pyproject.toml                                | 11 +++++--
 {src => python}/myproj/simulation/__init__.py |  0
 python/myproj/simulation/_csrc/CMakeLists.txt |  9 ++++++
 .../myproj/simulation/_csrc/_hello_python.cc  |  2 +-
 {src => python}/myproj/simulation/core.py     |  0
 src/myproj/simulation/_csrc/CMakeLists.txt    |  5 ---
 9 files changed, 57 insertions(+), 19 deletions(-)
 rename {src => python}/myproj/simulation/__init__.py (100%)
 create mode 100644 python/myproj/simulation/_csrc/CMakeLists.txt
 rename src/myproj/simulation/_csrc/hello_python.cc => python/myproj/simulation/_csrc/_hello_python.cc (89%)
 rename {src => python}/myproj/simulation/core.py (100%)
 delete mode 100644 src/myproj/simulation/_csrc/CMakeLists.txt

diff --git a/CMakeLists.txt b/CMakeLists.txt
index f11fe38..708673a 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -3,10 +3,9 @@ project("${SKBUILD_PROJECT_NAME}"
     LANGUAGES CXX
     VERSION "${SKBUILD_PROJECT_VERSION}")
 
-# include(GNUInstallDirs)
-set(PYBIND11_FINDPYTHON ON)
-find_package(pybind11 CONFIG REQUIRED)
+set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
 
+# include(GNUInstallDirs)
 add_subdirectory(cpp)
 
-add_subdirectory(src/myproj/simulation/_csrc)
\ No newline at end of file
+add_subdirectory(python/myproj/simulation/_csrc)
\ No newline at end of file
diff --git a/README.md b/README.md
index 66ad80d..e21ef73 100644
--- a/README.md
+++ b/README.md
@@ -8,4 +8,33 @@ use scikit-build-core to setup a python-cmake project that can be installed with
 
 ## Conclusion
 
-cannot make packages like import myproj.simulation. It replaces "." with "_"
+- cannot make packages like import myproj.simulation. It replaces "." with "_"
+- most of the case, check following
+```cmake
+install(FILES ${CMAKE_CURRENT_BINARY_DIR}/my_main.py DESTINATION .)
+```
+and set
+```
+[tool.scikit-build]
+wheel.install-dir = "kk/ss"
+```
+
+
+## special variable
+
+
+- `${SKBUILD_PLATLIB_DIR}`: The original platlib directory. Anything here goes
+  directly to site-packages when a wheel is installed.
+- `${SKBUILD_DATA_DIR}`: The data directory. Anything here goes to the root of
+  the environment when a wheel is installed (use with care).
+- `${SKBUILD_HEADERS_DIR}`: The header directory. Anything in here gets
+  installed to Python's header directory.
+- `${SKBUILD_SCRIPTS_DIR}`: The scripts directory. Anything placed in here will
+  go to `bin` (Unix) or `Scripts` (Windows).
+- `${SKBUILD_METADATA_DIR}`: The dist-info directory. Licenses go in the
+  `licenses` subdirectory. _Note that CMake is not run in the
+  `prepare_metadata_\*` hooks, so anything written to this directory will only
+  be present when writing wheels.\_
+- `${SKBUILD_NULL_DIR}`: Anything installed here will not be placed in the
+  wheel.
+
diff --git a/cpp/CMakeLists.txt b/cpp/CMakeLists.txt
index 8bddd5f..add6489 100644
--- a/cpp/CMakeLists.txt
+++ b/cpp/CMakeLists.txt
@@ -2,18 +2,17 @@ add_library(hello SHARED src/hello.cc)
 target_include_directories(hello PUBLIC
     $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
     $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
-install(
-    TARGETS hello
-    LIBRARY DESTINATION ${SKBUILD_SCRIPTS_DIR})
+install(TARGETS hello LIBRARY DESTINATION .)
 
 add_library(complex SHARED src/complex.cc)
 target_include_directories(complex PUBLIC
     $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
     $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
 target_link_libraries(complex PRIVATE hello)
-install(
-    TARGETS complex
-    LIBRARY DESTINATION ${SKBUILD_SCRIPTS_DIR})
+install(TARGETS complex LIBRARY DESTINATION .)
+set_target_properties(complex PROPERTIES
+    INSTALL_RPATH "$ORIGIN"
+)
 
 # install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/include/"
 # DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
diff --git a/pyproject.toml b/pyproject.toml
index be223eb..13ee28f 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -3,7 +3,7 @@ requires = ["scikit-build-core", "pybind11"]
 build-backend = "scikit_build_core.build"
 
 [project]
-name = "myproj.simulation"
+name = "myproj_simulation"
 version = "0.2.1"
 description = "A great package."
 readme = "README.md"
@@ -36,4 +36,11 @@ Homepage = "https://github.com/organization/package"
 Documentation = "https://package.readthedocs.io/"
 "Bug Tracker" = "https://github.com/organization/package/issues"
 Discussions = "https://github.com/organization/package/discussions"
-Changelog = "https://package.readthedocs.io/en/latest/changelog.html"
\ No newline at end of file
+Changelog = "https://package.readthedocs.io/en/latest/changelog.html"
+
+[tool.scikit-build.wheel.packages]
+"myproj/simulation" = "python/myproj/simulation"
+
+[tool.scikit-build]
+wheel.install-dir = "myproj/simulation"
+wheel.exclude = ["CMakeLists.txt","*.cc","*.h"]
\ No newline at end of file
diff --git a/src/myproj/simulation/__init__.py b/python/myproj/simulation/__init__.py
similarity index 100%
rename from src/myproj/simulation/__init__.py
rename to python/myproj/simulation/__init__.py
diff --git a/python/myproj/simulation/_csrc/CMakeLists.txt b/python/myproj/simulation/_csrc/CMakeLists.txt
new file mode 100644
index 0000000..d5baf7b
--- /dev/null
+++ b/python/myproj/simulation/_csrc/CMakeLists.txt
@@ -0,0 +1,9 @@
+find_package(Python COMPONENTS Interpreter Development.Module REQUIRED)
+find_package(pybind11 CONFIG REQUIRED)
+
+pybind11_add_module(_hello MODULE _hello_python.cc)
+target_link_libraries(_hello PRIVATE hello complex)
+install(TARGETS _hello DESTINATION _csrc)
+set_target_properties(_hello PROPERTIES
+    INSTALL_RPATH "$ORIGIN/.."
+)
\ No newline at end of file
diff --git a/src/myproj/simulation/_csrc/hello_python.cc b/python/myproj/simulation/_csrc/_hello_python.cc
similarity index 89%
rename from src/myproj/simulation/_csrc/hello_python.cc
rename to python/myproj/simulation/_csrc/_hello_python.cc
index e14360a..feaeae7 100644
--- a/src/myproj/simulation/_csrc/hello_python.cc
+++ b/python/myproj/simulation/_csrc/_hello_python.cc
@@ -4,7 +4,7 @@
 
 namespace py = pybind11;
 
-PYBIND11_MODULE(hello_python, m) {
+PYBIND11_MODULE(_hello, m) {
   m.doc() = "pybind11 example plugin"; // optional module docstring
 
   m.def("add", &add, "A function which adds two numbers");
diff --git a/src/myproj/simulation/core.py b/python/myproj/simulation/core.py
similarity index 100%
rename from src/myproj/simulation/core.py
rename to python/myproj/simulation/core.py
diff --git a/src/myproj/simulation/_csrc/CMakeLists.txt b/src/myproj/simulation/_csrc/CMakeLists.txt
deleted file mode 100644
index 53aa04f..0000000
--- a/src/myproj/simulation/_csrc/CMakeLists.txt
+++ /dev/null
@@ -1,5 +0,0 @@
-pybind11_add_module(_hello MODULE hello_python.cc)
-
-target_link_libraries(_hello PRIVATE hello complex)
-
-install(TARGETS _hello DESTINATION ${SKBUILD_PROJECT_NAME})
\ No newline at end of file
-- 
GitLab