Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: JNI Bind Build/Test Status

on:
push:
branches: [ "main" ]
branches: '*'
pull_request:
branches: [ "main" ]
branches: '*'

permissions:
contents: read
Expand Down Expand Up @@ -46,3 +46,13 @@ jobs:
- uses: actions/checkout@v4
- name: test
run: bazel test --cxxopt='-Werror' --cxxopt='-std=c++20' --repo_env=CC=clang --test_output=errors ...

windows-2019-cpp20-cmake-mvsc:
runs-on: windows-2019
steps:
- name: checkout
uses: actions/checkout@v4
- name: cmake configure
run: cmake --preset msvc-16-config-debug
- name: cmake build
run: cmake --build --preset msvc-16-build-debug
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
bazel*
.vscode
MODULE.bazel.lock
cmake-build
cmake_build
cmake_install
45 changes: 45 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
cmake_minimum_required(VERSION 3.20.0)

file(READ ${CMAKE_SOURCE_DIR}/JNI_BIND_VERSION.inc JNI_BIND_VERSION)
project(jni-bind VERSION "${JNI_BIND_VERSION}" LANGUAGES C CXX)

include(${CMAKE_SOURCE_DIR}/cmake/add_jni_test.cmake)
include(${CMAKE_SOURCE_DIR}/cmake/compile_flags.cmake)
include(${CMAKE_SOURCE_DIR}/cmake/find_dependencies.cmake)

# main target
add_library(jni_bind INTERFACE)

target_include_directories(jni_bind INTERFACE
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/class_defs
${CMAKE_CURRENT_SOURCE_DIR}/implementation
${CMAKE_CURRENT_SOURCE_DIR}/implementation/jni_helper
${CMAKE_CURRENT_SOURCE_DIR}/metaprogramming)

target_link_libraries(jni_bind INTERFACE jdk:jni)

# Testsuite
if (NOT TARGET gtest_main)
return()
endif()

enable_testing()

file(GLOB METAPROGRAMMING_TESTS "${CMAKE_SOURCE_DIR}/metaprogramming/*_test.cc")
foreach(TEST_SRC ${METAPROGRAMMING_TESTS})
get_filename_component(TEST_NAME ${TEST_SRC} NAME_WE)
add_jni_test(${TEST_NAME} ${TEST_SRC})
endforeach()

file(GLOB IMPLEMENTATION_TESTS "${CMAKE_SOURCE_DIR}/implementation/*_test.cc")
foreach(TEST_SRC ${IMPLEMENTATION_TESTS})
get_filename_component(TEST_NAME ${TEST_SRC} NAME_WE)
add_jni_test(${TEST_NAME} ${TEST_SRC})
endforeach()

file(GLOB JNI_HELPER_TESTS "${CMAKE_SOURCE_DIR}/implementation/jni_helper/*_test.cc")
foreach(TEST_SRC ${JNI_HELPER_TESTS})
get_filename_component(TEST_NAME ${TEST_SRC} NAME_WE)
add_jni_test(${TEST_NAME} ${TEST_SRC})
endforeach()
70 changes: 70 additions & 0 deletions CMakePresets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
"version": 6,

"cmakeMinimumRequired": {
"major": 3,
"minor": 20,
"patch": 0
},

"configurePresets": [
{
"hidden": true,
"name": "msvc-16-base",
"generator": "Visual Studio 16 2019",
"binaryDir": "${sourceDir}/cmake_build",
"architecture": {
"value": "x64",
"strategy": "external"
}
},
{
"name": "msvc-16-config-debug",
"inherits": "msvc-16-base",
"installDir": "${sourceDir}/cmake_install/Debug",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug"
}
},
{
"name": "msvc-16-config-release",
"inherits": "msvc-16-base",
"installDir": "${sourceDir}/cmake_install/Release",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release"
}
}
],

"buildPresets": [
{
"name": "msvc-16-build-debug",
"configurePreset": "msvc-16-config-debug",
"configuration": "Debug",
"jobs": 4
},
{
"name": "msvc-16-build-release",
"configurePreset": "msvc-16-config-release",
"configuration": "Release",
"jobs": 4
}
],

"testPresets": [
{
"name": "msvc-16-test-debug",
"configurePreset": "msvc-16-config-debug",
"configuration": "Debug",
"output": {
"outputOnFailure": true,
"verbosity": "verbose"
},
"execution": {
"noTestsAction": "error",
"stopOnFailure": true
}
}
]

}
2 changes: 1 addition & 1 deletion JNI_BIND_VERSION.inc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.2.0
1.2.0
19 changes: 19 additions & 0 deletions cmake/add_jni_test.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function(add_jni_test test_name test_source)

# workaround duplicated target test name "invoke_test"
if (TARGET ${test_name})
set(test_name ${test_name}_2)
endif()

add_executable(${test_name} ${test_source})

target_link_libraries(${test_name}
PRIVATE
jni_bind
gmock
gtest_main)

add_test(
NAME ${test_name}
COMMAND ${test_name})
endfunction()
14 changes: 14 additions & 0 deletions cmake/compile_flags.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
message("Detected compiler [${CMAKE_CXX_COMPILER_ID}]")

SET(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

if(MSVC)
set(CXX_FLAGS "/Zc:__cplusplus /EHsc")
#elseif(Clang)
# ...
endif()

set(CMAKE_CXX_FLAGS "${CXX_FLAGS}" CACHE STRING "" FORCE)
#set(CMAKE_CXX_FLAGS_DEBUG "" CACHE STRING "" FORCE)
#set(CMAKE_CXX_FLAGS_RELEASE "" CACHE STRING "" FORCE)
22 changes: 22 additions & 0 deletions cmake/find_dependencies.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# JNI
find_package(JNI REQUIRED)
if (NOT JNI_FOUND)
message(FATAL_ERROR "JDK/JNI environment not found")
endif()

if (NOT TARGET jdk:jni)
add_library(jdk:jni INTERFACE IMPORTED GLOBAL)

set_target_properties(
jdk:jni
PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${JNI_INCLUDE_DIRS}"
INTERFACE_LINK_LIBRARIES "${JNI_LIBRARIES}")
endif()

# GoogleTest
include(FetchContent)
FetchContent_Declare(googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG f8d7d77) # 1.14.0
FetchContent_MakeAvailable(googletest)
13 changes: 6 additions & 7 deletions implementation/class.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,12 @@ struct Class<Extends_, std::tuple<Constructors_...>,
////////////////////////////////////////////////////////////////////////////////
// Equality operators.
////////////////////////////////////////////////////////////////////////////////
template <typename LhsParentClass, typename... LhsParams,
typename... LhsConstructors, typename... LhsStaticMethods,
typename... LhsStaticFields, typename... LhsFields,
typename... LhsMethods, typename RhsParentClass,
typename... RhsParams, typename... RhsConstructors,
typename... RhsStaticMethods, typename... RhsStaticFields,
typename... RhsFields, typename... RhsMethods>
template <
typename LhsParentClass, typename... LhsParams, typename... LhsConstructors,
typename... LhsStaticMethods, typename... LhsStaticFields,
typename... LhsFields, typename... LhsMethods, typename RhsParentClass,
typename... RhsConstructors, typename... RhsStaticMethods,
typename... RhsStaticFields, typename... RhsFields, typename... RhsMethods>
constexpr bool operator==(
const Class<LhsParentClass, std::tuple<LhsConstructors...>,
std::tuple<Static<std::tuple<LhsStaticMethods...>,
Expand Down
2 changes: 1 addition & 1 deletion implementation/jni_helper/jni_array_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ struct JniArrayHelper<jint, 1> : public JniArrayHelperBase {
#endif // DRY_RUN
}

static inline void ReleaseArrayElements(jarray array, int* native_ptr,
static inline void ReleaseArrayElements(jarray array, jint* native_ptr,
bool copy_on_completion) {
Trace(
metaprogramming::LambdaToStr(STR("ReleaseArrayElements, jint, Rank 1")),
Expand Down
12 changes: 7 additions & 5 deletions implementation/no_class_specified.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,18 @@

namespace jni {

struct ExtendsBase {};
struct ExtendsBase {
constexpr ExtendsBase() {}
};

struct RootObject {
constexpr RootObject() = default;
constexpr RootObject() {}
};

static constexpr RootObject kObject{};
constexpr RootObject kObject{};

static constexpr struct NoClass {
// For compatability reasons, this must be defined (not default) because some
constexpr struct NoClass {
// For compatibility reasons, this must be defined (not default) because some
// compilers will complain about defaulted constructors being deleted.
constexpr NoClass() {}

Expand Down
21 changes: 21 additions & 0 deletions implementation/proxy_definitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,27 @@ struct Proxy<CharType,
IsConvertibleKey<T>::template value<jchar>;
};

// jint is differently sized on Windows platforms. This surely leads to widening
// issues for lay developers, but we will try to silently widen for them.
template <typename IntType>
struct Proxy<IntType, typename std::enable_if_t<std::is_same_v<IntType, jint> &&
!std::is_same_v<int, jint>>>
: public ProxyBase<jint> {
using AsArg = std::tuple<int, jint>;
using AsDecl = std::tuple<int, jint>;

template <typename OverloadSelection, typename T>
static constexpr bool kViable = IsConvertibleKey<T>::template value<jint> ||
IsConvertibleKey<T>::template value<int>;

static jint ProxyAsArg(jint val) { return val; }

template <typename T, typename = std::enable_if_t<std::is_same_v<T, int>>>
static jint ProxyAsArg(T val) {
return static_cast<jint>(val);
}
};

template <typename BooleanType>
struct Proxy<BooleanType,
typename std::enable_if_t<std::is_same_v<BooleanType, jboolean>>>
Expand Down
20 changes: 0 additions & 20 deletions metaprogramming/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -1207,26 +1207,6 @@ cc_library(
hdrs = ["vals.h"],
)

cc_library(
name = "vals_equal",
hdrs = ["vals_equal.h"],

# This is incompatibel with MSVC, do not use.
visibility = ["//visibility:private"],
deps = [":vals"],
)

cc_test(
name = "vals_equal_test",
srcs = ["vals_equal_test.cc"],
tags = ["manual"],
deps = [
":vals",
":vals_equal",
"@googletest//:gtest_main",
],
)

cc_library(
name = "vals_equal_diminished",
hdrs = ["vals_equal_diminished.h"],
Expand Down
Loading
Loading