|
| 1 | +cmake_minimum_required(VERSION 3.10) |
| 2 | +project(chess_engine) |
| 3 | + |
| 4 | +# Use C++17 |
| 5 | +set(CMAKE_CXX_STANDARD 17) |
| 6 | +set(CMAKE_CXX_STANDARD_REQUIRED ON) |
| 7 | + |
| 8 | +# Optional: enable warnings (MSVC specific flags) |
| 9 | +if (MSVC) |
| 10 | + add_compile_options(/W4 /permissive-) |
| 11 | +else() |
| 12 | + add_compile_options(-Wall -Wextra -pedantic) |
| 13 | +endif() |
| 14 | + |
| 15 | +# Add the source files |
| 16 | +set(SOURCES |
| 17 | + main.cpp |
| 18 | + eval.cpp |
| 19 | + search.cpp |
| 20 | + tt.cpp |
| 21 | + movepick.cpp |
| 22 | + uci.cpp |
| 23 | + ucioptions.cpp |
| 24 | + timeman.cpp |
| 25 | +) |
| 26 | + |
| 27 | +# Define the executable |
| 28 | +add_executable(${PROJECT_NAME} ${SOURCES}) |
| 29 | + |
| 30 | +# Threading support |
| 31 | +find_package(Threads REQUIRED) |
| 32 | +target_link_libraries(${PROJECT_NAME} Threads::Threads) |
| 33 | + |
| 34 | +# Platform-specific threading configurations |
| 35 | +if(WIN32) |
| 36 | + if(MSVC) |
| 37 | + # Windows with MSVC - uses Windows threading API |
| 38 | + target_compile_definitions(${PROJECT_NAME} PRIVATE WIN32_LEAN_AND_MEAN) |
| 39 | + elseif(MINGW) |
| 40 | + # MinGW on Windows - uses pthread |
| 41 | + target_compile_definitions(${PROJECT_NAME} PRIVATE _WIN32_WINNT=0x0601) |
| 42 | + # MinGW might need explicit pthread linking |
| 43 | + if(CMAKE_THREAD_LIBS_INIT) |
| 44 | + target_link_libraries(${PROJECT_NAME} ${CMAKE_THREAD_LIBS_INIT}) |
| 45 | + endif() |
| 46 | + endif() |
| 47 | +elseif(UNIX) |
| 48 | + # Unix/Linux systems - uses pthread |
| 49 | + if(CMAKE_THREAD_LIBS_INIT) |
| 50 | + target_link_libraries(${PROJECT_NAME} ${CMAKE_THREAD_LIBS_INIT}) |
| 51 | + endif() |
| 52 | + # Some older systems might need explicit -pthread flag |
| 53 | + if(CMAKE_USE_PTHREADS_INIT) |
| 54 | + target_compile_options(${PROJECT_NAME} PRIVATE -pthread) |
| 55 | + target_link_options(${PROJECT_NAME} PRIVATE -pthread) |
| 56 | + endif() |
| 57 | +endif() |
| 58 | + |
| 59 | +# Optional: Add atomic library support for older compilers |
| 60 | +if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS "4.9") |
| 61 | + target_link_libraries(${PROJECT_NAME} atomic) |
| 62 | +endif() |
| 63 | + |
| 64 | +# Set default build type if none is specified |
| 65 | +if(NOT CMAKE_BUILD_TYPE) |
| 66 | + set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the build type (Debug, Release, etc.)" FORCE) |
| 67 | +endif() |
| 68 | + |
| 69 | +# Optional: Add specific compile flags per build type |
| 70 | +if(NOT MSVC) |
| 71 | + set(CMAKE_CXX_FLAGS_DEBUG "-g -march=native -mtune=native") |
| 72 | + set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG -march=native -mtune=native -funroll-loops -ftree-vectorize -fomit-frame-pointer") |
| 73 | +else() |
| 74 | + set(CMAKE_CXX_FLAGS_DEBUG "/Zi /Od") |
| 75 | + set(CMAKE_CXX_FLAGS_RELEASE "/O2 /DNDEBUG") |
| 76 | +endif() |
0 commit comments