xref: /aosp_15_r20/external/libxaac/cmake/utils.cmake (revision 15dc779a375ca8b5125643b829a8aa4b70d7f451)
1*15dc779aSAndroid Build Coastguard Workerinclude(CheckCXXCompilerFlag)
2*15dc779aSAndroid Build Coastguard Workerset(CMAKE_C_STANDARD 99)
3*15dc779aSAndroid Build Coastguard Worker# Adds compiler options for all targets
4*15dc779aSAndroid Build Coastguard Workerfunction(libxaac_add_compile_options)
5*15dc779aSAndroid Build Coastguard Worker  if(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "aarch64")
6*15dc779aSAndroid Build Coastguard Worker    add_compile_options(-std=gnu99 -march=armv8-a)
7*15dc779aSAndroid Build Coastguard Worker  elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "aarch32")
8*15dc779aSAndroid Build Coastguard Worker    add_compile_options(
9*15dc779aSAndroid Build Coastguard Worker      -O3
10*15dc779aSAndroid Build Coastguard Worker      -Wall
11*15dc779aSAndroid Build Coastguard Worker      -std=c99
12*15dc779aSAndroid Build Coastguard Worker      -mcpu=cortex-a8
13*15dc779aSAndroid Build Coastguard Worker      -march=armv7-a
14*15dc779aSAndroid Build Coastguard Worker      -mfloat-abi=softfp
15*15dc779aSAndroid Build Coastguard Worker      -mfpu=neon)
16*15dc779aSAndroid Build Coastguard Worker  elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86_64")
17*15dc779aSAndroid Build Coastguard Worker    add_compile_options(-D_X86_ -O3 -Wall -Wsequence-point -Wunused-function -fwrapv)
18*15dc779aSAndroid Build Coastguard Worker  elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "i686")
19*15dc779aSAndroid Build Coastguard Worker    add_compile_options(-D_X86_ -O3 -Wall -Wsequence-point -Wunused-function -fwrapv -m32)
20*15dc779aSAndroid Build Coastguard Worker  endif()
21*15dc779aSAndroid Build Coastguard Worker
22*15dc779aSAndroid Build Coastguard Worker  set(CMAKE_REQUIRED_FLAGS -fsanitize=fuzzer-no-link)
23*15dc779aSAndroid Build Coastguard Worker  check_cxx_compiler_flag(-fsanitize=fuzzer-no-link
24*15dc779aSAndroid Build Coastguard Worker                          COMPILER_HAS_SANITIZE_FUZZER)
25*15dc779aSAndroid Build Coastguard Worker  unset(CMAKE_REQUIRED_FLAGS)
26*15dc779aSAndroid Build Coastguard Worker
27*15dc779aSAndroid Build Coastguard Worker  if(DEFINED SANITIZE)
28*15dc779aSAndroid Build Coastguard Worker    set(CMAKE_REQUIRED_FLAGS -fsanitize=${SANITIZE})
29*15dc779aSAndroid Build Coastguard Worker    check_cxx_compiler_flag(-fsanitize=${SANITIZE} COMPILER_HAS_SANITIZER)
30*15dc779aSAndroid Build Coastguard Worker    unset(CMAKE_REQUIRED_FLAGS)
31*15dc779aSAndroid Build Coastguard Worker
32*15dc779aSAndroid Build Coastguard Worker    if(NOT COMPILER_HAS_SANITIZER)
33*15dc779aSAndroid Build Coastguard Worker      message(
34*15dc779aSAndroid Build Coastguard Worker        FATAL_ERROR "ERROR: Compiler doesn't support -fsanitize=${SANITIZE}")
35*15dc779aSAndroid Build Coastguard Worker      return()
36*15dc779aSAndroid Build Coastguard Worker    endif()
37*15dc779aSAndroid Build Coastguard Worker    add_compile_options(-fno-omit-frame-pointer -fsanitize=${SANITIZE})
38*15dc779aSAndroid Build Coastguard Worker  endif()
39*15dc779aSAndroid Build Coastguard Worker
40*15dc779aSAndroid Build Coastguard Workerendfunction()
41*15dc779aSAndroid Build Coastguard Worker
42*15dc779aSAndroid Build Coastguard Worker# Adds defintions for all targets
43*15dc779aSAndroid Build Coastguard Workerfunction(libxaac_add_definitions)
44*15dc779aSAndroid Build Coastguard Worker  if(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "aarch64")
45*15dc779aSAndroid Build Coastguard Worker    add_definitions(-DARMV8)
46*15dc779aSAndroid Build Coastguard Worker  elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "aarch32")
47*15dc779aSAndroid Build Coastguard Worker    add_definitions(-DARMV7)
48*15dc779aSAndroid Build Coastguard Worker  elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "i686")
49*15dc779aSAndroid Build Coastguard Worker    add_definitions(-DX86 -D_X86_)
50*15dc779aSAndroid Build Coastguard Worker  elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86_64")
51*15dc779aSAndroid Build Coastguard Worker    add_definitions(-DX86_64 -D_X86_64_)
52*15dc779aSAndroid Build Coastguard Worker  endif()
53*15dc779aSAndroid Build Coastguard Workerendfunction()
54*15dc779aSAndroid Build Coastguard Worker
55*15dc779aSAndroid Build Coastguard Worker# Adds libraries needed for executables
56*15dc779aSAndroid Build Coastguard Workerfunction(libxaac_set_link_libraries)
57*15dc779aSAndroid Build Coastguard Worker  if(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "aarch32")
58*15dc779aSAndroid Build Coastguard Worker    link_libraries(Threads::Threads --static)
59*15dc779aSAndroid Build Coastguard Worker  endif()
60*15dc779aSAndroid Build Coastguard Workerendfunction()
61*15dc779aSAndroid Build Coastguard Worker
62*15dc779aSAndroid Build Coastguard Worker# cmake-format: off
63*15dc779aSAndroid Build Coastguard Worker# Adds a target for an executable
64*15dc779aSAndroid Build Coastguard Worker#
65*15dc779aSAndroid Build Coastguard Worker# Arguments:
66*15dc779aSAndroid Build Coastguard Worker# NAME: Name of the executatble
67*15dc779aSAndroid Build Coastguard Worker# LIB: Library that executable depends on
68*15dc779aSAndroid Build Coastguard Worker# SOURCES: Source files
69*15dc779aSAndroid Build Coastguard Worker#
70*15dc779aSAndroid Build Coastguard Worker# Optional Arguments:
71*15dc779aSAndroid Build Coastguard Worker# INCLUDES: Include paths
72*15dc779aSAndroid Build Coastguard Worker# LIBS: Additional libraries
73*15dc779aSAndroid Build Coastguard Worker# FUZZER: flag to specify if the target is a fuzzer binary
74*15dc779aSAndroid Build Coastguard Worker# cmake-format: on
75*15dc779aSAndroid Build Coastguard Worker
76*15dc779aSAndroid Build Coastguard Workerfunction(libxaac_add_executable NAME LIB)
77*15dc779aSAndroid Build Coastguard Worker  set(multi_value_args SOURCES INCLUDES LIBS)
78*15dc779aSAndroid Build Coastguard Worker  set(optional_args FUZZER)
79*15dc779aSAndroid Build Coastguard Worker  cmake_parse_arguments(ARG "${optional_args}" "${single_value_args}"
80*15dc779aSAndroid Build Coastguard Worker                        "${multi_value_args}" ${ARGN})
81*15dc779aSAndroid Build Coastguard Worker
82*15dc779aSAndroid Build Coastguard Worker  # Check if compiler supports -fsanitize=fuzzer. If not, skip building fuzzer
83*15dc779aSAndroid Build Coastguard Worker  # binary
84*15dc779aSAndroid Build Coastguard Worker  if(ARG_FUZZER)
85*15dc779aSAndroid Build Coastguard Worker    if(NOT COMPILER_HAS_SANITIZE_FUZZER)
86*15dc779aSAndroid Build Coastguard Worker      message("Compiler doesn't support -fsanitize=fuzzer. Skipping ${NAME}")
87*15dc779aSAndroid Build Coastguard Worker      return()
88*15dc779aSAndroid Build Coastguard Worker    endif()
89*15dc779aSAndroid Build Coastguard Worker  endif()
90*15dc779aSAndroid Build Coastguard Worker
91*15dc779aSAndroid Build Coastguard Worker  add_executable(${NAME} ${ARG_SOURCES})
92*15dc779aSAndroid Build Coastguard Worker  target_include_directories(${NAME} PRIVATE ${ARG_INCLUDES})
93*15dc779aSAndroid Build Coastguard Worker  add_dependencies(${NAME} ${LIB} ${ARG_LIBS})
94*15dc779aSAndroid Build Coastguard Worker
95*15dc779aSAndroid Build Coastguard Worker  if(NOT MSVC)
96*15dc779aSAndroid Build Coastguard Worker    target_link_libraries(${NAME} ${LIB} ${ARG_LIBS} m)
97*15dc779aSAndroid Build Coastguard Worker  else()
98*15dc779aSAndroid Build Coastguard Worker    target_link_libraries(${NAME} ${LIB} ${ARG_LIBS})
99*15dc779aSAndroid Build Coastguard Worker  endif()
100*15dc779aSAndroid Build Coastguard Worker
101*15dc779aSAndroid Build Coastguard Worker  if(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "i686")
102*15dc779aSAndroid Build Coastguard Worker	set_target_properties(${NAME}  PROPERTIES LINK_FLAGS "-m32")
103*15dc779aSAndroid Build Coastguard Worker  endif()
104*15dc779aSAndroid Build Coastguard Worker
105*15dc779aSAndroid Build Coastguard Worker  if(ARG_FUZZER)
106*15dc779aSAndroid Build Coastguard Worker    target_compile_options(${NAME}
107*15dc779aSAndroid Build Coastguard Worker                           PRIVATE $<$<COMPILE_LANGUAGE:CXX>:-std=c++17>)
108*15dc779aSAndroid Build Coastguard Worker    if(DEFINED ENV{LIB_FUZZING_ENGINE})
109*15dc779aSAndroid Build Coastguard Worker      set_target_properties(${NAME} PROPERTIES LINK_FLAGS
110*15dc779aSAndroid Build Coastguard Worker                                               $ENV{LIB_FUZZING_ENGINE})
111*15dc779aSAndroid Build Coastguard Worker    elseif(DEFINED SANITIZE)
112*15dc779aSAndroid Build Coastguard Worker      set_target_properties(${NAME} PROPERTIES LINK_FLAGS
113*15dc779aSAndroid Build Coastguard Worker                                               -fsanitize=fuzzer,${SANITIZE})
114*15dc779aSAndroid Build Coastguard Worker    else()
115*15dc779aSAndroid Build Coastguard Worker      set_target_properties(${NAME} PROPERTIES LINK_FLAGS -fsanitize=fuzzer)
116*15dc779aSAndroid Build Coastguard Worker    endif()
117*15dc779aSAndroid Build Coastguard Worker  else()
118*15dc779aSAndroid Build Coastguard Worker    if(DEFINED SANITIZE)
119*15dc779aSAndroid Build Coastguard Worker      set_target_properties(${NAME} PROPERTIES LINK_FLAGS
120*15dc779aSAndroid Build Coastguard Worker                                               -fsanitize=${SANITIZE})
121*15dc779aSAndroid Build Coastguard Worker    endif()
122*15dc779aSAndroid Build Coastguard Worker  endif()
123*15dc779aSAndroid Build Coastguard Workerendfunction()
124*15dc779aSAndroid Build Coastguard Worker
125*15dc779aSAndroid Build Coastguard Worker# cmake-format: off
126*15dc779aSAndroid Build Coastguard Worker# Adds a target for a fuzzer binary
127*15dc779aSAndroid Build Coastguard Worker# Calls libxaac_add_executable with all arguments with FUZZER set to 1
128*15dc779aSAndroid Build Coastguard Worker# Arguments:
129*15dc779aSAndroid Build Coastguard Worker# Refer to libxaac_add_executable's arguments
130*15dc779aSAndroid Build Coastguard Worker# cmake-format: on
131*15dc779aSAndroid Build Coastguard Worker
132*15dc779aSAndroid Build Coastguard Workerfunction(libxaac_add_fuzzer NAME LIB)
133*15dc779aSAndroid Build Coastguard Worker  libxaac_add_executable(${NAME} ${LIB} FUZZER 1 ${ARGV})
134*15dc779aSAndroid Build Coastguard Workerendfunction()
135