Home | History | Annotate | Download | only in src
      1 cmake_minimum_required (VERSION 2.8.11)
      2 
      3 # Defer enabling C and CXX languages.
      4 project (BoringSSL NONE)
      5 
      6 if(WIN32)
      7   # On Windows, prefer cl over gcc if both are available. By default most of
      8   # the CMake generators prefer gcc, even on Windows.
      9   set(CMAKE_GENERATOR_CC cl)
     10 endif()
     11 
     12 enable_language(C)
     13 enable_language(CXX)
     14 
     15 if(ANDROID)
     16   # Android-NDK CMake files reconfigure the path and so Go and Perl won't be
     17   # found. However, ninja will still find them in $PATH if we just name them.
     18   if(NOT PERL_EXECUTABLE)
     19     set(PERL_EXECUTABLE "perl")
     20   endif()
     21   if(NOT GO_EXECUTABLE)
     22     set(GO_EXECUTABLE "go")
     23   endif()
     24 else()
     25   find_package(Perl REQUIRED)
     26   find_program(GO_EXECUTABLE go)
     27 endif()
     28 
     29 if (NOT GO_EXECUTABLE)
     30   message(FATAL_ERROR "Could not find Go")
     31 endif()
     32 
     33 if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
     34   set(C_CXX_FLAGS "-Wall -Werror -Wformat=2 -Wsign-compare -Wmissing-field-initializers -Wwrite-strings -ggdb -fvisibility=hidden -fno-common")
     35   if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
     36     set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wnewline-eof")
     37   endif()
     38   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${C_CXX_FLAGS} -Wmissing-prototypes -Wold-style-definition -Wstrict-prototypes")
     39   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 ${C_CXX_FLAGS} -Wmissing-declarations")
     40   # Clang's integerated assembler does not support debug symbols.
     41   if(NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang")
     42     set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -Wa,-g")
     43   endif()
     44 elseif(MSVC)
     45   set(MSVC_DISABLED_WARNINGS_LIST
     46       "C4061" # enumerator 'identifier' in switch of enum 'enumeration' is not
     47               # explicitly handled by a case label
     48               # Disable this because it flags even when there is a default.
     49       "C4100" # 'exarg' : unreferenced formal parameter
     50       "C4127" # conditional expression is constant
     51       "C4200" # nonstandard extension used : zero-sized array in
     52               # struct/union.
     53       "C4204" # nonstandard extension used: non-constant aggregate initializer
     54       "C4221" # nonstandard extension used : 'identifier' : cannot be
     55               # initialized using address of automatic variable
     56       "C4242" # 'function' : conversion from 'int' to 'uint8_t',
     57               # possible loss of data
     58       "C4244" # 'function' : conversion from 'int' to 'uint8_t',
     59               # possible loss of data
     60       "C4245" # 'initializing' : conversion from 'long' to
     61               # 'unsigned long', signed/unsigned mismatch
     62       "C4267" # conversion from 'size_t' to 'int', possible loss of data
     63       "C4371" # layout of class may have changed from a previous version of the
     64               # compiler due to better packing of member '...'
     65       "C4388" # signed/unsigned mismatch
     66       "C4296" # '>=' : expression is always true
     67       "C4350" # behavior change: 'std::_Wrap_alloc...'
     68       "C4365" # '=' : conversion from 'size_t' to 'int',
     69               # signed/unsigned mismatch
     70       "C4389" # '!=' : signed/unsigned mismatch
     71       "C4464" # relative include path contains '..'
     72       "C4510" # 'argument' : default constructor could not be generated
     73       "C4512" # 'argument' : assignment operator could not be generated
     74       "C4514" # 'function': unreferenced inline function has been removed
     75       "C4548" # expression before comma has no effect; expected expression with
     76               # side-effect" caused by FD_* macros.
     77       "C4610" # struct 'argument' can never be instantiated - user defined
     78               # constructor required.
     79       "C4623" # default constructor was implicitly defined as deleted
     80       "C4625" # copy constructor could not be generated because a base class
     81               # copy constructor is inaccessible or deleted
     82       "C4626" # assignment operator could not be generated because a base class
     83               # assignment operator is inaccessible or deleted
     84       "C4668" # 'symbol' is not defined as a preprocessor macro, replacing with
     85               # '0' for 'directives'
     86               # Disable this because GTest uses it everywhere.
     87       "C4706" # assignment within conditional expression
     88       "C4710" # 'function': function not inlined
     89       "C4711" # function 'function' selected for inline expansion
     90       "C4800" # 'int' : forcing value to bool 'true' or 'false'
     91               # (performance warning)
     92       "C4820" # 'bytes' bytes padding added after construct 'member_name'
     93       "C5026" # move constructor was implicitly defined as deleted
     94       "C5027" # move assignment operator was implicitly defined as deleted
     95       )
     96   set(MSVC_LEVEL4_WARNINGS_LIST
     97       # See https://connect.microsoft.com/VisualStudio/feedback/details/1217660/warning-c4265-when-using-functional-header
     98       "C4265" # class has virtual functions, but destructor is not virtual
     99       )
    100   string(REPLACE "C" " -wd" MSVC_DISABLED_WARNINGS_STR
    101                             ${MSVC_DISABLED_WARNINGS_LIST})
    102   string(REPLACE "C" " -w4" MSVC_LEVEL4_WARNINGS_STR
    103                             ${MSVC_LEVEL4_WARNINGS_LIST})
    104   set(CMAKE_C_FLAGS   "-Wall -WX ${MSVC_DISABLED_WARNINGS_STR} ${MSVC_LEVEL4_WARNINGS_STR}")
    105   set(CMAKE_CXX_FLAGS "-Wall -WX ${MSVC_DISABLED_WARNINGS_STR} ${MSVC_LEVEL4_WARNINGS_STR}")
    106   set(CMAKE_ASM_NASM_FLAGS "-g cv8")
    107   add_definitions(-D_HAS_EXCEPTIONS=0)
    108   add_definitions(-DWIN32_LEAN_AND_MEAN)
    109   add_definitions(-DNOMINMAX)
    110   add_definitions(-D_CRT_SECURE_NO_WARNINGS) # Allow use of fopen
    111 endif()
    112 
    113 if((CMAKE_COMPILER_IS_GNUCXX AND CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.7.99") OR
    114    CMAKE_CXX_COMPILER_ID MATCHES "Clang")
    115   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wshadow")
    116   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wshadow")
    117 endif()
    118 
    119 if(CMAKE_COMPILER_IS_GNUCXX)
    120   if ((CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.8.99") OR
    121       CMAKE_CXX_COMPILER_ID MATCHES "Clang")
    122     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c11")
    123   else()
    124     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99")
    125   endif()
    126 endif()
    127 
    128 # pthread_rwlock_t requires a feature flag.
    129 if(NOT WIN32)
    130   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_XOPEN_SOURCE=700")
    131 endif()
    132 
    133 if(FUZZ)
    134   if(NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang")
    135     message(FATAL_ERROR "You need to build with Clang for fuzzing to work")
    136   endif()
    137 
    138   add_definitions(-DBORINGSSL_UNSAFE_DETERMINISTIC_MODE)
    139   set(RUNNER_ARGS "-deterministic")
    140 
    141   if(NOT NO_FUZZER_MODE)
    142     add_definitions(-DBORINGSSL_UNSAFE_FUZZER_MODE)
    143     set(RUNNER_ARGS ${RUNNER_ARGS} "-fuzzer" "-shim-config" "fuzzer_mode.json")
    144   endif()
    145 
    146   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -fsanitize-coverage=edge,indirect-calls,8bit-counters")
    147   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fsanitize-coverage=edge,indirect-calls,8bit-counters")
    148   set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address")
    149   link_directories(.)
    150 endif()
    151 
    152 add_definitions(-DBORINGSSL_IMPLEMENTATION)
    153 
    154 if (BUILD_SHARED_LIBS)
    155   add_definitions(-DBORINGSSL_SHARED_LIBRARY)
    156   # Enable position-independent code globally. This is needed because
    157   # some library targets are OBJECT libraries.
    158   set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
    159 endif()
    160 
    161 if (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86_64")
    162   set(ARCH "x86_64")
    163 elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "amd64")
    164   set(ARCH "x86_64")
    165 elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "AMD64")
    166   # cmake reports AMD64 on Windows, but we might be building for 32-bit.
    167   if (CMAKE_CL_64)
    168     set(ARCH "x86_64")
    169   else()
    170     set(ARCH "x86")
    171   endif()
    172 elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86")
    173   set(ARCH "x86")
    174 elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "i386")
    175   set(ARCH "x86")
    176 elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "i686")
    177   set(ARCH "x86")
    178 elseif (${CMAKE_SYSTEM_PROCESSOR} MATCHES "^arm*")
    179   set(ARCH "arm")
    180 elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "aarch64")
    181   set(ARCH "aarch64")
    182 elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "mips")
    183   # Just to avoid the unknown processor error.
    184   set(ARCH "generic")
    185 elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "ppc64le")
    186   set(ARCH "ppc64le")
    187 else()
    188   message(FATAL_ERROR "Unknown processor:" ${CMAKE_SYSTEM_PROCESSOR})
    189 endif()
    190 
    191 if (ANDROID AND ${ARCH} STREQUAL "arm")
    192   # The Android-NDK CMake files somehow fail to set the -march flag for
    193   # assembly files. Without this flag, the compiler believes that it's
    194   # building for ARMv5.
    195   set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -march=${CMAKE_SYSTEM_PROCESSOR}")
    196 endif()
    197 
    198 if (${ARCH} STREQUAL "x86" AND APPLE)
    199   # With CMake 2.8.x, ${CMAKE_SYSTEM_PROCESSOR} evalutes to i386 on OS X,
    200   # but clang defaults to 64-bit builds on OS X unless otherwise told.
    201   # Set ARCH to x86_64 so clang and CMake agree. This is fixed in CMake 3.
    202   set(ARCH "x86_64")
    203 endif()
    204 
    205 if (MSAN)
    206   if(NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang")
    207     message(FATAL_ERROR "Cannot enable MSAN unless using Clang")
    208   endif()
    209 
    210   if (ASAN)
    211     message(FATAL_ERROR "ASAN and MSAN are mutually exclusive")
    212   endif()
    213 
    214   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=memory -fsanitize-memory-track-origins -fno-omit-frame-pointer")
    215   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=memory -fsanitize-memory-track-origins -fno-omit-frame-pointer")
    216   set(OPENSSL_NO_ASM "1")
    217 endif()
    218 
    219 if (ASAN)
    220   if(NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang")
    221     message(FATAL_ERROR "Cannot enable ASAN unless using Clang")
    222   endif()
    223 
    224   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -fsanitize-address-use-after-scope -fno-omit-frame-pointer")
    225   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fsanitize-address-use-after-scope -fno-omit-frame-pointer")
    226   set(OPENSSL_NO_ASM "1")
    227 endif()
    228 
    229 if (GCOV)
    230   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
    231   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
    232 endif()
    233 
    234 if (OPENSSL_NO_ASM)
    235   add_definitions(-DOPENSSL_NO_ASM)
    236   set(ARCH "generic")
    237 endif()
    238 
    239 # Add minimal googletest targets. The provided one has many side-effects, and
    240 # googletest has a very straightforward build.
    241 add_library(gtest third_party/googletest/src/gtest-all.cc)
    242 target_include_directories(gtest PRIVATE third_party/googletest)
    243 
    244 include_directories(third_party/googletest/include)
    245 
    246 # Declare a dummy target to build all unit tests. Test targets should inject
    247 # themselves as dependencies next to the target definition.
    248 add_custom_target(all_tests)
    249 
    250 add_subdirectory(crypto)
    251 add_subdirectory(ssl)
    252 add_subdirectory(ssl/test)
    253 add_subdirectory(tool)
    254 add_subdirectory(decrepit)
    255 
    256 if(FUZZ)
    257   add_subdirectory(fuzz)
    258 endif()
    259 
    260 if (NOT ${CMAKE_VERSION} VERSION_LESS "3.2")
    261   # USES_TERMINAL is only available in CMake 3.2 or later.
    262   set(MAYBE_USES_TERMINAL USES_TERMINAL)
    263 endif()
    264 
    265 add_custom_target(
    266     run_tests
    267     COMMAND ${GO_EXECUTABLE} run util/all_tests.go -build-dir
    268             ${CMAKE_BINARY_DIR}
    269     COMMAND cd ssl/test/runner &&
    270             ${GO_EXECUTABLE} test -shim-path $<TARGET_FILE:bssl_shim>
    271               ${RUNNER_ARGS}
    272     WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
    273     DEPENDS all_tests bssl_shim
    274     ${MAYBE_USES_TERMINAL})
    275