blob: 3d1ea78d8e55dba4d37434b1a68ca478934c9b36 [file] [log] [blame]
Adam Langleyd9e397b2015-01-22 14:27:53 -08001cmake_minimum_required (VERSION 2.8.10)
2
David Benjamin4969cc92016-04-22 15:02:23 -04003# 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 Langleyd9e397b2015-01-22 14:27:53 -080014
Adam Langleye9ada862015-05-11 17:20:37 -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.
David Benjamind316cba2016-06-02 16:17:39 -040018 if(NOT PERL_EXECUTABLE)
19 set(PERL_EXECUTABLE "perl")
20 endif()
21 if(NOT GO_EXECUTABLE)
22 set(GO_EXECUTABLE "go")
23 endif()
Adam Langleye9ada862015-05-11 17:20:37 -070024else()
25 find_package(Perl REQUIRED)
26 find_program(GO_EXECUTABLE go)
27endif()
28
29if (NOT GO_EXECUTABLE)
30 message(FATAL_ERROR "Could not find Go")
31endif()
32
Adam Langleyd9e397b2015-01-22 14:27:53 -080033if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
David Benjaminc895d6b2016-08-11 13:26:41 -040034 set(C_CXX_FLAGS "-Wall -Werror -Wformat=2 -Wsign-compare -Wmissing-field-initializers -Wwrite-strings -ggdb -fvisibility=hidden -fno-common")
David Benjaminf0c4a6c2016-08-11 13:26:41 -040035 if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
36 set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wnewline-eof")
37 endif()
David Benjaminc895d6b2016-08-11 13:26:41 -040038 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")
David Benjamin7c0d06c2016-08-11 13:26:41 -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 Langleyd9e397b2015-01-22 14:27:53 -080044elseif(MSVC)
45 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
Adam Langleye9ada862015-05-11 17:20:37 -070056 "C4267" # conversion from 'size_t' to 'int', possible loss of data
57 "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
Adam Langleyd9e397b2015-01-22 14:27:53 -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 Benjamin9aaebef2016-04-22 15:02:23 -040065 "C4464" # relative include path contains '..'
Adam Langleyd9e397b2015-01-22 14:27:53 -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 Benjamin9aaebef2016-04-22 15:02:23 -040073 "C4623" # default constructor was implicitly defined as deleted
Adam Langleye9ada862015-05-11 17:20:37 -070074 "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
Adam Langleyd9e397b2015-01-22 14:27:53 -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 Benjamin9aaebef2016-04-22 15:02:23 -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 )
Adam Langleyd9e397b2015-01-22 14:27:53 -080090 string(REPLACE "C" " -wd" MSVC_DISABLED_WARNINGS_STR
91 ${MSVC_DISABLED_WARNINGS_LIST})
David Benjamin9aaebef2016-04-22 15:02:23 -040092 string(REPLACE "C" " -w4" MSVC_LEVEL4_WARNINGS_STR
93 ${MSVC_LEVEL4_WARNINGS_LIST})
David Benjamin4969cc92016-04-22 15:02:23 -040094 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 Benjamin7c0d06c2016-08-11 13:26:41 -040096 set(CMAKE_ASM_NASM_FLAGS "-g cv8")
Adam Langleyd9e397b2015-01-22 14:27:53 -080097 add_definitions(-D_HAS_EXCEPTIONS=0)
98 add_definitions(-DWIN32_LEAN_AND_MEAN)
Adam Langleye9ada862015-05-11 17:20:37 -070099 add_definitions(-DNOMINMAX)
David Benjamind316cba2016-06-02 16:17:39 -0400100 add_definitions(-D_CRT_SECURE_NO_WARNINGS) # Allow use of fopen
Adam Langleyd9e397b2015-01-22 14:27:53 -0800101endif()
102
Adam Langleye9ada862015-05-11 17:20:37 -0700103if((CMAKE_COMPILER_IS_GNUCXX AND CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.7.99") OR
Adam Langleyd9e397b2015-01-22 14:27:53 -0800104 CMAKE_CXX_COMPILER_ID MATCHES "Clang")
105 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wshadow")
106 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wshadow")
107endif()
108
David Benjamin6e899c72016-06-09 18:02:18 -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 Langleyf4e42722015-06-04 17:45:09 -0700121endif()
122
Adam Langleyfad63272015-11-12 12:15:39 -0800123if(FUZZ)
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400124 if(NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang")
125 message(FATAL_ERROR "You need to build with Clang for fuzzing to work")
Adam Langleyfad63272015-11-12 12:15:39 -0800126 endif()
127
David Benjamin4969cc92016-04-22 15:02:23 -0400128 add_definitions(-DBORINGSSL_UNSAFE_FUZZER_MODE)
129 set(RUNNER_ARGS "-fuzzer")
130
131 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 Langleyfad63272015-11-12 12:15:39 -0800133 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address")
134 link_directories(.)
135endif()
136
Adam Langleyd9e397b2015-01-22 14:27:53 -0800137add_definitions(-DBORINGSSL_IMPLEMENTATION)
138
139if (BUILD_SHARED_LIBS)
140 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)
144endif()
145
146if (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86_64")
147 set(ARCH "x86_64")
148elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "amd64")
149 set(ARCH "x86_64")
150elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "AMD64")
151 # 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()
157elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86")
158 set(ARCH "x86")
159elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "i386")
160 set(ARCH "x86")
161elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "i686")
162 set(ARCH "x86")
163elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "arm")
164 set(ARCH "arm")
Adam Langleyf4e42722015-06-04 17:45:09 -0700165elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "armv6")
166 set(ARCH "arm")
Adam Langleye9ada862015-05-11 17:20:37 -0700167elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "armv7-a")
168 set(ARCH "arm")
Adam Langleyd9e397b2015-01-22 14:27:53 -0800169elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "aarch64")
170 set(ARCH "aarch64")
171else()
172 message(FATAL_ERROR "Unknown processor:" ${CMAKE_SYSTEM_PROCESSOR})
173endif()
174
Adam Langleye9ada862015-05-11 17:20:37 -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.
Adam Langleyf4e42722015-06-04 17:45:09 -0700179 set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -march=${CMAKE_SYSTEM_PROCESSOR}")
Adam Langleye9ada862015-05-11 17:20:37 -0700180endif()
181
Adam Langleyd9e397b2015-01-22 14:27:53 -0800182if (${ARCH} STREQUAL "x86" AND APPLE)
183 # 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")
187endif()
188
Adam Langleye9ada862015-05-11 17:20:37 -0700189if (OPENSSL_NO_ASM)
190 add_definitions(-DOPENSSL_NO_ASM)
191 set(ARCH "generic")
192endif()
193
Kenny Roote99801b2015-11-06 15:31:15 -0800194# 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 Langleyd9e397b2015-01-22 14:27:53 -0800198add_subdirectory(crypto)
199add_subdirectory(ssl)
200add_subdirectory(ssl/test)
201add_subdirectory(tool)
Adam Langleye9ada862015-05-11 17:20:37 -0700202add_subdirectory(decrepit)
Kenny Roote99801b2015-11-06 15:31:15 -0800203
Adam Langleyfad63272015-11-12 12:15:39 -0800204if(FUZZ)
205 add_subdirectory(fuzz)
206endif()
207
Kenny Roote99801b2015-11-06 15:31:15 -0800208if (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}
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400217 COMMAND cd ssl/test/runner &&
218 ${GO_EXECUTABLE} test -shim-path $<TARGET_FILE:bssl_shim>
219 ${RUNNER_ARGS}
Kenny Roote99801b2015-11-06 15:31:15 -0800220 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
221 DEPENDS all_tests bssl_shim
222 ${MAYBE_USES_TERMINAL})