Skip to content

Commit 3804345

Browse files
committed
Moved from Makefile to CMake, moved run target to script to run dev srv
1 parent 4a80647 commit 3804345

9 files changed

Lines changed: 674 additions & 252 deletions

File tree

.gitignore

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.obj
22
engine
33
.deps
4-
build
4+
build/
55

66
# Generated LSP files
77
compile_commands.json
@@ -12,3 +12,9 @@ logs
1212
# Test server directory
1313
test-server
1414
include/external
15+
16+
# CMake
17+
CMakeCache.txt
18+
CMakeFiles/
19+
cmake_install.cmake
20+
CMakeUserPresets.json

CMakeLists.txt

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
# =============================================================================
2+
# mc-server - Minecraft C++ Server
3+
# =============================================================================
4+
cmake_minimum_required(VERSION 3.21)
5+
6+
project(mc-server
7+
VERSION 1.0.0
8+
DESCRIPTION "Minecraft Server Implementation in C++"
9+
LANGUAGES CXX
10+
)
11+
12+
# =============================================================================
13+
# Build Configuration
14+
# =============================================================================
15+
16+
# C++20 Standard
17+
set(CMAKE_CXX_STANDARD 20)
18+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
19+
set(CMAKE_CXX_EXTENSIONS OFF)
20+
21+
# Export compile_commands.json for LSP support
22+
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
23+
24+
# Set default build type if not specified
25+
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
26+
message(STATUS "Setting build type to 'Release' as none was specified.")
27+
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build." FORCE)
28+
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
29+
endif()
30+
31+
# =============================================================================
32+
# Dependencies
33+
# =============================================================================
34+
35+
include(FetchContent)
36+
37+
# Try to find system zlib first, otherwise download it
38+
find_package(ZLIB QUIET)
39+
40+
if(NOT ZLIB_FOUND)
41+
message(STATUS "System zlib not found, downloading via FetchContent...")
42+
FetchContent_Declare(
43+
zlib
44+
GIT_REPOSITORY https://github.com/madler/zlib.git
45+
GIT_TAG v1.3.1
46+
)
47+
# Prevent zlib from installing itself
48+
set(ZLIB_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
49+
set(SKIP_INSTALL_ALL ON CACHE BOOL "" FORCE)
50+
FetchContent_MakeAvailable(zlib)
51+
52+
# Create an alias target to match the system zlib interface
53+
if(NOT TARGET ZLIB::ZLIB)
54+
add_library(ZLIB::ZLIB ALIAS zlibstatic)
55+
endif()
56+
else()
57+
message(STATUS "Found system zlib: ${ZLIB_VERSION_STRING}")
58+
endif()
59+
60+
# =============================================================================
61+
# Source Files
62+
# =============================================================================
63+
64+
# Automatically discover source files
65+
file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS
66+
"${CMAKE_SOURCE_DIR}/src/*.cpp"
67+
)
68+
69+
file(GLOB_RECURSE HEADERS CONFIGURE_DEPENDS
70+
"${CMAKE_SOURCE_DIR}/include/*.hpp"
71+
)
72+
73+
# =============================================================================
74+
# Main Executable
75+
# =============================================================================
76+
77+
add_executable(${PROJECT_NAME} ${SOURCES} ${HEADERS})
78+
79+
# Include directories
80+
target_include_directories(${PROJECT_NAME} PRIVATE
81+
${CMAKE_SOURCE_DIR}/include
82+
${CMAKE_SOURCE_DIR}/include/data
83+
${CMAKE_SOURCE_DIR}/include/network
84+
${CMAKE_SOURCE_DIR}/include/world
85+
${CMAKE_SOURCE_DIR}/include/lib
86+
)
87+
88+
# Link libraries
89+
if(ZLIB_FOUND)
90+
target_link_libraries(${PROJECT_NAME} PRIVATE ZLIB::ZLIB)
91+
else()
92+
target_link_libraries(${PROJECT_NAME} PRIVATE zlibstatic)
93+
target_include_directories(${PROJECT_NAME} PRIVATE
94+
${zlib_SOURCE_DIR}
95+
${zlib_BINARY_DIR}
96+
)
97+
endif()
98+
99+
# Platform-specific configurations
100+
if(WIN32)
101+
# Windows socket library
102+
target_link_libraries(${PROJECT_NAME} PRIVATE ws2_32)
103+
endif()
104+
105+
# =============================================================================
106+
# Compiler Warnings
107+
# =============================================================================
108+
109+
include(cmake/CompilerWarnings.cmake)
110+
set_project_warnings(${PROJECT_NAME})
111+
112+
# =============================================================================
113+
# Build Type Specific Flags
114+
# =============================================================================
115+
116+
# Debug definitions
117+
target_compile_definitions(${PROJECT_NAME} PRIVATE
118+
$<$<CONFIG:Debug>:DEBUG>
119+
$<$<CONFIG:Release>:NDEBUG>
120+
$<$<CONFIG:RelWithDebInfo>:NDEBUG>
121+
$<$<CONFIG:MinSizeRel>:NDEBUG>
122+
)
123+
124+
# =============================================================================
125+
# Installation
126+
# =============================================================================
127+
128+
include(GNUInstallDirs)
129+
130+
install(TARGETS ${PROJECT_NAME}
131+
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
132+
)
133+
134+
# =============================================================================
135+
# Project Information
136+
# =============================================================================
137+
138+
message(STATUS "")
139+
message(STATUS "========================================")
140+
message(STATUS " ${PROJECT_NAME} v${PROJECT_VERSION}")
141+
message(STATUS "========================================")
142+
message(STATUS " Build type: ${CMAKE_BUILD_TYPE}")
143+
message(STATUS " C++ Standard: ${CMAKE_CXX_STANDARD}")
144+
message(STATUS " Compiler: ${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}")
145+
message(STATUS " Source files: ${CMAKE_SOURCE_DIR}/src")
146+
message(STATUS " Install prefix: ${CMAKE_INSTALL_PREFIX}")
147+
message(STATUS "========================================")
148+
message(STATUS "")

CMakePresets.json

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
{
2+
"version": 6,
3+
"cmakeMinimumRequired": {
4+
"major": 3,
5+
"minor": 21,
6+
"patch": 0
7+
},
8+
"configurePresets": [
9+
{
10+
"name": "base",
11+
"hidden": true,
12+
"generator": "Ninja",
13+
"binaryDir": "${sourceDir}/build/${presetName}",
14+
"cacheVariables": {
15+
"CMAKE_EXPORT_COMPILE_COMMANDS": "ON"
16+
}
17+
},
18+
{
19+
"name": "debug",
20+
"displayName": "Debug",
21+
"description": "Debug build with no optimizations",
22+
"inherits": "base",
23+
"cacheVariables": {
24+
"CMAKE_BUILD_TYPE": "Debug"
25+
}
26+
},
27+
{
28+
"name": "release",
29+
"displayName": "Release",
30+
"description": "Optimized release build",
31+
"inherits": "base",
32+
"cacheVariables": {
33+
"CMAKE_BUILD_TYPE": "Release"
34+
}
35+
},
36+
{
37+
"name": "relwithdebinfo",
38+
"displayName": "Release with Debug Info",
39+
"description": "Optimized build with debug symbols",
40+
"inherits": "base",
41+
"cacheVariables": {
42+
"CMAKE_BUILD_TYPE": "RelWithDebInfo"
43+
}
44+
}
45+
],
46+
"buildPresets": [
47+
{
48+
"name": "debug",
49+
"displayName": "Debug Build",
50+
"configurePreset": "debug"
51+
},
52+
{
53+
"name": "release",
54+
"displayName": "Release Build",
55+
"configurePreset": "release"
56+
},
57+
{
58+
"name": "relwithdebinfo",
59+
"displayName": "RelWithDebInfo Build",
60+
"configurePreset": "relwithdebinfo"
61+
}
62+
],
63+
"workflowPresets": [
64+
{
65+
"name": "debug",
66+
"displayName": "Debug Workflow",
67+
"description": "Configure and build in debug mode",
68+
"steps": [
69+
{
70+
"type": "configure",
71+
"name": "debug"
72+
},
73+
{
74+
"type": "build",
75+
"name": "debug"
76+
}
77+
]
78+
},
79+
{
80+
"name": "release",
81+
"displayName": "Release Workflow",
82+
"description": "Configure and build in release mode",
83+
"steps": [
84+
{
85+
"type": "configure",
86+
"name": "release"
87+
},
88+
{
89+
"type": "build",
90+
"name": "release"
91+
}
92+
]
93+
}
94+
]
95+
}

0 commit comments

Comments
 (0)