CMakeLists.txt 5.59 KB
Newer Older
1
2
3
4
5
6
7
cmake_minimum_required(VERSION 3.8 FATAL_ERROR)
project(k40gen CXX)

list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake) # for find_package

option(ENABLE_PYTHON "Enable python bindings" TRUE)
option(ENABLE_VC "Enable usage of VC library" TRUE)
8
9
10
11
option(ENABLE_TESTS "Enable tests" TRUE)

include(ExternalProject)
include(FindPackageHandleStandardArgs)
12

13
14
set(EXTERNAL_DIR "external")

15
16
17
if (ENABLE_VC)
find_package(Vc)

18
19
20
if (NOT Vc_FOUND OR "${Vc_VERSION}" VERSION_LESS "1.3.2")

message(STATUS "Using builtin Vc")
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71

set(Vc_VERSION "1.4.1")
set(Vc_PROJECT "Vc-${Vc_VERSION}")
set(Vc_SRC_URI "https://github.com/VcDevel/Vc/archive/${Vc_VERSION}.tar.gz")
set(Vc_DESTDIR "${CMAKE_BINARY_DIR}/${EXTERNAL_DIR}")
set(Vc_LIBNAME "${CMAKE_STATIC_LIBRARY_PREFIX}Vc${CMAKE_STATIC_LIBRARY_SUFFIX}")
set(Vc_LIBRARY "${Vc_DESTDIR}/lib/${Vc_LIBNAME}")


ExternalProject_Add(VC
  URL ${Vc_SRC_URI}
  URL_HASH SHA256=7e8b57ed5ff9eb0835636203898c21302733973ff8eaede5134dd7cb87f915f6
  INSTALL_DIR ${Vc_DESTDIR}
  BUILD_IN_SOURCE 0
  BUILD_BYPRODUCTS ${Vc_LIBRARY}
  LOG_DOWNLOAD 1 LOG_CONFIGURE 1 LOG_BUILD 1 LOG_INSTALL 1
  CMAKE_ARGS -G ${CMAKE_GENERATOR}
             -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
             -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
             -DCMAKE_C_FLAGS=${CMAKE_C_FLAGS}
             -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
             -DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS}
             -DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>
)

set(VC_TARGET Vc)
set(Vc_LIBRARIES Vc)
set(Vc_INCLUDE_DIR "${Vc_DESTDIR}/include")
set(Vc_CMAKE_MODULES_DIR "${Vc_DESTDIR}/lib/cmake/Vc")

find_package_handle_standard_args(Vc
  FOUND_VAR Vc_FOUND
  REQUIRED_VARS Vc_INCLUDE_DIR Vc_LIBRARIES Vc_CMAKE_MODULES_DIR
  VERSION_VAR Vc_VERSION)

endif()
else()
  set(USE_AVX2 FALSE)
endif()

# vectorclass
set(vectorclass_VERSION "1.30")
set(vectorclass_SRC_URI "https://www.agner.org/optimize/vectorclass.zip")
set(vectorclass_DESTDIR "${CMAKE_BINARY_DIR}/include")
set(vectorclass_ROOTDIR "${vectorclass_DESTDIR}/vectorclass")

ExternalProject_Add(vectorclass
  URL ${vectorclass_SRC_URI}
  URL_HASH SHA256=f9cb70a3e865dd019b58f449d11f90147ce8ba5f2c60410389ec0ead92944b97
  SOURCE_DIR vectorclass
  INSTALL_DIR ${vectorclass_ROOTDIR}
72
  LOG_DOWNLOAD 1 LOG_BUILD 1 LOG_INSTALL 1
73
  CONFIGURE_COMMAND ""
74
  BUILD_COMMAND unzip -d <SOURCE_DIR> -o <SOURCE_DIR>/special.zip
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
  INSTALL_COMMAND ${CMAKE_COMMAND} -E copy_directory <SOURCE_DIR> <INSTALL_DIR>
  STEP_TARGETS install
)

# FIXME: This is a workaround to let ROOT find the headers at runtime if
# they are in the build directory. This is necessary until we decide how to
# treat externals with headers used by ROOT
if(NOT EXISTS ${CMAKE_BINARY_DIR}/include/Vc)
  if (NOT EXISTS ${CMAKE_BINARY_DIR}/include)
    execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/include)
  endif()
  execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink
    ${Vc_INCLUDE_DIR}/Vc ${CMAKE_BINARY_DIR}/include/Vc)
endif()
# end of workaround

# install(DIRECTORY ${Vc_DESTDIR}/ DESTINATION ".")

list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")

set(CMAKE_CXX_STANDARD 17)
if (ENABLE_VC)
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${Vc_ARCHITECTURE_FLAGS}")
endif()
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O3 -g -DNDEBUG")
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG")
set(CMAKE_CXX_FLAGS_DEBUG "-O0 -g -DDEBUG")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lpthread")

SET(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} ${INCLUDES} -W -O3 -DNDEBUG")
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} " )

include(VcMacros)
vc_set_preferred_compiler_flags()

if (USE_AVX2)
  include_directories(${Vc_INCLUDE_DIR})
  add_definitions("-DUSE_AVX2")
  string(REPLACE ";" " " VC_FLAGS "${Vc_COMPILE_FLAGS}")
  SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${VC_FLAGS} -mavx2 -mfma -mbmi2")
endif()

set(generate_SOURCES
  src/generate/generate.cpp
  src/generate/storage.cpp
  src/generate/generate_scalar.cpp)

if (USE_AVX2)
  set(generate_SOURCES ${generate_SOURCES}
      src/generate/generate_avx2.cpp
      src/generate/vectorclass_ranvec1.cpp)
endif()

include_directories(${CMAKE_SOURCE_DIR}/lib)
include_directories(${CMAKE_SOURCE_DIR}/lib/generate)

131
132
133
134
135
136
137
138
139
140
add_library(generate SHARED ${generate_SOURCES})

if (USE_AVX2)
target_link_libraries(generate PUBLIC ${Vc_LIBRARIES})
target_include_directories(generate
    PUBLIC ${CMAKE_BINARY_DIR}/include/vectorclass)
add_dependencies(generate vectorclass-install)
target_compile_definitions(generate PUBLIC "-DVc_IMPL=AVX2")
endif()

141
if(ENABLE_PYTHON)
142
143
  include(XTensor)

144
  include(pybind11Tools)
145
146
  pybind11_add_module(k40gen "src/k40gen/bindings.cpp")
  target_link_libraries(k40gen PUBLIC generate)
Roel Aaij's avatar
Roel Aaij committed
147

148
149
150
151
152
153
154
155
156
157
  if (xtp_BUILTIN)
    add_dependencies(k40gen xtp-install)
    target_include_directories(k40gen PRIVATE ${CMAKE_BINARY_DIR}/external/include)
  endif()

  # xtensor external dependencies
  if (USE_AVX2)
    target_compile_definitions(k40gen PRIVATE "-DXTENSOR_ENABLE_XSIMD")
  endif()

Roel Aaij's avatar
Roel Aaij committed
158
159
160
161
162
  # pybind11 headers
  if("${PYBIND11_INCLUDE_DIR}" STREQUAL "")
    find_package(pybind11 QUIET)
    if (NOT pybind11_FOUND)
      message(FAILURE "PYBIND11_INCLUDE_DIR is not set and pybind11 cannot be found")
163
164
    else()
      target_include_directories(k40gen PRIVATE ${PYBIND11_INCLUDE_DIR})
Roel Aaij's avatar
Roel Aaij committed
165
166
167
168
169
170
171
172
173
174
175
    endif()
  else()
  endif()

  # numpy headers
  if("${NUMPY_INCLUDE_DIR}" STREQUAL "")
    message(STATUS "Assuming numpy headers are installed in python site packages location")
    target_include_directories(k40gen PRIVATE ${PYTHON_SITE_PACKAGES}/numpy/core/include)
  else()
    target_include_directories(k40gen PRIVATE ${NUMPY_INCLUDE_DIR})
  endif()
176
177
endif()

178
179
180
if(ENABLE_TESTS)
  enable_testing()
  add_subdirectory(tests)
181
endif()