Skip to content

Commit

Permalink
Link against libopen_spiel for simple tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Hemphill authored and jthemphill committed Dec 18, 2023
1 parent 7c58b6c commit c7324ad
Show file tree
Hide file tree
Showing 10 changed files with 317 additions and 615 deletions.
8 changes: 0 additions & 8 deletions .github/workflows/actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,53 +16,46 @@ jobs:
OS_PYTHON_VERSION: "3.11"
TRAVIS_USE_NOX: 0
DEFAULT_OPTIONAL_DEPENDENCY: "ON"
BUILD_SHARED_LIB: "OFF"
OPEN_SPIEL_BUILD_WITH_ORTOOLS: "OFF"
OPEN_SPIEL_BUILD_WITH_ORTOOLS_DOWNLOAD_URL: ""
# Standard (most current) platforms and versions.
- os: ubuntu-22.04
OS_PYTHON_VERSION: "3.10"
TRAVIS_USE_NOX: 0
DEFAULT_OPTIONAL_DEPENDENCY: "ON"
BUILD_SHARED_LIB: "OFF"
OPEN_SPIEL_BUILD_WITH_ORTOOLS: "ON"
OPEN_SPIEL_BUILD_WITH_ORTOOLS_DOWNLOAD_URL: "https://github.com/google/or-tools/releases/download/v9.6/or-tools_amd64_ubuntu-22.04_cpp_v9.6.2534.tar.gz"
- os: ubuntu-22.04
OS_PYTHON_VERSION: "3.10"
TRAVIS_USE_NOX: 0
DEFAULT_OPTIONAL_DEPENDENCY: "OFF"
BUILD_SHARED_LIB: "OFF"
OPEN_SPIEL_BUILD_WITH_ORTOOLS: "OFF"
OPEN_SPIEL_BUILD_WITH_ORTOOLS_DOWNLOAD_URL: ""
- os: macos-12
OS_PYTHON_VERSION: "3.9"
TRAVIS_USE_NOX: 0
DEFAULT_OPTIONAL_DEPENDENCY: "OFF"
BUILD_SHARED_LIB: "OFF"
OPEN_SPIEL_BUILD_WITH_ORTOOLS: "OFF"
OPEN_SPIEL_BUILD_WITH_ORTOOLS_DOWNLOAD_URL: ""
# Standard or older platforms with older Python versions.
- os: macos-11
OS_PYTHON_VERSION: "3.8"
TRAVIS_USE_NOX: 0
DEFAULT_OPTIONAL_DEPENDENCY: "OFF"
BUILD_SHARED_LIB: "OFF"
OPEN_SPIEL_BUILD_WITH_ORTOOLS: "OFF"
OPEN_SPIEL_BUILD_WITH_ORTOOLS_DOWNLOAD_URL: ""
# Older Python version on Ubuntu 20.04
- os: ubuntu-20.04
OS_PYTHON_VERSION: "3.9"
DEFAULT_OPTIONAL_DEPENDENCY: "ON"
TRAVIS_USE_NOX: 0
BUILD_SHARED_LIB: "ON"
OPEN_SPIEL_BUILD_WITH_ORTOOLS: "OFF"
OPEN_SPIEL_BUILD_WITH_ORTOOLS_DOWNLOAD_URL: ""
# One older platform with oldest Python version on that platform.
- os: ubuntu-20.04
OS_PYTHON_VERSION: "3.8"
TRAVIS_USE_NOX: 0
DEFAULT_OPTIONAL_DEPENDENCY: "OFF"
BUILD_SHARED_LIB: "OFF"
OPEN_SPIEL_BUILD_WITH_ORTOOLS: "OFF"
OPEN_SPIEL_BUILD_WITH_ORTOOLS_DOWNLOAD_URL: ""

Expand All @@ -76,7 +69,6 @@ jobs:
TRAVIS_USE_NOX: ${{ matrix.TRAVIS_USE_NOX }}
DEFAULT_OPTIONAL_DEPENDENCY: ${{ matrix.DEFAULT_OPTIONAL_DEPENDENCY }}
OPEN_SPIEL_BUILD_WITH_JULIA: ${{ matrix.OPEN_SPIEL_BUILD_WITH_JULIA }}
BUILD_SHARED_LIB: ${{ matrix.BUILD_SHARED_LIB }}
OPEN_SPIEL_BUILD_WITH_ORTOOLS: ${{ matrix.OPEN_SPIEL_BUILD_WITH_ORTOOLS }}
OPEN_SPIEL_BUILD_WITH_ORTOOLS_DOWNLOAD_URL: ${{ matrix.OPEN_SPIEL_BUILD_WITH_ORTOOLS_DOWNLOAD_URL }}

Expand Down
2 changes: 1 addition & 1 deletion docs/library.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ To build OpenSpiel as a shared library, simply run:
```
mkdir build
cd build
BUILD_SHARED_LIB=ON CXX=clang++ cmake -DPython3_EXECUTABLE=$(which python3) -DCMAKE_CXX_COMPILER=${CXX} ../open_spiel
CXX=clang++ cmake -DPython3_EXECUTABLE=$(which python3) -DCMAKE_CXX_COMPILER=${CXX} ../open_spiel
make -j$(nproc) open_spiel
```

Expand Down
129 changes: 76 additions & 53 deletions open_spiel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,57 @@ set (BUILD_TESTING OFF)
# For now, let's enable all the tests.
enable_testing()

# Define a C++ executable that links against the shared open_spiel library.
# Convenience macro to replace the common pattern
#
# add_executable(my_exe my_exe.cc ${OPEN_SPIEL_OBJECTS})
#
# with
# add_open_spiel_executable(my_exe my_exe.cc)
#
macro(add_open_spiel_executable)
set(ARGS ${ARGV})
list(GET ARGS 0 EXE_NAME)
add_executable(${ARGV})
target_link_libraries("${EXE_NAME}" open_spiel)
endmacro()

# Define a C++ executable and register it as a test.
# Convenience macro to replace the common pattern
#
# add_executable(my_exe my_exe.cc ${OPEN_SPIEL_OBJECTS})
# add_test(my_exe my_exe)
#
# with
# add_open_spiel_test(my_exe my_exe.cc)
macro(add_open_spiel_test)
set(ARGS ${ARGV})
list(GET ARGS 0 EXE_NAME)
add_open_spiel_executable(${ARGV})
add_test("${EXE_NAME}" "${EXE_NAME}")
endmacro()

# 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)

# The abseil libraries we use within open_spiel
add_library(absl INTERFACE)
target_link_libraries(absl
INTERFACE
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_FILES
action_view.h
action_view.cc
Expand Down Expand Up @@ -196,32 +247,23 @@ set (OPEN_SPIEL_CORE_FILES
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
add_library(open_spiel_core STATIC ${OPEN_SPIEL_CORE_FILES})

# Use an interface library and `link_libraries()` to ensure that every library
# and executable below this line will be linked against abseil and
# open_spiel_core
add_library(open_spiel_interface INTERFACE)
target_include_directories(
open_spiel_interface INTERFACE ${CMAKE_CURRENT_SOURCE_DIR} abseil-cpp)
target_link_libraries(open_spiel_interface
INTERFACE
absl
open_spiel_core
)

# Just the minimal base library: no games.
set (OPEN_SPIEL_CORE_OBJECTS $<TARGET_OBJECTS:open_spiel_core>)
link_libraries(open_spiel_interface)

set (OPEN_SPIEL_OBJECTS
$<TARGET_OBJECTS:open_spiel_core>
$<TARGET_OBJECTS:bots>
$<TARGET_OBJECTS:games>
$<TARGET_OBJECTS:game_transforms>
Expand Down Expand Up @@ -330,40 +372,21 @@ 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)
# Build a shared library, i.e. libopen_spiel.so. Used for tests and
# binary releases.
add_library(open_spiel SHARED ${OPEN_SPIEL_OBJECTS})
if (OPEN_SPIEL_BUILD_WITH_ORTOOLS)
# Optionally include files that use external dependencies, for example
# linear program specification for finding Nash equilibria.
target_link_libraries(open_spiel PUBLIC $<TARGET_OBJECTS:open_spiel_ortools>)
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
target_include_directories(open_spiel PUBLIC
${CMAKE_CURRENT_SOURCE_DIR} abseil-cpp)
target_link_libraries(open_spiel
INTERFACE
# Optionally link external dependencies, for example OrTools for solving
# linear programs.
${ORTOOLS_LIBS}
)
endif()
)

add_subdirectory (tests)
104 changes: 26 additions & 78 deletions open_spiel/algorithms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,109 +84,57 @@ if (${OPEN_SPIEL_BUILD_WITH_ORTOOLS})
add_subdirectory (ortools)
endif()

add_executable(best_response_test best_response_test.cc
$<TARGET_OBJECTS:algorithms> ${OPEN_SPIEL_OBJECTS})
add_test(best_response_test best_response_test)
add_open_spiel_test(best_response_test best_response_test.cc)

add_executable(cfr_test cfr_test.cc
$<TARGET_OBJECTS:algorithms> ${OPEN_SPIEL_OBJECTS})
add_test(cfr_test cfr_test)
add_open_spiel_test(cfr_test cfr_test.cc)

add_executable(cfr_br_test cfr_br_test.cc
$<TARGET_OBJECTS:algorithms> ${OPEN_SPIEL_OBJECTS})
add_test(cfr_br_test cfr_br_test)
add_open_spiel_test(cfr_br_test cfr_br_test.cc)

add_executable(corr_dist_test corr_dist_test.cc
$<TARGET_OBJECTS:algorithms> ${OPEN_SPIEL_OBJECTS})
add_test(corr_dist_test corr_dist_test)
add_open_spiel_test(corr_dist_test corr_dist_test.cc)

add_executable(corr_dev_builder_test corr_dev_builder_test.cc
$<TARGET_OBJECTS:algorithms> ${OPEN_SPIEL_OBJECTS})
add_test(corr_dev_builder_test corr_dev_builder_test)
add_open_spiel_test(corr_dev_builder_test corr_dev_builder_test.cc)

add_executable(deterministic_policy_test deterministic_policy_test.cc
$<TARGET_OBJECTS:algorithms> ${OPEN_SPIEL_OBJECTS})
add_test(deterministic_policy_test deterministic_policy_test)
add_open_spiel_test(deterministic_policy_test deterministic_policy_test.cc)

add_executable(evaluate_bots_test evaluate_bots_test.cc
$<TARGET_OBJECTS:algorithms> ${OPEN_SPIEL_OBJECTS})
add_test(evaluate_bots_test evaluate_bots_test)
add_open_spiel_test(evaluate_bots_test evaluate_bots_test.cc)

add_executable(external_sampling_mccfr_test external_sampling_mccfr_test.cc
$<TARGET_OBJECTS:algorithms> ${OPEN_SPIEL_OBJECTS})
add_test(external_sampling_mccfr_test external_sampling_mccfr_test)
add_open_spiel_test(external_sampling_mccfr_test external_sampling_mccfr_test.cc)

add_executable(get_all_histories_test get_all_histories_test.cc
$<TARGET_OBJECTS:algorithms> ${OPEN_SPIEL_OBJECTS})
add_test(get_all_histories_test get_all_histories_test)
add_open_spiel_test(get_all_histories_test get_all_histories_test.cc)

add_executable(get_all_states_test get_all_states_test.cc
$<TARGET_OBJECTS:algorithms> ${OPEN_SPIEL_OBJECTS})
add_test(get_all_states_test get_all_states_test)
add_open_spiel_test(get_all_states_test get_all_states_test.cc)

add_executable(get_legal_actions_map_test get_legal_actions_map_test.cc
$<TARGET_OBJECTS:algorithms> ${OPEN_SPIEL_OBJECTS})
add_test(get_legal_actions_map_test get_legal_actions_map_test)
add_open_spiel_test(get_legal_actions_map_test get_legal_actions_map_test.cc)

add_executable(history_tree_test history_tree_test.cc
$<TARGET_OBJECTS:algorithms> ${OPEN_SPIEL_OBJECTS})
add_test(history_tree_test history_tree_test)
add_open_spiel_test(history_tree_test history_tree_test.cc)

add_executable(infostate_tree_test infostate_tree_test.cc
$<TARGET_OBJECTS:algorithms> ${OPEN_SPIEL_OBJECTS})
add_test(infostate_tree_test infostate_tree_test)
add_open_spiel_test(infostate_tree_test infostate_tree_test.cc)

add_executable(is_mcts_test is_mcts_test.cc
$<TARGET_OBJECTS:algorithms> ${OPEN_SPIEL_OBJECTS})
add_test(is_mcts_test is_mcts_test)
add_open_spiel_test(is_mcts_test is_mcts_test.cc)

add_executable(matrix_game_utils_test matrix_game_utils_test.cc
$<TARGET_OBJECTS:algorithms> ${OPEN_SPIEL_OBJECTS})
add_test(matrix_game_utils_test matrix_game_utils_test)
add_open_spiel_test(matrix_game_utils_test matrix_game_utils_test.cc)

add_executable(minimax_test minimax_test.cc
$<TARGET_OBJECTS:algorithms> ${OPEN_SPIEL_OBJECTS})
add_test(minimax_test minimax_test)
add_open_spiel_test(minimax_test minimax_test.cc)

add_executable(observation_history_test observation_history_test.cc
$<TARGET_OBJECTS:algorithms> ${OPEN_SPIEL_OBJECTS})
add_test(observation_history_test observation_history_test)
add_open_spiel_test(observation_history_test observation_history_test.cc)

add_executable(oos_test oos_test.cc
$<TARGET_OBJECTS:algorithms> ${OPEN_SPIEL_OBJECTS})
add_test(oos_test oos_test)
add_open_spiel_test(oos_test oos_test.cc)

add_executable(outcome_sampling_mccfr_test outcome_sampling_mccfr_test.cc
$<TARGET_OBJECTS:algorithms> ${OPEN_SPIEL_OBJECTS})
add_test(outcome_sampling_mccfr_test outcome_sampling_mccfr_test)
add_open_spiel_test(outcome_sampling_mccfr_test outcome_sampling_mccfr_test.cc)

add_executable(state_distribution_test state_distribution_test.cc
$<TARGET_OBJECTS:algorithms> ${OPEN_SPIEL_OBJECTS})
add_test(state_distribution_test state_distribution_test)
add_open_spiel_test(state_distribution_test state_distribution_test.cc)

add_executable(tabular_best_response_mdp_test tabular_best_response_mdp_test.cc
$<TARGET_OBJECTS:algorithms> ${OPEN_SPIEL_OBJECTS})
add_test(tabular_best_response_mdp_test tabular_best_response_mdp_test)
add_open_spiel_test(tabular_best_response_mdp_test tabular_best_response_mdp_test.cc)

add_executable(tabular_exploitability_test tabular_exploitability_test.cc
$<TARGET_OBJECTS:algorithms> ${OPEN_SPIEL_OBJECTS})
add_test(tabular_exploitability_test tabular_exploitability_test)
add_open_spiel_test(tabular_exploitability_test tabular_exploitability_test.cc)

add_executable(tabular_sarsa_test tabular_sarsa_test.cc
$<TARGET_OBJECTS:algorithms> ${OPEN_SPIEL_OBJECTS})
add_test(tabular_sarsa_test tabular_sarsa_test)
add_open_spiel_test(tabular_sarsa_test tabular_sarsa_test.cc)

add_executable(tabular_q_learning_test tabular_q_learning_test.cc
$<TARGET_OBJECTS:algorithms> ${OPEN_SPIEL_OBJECTS})
add_test(tabular_q_learning_test tabular_q_learning_test)
add_open_spiel_test(tabular_q_learning_test tabular_q_learning_test.cc)

add_executable(tensor_game_utils_test tensor_game_utils_test.cc
$<TARGET_OBJECTS:algorithms> ${OPEN_SPIEL_OBJECTS})
add_test(tensor_game_utils_test tensor_game_utils_test)
add_open_spiel_test(tensor_game_utils_test tensor_game_utils_test.cc)

add_executable(trajectories_test trajectories_test.cc
$<TARGET_OBJECTS:algorithms> ${OPEN_SPIEL_OBJECTS})
add_test(trajectories_test trajectories_test)
add_open_spiel_test(trajectories_test trajectories_test.cc)

add_subdirectory (alpha_zero)
add_subdirectory (alpha_zero_torch)
Expand Down
Loading

0 comments on commit c7324ad

Please sign in to comment.