xref: /aosp_15_r20/external/libavc/cmake/utils.cmake (revision 495ae853bb871d1e5a258cb02c2cc13cde8ddb9a)
1*495ae853SAndroid Build Coastguard Workerinclude(CheckCXXCompilerFlag)
2*495ae853SAndroid Build Coastguard Workerset(CMAKE_C_STANDARD 90)
3*495ae853SAndroid Build Coastguard Worker
4*495ae853SAndroid Build Coastguard Worker# Adds compiler options for all targets
5*495ae853SAndroid Build Coastguard Workerfunction(libavc_add_compile_options)
6*495ae853SAndroid Build Coastguard Worker  if("${SYSTEM_PROCESSOR}" STREQUAL "aarch64" OR "${SYSTEM_PROCESSOR}" STREQUAL "arm64")
7*495ae853SAndroid Build Coastguard Worker    add_compile_options(-march=armv8-a)
8*495ae853SAndroid Build Coastguard Worker  elseif("${SYSTEM_PROCESSOR}" STREQUAL "aarch32")
9*495ae853SAndroid Build Coastguard Worker    add_compile_options(-march=armv7-a -mfpu=neon)
10*495ae853SAndroid Build Coastguard Worker  else()
11*495ae853SAndroid Build Coastguard Worker    add_compile_options(-msse4.2 -mno-avx)
12*495ae853SAndroid Build Coastguard Worker  endif()
13*495ae853SAndroid Build Coastguard Worker  add_compile_options(-Wdeclaration-after-statement)
14*495ae853SAndroid Build Coastguard Worker
15*495ae853SAndroid Build Coastguard Worker  set(CMAKE_REQUIRED_FLAGS -fsanitize=fuzzer-no-link)
16*495ae853SAndroid Build Coastguard Worker  check_cxx_compiler_flag(-fsanitize=fuzzer-no-link
17*495ae853SAndroid Build Coastguard Worker                          COMPILER_HAS_SANITIZE_FUZZER)
18*495ae853SAndroid Build Coastguard Worker  unset(CMAKE_REQUIRED_FLAGS)
19*495ae853SAndroid Build Coastguard Worker
20*495ae853SAndroid Build Coastguard Worker  if(DEFINED SANITIZE)
21*495ae853SAndroid Build Coastguard Worker    set(CMAKE_REQUIRED_FLAGS -fsanitize=${SANITIZE})
22*495ae853SAndroid Build Coastguard Worker    check_cxx_compiler_flag(-fsanitize=${SANITIZE} COMPILER_HAS_SANITIZER)
23*495ae853SAndroid Build Coastguard Worker    unset(CMAKE_REQUIRED_FLAGS)
24*495ae853SAndroid Build Coastguard Worker
25*495ae853SAndroid Build Coastguard Worker    if(NOT COMPILER_HAS_SANITIZER)
26*495ae853SAndroid Build Coastguard Worker      message(
27*495ae853SAndroid Build Coastguard Worker        FATAL_ERROR "ERROR: Compiler doesn't support -fsanitize=${SANITIZE}")
28*495ae853SAndroid Build Coastguard Worker      return()
29*495ae853SAndroid Build Coastguard Worker    endif()
30*495ae853SAndroid Build Coastguard Worker    add_compile_options(-fno-omit-frame-pointer -fsanitize=${SANITIZE})
31*495ae853SAndroid Build Coastguard Worker  endif()
32*495ae853SAndroid Build Coastguard Worker
33*495ae853SAndroid Build Coastguard Workerendfunction()
34*495ae853SAndroid Build Coastguard Worker
35*495ae853SAndroid Build Coastguard Worker# Adds defintions for all targets
36*495ae853SAndroid Build Coastguard Workerfunction(libavc_add_definitions)
37*495ae853SAndroid Build Coastguard Worker  if("${SYSTEM_NAME}" STREQUAL "Darwin")
38*495ae853SAndroid Build Coastguard Worker    if("${SYSTEM_PROCESSOR}" STREQUAL "arm64")
39*495ae853SAndroid Build Coastguard Worker      add_definitions(-DARMV8 -DDARWIN -DDEFAULT_ARCH=D_ARCH_ARMV8_GENERIC)
40*495ae853SAndroid Build Coastguard Worker    else()
41*495ae853SAndroid Build Coastguard Worker      add_definitions(-DX86 -DDARWIN -DDISABLE_AVX2 -DDEFAULT_ARCH=D_ARCH_X86_GENERIC)
42*495ae853SAndroid Build Coastguard Worker    endif()
43*495ae853SAndroid Build Coastguard Worker  elseif("${SYSTEM_PROCESSOR}" STREQUAL "aarch64")
44*495ae853SAndroid Build Coastguard Worker    add_definitions(-DARMV8 -DDEFAULT_ARCH=D_ARCH_ARMV8_GENERIC)
45*495ae853SAndroid Build Coastguard Worker  elseif("${SYSTEM_PROCESSOR}" STREQUAL "aarch32")
46*495ae853SAndroid Build Coastguard Worker    add_definitions(-DARMV7 -DDEFAULT_ARCH=D_ARCH_ARM_A9Q)
47*495ae853SAndroid Build Coastguard Worker  else()
48*495ae853SAndroid Build Coastguard Worker    add_definitions(-DX86 -DX86_LINUX=1 -DDISABLE_AVX2
49*495ae853SAndroid Build Coastguard Worker                    -DDEFAULT_ARCH=D_ARCH_X86_SSE42)
50*495ae853SAndroid Build Coastguard Worker  endif()
51*495ae853SAndroid Build Coastguard Workerendfunction()
52*495ae853SAndroid Build Coastguard Worker
53*495ae853SAndroid Build Coastguard Worker# Adds libraries needed for executables
54*495ae853SAndroid Build Coastguard Workerfunction(libavc_set_link_libraries)
55*495ae853SAndroid Build Coastguard Worker  link_libraries(Threads::Threads m)
56*495ae853SAndroid Build Coastguard Workerendfunction()
57*495ae853SAndroid Build Coastguard Worker
58*495ae853SAndroid Build Coastguard Worker# cmake-format: off
59*495ae853SAndroid Build Coastguard Worker# Adds a target for an executable
60*495ae853SAndroid Build Coastguard Worker#
61*495ae853SAndroid Build Coastguard Worker# Arguments:
62*495ae853SAndroid Build Coastguard Worker# NAME: Name of the executatble
63*495ae853SAndroid Build Coastguard Worker# LIB: Library that executable depends on
64*495ae853SAndroid Build Coastguard Worker# SOURCES: Source files
65*495ae853SAndroid Build Coastguard Worker#
66*495ae853SAndroid Build Coastguard Worker# Optional Arguments:
67*495ae853SAndroid Build Coastguard Worker# INCLUDES: Include paths
68*495ae853SAndroid Build Coastguard Worker# LIBS: Additional libraries
69*495ae853SAndroid Build Coastguard Worker# FUZZER: flag to specify if the target is a fuzzer binary
70*495ae853SAndroid Build Coastguard Worker# cmake-format: on
71*495ae853SAndroid Build Coastguard Worker
72*495ae853SAndroid Build Coastguard Workerfunction(libavc_add_executable NAME LIB)
73*495ae853SAndroid Build Coastguard Worker  set(multi_value_args SOURCES INCLUDES LIBS)
74*495ae853SAndroid Build Coastguard Worker  set(optional_args FUZZER)
75*495ae853SAndroid Build Coastguard Worker  cmake_parse_arguments(ARG "${optional_args}" "${single_value_args}"
76*495ae853SAndroid Build Coastguard Worker                        "${multi_value_args}" ${ARGN})
77*495ae853SAndroid Build Coastguard Worker
78*495ae853SAndroid Build Coastguard Worker  # Check if compiler supports -fsanitize=fuzzer. If not, skip building fuzzer
79*495ae853SAndroid Build Coastguard Worker  # binary
80*495ae853SAndroid Build Coastguard Worker  if(ARG_FUZZER)
81*495ae853SAndroid Build Coastguard Worker    if(NOT COMPILER_HAS_SANITIZE_FUZZER)
82*495ae853SAndroid Build Coastguard Worker      message("Compiler doesn't support -fsanitize=fuzzer. Skipping ${NAME}")
83*495ae853SAndroid Build Coastguard Worker      return()
84*495ae853SAndroid Build Coastguard Worker    endif()
85*495ae853SAndroid Build Coastguard Worker  endif()
86*495ae853SAndroid Build Coastguard Worker
87*495ae853SAndroid Build Coastguard Worker  add_executable(${NAME} ${ARG_SOURCES})
88*495ae853SAndroid Build Coastguard Worker  target_include_directories(${NAME} PRIVATE ${ARG_INCLUDES})
89*495ae853SAndroid Build Coastguard Worker  add_dependencies(${NAME} ${LIB} ${ARG_LIBS})
90*495ae853SAndroid Build Coastguard Worker
91*495ae853SAndroid Build Coastguard Worker  target_link_libraries(${NAME} ${LIB} ${ARG_LIBS})
92*495ae853SAndroid Build Coastguard Worker  if("${SYSTEM_NAME}" STREQUAL "Android")
93*495ae853SAndroid Build Coastguard Worker    target_link_libraries(${NAME} ${log-lib})
94*495ae853SAndroid Build Coastguard Worker  endif()
95*495ae853SAndroid Build Coastguard Worker
96*495ae853SAndroid Build Coastguard Worker  if(ARG_FUZZER)
97*495ae853SAndroid Build Coastguard Worker    target_compile_options(${NAME}
98*495ae853SAndroid Build Coastguard Worker                           PRIVATE $<$<COMPILE_LANGUAGE:CXX>:-std=c++17>)
99*495ae853SAndroid Build Coastguard Worker    if(DEFINED ENV{LIB_FUZZING_ENGINE})
100*495ae853SAndroid Build Coastguard Worker      set_target_properties(${NAME} PROPERTIES LINK_FLAGS
101*495ae853SAndroid Build Coastguard Worker                                               $ENV{LIB_FUZZING_ENGINE})
102*495ae853SAndroid Build Coastguard Worker    elseif(DEFINED SANITIZE)
103*495ae853SAndroid Build Coastguard Worker      set_target_properties(${NAME} PROPERTIES LINK_FLAGS
104*495ae853SAndroid Build Coastguard Worker                                               -fsanitize=fuzzer,${SANITIZE})
105*495ae853SAndroid Build Coastguard Worker    else()
106*495ae853SAndroid Build Coastguard Worker      set_target_properties(${NAME} PROPERTIES LINK_FLAGS -fsanitize=fuzzer)
107*495ae853SAndroid Build Coastguard Worker    endif()
108*495ae853SAndroid Build Coastguard Worker  else()
109*495ae853SAndroid Build Coastguard Worker    if(DEFINED SANITIZE)
110*495ae853SAndroid Build Coastguard Worker      set_target_properties(${NAME} PROPERTIES LINK_FLAGS
111*495ae853SAndroid Build Coastguard Worker                                               -fsanitize=${SANITIZE})
112*495ae853SAndroid Build Coastguard Worker    endif()
113*495ae853SAndroid Build Coastguard Worker  endif()
114*495ae853SAndroid Build Coastguard Workerendfunction()
115*495ae853SAndroid Build Coastguard Worker
116*495ae853SAndroid Build Coastguard Worker# cmake-format: off
117*495ae853SAndroid Build Coastguard Worker# Adds a target for a fuzzer binary
118*495ae853SAndroid Build Coastguard Worker# Calls libavc_add_executable with all arguments with FUZZER set to 1
119*495ae853SAndroid Build Coastguard Worker# Arguments:
120*495ae853SAndroid Build Coastguard Worker# Refer to libavc_add_executable's arguments
121*495ae853SAndroid Build Coastguard Worker# cmake-format: on
122*495ae853SAndroid Build Coastguard Worker
123*495ae853SAndroid Build Coastguard Workerfunction(libavc_add_fuzzer NAME LIB)
124*495ae853SAndroid Build Coastguard Worker  libavc_add_executable(${NAME} ${LIB} FUZZER 1 ${ARGV})
125*495ae853SAndroid Build Coastguard Workerendfunction()
126