cmake_minimum_required(VERSION 3.20) project(AIServer LANGUAGES C CXX) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(GAMEPLAY_FILE "${CMAKE_CURRENT_SOURCE_DIR}/../Config/Gameplay/greenfield.json") set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${GAMEPLAY_FILE}") file(READ "${GAMEPLAY_FILE}" GAMEPLAY_JSON) file(SHA256 "${GAMEPLAY_FILE}" GAMEPLAY_HASH) string(SUBSTRING "${GAMEPLAY_HASH}" 0 12 GAMEPLAY_HASH_SHORT) set(GAMEPLAY_VERSION "greenfield-1-${GAMEPLAY_HASH_SHORT}") # MSVC caps a single string literal at 16380 bytes; emit adjacent chunks that concatenate back # to the exact file bytes (splitting at a fixed byte offset never rewrites any character). string(LENGTH "${GAMEPLAY_JSON}" GAMEPLAY_JSON_LENGTH) set(GAMEPLAY_JSON_CHUNKS "") set(GAMEPLAY_JSON_OFFSET 0) while(GAMEPLAY_JSON_OFFSET LESS GAMEPLAY_JSON_LENGTH) string(SUBSTRING "${GAMEPLAY_JSON}" ${GAMEPLAY_JSON_OFFSET} 8000 GAMEPLAY_JSON_PART) string(APPEND GAMEPLAY_JSON_CHUNKS "R\"GAMECONFIG(${GAMEPLAY_JSON_PART})GAMECONFIG\"\n") math(EXPR GAMEPLAY_JSON_OFFSET "${GAMEPLAY_JSON_OFFSET} + 8000") endwhile() configure_file(src/game_config.hpp.in generated/game_config_data.hpp @ONLY) include_directories("${CMAKE_CURRENT_BINARY_DIR}/generated") add_executable(aiserver src/main.cpp src/db.cpp src/game.cpp src/game_config.cpp src/auth.cpp src/http_ws_server.cpp src/maintenance.cpp src/crypto.cpp third_party/sqlite3.c ) target_include_directories(aiserver PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src ${CMAKE_CURRENT_SOURCE_DIR}/third_party ) # SQLite: 关闭部分扩展,加快编译 target_compile_definitions(aiserver PRIVATE SQLITE_OMIT_LOAD_EXTENSION SQLITE_THREADSAFE=1 _CRT_SECURE_NO_WARNINGS ) if(WIN32) target_link_libraries(aiserver PRIVATE ws2_32 bcrypt) # sqlite3.c 是 C,MSVC 下关闭部分告警 if(MSVC) target_compile_options(aiserver PRIVATE /W3 /utf-8) set_source_files_properties(third_party/sqlite3.c PROPERTIES COMPILE_FLAGS "/W0") endif() else() target_link_libraries(aiserver PRIVATE pthread dl) endif() # 运行时默认数据目录:可执行文件旁的 data/ add_custom_command(TARGET aiserver POST_BUILD COMMAND ${CMAKE_COMMAND} -E make_directory $/data ) include(CTest) if(BUILD_TESTING) add_executable(game_tests tests/game_tests.cpp src/db.cpp src/game.cpp src/game_config.cpp third_party/sqlite3.c) target_include_directories(game_tests PRIVATE src third_party) target_compile_definitions(game_tests PRIVATE SQLITE_OMIT_LOAD_EXTENSION SQLITE_THREADSAFE=1 _CRT_SECURE_NO_WARNINGS) target_compile_definitions(game_tests PRIVATE GAME_TEST_FIXTURE_DIR="${CMAKE_CURRENT_SOURCE_DIR}/tests/fixtures") if(MSVC) target_compile_options(game_tests PRIVATE /W3 /utf-8) elseif(NOT WIN32) target_link_libraries(game_tests PRIVATE pthread dl) endif() add_test(NAME game_transactions COMMAND game_tests) endif()