<commit_before>string(TOLOWER ${ANDROID_NDK_HOST_SYSTEM_NAME} ANDROID_SYSTEM_LOWER)

if (${ANDROID_SYSTEM_LOWER} STREQUAL "windows" OR
      ${ANDROID_SYSTEM_LOWER} STREQUAL "windows-x86_64")
    set(WN_NULL_LOCATION NUL)
else()
    set(WN_NULL_LOCATION /dev/null)
endif()

function(library_name var target)
  get_target_property(LIB_NAME ${target} LIBRARY_OUTPUT_NAME)
  if(NOT LIB_NAME)
    get_target_property(LIB_NAME ${target} OUTPUT_NAME)
  endif()
  if(NOT LIB_NAME)
    set(LIB_NAME ${target})
  endif()
  set(${var} ${LIB_NAME} PARENT_SCOPE)
endfunction()

function(fix_path source target)
  file(TO_NATIVE_PATH ${source} temp_target)
  string(REPLACE "\\" "\\\\" temp_target "${temp_target}")
  set(${target} ${temp_target} PARENT_SCOPE)
endfunction()

function(afix target prefix postfix)
  set(tempvar "")
  foreach(f ${ARGN})
    list(APPEND tempvar "${prefix}${f}${postfix}")
  endforeach()
  set(${target} "${tempvar}" PARENT_SCOPE)
endfunction()

if (${ANDROID_SYSTEM_LOWER} STREQUAL "windows" OR
  ${ANDROID_SYSTEM_LOWER} STREQUAL "windows-x86_64")
  find_host_program(PYTHON python REQUIRED)
  set(DEBUG_KEYSTORE_PATH "$ENV{USERPROFILE}\\.android\\debug.keystore")
  fix_path(${DEBUG_KEYSTORE_PATH} DEBUG_KEYSTORE_PATH)
else()
  set(DEBUG_KEYSTORE_PATH "$ENV{HOME}/.android/debug.keystore")
endif()

set(ANDROID_RELEASE_KEY_STORE "${DEBUG_KEYSTORE_PATH}" CACHE STRING "Keystore used to sign release executables")
set(ANDROID_RELEASE_KEY_ALIAS "androiddebugkey"  CACHE STRING "Alias for your release key")
set(ANDROID_RELEASE_KEY_STORE_PASSWORD ""  CACHE STRING "Password for your android keystore")
set(ANDROID_RELEASE_KEY_ALIAS_PASSWORD ""  CACHE STRING "Password for your android keystore")

function(build_apk)
  cmake_parse_arguments(
    PARSED_ARGS
    ""
    "TOP_LEVEL;DOMAIN;ACTIVITY;PROGRAM_NAME;TARGET"
    ""
    ${ARGN})
  if(NOT PARSED_ARGS_TOP_LEVEL)
    set(PARSED_ARGS_TOP_LEVEL "project")
  endif()
  if(NOT PARSED_ARGS_DOMAIN)
    set(PARSED_ARGS_DOMAIN "wn")
  endif()
  if(NOT PARSED_ARGS_ACTIVITY)
    message(FATAL_ERROR "Must supply an activity name to build_apk")
  endif()
  if(NOT PARSED_ARGS_PROGRAM_NAME)
    message(FATAL_ERROR "Must supply a program name to build_apk")
  endif()
  if(NOT PARSED_ARGS_TARGET)
    message(FATAL_ERROR "Must supply a target shared object to build_apk")
  endif()

  set(WN_PACKAGE_NAME ${PARSED_ARGS_TOP_LEVEL}.${PARSED_ARGS_DOMAIN}.${PARSED_ARGS_PROGRAM_NAME})
  if (CMAKE_BUILD_TYPE STREQUAL "Debug")
    set(WN_ANDROID_DEBUG "true")
    set(WN_ANDROID_BUILD "debug")
  else()
    set(WN_ANDROID_DEBUG "false")
    set(WN_ANDROID_BUILD "release")
  endif()
  set(WN_ACTIVITY_NAME ${PARSED_ARGS_ACTIVITY})
  set(WN_PROGRAM_NAME ${PARSED_ARGS_PROGRAM_NAME})
  set(WN_TARGET_DIR ${CMAKE_CURRENT_BINARY_DIR}/${WN_PROGRAM_NAME})

  set(configure_source_dir ${Posix_Android_ROOT_DIR}/cmake/android_helpers/app)

  set(ANDROID_RUNNER ${configure_source_dir}/android_runner.py)

  file(MAKE_DIRECTORY ${WN_TARGET_DIR})

  set(WN_TARGET_NAME ${PARSED_ARGS_TARGET} )
  if (CMAKE_BUILD_TYPE STREQUAL "Debug")
    set(WN_APK_NAME ${WN_TARGET_NAME}-debug.apk)
  else()
    set(WN_APK_NAME ${WN_TARGET_NAME}-release.apk)
  endif()
  set(WN_APK_LOCATION ${WN_TARGET_DIR}/bin/${WN_APK_NAME})

  get_target_property(PERMISSIONS ${PARSED_ARGS_TARGET} WN_ANDROID_PERMISSIONS)
  if (PERMISSIONS)
    afix(APP_PERMISSIONS
      "      <uses-permission android:name=\"android.permission."
      "\" />" ${PERMISSIONS})
    string(REPLACE ";" "\n" WN_PERMISSIONS "${APP_PERMISSIONS}")
  endif()
  library_name(LIB_NAME ${PARSED_ARGS_TARGET})<commit_msg>Force the READ_EXTERNAL_STORAGE permission for debug builds.<commit_after>string(TOLOWER ${ANDROID_NDK_HOST_SYSTEM_NAME} ANDROID_SYSTEM_LOWER)

if (${ANDROID_SYSTEM_LOWER} STREQUAL "windows" OR
      ${ANDROID_SYSTEM_LOWER} STREQUAL "windows-x86_64")
    set(WN_NULL_LOCATION NUL)
else()
    set(WN_NULL_LOCATION /dev/null)
endif()

function(library_name var target)
  get_target_property(LIB_NAME ${target} LIBRARY_OUTPUT_NAME)
  if(NOT LIB_NAME)
    get_target_property(LIB_NAME ${target} OUTPUT_NAME)
  endif()
  if(NOT LIB_NAME)
    set(LIB_NAME ${target})
  endif()
  set(${var} ${LIB_NAME} PARENT_SCOPE)
endfunction()

function(fix_path source target)
  file(TO_NATIVE_PATH ${source} temp_target)
  string(REPLACE "\\" "\\\\" temp_target "${temp_target}")
  set(${target} ${temp_target} PARENT_SCOPE)
endfunction()

function(afix target prefix postfix)
  set(tempvar "")
  foreach(f ${ARGN})
    list(APPEND tempvar "${prefix}${f}${postfix}")
  endforeach()
  set(${target} "${tempvar}" PARENT_SCOPE)
endfunction()

if (${ANDROID_SYSTEM_LOWER} STREQUAL "windows" OR
  ${ANDROID_SYSTEM_LOWER} STREQUAL "windows-x86_64")
  find_host_program(PYTHON python REQUIRED)
  set(DEBUG_KEYSTORE_PATH "$ENV{USERPROFILE}\\.android\\debug.keystore")
  fix_path(${DEBUG_KEYSTORE_PATH} DEBUG_KEYSTORE_PATH)
else()
  set(DEBUG_KEYSTORE_PATH "$ENV{HOME}/.android/debug.keystore")
endif()

set(ANDROID_RELEASE_KEY_STORE "${DEBUG_KEYSTORE_PATH}" CACHE STRING "Keystore used to sign release executables")
set(ANDROID_RELEASE_KEY_ALIAS "androiddebugkey"  CACHE STRING "Alias for your release key")
set(ANDROID_RELEASE_KEY_STORE_PASSWORD ""  CACHE STRING "Password for your android keystore")
set(ANDROID_RELEASE_KEY_ALIAS_PASSWORD ""  CACHE STRING "Password for your android keystore")

function(build_apk)
  cmake_parse_arguments(
    PARSED_ARGS
    ""
    "TOP_LEVEL;DOMAIN;ACTIVITY;PROGRAM_NAME;TARGET"
    ""
    ${ARGN})
  if(NOT PARSED_ARGS_TOP_LEVEL)
    set(PARSED_ARGS_TOP_LEVEL "project")
  endif()
  if(NOT PARSED_ARGS_DOMAIN)
    set(PARSED_ARGS_DOMAIN "wn")
  endif()
  if(NOT PARSED_ARGS_ACTIVITY)
    message(FATAL_ERROR "Must supply an activity name to build_apk")
  endif()
  if(NOT PARSED_ARGS_PROGRAM_NAME)
    message(FATAL_ERROR "Must supply a program name to build_apk")
  endif()
  if(NOT PARSED_ARGS_TARGET)
    message(FATAL_ERROR "Must supply a target shared object to build_apk")
  endif()

  set(WN_PACKAGE_NAME ${PARSED_ARGS_TOP_LEVEL}.${PARSED_ARGS_DOMAIN}.${PARSED_ARGS_PROGRAM_NAME})
  if (CMAKE_BUILD_TYPE STREQUAL "Debug")
    set(WN_ANDROID_DEBUG "true")
    set(WN_ANDROID_BUILD "debug")
  else()
    set(WN_ANDROID_DEBUG "false")
    set(WN_ANDROID_BUILD "release")
  endif()
  set(WN_ACTIVITY_NAME ${PARSED_ARGS_ACTIVITY})
  set(WN_PROGRAM_NAME ${PARSED_ARGS_PROGRAM_NAME})
  set(WN_TARGET_DIR ${CMAKE_CURRENT_BINARY_DIR}/${WN_PROGRAM_NAME})

  set(configure_source_dir ${Posix_Android_ROOT_DIR}/cmake/android_helpers/app)

  set(ANDROID_RUNNER ${configure_source_dir}/android_runner.py)

  file(MAKE_DIRECTORY ${WN_TARGET_DIR})

  set(WN_TARGET_NAME ${PARSED_ARGS_TARGET} )
  if (CMAKE_BUILD_TYPE STREQUAL "Debug")
    set(WN_APK_NAME ${WN_TARGET_NAME}-debug.apk)
  else()
    set(WN_APK_NAME ${WN_TARGET_NAME}-release.apk)
  endif()
  set(WN_APK_LOCATION ${WN_TARGET_DIR}/bin/${WN_APK_NAME})

  get_target_property(PERMISSIONS ${PARSED_ARGS_TARGET} WN_ANDROID_PERMISSIONS)
  if (CMAKE_BUILD_TYPE STREQUAL "Debug")
    if (NOT PERMISSIONS)
      set(PERMISSIONS "READ_EXTERNAL_STORAGE")
    else()
      list(APPEND PERMISSIONS "READ_EXTERNAL_STORAGE")
    endif()
  endif()
 
  if (PERMISSIONS)
    afix(APP_PERMISSIONS
      "      <uses-permission android:name=\"android.permission."
      "\" />" ${PERMISSIONS})
    string(REPLACE ";" "\n" WN_PERMISSIONS "${APP_PERMISSIONS}")
  endif()
  library_name(LIB_NAME ${PARSED_ARGS_TARGET})
{'fraction_non_alphanumeric': 0.11814289537061815, 'fraction_numerical': 0.0021407546160021407, 'mean_word_length': 6.018779342723005, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '3134225', 'n_tokens_mistral': 2944, 'n_tokens_neox': 2831, 'n_words': 474}