Skip to content

Commit

Permalink
[unity]支持运行时指定脚本后端(为v8还是quickjs),fix #1293
Browse files Browse the repository at this point in the history
  • Loading branch information
chexiongsheng committed Mar 25, 2024
1 parent c1723d4 commit 148dbc4
Show file tree
Hide file tree
Showing 10 changed files with 2,134 additions and 27 deletions.
109 changes: 86 additions & 23 deletions unity/native_src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ if ( NOT DEFINED JS_ENGINE )
set(JS_ENGINE v8)
endif()

set(BACKEND_ROOT ${PROJECT_SOURCE_DIR}/.backends/${JS_ENGINE})
set(BACKEND_ROOT ${PROJECT_SOURCE_DIR}/../native_src/.backends/${JS_ENGINE})

set(ThirdParty ${PROJECT_SOURCE_DIR}/../../unreal/Puerts/ThirdParty)

Expand All @@ -46,24 +46,17 @@ include_directories(
)

set ( PUERTS_INC
Inc/Common.h
Inc/Log.h
Inc/BackendEnv.h
Inc/JSEngine.h
Inc/V8Utils.h
Inc/JSFunction.h
Inc/IPuertsPlugin.h
${PROJECT_SOURCE_DIR}/../../unreal/Puerts/Source/JsEnv/Private/V8InspectorImpl.h
${PROJECT_SOURCE_DIR}/../../unreal/Puerts/Source/JsEnv/Private/PromiseRejectCallback.hpp
)

set ( PUERTS_SRC
Src/Puerts.cpp
Src/Log.cpp
Src/BackendEnv.cpp
Src/JSEngine.cpp
Src/JSFunction.cpp
${PROJECT_SOURCE_DIR}/../../unreal/Puerts/Source/JsEnv/Private/V8InspectorImpl.cpp
)

if(DEFINED PUERTS_EXTRA_SRC)
list(APPEND PUERTS_SRC ${PUERTS_EXTRA_SRC})
endif()
Expand Down Expand Up @@ -92,6 +85,21 @@ endmacro(source_group_by_dir)
source_group_by_dir(${CMAKE_CURRENT_SOURCE_DIR} PUERTS_INC)
source_group_by_dir(${CMAKE_CURRENT_SOURCE_DIR} PUERTS_SRC)

if("${JS_ENGINE}" STREQUAL "mult")
message("mult backend>>>>>>>>>>>>>>>>>>>>>>>>")
set ( PUERTS_SRC
Src/PuertsMultBackend.cpp
)
else ()
set ( PUERTS_SRC
Src/Puerts.cpp
Src/Log.cpp
Src/BackendEnv.cpp
Src/JSEngine.cpp
Src/JSFunction.cpp
${PROJECT_SOURCE_DIR}/../../unreal/Puerts/Source/JsEnv/Private/V8InspectorImpl.cpp
)
endif()

if ( APPLE )
if ( IOS )
Expand Down Expand Up @@ -122,14 +130,54 @@ else ()
)
endif ()

# target_compile_definitions (puerts PRIVATE THREAD_SAFE)
set(PUERTS_COMPILE_DEFINITIONS)

if("${JS_ENGINE}" STREQUAL "mult")
# -------------------begin v8 backend-------------------------
add_library(v8backend STATIC
Src/Log.cpp
Src/BackendEnv.cpp
Src/JSEngine.cpp
Src/JSFunction.cpp
${PROJECT_SOURCE_DIR}/../../unreal/Puerts/Source/JsEnv/Private/V8InspectorImpl.cpp
Src/PluginImpl.cpp
)
target_include_directories (v8backend PRIVATE "${BACKEND_ROOT}/Inc/v8")
target_compile_definitions (v8backend PRIVATE PUERTS_NAMESPACE=puerts_v8)
target_compile_definitions (v8backend PRIVATE MULT_BACKENDS)
target_compile_definitions (v8backend PRIVATE V8_BACKEND)
# -------------------end v8 backend-------------------------

# -------------------begin qjs backend-------------------------
add_library(qjsbackend STATIC
Src/Log.cpp
Src/BackendEnv.cpp
Src/JSEngine.cpp
Src/JSFunction.cpp
${PROJECT_SOURCE_DIR}/../../unreal/Puerts/Source/JsEnv/Private/V8InspectorImpl.cpp
Src/PluginImpl.cpp
)
target_include_directories (qjsbackend PRIVATE "${BACKEND_ROOT}/Inc/quickjs")
target_compile_definitions (qjsbackend PRIVATE PUERTS_NAMESPACE=puerts_qjs)
target_compile_definitions (qjsbackend PRIVATE MULT_BACKENDS)
target_compile_definitions (qjsbackend PRIVATE QJS_BACKEND)
target_compile_definitions (qjsbackend PRIVATE WITH_QUICKJS)
target_compile_definitions (qjsbackend PRIVATE WITHOUT_INSPECTOR)
target_compile_definitions (qjsbackend PRIVATE CUSTOMV8NAMESPACE=v8_qjs)
# -------------------end qjs backend-------------------------

target_include_directories(puerts PRIVATE "${BACKEND_ROOT}/Inc/v8") #只需要用到V8_EXPORT
target_link_libraries(puerts v8backend qjsbackend )
endif ()

# list(APPEND PUERTS_COMPILE_DEFINITIONS THREAD_SAFE)

if ( WIN32 AND NOT CYGWIN )
target_compile_definitions (puerts PRIVATE BUILDING_V8_SHARED)
list(APPEND PUERTS_COMPILE_DEFINITIONS BUILDING_V8_SHARED)
endif ()

if ( NOT CMAKE_BUILD_TYPE MATCHES "Release" )
target_compile_definitions (puerts PRIVATE PUERTS_DEBUG)
list(APPEND PUERTS_COMPILE_DEFINITIONS PUERTS_DEBUG)
endif ()

if ( MSYS OR WIN32 )
Expand All @@ -142,7 +190,7 @@ if ( MSYS OR WIN32 )
endif ()

# definition
target_compile_definitions (puerts PRIVATE PLATFORM_WINDOWS)
list(APPEND PUERTS_COMPILE_DEFINITIONS PLATFORM_WINDOWS)

elseif ( ANDROID )

Expand All @@ -154,7 +202,7 @@ elseif ( ANDROID )
set(BACKEND_LIB_NAMES "-Wl,--whole-archive$<SEMICOLON>${BACKEND_LIB_NAMES}$<SEMICOLON>-Wl,--no-whole-archive")
if ( ANDROID_ABI STREQUAL "armeabi-v7a")
# definition
target_compile_definitions (puerts PRIVATE PLATFORM_ANDROID_ARM)
list(APPEND PUERTS_COMPILE_DEFINITIONS PLATFORM_ANDROID_ARM)

elseif ( ANDROID_ABI STREQUAL "arm64-v8a")
# link
Expand All @@ -163,10 +211,10 @@ elseif ( ANDROID )
)

# definition
target_compile_definitions (puerts PRIVATE PLATFORM_ANDROID_ARM64)
list(APPEND PUERTS_COMPILE_DEFINITIONS PLATFORM_ANDROID_ARM64)
else ()

target_compile_definitions (puerts PRIVATE PLATFORM_ANDROID_x64)
list(APPEND PUERTS_COMPILE_DEFINITIONS PLATFORM_ANDROID_x64)
endif ()

#set_target_properties( puerts PROPERTIES LINK_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/android_version.script)
Expand All @@ -183,27 +231,27 @@ elseif ( APPLE )
if ( IOS )
#definition
if(PLATFORM STREQUAL "SIMULATOR64")
target_compile_definitions (puerts PRIVATE PLATFORM_IOS_SIMULATOR)
list(APPEND PUERTS_COMPILE_DEFINITIONS PLATFORM_IOS_SIMULATOR)
message(STATUS "SIMULATOR64 BUILD...")
else ()
target_compile_definitions (puerts PRIVATE PLATFORM_IOS)
list(APPEND PUERTS_COMPILE_DEFINITIONS PLATFORM_IOS)
endif ()

else ()

#definition
if ( DEFINED FOR_SILICON )
target_compile_definitions (puerts PRIVATE PLATFORM_MAC_ARM64)
list(APPEND PUERTS_COMPILE_DEFINITIONS PLATFORM_MAC_ARM64)
endif()
target_compile_definitions (puerts PRIVATE PLATFORM_MAC)
list(APPEND PUERTS_COMPILE_DEFINITIONS PLATFORM_MAC)
endif ()

elseif (UNIX)
# link
target_link_libraries(puerts pthread)

# definition
target_compile_definitions (puerts PRIVATE PLATFORM_LINUX)
list(APPEND PUERTS_COMPILE_DEFINITIONS PLATFORM_LINUX)
set(CMAKE_CXX_COMPILER "clang++")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -stdlib=libc++ -lc++abi")
Expand All @@ -213,7 +261,22 @@ endif ()
target_link_libraries(puerts
${BACKEND_LIB_NAMES}
)
target_compile_definitions (puerts PRIVATE ${BACKEND_DEFINITIONS})
list(APPEND PUERTS_COMPILE_DEFINITIONS ${BACKEND_DEFINITIONS})

target_compile_definitions (puerts PUBLIC ${PUERTS_COMPILE_DEFINITIONS})
if("${JS_ENGINE}" STREQUAL "mult")
target_compile_definitions (v8backend PUBLIC ${PUERTS_COMPILE_DEFINITIONS})
if ( WIN32 AND NOT CYGWIN AND NOT ( CMAKE_SYSTEM_NAME STREQUAL "WindowsStore" ) AND NOT ANDROID AND NOT MSYS)
set_property(TARGET v8backend PROPERTY
MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
endif ()

target_compile_definitions (qjsbackend PUBLIC ${PUERTS_COMPILE_DEFINITIONS})
if ( WIN32 AND NOT CYGWIN AND NOT ( CMAKE_SYSTEM_NAME STREQUAL "WindowsStore" ) AND NOT ANDROID AND NOT MSYS)
set_property(TARGET qjsbackend PROPERTY
MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
endif ()
endif ()

if ( WIN32 AND NOT CYGWIN AND NOT ( CMAKE_SYSTEM_NAME STREQUAL "WindowsStore" ) AND NOT ANDROID AND NOT MSYS)
set_property(TARGET puerts PROPERTY
Expand Down
4 changes: 4 additions & 0 deletions unity/native_src/Inc/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
#define PUERTS_NAMESPACE puerts
#endif

#if defined(QJS_BACKEND)
namespace v8 = v8_qjs;
#endif

namespace puerts
{

Expand Down
Loading

0 comments on commit 148dbc4

Please sign in to comment.