aom_configure.cmake 8.24 KB
Newer Older
Tom Finegan's avatar
Tom Finegan committed
1 2 3 4 5 6 7 8 9 10 11 12
##
## Copyright (c) 2016, Alliance for Open Media. All rights reserved
##
## This source code is subject to the terms of the BSD 2 Clause License and
## the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
## was not distributed with this source code in the LICENSE file, you can
## obtain it at www.aomedia.org/license/software. If the Alliance for Open
## Media Patent License 1.0 was not distributed with this source code in the
## PATENTS file, you can obtain it at www.aomedia.org/license/patent.
##
cmake_minimum_required(VERSION 3.2)

13
include("${AOM_ROOT}/build/cmake/aom_config_defaults.cmake")
Tom Finegan's avatar
Tom Finegan committed
14
include("${AOM_ROOT}/build/cmake/compiler_flags.cmake")
15
include("${AOM_ROOT}/build/cmake/compiler_tests.cmake")
Tom Finegan's avatar
Tom Finegan committed
16

17 18
# Detect target CPU.
if (NOT AOM_TARGET_CPU)
19 20 21 22 23 24 25 26 27 28 29 30 31
  if ("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "AMD64" OR
      "${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86_64")
    if (${CMAKE_SIZEOF_VOID_P} EQUAL 4)
      set(AOM_TARGET_CPU "x86")
    elseif (${CMAKE_SIZEOF_VOID_P} EQUAL 8)
      set(AOM_TARGET_CPU "x86_64")
    else ()
      message(FATAL_ERROR
              "--- Unexpected pointer size (${CMAKE_SIZEOF_VOID_P}) for\n"
              "      CMAKE_SYSTEM_NAME=${CMAKE_SYSTEM_NAME}\n"
              "      CMAKE_SYSTEM_PROCESSOR=${CMAKE_SYSTEM_PROCESSOR}\n"
              "      CMAKE_GENERATOR=${CMAKE_GENERATOR}\n")
    endif ()
32 33 34
  elseif ("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "i386" OR
          "${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86")
    set(AOM_TARGET_CPU "x86")
35
  endif ()
36 37
endif ()

38
message("--- aom_configure: Detected CPU: ${AOM_TARGET_CPU}")
39
set(AOM_TARGET_SYSTEM ${CMAKE_SYSTEM_NAME})
40

41
if (NOT EXISTS "${AOM_ROOT}/build/cmake/targets/${AOM_TARGET_CPU}.cmake")
42 43 44
  message(FATAL_ERROR "No RTCD template for ${AOM_TARGET_CPU}. Create one, or "
          "add -DAOM_TARGET_CPU=generic to your cmake command line for a "
          "generic build of libaom and tools.")
45
endif ()
46

47 48 49 50
if ("${AOM_TARGET_CPU}" STREQUAL "x86" OR "${AOM_TARGET_CPU}" STREQUAL "x86_64")
  find_program(YASM_EXECUTABLE yasm $ENV{YASM_PATH})
  if (NOT YASM_EXECUTABLE)
    message(WARNING "Unable to find yasm, using generic as target CPU.")
51 52 53 54 55
    set(AOM_TARGET_CPU "generic")
  endif ()
endif ()

include("${AOM_ROOT}/build/cmake/targets/${AOM_TARGET_CPU}.cmake")
Tom Finegan's avatar
Tom Finegan committed
56 57

include(FindGit)
Tom Finegan's avatar
Tom Finegan committed
58
include(FindPerl)
Tom Finegan's avatar
Tom Finegan committed
59

60 61 62 63 64 65 66 67 68
# Test compiler flags.
if (MSVC)
  add_compiler_flag_if_supported("/W3")
  # Disable MSVC warnings that suggest making code non-portable.
  add_compiler_flag_if_supported("/wd4996")
  if (ENABLE_WERROR)
    add_compiler_flag_if_supported("/WX")
  endif ()
else ()
69
  require_c_flag("-std=c99" YES)
70 71 72 73 74 75 76 77 78 79 80
  add_compiler_flag_if_supported("-Wall")
  add_compiler_flag_if_supported("-Wdisabled-optimization")
  add_compiler_flag_if_supported("-Wextra")
  add_compiler_flag_if_supported("-Wfloat-conversion")
  add_compiler_flag_if_supported("-Wimplicit-function-declaration")
  add_compiler_flag_if_supported("-Wpointer-arith")
  add_compiler_flag_if_supported("-Wsign-compare")
  add_compiler_flag_if_supported("-Wtype-limits")
  add_compiler_flag_if_supported("-Wuninitialized")
  add_compiler_flag_if_supported("-Wunused")
  add_compiler_flag_if_supported("-Wvla")
81 82 83 84

  # Add -Wshadow only for C files to avoid massive gtest warning spam.
  add_c_flag_if_supported("-Wshadow")

85 86 87 88 89 90 91
  if (ENABLE_WERROR)
    add_compiler_flag_if_supported("-Werror")
  endif ()
  # Flag(s) added here negate CMake defaults and produce build output similar
  # to the existing configure/make build system.
  add_compiler_flag_if_supported("-Wno-unused-function")
endif ()
Tom Finegan's avatar
Tom Finegan committed
92

93
# Test compiler support.
94 95 96 97
AomCheckSourceCompiles("inline_check" "static inline void function(void) {}"
                       HAVE_INLINE)
if (HAVE_INLINE EQUAL 1)
  set(INLINE "inline")
98
endif ()
99 100 101 102 103 104 105 106
# TODO(tomfinegan): aom_ports_check is legacy; HAVE_AOM_PORTS is not used
# anywhere in the aom sources. To be removed after parity with the legacy
# build system stops being important.
AomCheckSourceCompiles("aom_ports_check"
                       "#include \"${AOM_ROOT}/aom/aom_integer.h\""
                       HAVE_AOM_PORTS)
AomCheckSourceCompiles("pthread_check" "#include <pthread.h>" HAVE_PTHREAD_H)
AomCheckSourceCompiles("unistd_check" "#include <unistd.h>" HAVE_UNISTD_H)
107

108 109 110
# TODO(tomfinegan): consume trailing whitespace after configure_file() when
# target platform check produces empty INLINE and RESTRICT values (aka empty
# values require special casing).
Tom Finegan's avatar
Tom Finegan committed
111
configure_file("${AOM_ROOT}/build/cmake/aom_config.h.cmake"
Tom Finegan's avatar
Tom Finegan committed
112
               "${AOM_CONFIG_DIR}/aom_config.h")
113 114
configure_file("${AOM_ROOT}/build/cmake/aom_config.asm.cmake"
               "${AOM_CONFIG_DIR}/aom_config.asm")
Tom Finegan's avatar
Tom Finegan committed
115 116 117

# Read the current git hash.
find_package(Git)
118 119
set(AOM_GIT_DESCRIPTION)
set(AOM_GIT_HASH)
Tom Finegan's avatar
Tom Finegan committed
120
if (GIT_FOUND)
121 122
  # TODO(tomfinegan): Add build rule so users don't have to re-run cmake to
  # create accurately versioned cmake builds.
Tom Finegan's avatar
Tom Finegan committed
123 124 125
  execute_process(COMMAND ${GIT_EXECUTABLE}
                  --git-dir=${AOM_ROOT}/.git rev-parse HEAD
                  OUTPUT_VARIABLE AOM_GIT_HASH)
126 127
  execute_process(COMMAND ${GIT_EXECUTABLE} --git-dir=${AOM_ROOT}/.git describe
                  OUTPUT_VARIABLE AOM_GIT_DESCRIPTION ERROR_QUIET)
Tom Finegan's avatar
Tom Finegan committed
128
  # Consume the newline at the end of the git output.
129 130 131
  string(STRIP "${AOM_GIT_HASH}" AOM_GIT_HASH)
  string(STRIP "${AOM_GIT_DESCRIPTION}" AOM_GIT_DESCRIPTION)
endif ()
Tom Finegan's avatar
Tom Finegan committed
132 133 134 135 136 137 138 139 140 141 142 143

# TODO(tomfinegan): An alternative to dumping the configure command line to
# aom_config.c is needed in cmake. Normal cmake generation runs do not make the
# command line available in the cmake script. For now, we just set the variable
# to the following. The configure_file() command will expand the message in
# aom_config.c.
# Note: This message isn't strictly true. When cmake is run in script mode (with
# the -P argument), CMAKE_ARGC and CMAKE_ARGVn are defined (n = 0 through
# n = CMAKE_ARGC become valid). Normal cmake generation runs do not make the
# information available.
set(AOM_CMAKE_CONFIG "cmake")
configure_file("${AOM_ROOT}/build/cmake/aom_config.c.cmake"
144 145
               "${AOM_CONFIG_DIR}/aom_config.c")

Tom Finegan's avatar
Tom Finegan committed
146
# Find Perl and generate the RTCD sources.
147 148 149 150
find_package(Perl)
if (NOT PERL_FOUND)
  message(FATAL_ERROR "Perl is required to build libaom.")
endif ()
Tom Finegan's avatar
Tom Finegan committed
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
configure_file(
  "${AOM_ROOT}/build/cmake/targets/rtcd_templates/${AOM_ARCH}.rtcd.cmake"
  "${AOM_CONFIG_DIR}/${AOM_ARCH}.rtcd")

set(AOM_RTCD_CONFIG_FILE_LIST
    "${AOM_ROOT}/aom_dsp/aom_dsp_rtcd_defs.pl"
    "${AOM_ROOT}/aom_scale/aom_scale_rtcd.pl"
    "${AOM_ROOT}/av1/common/av1_rtcd_defs.pl")
set(AOM_RTCD_HEADER_FILE_LIST
    "${AOM_CONFIG_DIR}/aom_dsp_rtcd.h"
    "${AOM_CONFIG_DIR}/aom_scale_rtcd.h"
    "${AOM_CONFIG_DIR}/av1_rtcd.h")
set(AOM_RTCD_SOURCE_FILE_LIST
    "${AOM_ROOT}/aom_dsp/aom_dsp_rtcd.c"
    "${AOM_ROOT}/aom_scale/aom_scale_rtcd.c"
    "${AOM_ROOT}/av1/common/av1_rtcd.c")
167
set(AOM_RTCD_SYMBOL_LIST aom_dsp_rtcd aom_scale_rtcd av1_rtcd)
Tom Finegan's avatar
Tom Finegan committed
168 169
list(LENGTH AOM_RTCD_SYMBOL_LIST AOM_RTCD_CUSTOM_COMMAND_COUNT)
math(EXPR AOM_RTCD_CUSTOM_COMMAND_COUNT "${AOM_RTCD_CUSTOM_COMMAND_COUNT} - 1")
170

Tom Finegan's avatar
Tom Finegan committed
171 172 173 174 175
foreach(NUM RANGE ${AOM_RTCD_CUSTOM_COMMAND_COUNT})
  list(GET AOM_RTCD_CONFIG_FILE_LIST ${NUM} AOM_RTCD_CONFIG_FILE)
  list(GET AOM_RTCD_HEADER_FILE_LIST ${NUM} AOM_RTCD_HEADER_FILE)
  list(GET AOM_RTCD_SOURCE_FILE_LIST ${NUM} AOM_RTCD_SOURCE_FILE)
  list(GET AOM_RTCD_SYMBOL_LIST ${NUM} AOM_RTCD_SYMBOL)
176 177 178 179 180
  execute_process(
    COMMAND ${PERL_EXECUTABLE} "${AOM_ROOT}/build/make/rtcd.pl"
      --arch=${AOM_ARCH} --sym=${AOM_RTCD_SYMBOL}
      --config=${AOM_CONFIG_DIR}/${AOM_ARCH}.rtcd ${AOM_RTCD_CONFIG_FILE}
    OUTPUT_FILE ${AOM_RTCD_HEADER_FILE})
Tom Finegan's avatar
Tom Finegan committed
181 182
endforeach()

183 184 185 186 187 188 189 190 191 192 193 194 195
function (add_rtcd_build_step config output source symbol)
  add_custom_command(
    OUTPUT ${output}
    COMMAND ${PERL_EXECUTABLE}
    ARGS "${AOM_ROOT}/build/make/rtcd.pl" --arch=${AOM_ARCH} --sym=${symbol}
      --config=${AOM_CONFIG_DIR}/${AOM_ARCH}.rtcd ${config} > ${output}
    DEPENDS ${config}
    COMMENT "Generating ${output}"
    WORKING_DIRECTORY ${AOM_CONFIG_DIR}
    VERBATIM)
  set_property(SOURCE ${source} PROPERTY OBJECT_DEPENDS ${output})
  set_property(SOURCE ${output} PROPERTY GENERATED)
endfunction ()
196 197 198 199 200 201 202 203 204

# Generate aom_version.h.
if ("${AOM_GIT_DESCRIPTION}" STREQUAL "")
  set(AOM_GIT_DESCRIPTION "${AOM_ROOT}/CHANGELOG")
endif ()
execute_process(
  COMMAND ${PERL_EXECUTABLE} "${AOM_ROOT}/build/cmake/aom_version.pl"
  --version_data=${AOM_GIT_DESCRIPTION}
  --version_filename=${AOM_CONFIG_DIR}/aom_version.h)