diff --git a/AI/BattleAI/CMakeLists.txt b/AI/BattleAI/CMakeLists.txt index 337fa9524..f09bbd87a 100644 --- a/AI/BattleAI/CMakeLists.txt +++ b/AI/BattleAI/CMakeLists.txt @@ -1,11 +1,8 @@ set(battleAI_SRCS - StdInc.cpp - AttackPossibility.cpp BattleAI.cpp common.cpp EnemyInfo.cpp - main.cpp PossibleSpellcast.cpp PotentialTargets.cpp StackWithBonuses.cpp @@ -27,9 +24,14 @@ set(battleAI_HEADERS BattleExchangeVariant.h ) +if(NOT ENABLE_STATIC_AI_LIBS) + list(APPEND battleAI_SRCS main.cpp StdInc.cpp) +endif() assign_source_group(${battleAI_SRCS} ${battleAI_HEADERS}) -if(ANDROID) # android compiles ai libs into main lib directly, so we skip this library and just reuse sources list +if(ENABLE_STATIC_AI_LIBS) + list(TRANSFORM battleAI_SRCS PREPEND "${CMAKE_CURRENT_SOURCE_DIR}/") + set(VCMILIB_AI_SOURCES ${VCMILIB_AI_SOURCES} ${battleAI_SRCS} PARENT_SCOPE) return() endif() diff --git a/AI/CMakeLists.txt b/AI/CMakeLists.txt index 07d63504e..59d1b3d39 100644 --- a/AI/CMakeLists.txt +++ b/AI/CMakeLists.txt @@ -49,10 +49,12 @@ endif() add_subdirectory(BattleAI) add_subdirectory(VCAI) -if(NOT ANDROID) +if(NOT ENABLE_STATIC_AI_LIBS) add_subdirectory(StupidAI) add_subdirectory(EmptyAI) endif() if(ENABLE_NULLKILLER_AI) add_subdirectory(Nullkiller) endif() +set(VCMILIB_AI_SOURCES ${VCMILIB_AI_SOURCES} PARENT_SCOPE) +set(VCMILIB_AI_LIBRARIES ${VCMILIB_AI_LIBRARIES} PARENT_SCOPE) diff --git a/AI/Nullkiller/CMakeLists.txt b/AI/Nullkiller/CMakeLists.txt index 7af251a30..aaf66bd1c 100644 --- a/AI/Nullkiller/CMakeLists.txt +++ b/AI/Nullkiller/CMakeLists.txt @@ -1,6 +1,4 @@ set(Nullkiller_SRCS - StdInc.cpp - Pathfinding/AIPathfinderConfig.cpp Pathfinding/AIPathfinder.cpp Pathfinding/AINodeStorage.cpp @@ -54,7 +52,6 @@ set(Nullkiller_SRCS Behaviors/BuildingBehavior.cpp Behaviors/GatherArmyBehavior.cpp Behaviors/ClusterBehavior.cpp - main.cpp AIGateway.cpp ) @@ -120,19 +117,23 @@ set(Nullkiller_HEADERS AIGateway.h ) +if(NOT ENABLE_STATIC_AI_LIBS) + list(APPEND Nullkiller_SRCS main.cpp StdInc.cpp) +endif() assign_source_group(${Nullkiller_SRCS} ${Nullkiller_HEADERS}) -if(ANDROID) # android compiles ai libs into main lib directly, so we skip this library and just reuse sources list +list(APPEND requiredLibs fuzzylite::fuzzylite TBB::tbb) + +if(ENABLE_STATIC_AI_LIBS) + list(TRANSFORM Nullkiller_SRCS PREPEND "${CMAKE_CURRENT_SOURCE_DIR}/") + set(VCMILIB_AI_SOURCES ${VCMILIB_AI_SOURCES} ${Nullkiller_SRCS} PARENT_SCOPE) + set(VCMILIB_AI_LIBRARIES ${VCMILIB_AI_LIBRARIES} ${requiredLibs} PARENT_SCOPE) return() endif() add_library(Nullkiller SHARED ${Nullkiller_SRCS} ${Nullkiller_HEADERS}) - target_include_directories(Nullkiller PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) - -target_link_libraries(Nullkiller PRIVATE ${VCMI_LIB_TARGET} fuzzylite::fuzzylite) - -target_link_libraries(Nullkiller PRIVATE TBB::tbb) +target_link_libraries(Nullkiller PRIVATE ${VCMI_LIB_TARGET} ${requiredLibs}) vcmi_set_output_dir(Nullkiller "AI") enable_pch(Nullkiller) diff --git a/AI/VCAI/CMakeLists.txt b/AI/VCAI/CMakeLists.txt index 259129afe..93719cf39 100644 --- a/AI/VCAI/CMakeLists.txt +++ b/AI/VCAI/CMakeLists.txt @@ -1,6 +1,4 @@ set(VCAI_SRCS - StdInc.cpp - Pathfinding/AIPathfinderConfig.cpp Pathfinding/AIPathfinder.cpp Pathfinding/AINodeStorage.cpp @@ -42,7 +40,6 @@ set(VCAI_SRCS Goals/GetArtOfType.cpp Goals/FindObj.cpp Goals/CompleteQuest.cpp - main.cpp VCAI.cpp ) @@ -97,17 +94,23 @@ set(VCAI_HEADERS VCAI.h ) +if(NOT ENABLE_STATIC_AI_LIBS) + list(APPEND VCAI_SRCS main.cpp StdInc.cpp) +endif() assign_source_group(${VCAI_SRCS} ${VCAI_HEADERS}) -if(ANDROID) # android compiles ai libs into main lib directly, so we skip this library and just reuse sources list +list(APPEND requiredLibs fuzzylite::fuzzylite) + +if(ENABLE_STATIC_AI_LIBS) + list(TRANSFORM VCAI_SRCS PREPEND "${CMAKE_CURRENT_SOURCE_DIR}/") + set(VCMILIB_AI_SOURCES ${VCMILIB_AI_SOURCES} ${VCAI_SRCS} PARENT_SCOPE) + set(VCMILIB_AI_LIBRARIES ${VCMILIB_AI_LIBRARIES} ${requiredLibs} PARENT_SCOPE) return() endif() add_library(VCAI SHARED ${VCAI_SRCS} ${VCAI_HEADERS}) - target_include_directories(VCAI PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) - -target_link_libraries(VCAI PRIVATE ${VCMI_LIB_TARGET} fuzzylite::fuzzylite) +target_link_libraries(VCAI PRIVATE ${VCMI_LIB_TARGET} ${requiredLibs}) vcmi_set_output_dir(VCAI "AI") enable_pch(VCAI) diff --git a/CMakeLists.txt b/CMakeLists.txt index 404f6d7d0..528800b78 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -42,8 +42,10 @@ if(NOT CMAKE_BUILD_TYPE) endif() set(qtParts ON) +set(staticAI OFF) if(ANDROID) set(qtParts OFF) + set(staticAI ON) endif() option(ENABLE_ERM "Enable compilation of ERM scripting module" OFF) @@ -67,6 +69,7 @@ option(ENABLE_STRICT_COMPILATION "Treat all compiler warnings as errors" OFF) option(ENABLE_MULTI_PROCESS_BUILDS "Enable /MP flag for MSVS solution" ON) option(ENABLE_SINGLE_APP_BUILD "Builds client and server as single executable" OFF) option(COPY_CONFIG_ON_BUILD "Copies config folder into output directory at building phase" ON) +option(ENABLE_STATIC_AI_LIBS "Add AI code into VCMI lib directly" ${staticAI}) # Used for Snap packages and also useful for debugging if(NOT APPLE_IOS AND NOT ANDROID) @@ -522,6 +525,8 @@ if(APPLE_IOS) endif() set(VCMI_LIB_TARGET vcmi) +add_subdirectory_with_folder("AI" AI) + include(VCMI_lib) add_subdirectory(lib) if(ENABLE_SINGLE_APP_BUILD) @@ -547,7 +552,6 @@ if(ENABLE_EDITOR) endif() add_subdirectory(client) add_subdirectory(server) -add_subdirectory_with_folder("AI" AI) if(ENABLE_TEST) enable_testing() add_subdirectory(test) diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt index 64f94d982..01a069ce1 100644 --- a/client/CMakeLists.txt +++ b/client/CMakeLists.txt @@ -251,12 +251,16 @@ else() add_executable(vcmiclient ${client_SRCS} ${client_HEADERS}) endif() -add_dependencies(vcmiclient vcmiserver BattleAI VCAI) -if(NOT ANDROID) - add_dependencies(vcmiclient StupidAI) -endif() -if(ENABLE_NULLKILLER_AI) - add_dependencies(vcmiclient Nullkiller) +add_dependencies(vcmiclient vcmiserver) +if(NOT ENABLE_STATIC_AI_LIBS) + add_dependencies(vcmiclient + BattleAI + StupidAI + VCAI + ) + if(ENABLE_NULLKILLER_AI) + add_dependencies(vcmiclient Nullkiller) + endif() endif() if(APPLE_IOS) if(ENABLE_ERM) diff --git a/cmake_modules/VCMIUtils.cmake b/cmake_modules/VCMIUtils.cmake index e2ac01762..7ed85ab9d 100644 --- a/cmake_modules/VCMIUtils.cmake +++ b/cmake_modules/VCMIUtils.cmake @@ -39,10 +39,10 @@ function(assign_source_group) endfunction(assign_source_group) # Macro to add subdirectory and set appropriate FOLDER for generated projects files -function(add_subdirectory_with_folder _folder_name _folder) +macro(add_subdirectory_with_folder _folder_name _folder) add_subdirectory(${_folder} ${ARGN}) set_property(DIRECTORY "${_folder}" PROPERTY FOLDER "${_folder_name}") -endfunction() +endmacro() ####################################### # CMake debugging # diff --git a/cmake_modules/VCMI_lib.cmake b/cmake_modules/VCMI_lib.cmake index e363833e1..15aeb9892 100644 --- a/cmake_modules/VCMI_lib.cmake +++ b/cmake_modules/VCMI_lib.cmake @@ -199,8 +199,6 @@ macro(add_main_lib TARGET_NAME LIBRARY_TYPE) ${MAIN_LIB_DIR}/TerrainHandler.cpp ${MAIN_LIB_DIR}/VCMIDirs.cpp ${MAIN_LIB_DIR}/VCMI_Lib.cpp - - ${VCMILIB_ADDITIONAL_SOURCES} ) # Version.cpp is a generated file diff --git a/lib/CAndroidVMHelper.cpp b/lib/CAndroidVMHelper.cpp index 7b67f8b18..34428668b 100644 --- a/lib/CAndroidVMHelper.cpp +++ b/lib/CAndroidVMHelper.cpp @@ -109,8 +109,4 @@ void CAndroidVMHelper::initClassloader(void * baseEnv) vcmiFindClassMethod = env->GetMethodID(classLoaderClass, "findClass", "(Ljava/lang/String;)Ljava/lang/Class;"); } -extern "C" JNIEXPORT void JNICALL Java_eu_vcmi_vcmi_NativeMethods_initClassloader(JNIEnv * baseEnv, jclass cls) -{ - CAndroidVMHelper::initClassloader(baseEnv); -} #endif diff --git a/lib/CGameInterface.cpp b/lib/CGameInterface.cpp index 10a8394b0..2b3c0fb45 100644 --- a/lib/CGameInterface.cpp +++ b/lib/CGameInterface.cpp @@ -13,29 +13,27 @@ #include "CStack.h" #include "VCMIDirs.h" -#ifdef VCMI_WINDOWS -#include //for .dll libs -#elif !defined VCMI_ANDROID -#include -#endif - #include "serializer/BinaryDeserializer.h" #include "serializer/BinarySerializer.h" -#ifdef VCMI_ANDROID - -#include "AI/VCAI/VCAI.h" -#include "AI/Nullkiller/AIGateway.h" -#include "AI/BattleAI/BattleAI.h" - -#endif +#ifdef STATIC_AI +# include "AI/VCAI/VCAI.h" +# include "AI/Nullkiller/AIGateway.h" +# include "AI/BattleAI/BattleAI.h" +#else +# ifdef VCMI_WINDOWS +# include //for .dll libs +# else +# include +# endif // VCMI_WINDOWS +#endif // STATIC_AI VCMI_LIB_NAMESPACE_BEGIN template std::shared_ptr createAny(const boost::filesystem::path & libpath, const std::string & methodName) { -#ifdef VCMI_ANDROID +#ifdef STATIC_AI // android currently doesn't support loading libs dynamically, so the access to the known libraries // is possible only via specializations of this template throw std::runtime_error("Could not resolve ai library " + libpath.generic_string()); @@ -96,10 +94,10 @@ std::shared_ptr createAny(const boost::filesystem::path & libpath, const s logGlobal->error("Cannot get AI!"); return ret; -#endif //!VCMI_ANDROID +#endif // STATIC_AI } -#ifdef VCMI_ANDROID +#ifdef STATIC_AI template<> std::shared_ptr createAny(const boost::filesystem::path & libpath, const std::string & methodName) @@ -118,7 +116,7 @@ std::shared_ptr createAny(const boost::filesystem::path & return std::make_shared(); } -#endif +#endif // STATIC_AI template std::shared_ptr createAnyAI(std::string dllname, const std::string & methodName) diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 5b0190bd6..8a9c0adab 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -1,4 +1,9 @@ add_main_lib(${VCMI_LIB_TARGET} SHARED) +target_sources(${VCMI_LIB_TARGET} PRIVATE ${VCMILIB_AI_SOURCES}) +target_link_libraries(${VCMI_LIB_TARGET} PRIVATE ${VCMILIB_AI_LIBRARIES}) +if(ENABLE_STATIC_AI_LIBS) + target_compile_definitions(${VCMI_LIB_TARGET} PRIVATE STATIC_AI) +endif() if(ENABLE_SINGLE_APP_BUILD) target_compile_definitions(${VCMI_LIB_TARGET} PUBLIC VCMI_LIB_NAMESPACE=LIB_CLIENT) endif() diff --git a/server/CVCMIServer.cpp b/server/CVCMIServer.cpp index 9382c69bc..bd5771804 100644 --- a/server/CVCMIServer.cpp +++ b/server/CVCMIServer.cpp @@ -1177,6 +1177,11 @@ extern "C" JNIEXPORT void JNICALL Java_eu_vcmi_vcmi_NativeMethods_createServer(J CVCMIServer::create(); } +extern "C" JNIEXPORT void JNICALL Java_eu_vcmi_vcmi_NativeMethods_initClassloader(JNIEnv * baseEnv, jclass cls) +{ + CAndroidVMHelper::initClassloader(baseEnv); +} + #elif defined(SINGLE_PROCESS_APP) void CVCMIServer::create(boost::condition_variable * cond, const std::vector & args) {