1# 2# Copyright (c) 2016, Alliance for Open Media. All rights reserved 3# 4# This source code is subject to the terms of the BSD 2 Clause License and the 5# Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License was 6# not distributed with this source code in the LICENSE file, you can obtain it 7# at www.aomedia.org/license/software. If the Alliance for Open Media Patent 8# License 1.0 was not distributed with this source code in the PATENTS file, you 9# can obtain it at www.aomedia.org/license/patent. 10# 11if(AOM_BUILD_CMAKE_AOM_CONFIGURE_CMAKE_) 12 return() 13endif() # AOM_BUILD_CMAKE_AOM_CONFIGURE_CMAKE_ 14set(AOM_BUILD_CMAKE_AOM_CONFIGURE_CMAKE_ 1) 15 16include(FindGit) 17include(FindPerl) 18include(FindThreads) 19 20include("${AOM_ROOT}/build/cmake/aom_config_defaults.cmake") 21include("${AOM_ROOT}/build/cmake/aom_experiment_deps.cmake") 22include("${AOM_ROOT}/build/cmake/aom_optimization.cmake") 23include("${AOM_ROOT}/build/cmake/compiler_flags.cmake") 24include("${AOM_ROOT}/build/cmake/compiler_tests.cmake") 25include("${AOM_ROOT}/build/cmake/util.cmake") 26 27if(DEFINED CONFIG_LOWBITDEPTH) 28 message(WARNING "CONFIG_LOWBITDEPTH has been removed. \ 29 Use -DFORCE_HIGHBITDEPTH_DECODING=1 instead of -DCONFIG_LOWBITDEPTH=0 \ 30 and -DFORCE_HIGHBITDEPTH_DECODING=0 instead of -DCONFIG_LOWBITDEPTH=1.") 31 if(NOT CONFIG_LOWBITDEPTH) 32 set(FORCE_HIGHBITDEPTH_DECODING 33 1 34 CACHE STRING "${cmake_cmdline_helpstring}" FORCE) 35 endif() 36endif() 37 38if(FORCE_HIGHBITDEPTH_DECODING AND NOT CONFIG_AV1_HIGHBITDEPTH) 39 change_config_and_warn(CONFIG_AV1_HIGHBITDEPTH 1 40 "FORCE_HIGHBITDEPTH_DECODING") 41endif() 42 43# Generate the user config settings. 44list(APPEND aom_build_vars ${AOM_CONFIG_VARS} ${AOM_OPTION_VARS}) 45foreach(cache_var ${aom_build_vars}) 46 get_property(cache_var_helpstring CACHE ${cache_var} PROPERTY HELPSTRING) 47 if(cache_var_helpstring STREQUAL cmake_cmdline_helpstring) 48 set(AOM_CMAKE_CONFIG "${AOM_CMAKE_CONFIG} -D${cache_var}=${${cache_var}}") 49 endif() 50endforeach() 51string(STRIP "${AOM_CMAKE_CONFIG}" AOM_CMAKE_CONFIG) 52 53# Detect target CPU. 54if(NOT AOM_TARGET_CPU) 55 string(TOLOWER "${CMAKE_SYSTEM_PROCESSOR}" cpu_lowercase) 56 if(cpu_lowercase STREQUAL "amd64" OR cpu_lowercase STREQUAL "x86_64") 57 if(CMAKE_SIZEOF_VOID_P EQUAL 4) 58 set(AOM_TARGET_CPU "x86") 59 elseif(CMAKE_SIZEOF_VOID_P EQUAL 8) 60 set(AOM_TARGET_CPU "x86_64") 61 else() 62 message( 63 FATAL_ERROR "--- Unexpected pointer size (${CMAKE_SIZEOF_VOID_P}) for\n" 64 " CMAKE_SYSTEM_NAME=${CMAKE_SYSTEM_NAME}\n" 65 " CMAKE_SYSTEM_PROCESSOR=${CMAKE_SYSTEM_PROCESSOR}\n" 66 " CMAKE_GENERATOR=${CMAKE_GENERATOR}\n") 67 endif() 68 elseif(cpu_lowercase STREQUAL "i386" OR cpu_lowercase STREQUAL "x86") 69 set(AOM_TARGET_CPU "x86") 70 elseif(cpu_lowercase MATCHES "^arm" OR cpu_lowercase MATCHES "^mips") 71 set(AOM_TARGET_CPU "${cpu_lowercase}") 72 elseif(cpu_lowercase MATCHES "aarch64") 73 set(AOM_TARGET_CPU "arm64") 74 elseif(cpu_lowercase MATCHES "^ppc") 75 set(AOM_TARGET_CPU "ppc") 76 else() 77 message(WARNING "The architecture ${CMAKE_SYSTEM_PROCESSOR} is not " 78 "supported, falling back to the generic target") 79 set(AOM_TARGET_CPU "generic") 80 endif() 81endif() 82 83if(CMAKE_TOOLCHAIN_FILE) # Add toolchain file to config string. 84 if(IS_ABSOLUTE "${CMAKE_TOOLCHAIN_FILE}") 85 file(RELATIVE_PATH toolchain_path "${AOM_CONFIG_DIR}" 86 "${CMAKE_TOOLCHAIN_FILE}") 87 else() 88 set(toolchain_path "${CMAKE_TOOLCHAIN_FILE}") 89 endif() 90 set(toolchain_string "-DCMAKE_TOOLCHAIN_FILE=\\\"${toolchain_path}\\\"") 91 set(AOM_CMAKE_CONFIG "${toolchain_string} ${AOM_CMAKE_CONFIG}") 92else() 93 94 # Add detected CPU to the config string. 95 set(AOM_CMAKE_CONFIG "-DAOM_TARGET_CPU=${AOM_TARGET_CPU} ${AOM_CMAKE_CONFIG}") 96endif() 97set(AOM_CMAKE_CONFIG "-G \\\"${CMAKE_GENERATOR}\\\" ${AOM_CMAKE_CONFIG}") 98file(RELATIVE_PATH source_path "${AOM_CONFIG_DIR}" "${AOM_ROOT}") 99set(AOM_CMAKE_CONFIG "cmake ${source_path} ${AOM_CMAKE_CONFIG}") 100string(STRIP "${AOM_CMAKE_CONFIG}" AOM_CMAKE_CONFIG) 101 102message("--- aom_configure: Detected CPU: ${AOM_TARGET_CPU}") 103set(AOM_TARGET_SYSTEM ${CMAKE_SYSTEM_NAME}) 104 105string(TOLOWER "${CMAKE_BUILD_TYPE}" build_type_lowercase) 106if(build_type_lowercase STREQUAL "debug") 107 set(CONFIG_DEBUG 1) 108endif() 109 110if(BUILD_SHARED_LIBS) 111 set(CONFIG_PIC 1) 112 set(CONFIG_SHARED 1) 113endif() 114 115if(NOT MSVC) 116 if(CONFIG_PIC) 117 118 # TODO(tomfinegan): clang needs -pie in CMAKE_EXE_LINKER_FLAGS for this to 119 # work. 120 set(CMAKE_POSITION_INDEPENDENT_CODE ON) 121 if(AOM_TARGET_SYSTEM STREQUAL "Linux" 122 AND AOM_TARGET_CPU MATCHES "^armv[78]") 123 set(AOM_AS_FLAGS ${AOM_AS_FLAGS} --defsym PIC=1) 124 else() 125 set(AOM_AS_FLAGS ${AOM_AS_FLAGS} -DPIC) 126 endif() 127 endif() 128endif() 129 130if(AOM_TARGET_CPU STREQUAL "x86" OR AOM_TARGET_CPU STREQUAL "x86_64") 131 find_program(AS_EXECUTABLE yasm $ENV{YASM_PATH}) 132 if(NOT AS_EXECUTABLE OR ENABLE_NASM) 133 unset(AS_EXECUTABLE CACHE) 134 find_program(AS_EXECUTABLE nasm $ENV{NASM_PATH}) 135 if(AS_EXECUTABLE) 136 test_nasm() 137 endif() 138 endif() 139 140 if(NOT AS_EXECUTABLE) 141 message( 142 FATAL_ERROR 143 "Unable to find assembler. Install 'yasm' or 'nasm.' " 144 "To build without optimizations, add -DAOM_TARGET_CPU=generic to " 145 "your cmake command line.") 146 endif() 147 get_asm_obj_format("objformat") 148 set(AOM_AS_FLAGS -f ${objformat} ${AOM_AS_FLAGS}) 149 string(STRIP "${AOM_AS_FLAGS}" AOM_AS_FLAGS) 150elseif(AOM_TARGET_CPU MATCHES "arm") 151 if(AOM_TARGET_SYSTEM STREQUAL "Darwin") 152 set(AS_EXECUTABLE as) 153 set(AOM_AS_FLAGS -arch ${AOM_TARGET_CPU} -isysroot ${CMAKE_OSX_SYSROOT}) 154 elseif(AOM_TARGET_SYSTEM STREQUAL "Windows") 155 if(NOT AS_EXECUTABLE) 156 set(AS_EXECUTABLE ${CMAKE_C_COMPILER} -c -mimplicit-it=always) 157 endif() 158 else() 159 if(NOT AS_EXECUTABLE) 160 set(AS_EXECUTABLE as) 161 endif() 162 endif() 163 find_program(as_executable_found ${AS_EXECUTABLE}) 164 if(NOT as_executable_found) 165 message( 166 FATAL_ERROR 167 "Unable to find assembler and optimizations are enabled." 168 "Searched for ${AS_EXECUTABLE}. Install it, add it to your path, or " 169 "set the assembler directly by adding -DAS_EXECUTABLE=<assembler path> " 170 "to your CMake command line." 171 "To build without optimizations, add -DAOM_TARGET_CPU=generic to your " 172 "cmake command line.") 173 endif() 174 string(STRIP "${AOM_AS_FLAGS}" AOM_AS_FLAGS) 175endif() 176 177if(CONFIG_ANALYZER) 178 include(FindwxWidgets) 179 find_package(wxWidgets REQUIRED adv base core) 180 include(${wxWidgets_USE_FILE}) 181endif() 182 183if(NOT MSVC AND CMAKE_C_COMPILER_ID MATCHES "GNU\|Clang") 184 set(CONFIG_GCC 1) 185endif() 186 187if(CONFIG_GCOV) 188 message("--- Testing for CONFIG_GCOV support.") 189 require_linker_flag("-fprofile-arcs -ftest-coverage") 190 require_compiler_flag("-fprofile-arcs -ftest-coverage" YES) 191endif() 192 193if(CONFIG_GPROF) 194 message("--- Testing for CONFIG_GPROF support.") 195 require_compiler_flag("-pg" YES) 196endif() 197 198if(AOM_TARGET_SYSTEM MATCHES "Darwin\|Linux\|Windows\|Android") 199 set(CONFIG_OS_SUPPORT 1) 200endif() 201 202# The default _WIN32_WINNT value in MinGW is 0x0502 (Windows XP with SP2). Set 203# it to 0x0601 (Windows 7). 204if(AOM_TARGET_SYSTEM STREQUAL "Windows") 205 add_compiler_flag_if_supported("-D_WIN32_WINNT=0x0601") 206endif() 207 208# 209# Fix CONFIG_* dependencies. This must be done before including cpu.cmake to 210# ensure RTCD_CONFIG_* are properly set. 211fix_experiment_configs() 212 213# Test compiler support. 214aom_get_inline("INLINE") 215 216# Don't just check for pthread.h, but use the result of the full pthreads 217# including a linking check in FindThreads above. 218set(HAVE_PTHREAD_H ${CMAKE_USE_PTHREADS_INIT}) 219aom_check_source_compiles("unistd_check" "#include <unistd.h>" HAVE_UNISTD_H) 220 221if(NOT MSVC) 222 aom_push_var(CMAKE_REQUIRED_LIBRARIES "m") 223 aom_check_c_compiles("fenv_check" "#define _GNU_SOURCE 224 #include <fenv.h> 225 void unused(void) { 226 (void)unused; 227 (void)feenableexcept(FE_DIVBYZERO | FE_INVALID); 228 }" HAVE_FEXCEPT) 229 aom_pop_var(CMAKE_REQUIRED_LIBRARIES) 230endif() 231 232include("${AOM_ROOT}/build/cmake/cpu.cmake") 233 234if(ENABLE_CCACHE) 235 set_compiler_launcher(ENABLE_CCACHE ccache) 236endif() 237 238if(ENABLE_DISTCC) 239 set_compiler_launcher(ENABLE_DISTCC distcc) 240endif() 241 242if(ENABLE_GOMA) 243 set_compiler_launcher(ENABLE_GOMA gomacc) 244endif() 245 246if(NOT CONFIG_AV1_DECODER AND NOT CONFIG_AV1_ENCODER) 247 message(FATAL_ERROR "Decoder and encoder disabled, nothing to build.") 248endif() 249 250if(DECODE_HEIGHT_LIMIT OR DECODE_WIDTH_LIMIT) 251 change_config_and_warn(CONFIG_SIZE_LIMIT 1 252 "DECODE_HEIGHT_LIMIT and DECODE_WIDTH_LIMIT") 253endif() 254 255if(CONFIG_SIZE_LIMIT) 256 if(NOT DECODE_HEIGHT_LIMIT OR NOT DECODE_WIDTH_LIMIT) 257 message(FATAL_ERROR "When setting CONFIG_SIZE_LIMIT, DECODE_HEIGHT_LIMIT " 258 "and DECODE_WIDTH_LIMIT must be set.") 259 endif() 260endif() 261 262# Test compiler flags. 263if(MSVC) 264 add_compiler_flag_if_supported("/W3") 265 266 # Disable MSVC warnings that suggest making code non-portable. 267 add_compiler_flag_if_supported("/wd4996") 268 if(ENABLE_WERROR) 269 add_compiler_flag_if_supported("/WX") 270 endif() 271else() 272 require_c_flag("-std=c99" YES) 273 require_cxx_flag_nomsvc("-std=c++11" YES) 274 add_compiler_flag_if_supported("-Wall") 275 add_compiler_flag_if_supported("-Wdisabled-optimization") 276 add_compiler_flag_if_supported("-Wextra") 277 add_compiler_flag_if_supported("-Wfloat-conversion") 278 add_c_flag_if_supported("-Wimplicit-function-declaration") 279 add_compiler_flag_if_supported("-Wlogical-op") 280 add_compiler_flag_if_supported("-Wpointer-arith") 281 add_compiler_flag_if_supported("-Wshorten-64-to-32") 282 add_compiler_flag_if_supported("-Wsign-compare") 283 add_compiler_flag_if_supported("-Wstring-conversion") 284 add_compiler_flag_if_supported("-Wtype-limits") 285 add_compiler_flag_if_supported("-Wuninitialized") 286 add_compiler_flag_if_supported("-Wunused") 287 add_compiler_flag_if_supported("-Wvla") 288 289 if(CMAKE_C_COMPILER_ID MATCHES "GNU" AND SANITIZE MATCHES "address|undefined") 290 291 # This combination has more stack overhead, so we account for it by 292 # providing higher stack limit than usual. 293 add_c_flag_if_supported("-Wstack-usage=170000") 294 add_cxx_flag_if_supported("-Wstack-usage=270000") 295 elseif(CONFIG_RD_DEBUG) # Another case where higher stack usage is expected. 296 add_c_flag_if_supported("-Wstack-usage=135000") 297 add_cxx_flag_if_supported("-Wstack-usage=240000") 298 else() 299 add_c_flag_if_supported("-Wstack-usage=100000") 300 add_cxx_flag_if_supported("-Wstack-usage=240000") 301 endif() 302 303 if(CMAKE_C_COMPILER_ID MATCHES "GNU" AND SANITIZE MATCHES "address") 304 # Disable no optimization warning when compiling with sanitizers 305 add_compiler_flag_if_supported("-Wno-disabled-optimization") 306 endif() 307 308 # Add -Wshadow only for C files to avoid massive gtest warning spam. 309 add_c_flag_if_supported("-Wshadow") 310 311 # Add -Wundef only for C files to avoid massive gtest warning spam. 312 add_c_flag_if_supported("-Wundef") 313 314 # Quiet gcc 6 vs 7 abi warnings: 315 # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77728 316 if(AOM_TARGET_CPU MATCHES "arm") 317 add_cxx_flag_if_supported("-Wno-psabi") 318 endif() 319 320 if(ENABLE_WERROR) 321 add_compiler_flag_if_supported("-Werror") 322 endif() 323 324 if(build_type_lowercase MATCHES "rel") 325 add_compiler_flag_if_supported("-U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=0") 326 endif() 327 add_compiler_flag_if_supported("-D_LARGEFILE_SOURCE") 328 add_compiler_flag_if_supported("-D_FILE_OFFSET_BITS=64") 329endif() 330 331set(AOM_LIB_LINK_TYPE PUBLIC) 332if(EMSCRIPTEN) 333 334 # Avoid CMake generation time errors resulting from collisions with the form 335 # of target_link_libraries() used by Emscripten.cmake. 336 unset(AOM_LIB_LINK_TYPE) 337endif() 338 339# Generate aom_config templates. 340set(aom_config_asm_template "${AOM_CONFIG_DIR}/config/aom_config.asm.cmake") 341set(aom_config_h_template "${AOM_CONFIG_DIR}/config/aom_config.h.cmake") 342execute_process( 343 COMMAND ${CMAKE_COMMAND} 344 -DAOM_CONFIG_DIR=${AOM_CONFIG_DIR} -DAOM_ROOT=${AOM_ROOT} -P 345 "${AOM_ROOT}/build/cmake/generate_aom_config_templates.cmake") 346 347# Generate aom_config.{asm,h}. 348configure_file("${aom_config_asm_template}" 349 "${AOM_CONFIG_DIR}/config/aom_config.asm") 350configure_file("${aom_config_h_template}" 351 "${AOM_CONFIG_DIR}/config/aom_config.h") 352 353# Read the current git hash. 354find_package(Git) 355if(NOT GIT_FOUND) 356 message("--- Git missing, version will be read from CHANGELOG.") 357endif() 358 359configure_file("${AOM_ROOT}/build/cmake/aom_config.c.template" 360 "${AOM_CONFIG_DIR}/config/aom_config.c") 361 362# Find Perl and generate the RTCD sources. 363find_package(Perl) 364if(NOT PERL_FOUND) 365 message(FATAL_ERROR "Perl is required to build libaom.") 366endif() 367 368set(AOM_RTCD_CONFIG_FILE_LIST "${AOM_ROOT}/aom_dsp/aom_dsp_rtcd_defs.pl" 369 "${AOM_ROOT}/aom_scale/aom_scale_rtcd.pl" 370 "${AOM_ROOT}/av1/common/av1_rtcd_defs.pl") 371set(AOM_RTCD_HEADER_FILE_LIST "${AOM_CONFIG_DIR}/config/aom_dsp_rtcd.h" 372 "${AOM_CONFIG_DIR}/config/aom_scale_rtcd.h" 373 "${AOM_CONFIG_DIR}/config/av1_rtcd.h") 374set(AOM_RTCD_SOURCE_FILE_LIST "${AOM_ROOT}/aom_dsp/aom_dsp_rtcd.c" 375 "${AOM_ROOT}/aom_scale/aom_scale_rtcd.c" 376 "${AOM_ROOT}/av1/common/av1_rtcd.c") 377set(AOM_RTCD_SYMBOL_LIST aom_dsp_rtcd aom_scale_rtcd av1_rtcd) 378list(LENGTH AOM_RTCD_SYMBOL_LIST AOM_RTCD_CUSTOM_COMMAND_COUNT) 379math(EXPR AOM_RTCD_CUSTOM_COMMAND_COUNT "${AOM_RTCD_CUSTOM_COMMAND_COUNT} - 1") 380 381foreach(NUM RANGE ${AOM_RTCD_CUSTOM_COMMAND_COUNT}) 382 list(GET AOM_RTCD_CONFIG_FILE_LIST ${NUM} AOM_RTCD_CONFIG_FILE) 383 list(GET AOM_RTCD_HEADER_FILE_LIST ${NUM} AOM_RTCD_HEADER_FILE) 384 list(GET AOM_RTCD_SOURCE_FILE_LIST ${NUM} AOM_RTCD_SOURCE_FILE) 385 list(GET AOM_RTCD_SYMBOL_LIST ${NUM} AOM_RTCD_SYMBOL) 386 execute_process( 387 COMMAND 388 ${PERL_EXECUTABLE} "${AOM_ROOT}/build/cmake/rtcd.pl" 389 --arch=${AOM_TARGET_CPU} 390 --sym=${AOM_RTCD_SYMBOL} ${AOM_RTCD_FLAGS} 391 --config=${AOM_CONFIG_DIR}/config/aom_config.h ${AOM_RTCD_CONFIG_FILE} 392 OUTPUT_FILE ${AOM_RTCD_HEADER_FILE}) 393endforeach() 394 395# Generate aom_version.h. 396execute_process(COMMAND ${CMAKE_COMMAND} 397 -DAOM_CONFIG_DIR=${AOM_CONFIG_DIR} 398 -DAOM_ROOT=${AOM_ROOT} 399 -DGIT_EXECUTABLE=${GIT_EXECUTABLE} 400 -DPERL_EXECUTABLE=${PERL_EXECUTABLE} -P 401 "${AOM_ROOT}/build/cmake/version.cmake") 402