Explorar el Código

add option to compile AI code into libvcmi directly

- used on Android by default
- AI sources and libs are propagated to upper level with set(... PARENT_SCOPE)
Andrey Filipenkov hace 2 años
padre
commit
4c19d8794d

+ 6 - 4
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()
 

+ 3 - 1
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)

+ 10 - 9
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)

+ 10 - 7
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)

+ 5 - 1
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)

+ 10 - 6
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)

+ 2 - 2
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              #

+ 0 - 2
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

+ 0 - 4
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

+ 15 - 17
lib/CGameInterface.cpp

@@ -13,29 +13,27 @@
 #include "CStack.h"
 #include "VCMIDirs.h"
 
-#ifdef VCMI_WINDOWS
-#include <windows.h> //for .dll libs
-#elif !defined VCMI_ANDROID
-#include <dlfcn.h>
-#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 <windows.h> //for .dll libs
+# else
+#  include <dlfcn.h>
+# endif // VCMI_WINDOWS
+#endif // STATIC_AI
 
 VCMI_LIB_NAMESPACE_BEGIN
 
 template<typename rett>
 std::shared_ptr<rett> 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<rett> 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<CGlobalAI> createAny(const boost::filesystem::path & libpath, const std::string & methodName)
@@ -118,7 +116,7 @@ std::shared_ptr<CBattleGameInterface> createAny(const boost::filesystem::path &
 	return std::make_shared<CBattleAI>();
 }
 
-#endif
+#endif // STATIC_AI
 
 template<typename rett>
 std::shared_ptr<rett> createAnyAI(std::string dllname, const std::string & methodName)

+ 5 - 0
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()

+ 5 - 0
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<std::string> & args)
 {