From d1ce9656459007b5c7f386fdafb018ca16ddf0c9 Mon Sep 17 00:00:00 2001 From: Gautham B A Date: Tue, 22 Oct 2024 23:15:23 +0530 Subject: [PATCH] HDFS-17636. Don't add declspec for Windows (#7096) * Windows doesn't want the macro _JNI_IMPORT_OR_EXPORT_ to be defined in the function definition. It fails to compile with the following error - "definition of dllimport function not allowed". * However, Linux needs it. Hence, we're going to add this macro based on the OS. * Also, we'll be compiling the `hdfs` target as an object library so that we can avoid linking to `jvm` library for `get_jni_test` target. --- .../src/main/native/libhdfs/CMakeLists.txt | 12 +++++++++++- .../main/native/libhdfspp/tests/CMakeLists.txt | 13 ++++++++++++- .../libhdfspp/tests/libhdfs_getjni_test.cc | 17 +++++++++++++++-- 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfs/CMakeLists.txt b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfs/CMakeLists.txt index 958bdcf2a41c9..7094b49fd15a0 100644 --- a/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfs/CMakeLists.txt +++ b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfs/CMakeLists.txt @@ -32,13 +32,23 @@ include_directories( ../libhdfspp/lib ) -hadoop_add_dual_library(hdfs +set(HDFS_SOURCES exception.c jni_helper.c hdfs.c jclasses.c ${OS_DIR}/mutexes.c ${OS_DIR}/thread_local_storage.c +) +# We want to create an object library for hdfs +# so that we can reuse it for the targets +# (like get_jni_test), where we don't wish to +# link to hdfs's publicly linked libraries +# (like jvm) +add_library(hdfs_obj OBJECT ${HDFS_SOURCES}) +set_target_properties(hdfs_obj PROPERTIES POSITION_INDEPENDENT_CODE ON) +hadoop_add_dual_library(hdfs + $ $ $ ) diff --git a/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/CMakeLists.txt b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/CMakeLists.txt index 3e52c6d965a01..3e3112181d194 100644 --- a/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/CMakeLists.txt +++ b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/CMakeLists.txt @@ -74,8 +74,19 @@ add_executable(uri_test uri_test.cc) target_link_libraries(uri_test common gmock_main ${CMAKE_THREAD_LIBS_INIT}) add_memcheck_test(uri uri_test) +# We want to link to all the libraries of hdfs_static library, +# except jvm.lib since we want to override some of the functions +# provided by jvm.lib. +get_target_property(HDFS_STATIC_LIBS_NO_JVM hdfs_static LINK_LIBRARIES) +list(REMOVE_ITEM HDFS_STATIC_LIBS_NO_JVM ${JAVA_JVM_LIBRARY}) add_executable(get_jni_test libhdfs_getjni_test.cc) -target_link_libraries(get_jni_test gmock_main hdfs_static ${CMAKE_THREAD_LIBS_INIT}) +target_link_libraries(get_jni_test + gmock_main + $ + $ + $ + ${HDFS_STATIC_LIBS_NO_JVM} + ${CMAKE_THREAD_LIBS_INIT}) add_memcheck_test(get_jni get_jni_test) add_executable(remote_block_reader_test remote_block_reader_test.cc) diff --git a/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/libhdfs_getjni_test.cc b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/libhdfs_getjni_test.cc index b2648da23bb4d..a4356a5255fd5 100644 --- a/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/libhdfs_getjni_test.cc +++ b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/libhdfs_getjni_test.cc @@ -20,13 +20,26 @@ #include #include +#ifdef WIN32 +#define DECLSPEC +#else +// Windows cribs when this is declared in the function definition, +// However, Linux needs it. +#define DECLSPEC _JNI_IMPORT_OR_EXPORT_ +#endif + +// hook the jvm runtime function. expect always failure +DECLSPEC jint JNICALL JNI_GetDefaultJavaVMInitArgs(void*) { + return 1; +} + // hook the jvm runtime function. expect always failure -_JNI_IMPORT_OR_EXPORT_ jint JNICALL JNI_GetDefaultJavaVMInitArgs(void*) { +DECLSPEC jint JNICALL JNI_CreateJavaVM(JavaVM**, void**, void*) { return 1; } // hook the jvm runtime function. expect always failure -_JNI_IMPORT_OR_EXPORT_ jint JNICALL JNI_CreateJavaVM(JavaVM**, void**, void*) { +DECLSPEC jint JNICALL JNI_GetCreatedJavaVMs(JavaVM**, jsize, jsize*) { return 1; }