blob: 3d1ea78d8e55dba4d37434b1a68ca478934c9b36 [file] [log] [blame]
Adam Langley07100c62015-01-16 15:20:54 -08001cmake_minimum_required (VERSION 2.8.10)
Adam Langley95c29f32014-06-20 12:00:00 -07002
David Benjamin6b34d542016-02-05 21:58:39 -05003# Defer enabling C and CXX languages.
4project (BoringSSL NONE)
5
6if(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)
10endif()
11
12enable_language(C)
13enable_language(CXX)
Adam Langley95c29f32014-06-20 12:00:00 -070014
Adam Langley843ab662015-04-28 17:46:58 -070015if(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.
Tamas Berghammer5693e422016-05-19 14:28:14 +010018 if(NOT PERL_EXECUTABLE)
19 set(PERL_EXECUTABLE "perl")
20 endif()
21 if(NOT GO_EXECUTABLE)
22 set(GO_EXECUTABLE "go")
23 endif()
Adam Langley843ab662015-04-28 17:46:58 -070024else()
25 find_package(Perl REQUIRED)
26 find_program(GO_EXECUTABLE go)
27endif()
David Benjamin3ce3c362015-02-23 13:06:19 -050028
David Benjamind27eda02015-03-05 01:19:27 -050029if (NOT GO_EXECUTABLE)
30 message(FATAL_ERROR "Could not find Go")
31endif()
32
Brian Smith1d75c8b2015-01-23 17:25:44 -080033if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
Adam Langley01867872016-06-30 14:16:59 -070034 set(C_CXX_FLAGS "-Wall -Werror -Wformat=2 -Wsign-compare -Wmissing-field-initializers -Wwrite-strings -ggdb -fvisibility=hidden -fno-common")
David Benjamin9b7d8362016-08-29 14:59:31 -040035 if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
36 set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wnewline-eof")
37 endif()
Adam Langley01867872016-06-30 14:16:59 -070038 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${C_CXX_FLAGS} -Wmissing-prototypes -Wold-style-definition -Wstrict-prototypes")
David Benjamin37e01b32016-06-13 13:42:04 -040039 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 ${C_CXX_FLAGS} -Wmissing-declarations")
David Benjamin41b8ff22016-09-16 15:56:53 -040040 # 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()
Adam Langley95c29f32014-06-20 12:00:00 -070044elseif(MSVC)
Brian Smithefed2212015-01-28 16:20:02 -080045 set(MSVC_DISABLED_WARNINGS_LIST
46 "C4100" # 'exarg' : unreferenced formal parameter
47 "C4127" # conditional expression is constant
48 "C4200" # nonstandard extension used : zero-sized array in
49 # struct/union.
50 "C4242" # 'function' : conversion from 'int' to 'uint8_t',
51 # possible loss of data
52 "C4244" # 'function' : conversion from 'int' to 'uint8_t',
53 # possible loss of data
54 "C4245" # 'initializing' : conversion from 'long' to
55 # 'unsigned long', signed/unsigned mismatch
David Benjamin3673be72015-02-11 15:12:05 -050056 "C4267" # conversion from 'size_t' to 'int', possible loss of data
David Benjamin3673be72015-02-11 15:12:05 -050057 "C4371" # layout of class may have changed from a previous version of the
58 # compiler due to better packing of member '...'
59 "C4388" # signed/unsigned mismatch
Brian Smithefed2212015-01-28 16:20:02 -080060 "C4296" # '>=' : expression is always true
61 "C4350" # behavior change: 'std::_Wrap_alloc...'
62 "C4365" # '=' : conversion from 'size_t' to 'int',
63 # signed/unsigned mismatch
64 "C4389" # '!=' : signed/unsigned mismatch
David Benjamin7acd6bc2016-05-02 12:57:01 -040065 "C4464" # relative include path contains '..'
Brian Smithefed2212015-01-28 16:20:02 -080066 "C4510" # 'argument' : default constructor could not be generated
67 "C4512" # 'argument' : assignment operator could not be generated
68 "C4514" # 'function': unreferenced inline function has been removed
69 "C4548" # expression before comma has no effect; expected expression with
70 # side-effect" caused by FD_* macros.
71 "C4610" # struct 'argument' can never be instantiated - user defined
72 # constructor required.
David Benjamin7acd6bc2016-05-02 12:57:01 -040073 "C4623" # default constructor was implicitly defined as deleted
David Benjamin3673be72015-02-11 15:12:05 -050074 "C4625" # copy constructor could not be generated because a base class
75 # copy constructor is inaccessible or deleted
76 "C4626" # assignment operator could not be generated because a base class
77 # assignment operator is inaccessible or deleted
Brian Smithefed2212015-01-28 16:20:02 -080078 "C4706" # assignment within conditional expression
79 "C4710" # 'function': function not inlined
80 "C4711" # function 'function' selected for inline expansion
81 "C4800" # 'int' : forcing value to bool 'true' or 'false'
82 # (performance warning)
83 "C4820" # 'bytes' bytes padding added after construct 'member_name'
David Benjamin7acd6bc2016-05-02 12:57:01 -040084 "C5027" # move assignment operator was implicitly defined as deleted
85 )
86 set(MSVC_LEVEL4_WARNINGS_LIST
87 # See https://connect.microsoft.com/VisualStudio/feedback/details/1217660/warning-c4265-when-using-functional-header
88 "C4265" # class has virtual functions, but destructor is not virtual
89 )
Brian Smithefed2212015-01-28 16:20:02 -080090 string(REPLACE "C" " -wd" MSVC_DISABLED_WARNINGS_STR
91 ${MSVC_DISABLED_WARNINGS_LIST})
David Benjamin7acd6bc2016-05-02 12:57:01 -040092 string(REPLACE "C" " -w4" MSVC_LEVEL4_WARNINGS_STR
93 ${MSVC_LEVEL4_WARNINGS_LIST})
Brian Smithdc6c1b82016-01-17 22:21:42 -100094 set(CMAKE_C_FLAGS "-Wall -WX ${MSVC_DISABLED_WARNINGS_STR} ${MSVC_LEVEL4_WARNINGS_STR}")
95 set(CMAKE_CXX_FLAGS "-Wall -WX ${MSVC_DISABLED_WARNINGS_STR} ${MSVC_LEVEL4_WARNINGS_STR}")
David Benjamin72088222016-08-04 19:09:45 -040096 set(CMAKE_ASM_NASM_FLAGS "-g cv8")
Adam Langley4a0f0c42015-01-28 16:37:10 -080097 add_definitions(-D_HAS_EXCEPTIONS=0)
Brian Smitha87de9b2015-01-28 20:34:47 -080098 add_definitions(-DWIN32_LEAN_AND_MEAN)
David Benjamin0e434b92015-04-02 13:20:01 -040099 add_definitions(-DNOMINMAX)
nmittlerf0322b22016-05-19 08:49:59 -0700100 add_definitions(-D_CRT_SECURE_NO_WARNINGS) # Allow use of fopen
Adam Langley95c29f32014-06-20 12:00:00 -0700101endif()
102
David Benjaminb826c0d2015-02-28 00:00:44 -0500103if((CMAKE_COMPILER_IS_GNUCXX AND CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.7.99") OR
Brian Smith1d75c8b2015-01-23 17:25:44 -0800104 CMAKE_CXX_COMPILER_ID MATCHES "Clang")
Adam Langley4a0f0c42015-01-28 16:37:10 -0800105 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wshadow")
106 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wshadow")
Adam Langley07100c62015-01-16 15:20:54 -0800107endif()
108
David Benjamin2e8ba2d2016-06-09 16:22:26 -0400109if(CMAKE_COMPILER_IS_GNUCXX)
110 if ((CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.8.99") OR
111 CMAKE_CXX_COMPILER_ID MATCHES "Clang")
112 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c11")
113 else()
114 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99")
115 endif()
116endif()
117
118# pthread_rwlock_t requires a feature flag.
119if(NOT WIN32)
120 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_XOPEN_SOURCE=700")
Adam Langley6f2e7332015-05-15 12:01:29 -0700121endif()
122
Adam Langley9a4beb82015-11-09 13:57:26 -0800123if(FUZZ)
Alessandro Ghedinib6f69272016-09-28 22:14:01 +0100124 if(NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang")
125 message(FATAL_ERROR "You need to build with Clang for fuzzing to work")
Adam Langley9a4beb82015-11-09 13:57:26 -0800126 endif()
127
David Benjaminbc5b2a22016-03-01 22:57:32 -0500128 add_definitions(-DBORINGSSL_UNSAFE_FUZZER_MODE)
David Benjaminf2b83632016-03-01 22:57:46 -0500129 set(RUNNER_ARGS "-fuzzer")
David Benjaminbc5b2a22016-03-01 22:57:32 -0500130
Adam Langleyde29f362016-03-03 09:50:10 -0800131 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -fsanitize-coverage=edge,indirect-calls,8bit-counters")
132 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fsanitize-coverage=edge,indirect-calls,8bit-counters")
Adam Langley9a4beb82015-11-09 13:57:26 -0800133 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address")
Adam Langley7104cc92015-11-10 15:00:51 -0800134 link_directories(.)
Adam Langley9a4beb82015-11-09 13:57:26 -0800135endif()
136
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700137add_definitions(-DBORINGSSL_IMPLEMENTATION)
138
David Benjamin507c1ee2015-01-28 00:50:21 -0500139if (BUILD_SHARED_LIBS)
Adam Langley4a0f0c42015-01-28 16:37:10 -0800140 add_definitions(-DBORINGSSL_SHARED_LIBRARY)
141 # Enable position-independent code globally. This is needed because
142 # some library targets are OBJECT libraries.
143 set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
David Benjamin507c1ee2015-01-28 00:50:21 -0500144endif()
145
Adam Langley95c29f32014-06-20 12:00:00 -0700146if (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86_64")
Adam Langley4a0f0c42015-01-28 16:37:10 -0800147 set(ARCH "x86_64")
Piotr Sikora1d8adf12014-07-31 03:09:49 -0700148elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "amd64")
Adam Langley4a0f0c42015-01-28 16:37:10 -0800149 set(ARCH "x86_64")
Adam Langley95c29f32014-06-20 12:00:00 -0700150elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "AMD64")
Adam Langley4a0f0c42015-01-28 16:37:10 -0800151 # cmake reports AMD64 on Windows, but we might be building for 32-bit.
152 if (CMAKE_CL_64)
153 set(ARCH "x86_64")
154 else()
155 set(ARCH "x86")
156 endif()
Adam Langley95c29f32014-06-20 12:00:00 -0700157elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86")
Adam Langley4a0f0c42015-01-28 16:37:10 -0800158 set(ARCH "x86")
Piotr Sikora1d8adf12014-07-31 03:09:49 -0700159elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "i386")
Adam Langley4a0f0c42015-01-28 16:37:10 -0800160 set(ARCH "x86")
Lukas Tribusd83f38c2014-08-11 13:27:44 -0700161elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "i686")
Adam Langley4a0f0c42015-01-28 16:37:10 -0800162 set(ARCH "x86")
Adam Langley95c29f32014-06-20 12:00:00 -0700163elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "arm")
Adam Langley4a0f0c42015-01-28 16:37:10 -0800164 set(ARCH "arm")
Joel Klinghed1550a842015-05-29 15:06:45 +0200165elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "armv6")
166 set(ARCH "arm")
Adam Langley843ab662015-04-28 17:46:58 -0700167elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "armv7-a")
168 set(ARCH "arm")
Adam Langley3e652652015-01-09 15:44:37 -0800169elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "aarch64")
Adam Langley4a0f0c42015-01-28 16:37:10 -0800170 set(ARCH "aarch64")
Adam Langley95c29f32014-06-20 12:00:00 -0700171else()
Adam Langley4a0f0c42015-01-28 16:37:10 -0800172 message(FATAL_ERROR "Unknown processor:" ${CMAKE_SYSTEM_PROCESSOR})
Adam Langley95c29f32014-06-20 12:00:00 -0700173endif()
174
Adam Langley843ab662015-04-28 17:46:58 -0700175if (ANDROID AND ${ARCH} STREQUAL "arm")
176 # The Android-NDK CMake files somehow fail to set the -march flag for
177 # assembly files. Without this flag, the compiler believes that it's
178 # building for ARMv5.
Joel Klinghed1550a842015-05-29 15:06:45 +0200179 set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -march=${CMAKE_SYSTEM_PROCESSOR}")
Adam Langley843ab662015-04-28 17:46:58 -0700180endif()
181
Nico Weberdeb52842014-11-18 12:14:46 -0800182if (${ARCH} STREQUAL "x86" AND APPLE)
Adam Langley4a0f0c42015-01-28 16:37:10 -0800183 # With CMake 2.8.x, ${CMAKE_SYSTEM_PROCESSOR} evalutes to i386 on OS X,
184 # but clang defaults to 64-bit builds on OS X unless otherwise told.
185 # Set ARCH to x86_64 so clang and CMake agree. This is fixed in CMake 3.
186 set(ARCH "x86_64")
Nico Weberdeb52842014-11-18 12:14:46 -0800187endif()
188
David Benjamin27b08e92015-05-04 15:16:57 -0400189if (OPENSSL_NO_ASM)
190 add_definitions(-DOPENSSL_NO_ASM)
191 set(ARCH "generic")
192endif()
193
David Benjamin301afaf2015-10-14 21:34:40 -0400194# Declare a dummy target to build all unit tests. Test targets should inject
195# themselves as dependencies next to the target definition.
196add_custom_target(all_tests)
197
Adam Langley95c29f32014-06-20 12:00:00 -0700198add_subdirectory(crypto)
199add_subdirectory(ssl)
200add_subdirectory(ssl/test)
201add_subdirectory(tool)
Adam Langleyc004dfc2015-02-03 10:45:12 -0800202add_subdirectory(decrepit)
David Benjamin301afaf2015-10-14 21:34:40 -0400203
Adam Langley9a4beb82015-11-09 13:57:26 -0800204if(FUZZ)
205 add_subdirectory(fuzz)
206endif()
207
David Benjamin301afaf2015-10-14 21:34:40 -0400208if (NOT ${CMAKE_VERSION} VERSION_LESS "3.2")
209 # USES_TERMINAL is only available in CMake 3.2 or later.
210 set(MAYBE_USES_TERMINAL USES_TERMINAL)
211endif()
212
213add_custom_target(
214 run_tests
215 COMMAND ${GO_EXECUTABLE} run util/all_tests.go -build-dir
216 ${CMAKE_BINARY_DIR}
Adam Langleyd5c72c82016-09-23 16:43:17 -0700217 COMMAND cd ssl/test/runner &&
218 ${GO_EXECUTABLE} test -shim-path $<TARGET_FILE:bssl_shim>
219 ${RUNNER_ARGS}
David Benjamin301afaf2015-10-14 21:34:40 -0400220 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
221 DEPENDS all_tests bssl_shim
222 ${MAYBE_USES_TERMINAL})