forked from dendibakh/perf-ninja
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlabs.cmake
156 lines (133 loc) · 4.96 KB
/
labs.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
72
73
74
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# https://cmake.org/documentation/
# Check usage of 'build' subdirectory
if("${CMAKE_SOURCE_DIR}" STREQUAL "${CMAKE_BINARY_DIR}")
message("CMAKE_BINARY_DIR=${CMAKE_BINARY_DIR}")
message("Please always use the 'build' subdirectory:")
if(MSVC)
message(FATAL_ERROR "git clean -dfx & cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -T ClangCL")
else()
message(FATAL_ERROR "git clean -dfx & mkdir build & cd build & cmake -DCMAKE_BUILD_TYPE=Release ..")
endif()
endif()
# Just use the variable
if("${CMAKE_BUILD_TYPE}" STREQUAL "")
message("Please consider: cmake -DCMAKE_BUILD_TYPE=Release ..")
endif()
if(NOT DEFINED CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 17)
endif()
# Set compiler options
if(NOT MSVC)
set(CMAKE_C_FLAGS "-O3 -ffast-math -march=native ${CMAKE_C_FLAGS}")
else()
include("${CMAKE_CURRENT_LIST_DIR}/msvc_simd_isa.cmake")
if(SUPPORT_MSVC_AVX512)
set(MSVC_SIMD_FLAGS "/arch:AVX512")
elseif(SUPPORT_MSVC_AVX2)
set(MSVC_SIMD_FLAGS "/arch:AVX2")
elseif(SUPPORT_MSVC_AVX)
set(MSVC_SIMD_FLAGS "/arch:AVX")
else()
set(MSVC_SIMD_FLAGS "")
endif()
set(CMAKE_C_FLAGS "/O2 /fp:fast ${MSVC_SIMD_FLAGS} ${CMAKE_C_FLAGS}")
endif()
# Set Windows stack size as on Linux: 2MB on 32-bit, 8MB on 64-bit
if (WIN32)
math(EXPR stack_size "${CMAKE_SIZEOF_VOID_P}*${CMAKE_SIZEOF_VOID_P}*128*1024")
if (MSVC)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /STACK:${stack_size}")
else()
# compiling with clang + lld
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Xlinker /stack:${stack_size}")
endif()
endif()
set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} ${CMAKE_CXX_FLAGS}")
# https://github.com/google/benchmark
find_package(benchmark PATHS "${CMAKE_CURRENT_LIST_DIR}/benchmark/build" REQUIRED)
set(BENCHMARK_LIBRARY "benchmark::benchmark")
# Find source files
file(GLOB srcs *.c *.h *.cpp *.hpp *.cxx *.hxx *.inl)
list(FILTER srcs EXCLUDE REGEX ".*bench.cpp$")
list(FILTER srcs EXCLUDE REGEX ".*validate.cpp$")
# Add main targets
add_executable(lab bench.cpp ${srcs} ${EXT_LAB_srcs})
add_executable(validate validate.cpp ${srcs} ${EXT_VALIDATE_srcs})
# Add path to a local benchmark library
if(EXISTS "${BENCHMARK_FOLDER}/include")
target_include_directories(lab BEFORE PRIVATE "${BENCHMARK_FOLDER}/include")
target_include_directories(validate BEFORE PRIVATE "${BENCHMARK_FOLDER}/include")
endif()
if(EXISTS "${BENCHMARK_FOLDER}/build/include")
target_include_directories(lab BEFORE PRIVATE "${BENCHMARK_FOLDER}/build/include")
target_include_directories(validate BEFORE PRIVATE "${BENCHMARK_FOLDER}/build/include")
endif()
# Check optional arguments
if(NOT DEFINED CI)
set(CI OFF)
endif()
if(NOT DEFINED VALIDATE_ARGS)
set(VALIDATE_ARGS "")
endif()
if(NOT DEFINED LAB_ARGS)
set(LAB_ARGS "")
endif()
if("${BENCHMARK_MIN_TIME}" STREQUAL "")
set(BENCHMARK_MIN_TIME "2s")
endif()
set(LAB_BENCHMARK_ARGS --benchmark_min_time=${BENCHMARK_MIN_TIME} --benchmark_out_format=json --benchmark_out=result.json)
if(CI)
# Add CI targets without dependencies
get_property(isMultiConfig GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
if(isMultiConfig)
add_custom_target(validateLab
COMMAND ${PROJECT_BINARY_DIR}/${CMAKE_BUILD_TYPE}/validate ${VALIDATE_ARGS}
VERBATIM)
add_custom_target(benchmarkLab
COMMAND ${PROJECT_BINARY_DIR}/${CMAKE_BUILD_TYPE}/lab ${LAB_ARGS} ${LAB_BENCHMARK_ARGS}
VERBATIM)
else()
add_custom_target(validateLab
COMMAND ${PROJECT_BINARY_DIR}/validate ${VALIDATE_ARGS}
VERBATIM)
add_custom_target(benchmarkLab
COMMAND ${PROJECT_BINARY_DIR}/lab ${LAB_ARGS} ${LAB_BENCHMARK_ARGS}
VERBATIM)
endif()
else()
# Add robust execution targets
add_custom_target(validateLab
COMMAND validate ${VALIDATE_ARGS}
VERBATIM)
add_custom_target(benchmarkLab
COMMAND lab ${LAB_ARGS} ${LAB_BENCHMARK_ARGS}
VERBATIM)
endif()
# Other settings
if(NOT MSVC)
if (WIN32)
target_link_libraries(lab ${BENCHMARK_LIBRARY} shlwapi)
target_link_libraries(validate ${BENCHMARK_LIBRARY} shlwapi)
else()
target_link_libraries(lab ${BENCHMARK_LIBRARY} pthread m)
target_link_libraries(validate ${BENCHMARK_LIBRARY} pthread m)
endif()
# MinGW
if(MINGW)
target_link_libraries(lab shlwapi)
target_link_libraries(validate shlwapi)
endif()
else()
target_link_libraries(lab Shlwapi.lib ${BENCHMARK_LIBRARY})
target_link_libraries(validate Shlwapi.lib ${BENCHMARK_LIBRARY})
set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT lab)
string(REPLACE ";" " " LAB_ARGS_STR "${LAB_ARGS}")
string(REPLACE ";" " " VALIDATE_ARGS_STR "${VALIDATE_ARGS}")
# Since CMake 3.13.0
set_property(TARGET lab PROPERTY VS_DEBUGGER_COMMAND_ARGUMENTS "${LAB_ARGS_STR}")
set_property(TARGET validate PROPERTY VS_DEBUGGER_COMMAND_ARGUMENTS "${VALIDATE_ARGS_STR}")
# Hide helper projects
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
set_target_properties(validateLab PROPERTIES FOLDER CI)
set_target_properties(benchmarkLab PROPERTIES FOLDER CI)
endif()