Skip to content

Commit

Permalink
fix JNI target (#26)
Browse files Browse the repository at this point in the history
* fix JNI build: always enable -fPIC for JNI, because the resulting static library will always be linked to a shared library.
* add different approach for handling the JNI_OnLoad problem. JNI_OnLoad & JNI_OnUnload are now part of the public library interface. This means any user can include them in his sources to be able to use the default implementation.
djinni-generator v1.0.0 will support this approach by automatically generating a file that includes the JNI_OnLoad implementation from the support-lib.
* update djinni generator version to v1.0.0
  • Loading branch information
jothepro authored May 7, 2021
1 parent 6a2f975 commit 34f07aa
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
djinni 0.5.0
djinni 1.0.0
java zulu-11.41.23
13 changes: 5 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ set(SRC_JNI
"djinni/jni/djinni_support.hpp"
"djinni/jni/Marshal.hpp"
"djinni/jni/djinni_support.cpp"
"djinni/jni/djinni_jni_main.hpp"
"djinni/jni/djinni_jni_main.cpp"
)

set(SRC_OBJC
Expand Down Expand Up @@ -62,22 +64,18 @@ set_target_properties(djinni_support_lib PROPERTIES
CXX_EXTENSIONS false
)


install(
TARGETS djinni_support_lib
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
)


install(
FILES ${SRC_SHARED}
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/djinni/
)



# Objective-C support
option(DJINNI_WITH_OBJC "Include the Objective-C support code in Djinni support library." off)
if(DJINNI_WITH_OBJC)
Expand All @@ -104,11 +102,9 @@ target_include_directories(djinni_support_lib PUBLIC "$<BUILD_INTERFACE:${CMAKE_

# JNI support
option(DJINNI_WITH_JNI "Include the JNI support code in Djinni support library." off)
option(DJINNI_JNI_WITH_MAIN "Include the default minimal JNI_OnLoad and JNI_OnUnload implementation in Djinni support library." on)
if(DJINNI_WITH_JNI)
if(DJINNI_JNI_WITH_MAIN)
list(APPEND SRC_JNI "djinni/jni/djinni_main.cpp")
endif()
set_target_properties(djinni_support_lib PROPERTIES
POSITION_INDEPENDENT_CODE ON)
target_include_directories(djinni_support_lib PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/djinni/jni/>")
target_sources(djinni_support_lib PRIVATE ${SRC_JNI})
source_group("jni" FILES ${SRC_JNI})
Expand All @@ -117,6 +113,7 @@ if(DJINNI_WITH_JNI)
FILES
"djinni/jni/djinni_support.hpp"
"djinni/jni/Marshal.hpp"
"djinni/jni/djinni_jni_main.hpp"
DESTINATION
${CMAKE_INSTALL_INCLUDEDIR}/djinni/jni
)
Expand Down
17 changes: 17 additions & 0 deletions djinni/jni/djinni_jni_main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// Copyright 2021 cross-language-cpp
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#include "djinni_jni_main.hpp"
7 changes: 4 additions & 3 deletions djinni/jni/djinni_main.cpp → djinni/jni/djinni_jni_main.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//
// Copyright 2014 Dropbox, Inc.
// Copyright 2021 cross-language-cpp
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -13,9 +14,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//

// This provides a minimal JNI_OnLoad and JNI_OnUnload implementation - include it if your
// app doesn't use JNI except through Djinni.
// This provides a minimal JNI_OnLoad and JNI_OnUnload implementation.
// Don't include it if your library does require a custom JNI_OnLoad implementation with custom initialization logic.
#pragma once

#include "djinni_support.hpp"

Expand Down
4 changes: 2 additions & 2 deletions djinni/jni/djinni_support.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
#include <string>
#include <vector>

#include "../proxy_cache_interface.hpp"
#include "../djinni_common.hpp"
#include "djinni/proxy_cache_interface.hpp"
#include "djinni/djinni_common.hpp"
#include <jni.h>

/*
Expand Down
2 changes: 1 addition & 1 deletion mkdocs-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# dependencies for mkdocs documentation
mkdocs>=1.1
mkdocs-material>=7.1.3
mkdocs-material>=7.1.3
5 changes: 2 additions & 3 deletions test-suite/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ add_djinni_target(DjinniWCharTests
JAVA_NONNULL_ANNOTATION "javax.annotation.Nonnull"
IDENT_JAVA_FIELD "mFooBar"
JNI_OUT "${CMAKE_CURRENT_BINARY_DIR}/generated-src/jni"
JNI_GENERATE_MAIN false
IDENT_JNI_CLASS "NativeFooBar"
IDENT_JNI_FILE "NativeFooBar"
OBJC_OUT "${CMAKE_CURRENT_BINARY_DIR}/generated-src/objc"
Expand Down Expand Up @@ -133,10 +134,7 @@ source_group("generated-objcpp" FILES ${WCHAR_OBJCPP_GENERATED_SRCS})
source_group("generated-yaml" FILES ${WCHAR_YAML_GENERATED_SRCS})

if(DJINNI_WITH_JNI)
file(GLOB_RECURSE SUPPORT_LIB_JNI_SRCS "${CMAKE_SOURCE_DIR}/djinni/jni/*")

add_library(DjinniTestNative SHARED
${SUPPORT_LIB_JNI_SRCS}
${JNI_GENERATED_SRCS}
${CPP_GENERATED_SRCS}
${WCHAR_JNI_GENERATED_SRCS}
Expand All @@ -150,6 +148,7 @@ if(DJINNI_WITH_JNI)
${CMAKE_CURRENT_BINARY_DIR}/generated-src/cpp
${JNI_INCLUDE_DIRS}
)
target_link_libraries(DjinniTestNative PUBLIC djinni-support-lib::djinni-support-lib)

find_package(Java REQUIRED)
include(UseJava)
Expand Down
2 changes: 2 additions & 0 deletions test-suite/Djinni.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ function(add_djinni_target)
JNI_INCLUDE_CPP_PREFIX
JNI_NAMESPACE
JNI_BASE_LIB_INCLUDE_PREFIX
JNI_GENERATE_MAIN
IDENT_JNI_CLASS
IDENT_JNI_FILE

Expand Down Expand Up @@ -233,6 +234,7 @@ function(add_djinni_target)
append_if_defined(DJINNI_GENERATION_COMMAND "--jni-include-cpp-prefix" ${DJINNI_JNI_INCLUDE_CPP_PREFIX})
append_if_defined(DJINNI_GENERATION_COMMAND "--jni-namespace" ${DJINNI_JNI_NAMESPACE})
append_if_defined(DJINNI_GENERATION_COMMAND "--jni-base-lib-include-prefix" ${DJINNI_JNI_BASE_LIB_INCLUDE_PREFIX})
append_if_defined(DJINNI_GENERATION_COMMAND "--jni-generate-main" ${DJINNI_JNI_GENERATE_MAIN})
append_if_defined(DJINNI_GENERATION_COMMAND "--ident-jni-class" ${DJINNI_IDENT_JNI_CLASS})
append_if_defined(DJINNI_GENERATION_COMMAND "--ident-jni-file" ${DJINNI_IDENT_JNI_FILE})

Expand Down
1 change: 1 addition & 0 deletions test/jni_list.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
./usr/local/include/djinni/djinni_common.hpp
./usr/local/include/djinni/jni
./usr/local/include/djinni/jni/Marshal.hpp
./usr/local/include/djinni/jni/djinni_jni_main.hpp
./usr/local/include/djinni/jni/djinni_support.hpp
./usr/local/include/djinni/proxy_cache_impl.hpp
./usr/local/include/djinni/proxy_cache_interface.hpp
Expand Down

0 comments on commit 34f07aa

Please sign in to comment.