<commit_before>cmake_minimum_required(VERSION 3.9)
project(SKryptonNative)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_AUTOMOC ON)

find_package(Qt5 COMPONENTS
        Quick
        Core
        Network
        Widgets
        WebEngine
        WebEngineWidgets
        WebSockets)

find_package(Java)
find_package(JNI)
include(UseJava)

file(GLOB_RECURSE SOURCE_FILES "src/*.c" "src/*.cpp")
file(GLOB_RECURSE INC_FILES "inc/*.h")
include_directories(inc inc/jni inc/jni/headers ${JNI_INCLUDE_DIRS})

add_library(${CMAKE_PROJECT_NAME} SHARED ${SOURCE_FILES} ${INC_FILES} ${RESOURCES})
add_dependencies(${CMAKE_PROJECT_NAME} JNI_Headers)
target_link_libraries(${CMAKE_PROJECT_NAME}
        Qt5::Quick
        Qt5::Core
        Qt5::Network
        Qt5::Widgets
        Qt5::WebEngine
        Qt5::WebEngineWidgets
        Qt5::WebSockets
        ${JNI_LIBRARIES})

install(TARGETS ${CMAKE_PROJECT_NAME} DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/build)

# Scan for JNI classes and generate headers

get_filename_component(
        PARENT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../"
        REALPATH BASE_DIR "${CMAKE_BINARY_DIR}"
)

set(CLASS_SEARCH_DIR "${PARENT_DIR}/build/classes/main")
file(GLOB_RECURSE JAVA_CLASSES "${CLASS_SEARCH_DIR}/*.class")

foreach (CLASS ${JAVA_CLASSES})
    if (NOT ${CLASS} MATCHES ".*\\$.*" AND ${CLASS} MATCHES "jni/objects/.*")
        STRING(REGEX REPLACE "${CLASS_SEARCH_DIR}/" "" CLASS ${CLASS})
        STRING(REGEX REPLACE "/" "." CLASS ${CLASS})
        STRING(REGEX REPLACE ".class" "" CLASS ${CLASS})
        list(APPEND JNI_CLASSES ${CLASS})
        message(STATUS "[JAVAH] Will generate headers for class: \"${CLASS}\"")
    endif ()
endforeach ()

if (NOT JNI_CLASSES)
    message(FATAL_ERROR "Could not find any suitable Java classes for header generation")
endif ()

add_custom_target(
        Clean_JNI_Headers
        COMMAND ${CMAKE_COMMAND} -E remove_directory "${CMAKE_CURRENT_SOURCE_DIR}/inc/jni/headers"
        COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_CURRENT_SOURCE_DIR}/inc/jni/headers"
)

if (UNIX)
    set(GRADLE "${PARENT_DIR}/gradlew")
else ()
    set(GRADLE "${PARENT_DIR}/gradlew.bat")
endif ()

add_custom_target(
        CopyKotlinStdLib
        COMMAND "${GRADLE}" native:copyKotlinStdLib
        WORKING_DIRECTORY "${PARENT_DIR}"
)

if (NOT EXISTS build/kstdlib)
    file(MAKE_DIRECTORY build/kstdlib)
endif ()

create_javah(
        TARGET JNI_Headers
        DEPENDS Clean_JNI_Headers CopyKotlinStdLib
        CLASSES ${JNI_CLASSES}
        CLASSPATH ${CLASS_SEARCH_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/build/kstdlib
        OUTPUT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/inc/jni/headers
)<commit_msg>Copying kotlin classes on cmake reload instead of each time during build<commit_after>cmake_minimum_required(VERSION 3.9)
project(SKryptonNative)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_AUTOMOC ON)

find_package(Qt5 COMPONENTS
        Quick
        Core
        Network
        Widgets
        WebEngine
        WebEngineWidgets
        WebSockets)

find_package(Java)
find_package(JNI)
include(UseJava)

file(GLOB_RECURSE SOURCE_FILES "src/*.c" "src/*.cpp")
file(GLOB_RECURSE INC_FILES "inc/*.h")
include_directories(inc inc/jni inc/jni/headers ${JNI_INCLUDE_DIRS})

add_library(${CMAKE_PROJECT_NAME} SHARED ${SOURCE_FILES} ${INC_FILES} ${RESOURCES})
add_dependencies(${CMAKE_PROJECT_NAME} JNI_Headers)
target_link_libraries(${CMAKE_PROJECT_NAME}
        Qt5::Quick
        Qt5::Core
        Qt5::Network
        Qt5::Widgets
        Qt5::WebEngine
        Qt5::WebEngineWidgets
        Qt5::WebSockets
        ${JNI_LIBRARIES})

install(TARGETS ${CMAKE_PROJECT_NAME} DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/build)

# Scan for JNI classes and generate headers

get_filename_component(
        PARENT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../"
        REALPATH BASE_DIR "${CMAKE_BINARY_DIR}"
)

set(CLASS_SEARCH_DIR "${PARENT_DIR}/build/classes/main")
file(GLOB_RECURSE JAVA_CLASSES "${CLASS_SEARCH_DIR}/*.class")

foreach (CLASS ${JAVA_CLASSES})
    if (NOT ${CLASS} MATCHES ".*\\$.*" AND ${CLASS} MATCHES "jni/objects/.*")
        STRING(REGEX REPLACE "${CLASS_SEARCH_DIR}/" "" CLASS ${CLASS})
        STRING(REGEX REPLACE "/" "." CLASS ${CLASS})
        STRING(REGEX REPLACE ".class" "" CLASS ${CLASS})
        list(APPEND JNI_CLASSES ${CLASS})
        message(STATUS "[JAVAH] Will generate headers for class: \"${CLASS}\"")
    endif ()
endforeach ()

if (NOT JNI_CLASSES)
    message(FATAL_ERROR "Could not find any suitable Java classes for header generation")
endif ()

add_custom_target(
        Clean_JNI_Headers
        COMMAND ${CMAKE_COMMAND} -E remove_directory "${CMAKE_CURRENT_SOURCE_DIR}/inc/jni/headers"
        COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_CURRENT_SOURCE_DIR}/inc/jni/headers"
)

if (UNIX)
    set(GRADLE "${PARENT_DIR}/gradlew")
else ()
    set(GRADLE "${PARENT_DIR}/gradlew.bat")
endif ()

set(FUNCTION_CLASSES "${CMAKE_CURRENT_SOURCE_DIR}/build/kstdlib/kotlin/Function.class")
foreach (NUM RANGE 22)
    list(APPEND FUNCTION_CLASSES "${CMAKE_CURRENT_SOURCE_DIR}/build/kstdlib/kotlin/jvm/functions/Function${NUM}.class")
endforeach ()

foreach (FILE ${FUNCTION_CLASSES})
    if (NOT EXISTS ${FILE})
        execute_process(
                COMMAND "${GRADLE}" native:copyKotlinStdLib
                WORKING_DIRECTORY "${PARENT_DIR}"
        )
        break()
    endif ()
endforeach ()

if (NOT EXISTS build/kstdlib)
    file(MAKE_DIRECTORY build/kstdlib)
endif ()

create_javah(
        TARGET JNI_Headers
        DEPENDS Clean_JNI_Headers
        CLASSES ${JNI_CLASSES}
        CLASSPATH ${CLASS_SEARCH_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/build/kstdlib
        OUTPUT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/inc/jni/headers
)
{'fraction_non_alphanumeric': 0.11932714210618539, 'fraction_numerical': 0.004555808656036446, 'mean_word_length': 3.895368782161235, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1640725', 'n_tokens_mistral': 2138, 'n_tokens_neox': 1958, 'n_words': 365}