adepierre / Botcraft

Botcraft is a cross-platform C++ library to create bots that connect and interact with Minecraft servers with (optional) integrated OpenGL renderer
GNU General Public License v3.0
405 stars 43 forks source link

Build Failure On Multiple Builds #137

Closed DanDucky closed 4 months ago

DanDucky commented 5 months ago

When my IDE (CLion) re-runs the cmake build command on my cmake-build-debug config, it fails with the following errors:

CMake Error in cmake-build-debug/_deps/botcraft-src/CMakeLists.txt:
  IMPORTED_LOCATION not set for imported target "OpenSSL::SSL" configuration
  "Debug".

CMake Error in cmake-build-debug/_deps/botcraft-src/CMakeLists.txt:
  IMPORTED_LOCATION not set for imported target "OpenSSL::Crypto"
  configuration "Debug".

CMake Error in cmake-build-debug/_deps/botcraft-src/botcraft/CMakeLists.txt:
  Imported target "OpenSSL::SSL" includes non-existent path

    "/install/include"

  in its INTERFACE_INCLUDE_DIRECTORIES.  Possible reasons include:

  * The path was deleted, renamed, or moved to another location.

  * An install or uninstall procedure did not complete successfully.

  * The installation package was faulty and references files it does not
  provide.

CMake Error in cmake-build-debug/_deps/botcraft-src/CMakeLists.txt:
  IMPORTED_LOCATION not set for imported target "OpenSSL::SSL" configuration
  "Debug".

CMake Error in cmake-build-debug/_deps/botcraft-src/CMakeLists.txt:
  IMPORTED_LOCATION not set for imported target "OpenSSL::Crypto"
  configuration "Debug".

I'm compiling Botcraft with these settings to make it build statically:

            "BOTCRAFT_BUILD_EXAMPLES OFF"
            "BOTCRAFT_BUILD_DOC OFF"
            "BOTCRAFT_GAME_VERSION 1.20.4"
            "BOTCRAFT_STATIC ON"
            "PROTOCOLCRAFT_STATIC ON"
            "BOTCRAFT_FORCE_LOCAL_ZLIB ON"
            "BOTCRAFT_FORCE_LOCAL_OPENSSL ON"
            "BUILD_SHARED_LIBS OFF"

I've narrowed down the issue to a simpler CMakeLists.txt and settings. With this CMakeLists the error occurs if you configure and build, change the CMakeLists, then configure and build again then it fails.

cmake_minimum_required(VERSION 3.20)
project(TheHand)

set(CMAKE_CXX_STANDARD 23)

#include(cmake/botcraft.cmake)
set(BOTCRAFT_BUILD_EXAMPLES OFF)
set(BOTCRAFT_BUILD_DOC OFF)
set(BOTCRAFT_GAME_VERSION 1.20.4)
set(BOTCRAFT_STATIC ON)
set(PROTOCOLCRAFT_STATIC ON)
set(BOTCRAFT_FORCE_LOCAL_ZLIB ON)
set(BOTCRAFT_FORCE_LOCAL_OPENSSL ON)
set(BUILD_SHARED_LIBS OFF)

add_subdirectory(3rdparty/Botcraft)

add_executable(TheHand src/main.cpp)

target_include_directories(TheHand PUBLIC 3rdparty/botcraft/include)

target_link_libraries(TheHand m botcraft)
set(CMAKE_CXX_FLAGS "-static")

install(TARGETS TheHand)

I don't believe the issue is related to the static build, because removing any static options changes nothing. However the issue does not persist when I remove the BOTCRAFT_FORCELOCAL* options, so it should be related to how botcraft adds these libraries. The build does work with the CMakeLists above, but only if the CMakeLists does not change. As soon as you change the CMakeLists and update the config and build again, the build fails.

I don't really know how else to describe this issue because I don't know that much about cmake. The last thing I can really describe about this project is the structure:

|-3rdParty | |-Botcraft | | |-CMakeLists.txt |-src | |-main.cpp |-CMakeLists.txt

adepierre commented 5 months ago

Thanks for the detailed report.

I don't know much about CLion, but I think the issue is that the cmake build system only installs a Release version of the dependencies that are not found on the system (or the one forced using FORCE_LOCAL options) and assumes they are compatible with the currently used config. I haven't used a Debug config in years as at one point it was too slow and ended up time out the connection quite often. I usually use a RelWithDebInfo configuration as a result.

Anyway, I'll try to see if I can install CLion and give it a try to see if I can fix it for next monthly release. In the meantime, you could try installing a debug version of openssl (and zlib ?) manually and set OPENSSL_ROOT_DIR to the right path with the hope that cmake finds it properly.

DanDucky commented 5 months ago

Ok yeah I think you're right, the build system only configures dependencies as release configs. I just reproduced the error without CLion as well with the following commands:

cmake -DCMAKE_BUILD_TYPE=Debug ..
cmake --build . --config Debug
# works so far, builds exe
nvim ../CMakeLists.txt
# add a simple message("TEST") to CMakeLists.txt
cmake -DCMAKE_BUILD_TYPE=Debug ..
TEST
-- Selected game version: 1.20.4 || Protocol: 765
-- Configuring done (0.1s)
CMake Error in 3rdparty/Botcraft/CMakeLists.txt:
  IMPORTED_LOCATION not set for imported target "OpenSSL::SSL" configuration
  "Debug".

CMake Error in 3rdparty/Botcraft/CMakeLists.txt:
  IMPORTED_LOCATION not set for imported target "OpenSSL::Crypto"
  configuration "Debug".

CMake Error in 3rdparty/Botcraft/botcraft/CMakeLists.txt:
  Imported target "OpenSSL::SSL" includes non-existent path

    "/install/include"

  in its INTERFACE_INCLUDE_DIRECTORIES.  Possible reasons include:

  * The path was deleted, renamed, or moved to another location.

  * An install or uninstall procedure did not complete successfully.

  * The installation package was faulty and references files it does not
  provide.

CMake Error in 3rdparty/Botcraft/CMakeLists.txt:
  IMPORTED_LOCATION not set for imported target "OpenSSL::SSL" configuration
  "Debug".

CMake Error in 3rdparty/Botcraft/CMakeLists.txt:
  IMPORTED_LOCATION not set for imported target "OpenSSL::Crypto"
  configuration "Debug".

-- Generating done (0.1s)
CMake Generate step failed.  Build files cannot be regenerated correctly.
adepierre commented 5 months ago

I took a bit of time to look around and I think I found the issue. It was in fact not related to CLion or Debug config. It was just a cmake-variable-scope issue for local openssl. Will push the fix in the next release, here is the diff in the meantime:

diff --git a/cmake/openssl.cmake b/cmake/openssl.cmake
index 335103e5..887709b7 100644
--- a/cmake/openssl.cmake
+++ b/cmake/openssl.cmake
@@ -8,7 +8,7 @@ endif()
 # If not found, build from sources
 if(NOT OPENSSL_FOUND)
     set(OPENSSL_SRC_PATH "${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/openssl/")
-    set(OPENSSL_BUILD_PATH "${CMAKE_CURRENT_BINARY_DIR}/3rdparty/openssl")
+    set(OPENSSL_BUILD_PATH "${CMAKE_CURRENT_BINARY_DIR}/3rdparty/openssl" CACHE INTERNAL "Local OpenSSL build path")

     file(GLOB RESULT "${OPENSSL_BUILD_PATH}/install")
     list(LENGTH RESULT RES_LEN)

Also, as the code is almost never tested with CLion/MinGW feel free to report any bug or weird compilation stuff either here or in the discord.