forked from google-deepmind/open_spiel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMakeLists.txt
337 lines (305 loc) · 11.3 KB
/
CMakeLists.txt
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# Version >= 3.12 required for new FindPython module
# https://cmake.org/cmake/help/v3.12/release/3.12.html
# Version >= 3.17 required for CMAKE_CUDA_STANDARD
# https://gitlab.kitware.com/cmake/cmake/-/issues/19123
cmake_minimum_required (VERSION 3.17)
project (open_spiel)
# Define some nice terminal colors.
if(NOT WIN32)
string(ASCII 27 Esc)
set(ColourReset "${Esc}[m")
set(ColourBold "${Esc}[1m")
set(Red "${Esc}[31m")
set(Green "${Esc}[32m")
set(Yellow "${Esc}[33m")
set(Blue "${Esc}[34m")
set(Magenta "${Esc}[35m")
set(Cyan "${Esc}[36m")
set(White "${Esc}[37m")
set(BoldRed "${Esc}[1;31m")
set(BoldGreen "${Esc}[1;32m")
set(BoldYellow "${Esc}[1;33m")
set(BoldBlue "${Esc}[1;34m")
set(BoldMagenta "${Esc}[1;35m")
set(BoldCyan "${Esc}[1;36m")
set(BoldWhite "${Esc}[1;37m")
endif()
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CUDA_STANDARD 14)
set(CMAKE_CUDA_STANDARD_REQUIRED TRUE)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
# Set default build type.
set (BUILD_TYPE $ENV{BUILD_TYPE})
if(NOT BUILD_TYPE)
set(BUILD_TYPE Testing
CACHE STRING "Choose the type of build: Debug Release Testing."
FORCE)
endif()
message("${BoldYellow}Current build type is: ${BUILD_TYPE}${ColourReset}")
if(${BUILD_TYPE} STREQUAL "Debug")
# Basic build for debugging (default).
# -Og enables optimizations that do not interfere with debugging.
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -Og")
endif()
if(${BUILD_TYPE} STREQUAL "Testing")
# A build used for running tests: keep all runtime checks (assert,
# SPIEL_CHECK_*, SPIEL_DCHECK_*), but turn on some speed optimizations,
# otherwise tests run for too long.
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2")
endif()
if(${BUILD_TYPE} STREQUAL "Release")
# Optimized release build: turn off debug runtime checks (assert,
# SPIEL_DCHECK_*) and turn on highest speed optimizations.
# The difference in perfomance can be up to 10x higher.
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DNDEBUG -O3")
endif()
if(APPLE)
# On MacOS:
# -undefined dynamic_lookup is necessary for pybind11 linking
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-everything -w -undefined dynamic_lookup")
# On MacOS, we need this so that CMake will use the right Python if the user
# has a virtual environment active
set (CMAKE_FIND_FRAMEWORK LAST)
elseif(WIN32)
# Setup for MSVC 2022.
# No changes needed. In particular: do not use -Wno-everything.
else()
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-everything")
endif()
# Position-independent code is needed for Python extension modules.
set (CMAKE_POSITION_INDEPENDENT_CODE ON)
## Optional dependencies
# One can optionally build and link against specific external dependencies.
# We expect these arguments to be always defined, when building using any script
# in `open_spiel/scripts/`, thus, we emit a warning when it's not, with a
# conservative default.
# See the documentation in install.md.
# Use this macro to define optional dependencies.
# You can then use your chosen DEP_NAME as a variable to check if that
# dependency is enabled -- see code below.
macro(openspiel_optional_dependency DEP_NAME DEP_DEFAULT DEP_DESCRIPTION)
set (${DEP_NAME} ${DEP_DEFAULT} CACHE BOOL ${DEP_DESCRIPTION})
if(NOT DEFINED ENV{${DEP_NAME}})
message("${BoldRed}${DEP_NAME} not set. Defaults to ${DEP_DEFAULT}${ColourReset}")
set (ENV{${DEP_NAME}} ${DEP_DEFAULT})
endif()
set (${DEP_NAME} $ENV{${DEP_NAME}})
message("${BoldYellow}${DEP_NAME}: ${${DEP_NAME}} ${ColourReset}")
# If the dependency is on, pass in compiler flags to enable conditional code,
# e.g. #if OPEN_SPIEL_BUILD_WITH_...
if (${DEP_NAME})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D${DEP_NAME}")
endif()
endmacro()
# List of all optional dependencies:
openspiel_optional_dependency(OPEN_SPIEL_BUILD_WITH_ACPC OFF
"Build against the Universal Poker library.")
openspiel_optional_dependency(OPEN_SPIEL_BUILD_WITH_GO OFF
"Build with support for Golang API.")
openspiel_optional_dependency(OPEN_SPIEL_BUILD_WITH_HANABI OFF
"Build against the Hanabi game.")
openspiel_optional_dependency(OPEN_SPIEL_BUILD_WITH_JULIA OFF
"Build binary for Julia.")
openspiel_optional_dependency(OPEN_SPIEL_BUILD_WITH_LIBNOP OFF
"Build with support for libnop.")
openspiel_optional_dependency(OPEN_SPIEL_BUILD_WITH_LIBTORCH OFF
"Build with support for libtorch.")
openspiel_optional_dependency(OPEN_SPIEL_BUILD_WITH_PYTHON ON
"Build binary for Python.")
openspiel_optional_dependency(OPEN_SPIEL_BUILD_WITH_XINXIN OFF
"Build against xinxin Hearts program.")
openspiel_optional_dependency(OPEN_SPIEL_BUILD_WITH_ROSHAMBO OFF
"Build against RoShamBo bots.")
openspiel_optional_dependency(OPEN_SPIEL_BUILD_WITH_GAMUT OFF
"Build with GAMUT generator integration.")
openspiel_optional_dependency(OPEN_SPIEL_BUILD_WITH_ORTOOLS OFF
"Build with C++ optimization library OR-Tools.")
openspiel_optional_dependency(OPEN_SPIEL_BUILD_WITH_RUST OFF
"Build with support for Rust API.")
if (WIN32)
openspiel_optional_dependency(OPEN_SPIEL_ENABLE_JAX OFF
"Enable JAX.")
openspiel_optional_dependency(OPEN_SPIEL_ENABLE_PYTORCH OFF
"Enable PyTorch.")
openspiel_optional_dependency(OPEN_SPIEL_ENABLE_TENSORFLOW OFF
"Enable Tensorflow.")
else()
openspiel_optional_dependency(OPEN_SPIEL_ENABLE_JAX AUTO
"Enable JAX.")
openspiel_optional_dependency(OPEN_SPIEL_ENABLE_PYTORCH AUTO
"Enable PyTorch.")
openspiel_optional_dependency(OPEN_SPIEL_ENABLE_TENSORFLOW AUTO
"Enable Tensorflow.")
endif()
openspiel_optional_dependency(OPEN_SPIEL_ENABLE_PYTHON_MISC OFF
"Enable miscellaneous Python dependencies.")
openspiel_optional_dependency(OPEN_SPIEL_BUILDING_WHEEL OFF
"Building a Python wheel?")
# Needed to disable Abseil tests.
set (BUILD_TESTING OFF)
# For now, let's enable all the tests.
enable_testing()
set (OPEN_SPIEL_CORE_FILES
action_view.h
action_view.cc
canonical_game_strings.cc
canonical_game_strings.h
game_parameters.cc
game_parameters.h
matrix_game.cc
matrix_game.h
normal_form_game.h
observer.cc
observer.h
policy.cc
policy.h
simultaneous_move_game.cc
simultaneous_move_game.h
spiel.cc
spiel.h
spiel_bots.cc
spiel_bots.h
spiel_globals.h
spiel_utils.cc
spiel_utils.h
tensor_game.cc
tensor_game.h
utils/usage_logging.h
utils/usage_logging.cc
)
# We add the subdirectory here so open_spiel_core can #include absl.
set(ABSL_PROPAGATE_CXX_STD ON)
add_subdirectory (abseil-cpp)
include_directories (abseil-cpp)
# Just the core without any of the games
add_library(open_spiel_core OBJECT ${OPEN_SPIEL_CORE_FILES})
target_include_directories (
open_spiel_core PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} abseil-cpp)
link_libraries(open_spiel_core
absl::algorithm
absl::flags
absl::flags_parse
absl::flat_hash_map
absl::optional
absl::random_random
absl::str_format
absl::strings
absl::time
)
# Just the minimal base library: no games.
set (OPEN_SPIEL_CORE_OBJECTS $<TARGET_OBJECTS:open_spiel_core>)
set (OPEN_SPIEL_OBJECTS
$<TARGET_OBJECTS:open_spiel_core>
$<TARGET_OBJECTS:bots>
$<TARGET_OBJECTS:games>
$<TARGET_OBJECTS:game_transforms>
$<TARGET_OBJECTS:bridge_double_dummy_solver>
$<TARGET_OBJECTS:algorithms>
$<TARGET_OBJECTS:utils>
$<TARGET_OBJECTS:tests>
)
if (OPEN_SPIEL_BUILD_WITH_HANABI)
set(OPEN_SPIEL_OBJECTS ${OPEN_SPIEL_OBJECTS}
$<TARGET_OBJECTS:hanabi_learning_environment>)
endif()
if (OPEN_SPIEL_BUILD_WITH_ACPC)
set(OPEN_SPIEL_OBJECTS ${OPEN_SPIEL_OBJECTS}
$<TARGET_OBJECTS:universal_poker_clib>
$<TARGET_OBJECTS:universal_poker_lib>)
endif()
if (OPEN_SPIEL_BUILD_WITH_XINXIN)
set(OPEN_SPIEL_OBJECTS ${OPEN_SPIEL_OBJECTS} $<TARGET_OBJECTS:xinxin>)
endif()
if (OPEN_SPIEL_BUILD_WITH_ROSHAMBO)
set(OPEN_SPIEL_OBJECTS ${OPEN_SPIEL_OBJECTS} $<TARGET_OBJECTS:roshambo>)
endif()
if (OPEN_SPIEL_BUILD_WITH_LIBNOP)
include_directories(libnop/libnop/include)
add_subdirectory(libnop)
endif()
if (OPEN_SPIEL_BUILD_WITH_LIBTORCH)
list(APPEND CMAKE_PREFIX_PATH "${CMAKE_CURRENT_SOURCE_DIR}/libtorch/libtorch")
find_package(Torch REQUIRED)
add_subdirectory(libtorch)
include_directories(${TORCH_INCLUDE_DIRS})
# Use following to link your_target_executable with torch libraries:
# target_link_libraries(your_target_executable ${TORCH_LIBRARIES})
endif()
if (OPEN_SPIEL_BUILD_WITH_GAMUT)
set(OPEN_SPIEL_OBJECTS ${OPEN_SPIEL_OBJECTS} $<TARGET_OBJECTS:gamut>)
endif()
if (OPEN_SPIEL_BUILD_WITH_ORTOOLS)
# Compile with OR-Tools headers and link against binary distribution,
# downloaded from https://developers.google.com/optimization/install/cpp/linux
# and assumed to be in $HOME/or-tools.
# The flags were taken from the compilation of linear_programming.cc after
# running make test_cc.
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DUSE_BOP -DUSE_GLOP -DUSE_CBC -DUSE_CLP -DUSE_SCIP -pthread")
set(ORTOOLS_HOME "${CMAKE_CURRENT_SOURCE_DIR}/ortools")
set(ORTOOLS_INC_DIRS ${ORTOOLS_HOME} ${ORTOOLS_HOME}/include)
set(ORTOOLS_LIB_DIRS ${ORTOOLS_HOME}/lib ${ORTOOLS_HOME}/lib64)
set(ORTOOLS_LIBS z rt pthread ortools)
set_target_properties(open_spiel_core PROPERTIES POSITION_INDEPENDENT_CODE ON)
include_directories(${ORTOOLS_INC_DIRS})
link_directories(${ORTOOLS_LIB_DIRS})
# Use following to link your_target_executable with OrTools libraries:
# target_link_libraries(your_target_executable ${ORTOOLS_LIBS})
endif()
# We have the parent of this directory in the include path, so that we can
# include for example "open_spiel/spiel.h" (assuming this directory is named
# open_spiel).
include_directories(..)
add_subdirectory (algorithms)
add_subdirectory (bots)
add_subdirectory (examples)
add_subdirectory (games)
add_subdirectory (game_transforms)
if (OPEN_SPIEL_BUILD_WITH_GO)
add_subdirectory(go)
endif()
if (OPEN_SPIEL_BUILD_WITH_RUST)
add_subdirectory(rust)
endif()
if (OPEN_SPIEL_BUILD_WITH_PYTHON)
add_subdirectory (python)
endif()
add_subdirectory (utils)
if (OPEN_SPIEL_BUILD_WITH_JULIA)
add_subdirectory (julia)
endif()
# Build a shared library, i.e. libopen_spiel.so. We generally only enable this
# for binary releases.
# Note that there are known problems when trying to use absl::flags within a
# shared library, hence is intentionally left out. To use ABSL flags, link with
# absl::flags and absl::flags_parse separately.
set (BUILD_SHARED_LIB OFF CACHE BOOL "Build a shared library?")
if(NOT DEFINED ENV{BUILD_SHARED_LIB})
set (ENV{BUILD_SHARED_LIB} OFF)
endif()
set (BUILD_SHARED_LIB $ENV{BUILD_SHARED_LIB})
if (BUILD_SHARED_LIB)
if (OPEN_SPIEL_BUILD_WITH_ORTOOLS)
add_library(open_spiel SHARED ${OPEN_SPIEL_OBJECTS}
# Optionally include files that use external dependencies, for example
# linear program specification for finding Nash equilibria.
$<TARGET_OBJECTS:open_spiel_ortools>
)
else()
add_library(open_spiel SHARED ${OPEN_SPIEL_OBJECTS})
endif()
target_include_directories(open_spiel PUBLIC
${CMAKE_CURRENT_SOURCE_DIR} abseil-cpp)
target_link_libraries(open_spiel PUBLIC
absl::algorithm
absl::flat_hash_map
absl::optional
absl::random_random
absl::str_format
absl::strings
absl::time
# Optionally link external dependencies, for example OrTools for solving
# linear programs.
${ORTOOLS_LIBS}
)
endif()
add_subdirectory (tests)