<commit_before>find_package(Git REQUIRED)

if(WIN32)
    set(BOOTSTRAP_SCRIPT "bootstrap.bat")
else()
    set(BOOTSTRAP_SCRIPT "bootstrap.sh")
endif()

set(BUILD_SCRIPT "b2")
set(BCP_EXEC_PATH "dist/bin/bcp")

function(execute_git)
    set(oneValueArgs
        WORKING_DIRECTORY
        OUTPUT_VARIABLE
    )
    set(multiValueArgs
        COMMAND
    )
    cmake_parse_arguments(args "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

    if(NOT args_WORKING_DIRECTORY)
        set(args_WORKING_DIRECTORY "${CMAKE_BINARY_DIR}")
    endif()

    message("Running git-command: git ${args_COMMAND}")

    execute_process(
        COMMAND ${GIT_EXECUTABLE} ${args_COMMAND}
        OUTPUT_VARIABLE GIT_RESULT
        ERROR_VARIABLE GIT_ERROR
        RESULT_VARIABLE RETURN_CODE
        WORKING_DIRECTORY ${args_WORKING_DIRECTORY}
    )

    if(NOT "${RETURN_CODE}" STREQUAL "0")
        message(FATAL_ERROR "Failed to run 'git ${args_COMMAND}' :\n\t${GIT_ERROR}")
    endif()

    set(${args_OUTPUT_VARIABLE} ${GIT_RESULT} PARENT_SCOPE)
endfunction()

function(clone_repo)
    set(oneValueArgs
        URL
        TAG
        CLONE_DIR
    )
    cmake_parse_arguments(args "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

    if(NOT EXISTS "${args_CLONE_DIR}")
        execute_git(
            COMMAND clone --depth=1 --single-branch --branch ${args_TAG} ${args_URL} "${args_CLONE_DIR}"
        )
        # Fix for issues with long paths
        execute_git(
            COMMAND config --system core.longpaths true
            WORKING_DIRECTORY ${args_CLONE_DIR}
        )
    else()
    # what to do here? Safest is to download again... Or clear changes and pull
        return()
        execute_git(
            COMMAND pull -b ${args_TAG} --single-branch ${args_URL} "${args_CLONE_DIR}"
            WORKING_DIRECTORY "${args_CLONE_DIR}"
        )
    endif()
endfunction()

function(pull_modules)
    set(oneValueArgs
        BOOST_DIR
    )
    cmake_parse_arguments(args "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

    execute_git(
        COMMAND submodule update --depth=1 --init
        WORKING_DIRECTORY "${args_BOOST_DIR}"
    )

endfunction()

function(copy_required_modules)
    set(oneValueArgs
        BOOST_DIR
        OUTPUT_DIR
    )
    set(multiValueArgs
        SUBMODULES
    )
    cmake_parse_arguments(args "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

    file(MAKE_DIRECTORY "${args_OUTPUT_DIR}")
    message("Copying subset of boost required for modules: ${args_SUBMODULES}\n\tTo folder: ${args_OUTPUT_DIR}")
    execute_process(
        COMMAND "${BCP_EXEC_PATH}" ${args_SUBMODULES} "${args_OUTPUT_DIR}"
        WORKING_DIRECTORY "${args_BOOST_DIR}"
    )

endfunction()

function(remove_non_source)
    set(oneValueArgs
        BOOST_DIR
    )
    cmake_parse_arguments(args "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

    message("Removing non-source files from all libraries, including docs, examples & tests.")
    
    file(GLOB NON_SOURCE_DIRS
        ABSOLUTE "${args_BOOST_DIR}/" 
            "${args_BOOST_DIR}/libs/*/doc/"
            "${args_BOOST_DIR}/libs/*/example/"
            "${args_BOOST_DIR}/libs/*/test/"
            "${args_BOOST_DIR}/tools/*/doc/"
            "${args_BOOST_DIR}/tools/*/example/"
            "${args_BOOST_DIR}/tools/*/test/"
    )

    foreach(DIR ${NON_SOURCE_DIRS})
        file(REMOVE_RECURSE "${DIR}")
        file(RELATIVE_PATH REL_DIR "${args_BOOST_DIR}" "${DIR}")
        message("Removed: ${REL_DIR}.")
    endforeach()

endfunction()

function(download_boost)
    set(oneValueArgs
        URL
        TAG
        CLONE_DIR
    )
    set(multiValueArgs
        SUBMODULES<commit_msg>Improved finding of doc/test/example folders when removing 'non-source'<commit_after>find_package(Git REQUIRED)

if(WIN32)
    set(BOOTSTRAP_SCRIPT "bootstrap.bat")
else()
    set(BOOTSTRAP_SCRIPT "bootstrap.sh")
endif()

set(BUILD_SCRIPT "b2")
set(BCP_EXEC_PATH "dist/bin/bcp")

function(execute_git)
    set(oneValueArgs
        WORKING_DIRECTORY
        OUTPUT_VARIABLE
    )
    set(multiValueArgs
        COMMAND
    )
    cmake_parse_arguments(args "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

    if(NOT args_WORKING_DIRECTORY)
        set(args_WORKING_DIRECTORY "${CMAKE_BINARY_DIR}")
    endif()

    message("Running git-command: git ${args_COMMAND}")

    execute_process(
        COMMAND ${GIT_EXECUTABLE} ${args_COMMAND}
        OUTPUT_VARIABLE GIT_RESULT
        ERROR_VARIABLE GIT_ERROR
        RESULT_VARIABLE RETURN_CODE
        WORKING_DIRECTORY ${args_WORKING_DIRECTORY}
    )

    if(NOT "${RETURN_CODE}" STREQUAL "0")
        message(FATAL_ERROR "Failed to run 'git ${args_COMMAND}' :\n\t${GIT_ERROR}")
    endif()

    set(${args_OUTPUT_VARIABLE} ${GIT_RESULT} PARENT_SCOPE)
endfunction()

function(clone_repo)
    set(oneValueArgs
        URL
        TAG
        CLONE_DIR
    )
    cmake_parse_arguments(args "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

    if(NOT EXISTS "${args_CLONE_DIR}")
        execute_git(
            COMMAND clone --depth=1 --single-branch --branch ${args_TAG} ${args_URL} "${args_CLONE_DIR}"
        )
        # Fix for issues with long paths
        execute_git(
            COMMAND config --system core.longpaths true
            WORKING_DIRECTORY ${args_CLONE_DIR}
        )
    else()
    # what to do here? Safest is to download again... Or clear changes and pull
        return()
        execute_git(
            COMMAND pull -b ${args_TAG} --single-branch ${args_URL} "${args_CLONE_DIR}"
            WORKING_DIRECTORY "${args_CLONE_DIR}"
        )
    endif()
endfunction()

function(pull_modules)
    set(oneValueArgs
        BOOST_DIR
    )
    cmake_parse_arguments(args "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

    execute_git(
        COMMAND submodule update --depth=1 --init
        WORKING_DIRECTORY "${args_BOOST_DIR}"
    )

endfunction()

function(copy_required_modules)
    set(oneValueArgs
        BOOST_DIR
        OUTPUT_DIR
    )
    set(multiValueArgs
        SUBMODULES
    )
    cmake_parse_arguments(args "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

    file(MAKE_DIRECTORY "${args_OUTPUT_DIR}")
    message("Copying subset of boost required for modules: ${args_SUBMODULES}\n\tTo folder: ${args_OUTPUT_DIR}")
    execute_process(
        COMMAND "${BCP_EXEC_PATH}" ${args_SUBMODULES} "${args_OUTPUT_DIR}"
        WORKING_DIRECTORY "${args_BOOST_DIR}"
    )

endfunction()

function(remove_non_source)
    set(oneValueArgs
        BOOST_DIR
    )
    cmake_parse_arguments(args "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

    message("Removing non-source files from all libraries, including docs, examples & tests.")
    
    file(GLOB_RECURSE NON_SOURCE_DIRS
        LIST_DIRECTORIES true
        "*"
    )
    list(FILTER NON_SOURCE_DIRS 
        INCLUDE
        REGEX "^.*\/(doc|example|test)$"
    )

    foreach(DIR ${NON_SOURCE_DIRS})
        file(REMOVE_RECURSE "${DIR}")
        file(RELATIVE_PATH REL_DIR "${args_BOOST_DIR}" "${DIR}")
        message("Removed: ${REL_DIR}.")
    endforeach()

endfunction()

function(download_boost)
    set(oneValueArgs
        URL
        TAG
        CLONE_DIR
    )
    set(multiValueArgs
        SUBMODULES
{'fraction_non_alphanumeric': 0.1220708446866485, 'fraction_numerical': 0.0016348773841961854, 'mean_word_length': 2.985342019543974, '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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '753174', 'n_tokens_mistral': 2462, 'n_tokens_neox': 2377, 'n_words': 445}