Skip to content

Commit e49505d

Browse files
committed
Add vcpkg json to pybind and cmake projects
1 parent 09f727b commit e49505d

File tree

14 files changed

+378
-0
lines changed

14 files changed

+378
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
cmake_minimum_required(VERSION 3.20.0)
2+
project(hatch-cpp-test-project-basic VERSION "0.1.0")
3+
set(CMAKE_CXX_STANDARD 20)
4+
include(CheckCCompilerFlag)
5+
include(CheckLinkerFlag)
6+
7+
if(${CMAKE_SYSTEM_NAME} MATCHES "Windows")
8+
set(WIN32 ON)
9+
set(MACOS OFF)
10+
set(LINUX OFF)
11+
elseif(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
12+
set(WIN32 OFF)
13+
set(MACOS ON)
14+
set(LINUX OFF)
15+
else()
16+
set(WIN32 OFF)
17+
set(MACOS OFF)
18+
set(LINUX ON)
19+
endif()
20+
21+
option(CMAKE_BUILD_TYPE "Release/Debug build" RELEASE)
22+
option(HATCH_CPP_TEST_PROJECT_BASIC_BUILD_TESTS "Build tests" OFF)
23+
option(HATCH_CPP_TEST_PROJECT_BASIC_MANYLINUX "Build for python's manylinux setup" OFF)
24+
25+
string(TOLOWER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_LOWER)
26+
27+
set(BUILD_SHARED_LIBS TRUE)
28+
set(CMAKE_MACOSX_RPATH TRUE)
29+
set(CMAKE_SKIP_RPATH FALSE)
30+
set(CMAKE_SKIP_BUILD_RPATH FALSE)
31+
set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
32+
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
33+
set(CMAKE_INSTALL_NAME_DIR "@rpath")
34+
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
35+
36+
string(REGEX REPLACE "[ ]*-O[^ ]+[ ]*" " " CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
37+
string(REGEX REPLACE "[ ]*-Wl,-O2 -Wl,[^ ]+[ ]*" " " CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}")
38+
string(REGEX REPLACE "[ ]*-Wl,-O2 -Wl,[^ ]+[ ]*" " " CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}")
39+
40+
41+
if(MACOS)
42+
set(CMAKE_THREAD_LIBS_INIT "-lpthread")
43+
set(CMAKE_HAVE_THREADS_LIBRARY 1)
44+
set(CMAKE_USE_WIN32_THREADS_INIT 0)
45+
set(CMAKE_USE_PTHREADS_INIT 1)
46+
set(THREADS_PREFER_PTHREAD_FLAG ON)
47+
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -undefined dynamic_lookup")
48+
endif()
49+
50+
51+
if(MACOS)
52+
set(CMAKE_INSTALL_RPATH "@loader_path/")
53+
elseif(LINUX)
54+
set(CMAKE_INSTALL_RPATH "\$ORIGIN")
55+
endif()
56+
57+
if(WIN32)
58+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc /MP /bigobj")
59+
foreach(warning 4244 4251 4267 4275 4290 4786 4305 4996)
60+
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd${warning}")
61+
endforeach(warning)
62+
else()
63+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} \
64+
-g \
65+
-Wall \
66+
-Werror \
67+
-Wno-deprecated-declarations \
68+
-Wno-deprecated \
69+
")
70+
endif()
71+
72+
73+
find_package(Python ${CSP_PYTHON_VERSION} EXACT REQUIRED COMPONENTS Interpreter Development.Module)
74+
link_directories(${Python_LIBRARY_DIRS})
75+
include_directories(${Python_INCLUDE_DIRS})
76+
77+
set(CMAKE_SHARED_LIBRARY_PREFIX "")
78+
if(NOT WIN32)
79+
set(CMAKE_SHARED_LIBRARY_SUFFIX .so)
80+
else()
81+
set(CMAKE_SHARED_LIBRARY_SUFFIX .pyd)
82+
endif()
83+
84+
include_directories("${CMAKE_SOURCE_DIR}/cpp")
85+
86+
add_library(extension SHARED cpp/project/basic.cpp)
87+
set_target_properties(extension PROPERTIES PUBLIC_HEADER cpp/project/basic.hpp)
88+
install(TARGETS extension
89+
PUBLIC_HEADER DESTINATION project/include/project
90+
RUNTIME DESTINATION project/
91+
LIBRARY DESTINATION project/
92+
)
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
# CMAKE generated file: DO NOT EDIT!
2+
# Generated by "Unix Makefiles" Generator, CMake Version 3.31
3+
4+
# Default target executed when no arguments are given to make.
5+
default_target: all
6+
.PHONY : default_target
7+
8+
# Allow only one "make -f Makefile2" at a time, but pass parallelism.
9+
.NOTPARALLEL:
10+
11+
#=============================================================================
12+
# Special targets provided by cmake.
13+
14+
# Disable implicit rules so canonical targets will work.
15+
.SUFFIXES:
16+
17+
# Disable VCS-based implicit rules.
18+
% : %,v
19+
20+
# Disable VCS-based implicit rules.
21+
% : RCS/%
22+
23+
# Disable VCS-based implicit rules.
24+
% : RCS/%,v
25+
26+
# Disable VCS-based implicit rules.
27+
% : SCCS/s.%
28+
29+
# Disable VCS-based implicit rules.
30+
% : s.%
31+
32+
.SUFFIXES: .hpux_make_needs_suffix_list
33+
34+
# Command-line flag to silence nested $(MAKE).
35+
$(VERBOSE)MAKESILENT = -s
36+
37+
#Suppress display of executed commands.
38+
$(VERBOSE).SILENT:
39+
40+
# A target that is always out of date.
41+
cmake_force:
42+
.PHONY : cmake_force
43+
44+
#=============================================================================
45+
# Set environment variables for the build.
46+
47+
# The shell in which to execute make rules.
48+
SHELL = /bin/sh
49+
50+
# The CMake executable.
51+
CMAKE_COMMAND = /opt/homebrew/bin/cmake
52+
53+
# The command to remove a file.
54+
RM = /opt/homebrew/bin/cmake -E rm -f
55+
56+
# Escaping for special characters.
57+
EQUALS = =
58+
59+
# The top-level source directory on which CMake was run.
60+
CMAKE_SOURCE_DIR = /Users/timkpaine/Developer/projects/templates/hatch-cpp/hatch_cpp/tests/test_project_cmake
61+
62+
# The top-level build directory on which CMake was run.
63+
CMAKE_BINARY_DIR = /Users/timkpaine/Developer/projects/templates/hatch-cpp/hatch_cpp/tests/test_project_cmake
64+
65+
#=============================================================================
66+
# Targets provided globally by CMake.
67+
68+
# Special rule for the target edit_cache
69+
edit_cache:
70+
@$(CMAKE_COMMAND) -E cmake_echo_color "--switch=$(COLOR)" --cyan "Running CMake cache editor..."
71+
/opt/homebrew/bin/ccmake -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)
72+
.PHONY : edit_cache
73+
74+
# Special rule for the target edit_cache
75+
edit_cache/fast: edit_cache
76+
.PHONY : edit_cache/fast
77+
78+
# Special rule for the target rebuild_cache
79+
rebuild_cache:
80+
@$(CMAKE_COMMAND) -E cmake_echo_color "--switch=$(COLOR)" --cyan "Running CMake to regenerate build system..."
81+
/opt/homebrew/bin/cmake --regenerate-during-build -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)
82+
.PHONY : rebuild_cache
83+
84+
# Special rule for the target rebuild_cache
85+
rebuild_cache/fast: rebuild_cache
86+
.PHONY : rebuild_cache/fast
87+
88+
# The main all target
89+
all: cmake_check_build_system
90+
$(CMAKE_COMMAND) -E cmake_progress_start /Users/timkpaine/Developer/projects/templates/hatch-cpp/hatch_cpp/tests/test_project_cmake/CMakeFiles /Users/timkpaine/Developer/projects/templates/hatch-cpp/hatch_cpp/tests/test_project_cmake//CMakeFiles/progress.marks
91+
$(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 all
92+
$(CMAKE_COMMAND) -E cmake_progress_start /Users/timkpaine/Developer/projects/templates/hatch-cpp/hatch_cpp/tests/test_project_cmake/CMakeFiles 0
93+
.PHONY : all
94+
95+
# The main clean target
96+
clean:
97+
$(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 clean
98+
.PHONY : clean
99+
100+
# The main clean target
101+
clean/fast: clean
102+
.PHONY : clean/fast
103+
104+
# Prepare targets for installation.
105+
preinstall: all
106+
$(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 preinstall
107+
.PHONY : preinstall
108+
109+
# Prepare targets for installation.
110+
preinstall/fast:
111+
$(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 preinstall
112+
.PHONY : preinstall/fast
113+
114+
# clear depends
115+
depend:
116+
$(CMAKE_COMMAND) -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 1
117+
.PHONY : depend
118+
119+
# Help Target
120+
help:
121+
@echo "The following are some of the valid targets for this Makefile:"
122+
@echo "... all (the default if no target is provided)"
123+
@echo "... clean"
124+
@echo "... depend"
125+
@echo "... edit_cache"
126+
@echo "... rebuild_cache"
127+
.PHONY : help
128+
129+
130+
131+
#=============================================================================
132+
# Special targets to cleanup operation of make.
133+
134+
# Special rule to run CMake to check the build system integrity.
135+
# No rule that depends on this can have commands that come from listfiles
136+
# because they might be regenerated.
137+
cmake_check_build_system:
138+
$(CMAKE_COMMAND) -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 0
139+
.PHONY : cmake_check_build_system
140+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include "project/basic.hpp"
2+
3+
PyObject* hello(PyObject*, PyObject*) {
4+
return PyUnicode_FromString("A string");
5+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#pragma once
2+
#include "Python.h"
3+
4+
PyObject* hello(PyObject*, PyObject*);
5+
6+
static PyMethodDef extension_methods[] = {
7+
{"hello", (PyCFunction)hello, METH_NOARGS},
8+
{nullptr, nullptr, 0, nullptr}
9+
};
10+
11+
static PyModuleDef extension_module = {
12+
PyModuleDef_HEAD_INIT, "extension", "extension", -1, extension_methods};
13+
14+
PyMODINIT_FUNC PyInit_extension(void) {
15+
Py_Initialize();
16+
return PyModule_Create(&extension_module);
17+
}

hatch_cpp/tests/test_project_cmake_vcpkg/project/__init__.py

Whitespace-only changes.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#pragma once
2+
#include "Python.h"
3+
4+
PyObject* hello(PyObject*, PyObject*);
5+
6+
static PyMethodDef extension_methods[] = {
7+
{"hello", (PyCFunction)hello, METH_NOARGS},
8+
{nullptr, nullptr, 0, nullptr}
9+
};
10+
11+
static PyModuleDef extension_module = {
12+
PyModuleDef_HEAD_INIT, "extension", "extension", -1, extension_methods};
13+
14+
PyMODINIT_FUNC PyInit_extension(void) {
15+
Py_Initialize();
16+
return PyModule_Create(&extension_module);
17+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
[build-system]
2+
requires = ["hatchling>=1.20"]
3+
build-backend = "hatchling.build"
4+
5+
[project]
6+
name = "hatch-cpp-test-project-basic"
7+
description = "Basic test project for hatch-cpp"
8+
version = "0.1.0"
9+
requires-python = ">=3.9"
10+
dependencies = [
11+
"hatchling>=1.20",
12+
"hatch-cpp",
13+
]
14+
15+
[tool.hatch.build]
16+
artifacts = [
17+
"project/*.dll",
18+
"project/*.dylib",
19+
"project/*.so",
20+
]
21+
22+
[tool.hatch.build.sources]
23+
src = "/"
24+
25+
[tool.hatch.build.targets.sdist]
26+
packages = ["project"]
27+
28+
[tool.hatch.build.targets.wheel]
29+
packages = ["project"]
30+
31+
[tool.hatch.build.hooks.hatch-cpp]
32+
verbose = true
33+
34+
[tool.hatch.build.hooks.hatch-cpp.cmake]
35+
root = "CMakeLists.txt"
36+
cmake_args = {"BUILD_TESTS" = "OFF"}
37+
include_flags = {"python_version" = true}
38+
[tool.hatch.build.hooks.hatch-cpp.cmake.cmake_env_args]
39+
linux = {"MANYLINUX" = "ON"}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "main",
3+
"version-string": "latest",
4+
"dependencies": [
5+
"arrow"
6+
],
7+
"builtin-baseline": "b94ade01f19e4436d8c8a16a5c52e8c802ef67dd"
8+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include "project/basic.hpp"
2+
3+
std::string hello() {
4+
return "A string";
5+
}
6+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#pragma once
2+
#include <pybind11/pybind11.h>
3+
#include <string>
4+
5+
std::string hello();
6+
7+
PYBIND11_MODULE(extension, m) {
8+
m.def("hello", &hello);
9+
}

0 commit comments

Comments
 (0)