| 1 |
# This file is part of Leela Zero. |
| 2 |
# Copyright (C) 2017 Marco Calignano |
| 3 |
# Copyright (C) 2017-2019 Gian-Carlo Pascutto and contributors |
| 4 |
# Leela Zero is free software: you can redistribute it and/or modify |
| 5 |
# it under the terms of the GNU General Public License as published by |
| 6 |
# the Free Software Foundation, either version 3 of the License, or |
| 7 |
# (at your option) any later version. |
| 8 |
# Leela Zero is distributed in the hope that it will be useful, |
| 9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
# GNU General Public License for more details. |
| 12 |
# You should have received a copy of the GNU General Public License |
| 13 |
# along with Leela Zero. If not, see <http://www.gnu.org/licenses/>. |
| 14 |
|
| 15 |
cmake_minimum_required(VERSION 3.1) |
| 16 |
|
| 17 |
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/") |
| 18 |
include(GNUInstallDirs) |
| 19 |
|
| 20 |
project(leelaz) |
| 21 |
add_subdirectory(gtest EXCLUDE_FROM_ALL) # We don't want to install gtest, exclude it from `all` |
| 22 |
|
| 23 |
# Required Packages |
| 24 |
set(Boost_MIN_VERSION "1.58.0") |
| 25 |
set(Boost_USE_MULTITHREADED ON) |
| 26 |
find_package(Boost 1.58.0 REQUIRED program_options filesystem) |
| 27 |
find_package(Threads REQUIRED) |
| 28 |
find_package(ZLIB REQUIRED) |
| 29 |
find_package(OpenCL REQUIRED) |
| 30 |
# We need OpenBLAS for now, because we make some specific |
| 31 |
# calls. Ideally we'd use OpenBLAS is possible and fall back to |
| 32 |
# not doing those calls if it's not present. |
| 33 |
if(NOT APPLE) |
| 34 |
set(BLA_VENDOR OpenBLAS) |
| 35 |
endif() |
| 36 |
if(USE_BLAS) |
| 37 |
message(STATUS "Looking for system BLAS/OpenBLAS library.") |
| 38 |
find_package(BLAS REQUIRED) |
| 39 |
find_path(BLAS_INCLUDE_DIRS openblas_config.h |
| 40 |
/usr/include |
| 41 |
/usr/local/include |
| 42 |
/usr/include/openblas |
| 43 |
/opt/OpenBLAS/include |
| 44 |
/usr/include/x86_64-linux-gnu |
| 45 |
$ENV{BLAS_HOME}/include) |
| 46 |
add_definitions(-DUSE_BLAS) |
| 47 |
else() |
| 48 |
message(STATUS "Using built-in matrix library.") |
| 49 |
endif() |
| 50 |
find_package(Qt5Core) |
| 51 |
|
| 52 |
set(CMAKE_CXX_STANDARD 14) |
| 53 |
set(CMAKE_CXX_STANDARD_REQUIRED on) |
| 54 |
|
| 55 |
# See if we can set optimization flags as expected. |
| 56 |
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") |
| 57 |
set(GccSpecificFlags 1) |
| 58 |
elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang") |
| 59 |
set(GccSpecificFlags 1) |
| 60 |
elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") |
| 61 |
set(GccSpecificFlags 1) |
| 62 |
elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel") |
| 63 |
set(GccSpecificFlags 0) |
| 64 |
elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") |
| 65 |
set(GccSpecificFlags 0) |
| 66 |
endif() |
| 67 |
|
| 68 |
if(NOT CMAKE_CONFIGURATION_TYPES AND NOT CMAKE_BUILD_TYPE) |
| 69 |
set(CMAKE_BUILD_TYPE RELEASE) |
| 70 |
endif(NOT CMAKE_CONFIGURATION_TYPES AND NOT CMAKE_BUILD_TYPE) |
| 71 |
|
| 72 |
if(GccSpecificFlags) |
| 73 |
set(GCC_COMPILE_FLAGS "-Wall -Wextra -ffast-math -flto -march=native") |
| 74 |
set(GCC_DISABLED_WARNING_COMPILE_FLAGS "-Wno-ignored-attributes -Wno-maybe-uninitialized \ |
| 75 |
-Wno-mismatched-tags") |
| 76 |
set(GCC_FLAGS "${GCC_COMPILE_FLAGS} ${GCC_DISABLED_WARNING_COMPILE_FLAGS}") |
| 77 |
set(CMAKE_CXX_FLAGS_DEBUG "${GCC_FLAGS} -g -Og") |
| 78 |
set(CMAKE_CXX_FLAGS_RELEASE "${GCC_FLAGS} -g -O3 -DNDEBUG") |
| 79 |
set(CMAKE_EXE_LINKER_FLAGS "-flto -g") |
| 80 |
endif(GccSpecificFlags) |
| 81 |
|
| 82 |
if(USE_CPU_ONLY) |
| 83 |
add_definitions(-DUSE_CPU_ONLY) |
| 84 |
endif() |
| 85 |
if(USE_HALF) |
| 86 |
add_definitions(-DUSE_HALF) |
| 87 |
endif() |
| 88 |
|
| 89 |
set(IncludePath "${CMAKE_CURRENT_SOURCE_DIR}/src" "${CMAKE_CURRENT_SOURCE_DIR}/src/Eigen") |
| 90 |
set(SrcPath "${CMAKE_CURRENT_SOURCE_DIR}/src") |
| 91 |
|
| 92 |
include_directories(${IncludePath}) |
| 93 |
include_directories(${Boost_INCLUDE_DIRS}) |
| 94 |
include_directories(${OpenCL_INCLUDE_DIRS}) |
| 95 |
include_directories(${ZLIB_INCLUDE_DIRS}) |
| 96 |
|
| 97 |
if((UNIX AND NOT APPLE) OR WIN32) |
| 98 |
include_directories(${BLAS_INCLUDE_DIRS}) |
| 99 |
endif() |
| 100 |
if(APPLE) |
| 101 |
include_directories("/System/Library/Frameworks/Accelerate.framework/Versions/Current/Headers") |
| 102 |
endif() |
| 103 |
|
| 104 |
set(leelaz_MAIN "${SrcPath}/Leela.cpp") |
| 105 |
file(GLOB leelaz_SRC "${SrcPath}/*.cpp") |
| 106 |
list(REMOVE_ITEM leelaz_SRC ${leelaz_MAIN}) |
| 107 |
|
| 108 |
# Reuse for leelaz and gtest |
| 109 |
add_library(objs OBJECT ${leelaz_SRC}) |
| 110 |
|
| 111 |
add_executable(leelaz $<TARGET_OBJECTS:objs> ${leelaz_MAIN}) |
| 112 |
|
| 113 |
target_link_libraries(leelaz ${Boost_LIBRARIES}) |
| 114 |
target_link_libraries(leelaz ${BLAS_LIBRARIES}) |
| 115 |
target_link_libraries(leelaz ${OpenCL_LIBRARIES}) |
| 116 |
target_link_libraries(leelaz ${ZLIB_LIBRARIES}) |
| 117 |
target_link_libraries(leelaz ${CMAKE_THREAD_LIBS_INIT}) |
| 118 |
install(TARGETS leelaz DESTINATION ${CMAKE_INSTALL_BINDIR}) |
| 119 |
|
| 120 |
if(Qt5Core_FOUND) |
| 121 |
if(NOT Qt5Core_VERSION VERSION_LESS "5.3.0") |
| 122 |
add_subdirectory(autogtp) |
| 123 |
add_subdirectory(validation) |
| 124 |
else() |
| 125 |
message(WARNING "Qt ${Qt5Core_VERSION} is found but does not met required version 5.3.0, \ |
| 126 |
build target for `autogtp` and `validation` is disabled.") |
| 127 |
endif() |
| 128 |
else() |
| 129 |
message(WARNING "Qt is not found, build for `autogtp` and `validation` is disabled") |
| 130 |
endif() |
| 131 |
|
| 132 |
# Google Test below |
| 133 |
file(GLOB tests_SRC "${SrcPath}/tests/*.cpp") |
| 134 |
|
| 135 |
add_executable(tests ${tests_SRC} $<TARGET_OBJECTS:objs>) |
| 136 |
if(GccSpecificFlags) |
| 137 |
target_compile_options(tests PRIVATE "-Wno-unused-variable") |
| 138 |
endif() |
| 139 |
|
| 140 |
target_link_libraries(tests ${Boost_LIBRARIES}) |
| 141 |
target_link_libraries(tests ${BLAS_LIBRARIES}) |
| 142 |
target_link_libraries(tests ${OpenCL_LIBRARIES}) |
| 143 |
target_link_libraries(tests ${ZLIB_LIBRARIES}) |
| 144 |
target_link_libraries(tests gtest_main ${CMAKE_THREAD_LIBS_INIT}) |
| 145 |
|
| 146 |
include(GetGitRevisionDescription) |
| 147 |
git_describe(VERSION --tags) |
| 148 |
string(REGEX REPLACE "^v([0-9]+)\\..*" "\\1" MAJOR_VERSION "${VERSION}") |
| 149 |
string(REGEX REPLACE "^v[0-9]+\\.([0-9]+).*" "\\1" MINOR_VERSION "${VERSION}") |
| 150 |
|
| 151 |
SET(CPACK_GENERATOR "DEB") |
| 152 |
SET(CPACK_DEBIAN_PACKAGE_NAME "leelaz") |
| 153 |
SET(CPACK_DEBIAN_PACKAGE_VERSION "${MAJOR_VERSION}.${MINOR_VERSION}") |
| 154 |
SET(CPACK_DEBIAN_ARCHITECTURE ${CMAKE_SYSTEM_PROCESSOR}) |
| 155 |
SET(CPACK_DEBIAN_PACKAGE_MAINTAINER "Gian-Carlo Pascutto https://github.com/gcp/leela-zero") |
| 156 |
SET(CPACK_DEBIAN_PACKAGE_DESCRIPTION "Go engine with no human-provided knowledge, modeled after the AlphaGo Zero paper.") |
| 157 |
SET(CPACK_DEBIAN_PACKAGE_PRIORITY "optional") |
| 158 |
SET(CPACK_DEBIAN_PACKAGE_SECTION "games") |
| 159 |
SET(CPACK_DEBIAN_PACKAGE_SHLIBDEPS ON) |
| 160 |
SET(CPACK_PACKAGE_FILE_NAME "${CMAKE_PROJECT_NAME}-${MAJOR_VERSION}.${MINOR_VERSION}") |
| 161 |
|
| 162 |
INCLUDE(CPack) |