Skip to content

Commit

Permalink
Merge pull request #966 from OpenShot/java-bindings
Browse files Browse the repository at this point in the history
Java Swig Bindings for libopenshot
  • Loading branch information
jonoomph authored Jun 8, 2024
2 parents d73763f + ed145d2 commit 9424cc0
Show file tree
Hide file tree
Showing 6 changed files with 295 additions and 101 deletions.
96 changes: 8 additions & 88 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ jobs:
- uses: haya14busa/action-cond@v1
id: coverage
with:
cond: ${{ (matrix.compiler.cc == 'gcc' && runner.os == 'linux') || (matrix.compiler.cc == 'clang' && runner.os == 'macos') }}
cond: ${{ matrix.compiler.cc == 'gcc' && runner.os == 'linux' }}
if_true: "-DENABLE_COVERAGE:BOOL=1"

- uses: haya14busa/action-cond@v1
Expand Down Expand Up @@ -96,80 +96,18 @@ jobs:
wget https://launchpad.net/ubuntu/+archive/primary/+files/catch2_2.13.8-1_amd64.deb
sudo dpkg -i catch2_2.13.8-1_amd64.deb

- name: Install macOS dependencies
if: ${{ runner.os == 'macos' }}
run: |
brew install \
qt5 ffmpeg zeromq cppzmq libomp opencv protobuf babl \
python3 swig catch2 doxygen graphviz lcov
- name: Set up MSYS and install Windows dependencies
if: ${{ runner.os == 'Windows' }}
uses: msys2/setup-msys2@v2
with:
release: true
update: true
install: >-
mingw-w64-x86_64-gcc
mingw-w64-x86_64-lld
mingw-w64-x86_64-make
mingw-w64-x86_64-cmake
mingw-w64-x86_64-pkgconf
mingw-w64-x86_64-qt5-base
mingw-w64-x86_64-qt5-svg
mingw-w64-x86_64-qt5-winextras
mingw-w64-x86_64-qt5-tools
mingw-w64-x86_64-libvpx
mingw-w64-x86_64-ffmpeg
mingw-w64-x86_64-zeromq
mingw-w64-x86_64-cppzmq
mingw-w64-x86_64-opencv
mingw-w64-x86_64-protobuf
mingw-w64-x86_64-babl
mingw-w64-x86_64-catch
mingw-w64-x86_64-python3
mingw-w64-x86_64-swig
- uses: actions/[email protected]
name: Import OpenShotAudio cache
id: cache-audio
with:
path: audio/build
key: audio-${{ matrix.sys.os }}-${{ matrix.compiler.cxx }}-${{ hashFiles('audio/CMakeLists.txt') }}

- uses: actions/[email protected]
name: Import Resvg cache
if: ${{ steps.use-resvg.outputs.value }}
id: cache-resvg
with:
path: resvg/target
key: resvg-${{ matrix.sys.os }}-${{ matrix.compiler.cxx }}-${{ hashFiles('resvg/Cargo.toml') }}

- name: Build OpenShotAudio (if not cached)
if: steps.cache-audio.outputs.cache-hit != 'true'
- name: Build OpenShotAudio
run: |
pushd audio
if [ ! -d build ]; then
mkdir build
if [ "_${{ runner.os }}" == "_macOS" ]; then
export CMAKE_EXTRA="${CMAKE_EXTRA} -DCMAKE_OSX_DEPLOYMENT_TARGET=10.9"
fi
if [ "_${{ runner.os }}_${{ matrix.compiler.cc }}" == "_macOS_clang" ]; then
export CMAKE_EXTRA="${CMAKE_EXTRA} \
-DCMAKE_EXE_LINKER_FLAGS=-stdlib=libc++ \
-DCMAKE_MODULE_LINKER_FLAGS=-stdlib=libc++ \
-DCMAKE_SHARED_LINKER_FLAGS=-stdlib=libc++";
fi
cmake -B build -S . -G "${{ steps.generator.outputs.value }}" \
-DCMAKE_BUILD_TYPE="Debug" \
-DCMAKE_INSTALL_PREFIX="${{ github.workspace }}/install" \
${CMAKE_EXTRA}
fi
mkdir -p build
cmake -B build -S . -G "${{ steps.generator.outputs.value }}" \
-DCMAKE_BUILD_TYPE="Debug" \
-DCMAKE_INSTALL_PREFIX="${{ github.workspace }}/install"
cmake --build build
popd

- name: Build Resvg (if enabled and not cached)
if: steps.cache-resvg && (steps.cache-resvg.outputs.cache-hit != 'true')
- name: Build Resvg (if enabled)
if: steps.use-resvg
run: |
if [ -d "resvg/c-api" ]; then
pushd resvg/c-api
Expand All @@ -179,24 +117,6 @@ jobs:

- name: Build libopenshot
run: |
if [ "_${{ runner.os }}" == "_macOS" ]; then
export CMAKE_EXTRA="${CMAKE_EXTRA} -DCMAKE_OSX_DEPLOYMENT_TARGET=10.9 \
-DQt5_DIR=/usr/local/opt/qt@5/lib/cmake/Qt5 \
-DENABLE_RUBY=0";
export PATH="/usr/local/opt/qt@5/bin:$PATH";
fi
if [ "_${{ runner.os }}_${{ matrix.compiler.cc }}" == "_macOS_clang" ]; then
export CMAKE_EXTRA="${CMAKE_EXTRA} \
-DCMAKE_EXE_LINKER_FLAGS=-stdlib=libc++ \
-DCMAKE_MODULE_LINKER_FLAGS=-stdlib=libc++ \
-DCMAKE_SHARED_LINKER_FLAGS=-stdlib=libc++";
fi
if [ "_${{ runner.os }}" == "_Windows" ]; then
export CMAKE_EXTRA="${CMAKE_EXTRA} \
-DCMAKE_EXE_LINKER_FLAGS=-fuse-ld=lld \
-DCMAKE_MODULE_LINKER_FLAGS=-fuse-ld=lld \
-DCMAKE_SHARED_LINKER_FLAGS=-fuse-ld=lld"
fi
mkdir build
cmake -B build -S . -G "${{ steps.generator.outputs.value }}" \
-DCMAKE_BUILD_TYPE="Debug" \
Expand Down
9 changes: 8 additions & 1 deletion bindings/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#
# @section LICENSE
#
# Copyright (c) 2008-2019 OpenShot Studios, LLC
# Copyright (c) 2008-2024 OpenShot Studios, LLC
#
# SPDX-License-Identifier: LGPL-3.0-or-later

Expand All @@ -16,6 +16,10 @@ IF(NOT DEFINED ENABLE_RUBY)
SET(ENABLE_RUBY 1)
ENDIF()

IF(NOT DEFINED ENABLE_JAVA)
SET(ENABLE_JAVA 0)
ENDIF()

############### INCLUDE EACH LANGUAGE BINDING ################
IF (ENABLE_PYTHON)
add_subdirectory(python)
Expand All @@ -25,3 +29,6 @@ IF (ENABLE_RUBY)
add_subdirectory(ruby)
ENDIF (ENABLE_RUBY)

IF (ENABLE_JAVA)
add_subdirectory(java)
ENDIF (ENABLE_JAVA)
86 changes: 86 additions & 0 deletions bindings/java/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
####################### CMakeLists.txt (libopenshot) #########################
# @brief CMake build file for libopenshot (used to generate Java SWIG bindings)
# @author Jonathan Thomas <[email protected]>
#
# @section LICENSE
#
# Copyright (c) 2008-2024 OpenShot Studios, LLC
#
# SPDX-License-Identifier: LGPL-3.0-or-later

############### JAVA BINDINGS ################
find_package(SWIG 4.0 REQUIRED)
include(${SWIG_USE_FILE})

### Enable some legacy SWIG behaviors in newer CMake versions
if (POLICY CMP0078)
cmake_policy(SET CMP0078 OLD)
endif()
if (POLICY CMP0086)
cmake_policy(SET CMP0086 OLD)
endif()

find_package(Java REQUIRED)
include(UseJava)

find_package(JNI REQUIRED)
include_directories(${JNI_INCLUDE_DIRS})
message(STATUS "JNI_INCLUDE_DIRS=${JNI_INCLUDE_DIRS}")
message(STATUS "JNI_LIBRARIES=${JNI_LIBRARIES}")

### Enable C++ in SWIG
set_property(SOURCE openshot.i PROPERTY CPLUSPLUS ON)
set_property(SOURCE openshot.i PROPERTY SWIG_MODULE_NAME openshot)

### Suppress a ton of warnings in the generated SWIG C++ code
set(SWIG_CXX_FLAGS "-Wno-unused-variable -Wno-unused-function \
-Wno-deprecated-copy -Wno-class-memaccess -Wno-cast-function-type \
-Wno-unused-parameter -Wno-catch-value -Wno-sign-compare -Wno-ignored-qualifiers")
separate_arguments(sw_flags UNIX_COMMAND ${SWIG_CXX_FLAGS})
set_property(SOURCE openshot.i PROPERTY GENERATED_COMPILE_OPTIONS ${sw_flags})

### Take include dirs from target
if(CMAKE_VERSION VERSION_GREATER 3.15)
set(_inc $<REMOVE_DUPLICATES:$<TARGET_PROPERTY:openshot,INCLUDE_DIRECTORIES>>)
elseif(CMAKE_VERSION VERSION_GREATER 3.12)
set(_inc $<TARGET_PROPERTY:openshot,INCLUDE_DIRECTORIES>)
endif()
if (DEFINED _inc)
message(STATUS "Include directories: ${_inc}")
set_property(SOURCE openshot.i PROPERTY INCLUDE_DIRECTORIES ${_inc})
endif()

### Properly manage dependencies (regenerate bindings after changes)
if (CMAKE_VERSION VERSION_GREATER 3.20)
set_property(SOURCE openshot.i PROPERTY USE_SWIG_DEPENDENCIES TRUE)
endif()

### Add the SWIG interface file (which defines all the SWIG methods)
if (CMAKE_VERSION VERSION_LESS 3.8.0)
swig_add_module(openshot-java java openshot.i)
else()
swig_add_library(openshot-java LANGUAGE java SOURCES openshot.i)
endif()

### Set the prefix for the library to 'lib'
#set_target_properties(${SWIG_MODULE_openshot-java_REAL_NAME} PROPERTIES PREFIX "lib")

### Compile the generated wrapper file
get_property(_java_files TARGET openshot-java PROPERTY SWIG_SUPPORT_FILES)
message("_java_files: ${_java_files}")

### Link the new Java wrapper library with libopenshot
target_link_libraries(${SWIG_MODULE_openshot-java_REAL_NAME} PUBLIC
${JNI_LIBRARIES} openshot)

### Create a custom target for the JAR file
add_custom_command(
OUTPUT ${CMAKE_BINARY_DIR}/bindings/java/openshotJNI.jar
COMMAND ${Java_JAVAC_EXECUTABLE} -d ${CMAKE_BINARY_DIR}/bindings/java ${CMAKE_BINARY_DIR}/bindings/java/*.java
COMMAND ${Java_JAR_EXECUTABLE} -cf ${CMAKE_BINARY_DIR}/bindings/java/openshotJNI.jar -C ${CMAKE_BINARY_DIR}/bindings/java .
DEPENDS openshot-java
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/bindings/java
COMMENT "Creating openshotJNI.jar"
)

add_custom_target(openshotJNIJar ALL DEPENDS ${CMAKE_BINARY_DIR}/bindings/java/openshotJNI.jar)
Loading

0 comments on commit 9424cc0

Please sign in to comment.