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