From ba9952130898bde2879b01ff2b49d3d48d26ce81 Mon Sep 17 00:00:00 2001 From: Victor Khaustov <3192677+vi3itor@users.noreply.github.com> Date: Wed, 26 Jan 2022 00:26:29 +0900 Subject: [PATCH] Update deprecated FindPythonLibs (#307) * Update deprecated FindPythonLibs * Set Python version and directories manually on Windows * Change CMakeLists variables according to the convention * Revert include dirs name for OpenGL * Update documentation and README.md * Use deprecated FindPythonLib on legacy systems Co-authored-by: qstanczyk --- README.md | 9 ++-- gfootball/doc/compile_engine.md | 29 +++------- third_party/gfootball_engine/CMakeLists.txt | 60 ++++++++++++--------- 3 files changed, 47 insertions(+), 51 deletions(-) diff --git a/README.md b/README.md index 7e9989f8..986511a0 100644 --- a/README.md +++ b/README.md @@ -60,12 +60,14 @@ python -m pip install --upgrade pip setuptools psutil wheel #### 2. Install GFootball -#### 2a. From PyPi package +#### Option a. From PyPi package (recommended) ```shell python3 -m pip install gfootball ``` -#### 2b. Installing from sources using GitHub repository +#### Option b. Installing from sources using GitHub repository +(On Windows you have to install additional tools and set environment variables, see +[Compiling Engine](gfootball/doc/compile_engine.md#windows) for detailed instructions.) ```shell git clone https://github.com/google-research/football.git @@ -85,9 +87,8 @@ Next, build the game engine and install dependencies: python3 -m pip install . ``` This command can run for a couple of minutes, as it compiles the C++ environment in the background. +If you face any problems, first check [Compiling Engine](gfootball/doc/compile_engine.md) documentation and search GitHub issues. -To compile the engine on Windows you have to install additional tools and set environment variables. See -[Compiling Engine](gfootball/doc/compile_engine.md#windows) for detailed instructions. #### 3. Time to play! ```shell diff --git a/gfootball/doc/compile_engine.md b/gfootball/doc/compile_engine.md index c0321068..8c285025 100644 --- a/gfootball/doc/compile_engine.md +++ b/gfootball/doc/compile_engine.md @@ -25,11 +25,8 @@ Finally, install [vcpkg](https://github.com/microsoft/vcpkg) by following a cd C:\dev :: Clone vckpg git clone https://github.com/microsoft/vcpkg.git -cd vckpg :: Run installation script -.\bootstrap-vcpkg.bat -:: Return to previous directory -cd .. +.\vcpkg\bootstrap-vcpkg.bat ``` If you have `vcpkg` already installed, consider updating it to the latest commit and running `.\bootstrap-vcpkg.bat`. @@ -59,7 +56,7 @@ python -m pip install . ``` -## macOS +## macOS (both Intel processors and Apple Silicon) First, install [brew](https://brew.sh/). It should automatically download Command Line Tools. Next, install the required packages: @@ -75,11 +72,9 @@ git clone https://github.com/google-research/football.git cd football ``` -### Intel processor -#### Installation with brew version of Python +### Installation with brew version of Python It is recommended to use Python shipped with `brew`, because `boost-python3` is compiled against the same version. To check which Python 3 is used by default on your setup, execute `which python3`. -For Intel-based Macs it should be `/usr/local/bin/python3`. If you have a different path, and you don't want to change symlinks, create a virtual environment with `/usr/local/bin/python3 -m venv football-env` or `$(brew --prefix python3)/bin/python3.9 -m venv football-env`. @@ -96,10 +91,10 @@ python3 -m pip install psutil Finally, build the game environment: ```shell -python3 -m pip install . --use-feature=in-tree-build +python3 -m pip install . ``` -#### Installation with conda +### Installation with conda If you installed the engine using `conda`, you might encounter the following error: `TypeError: __init__() should return None, not 'NoneType'` when trying to run the game. @@ -112,17 +107,9 @@ conda deactivate $(brew --prefix python3)/bin/python3.9 -m venv football-env source football-env/bin/activate python3 -m pip install --upgrade pip setuptools psutil wheel -python3 -m pip install . --use-feature=in-tree-build +python3 -m pip install . ``` -### Apple Silicon -The environment can be compiled and run on Apple Silicon. Until some dependencies (`opencv-python`, `numpy`, etc.) -fully support new architecture, the required components should be installed manually beforehand, -and Google Research Football should be installed without dependencies (`--no-deps`). - -#### Installation with conda -Python dependencies can be installed via `conda's` fork [miniforge](https://github.com/conda-forge/miniforge). -But you may encounter a `TypeError` when running the game if you use `boost-python3` from `brew`. ## Linux Install required packages: @@ -148,7 +135,7 @@ python3 -m pip install psutil Finally, build the game environment: ```shell -python3 -m pip install . --use-feature=in-tree-build +python3 -m pip install . ``` ## Development mode @@ -158,7 +145,7 @@ in the [development](https://packaging.python.org/guides/distributing-packages-u (aka editable) mode by running: ```shell -python3 -m pip install -e . --use-feature=in-tree-build +python3 -m pip install -e . ``` In such case, Python source files in projects can be edited in-place without reinstallation, diff --git a/third_party/gfootball_engine/CMakeLists.txt b/third_party/gfootball_engine/CMakeLists.txt index 0d847cb0..b333e14b 100644 --- a/third_party/gfootball_engine/CMakeLists.txt +++ b/third_party/gfootball_engine/CMakeLists.txt @@ -11,9 +11,9 @@ if(WIN32) endif() if(${CMAKE_GENERATOR_PLATFORM} STREQUAL Win32) - set(VCPKG_TARGET_TRIPLET x86-windows) + set(VCPKG_TARGET_TRIPLET "x86-windows") elseif(${CMAKE_GENERATOR_PLATFORM} STREQUAL x64) - set(VCPKG_TARGET_TRIPLET x64-windows) + set(VCPKG_TARGET_TRIPLET "x64-windows") else() message(FATAL_ERROR "Unsupported Generator Platform, only Win32 or x64 are supported") endif() @@ -33,14 +33,15 @@ set (CMAKE_CXX_STANDARD 14) if(UNIX) SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS} -fPIC -g -O3") SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC -O3 -g") + set(OpenGL_GL_PREFERENCE GLVND) endif(UNIX) -# Find needed libraries +# Find required libraries FIND_PACKAGE(OpenGL REQUIRED) include_directories(${OPENGL_INCLUDE_DIR}) FIND_PACKAGE(SDL2 REQUIRED) -include_directories(${SDL2_INCLUDE_DIR}) +include_directories(${SDL2_INCLUDE_DIRS}) if(UNIX AND NOT APPLE) FIND_PACKAGE(EGL REQUIRED) @@ -62,35 +63,44 @@ include_directories(${SDL2_TTF_DIRS}) include_directories(${SDL2_GFX_DIRS}) if(UNIX) - find_package(PythonLibs 3 REQUIRED) -else() - set(PYTHONLIBS_VERSION_STRING $ENV{PY_VERSION}) -endif() - -message("Using Python: ${PYTHONLIBS_VERSION_STRING}") -string(REPLACE "." ";" VERSION_LIST ${PYTHONLIBS_VERSION_STRING}) -list(GET VERSION_LIST 0 PYTHON_MAJOR) -list(GET VERSION_LIST 1 PYTHON_MINOR) - -if(WIN32) + if (${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.12") + find_package(Python COMPONENTS Development REQUIRED) + else() + # Starting from CMake 3.12 FindPythonLibs was deprecated in favour of FindPython. + # But Ubuntu 18.04 and other older distributions have CMake version less than 3.12, + # so we have to support the old behaviour for some time. + find_package(PythonLibs 3 REQUIRED) + string(REPLACE "." ";" VERSION_LIST ${PYTHONLIBS_VERSION_STRING}) + list(GET VERSION_LIST 0 Python_VERSION_MAJOR) + list(GET VERSION_LIST 1 Python_VERSION_MINOR) + set(Python_INCLUDE_DIRS ${PYTHON_INCLUDE_DIRS}) + set(Python_LIBRARIES ${PYTHON_LIBRARIES}) + endif() +elseif(WIN32) # find_package (Python COMPONENTS Development) # FindPython package searches for system-wide python installations first. # It was improved only recently (Jan 2021), so we can't use it for earlier py versions: # (see https://github.com/microsoft/vcpkg/pull/15221 ) # To avoid version mismatch we point to vcpkg's libs directly - set(PYTHON_LIBRARIES "${CMAKE_BINARY_DIR}/vcpkg_installed/${VCPKG_TARGET_TRIPLET}/lib/python${PYTHON_MAJOR}${PYTHON_MINOR}.lib") - set(PYTHON_INCLUDE_DIR "${CMAKE_BINARY_DIR}/vcpkg_installed/${VCPKG_TARGET_TRIPLET}/include/python${PYTHON_MAJOR}.${PYTHON_MINOR}") + string(REPLACE "." ";" VERSION_LIST $ENV{PY_VERSION}) + list(GET VERSION_LIST 0 Python_VERSION_MAJOR) + list(GET VERSION_LIST 1 Python_VERSION_MINOR) + set(Python_INCLUDE_DIRS "${CMAKE_BINARY_DIR}/vcpkg_installed/${VCPKG_TARGET_TRIPLET}/include/python${Python_VERSION_MAJOR}.${Python_VERSION_MINOR}") + set(Python_LIBRARIES "${CMAKE_BINARY_DIR}/vcpkg_installed/${VCPKG_TARGET_TRIPLET}/lib/python${Python_VERSION_MAJOR}${Python_VERSION_MINOR}.lib") endif() +message("Using Python: ${Python_VERSION_MAJOR}.${Python_VERSION_MINOR}") +include_directories(${Python_INCLUDE_DIRS}) + # Include Boost Python. if(WIN32 OR APPLE) - set(BOOST_PYTHON_VERSION python${PYTHON_MAJOR}${PYTHON_MINOR}) + set(BOOST_PYTHON_VERSION python${Python_VERSION_MAJOR}${Python_VERSION_MINOR}) else() # Unfortunately different linux distributions are using different naming for boost-python - # list of prefixes: Ubuntu, Debian, Some other distributions - set(BOOST_PYTHON_PREFIXES python3-py python-py python) + # list of prefixes: newer Ubuntu and other distributions, older Ubuntu, Debian + set(BOOST_PYTHON_PREFIXES python python3-py python-py) foreach(BP_PREFIX ${BOOST_PYTHON_PREFIXES}) - set(BOOST_PYTHON_NAME ${BP_PREFIX}${PYTHON_MAJOR}${PYTHON_MINOR}) + set(BOOST_PYTHON_NAME ${BP_PREFIX}${Python_VERSION_MAJOR}${Python_VERSION_MINOR}) string(TOUPPER ${BOOST_PYTHON_NAME} BOOST_PYTHON_NAME_UPPER) FIND_PACKAGE(Boost COMPONENTS ${BOOST_PYTHON_NAME}) if(Boost_${BOOST_PYTHON_NAME_UPPER}_FOUND) @@ -103,12 +113,10 @@ else() message(FATAL_ERROR "Boost Python not found") endif() endif() -message("Using python_boost: ${BOOST_PYTHON_VERSION}") +message("Using Boost.Python: ${BOOST_PYTHON_VERSION}") FIND_PACKAGE(Boost REQUIRED COMPONENTS thread system filesystem ${BOOST_PYTHON_VERSION}) -include_directories(${Boost_INCLUDE_DIR}) - -include_directories(${PYTHON_INCLUDE_DIR}) +include_directories(${Boost_INCLUDE_DIRS}) include_directories(${PROJECT_SOURCE_DIR}/src) include_directories(${PROJECT_SOURCE_DIR}/src/cmake) @@ -151,7 +159,7 @@ endif() set(LIBRARIES gamelib menulib datalib blunted2 Boost::filesystem Boost::system Boost::thread Boost::${BOOST_PYTHON_VERSION} - ${PYTHON_LIBRARIES} ${SDL2_LIBS_ALL} ${EGL_LIBRARIES} ${OPENGL_LIBRARY}) + ${Python_LIBRARIES} ${SDL2_LIBS_ALL} ${EGL_LIBRARIES} ${OPENGL_LIBRARIES}) if(UNIX) set(LIB_NAME game)