-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
140 lines (121 loc) · 4.82 KB
/
CMakeLists.txt
File metadata and controls
140 lines (121 loc) · 4.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
cmake_minimum_required(VERSION 3.22)
project(PacketHandling)
set(CMAKE_CXX_STANDARD 20)
# --------------------------
# PACKET GENERATOR SETTINGS
# --------------------------
# Python required to run the generator
find_package(Python3 REQUIRED)
set(PACKET_DEF_FILE ${CMAKE_SOURCE_DIR}/include/packets/PacketList.packetdef)
set(GENERATED_DIR ${CMAKE_SOURCE_DIR})
# Run the generator to create Packets.cpp and Packets.hpp
add_custom_command(
OUTPUT
${GENERATED_DIR}/src/packets/PacketsData.cpp
${GENERATED_DIR}/include/packets/PacketsData.hpp
${GENERATED_DIR}/include/packets/PacketMacros.hpp
COMMAND ${CMAKE_COMMAND} -E make_directory ${GENERATED_DIR}
COMMAND ${Python3_EXECUTABLE} ${CMAKE_SOURCE_DIR}/scripts/packet_gen.py
${PACKET_DEF_FILE} ${GENERATED_DIR}
DEPENDS ${PACKET_DEF_FILE} ${CMAKE_SOURCE_DIR}/scripts/packet_gen.py
COMMENT "Generating packet classes from PacketList.packetdef"
)
add_custom_target(GeneratePackets
DEPENDS
${GENERATED_DIR}/src/packets/PacketsData.cpp
${GENERATED_DIR}/include/packets/PacketsData.hpp
${GENERATED_DIR}/include/packets/PacketMacros.hpp
)
# --------------------------
# PYBIND11 + PACKET LOGIC
# --------------------------
find_package(Python3 REQUIRED COMPONENTS Interpreter Development)
add_subdirectory(extern/pybind11)
# Gather your source files
file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS src/*.cpp)
file(GLOB_RECURSE HEADERS CONFIGURE_DEPENDS include/*.h)
# Include the generated files
list(APPEND SOURCES ${GENERATED_CPP})
list(APPEND HEADERS ${GENERATED_HPP})
# Create the static library
add_library(PacketHandling STATIC ${SOURCES} ${HEADERS})
set_target_properties(PacketHandling PROPERTIES POSITION_INDEPENDENT_CODE ON)
add_dependencies(PacketHandling GeneratePackets)
# Include both your own headers and the generated folder
target_include_directories(PacketHandling
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include
${GENERATED_DIR}
)
# Pybind11 module
pybind11_add_module(packet_handler bindings/packet_handler.cpp)
target_link_libraries(packet_handler PRIVATE PacketHandling)
target_include_directories(packet_handler
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/include
${GENERATED_DIR}
)
if (WIN32)
set(PYEXT_SUFFIX ".pyd")
else()
set(PYEXT_SUFFIX ".so")
endif()
set(PYEXT_NAME "_packet_handler${PYEXT_SUFFIX}")
# Apply the name to the target
set_target_properties(packet_handler PROPERTIES OUTPUT_NAME "_packet_handler" SUFFIX "${PYEXT_SUFFIX}")
install(TARGETS packet_handler
LIBRARY DESTINATION packet_handler
)
install(FILES packet_handler/__init__.py DESTINATION packet_handler)
target_link_options(packet_handler PRIVATE -static-libgcc -static-libstdc++)
# Stub generation
find_program(PYBIND11_STUBGEN_EXECUTABLE pybind11-stubgen)
message(STATUS "Using Python executable: ${Python3_EXECUTABLE}")
if (PYBIND11_STUBGEN_EXECUTABLE)
add_custom_command(
TARGET packet_handler
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
$<TARGET_FILE:packet_handler>
${CMAKE_CURRENT_SOURCE_DIR}/packet_handler/${PYEXT_NAME}
COMMENT "Copying ${PYEXT_SUFFIX} to packet_handler/ for stub generation"
)
add_custom_command(
TARGET packet_handler
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
$<TARGET_FILE:packet_handler>
${CMAKE_CURRENT_BINARY_DIR}/packet_handler/${PYEXT_NAME}
COMMENT "Copying ${PYEXT_SUFFIX} to packet_handler/ for stub generation"
)
# Copy the Python package's __init__.py into the build folder
add_custom_command(
TARGET packet_handler
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${CMAKE_CURRENT_SOURCE_DIR}/packet_handler/__init__.py
${CMAKE_CURRENT_BINARY_DIR}/packet_handler/__init__.py
COMMENT "Copying __init__.py to build/packet_handler/"
)
message(STATUS $<TARGET_FILE:packet_handler>)
message(STATUS ${CMAKE_CURRENT_SOURCE_DIR}/packet_handler/${PYEXT_NAME})
add_custom_command(
TARGET packet_handler
POST_BUILD
COMMAND ${Python3_EXECUTABLE} ${CMAKE_SOURCE_DIR}/scripts/run_stubgen.py
COMMENT "Generating .pyi stub file for packet_handler"
)
add_custom_command(
TARGET packet_handler
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E remove
${CMAKE_CURRENT_SOURCE_DIR}/packet_handler/${PYEXT_NAME}
COMMENT "Cleaning up copied ${PYEXT_NAME}"
)
install(FILES
${CMAKE_CURRENT_BINARY_DIR}/generated_stubs/packet_handler/_packet_handler.pyi
DESTINATION packet_handler
)
else()
message(WARNING "pybind11-stubgen not found; .pyi stub will not be generated")
endif()