<commit_before>cmake_minimum_required(VERSION 3.1)
project(chainerx CXX C)

# CMake setup
if(POLICY ${CMP0054})
    cmake_policy(SET ${CMP0054} NEW)
endif()

SET(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH})

# includes
include(cmake/clang-tidy.cmake)
include(cmake/third-party.cmake)

# Configure options
option(CHAINERX_BUILD_PYTHON "Build Python binding" OFF)
option(CHAINERX_BUILD_TEST "Build test" ON)
option(CHAINERX_WARNINGS_AS_ERRORS "Make all warnings of compilers into errors" ON)
option(CHAINERX_ENABLE_THREAD_SANITIZER "Enable thread sanitizer." OFF)

if(DEFINED ENV{CHAINERX_ENABLE_COVERAGE})
    set(DEFAULT_CHAINERX_ENABLE_COVERAGE $ENV{CHAINERX_ENABLE_COVERAGE})
else()
    set(DEFAULT_CHAINERX_ENABLE_COVERAGE OFF)
endif()
option(CHAINERX_ENABLE_COVERAGE "Enable test coverage with gcov" ${DEFAULT_CHAINERX_ENABLE_COVERAGE})

if(DEFINED ENV{CHAINERX_BUILD_CUDA})
    set(DEFAULT_CHAINERX_BUILD_CUDA $ENV{CHAINERX_BUILD_CUDA})
else()
    set(DEFAULT_CHAINERX_BUILD_CUDA ON)
endif()
option(CHAINERX_BUILD_CUDA "Build CUDA backend (if CUDA is available)" ${DEFAULT_CHAINERX_BUILD_CUDA})

# Allow to specify *one* --generate-code option of the nvcc command.
# Supposed usage is to avoid slowness of PTX JIT compilation on development.
set(CHAINERX_NVCC_GENERATE_CODE "$ENV{CHAINERX_NVCC_GENERATE_CODE}" CACHE STRING "nvcc --generate-code option")

if(DEFINED ENV{CHAINERX_ENABLE_BLAS})
    set(DEFAULT_CHAINERX_ENABLE_BLAS $ENV{CHAINERX_ENABLE_BLAS})
else()
    set(DEFAULT_CHAINERX_ENABLE_BLAS ON)
endif()
option(CHAINERX_ENABLE_BLAS "Use BLAS if available" ${DEFAULT_CHAINERX_ENABLE_BLAS})

# defaults to cmake -DCMAKE_BUILD_TYPE=Debug
if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE Debug)
endif()

# CUDA
if(${CHAINERX_BUILD_CUDA})
    find_package(CUDA QUIET)
    if(${CUDA_FOUND})
        add_definitions(-DCHAINERX_ENABLE_CUDA=1)
        find_package(CuDNN 7 REQUIRED)
        include_directories(${CUDNN_INCLUDE_DIRS})
        if(NOT CHAINERX_NVCC_GENERATE_CODE STREQUAL "")
            list(APPEND CUDA_NVCC_FLAGS --generate-code=${CHAINERX_NVCC_GENERATE_CODE})
        elseif(CMAKE_BUILD_TYPE MATCHES Release)
            list(APPEND CUDA_NVCC_FLAGS --generate-code=arch=compute_30,code=sm_30)
            list(APPEND CUDA_NVCC_FLAGS --generate-code=arch=compute_50,code=sm_50)
            list(APPEND CUDA_NVCC_FLAGS --generate-code=arch=compute_60,code=sm_60)
            list(APPEND CUDA_NVCC_FLAGS --generate-code=arch=compute_70,code=sm_70)
        endif()
    endif()
endif()

# BLAS
if(${CHAINERX_ENABLE_BLAS})
    find_package(BLAS QUIET)
    if (${BLAS_FOUND})
        message(STATUS "Found BLAS (library: ${BLAS_LIBRARIES})")
        add_definitions(-DCHAINERX_ENABLE_BLAS=1)
    else()
        message(STATUS "BLAS not found")
    endif()
endif()

# C++ setup
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_OUTPUT_EXTENSION_REPLACE 1)  # ref. https://texus.me/2015/09/06/cmake-and-gcov/

if("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU|Clang|Intel")
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pthread")
    set(CMAKE_CXX_FLAGS "-g -Wall -Wextra -fPIC -pthread")
    set(CMAKE_CXX_FLAGS_RELEASE "-O2 -DNDEBUG")  # cmake -DCMAKE_BUILD_TYPE=Release
    set(CMAKE_CXX_FLAGS_DEBUG "-O0")  # cmake -DCMAKE_BUILD_TYPE=Debug
    if (${CUDA_FOUND})
        list(APPEND CUDA_NVCC_FLAGS -std=c++14)
    endif()

    # cmake -DCHAINERX_ENABLE_COVERAGE=ON|OFF
    if(CHAINERX_ENABLE_COVERAGE)
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --coverage")
        set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --coverage")
    endif()

    # cmake -DCHAINERX_WARNINGS_AS_ERRORS=ON|OFF
    if (CHAINERX_WARNINGS_AS_ERRORS)
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror")
    endif()

    # cmake -DCHAINERX_ENABLE_THREAD_SANITIZER=ON|OFF
    if(CHAINERX_ENABLE_THREAD_SANITIZER)
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread")
    endif()
endif()
include_directories("${PROJECT_SOURCE_DIR}")

# dl libs
if(DEFINED CMAKE_DL_LIBS)
else()
    message(FATAL_ERROR "libdl not found")
endif()

# pybind11
if(${CHAINERX_BUILD_PYTHON})
    get_third_party(pybind11)
    include_directories(${CMAKE_BINARY_DIR}/pybind11/include)
    add_subdirectory(${CMAKE_BINARY_DIR}/pybind11 ${CMAKE_BINARY_DIR}/pybind11-build)
endif()

# gsl-lite
# gsl-lite is a header-only library, we do not need to build and run tests
get_third_party(gsl-lite)
include_directories(${CMAKE_BINARY_DIR}/gsl-lite/include)
# ref. https://github.com/martinmoene/gsl-lite#api-macro
# As default, functions (methods) are decorated with __host__ __device__ for the CUDA platform. 
# We want to stop it because we get many warnings with nvcc.
add_definitions(-Dgsl_api=)
if(${CUDA_FOUND})
    list(APPEND CUDA_NVCC_FLAGS "-Dgsl_api=")
endif()

# optional-lite
get_third_party(optional-lite)
include_directories(${CMAKE_BINARY_DIR}/optional-lite/include)

# Test
# TODO(niboshi): Remove gtest dependency from testing
get_third_party(gtest)
add_subdirectory(${CMAKE_BINARY_DIR}/googletest-src
    ${CMAKE_BINARY_DIR}/googletest-build
    EXCLUDE_FROM_ALL)
include_directories(${CMAKE_BINARY_DIR}/googletest-src/googletest/include)
if(${CHAINERX_BUILD_TEST})
    enable_testing()
endif()

add_subdirectory(chainerx)<commit_msg>Fix CMake build failure for CUDA 10 with Ubuntu 18<commit_after>cmake_minimum_required(VERSION 3.1)
project(chainerx CXX C)

# CMake setup
if(POLICY ${CMP0054})
    cmake_policy(SET ${CMP0054} NEW)
endif()

SET(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH})

# includes
include(cmake/clang-tidy.cmake)
include(cmake/third-party.cmake)

# Configure options
option(CHAINERX_BUILD_PYTHON "Build Python binding" OFF)
option(CHAINERX_BUILD_TEST "Build test" ON)
option(CHAINERX_WARNINGS_AS_ERRORS "Make all warnings of compilers into errors" ON)
option(CHAINERX_ENABLE_THREAD_SANITIZER "Enable thread sanitizer." OFF)

if(DEFINED ENV{CHAINERX_ENABLE_COVERAGE})
    set(DEFAULT_CHAINERX_ENABLE_COVERAGE $ENV{CHAINERX_ENABLE_COVERAGE})
else()
    set(DEFAULT_CHAINERX_ENABLE_COVERAGE OFF)
endif()
option(CHAINERX_ENABLE_COVERAGE "Enable test coverage with gcov" ${DEFAULT_CHAINERX_ENABLE_COVERAGE})

if(DEFINED ENV{CHAINERX_BUILD_CUDA})
    set(DEFAULT_CHAINERX_BUILD_CUDA $ENV{CHAINERX_BUILD_CUDA})
else()
    set(DEFAULT_CHAINERX_BUILD_CUDA ON)
endif()
option(CHAINERX_BUILD_CUDA "Build CUDA backend (if CUDA is available)" ${DEFAULT_CHAINERX_BUILD_CUDA})

# Allow to specify *one* --generate-code option of the nvcc command.
# Supposed usage is to avoid slowness of PTX JIT compilation on development.
set(CHAINERX_NVCC_GENERATE_CODE "$ENV{CHAINERX_NVCC_GENERATE_CODE}" CACHE STRING "nvcc --generate-code option")

if(DEFINED ENV{CHAINERX_ENABLE_BLAS})
    set(DEFAULT_CHAINERX_ENABLE_BLAS $ENV{CHAINERX_ENABLE_BLAS})
else()
    set(DEFAULT_CHAINERX_ENABLE_BLAS ON)
endif()
option(CHAINERX_ENABLE_BLAS "Use BLAS if available" ${DEFAULT_CHAINERX_ENABLE_BLAS})

# defaults to cmake -DCMAKE_BUILD_TYPE=Debug
if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE Debug)
endif()

# CUDA
if(${CHAINERX_BUILD_CUDA})
    find_package(CUDA QUIET)
    if(${CUDA_FOUND})
        # CUDA_cublas_device_LIBRARY needs to explicitly set for cmake 3.10.2.
        set(CUDA_cublas_device_LIBRARY ${CUDA_LIBRARIES})
        add_definitions(-DCHAINERX_ENABLE_CUDA=1)
        find_package(CuDNN 7 REQUIRED)
        include_directories(${CUDNN_INCLUDE_DIRS})
        if(NOT CHAINERX_NVCC_GENERATE_CODE STREQUAL "")
            list(APPEND CUDA_NVCC_FLAGS --generate-code=${CHAINERX_NVCC_GENERATE_CODE})
        elseif(CMAKE_BUILD_TYPE MATCHES Release)
            list(APPEND CUDA_NVCC_FLAGS --generate-code=arch=compute_30,code=sm_30)
            list(APPEND CUDA_NVCC_FLAGS --generate-code=arch=compute_50,code=sm_50)
            list(APPEND CUDA_NVCC_FLAGS --generate-code=arch=compute_60,code=sm_60)
            list(APPEND CUDA_NVCC_FLAGS --generate-code=arch=compute_70,code=sm_70)
        endif()
    endif()
endif()

# BLAS
if(${CHAINERX_ENABLE_BLAS})
    find_package(BLAS QUIET)
    if (${BLAS_FOUND})
        message(STATUS "Found BLAS (library: ${BLAS_LIBRARIES})")
        add_definitions(-DCHAINERX_ENABLE_BLAS=1)
    else()
        message(STATUS "BLAS not found")
    endif()
endif()

# C++ setup
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_OUTPUT_EXTENSION_REPLACE 1)  # ref. https://texus.me/2015/09/06/cmake-and-gcov/

if("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU|Clang|Intel")
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pthread")
    set(CMAKE_CXX_FLAGS "-g -Wall -Wextra -fPIC -pthread")
    set(CMAKE_CXX_FLAGS_RELEASE "-O2 -DNDEBUG")  # cmake -DCMAKE_BUILD_TYPE=Release
    set(CMAKE_CXX_FLAGS_DEBUG "-O0")  # cmake -DCMAKE_BUILD_TYPE=Debug
    if (${CUDA_FOUND})
        list(APPEND CUDA_NVCC_FLAGS -std=c++14)
    endif()

    # cmake -DCHAINERX_ENABLE_COVERAGE=ON|OFF
    if(CHAINERX_ENABLE_COVERAGE)
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --coverage")
        set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --coverage")
    endif()

    # cmake -DCHAINERX_WARNINGS_AS_ERRORS=ON|OFF
    if (CHAINERX_WARNINGS_AS_ERRORS)
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror")
    endif()

    # cmake -DCHAINERX_ENABLE_THREAD_SANITIZER=ON|OFF
    if(CHAINERX_ENABLE_THREAD_SANITIZER)
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread")
    endif()
endif()
include_directories("${PROJECT_SOURCE_DIR}")

# dl libs
if(DEFINED CMAKE_DL_LIBS)
else()
    message(FATAL_ERROR "libdl not found")
endif()

# pybind11
if(${CHAINERX_BUILD_PYTHON})
    get_third_party(pybind11)
    include_directories(${CMAKE_BINARY_DIR}/pybind11/include)
    add_subdirectory(${CMAKE_BINARY_DIR}/pybind11 ${CMAKE_BINARY_DIR}/pybind11-build)
endif()

# gsl-lite
# gsl-lite is a header-only library, we do not need to build and run tests
get_third_party(gsl-lite)
include_directories(${CMAKE_BINARY_DIR}/gsl-lite/include)
# ref. https://github.com/martinmoene/gsl-lite#api-macro
# As default, functions (methods) are decorated with __host__ __device__ for the CUDA platform.
# We want to stop it because we get many warnings with nvcc.
add_definitions(-Dgsl_api=)
if(${CUDA_FOUND})
    list(APPEND CUDA_NVCC_FLAGS "-Dgsl_api=")
endif()

# optional-lite
get_third_party(optional-lite)
include_directories(${CMAKE_BINARY_DIR}/optional-lite/include)

# Test
# TODO(niboshi): Remove gtest dependency from testing
get_third_party(gtest)
add_subdirectory(${CMAKE_BINARY_DIR}/googletest-src
    ${CMAKE_BINARY_DIR}/googletest-build
    EXCLUDE_FROM_ALL)
include_directories(${CMAKE_BINARY_DIR}/googletest-src/googletest/include)
if(${CHAINERX_BUILD_TEST})
    enable_testing()
endif()

add_subdirectory(chainerx)
{'fraction_non_alphanumeric': 0.10609312307548754, 'fraction_numerical': 0.01082392460576654, 'mean_word_length': 5.476132930513595, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, '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': '7217540', 'n_tokens_mistral': 4391, 'n_tokens_neox': 4127, 'n_words': 676}