set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG "${CMAKE_BINARY_DIR}/arch" )
set( CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG "${CMAKE_BINARY_DIR}/lib" )
set( CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG "${CMAKE_BINARY_DIR}/bin" )

set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE "${CMAKE_BINARY_DIR}/arch" )
set( CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE "${CMAKE_BINARY_DIR}/lib" )
set( CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE "${CMAKE_BINARY_DIR}/bin" )

set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/arch" )
set( CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib" )
set( CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin" )

MACRO(SUBDIRLIST result curdir)
  FILE(GLOB children RELATIVE ${curdir} ${curdir}/*)
  SET(dirlist "")
  FOREACH(child ${children})
    IF(IS_DIRECTORY ${curdir}/${child})
      LIST(APPEND dirlist ${child})
    ENDIF()
  ENDFOREACH()
  SET(${result} ${dirlist})
ENDMACRO()

if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE RELEASE)
endif()

set(MAIN_DIR ${CMAKE_SOURCE_DIR})

include_directories(${MAIN_DIR})
include_directories("${MAIN_DIR}/3rdparty/filesystem/include")

if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
    add_definitions(-DCOMPILER_NAME="g++")
endif()

if(MSVC)
    add_definitions(-DCOMPILER_NAME="msvc")
endif()

if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
    add_definitions(-DCOMPILER_NAME="clang")
endif()

if(CMAKE_CXX_COMPILER MATCHES "icpc")
    add_definitions(-DCOMPILER_NAME="icc")
endif()

if(CMAKE_VERSION VERSION_LESS "3.1")
    if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
    endif()
else()
    set(CMAKE_CXX_STANDARD 11)
endif()

if ( UNIX )
    set(CMAKE_C_FLAGS  "${CMAKE_C_FLAGS} -Wall -Werror")
    set(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} -Wall -Werror")
    if(CMAKE_BUILD_TYPE STREQUAL "RELEASE")
        set(CMAKE_CXX_FLAGS_RELEASE "-O3")
    endif()
endif( UNIX )

if ( MSVC )
    set ( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /MDd" )
    if(CMAKE_BUILD_TYPE STREQUAL "RELEASE")
        set(CMAKE_CXX_FLAGS_RELEASE "/O2")
    endif()
endif( MSVC )

if( CMAKE_CXX_COMPILER_ID STREQUAL "Clang" )
    if(NOT APPLE)
        set ( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fopenmp=libiomp5" )
        set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fopenmp=libiomp5" )
    else(NOT APPLE)
        set ( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -lgomp -Wno-error=unused-command-line-argument" )
        set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -lgomp -Wno-error=unused-command-line-argument" )
    endif(NOT APPLE)
else( CMAKE_CXX_COMPILER_ID STREQUAL "Clang" )
    find_package( OpenMP REQUIRED )
    if ( OPENMP_FOUND )
        set ( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}" )
        set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_C_FLAGS}" )
        set ( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}" )
    endif( OPENMP_FOUND )
endif( CMAKE_CXX_COMPILER_ID STREQUAL "Clang" )
