1#
2# Copyright (c) 2017, 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_OPTIMIZATION_CMAKE_)
12  return()
13endif() # AOM_BUILD_CMAKE_AOM_OPTIMIZATION_CMAKE_
14set(AOM_BUILD_CMAKE_AOM_OPTIMIZATION_CMAKE_ 1)
15
16include("${AOM_ROOT}/build/cmake/util.cmake")
17
18# Translate $flag to one which MSVC understands, and write the new flag to the
19# variable named by $translated_flag (or unset it, when MSVC needs no flag).
20function(get_msvc_intrinsic_flag flag translated_flag)
21  if("${flag}" STREQUAL "-mavx")
22    set(${translated_flag} "/arch:AVX" PARENT_SCOPE)
23  elseif("${flag}" STREQUAL "-mavx2")
24    set(${translated_flag} "/arch:AVX2" PARENT_SCOPE)
25  else()
26
27    # MSVC does not need flags for intrinsics flavors other than AVX/AVX2.
28    unset(${translated_flag} PARENT_SCOPE)
29  endif()
30endfunction()
31
32# Adds an object library target. Terminates generation if $flag is not supported
33# by the current compiler. $flag is the intrinsics flag required by the current
34# compiler, and is added to the compile flags for all sources in $sources.
35# $opt_name is used to name the target. $target_to_update is made dependent upon
36# the created target.
37#
38# Note: this function always updates the aom, and aom_static targets because
39# OBJECT libraries have rules that disallow the direct addition of .o files to
40# them as dependencies. Static and shared libraries do not have this limitation.
41function(add_intrinsics_object_library flag opt_name target_to_update sources)
42  if("${${sources}}" STREQUAL "")
43    return()
44  endif()
45  set(target_name ${target_to_update}_${opt_name}_intrinsics)
46  add_library(${target_name} OBJECT ${${sources}})
47
48  if(MSVC)
49    get_msvc_intrinsic_flag(${flag} "flag")
50  endif()
51
52  if("${flag}" STREQUAL "-mavx2")
53    unset(FLAG_SUPPORTED)
54    check_c_compiler_flag("-mno-avx256-split-unaligned-load" FLAG_SUPPORTED)
55    if(${FLAG_SUPPORTED})
56      set(flag "${flag} -mno-avx256-split-unaligned-load")
57    endif()
58
59    unset(FLAG_SUPPORTED)
60    check_c_compiler_flag("-mno-avx256-split-unaligned-store" FLAG_SUPPORTED)
61    if(${FLAG_SUPPORTED})
62      set(flag "${flag} -mno-avx256-split-unaligned-store")
63    endif()
64  endif()
65
66  if(flag)
67    separate_arguments(flag)
68    target_compile_options(${target_name} PUBLIC ${flag})
69  endif()
70
71  target_sources(aom PRIVATE $<TARGET_OBJECTS:${target_name}>)
72  if(BUILD_SHARED_LIBS)
73    target_sources(aom_static PRIVATE $<TARGET_OBJECTS:${target_name}>)
74  endif()
75
76  # Add the new lib target to the global list of aom library targets.
77  list(APPEND AOM_LIB_TARGETS ${target_name})
78  set(AOM_LIB_TARGETS ${AOM_LIB_TARGETS} PARENT_SCOPE)
79endfunction()
80
81# Adds sources in list named by $sources to $target and adds $flag to the
82# compile flags for each source file.
83function(add_intrinsics_source_to_target flag target sources)
84  target_sources(${target} PRIVATE ${${sources}})
85  if(MSVC)
86    get_msvc_intrinsic_flag(${flag} "flag")
87  endif()
88  if(flag)
89    foreach(source ${${sources}})
90      set_property(SOURCE ${source} APPEND PROPERTY COMPILE_FLAGS ${flag})
91    endforeach()
92  endif()
93endfunction()
94
95# Writes object format for the current target to the var named by $out_format,
96# or terminates the build when the object format for the current target is
97# unknown.
98function(get_asm_obj_format out_format)
99  if("${AOM_TARGET_CPU}" STREQUAL "x86_64")
100    if("${AOM_TARGET_SYSTEM}" STREQUAL "Darwin")
101      set(objformat "macho64")
102    elseif("${AOM_TARGET_SYSTEM}" STREQUAL "MSYS"
103           OR "${AOM_TARGET_SYSTEM}" STREQUAL "Windows")
104      set(objformat "win64")
105    else()
106      set(objformat "elf64")
107    endif()
108  elseif("${AOM_TARGET_CPU}" STREQUAL "x86")
109    if("${AOM_TARGET_SYSTEM}" STREQUAL "Darwin")
110      set(objformat "macho32")
111    elseif("${AOM_TARGET_SYSTEM}" STREQUAL "MSYS"
112           OR "${AOM_TARGET_SYSTEM}" STREQUAL "Windows")
113      set(objformat "win32")
114    else()
115      set(objformat "elf32")
116    endif()
117  else()
118    message(
119      FATAL_ERROR "Unknown obj format: ${AOM_TARGET_CPU}-${AOM_TARGET_SYSTEM}")
120  endif()
121
122  set(${out_format} ${objformat} PARENT_SCOPE)
123endfunction()
124
125# Adds library target named $lib_name for ASM files in variable named by
126# $asm_sources. Builds an output directory path from $lib_name. Links $lib_name
127# into the aom library target(s). Generates a dummy C file with a dummy function
128# to ensure that all cmake generators can determine the linker language, and
129# that build tools don't complain that an object exposes no symbols.
130function(add_asm_library lib_name asm_sources)
131  if("${${asm_sources}}" STREQUAL "")
132    return()
133  endif()
134  set(asm_lib_obj_dir "${AOM_CONFIG_DIR}/asm_objects/${lib_name}")
135  if(NOT EXISTS "${asm_lib_obj_dir}")
136    file(MAKE_DIRECTORY "${asm_lib_obj_dir}")
137  endif()
138
139  # TODO(tomfinegan): If cmake ever allows addition of .o files to OBJECT lib
140  # targets, make this OBJECT instead of STATIC to hide the target from
141  # consumers of the AOM cmake build.
142  add_library(${lib_name} STATIC ${${asm_sources}})
143
144  foreach(asm_source ${${asm_sources}})
145    get_filename_component(asm_source_name "${asm_source}" NAME)
146    set(asm_object "${asm_lib_obj_dir}/${asm_source_name}.o")
147    add_custom_command(OUTPUT "${asm_object}"
148                       COMMAND ${AS_EXECUTABLE} ARGS ${AOM_AS_FLAGS}
149                               -I${AOM_ROOT}/ -I${AOM_CONFIG_DIR}/ -o
150                               "${asm_object}" "${asm_source}"
151                       DEPENDS "${asm_source}"
152                       COMMENT "Building ASM object ${asm_object}"
153                       WORKING_DIRECTORY "${AOM_CONFIG_DIR}"
154                       VERBATIM)
155    target_sources(aom PRIVATE "${asm_object}")
156    if(BUILD_SHARED_LIBS)
157      target_sources(aom_static PRIVATE "${asm_object}")
158    endif()
159  endforeach()
160
161  # The above created a target containing only ASM sources. Cmake needs help
162  # here to determine the linker language. Add a dummy C file to force the
163  # linker language to C. We don't bother with setting the LINKER_LANGUAGE
164  # property on the library target because not all generators obey it (looking
165  # at you, xcode generator).
166  add_dummy_source_file_to_target("${lib_name}" "c")
167
168  # Add the new lib target to the global list of aom library targets.
169  list(APPEND AOM_LIB_TARGETS ${lib_name})
170  set(AOM_LIB_TARGETS ${AOM_LIB_TARGETS} PARENT_SCOPE)
171endfunction()
172
173# Terminates generation if nasm found in PATH does not meet requirements.
174# Currently checks only for presence of required object formats and support for
175# the -Ox argument (multipass optimization).
176function(test_nasm)
177  execute_process(COMMAND ${AS_EXECUTABLE} -hf OUTPUT_VARIABLE nasm_helptext)
178
179  if(NOT "${nasm_helptext}" MATCHES "-Ox")
180    message(
181      FATAL_ERROR "Unsupported nasm: multipass optimization not supported.")
182  endif()
183
184  if("${AOM_TARGET_CPU}" STREQUAL "x86")
185    if("${AOM_TARGET_SYSTEM}" STREQUAL "Darwin")
186      if(NOT "${nasm_helptext}" MATCHES "macho32")
187        message(
188          FATAL_ERROR "Unsupported nasm: macho32 object format not supported.")
189      endif()
190    elseif("${AOM_TARGET_SYSTEM}" STREQUAL "MSYS"
191           OR "${AOM_TARGET_SYSTEM}" STREQUAL "Windows")
192      if(NOT "${nasm_helptext}" MATCHES "win32")
193        message(
194          FATAL_ERROR "Unsupported nasm: win32 object format not supported.")
195      endif()
196    else()
197      if(NOT "${nasm_helptext}" MATCHES "elf32")
198        message(
199          FATAL_ERROR "Unsupported nasm: elf32 object format not supported.")
200      endif()
201    endif()
202  else()
203    if("${AOM_TARGET_SYSTEM}" STREQUAL "Darwin")
204      if(NOT "${nasm_helptext}" MATCHES "macho64")
205        message(
206          FATAL_ERROR "Unsupported nasm: macho64 object format not supported.")
207      endif()
208    elseif("${AOM_TARGET_SYSTEM}" STREQUAL "MSYS"
209           OR "${AOM_TARGET_SYSTEM}" STREQUAL "Windows")
210      if(NOT "${nasm_helptext}" MATCHES "win64")
211        message(
212          FATAL_ERROR "Unsupported nasm: win64 object format not supported.")
213      endif()
214    else()
215      if(NOT "${nasm_helptext}" MATCHES "elf64")
216        message(
217          FATAL_ERROR "Unsupported nasm: elf64 object format not supported.")
218      endif()
219    endif()
220  endif()
221endfunction()
222
223# Adds build command for generation of rtcd C source files using
224# build/cmake/rtcd.pl. $config is the input perl file, $output is the output C
225# include file, $source is the C source file, and $symbol is used for the symbol
226# argument passed to rtcd.pl.
227function(add_rtcd_build_step config output source symbol)
228  add_custom_command(
229    OUTPUT ${output}
230    COMMAND ${PERL_EXECUTABLE} ARGS "${AOM_ROOT}/build/cmake/rtcd.pl"
231            --arch=${AOM_TARGET_CPU}
232            --sym=${symbol} ${AOM_RTCD_FLAGS}
233            --config=${AOM_CONFIG_DIR}/config/aom_config.h ${config} > ${output}
234    DEPENDS ${config}
235    COMMENT "Generating ${output}"
236    WORKING_DIRECTORY ${AOM_CONFIG_DIR}
237    VERBATIM)
238  set_property(SOURCE ${source} PROPERTY OBJECT_DEPENDS ${output})
239  set_property(SOURCE ${output} PROPERTY GENERATED)
240endfunction()
241