Skip to content

Commit

Permalink
use cmake; use MSVC compiler on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
GiviMAD committed Jan 12, 2025
1 parent 5e92812 commit 4fd9a02
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 94 deletions.
106 changes: 60 additions & 46 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ jobs:
build-lib-debian-x86_64:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/download-artifact@v3
- uses: actions/checkout@v4
- uses: actions/download-artifact@v4
- name: Docker Setup Buildx
uses: docker/setup-buildx-action@v1.6.0
uses: docker/setup-buildx-action@v3
with:
install: true
- name: Setup project dir
Expand All @@ -21,20 +21,20 @@ jobs:
docker build --build-arg="RUN_TESTS=true" -f dockerfile . -t libfvadjni_binary:x86_64 --load
docker run --platform=amd64 -v $(pwd)/dist:/out libfvadjni_binary:x86_64 bash -c "cp src/main/resources/debian-amd64/*.so /out/"
- name: Artifact debian x86_64
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: debian-binary-amd64
path: dist/*.so

build-lib-debian-arm64:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/download-artifact@v3
- uses: actions/checkout@v4
- uses: actions/download-artifact@v4
- name: Set up QEMU
uses: docker/setup-qemu-action@v1
uses: docker/setup-qemu-action@v3
- name: Docker Setup Buildx
uses: docker/setup-buildx-action@v1.6.0
uses: docker/setup-buildx-action@v3
with:
install: true
- name: Setup project dir
Expand All @@ -45,20 +45,20 @@ jobs:
docker build -f dockerfile . -t libfvadjni_binary:arm64 --platform arm64 --load
DOCKER_BUILDKIT=1 docker run --platform=arm64 -v $(pwd)/dist:/out libfvadjni_binary:arm64 bash -c "cp src/main/resources/debian-arm64/*.so /out/"
- name: Artifact debian arm64
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: debian-binary-arm64
path: dist/*.so

build-lib-debian-arm:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/download-artifact@v3
- uses: actions/checkout@v4
- uses: actions/download-artifact@v4
- name: Set up QEMU
uses: docker/setup-qemu-action@v1
uses: docker/setup-qemu-action@v3
- name: Docker Setup Buildx
uses: docker/setup-buildx-action@v1.6.0
uses: docker/setup-buildx-action@v3
with:
install: true
- name: Setup project dir
Expand All @@ -69,17 +69,17 @@ jobs:
docker build -f dockerfile . -t libfvadjni_binary:armv7l --platform arm --load
DOCKER_BUILDKIT=1 docker run --platform=arm -v $(pwd)/dist:/out libfvadjni_binary:armv7l bash -c "cp src/main/resources/debian-armv7l/*.so /out/"
- name: Artifact debian armv7l
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: debian-binary-armv7l
path: dist/*.so

build-lib-windows:
runs-on: windows-latest
steps:
- uses: actions/checkout@v3
- uses: actions/download-artifact@v3
- uses: actions/setup-java@v3
- uses: actions/checkout@v4
- uses: actions/download-artifact@v4
- uses: actions/setup-java@v4
with:
distribution: 'zulu'
java-version: '17'
Expand All @@ -92,77 +92,91 @@ jobs:
shell: powershell
run: |
.\build_win.ps1
mv src\main\resources\win-amd64\libfvadjni.dll .\dist\
mv src\main\resources\win-amd64\libfvad-jni.dll .\dist\
- name: Artifact windows x86_64
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: windows-binary-x86_64
path: dist/libfvadjni.dll
path: dist/*.dll

build-lib-macos:
runs-on: macos-latest
build-lib-macos-amd64:
runs-on: macos-13
steps:
- uses: actions/checkout@v3
- uses: actions/download-artifact@v3
- uses: actions/setup-java@v3
- uses: actions/checkout@v4
- uses: actions/download-artifact@v4
- uses: actions/setup-java@v4
with:
distribution: 'zulu'
java-version: '17'
# - name: Set up Maven
# uses: stCarolas/[email protected]
# with:
# maven-version: 3.9.2
- name: Setup project dir
run: |
export DYLD_LIBRARY_PATH=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/
mkdir -p dist
mkdir -p dist_arm64
git submodule update --init
- name: Build binary
run: |
export DYLD_LIBRARY_PATH=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/
./build_macos.sh amd64
./build_macos.sh arm64
# mvn test
mv src/main/resources/macos-amd64/libfvadjni.dylib dist/
mv src/main/resources/macos-arm64/libfvadjni.dylib dist_arm64/
mv src/main/resources/macos-amd64/*.dylib dist/
- name: Artifact macos amd64
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: macos-binary-amd64
path: dist/libfvadjni.dylib
path: dist/*.dylib


build-lib-macos-arm64:
runs-on: macos-latest
steps:
- uses: actions/checkout@v4
- uses: actions/download-artifact@v4
- uses: actions/setup-java@v4
with:
distribution: 'zulu'
java-version: '17'
- name: Setup project dir
run: |
export DYLD_LIBRARY_PATH=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/
mkdir -p dist_arm64
git submodule update --init
- name: Build binary
run: |
export DYLD_LIBRARY_PATH=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/
./build_macos.sh arm64
mv src/main/resources/macos-arm64/*.dylib dist_arm64/
- name: Artifact macos arm64
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: macos-binary-arm64
path: dist_arm64/libfvadjni.dylib
path: dist_arm64/*.dylib

deploy:
if: startsWith(github.ref, 'refs/tags/')
needs: [build-lib-debian-x86_64,build-lib-debian-arm,build-lib-debian-arm64,build-lib-windows,build-lib-macos]
needs: [build-lib-debian-x86_64,build-lib-debian-arm,build-lib-debian-arm64,build-lib-windows,build-lib-macos-amd64,build-lib-macos-arm64]
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/checkout@v2
- uses: actions/download-artifact@v3
- uses: actions/download-artifact@v4
- name: check tree
run: ls -R
- name: setup resources
run: |
mv ./debian-binary-amd64/*.so ./src/main/resources/debian-amd64/
mv ./debian-binary-arm64/*.so ./src/main/resources/debian-arm64/
mv ./debian-binary-armv7l/*.so ./src/main/resources/debian-armv7l/
mv ./windows-binary-x86_64/libfvadjni.dll ./src/main/resources/win-amd64/libfvadjni.dll
mv ./macos-binary-amd64/libfvadjni.dylib ./src/main/resources/macos-amd64/libfvadjni.dylib
mv ./macos-binary-arm64/libfvadjni.dylib ./src/main/resources/macos-arm64/libfvadjni.dylib
mv ./windows-binary-x86_64/*.dll ./src/main/resources/win-amd64/
mv ./macos-binary-amd64/*.dylib ./src/main/resources/macos-amd64/
mv ./macos-binary-arm64/*.dylib ./src/main/resources/macos-arm64/
- name: Set up Maven Central Repository
uses: actions/setup-java@v1
uses: actions/setup-java@v4
with:
java-version: 17
distribution: 'zulu'
server-id: sonatype-nexus-staging
server-username: MAVEN_USERNAME
server-password: MAVEN_PASSWORD
server-password: MAVEN_CENTRAL_TOKEN
gpg-private-key: ${{ secrets.MAVEN_GPG_PRIVATE_KEY }}
gpg-passphrase: MAVEN_GPG_PASSPHRASE
- name: Deploy with Maven
Expand All @@ -175,7 +189,7 @@ jobs:
echo "LIBFVAD_VERSION=$LIBFVAD_VERSION" >> $GITHUB_OUTPUT
env:
MAVEN_USERNAME: ${{ secrets.OSSRH_USERNAME }}
MAVEN_PASSWORD: ${{ secrets.OSSRH_TOKEN }}
MAVEN_CENTRAL_TOKEN: ${{ secrets.OSSRH_TOKEN }}
MAVEN_GPG_PASSPHRASE: ${{ secrets.MAVEN_GPG_PASSPHRASE }}
- name: Release
uses: softprops/action-gh-release@v1
Expand Down
101 changes: 101 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
cmake_minimum_required(VERSION 3.5)

project(libfvad-jni C CXX)

# ---- Set options ----

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(JAVA_AWT_LIBRARY NotNeeded)
set(JAVA_JVM_LIBRARY NotNeeded)
find_package(JNI REQUIRED)
if (JNI_FOUND)
message (STATUS "JNI_INCLUDE_DIRS=${JNI_INCLUDE_DIRS}")
endif()

if(MSVC)
# Force compiler to use UTF-8 for IPA constants
add_compile_options("$<$<C_COMPILER_ID:MSVC>:/utf-8>")
add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/utf-8>")
elseif(NOT APPLE)
string(APPEND CMAKE_CXX_FLAGS " -Wall -Wextra -Wl,-rpath,'$ORIGIN'")
string(APPEND CMAKE_C_FLAGS " -Wall -Wextra")
set(CMAKE_INSTALL_RPATH '$ORIGIN')
elseif(APPLE)
set(CMAKE_INSTALL_RPATH "@loader_path")
endif()

# ---- Declare libraries ----

add_library(libfvad-jni SHARED src/main/native/io_github_givimad_libfvadjni_VoiceActivityDetector.cpp)

if(NOT DEFINED LIBFVAD_DIR)
set(LIBFVAD_DIR "${CMAKE_CURRENT_BINARY_DIR}/libfvad.s")
set(LIBFVAD_INSTALL_DIR "${CMAKE_CURRENT_BINARY_DIR}/libfvad")
include(ExternalProject)
ExternalProject_Add(
libfvad-external
PREFIX "${LIBFVAD_INSTALL_DIR}"
URL "${CMAKE_CURRENT_LIST_DIR}/src/main/native/libfvad"
CMAKE_ARGS -DCMAKE_INSTALL_RPATH:STRING=${CMAKE_INSTALL_RPATH} -DCMAKE_INSTALL_PREFIX:PATH=${LIBFVAD_DIR} -DCMAKE_OSX_ARCHITECTURES:STRING=${CMAKE_OSX_ARCHITECTURES} -DCMAKE_OSX_DEPLOYMENT_TARGET:STRING=${CMAKE_OSX_DEPLOYMENT_TARGET}
)
add_dependencies(libfvad-jni libfvad-external)
endif()

# ---- Declare executable ----

if((NOT MSVC) AND (NOT APPLE))
# Linux flags
string(APPEND CMAKE_CXX_FLAGS " -Wall -Wextra -Wl,-rpath,'$ORIGIN'")
string(APPEND CMAKE_C_FLAGS " -Wall -Wextra")
# target_link_libraries(libfvad-jni -static-libgcc -static-libstdc++)
endif()

if(MSVC)
target_link_libraries(libfvad-jni
fvad
)
else()
target_link_libraries(libfvad-jni
libfvad.a
)
endif()
target_link_directories(libfvad-jni PUBLIC
${LIBFVAD_DIR}/lib
${LIBFVAD_DIR}/lib/static
)

target_include_directories(libfvad-jni PUBLIC
${JNI_INCLUDE_DIRS}
src/main/native
${LIBFVAD_DIR}/include
)

# ---- Declare install targets ----

install(
TARGETS libfvad-jni
DESTINATION ${CMAKE_INSTALL_PREFIX}
)

# Dependencies

install(
DIRECTORY ${LIBFVAD_DIR}/lib/
DESTINATION ${CMAKE_INSTALL_PREFIX}
USE_SOURCE_PERMISSIONS
FILES_MATCHING
PATTERN "*.so.*"
PATTERN "*.so"
PATTERN "*.dylib"
)

install(
DIRECTORY ${LIBFVAD_DIR}/bin/
DESTINATION ${CMAKE_INSTALL_PREFIX}
USE_SOURCE_PERMISSIONS
FILES_MATCHING
PATTERN "*.dll"
)

14 changes: 5 additions & 9 deletions build_debian.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
#!/bin/bash
set -xe
build_lib() {
(cd src/main/native/libfvad && cmake . && cmake --build .)
#exit 0
g++ -c -std=c++11 -O3 -DNDEBUG -fPIC -pthread -I${JAVA_HOME}/include -I${JAVA_HOME}/include/linux -I src/main/native/libfvad/include $CXXFLAGS \
src/main/native/io_github_givimad_libfvadjni_VoiceActivityDetector.cpp -o src/main/native/io_github_givimad_libfvadjni_VoiceActivityDetector.o

g++ -shared -fPIC -pthread $CXXFLAGS -I src/main/native -I src/main/native/libfvad/include -o src/main/resources/debian-$AARCH/libfvadjni.so src/main/native/libfvad/src/CMakeFiles/fvad.dir/*.o src/main/native/libfvad/src/CMakeFiles/fvad.dir/**/*.o src/main/native/libfvad/src/libfvad.a src/main/native/io_github_givimad_libfvadjni_VoiceActivityDetector.o -lc $LDFLAGS

rm -rf src/main/native/*.o
(cd src/main/native/libfvad && git clean -d -f -x)
TARGET_DIR=src/main/resources/debian-$AARCH
cmake -B build $CMAKE_ARGS -DCMAKE_C_FLAGS="$CMAKE_CFLAGS" -DCMAKE_INSTALL_PREFIX=$TARGET_DIR
cmake --build build --config Release
cmake --install build
rm -rf build
}
AARCH=$(dpkg --print-architecture)
case $AARCH in
Expand Down
14 changes: 5 additions & 9 deletions build_macos.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,9 @@ esac
INCLUDE_JAVA="-I $JAVA_HOME/include -I $JAVA_HOME/include/darwin"
TARGET=$AARCH-apple-macosx$TARGET_VERSION

(cd src/main/native/libfvad && cmake -DCMAKE_OSX_DEPLOYMENT_TARGET=$TARGET_VERSION -DCMAKE_OSX_ARCHITECTURES=$AARCH . && cmake --build .)
TARGET_DIR=src/main/resources/macos-$AARCH_NAME

g++ -c -std=c++11 -arch "$AARCH" -O3 -DNDEBUG -fPIC $INCLUDE_JAVA -I src/main/native/libfvad/include $CXXFLAGS \
$CXXFLAGS --target="$TARGET" \
src/main/native/io_github_givimad_libfvadjni_VoiceActivityDetector.cpp -o src/main/native/io_github_givimad_libfvadjni_VoiceActivityDetector.o

g++ -arch "$AARCH" --target="$TARGET" -dynamiclib -I src/main/native -o src/main/resources/macos-$AARCH_NAME/libfvadjni.dylib src/main/native/libfvad/src/libfvad.a src/main/native/io_github_givimad_libfvadjni_VoiceActivityDetector.o -lc $LDFLAGS

rm -rf src/main/native/*.o
(cd src/main/native/libfvad && git clean -d -f -x)
cmake -Bbuild -DCMAKE_INSTALL_PREFIX=$TARGET_DIR -DCMAKE_OSX_DEPLOYMENT_TARGET=$TARGET_VERSION -DCMAKE_OSX_ARCHITECTURES=$AARCH
cmake --build build --config Release
cmake --install build
rm -rf build
24 changes: 4 additions & 20 deletions build_win.ps1
Original file line number Diff line number Diff line change
@@ -1,21 +1,5 @@
Set-PSDebug -Trace 1

cd src\main\native\libfvad
cmake . -G "MinGW Makefiles"
cmake --build .
cd ..\..\..\..\

g++ -c -std=c++11 -O3 -DNDEBUG -fPIC -I $env:JAVA_HOME\include -I $env:JAVA_HOME\include\win32 -I src\main\native\libfvad\include -I src\main\native src\main\native\io_github_givimad_libfvadjni_VoiceActivityDetector.cpp -o src\main\native\io_github_givimad_libfvadjni_VoiceActivityDetector.o

g++ -shared -static -I src\main\native\libfvad\include -I src\main\native -o src\main\resources\win-amd64\libfvadjni.dll src\main\native\libfvad\src\CMakeFiles\fvad.dir\*.c.obj src\main\native\libfvad\src\CMakeFiles\fvad.dir\vad\*.c.obj src\main\native\libfvad\src\CMakeFiles\fvad.dir\signal_processing\*.c.obj src\main\native\libfvad\src\libfvad.a src\main\native\io_github_givimad_libfvadjni_VoiceActivityDetector.o

if ($LastExitCode -ne 0) {
Write-Error "Unable to build library"
Exit 1
}

rm -fo src\main\native\*.o
cd src\main\native\libfvad
git clean -d -f -x
cd ..\..\..\..\
Set-PSDebug -Trace 0
cmake -B build -DCMAKE_INSTALL_PREFIX=src/main/resources/win-amd64
cmake --build build --config Release
cmake --install build
rm -r -fo build
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<artifactId>libfvad-jni</artifactId>
<name>libfvad-jni</name>
<url>https://github.com/GiviMAD/libfvad-jni</url>
<version>1.0.0-0</version>
<version>1.0.0-1</version>
<description>A JNI wrapper for using libfvad</description>
<licenses>
<license>
Expand Down
Loading

0 comments on commit 4fd9a02

Please sign in to comment.