<commit_before># Copyright 2015-2016. <NAME> <<EMAIL>>
# Copyright 2017-2018. <NAME> <<EMAIL>>
# All rights reserved. Use of this source code is governed by
# a BSD-style license which can be found in the LICENSE file.
# \author <NAME> <<EMAIL>>
# \author <NAME> <<EMAIL>>
cmake_minimum_required(VERSION 2.8.12)

project(bart C)
enable_language(C)

# Silence a CMake-warning about object libraries
if(CMAKE_VERSION VERSION_GREATER 3.0)
  set(_policy_list CMP0028 CMP0051 CMP0054 CMP0057 CMP0074)
  foreach(_policy ${_policy_list})
    if(POLICY ${_policy})
      cmake_policy(SET ${_policy} NEW)
    endif()
  endforeach()
  # CMP0028: :: in target names
  # CMP0051: list TARGET_OBJECTS in SOURCES property
  # CMP0054: no more de-referencing of "expr" in if() statements
  # CMP0057: if IN_LIST
  # CMP0074: XXX_ROOT variables for find_package(XXX)
endif()
if(CMAKE_VERSION VERSION_LESS 3.10) # 3.10 instead of 3.3 due to CUDA support
  if(CMAKE_VERSION VERSION_GREATER 2.8.2)
    message(STATUS "From the BART maintainers: ignore the message below")
  endif()
  cmake_policy(SET CMP0023 OLD) # target_link_libraries mixed signatures
else()
  cmake_policy(SET CMP0023 NEW) # target_link_libraries mixed signatures
endif()

list(INSERT CMAKE_MODULE_PATH 0 ${CMAKE_CURRENT_LIST_DIR}/cmake)

#==============================================================================
# Options

# NB: must be before compiler flags since we might enable CXX or CUDA
include(${CMAKE_CURRENT_LIST_DIR}/cmake/bart_options.cmake)

#==============================================================================

# http://stackoverflow.com/questions/24840030/forcing-c99-in-cmake-to-use-for-loop-initial-declaration
if (CMAKE_VERSION VERSION_LESS 3.1)
  if (CMAKE_C_COMPILER_ID STREQUAL "GNU" OR
      CMAKE_C_COMPILER_ID STREQUAL "Clang")
    set (CMAKE_C_FLAGS "--std=gnu11 ${CMAKE_C_FLAGS}")
  endif()
  if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR
      CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
    set (CMAKE_CXX_FLAGS "--std=c++11 ${CMAKE_CXX_FLAGS}")
  endif()
else()
  set (CMAKE_C_STANDARD 11)
  set (CMAKE_C_STANDARD_REQUIRED ON)
  set (CMAKE_CXX_STANDARD 11)
  set (CMAKE_CXX_STANDARD_REQUIRED ON)
endif()

set(CMAKE_DEBUG_POSTFIX "d")
set(CMAKE_RELWITHDEBINFO_POSTFIX "rd")
set(CMAKE_MINSIZEREL_POSTFIX "rm")

if ("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang"
    OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fblocks")
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fblocks")
endif()

# ==============================================================================

# --- Provide good defaults for searching for packages (i.e. ismrmrd)
if(NOT CMAKE_PREFIX_PATH)
  list(APPEND CMAKE_PREFIX_PATH "/usr/local")
endif()
if(EXISTS $ENV{CMAKE_PREFIX_PATH})
  list(APPEND CMAKE_PREFIX_PATH $ENV{CMAKE_PREFIX_PATH})
endif()
if(EXISTS $ENV{ISMRMRD_HOME})
  list(APPEND CMAKE_PREFIX_PATH $ENV{ISMRMRD_HOME})
endif()
if(CMAKE_PREFIX_PATH)
  list(REMOVE_DUPLICATES CMAKE_PREFIX_PATH)
endif()
# message(STATUS "Looking for packages in : ${CMAKE_PREFIX_PATH}")

# --- Mac specific setup options
if(APPLE)
  # Look for MacPorts only as Homebrew users do not require any particular setup
  find_program(MACPORTS
    NAMES port
    PATHS /opt/local/bin /opt/local/sbin)
  if(MACPORTS)
    add_definitions(-DUSE_MACPORTS)
  endif()
endif()

# ==============================================================================

##- TODO option(BART_SLINK "Provide SLINK support" OFF)
# Set a default build type if none was specified
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" "RelWithDebInfo")
endif()

## Compiler flags -- TODO This could be better, see ITK it won't work on windows builds
if(${CMAKE_BUILD_TYPE} MATCHES "Release")
    set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ffast-math -O3") # -Wall -Wextra -Wmissing-prototypes")
    set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}  -ffast-math -O3") # -Wall -Wextra")
endif()

# ==============================================================================
# Find a BLAS-capable library

include(${CMAKE_CURRENT_LIST_DIR}/cmake/BARTFindBLASlib.cmake)

# Fix weird error when compiling on Mac...
if(APPLE)
  include_directories(${${LINALG_VENDOR_TGT}_INCLUDE_DIRS})
endif()

##======================================================================

execute_process(COMMAND ${CMAKE_CURRENT_LIST_DIR}/git-version.sh OUTPUT_VARIABLE BART_FULL_VERSION_STRING WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR})
string(STRIP ${BART_FULL_VERSION_STRING} BART_FULL_VERSION_STRING) ## Remove trailing whitespace (return characters)
string(REGEX REPLACE ".*v([0-9]*)\\.([0-9]*)\\.([0-9]*)-.*" "\\1" BART_VERSION_MAJOR "${BART_FULL_VERSION_STRING}")
string(REGEX REPLACE ".*v([0-9]*)\\.([0-9]*)\\.([0-9]*)-.*" "\\2" BART_VERSION_MINOR "${BART_FULL_VERSION_STRING}")
string(REGEX REPLACE ".*v([0-9]*)\\.([0-9]*)\\.([0-9]*)-.*" "\\3" BART_VERSION_PATCH "${BART_FULL_VERSION_STRING}")
message(STATUS "BART VERSION: ${BART_FULL_VERSION_STRING}:  ${BART_VERSION_MAJOR} ${BART_VERSION_MINOR} ${BART_VERSION_PATCH}")

file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/version.inc.in "VERSION(\@BART_FULL_VERSION_STRING\@)")
configure_file(${CMAKE_CURRENT_BINARY_DIR}/version.inc.in ${CMAKE_CURRENT_BINARY_DIR}/version.inc @ONLY)

include_directories(${CMAKE_CURRENT_BINARY_DIR})

## ========== Process build_targets.mk
set (ALLPROGS "")
file(READ "${CMAKE_CURRENT_LIST_DIR}/build_targets.mk" bld_tgt_list_of_list)
# Convert file contents into a CMake list (where each element in the list
# is one line of the file)
#
string(REGEX REPLACE ";" "\\\\;" bld_tgt_list_of_list "${bld_tgt_list_of_list}")
string(REGEX REPLACE "\n" ";" bld_tgt_list_of_list "${bld_tgt_list_of_list}")
foreach(bld_tgt_line ${bld_tgt_list_of_list})
   if( "${bld_tgt_line}" MATCHES "^[^#].*=.*")
     string(REGEX REPLACE "^ *([^=]*) *= *(.*) *" "\\1" BLD_KEY "${bld_tgt_line}")
     string(REGEX REPLACE "^ *([^=]*) *= *(.*) *" "\\2" BLD_VALUE "${bld_tgt_line}")
     string(REPLACE " " ";" ${BLD_KEY} "${BLD_VALUE}")  ## Create a new variable called ${BLD_KEY}
     ## message(STATUS "KEY:${BLD_KEY}:    VALUE:${${BLD_KEY}}:")
     list(APPEND ALLPROGS ${${BLD_KEY}})
   endif()
endforeach()


if(BART_ISMRMRD)
  list(APPEND ALLPROGS ismrmrd) <commit_msg>CMake: properly use version variables from CMake<commit_after># Copyright 2015-2016. <NAME> <<EMAIL>>
# Copyright 2017-2018. <NAME> <<EMAIL>>
# All rights reserved. Use of this source code is governed by
# a BSD-style license which can be found in the LICENSE file.
# \author <NAME> <<EMAIL>>
# \author <NAME> <<EMAIL>>
cmake_minimum_required(VERSION 2.8.12)

if(CMAKE_VERSION VERSION_GREATER 3.0)
  set(_policy_list CMP0028 CMP0048 CMP0051 CMP0054 CMP0057 CMP0074)
  foreach(_policy ${_policy_list})
    if(POLICY ${_policy})
      cmake_policy(SET ${_policy} NEW)
    endif()
  endforeach()
  # CMP0028: :: in target names
  # CMP0048: allow VERSION in project()
  # CMP0051: list TARGET_OBJECTS in SOURCES property
  # CMP0054: no more de-referencing of "expr" in if() statements
  # CMP0057: if IN_LIST
  # CMP0074: XXX_ROOT variables for find_package(XXX)
endif()
if(CMAKE_VERSION VERSION_LESS 3.10) # 3.10 instead of 3.3 due to CUDA support
  if(CMAKE_VERSION VERSION_GREATER 3.8.99)
    message(STATUS "From the BART maintainers: ignore the message below")
  endif()
  cmake_policy(SET CMP0023 OLD) # target_link_libraries mixed signatures
else()
  cmake_policy(SET CMP0023 NEW) # target_link_libraries mixed signatures
endif()

# ==============================================================================
# Get the version now, so that we can set it when creating the project

macro(get_version_from_file filename)
  message(STATUS "Getting version info from ${filename}")
  file(READ ${filename} _version_string)
  string(STRIP ${_version_string} _version_string)
  string(SUBSTRING ${_version_string} 1 -1 _version_string)
  string(REGEX MATCHALL "[0-9]+" _version_list ${_version_string})
  list(GET _version_list 0 _version_major)
  list(GET _version_list 1 _version_minor)
  list(GET _version_list 2 _version_patch)
endmacro()

# ------------------------------------------------------------------------------

if(EXISTS ${CMAKE_CURRENT_LIST_DIR}/.git)
  find_package(Git QUIET)
  if(Git_FOUND)
    execute_process(
      COMMAND ${GIT_EXECUTABLE} describe --match "v*" --dirty
      OUTPUT_VARIABLE _full_version
      RESULT_VARIABLE _ret
      WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR})

    if(_ret EQUAL 0)
      string(STRIP ${_full_version} _full_version)
      # _full_version is of the form: v0.4.03-109-gf9c0f56-dirty
      string(REGEX MATCHALL "[0-9]+" _version_list ${_full_version})
      list(GET _version_list 0 _version_major)
      list(GET _version_list 1 _version_minor)
      list(GET _version_list 2 _version_patch)
      file(WRITE ${CMAKE_CURRENT_LIST_DIR}/version.txt
	"v${_version_major}.${_version_minor}.${_version_patch}\n")
    else()
      get_version_from_file(${CMAKE_CURRENT_LIST_DIR}/version.txt)
    endif()
    
  else()
    get_version_from_file(${CMAKE_CURRENT_LIST_DIR}/version.txt)
  endif()
else()
  get_version_from_file(${CMAKE_CURRENT_LIST_DIR}/version.txt)
endif()

# ==============================================================================
# Create the BART project

if(CMAKE_VERSION VERSION_LESS 3.0)
  project(BART C)
  set(PROJECT_VERSION_MAJOR ${_version_major})
  set(PROJECT_VERSION_MINOR ${_version_minor})
  set(PROJECT_VERSION_PATCH ${_version_patch})
  set(${CMAKE_PROJECT_NAME}_VERSION_MAJOR ${_version_major})
  set(${CMAKE_PROJECT_NAME}_VERSION_MINOR ${_version_minor})
  set(${CMAKE_PROJECT_NAME}_VERSION_PATCH ${_version_patch})
else()
  project(BART
    LANGUAGES C
    VERSION "${_version_major}.${_version_minor}.${_version_patch}")
endif()

list(INSERT CMAKE_MODULE_PATH 0 ${CMAKE_CURRENT_LIST_DIR}/cmake)

#==============================================================================
# Options

# NB: must be before compiler flags since we might enable CXX or CUDA
include(${CMAKE_CURRENT_LIST_DIR}/cmake/bart_options.cmake)

#==============================================================================

# http://stackoverflow.com/questions/24840030/forcing-c99-in-cmake-to-use-for-loop-initial-declaration
if (CMAKE_VERSION VERSION_LESS 3.1)
  if (CMAKE_C_COMPILER_ID STREQUAL "GNU" OR
      CMAKE_C_COMPILER_ID STREQUAL "Clang")
    set (CMAKE_C_FLAGS "--std=gnu11 ${CMAKE_C_FLAGS}")
  endif()
  if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR
      CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
    set (CMAKE_CXX_FLAGS "--std=c++11 ${CMAKE_CXX_FLAGS}")
  endif()
else()
  set (CMAKE_C_STANDARD 11)
  set (CMAKE_C_STANDARD_REQUIRED ON)
  set (CMAKE_CXX_STANDARD 11)
  set (CMAKE_CXX_STANDARD_REQUIRED ON)
endif()

set(CMAKE_DEBUG_POSTFIX "d")
set(CMAKE_RELWITHDEBINFO_POSTFIX "rd")
set(CMAKE_MINSIZEREL_POSTFIX "rm")

if ("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang"
    OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fblocks")
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fblocks")
endif()

# ==============================================================================

# --- Provide good defaults for searching for packages (i.e. ismrmrd)
if(NOT CMAKE_PREFIX_PATH)
  list(APPEND CMAKE_PREFIX_PATH "/usr/local")
endif()
if(EXISTS $ENV{CMAKE_PREFIX_PATH})
  list(APPEND CMAKE_PREFIX_PATH $ENV{CMAKE_PREFIX_PATH})
endif()
if(EXISTS $ENV{ISMRMRD_HOME})
  list(APPEND CMAKE_PREFIX_PATH $ENV{ISMRMRD_HOME})
endif()
if(CMAKE_PREFIX_PATH)
  list(REMOVE_DUPLICATES CMAKE_PREFIX_PATH)
endif()
# message(STATUS "Looking for packages in : ${CMAKE_PREFIX_PATH}")

# --- Mac specific setup options
if(APPLE)
  # Look for MacPorts only as Homebrew users do not require any particular setup
  find_program(MACPORTS
    NAMES port
    PATHS /opt/local/bin /opt/local/sbin)
  if(MACPORTS)
    add_definitions(-DUSE_MACPORTS)
  endif()
endif()

# ==============================================================================

##- TODO option(BART_SLINK "Provide SLINK support" OFF)
# Set a default build type if none was specified
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" "RelWithDebInfo")
endif()

## Compiler flags -- TODO This could be better, see ITK it won't work on windows builds
if(${CMAKE_BUILD_TYPE} MATCHES "Release")
    set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ffast-math -O3") # -Wall -Wextra -Wmissing-prototypes")
    set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}  -ffast-math -O3") # -Wall -Wextra")
endif()

# ==============================================================================
# Find a BLAS-capable library

include(${CMAKE_CURRENT_LIST_DIR}/cmake/BARTFindBLASlib.cmake)

# Fix weird error when compiling on Mac...
if(APPLE)
  include_directories(${${LINALG_VENDOR_TGT}_INCLUDE_DIRS})
endif()

##======================================================================

file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/version.inc.in "VERSION(\@BART_FULL_VERSION_STRING\@)")
configure_file(${CMAKE_CURRENT_BINARY_DIR}/version.inc.in ${CMAKE_CURRENT_BINARY_DIR}/version.inc @ONLY)

include_directories(${CMAKE_CURRENT_BINARY_DIR})

## ========== Process build_targets.mk
set (ALLPROGS "")
file(READ "${CMAKE_CURRENT_LIST_DIR}/build_targets.mk" bld_tgt_list_of_list)
# Convert file contents into a CMake list (where each element in the list
# is one line of the file)
#
string(REGEX REPLACE ";" "\\\\;" bld_tgt_list_of_list "${bld_tgt_list_of_list}")
string(REGEX REPLACE "\n" ";" bld_tgt_list_of_list "${bld_tgt_list_of_list}")
foreach(bld_tgt_line ${bld_tgt_list_of_list})
   if( "${bld_tgt_line}" MATCHES "^[^#].*=.*")
     string(REGEX REPLACE "^ *([^=]*) *= *(.*) *" "\\1" BLD_KEY "${bld_tgt_line}")
     string(REGEX REPLACE "^ *([^=]*) *= *(.*) *" "\\2" BLD_VALUE "${bld_tgt_line}")
     string(REPLACE " " ";" ${BLD_KEY} "${BLD_VALUE}")  ## Create a new variable called ${BLD_KEY}
     ## message(STATUS "KEY:${BLD_KEY}:    VALUE:${${BLD_KEY}}:")
     list(APPEND ALLPROGS ${${BLD_KEY}})
   endif()
endforeach()


if(BART_ISMRMRD)
  list(APPEND ALLPROGS ismrmrd) 
{'fraction_non_alphanumeric': 0.19920727123624685, 'fraction_numerical': 0.018314767990159228, 'mean_word_length': 5.709766162310866, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, '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': '3570726', 'n_tokens_mistral': 5579, 'n_tokens_neox': 5102, 'n_words': 1112}