blob: cb8bb53935fcccc80f4107dac2aa91bf086cdeae [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.
David Benjaminffb11072016-11-13 10:32:10 +090050 "C4204" # nonstandard extension used: non-constant aggregate initializer
51 "C4221" # nonstandard extension used : 'identifier' : cannot be
52 # initialized using address of automatic variable
Brian Smithefed2212015-01-28 16:20:02 -080053 "C4242" # 'function' : conversion from 'int' to 'uint8_t',
54 # possible loss of data
55 "C4244" # 'function' : conversion from 'int' to 'uint8_t',
56 # possible loss of data
57 "C4245" # 'initializing' : conversion from 'long' to
58 # 'unsigned long', signed/unsigned mismatch
David Benjamin3673be72015-02-11 15:12:05 -050059 "C4267" # conversion from 'size_t' to 'int', possible loss of data
David Benjamin3673be72015-02-11 15:12:05 -050060 "C4371" # layout of class may have changed from a previous version of the
61 # compiler due to better packing of member '...'
62 "C4388" # signed/unsigned mismatch
Brian Smithefed2212015-01-28 16:20:02 -080063 "C4296" # '>=' : expression is always true
64 "C4350" # behavior change: 'std::_Wrap_alloc...'
65 "C4365" # '=' : conversion from 'size_t' to 'int',
66 # signed/unsigned mismatch
67 "C4389" # '!=' : signed/unsigned mismatch
David Benjamin7acd6bc2016-05-02 12:57:01 -040068 "C4464" # relative include path contains '..'
Brian Smithefed2212015-01-28 16:20:02 -080069 "C4510" # 'argument' : default constructor could not be generated
70 "C4512" # 'argument' : assignment operator could not be generated
71 "C4514" # 'function': unreferenced inline function has been removed
72 "C4548" # expression before comma has no effect; expected expression with
73 # side-effect" caused by FD_* macros.
74 "C4610" # struct 'argument' can never be instantiated - user defined
75 # constructor required.
David Benjamin7acd6bc2016-05-02 12:57:01 -040076 "C4623" # default constructor was implicitly defined as deleted
David Benjamin3673be72015-02-11 15:12:05 -050077 "C4625" # copy constructor could not be generated because a base class
78 # copy constructor is inaccessible or deleted
79 "C4626" # assignment operator could not be generated because a base class
80 # assignment operator is inaccessible or deleted
Brian Smithefed2212015-01-28 16:20:02 -080081 "C4706" # assignment within conditional expression
82 "C4710" # 'function': function not inlined
83 "C4711" # function 'function' selected for inline expansion
84 "C4800" # 'int' : forcing value to bool 'true' or 'false'
85 # (performance warning)
86 "C4820" # 'bytes' bytes padding added after construct 'member_name'
David Benjamin7acd6bc2016-05-02 12:57:01 -040087 "C5027" # move assignment operator was implicitly defined as deleted
88 )
89 set(MSVC_LEVEL4_WARNINGS_LIST
90 # See https://connect.microsoft.com/VisualStudio/feedback/details/1217660/warning-c4265-when-using-functional-header
91 "C4265" # class has virtual functions, but destructor is not virtual
92 )
Brian Smithefed2212015-01-28 16:20:02 -080093 string(REPLACE "C" " -wd" MSVC_DISABLED_WARNINGS_STR
94 ${MSVC_DISABLED_WARNINGS_LIST})
David Benjamin7acd6bc2016-05-02 12:57:01 -040095 string(REPLACE "C" " -w4" MSVC_LEVEL4_WARNINGS_STR
96 ${MSVC_LEVEL4_WARNINGS_LIST})
Brian Smithdc6c1b82016-01-17 22:21:42 -100097 set(CMAKE_C_FLAGS "-Wall -WX ${MSVC_DISABLED_WARNINGS_STR} ${MSVC_LEVEL4_WARNINGS_STR}")
98 set(CMAKE_CXX_FLAGS "-Wall -WX ${MSVC_DISABLED_WARNINGS_STR} ${MSVC_LEVEL4_WARNINGS_STR}")
David Benjamin72088222016-08-04 19:09:45 -040099 set(CMAKE_ASM_NASM_FLAGS "-g cv8")
Adam Langley4a0f0c42015-01-28 16:37:10 -0800100 add_definitions(-D_HAS_EXCEPTIONS=0)
Brian Smitha87de9b2015-01-28 20:34:47 -0800101 add_definitions(-DWIN32_LEAN_AND_MEAN)
David Benjamin0e434b92015-04-02 13:20:01 -0400102 add_definitions(-DNOMINMAX)
nmittlerf0322b22016-05-19 08:49:59 -0700103 add_definitions(-D_CRT_SECURE_NO_WARNINGS) # Allow use of fopen
Adam Langley95c29f32014-06-20 12:00:00 -0700104endif()
105
David Benjaminb826c0d2015-02-28 00:00:44 -0500106if((CMAKE_COMPILER_IS_GNUCXX AND CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.7.99") OR
Brian Smith1d75c8b2015-01-23 17:25:44 -0800107 CMAKE_CXX_COMPILER_ID MATCHES "Clang")
Adam Langley4a0f0c42015-01-28 16:37:10 -0800108 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wshadow")
109 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wshadow")
Adam Langley07100c62015-01-16 15:20:54 -0800110endif()
111
David Benjamin2e8ba2d2016-06-09 16:22:26 -0400112if(CMAKE_COMPILER_IS_GNUCXX)
113 if ((CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.8.99") OR
114 CMAKE_CXX_COMPILER_ID MATCHES "Clang")
115 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c11")
116 else()
117 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99")
118 endif()
119endif()
120
121# pthread_rwlock_t requires a feature flag.
122if(NOT WIN32)
123 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_XOPEN_SOURCE=700")
Adam Langley6f2e7332015-05-15 12:01:29 -0700124endif()
125
Adam Langley9a4beb82015-11-09 13:57:26 -0800126if(FUZZ)
Alessandro Ghedinib6f69272016-09-28 22:14:01 +0100127 if(NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang")
128 message(FATAL_ERROR "You need to build with Clang for fuzzing to work")
Adam Langley9a4beb82015-11-09 13:57:26 -0800129 endif()
130
David Benjaminec978dd2016-11-04 18:59:33 -0400131 add_definitions(-DBORINGSSL_UNSAFE_DETERMINISTIC_MODE)
132 set(RUNNER_ARGS "-deterministic")
133
134 if(NOT NO_FUZZER_MODE)
135 add_definitions(-DBORINGSSL_UNSAFE_FUZZER_MODE)
136 set(RUNNER_ARGS ${RUNNER_ARGS} "-fuzzer" "-shim-config" "fuzzer_mode.json")
137 endif()
David Benjaminbc5b2a22016-03-01 22:57:32 -0500138
Adam Langleyde29f362016-03-03 09:50:10 -0800139 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -fsanitize-coverage=edge,indirect-calls,8bit-counters")
140 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fsanitize-coverage=edge,indirect-calls,8bit-counters")
Adam Langley9a4beb82015-11-09 13:57:26 -0800141 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address")
Adam Langley7104cc92015-11-10 15:00:51 -0800142 link_directories(.)
Adam Langley9a4beb82015-11-09 13:57:26 -0800143endif()
144
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700145add_definitions(-DBORINGSSL_IMPLEMENTATION)
146
David Benjamin507c1ee2015-01-28 00:50:21 -0500147if (BUILD_SHARED_LIBS)
Adam Langley4a0f0c42015-01-28 16:37:10 -0800148 add_definitions(-DBORINGSSL_SHARED_LIBRARY)
149 # Enable position-independent code globally. This is needed because
150 # some library targets are OBJECT libraries.
151 set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
David Benjamin507c1ee2015-01-28 00:50:21 -0500152endif()
153
Adam Langley95c29f32014-06-20 12:00:00 -0700154if (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86_64")
Adam Langley4a0f0c42015-01-28 16:37:10 -0800155 set(ARCH "x86_64")
Piotr Sikora1d8adf12014-07-31 03:09:49 -0700156elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "amd64")
Adam Langley4a0f0c42015-01-28 16:37:10 -0800157 set(ARCH "x86_64")
Adam Langley95c29f32014-06-20 12:00:00 -0700158elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "AMD64")
Adam Langley4a0f0c42015-01-28 16:37:10 -0800159 # cmake reports AMD64 on Windows, but we might be building for 32-bit.
160 if (CMAKE_CL_64)
161 set(ARCH "x86_64")
162 else()
163 set(ARCH "x86")
164 endif()
Adam Langley95c29f32014-06-20 12:00:00 -0700165elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86")
Adam Langley4a0f0c42015-01-28 16:37:10 -0800166 set(ARCH "x86")
Piotr Sikora1d8adf12014-07-31 03:09:49 -0700167elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "i386")
Adam Langley4a0f0c42015-01-28 16:37:10 -0800168 set(ARCH "x86")
Lukas Tribusd83f38c2014-08-11 13:27:44 -0700169elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "i686")
Adam Langley4a0f0c42015-01-28 16:37:10 -0800170 set(ARCH "x86")
Aurelien Regat-Barrel1e21e992016-11-12 18:04:20 -0600171elseif (${CMAKE_SYSTEM_PROCESSOR} MATCHES "^arm*")
Adam Langley843ab662015-04-28 17:46:58 -0700172 set(ARCH "arm")
Adam Langley3e652652015-01-09 15:44:37 -0800173elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "aarch64")
Adam Langley4a0f0c42015-01-28 16:37:10 -0800174 set(ARCH "aarch64")
Kenneth Geisshirt54955fe2016-10-28 15:05:32 +0200175elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "mips")
176 # Just to avoid the “unknown processor” error.
Doug Kwan5f04b6b2016-11-11 17:18:53 -0800177elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "ppc64le")
178 set(ARCH "ppc64le")
Adam Langley95c29f32014-06-20 12:00:00 -0700179else()
Adam Langley4a0f0c42015-01-28 16:37:10 -0800180 message(FATAL_ERROR "Unknown processor:" ${CMAKE_SYSTEM_PROCESSOR})
Adam Langley95c29f32014-06-20 12:00:00 -0700181endif()
182
Adam Langley843ab662015-04-28 17:46:58 -0700183if (ANDROID AND ${ARCH} STREQUAL "arm")
184 # The Android-NDK CMake files somehow fail to set the -march flag for
185 # assembly files. Without this flag, the compiler believes that it's
186 # building for ARMv5.
Joel Klinghed1550a842015-05-29 15:06:45 +0200187 set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -march=${CMAKE_SYSTEM_PROCESSOR}")
Adam Langley843ab662015-04-28 17:46:58 -0700188endif()
189
Nico Weberdeb52842014-11-18 12:14:46 -0800190if (${ARCH} STREQUAL "x86" AND APPLE)
Adam Langley4a0f0c42015-01-28 16:37:10 -0800191 # With CMake 2.8.x, ${CMAKE_SYSTEM_PROCESSOR} evalutes to i386 on OS X,
192 # but clang defaults to 64-bit builds on OS X unless otherwise told.
193 # Set ARCH to x86_64 so clang and CMake agree. This is fixed in CMake 3.
194 set(ARCH "x86_64")
Nico Weberdeb52842014-11-18 12:14:46 -0800195endif()
196
David Benjamin27b08e92015-05-04 15:16:57 -0400197if (OPENSSL_NO_ASM)
198 add_definitions(-DOPENSSL_NO_ASM)
199 set(ARCH "generic")
200endif()
201
David Benjamin301afaf2015-10-14 21:34:40 -0400202# Declare a dummy target to build all unit tests. Test targets should inject
203# themselves as dependencies next to the target definition.
204add_custom_target(all_tests)
205
Adam Langley95c29f32014-06-20 12:00:00 -0700206add_subdirectory(crypto)
207add_subdirectory(ssl)
208add_subdirectory(ssl/test)
209add_subdirectory(tool)
Adam Langleyc004dfc2015-02-03 10:45:12 -0800210add_subdirectory(decrepit)
David Benjamin301afaf2015-10-14 21:34:40 -0400211
Adam Langley9a4beb82015-11-09 13:57:26 -0800212if(FUZZ)
213 add_subdirectory(fuzz)
214endif()
215
David Benjamin301afaf2015-10-14 21:34:40 -0400216if (NOT ${CMAKE_VERSION} VERSION_LESS "3.2")
217 # USES_TERMINAL is only available in CMake 3.2 or later.
218 set(MAYBE_USES_TERMINAL USES_TERMINAL)
219endif()
220
221add_custom_target(
222 run_tests
223 COMMAND ${GO_EXECUTABLE} run util/all_tests.go -build-dir
224 ${CMAKE_BINARY_DIR}
Adam Langleyd5c72c82016-09-23 16:43:17 -0700225 COMMAND cd ssl/test/runner &&
226 ${GO_EXECUTABLE} test -shim-path $<TARGET_FILE:bssl_shim>
227 ${RUNNER_ARGS}
David Benjamin301afaf2015-10-14 21:34:40 -0400228 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
229 DEPENDS all_tests bssl_shim
230 ${MAYBE_USES_TERMINAL})