aboutsummaryrefslogtreecommitdiff
path: root/cmake/utils.cmake
blob: 57b939f287a2d0bc055542ea22ee02c5ac4febf9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
include(CheckCXXCompilerFlag)

# Adds compiler options for all targets
function(libhevc_add_compile_options)
  if(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "aarch64")
    add_compile_options(-march=armv8-a)
  elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "aarch32")
    add_compile_options(-march=armv7-a -mfpu=neon)
  else()
    add_compile_options(-msse4.2 -mno-avx)
  endif()

  set(CMAKE_REQUIRED_FLAGS -fsanitize=fuzzer-no-link)
  check_cxx_compiler_flag(-fsanitize=fuzzer-no-link
                          COMPILER_HAS_SANITIZE_FUZZER)
  unset(CMAKE_REQUIRED_FLAGS)

  if(DEFINED SANITIZE)
    set(CMAKE_REQUIRED_FLAGS -fsanitize=${SANITIZE})
    check_cxx_compiler_flag(-fsanitize=${SANITIZE} COMPILER_HAS_SANITIZER)
    unset(CMAKE_REQUIRED_FLAGS)

    if(NOT COMPILER_HAS_SANITIZER)
      message(
        FATAL_ERROR "ERROR: Compiler doesn't support -fsanitize=${SANITIZE}")
      return()
    endif()
    add_compile_options(-fno-omit-frame-pointer -fsanitize=${SANITIZE})
  endif()

endfunction()

# Adds defintions for all targets
function(libhevc_add_definitions)
  add_definitions(-DPROFILE_ENABLE -DMD5_DISABLE)

  if(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "aarch64")
    add_definitions(-DARMV8 -DDEFAULT_ARCH=D_ARCH_ARMV8_GENERIC)
  elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "aarch32")
    add_definitions(-DARMV7 -DDEFAULT_ARCH=D_ARCH_ARM_A9Q)
  else()
    add_definitions(-DX86 -DX86_LINUX=1 -DDISABLE_AVX2
                    -DDEFAULT_ARCH=D_ARCH_X86_SSE42)
  endif()
endfunction()

# Adds libraries needed for executables
function(libhevc_set_link_libraries)
  link_libraries(Threads::Threads m)
endfunction()

# cmake-format: off
# Adds a target for an executable
#
# Arguments:
# NAME: Name of the executatble
# LIB: Library that executable depends on
# SOURCES: Source files
#
# Optional Arguments:
# INCLUDES: Include paths
# LIBS: Additional libraries
# FUZZER: flag to specify if the target is a fuzzer binary
# cmake-format: on

function(libhevc_add_executable NAME LIB)
  set(multi_value_args SOURCES INCLUDES LIBS)
  set(optional_args FUZZER)
  cmake_parse_arguments(ARG "${optional_args}" "${single_value_args}"
                        "${multi_value_args}" ${ARGN})

  # Check if compiler supports -fsanitize=fuzzer. If not, skip building fuzzer
  # binary
  if(ARG_FUZZER)
    if(NOT COMPILER_HAS_SANITIZE_FUZZER)
      message("Compiler doesn't support -fsanitize=fuzzer. Skipping ${NAME}")
      return()
    endif()
  endif()

  add_executable(${NAME} ${ARG_SOURCES})
  target_include_directories(${NAME} PRIVATE ${ARG_INCLUDES})
  add_dependencies(${NAME} ${LIB} ${ARG_LIBS})

  target_link_libraries(${NAME} ${LIB} ${ARG_LIBS})
  if(ARG_FUZZER)
    target_compile_options(${NAME}
                           PRIVATE $<$<COMPILE_LANGUAGE:CXX>:-std=c++17>)
    if(DEFINED ENV{LIB_FUZZING_ENGINE})
      set_target_properties(${NAME} PROPERTIES LINK_FLAGS
                                               $ENV{LIB_FUZZING_ENGINE})
    elseif(DEFINED SANITIZE)
      set_target_properties(${NAME} PROPERTIES LINK_FLAGS
                                               -fsanitize=fuzzer,${SANITIZE})
    else()
      set_target_properties(${NAME} PROPERTIES LINK_FLAGS -fsanitize=fuzzer)
    endif()
  else()
    if(DEFINED SANITIZE)
      set_target_properties(${NAME} PROPERTIES LINK_FLAGS
                                               -fsanitize=${SANITIZE})
    endif()
  endif()
endfunction()

# cmake-format: off
# Adds a target for a fuzzer binary
# Calls libhevc_add_executable with all arguments with FUZZER set to 1
# Arguments:
# Refer to libhevc_add_executable's arguments
# cmake-format: on

function(libhevc_add_fuzzer NAME LIB)
  libhevc_add_executable(${NAME} ${LIB} FUZZER 1 ${ARGV})
endfunction()