Skip to content

Commit

Permalink
Fixed building for Lua, added additional searching for libpython in s…
Browse files Browse the repository at this point in the history
…etup.py for old cmake and Anaconda versions

Former-commit-id: b90ff9a
  • Loading branch information
mwydmuch committed Sep 28, 2017
1 parent 6a7a1a9 commit 697c27c
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 33 deletions.
63 changes: 39 additions & 24 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ project(ViZDoom)

# Project version
#-----------------------------------------------------------------------------------------------------------------------

set(ViZDoom_MAJOR_VERSION 1)
set(ViZDoom_MINOR_VERSION 1)
set(ViZDoom_PATCH_VERSION 4)
Expand All @@ -13,15 +14,20 @@ set(ViZDoom_VERSION_ID ${ViZDoom_MAJOR_VERSION}${ViZDoom_MINOR_VERSION}${ViZDoom

# Building options
#-----------------------------------------------------------------------------------------------------------------------

option(BUILD_PYTHON "Build ViZDoom Python binding." OFF)
option(BUILD_PYTHON3 "Build ViZDoom Python3 binding instead of default Python2." OFF)
option(BUILD_LUA "Build ViZDoom Lua binding." OFF) # only for Lua 5.1 and LuaJIT
option(BUILD_JAVA "Build ViZDoom Java binding." OFF)
option(BUILD_ENGINE "Build ViZDoom game engine." ON)
option(DOWNLOAD_FREEDOOM "Download freedoom2.wad." ON)


# CMake options
#-----------------------------------------------------------------------------------------------------------------------

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_LINK)
if (COMMAND cmake_policy)
cmake_policy(SET CMP0003 NEW)
endif (COMMAND cmake_policy)
Expand All @@ -39,6 +45,7 @@ endif ()

# System dependent settings
#-----------------------------------------------------------------------------------------------------------------------

if (UNIX)
set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON)
Expand Down Expand Up @@ -74,6 +81,7 @@ endif (APPLE)

# Compiler dependent settings
#-----------------------------------------------------------------------------------------------------------------------

if (CMAKE_COMPILER_IS_GNUCXX)
add_definitions("-fPIC")
endif ()
Expand All @@ -88,6 +96,7 @@ endif ()

# Targets
#-----------------------------------------------------------------------------------------------------------------------

find_package(Boost COMPONENTS filesystem thread system date_time chrono regex iostreams REQUIRED)
find_package(Threads REQUIRED)

Expand Down Expand Up @@ -124,10 +133,10 @@ file(GLOB VIZDOOM_LIB_SOURCES
${VIZDOOM_LIB_SRC_DIR}/*.cpp)

add_library(libvizdoom_static STATIC ${VIZDOOM_LIB_SOURCES})
target_link_libraries(libvizdoom_static ${VIZDOOM_LIBS})
target_link_libraries(libvizdoom_static PRIVATE ${VIZDOOM_LIBS})

add_library(libvizdoom_shared SHARED ${VIZDOOM_LIB_SOURCES})
target_link_libraries(libvizdoom_shared ${VIZDOOM_LIBS})
target_link_libraries(libvizdoom_shared PRIVATE ${VIZDOOM_LIBS})

set_target_properties(libvizdoom_static libvizdoom_shared
PROPERTIES
Expand All @@ -152,12 +161,14 @@ set_target_properties(libvizdoom_shared
PROPERTIES
PROJECT_LABEL "Shared library")


add_subdirectory(${VIZDOOM_SRC_DIR}/vizdoom)
if (BUILD_ENGINE)
add_subdirectory(${VIZDOOM_SRC_DIR}/vizdoom)
endif ()


# Python binding
# Python bindingcd
#-----------------------------------------------------------------------------------------------------------------------

if (BUILD_PYTHON OR BUILD_PYTHON3)

if (BUILD_PYTHON3)
Expand All @@ -173,6 +184,8 @@ if (BUILD_PYTHON OR BUILD_PYTHON3)
set(VIZDOOM_PYTHON_OUTPUT_DIR ${VIZDOOM_OUTPUT_DIR}/python${_PYTHON_VERSION})
set(VIZDOOM_PYTHON_SRC_DIR ${VIZDOOM_SRC_DIR}/lib_python)
set(VIZDOOM_PYTHON_INCLUDE_DIR ${VIZDOOM_INCLUDE_DIR} ${VIZDOOM_PYTHON_SRC_DIR})

# Use included pybind11
add_subdirectory(${VIZDOOM_PYTHON_SRC_DIR}/pybind11)

set(VIZDOOM_PYTHON_LIBS
Expand Down Expand Up @@ -202,9 +215,9 @@ if (BUILD_PYTHON OR BUILD_PYTHON3)
LIBRARY_OUTPUT_DIRECTORY_RELWITHDEBINFO ${VIZDOOM_PYTHON_OUTPUT_DIR}
OUTPUT_NAME vizdoom
PROJECT_LABEL "Python binding")
add_dependencies(libvizdoom_python vizdoom pk3 freedoom2 pybind11)
add_dependencies(libvizdoom_python pybind11)

if (UNIX)
if (UNIX AND BUILD_ENGINE AND DOWNLOAD_FREEDOOM)
add_custom_target(python_pip_package ALL
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/scripts/assemble_pip_package.sh ${_PYTHON_VERSION}
COMMENT "Assembling pip package in ${VIZDOOM_PYTHON_OUTPUT_DIR}/pip_package")
Expand All @@ -218,31 +231,26 @@ endif ()

# Lua binding
#-----------------------------------------------------------------------------------------------------------------------
if (BUILD_LUA)

# Use ViZDoom's custom version of luabind
#find_package(Luabind REQUIRED)

find_package(Lua51 REQUIRED)
#find_package(Torch REQUIRED)
if (BUILD_LUA)

set(VIZDOOM_LUA_OUTPUT_DIR ${VIZDOOM_OUTPUT_DIR}/lua)
set(VIZDOOM_LUA_SRC_DIR ${VIZDOOM_SRC_DIR}/lib_lua)
set(VIZDOOM_LUA_INCLUDE_DIR ${VIZDOOM_INCLUDE_DIR} ${VIZDOOM_LUA_SRC_DIR})

# Use ViZDoom's custom version of luabind
add_subdirectory(${VIZDOOM_LUA_SRC_DIR}/luabind)
find_package(Lua51 REQUIRED)

set(VIZDOOM_LUA_LIBS
${VIZDOOM_LIBS}
${Boost_LIBRARIES}
${LUA_LIBRARIES}
#${LUABIND_LIBRARIES}
#${VIZDOOM_SRC_DIR}/lib_lua/luabind/src/libluabind.a
luabind)

include_directories(${VIZDOOM_LUA_INCLUDE_DIR}
${Boost_INCLUDE_DIR}
${LUA_INCLUDE_DIR}
#${LUABIND_INCLUDE_DIRS}
${VIZDOOM_SRC_DIR}/lib_lua/luabind)

set(VIZDOOM_LUA_SOURCES
Expand All @@ -262,8 +270,9 @@ if (BUILD_LUA)
LIBRARY_OUTPUT_DIRECTORY_RELWITHDEBINFO ${VIZDOOM_LUA_OUTPUT_DIR}
OUTPUT_NAME vizdoom
PROJECT_LABEL "Lua binding")
add_dependencies(libvizdoom_python luabind)

if (UNIX)
if (UNIX AND BUILD_ENGINE AND DOWNLOAD_FREEDOOM)
add_custom_target(luarocks_package ALL
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/scripts/assemble_luarocks_package.sh
COMMENT "Assembling luarocks package in ${VIZDOOM_LUA_OUTPUT_DIR}/luarocks_package")
Expand All @@ -277,33 +286,39 @@ endif ()

# Java binding
#-----------------------------------------------------------------------------------------------------------------------

if (BUILD_JAVA)

if (NOT DEFINED ENV{JAVA_HOME})
message(FATAL_ERROR "JAVA_HOME environment variable is not set.")
endif ()

find_package(Java REQUIRED)
find_package(JNI REQUIRED)

include(UseJava)

set(VIZDOOM_JAVA_OUTPUT_DIR ${VIZDOOM_OUTPUT_DIR}/java)
set(VIZDOOM_JAVA_SRC_DIR ${VIZDOOM_SRC_DIR}/lib_java)
set(VIZDOOM_JAVA_CLASSES_DIR ${VIZDOOM_JAVA_SRC_DIR}/java_classes)
set(VIZDOOM_JAVA_INCLUDE_DIR ${VIZDOOM_INCLUDE_DIR} ${VIZDOOM_JAVA_SRC_DIR})

find_package(Java REQUIRED)
find_package(JNI REQUIRED)
include(UseJava)

set(VIZDOOM_JAVA_LIBS
${VIZDOOM_LIBS}
${Java_LIBRARIES}
${JNI_LIBRARIES})

if (UNIX)
if (UNIX AND NOT APPLE)
set(JNI_INCLUDE_DIR ${JNI_INCLUDE_DIR}
${_JAVA_HOME}/include
${_JAVA_HOME}/include/linux)
endif ()

if (APPLE)
set(JNI_INCLUDE_DIR ${JNI_INCLUDE_DIR}
${_JAVA_HOME}/include
${_JAVA_HOME}/include/darwin)
endif ()

if (WIN32)
set(JNI_INCLUDE_DIR ${JNI_INCLUDE_DIR}
${_JAVA_HOME}/include
Expand Down Expand Up @@ -365,7 +380,7 @@ endif ()
# Freedoom 2 download
#-----------------------------------------------------------------------------------------------------------------------

if (UNIX)
if (UNIX AND DOWNLOAD_FREEDOOM)
add_custom_target(freedoom2 ALL
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/scripts/download_freedoom.sh
COMMENT "Downloading freedoom2.wad")
Expand Down
10 changes: 5 additions & 5 deletions doc/Building.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,25 +171,25 @@ make

Users with brew-installed Python/Anaconda may need to manually set PYTHON_EXECUTABLE, PYTHON_INCLUDE_DIR, PYTHON_LIBRARY:

It should look like this for brew-installed Python (use `-DBUILD_PYTHON3=ON` and `libpythonX.Xm.dylib` for Python 3):
It should look like this for brew-installed Python (use `-DBUILD_PYTHON3=ON`, `include/pythonX.Xm` and `lib/libpythonX.Xm.dylib` for Python 3):

```sh
cmake -DCMAKE_BUILD_TYPE=Release \
-DBUILD_PYTHON=ON \
-DPYTHON_EXECUTABLE=/usr/local/Cellar/python/X.X.X/Frameworks/Python.framework/Versions/X.X/bin/pythonX.X \
-DPYTHON_EXECUTABLE=/usr/local/Cellar/python/X.X.X/Frameworks/Python.framework/Versions/X.X/bin/pythonX \
-DPYTHON_INCLUDE_DIR=/usr/local/Cellar/python/X.X.X/Frameworks/Python.framework/Versions/X.X/include/pythonX.X \
-DPYTHON_LIBRARY=/usr/local/Cellar/python/X.X.X/Frameworks/Python.framework/Versions/X.X/lib/libpythonX.X.dylib \
-DNUMPY_INCLUDES=/usr/local/Cellar/python/X.X.X/Frameworks/Python.framework/Versions/X.X/lib/pythonX.X/site-packages/numpy/core/include
```

Or for Anaconda (use `-DBUILD_PYTHON3=ON` and `libpythonX.Xm.dylib` for Python 3):
Or for Anaconda (use `-DBUILD_PYTHON3=ON`, `include/pythonX.Xm` and `lib/libpythonX.Xm.dylib` for Python 3):

```sh
cmake -DCMAKE_BUILD_TYPE=Release \
-DBUILD_PYTHON=ON \
-DPYTHON_INCLUDE_DIR=~/anacondaX/include/pythonX \
-DPYTHON_EXECUTABLE=~/anacondaX/bin/pythonX \
-DPYTHON_INCLUDE_DIR=~/anacondaX/include/pythonX.X \
-DPYTHON_LIBRARY=~/anacondaX/lib/libpythonX.X.dylib \
-DPYTHON_EXECUTABLE=~/anacondaX/bin/pythonX.X \
-DNUMPY_INCLUDES=~/anacondaX/lib/pythonX.X/site-packages/numpy/core/include
```

Expand Down
23 changes: 23 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ def get_vizdoom_version():
"you should create an issue at https://github.com/mwydmuch/ViZDoom/")


def get_python_library(python_lib_dir):
if python_version[0] == 2:
python_lib_name = 'libpython{}.{}'
else:
python_lib_name = 'libpython{}m.{}'

python_lib_name.format(python_version, library_extension)
python_library = os.path.join(python_lib_dir, python_lib_name)
return python_library


class BuildCommand(build):
def run(self):
try:
Expand All @@ -51,6 +62,18 @@ def run(self):
cmake_arg_list.append("-DBUILD_PYTHON=ON")
cmake_arg_list.append("-DPYTHON_EXECUTABLE={}".format(python_executable))

python_standard_lib = sysconfig.get_python_lib(standard_lib=True)
python_site_packages = sysconfig.get_python_lib(standard_lib=False)
python_lib_dir = os.path.dirname(python_standard_lib)
python_library = get_python_library(python_lib_dir)
python_include_dir = sysconfig.get_python_inc()
numpy_include_dir = os.path.join(python_site_packages, "numpy/core/include")

if os.path.exists(python_library) and os.path.exists(python_include_dir) and os.path.exists(numpy_include_dir):
cmake_arg_list.append("-DPYTHON_LIBRARY={}".format(python_library))
cmake_arg_list.append("-DPYTHON_INCLUDE_DIR={}".format(python_include_dir))
cmake_arg_list.append("-DNUMPY_INCLUDES={}".format(numpy_include_dir))

if python_version[0] == "3":
cmake_arg_list.append("-DBUILD_PYTHON3=ON")

Expand Down
4 changes: 2 additions & 2 deletions src/lib_python/ViZDoomGamePython.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
namespace vizdoom {

#if PY_MAJOR_VERSION >= 3
int init_numpy() {
void* init_numpy() {
import_array();
return 0;
return NULL;
}
#else
void init_numpy() {
Expand Down
4 changes: 2 additions & 2 deletions src/lib_python/ViZDoomPythonModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ double (*doomFixedToDouble_double)(double) = &doomFixedToDouble;
/*--------------------------------------------------------------------------------------------------------------------*/

#if PY_MAJOR_VERSION >= 3
int init_numpy() {
void* init_numpy() {
import_array();
return 0;
return NULL;
}
#else
void init_numpy() {
Expand Down

0 comments on commit 697c27c

Please sign in to comment.