1# SPDX-License-Identifier: Apache-2.0 2# ---------------------------------------------------------------------------- 3# Copyright 2020-2022 Arm Limited 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); you may not 6# use this file except in compliance with the License. You may obtain a copy 7# of the License at: 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 14# License for the specific language governing permissions and limitations 15# under the License. 16# ---------------------------------------------------------------------------- 17 18if(${UNIVERSAL_BUILD}) 19 set(ASTC_TARGET astc${CODEC}) 20else() 21 set(ASTC_TARGET astc${CODEC}-${ISA_SIMD}) 22endif() 23 24project(${ASTC_TARGET}) 25 26set(GNU_LIKE "GNU,Clang,AppleClang") 27set(CLANG_LIKE "Clang,AppleClang") 28 29add_library(${ASTC_TARGET}-static 30 STATIC 31 astcenc_averages_and_directions.cpp 32 astcenc_block_sizes.cpp 33 astcenc_color_quantize.cpp 34 astcenc_color_unquantize.cpp 35 astcenc_compress_symbolic.cpp 36 astcenc_compute_variance.cpp 37 astcenc_decompress_symbolic.cpp 38 astcenc_diagnostic_trace.cpp 39 astcenc_entry.cpp 40 astcenc_find_best_partitioning.cpp 41 astcenc_ideal_endpoints_and_weights.cpp 42 astcenc_image.cpp 43 astcenc_integer_sequence.cpp 44 astcenc_mathlib.cpp 45 astcenc_mathlib_softfloat.cpp 46 astcenc_partition_tables.cpp 47 astcenc_percentile_tables.cpp 48 astcenc_pick_best_endpoint_format.cpp 49 astcenc_platform_isa_detection.cpp 50 astcenc_quantization.cpp 51 astcenc_symbolic_physical.cpp 52 astcenc_weight_align.cpp 53 astcenc_weight_quant_xfer_tables.cpp) 54 55target_include_directories(${ASTC_TARGET}-static 56 PUBLIC 57 $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}> 58 $<INSTALL_INTERFACE:.>) 59 60if(${CLI}) 61 add_executable(${ASTC_TARGET} 62 astcenccli_error_metrics.cpp 63 astcenccli_image.cpp 64 astcenccli_image_external.cpp 65 astcenccli_image_load_store.cpp 66 astcenccli_platform_dependents.cpp 67 astcenccli_toplevel.cpp 68 astcenccli_toplevel_help.cpp) 69 70 target_link_libraries(${ASTC_TARGET} 71 PRIVATE 72 ${ASTC_TARGET}-static) 73endif() 74 75macro(astcenc_set_properties NAME) 76 77 target_compile_features(${NAME} 78 PRIVATE 79 cxx_std_14) 80 81 target_compile_definitions(${NAME} 82 PRIVATE 83 # MSVC defines 84 $<$<CXX_COMPILER_ID:MSVC>:_CRT_SECURE_NO_WARNINGS>) 85 86 if(${DECOMPRESSOR}) 87 target_compile_definitions(${NAME} 88 PRIVATE 89 ASTCENC_DECOMPRESS_ONLY) 90 endif() 91 92 if(${BLOCK_MAX_TEXELS}) 93 target_compile_definitions(${NAME} 94 PRIVATE 95 ASTCENC_BLOCK_MAX_TEXELS=${BLOCK_MAX_TEXELS}) 96 endif() 97 98 if(${DIAGNOSTICS}) 99 target_compile_definitions(${NAME} 100 PUBLIC 101 ASTCENC_DIAGNOSTICS) 102 endif() 103 104 target_compile_options(${NAME} 105 PRIVATE 106 # Use pthreads on Linux/macOS 107 $<$<PLATFORM_ID:Linux,Darwin>:-pthread> 108 109 # MSVC compiler defines 110 $<$<CXX_COMPILER_ID:MSVC>:/EHsc> 111 $<$<CXX_COMPILER_ID:MSVC>:/fp:strict> 112 $<$<CXX_COMPILER_ID:MSVC>:/wd4324> 113 114 # G++ and Clang++ compiler defines 115 $<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wall> 116 $<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wextra> 117 $<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wpedantic> 118 $<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Werror> 119 $<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wshadow> 120 $<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wdouble-promotion> 121 122 # Hide noise thrown up by Clang 10 and clang-cl 123 $<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wno-unknown-warning-option> 124 $<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wno-c++98-compat-pedantic> 125 $<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wno-c++98-c++11-compat-pedantic> 126 $<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wno-float-equal> 127 $<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wno-deprecated-declarations> 128 $<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wno-atomic-implicit-seq-cst> 129 130 # Clang 10 also throws up warnings we need to investigate (ours) 131 $<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wno-cast-align> 132 $<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wno-sign-conversion> 133 $<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wno-implicit-int-conversion> 134 $<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wno-shift-sign-overflow> 135 $<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wno-format-nonliteral> 136 $<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wno-reserved-identifier> 137 $<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wno-cast-function-type> 138 139 $<$<CXX_COMPILER_ID:Clang>:-Wdocumentation>) 140 141 target_link_options(${NAME} 142 PRIVATE 143 # Use pthreads on Linux/macOS 144 $<$<PLATFORM_ID:Linux,Darwin>:-pthread>) 145 146 if(${ASAN}) 147 target_compile_options(${NAME} 148 PRIVATE 149 $<$<CXX_COMPILER_ID:${CLANG_LIKE}>:-fsanitize=address>) 150 151 target_link_options(${NAME} 152 PRIVATE 153 $<$<CXX_COMPILER_ID:${CLANG_LIKE}>:-fsanitize=address>) 154 endif() 155 156 if(${NO_INVARIANCE}) 157 target_compile_definitions(${NAME} 158 PRIVATE 159 ASTCENC_NO_INVARIANCE=1) 160 161 target_compile_options(${NAME} 162 PRIVATE 163 $<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-ffp-contract=fast>) 164 endif() 165 166 if(${CLI}) 167 # Enable LTO on release builds 168 set_property(TARGET ${NAME} 169 PROPERTY 170 INTERPROCEDURAL_OPTIMIZATION_RELEASE True) 171 172 # Use a static runtime on MSVC builds (ignored on non-MSVC compilers) 173 set_property(TARGET ${NAME} 174 PROPERTY 175 MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>") 176 endif() 177 178 # Set up configuration for SIMD ISA builds 179 if(${ISA_SIMD} MATCHES "none") 180 if(NOT ${UNIVERSAL_BUILD}) 181 target_compile_definitions(${NAME} 182 PRIVATE 183 ASTCENC_NEON=0 184 ASTCENC_SSE=0 185 ASTCENC_AVX=0 186 ASTCENC_POPCNT=0 187 ASTCENC_F16C=0) 188 endif() 189 190 elseif(${ISA_SIMD} MATCHES "neon") 191 if(NOT ${UNIVERSAL_BUILD}) 192 target_compile_definitions(${NAME} 193 PRIVATE 194 ASTCENC_NEON=1 195 ASTCENC_SSE=0 196 ASTCENC_AVX=0 197 ASTCENC_POPCNT=0 198 ASTCENC_F16C=0) 199 endif() 200 201 # Workaround MSVC codegen bug for NEON builds on VS 2022 17.2 or older 202 # https://developercommunity.visualstudio.com/t/inlining-turns-constant-into-register-operand-for/1394798 203 if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC" AND MSVC_VERSION LESS 1933) 204 target_compile_options(${NAME} 205 PRIVATE 206 $<$<CXX_COMPILER_ID:MSVC>:/d2ssa-cfg-sink->) 207 endif() 208 209 elseif((${ISA_SIMD} MATCHES "sse2") OR (${UNIVERSAL_BUILD} AND ${ISA_SSE2})) 210 if(NOT ${UNIVERSAL_BUILD}) 211 target_compile_definitions(${NAME} 212 PRIVATE 213 ASTCENC_NEON=0 214 ASTCENC_SSE=20 215 ASTCENC_AVX=0 216 ASTCENC_POPCNT=0 217 ASTCENC_F16C=0) 218 endif() 219 220 # These settings are needed on AppleClang as SSE4.1 is on by default 221 # Suppress unused argument for macOS universal build behavior 222 target_compile_options(${NAME} 223 PRIVATE 224 $<$<CXX_COMPILER_ID:AppleClang>:-msse2> 225 $<$<CXX_COMPILER_ID:AppleClang>:-mno-sse4.1> 226 $<$<CXX_COMPILER_ID:AppleClang>:-Wno-unused-command-line-argument>) 227 228 elseif((${ISA_SIMD} MATCHES "sse4.1") OR (${UNIVERSAL_BUILD} AND ${ISA_SSE41})) 229 if(NOT ${UNIVERSAL_BUILD}) 230 target_compile_definitions(${NAME} 231 PRIVATE 232 ASTCENC_NEON=0 233 ASTCENC_SSE=41 234 ASTCENC_AVX=0 235 ASTCENC_POPCNT=1 236 ASTCENC_F16C=0) 237 endif() 238 239 # Suppress unused argument for macOS universal build behavior 240 target_compile_options(${NAME} 241 PRIVATE 242 $<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-msse4.1 -mpopcnt> 243 $<$<CXX_COMPILER_ID:AppleClang>:-Wno-unused-command-line-argument>) 244 245 elseif((${ISA_SIMD} MATCHES "avx2") OR (${UNIVERSAL_BUILD} AND ${ISA_AVX2})) 246 if(NOT ${UNIVERSAL_BUILD}) 247 target_compile_definitions(${NAME} 248 PRIVATE 249 ASTCENC_NEON=0 250 ASTCENC_SSE=41 251 ASTCENC_AVX=2 252 ASTCENC_POPCNT=1 253 ASTCENC_F16C=1) 254 endif() 255 256 # Suppress unused argument for macOS universal build behavior 257 target_compile_options(${NAME} 258 PRIVATE 259 $<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-mavx2 -mpopcnt -mf16c> 260 $<$<CXX_COMPILER_ID:MSVC>:/arch:AVX2> 261 $<$<CXX_COMPILER_ID:AppleClang>:-Wno-unused-command-line-argument>) 262 263 # Non-invariant builds enable us to loosen the compiler constraints on 264 # floating point, but this is only worth doing on CPUs with AVX2 because 265 # this implies we can also enable the FMA instruction set extensions 266 # which significantly improve performance. Note that this DOES reduce 267 # image quality by up to 0.2 dB (normally much less), but buys an 268 # average of 10-15% performance improvement ... 269 if(${NO_INVARIANCE}) 270 target_compile_options(${NAME} 271 PRIVATE 272 $<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-mfma>) 273 endif() 274 275 endif() 276 277endmacro() 278 279if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang") 280 string(CONCAT EXTERNAL_CXX_FLAGS 281 " $<$<NOT:$<CXX_COMPILER_ID:MSVC>>: -fno-strict-aliasing>" 282 " $<$<NOT:$<CXX_COMPILER_ID:MSVC>>: -Wno-unused-parameter>" 283 " $<$<NOT:$<CXX_COMPILER_ID:MSVC>>: -Wno-old-style-cast>" 284 " $<$<NOT:$<CXX_COMPILER_ID:MSVC>>: -Wno-double-promotion>" 285 " $<$<NOT:$<CXX_COMPILER_ID:MSVC>>: -Wno-zero-as-null-pointer-constant>" 286 " $<$<NOT:$<CXX_COMPILER_ID:MSVC>>: -Wno-disabled-macro-expansion>" 287 " $<$<NOT:$<CXX_COMPILER_ID:MSVC>>: -Wno-reserved-id-macro>" 288 " $<$<NOT:$<CXX_COMPILER_ID:MSVC>>: -Wno-extra-semi-stmt>" 289 " $<$<NOT:$<CXX_COMPILER_ID:MSVC>>: -Wno-implicit-fallthrough>" 290 " $<$<NOT:$<CXX_COMPILER_ID:MSVC>>: -Wno-tautological-type-limit-compare>" 291 " $<$<NOT:$<CXX_COMPILER_ID:MSVC>>: -Wno-cast-qual>" 292 " $<$<NOT:$<CXX_COMPILER_ID:MSVC>>: -Wno-reserved-identifier>" 293 " $<$<CXX_COMPILER_ID:Clang>: -Wno-missing-prototypes>" 294 " $<$<NOT:$<CXX_COMPILER_ID:MSVC>>: -Wno-suggest-override>" 295 " $<$<NOT:$<CXX_COMPILER_ID:MSVC>>: -Wno-used-but-marked-unused>" 296 " $<$<NOT:$<CXX_COMPILER_ID:MSVC>>: -Wno-noexcept-type>" 297 " $<$<NOT:$<CXX_COMPILER_ID:MSVC>>: -Wno-comma>" 298 " $<$<NOT:$<CXX_COMPILER_ID:MSVC>>: -Wno-c99-extensions>") 299 300 set_source_files_properties(astcenccli_image_external.cpp 301 PROPERTIES 302 COMPILE_FLAGS ${EXTERNAL_CXX_FLAGS}) 303endif() 304 305astcenc_set_properties(${ASTC_TARGET}-static) 306 307 target_compile_options(${ASTC_TARGET}-static 308 PRIVATE 309 $<$<CXX_COMPILER_ID:MSVC>:/W4>) 310 311if(${CLI}) 312 astcenc_set_properties(${ASTC_TARGET}) 313 314 target_compile_options(${ASTC_TARGET} 315 PRIVATE 316 $<$<CXX_COMPILER_ID:MSVC>:/W3>) 317 318 string(TIMESTAMP astcencoder_YEAR "%Y") 319 320 configure_file( 321 astcenccli_version.h.in 322 astcenccli_version.h 323 ESCAPE_QUOTES @ONLY) 324 325 target_include_directories(${ASTC_TARGET} 326 PRIVATE 327 ${CMAKE_CURRENT_BINARY_DIR}) 328 329 install(TARGETS ${ASTC_TARGET} DESTINATION ${PACKAGE_ROOT}) 330endif() 331