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