<commit_before>cmake_minimum_required(VERSION 3.5)
project(tiramisu)


enable_testing()

set(PROJECT_DIR ${CMAKE_SOURCE_DIR})

set(CMAKE_EXPORT_COMPILE_COMMANDS "ON")


# Add CMake header files
set(HEADER_FILES
        include/tiramisu/cuda_ast.h
        include/tiramisu/core.h
        include/tiramisu/debug.h
        include/tiramisu/utils.h
        include/tiramisu/type.h
        include/tiramisu/expr.h
        include/tiramisu/mpi_comm.h
        )

# Add CMake cpp files
set(OBJS expr core codegen_halide codegen_c debug utils codegen_halide_lowering codegen_from_halide mpi codegen_cuda)


### CMAKE FILE INTERNALS ###

include(configure.cmake)

if (${USE_GPU})
    find_package(CUDA REQUIRED)
    set(GPU_ENV_EXEC "${CMAKE_COMMAND} -E env NVCC_BIN_DIR=${CUDA_TOOLKIT_ROOT_DIR}/bin/")
    add_library(cuda_wrapper STATIC "src/tiramisu_cuda_wrappers.cpp")
    target_link_libraries(cuda_wrapper ${CUDA_LIBRARIES})
    target_include_directories(cuda_wrapper PUBLIC ${CUDA_INCLUDE_DIRS})
endif()


FILE(STRINGS test_list TIRAMISU_TESTS_RAW)
FILE(STRINGS benchmark_list TIRAMISU_BENCHMARKS_RAW)
FILE(STRINGS tutorial_list TIRAMISU_TUTORIALS_RAW)

macro(init_tags)
    set(is_gpu false)
endmacro()

macro(parse_tags tags)
    foreach(tag ${tags})
        if (${tag} STREQUAL gpu)
            set(is_gpu true)
        else()
            message(WARNING "Tag ${tag} not recognized")
        endif()
    endforeach()
endmacro()

# transforms the list of the format (program_name/program_name[tag0,tag1,..,tagn]) into a list
# containing a list of "program_name property0 property1 property2 .. propertyn"
# the list can contain as many tags as necessary
# the number of properties is fixed and is in the order given below
# current tags are : gpu.
# current properties are : is_gpu(boolean)
function (parse_list raw_list list_name)
    set(result)
    foreach(element ${raw_list})
        init_tags()

        string(REGEX MATCH "^([a-zA-Z_0-9]+)(\\[([a-zA-Z0-9_]+(,[a-zA-Z0-9_]+)*)\\])?$" the_match "${element}")
        if(NOT ${CMAKE_MATCH_COUNT})
            message(WARNING "Could not parse: ${element}")
            continue()
        elseif(${CMAKE_MATCH_COUNT} GREATER 1)
            STRING(REPLACE "," ";" tags ${CMAKE_MATCH_3})
            parse_tags("${tags}")
        endif()
        set(name ${CMAKE_MATCH_1})
        if (${is_gpu} AND NOT "${USE_GPU}")
            message(STATUS "skipping gpu tagged ${name} because USE_GPU is not set to true")
        else()
            list(APPEND result "${name} ${is_gpu}")
        endif()
    endforeach()
    set("${list_name}" "${result}" PARENT_SCOPE)
endfunction()


parse_list("${TIRAMISU_BENCHMARKS_RAW}" TIRAMISU_BENCHMARKS)
parse_list("${TIRAMISU_TESTS_RAW}" TIRAMISU_TESTS)
parse_list("${TIRAMISU_TUTORIALS_RAW}" TIRAMISU_TUTORIALS)



set(CMAKE_CXX_COMPILER "g++")
set(CMAKE_CXX_FLAGS "-g -std=c++11 -Wall -Wno-sign-compare -fno-rtti")

set(CMAKE_CXX_FLAGS_DEBUG "-g -std=c++11 -Wall -Wno-sign-compare -fno-rtti")# -fvisibility=hidden")
set(CMAKE_CXX_FLAGS_RELEASE "-g -std=c++11 -O3 -Wall -Wno-sign-compare -fno-rtti")# -fvisibility=hidden")

execute_process(COMMAND ${MPICXX_BIN}mpicxx -showme:incdirs OUTPUT_VARIABLE MPI_INCLUDE_DIRS)
execute_process(COMMAND ${MPICXX_BIN}mpicxx -showme:link OUTPUT_VARIABLE MPI_LIB_FLAGS)

string(STRIP ${MPI_INCLUDE_DIRS} MPI_INCLUDE_DIRS)
string(REPLACE " " ";" MPI_INCLUDE_DIRS ${MPI_INCLUDE_DIRS})
string(STRIP ${MPI_LIB_FLAGS} MPI_LIB_FLAGS)

include_directories("include"
                    "${ISL_INCLUDE_DIRECTORY}"
                    "${HALIDE_SOURCE_DIRECTORY}/include" "${HALIDE_SOURCE_DIRECTORY}/tools"
                    ${MPI_INCLUDE_DIRS}
                    "build")

find_library(HalideLib Halide PATHS ${HALIDE_LIB_DIRECTORY} NO_DEFAULT_PATH)
find_library(ISLLib isl PATHS ${ISL_LIB_DIRECTORY} NO_DEFAULT_PATH)

execute_process(COMMAND libpng-config --ldflags OUTPUT_VARIABLE LIBPNG_FLAGS)
execute_process(COMMAND ${LLVM_CONFIG_BIN}llvm-config --ignore-libllvm --system-libs OUTPUT_VARIABLE LLVM_FLAGS)

string(STRIP ${LLVM_FLAGS} LLVM_FLAGS)
string(STRIP ${LIBPNG_FLAGS} LIBPNG_FLAGS)


set(LINK_FLAGS "-lgmp -ldl -lpthread -lz -ljpeg -lmpi")
set(LINK_FLAGS "${LINK_FLAGS} ${LLVM_FLAGS} ${LIBPNG_FLAGS} ${MPI_LIB_FLAGS}")


set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_DIR}/build)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_DIR}/build)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_DIR}/build)

set(T_CODE "")

foreach (obj ${OBJS})
    set(T_CODE ${T_CODE} src/tiramisu_${obj}.cpp)
endforeach ()

add_library(tiramisu SHARED ${T_CODE})
target_link_libraries(tiramisu ${HalideLib} ${ISLLib})
target_link_libraries(tiramisu ${LINK_FLAGS})

function(link_tags name)
    if (${is_gpu})
        target_link_libraries(${name} cuda_wrapper)
    endif()
endfunction()

function(build_g name generator result)
    add_executable(${name} ${generator} ${HEADER_FILES})
    target_link_libraries(${name} tiramisu ${HalideLib} ${ISLLib})
    target_link_libraries(${name} ${LINK_FLAGS})
    link_tags(${name})
    if (NOT result STREQUAL "")
        set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES "${result}")
        add_custom_command(OUTPUT ${result} COMMAND ${name} WORKING_DIRECTORY ${PROJECT_DIR} DEPENDS ${name})<commit_msg>Update CMakeLists.txt<commit_after>cmake_minimum_required(VERSION 3.5)
project(tiramisu)


enable_testing()

set(PROJECT_DIR ${CMAKE_SOURCE_DIR})

set(CMAKE_EXPORT_COMPILE_COMMANDS "ON")


# Add CMake header files
set(HEADER_FILES
        include/tiramisu/cuda_ast.h
        include/tiramisu/core.h
        include/tiramisu/debug.h
        include/tiramisu/utils.h
        include/tiramisu/type.h
        include/tiramisu/expr.h
        include/tiramisu/mpi_comm.h
        )

# Add CMake cpp files
set(OBJS expr core codegen_halide codegen_c debug utils codegen_halide_lowering codegen_from_halide mpi codegen_cuda)


### CMAKE FILE INTERNALS ###

include(configure.cmake)

if (${USE_GPU})
    find_package(CUDA REQUIRED)
    set(GPU_ENV_EXEC "${CMAKE_COMMAND} -E env NVCC_BIN_DIR=${CUDA_TOOLKIT_ROOT_DIR}/bin/")
    add_library(cuda_wrapper STATIC "src/tiramisu_cuda_wrappers.cpp")
    target_link_libraries(cuda_wrapper ${CUDA_LIBRARIES})
    target_include_directories(cuda_wrapper PUBLIC ${CUDA_INCLUDE_DIRS})
endif()


FILE(STRINGS test_list TIRAMISU_TESTS_RAW)
FILE(STRINGS benchmark_list TIRAMISU_BENCHMARKS_RAW)
FILE(STRINGS tutorial_list TIRAMISU_TUTORIALS_RAW)

macro(init_tags)
    set(is_gpu false)
endmacro()

macro(parse_tags tags)
    foreach(tag ${tags})
        if (${tag} STREQUAL gpu)
            set(is_gpu true)
        else()
            message(WARNING "Tag ${tag} not recognized")
        endif()
    endforeach()
endmacro()

# transforms the list of the format (program_name/program_name[tag0,tag1,..,tagn]) into a list
# containing a list of "program_name property0 property1 property2 .. propertyn"
# the list can contain as many tags as necessary
# the number of properties is fixed and is in the order given below
# current tags are : gpu.
# current properties are : is_gpu(boolean)
function (parse_list raw_list list_name)
    set(result)
    foreach(element ${raw_list})
        init_tags()

        string(REGEX MATCH "^([a-zA-Z_0-9]+)(\\[([a-zA-Z0-9_]+(,[a-zA-Z0-9_]+)*)\\])?$" the_match "${element}")
        if(NOT ${CMAKE_MATCH_COUNT})
            message(WARNING "Could not parse: ${element}")
            continue()
        elseif(${CMAKE_MATCH_COUNT} GREATER 1)
            STRING(REPLACE "," ";" tags ${CMAKE_MATCH_3})
            parse_tags("${tags}")
        endif()
        set(name ${CMAKE_MATCH_1})
        if (${is_gpu} AND NOT "${USE_GPU}")
            message(STATUS "skipping gpu tagged ${name} because USE_GPU is not set to true")
        else()
            list(APPEND result "${name} ${is_gpu}")
        endif()
    endforeach()
    set("${list_name}" "${result}" PARENT_SCOPE)
endfunction()


parse_list("${TIRAMISU_BENCHMARKS_RAW}" TIRAMISU_BENCHMARKS)
parse_list("${TIRAMISU_TESTS_RAW}" TIRAMISU_TESTS)
parse_list("${TIRAMISU_TUTORIALS_RAW}" TIRAMISU_TUTORIALS)


if (${USE_MPI})
  set(CMAKE_CXX_COMPILER "mpicxx")
else()
  set(CMAKE_CXX_COMPILER "g++")
endif()
set(CMAKE_CXX_FLAGS "-g -std=c++11 -Wall -Wno-sign-compare -fno-rtti")

set(CMAKE_CXX_FLAGS_DEBUG "-g -std=c++11 -Wall -Wno-sign-compare -fno-rtti")# -fvisibility=hidden")
set(CMAKE_CXX_FLAGS_RELEASE "-std=c++11 -O3 -Wall -Wno-sign-compare -fno-rtti")# -fvisibility=hidden")

link_directories(${MPI_LIB_DIR})
include_directories("include"
                    "${ISL_INCLUDE_DIRECTORY}"
                    "${HALIDE_SOURCE_DIRECTORY}/include" "${HALIDE_SOURCE_DIRECTORY}/tools"
                    ${MPI_INCLUDE_DIRS}
                    "build")

find_library(HalideLib Halide PATHS ${HALIDE_LIB_DIRECTORY} NO_DEFAULT_PATH)
find_library(ISLLib isl PATHS ${ISL_LIB_DIRECTORY} NO_DEFAULT_PATH)

execute_process(COMMAND libpng-config --ldflags OUTPUT_VARIABLE LIBPNG_FLAGS)
execute_process(COMMAND ${LLVM_CONFIG_BIN}llvm-config --ignore-libllvm --system-libs OUTPUT_VARIABLE LLVM_FLAGS)

string(STRIP ${LLVM_FLAGS} LLVM_FLAGS)
string(STRIP ${LIBPNG_FLAGS} LIBPNG_FLAGS)


set(LINK_FLAGS "-lgmp -ldl -lpthread -lz -ljpeg")
set(LINK_FLAGS "${LINK_FLAGS} ${LLVM_FLAGS} ${LIBPNG_FLAGS} ${MPI_LIB_FLAGS}")


set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_DIR}/build)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_DIR}/build)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_DIR}/build)

set(T_CODE "")

foreach (obj ${OBJS})
    set(T_CODE ${T_CODE} src/tiramisu_${obj}.cpp)
endforeach ()

add_library(tiramisu SHARED ${T_CODE})
target_link_libraries(tiramisu ${HalideLib} ${ISLLib})
target_link_libraries(tiramisu ${LINK_FLAGS})

function(link_tags name)
    if (${is_gpu})
        target_link_libraries(${name} cuda_wrapper)
    endif()
endfunction()

function(build_g name generator result)
    add_executable(${name} ${generator} ${HEADER_FILES})
    target_link_libraries(${name} tiramisu ${HalideLib} ${ISLLib})
    target_link_libraries(${name} ${LINK_FLAGS})
    link_tags(${name})
    if (NOT result STREQUAL "")
        set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES "${result}")
        add_custom_command(OUTPUT ${result} COMMAND ${name} WORKING_DIRECTORY ${PROJECT_DIR} DEPENDS ${name})
{'fraction_non_alphanumeric': 0.12480544747081712, 'fraction_numerical': 0.004474708171206225, 'mean_word_length': 4.4773574853489615, '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': '7268761', 'n_tokens_mistral': 3954, 'n_tokens_neox': 3741, 'n_words': 684}