<commit_before>cmake_minimum_required(VERSION 2.8.11)

if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_BINARY_DIR})
  message(FATAL_ERROR "In-source builds not allowed. Please make a new directory (called a build directory) and run CMake from there.\nBut first clean the build files that just got created in the source directory:\nrm -rf CMakeFiles CMakeCache.txt")
endif()

project(cgt)

# http://cmake.3232098.n2.nabble.com/Default-value-for-CMAKE-BUILD-TYPE-td7550756.html
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
  message(STATUS "Setting build type to 'Release' as none was specified.")
  set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build." FORCE)
  # Set the possible values of build type for cmake-gui
  set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release"
    "MinSizeRel" "RelWithDebInfo")
endif()

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib
    CACHE PATH "Output directory for static libraries.")

set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib
    CACHE PATH "Output directory for shared libraries.")

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
    CACHE PATH "Output directory for executables and DLL's.")

# set(CMAKE_C_STANDARD 99)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99")
if(APPLE)
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -stdlib=libc++")
else()
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
endif()

list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/4build/cmake)

include(${CMAKE_SOURCE_DIR}/4build/cmake/UseCython.cmake)
include(${CMAKE_SOURCE_DIR}/4build/cmake/CUDA.cmake)

find_package(PythonLibs 2.7 REQUIRED)
find_package(Numpy REQUIRED)

if (APPLE)
  set(CMAKE_MACOSX_RPATH 0)
endif()


option(CGT_ENABLE_CUDA OFF "Enable CUDA support")
option(CGT_ENABLE_CUDNN ON "Enable CUDNN support")
option(CGT_DEVEL_MODE OFF "Build with more compiler errors and warnings")

message(
    "\n"
    "*****************************\n"
    "OPTIONS: \n"
    "   CGT_ENABLE_CUDA:    ${CGT_ENABLE_CUDA}\n"
    "   CGT_ENABLE_CUDNN:    ${CGT_ENABLE_CUDNN}\n"
    # "   CGT_DEVEL_MODE:     ${CGT_DEVEL_MODE}\n"
    "   CMAKE_BUILD_TYPE:   ${CMAKE_BUILD_TYPE}\n"
    "*****************************\n"
    )


if (CGT_DEVEL_MODE)
  set(CMAKE_C_FLAGS "-Wall -Wextra -Wno-unused-function -Wno-sign-compare")
endif(CGT_DEVEL_MODE)

set(CGT_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/include)
message("include dir ${CGT_INCLUDE_DIR}")

add_library(cgt SHARED src/cgt_common.cpp include/cgt_common.h)
if (CGT_ENABLE_CUDA)
  find_package(CUDA REQUIRED)
  add_definitions(-DCGT_ENABLE_CUDA)  
endif()

if (CGT_ENABLE_CUDNN)
    detect_cuDNN()
    if(NOT CUDNN_FOUND AND NOT CUDNN_ROOT)
      message(FATAL_ERROR "Couldn't find CUDNN. Set CUDNN_ROOT")
    endif()
endif()

target_link_libraries(cgt ${CUDA_CUDART_LIBRARY})
target_include_directories(cgt PRIVATE ${CUDA_INCLUDE_DIRS} ${CGT_INCLUDE_DIR})


cython_add_module(cycgt ${CMAKE_SOURCE_DIR}/src/cycgt.pyx 
  ${CMAKE_SOURCE_DIR}/include/execution.h
  ${CMAKE_SOURCE_DIR}/src/execution.cpp
)
set_source_files_properties(${CMAKE_SOURCE_DIR}/src/cycgt.pyx PROPERTIES CYTHON_IS_CXX TRUE)
target_include_directories(cycgt PRIVATE ${NUMPY_INCLUDE_DIR} ${CGT_INCLUDE_DIR})
target_link_libraries(cycgt cgt)



# add_custom_command(
#   COMMAND ./do_autogen.py ${CMAKE_BINARY_DIR}
#   OUTPUT ${CMAKE_BINARY_DIR}/cgtcorefuns.c ${CMAKE_BINARY_DIR}/cgtcorefunscuda.cu
#   WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
#   # DEPENDS cgt.py
# )
# add_custom_target(gencfiles ALL DEPENDS cgtcorefuns.c)

set(OPENBLAS_LIBRARY ${CMAKE_BINARY_DIR}/OpenBLAS/libopenblas.a)

add_custom_command(
  COMMAND 4build/download_and_build_openblas.py ${CMAKE_BINARY_DIR}/OpenBLAS
  OUTPUT ${OPENBLAS_LIBRARY}
  WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
)
add_custom_target(openblas ALL DEPENDS ${OPENBLAS_LIBRARY})

# add_library(cgtcorefuns SHARED cgtcorefuns.c)
# add_dependencies(cgtcorefuns openblas) # WHY IS THIS NECESSARY?
# target_link_libraries(cgtcorefuns ${OPENBLAS_LIBRARY})
# target_include_directories(cgtcorefuns PRIVATE ${CMAKE_BINARY_DIR}/OpenBLAS ${CGT_INCLUDE_DIR})
# set_target_properties(cgtcorefuns PROPERTIES SUFFIX .so)

# if (CGT_ENABLE_CUDA)
#   # http://cuda-insight-toolkit.googlecode.com/svn-history/r46/trunk/Examples/Architecture1/src/CMakeLists.txt
#   find_package(CUDA REQUIRED)
#   cuda_include_directories(${CUDA_INCLUDE_DIRS} ${CGT_INCLUDE_DIR})
#   cuda_add_library(cgtcorefunscuda SHARED ${CMAKE_BINARY_DIR}/cgtcorefunscuda.cu)
#   target_link_libraries(cgtcorefunscuda ${CUDA_LIBRARIES})
#   set_target_properties(cgtcorefunscuda PROPERTIES SUFFIX .so)
# endif()

# cuda_add_library(cudadummy SHARED dummy.cu)

message("link flags ${CMAKE_SHARED_LINKER_FLAGS}")

get_property(CGT_LIBRARY_PATH TARGET cgt PROPERTY LOCATION)<commit_msg>try to find system OpenBLAS before downloading and compiling it<commit_after>cmake_minimum_required(VERSION 2.8.11)

if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_BINARY_DIR})
  message(FATAL_ERROR "In-source builds not allowed. Please make a new directory (called a build directory) and run CMake from there.\nBut first clean the build files that just got created in the source directory:\nrm -rf CMakeFiles CMakeCache.txt")
endif()

project(cgt)

# http://cmake.3232098.n2.nabble.com/Default-value-for-CMAKE-BUILD-TYPE-td7550756.html
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
  message(STATUS "Setting build type to 'Release' as none was specified.")
  set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build." FORCE)
  # Set the possible values of build type for cmake-gui
  set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release"
    "MinSizeRel" "RelWithDebInfo")
endif()

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib
    CACHE PATH "Output directory for static libraries.")

set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib
    CACHE PATH "Output directory for shared libraries.")

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
    CACHE PATH "Output directory for executables and DLL's.")

# set(CMAKE_C_STANDARD 99)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99")
if(APPLE)
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -stdlib=libc++")
else()
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
endif()

list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/4build/cmake)

include(${CMAKE_SOURCE_DIR}/4build/cmake/UseCython.cmake)
include(${CMAKE_SOURCE_DIR}/4build/cmake/CUDA.cmake)

find_package(PythonLibs 2.7 REQUIRED)
find_package(Numpy REQUIRED)
find_package(OpenBLAS REQUIRED)

if (APPLE)
  set(CMAKE_MACOSX_RPATH 0)
endif()


option(CGT_ENABLE_CUDA OFF "Enable CUDA support")
option(CGT_ENABLE_CUDNN ON "Enable CUDNN support")
option(CGT_DEVEL_MODE OFF "Build with more compiler errors and warnings")

message(
    "\n"
    "*****************************\n"
    "OPTIONS: \n"
    "   CGT_ENABLE_CUDA:    ${CGT_ENABLE_CUDA}\n"
    "   CGT_ENABLE_CUDNN:    ${CGT_ENABLE_CUDNN}\n"
    # "   CGT_DEVEL_MODE:     ${CGT_DEVEL_MODE}\n"
    "   CMAKE_BUILD_TYPE:   ${CMAKE_BUILD_TYPE}\n"
    "*****************************\n"
    )


if (CGT_DEVEL_MODE)
  set(CMAKE_C_FLAGS "-Wall -Wextra -Wno-unused-function -Wno-sign-compare")
endif(CGT_DEVEL_MODE)

set(CGT_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/include)
message("include dir ${CGT_INCLUDE_DIR}")

add_library(cgt SHARED src/cgt_common.cpp include/cgt_common.h)
if (CGT_ENABLE_CUDA)
  find_package(CUDA REQUIRED)
  add_definitions(-DCGT_ENABLE_CUDA)  
endif()

if (CGT_ENABLE_CUDNN)
    detect_cuDNN()
    if(NOT CUDNN_FOUND AND NOT CUDNN_ROOT)
      message(FATAL_ERROR "Couldn't find CUDNN. Set CUDNN_ROOT")
    endif()
endif()

target_link_libraries(cgt ${CUDA_CUDART_LIBRARY})
target_include_directories(cgt PRIVATE ${CUDA_INCLUDE_DIRS} ${CGT_INCLUDE_DIR})


cython_add_module(cycgt ${CMAKE_SOURCE_DIR}/src/cycgt.pyx 
  ${CMAKE_SOURCE_DIR}/include/execution.h
  ${CMAKE_SOURCE_DIR}/src/execution.cpp
)
set_source_files_properties(${CMAKE_SOURCE_DIR}/src/cycgt.pyx PROPERTIES CYTHON_IS_CXX TRUE)
target_include_directories(cycgt PRIVATE ${NUMPY_INCLUDE_DIR} ${CGT_INCLUDE_DIR})
target_link_libraries(cycgt cgt)



# add_custom_command(
#   COMMAND ./do_autogen.py ${CMAKE_BINARY_DIR}
#   OUTPUT ${CMAKE_BINARY_DIR}/cgtcorefuns.c ${CMAKE_BINARY_DIR}/cgtcorefunscuda.cu
#   WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
#   # DEPENDS cgt.py
# )
# add_custom_target(gencfiles ALL DEPENDS cgtcorefuns.c)

if(NOT OpenBLAS_FOUND)
  set(OPENBLAS_LIBRARY ${CMAKE_BINARY_DIR}/OpenBLAS/libopenblas.a)

  add_custom_command(
    COMMAND 4build/download_and_build_openblas.py ${CMAKE_BINARY_DIR}/OpenBLAS
    OUTPUT ${OPENBLAS_LIBRARY}
    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
  )

  add_custom_target(openblas ALL DEPENDS ${OPENBLAS_LIBRARY})
endif()

# add_library(cgtcorefuns SHARED cgtcorefuns.c)
# add_dependencies(cgtcorefuns openblas) # WHY IS THIS NECESSARY?
# target_link_libraries(cgtcorefuns ${OPENBLAS_LIBRARY})
# target_include_directories(cgtcorefuns PRIVATE ${CMAKE_BINARY_DIR}/OpenBLAS ${CGT_INCLUDE_DIR})
# set_target_properties(cgtcorefuns PROPERTIES SUFFIX .so)

# if (CGT_ENABLE_CUDA)
#   # http://cuda-insight-toolkit.googlecode.com/svn-history/r46/trunk/Examples/Architecture1/src/CMakeLists.txt
#   find_package(CUDA REQUIRED)
#   cuda_include_directories(${CUDA_INCLUDE_DIRS} ${CGT_INCLUDE_DIR})
#   cuda_add_library(cgtcorefunscuda SHARED ${CMAKE_BINARY_DIR}/cgtcorefunscuda.cu)
#   target_link_libraries(cgtcorefunscuda ${CUDA_LIBRARIES})
#   set_target_properties(cgtcorefunscuda PROPERTIES SUFFIX .so)
# endif()

# cuda_add_library(cudadummy SHARED dummy.cu)

message("link flags ${CMAKE_SHARED_LINKER_FLAGS}")

get_property(CGT_LIBRARY_PATH TARGET cgt PROPERTY LOCATION)
{'fraction_non_alphanumeric': 0.11700596339708, 'fraction_numerical': 0.0076084721365412295, 'mean_word_length': 6.391337386018237, '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': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6560540', 'n_tokens_mistral': 4008, 'n_tokens_neox': 3743, 'n_words': 637}