blob: f36cd38df0b7286ebdc7eecd7c06edb8b910c0bd [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.
18 set(PERL_EXECUTABLE "perl")
19 set(GO_EXECUTABLE "go")
20else()
21 find_package(Perl REQUIRED)
22 find_program(GO_EXECUTABLE go)
23endif()
24
25if (NOT GO_EXECUTABLE)
26 message(FATAL_ERROR "Could not find Go")
27endif()
28
Adam Langleyd9e397b2015-01-22 14:27:53 -080029if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
David Benjamin4969cc92016-04-22 15:02:23 -040030 set(C_CXX_FLAGS "-Wall -Werror -Wformat=2 -Wsign-compare -Wmissing-field-initializers -ggdb -fvisibility=hidden")
31 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${C_CXX_FLAGS} -Wmissing-prototypes")
32 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x ${C_CXX_FLAGS} -Wmissing-declarations")
Adam Langleyd9e397b2015-01-22 14:27:53 -080033elseif(MSVC)
34 set(MSVC_DISABLED_WARNINGS_LIST
35 "C4100" # 'exarg' : unreferenced formal parameter
36 "C4127" # conditional expression is constant
37 "C4200" # nonstandard extension used : zero-sized array in
38 # struct/union.
39 "C4242" # 'function' : conversion from 'int' to 'uint8_t',
40 # possible loss of data
41 "C4244" # 'function' : conversion from 'int' to 'uint8_t',
42 # possible loss of data
43 "C4245" # 'initializing' : conversion from 'long' to
44 # 'unsigned long', signed/unsigned mismatch
Adam Langleye9ada862015-05-11 17:20:37 -070045 "C4267" # conversion from 'size_t' to 'int', possible loss of data
46 "C4371" # layout of class may have changed from a previous version of the
47 # compiler due to better packing of member '...'
48 "C4388" # signed/unsigned mismatch
Adam Langleyd9e397b2015-01-22 14:27:53 -080049 "C4296" # '>=' : expression is always true
50 "C4350" # behavior change: 'std::_Wrap_alloc...'
51 "C4365" # '=' : conversion from 'size_t' to 'int',
52 # signed/unsigned mismatch
53 "C4389" # '!=' : signed/unsigned mismatch
David Benjamin9aaebef2016-04-22 15:02:23 -040054 "C4464" # relative include path contains '..'
Adam Langleyd9e397b2015-01-22 14:27:53 -080055 "C4510" # 'argument' : default constructor could not be generated
56 "C4512" # 'argument' : assignment operator could not be generated
57 "C4514" # 'function': unreferenced inline function has been removed
58 "C4548" # expression before comma has no effect; expected expression with
59 # side-effect" caused by FD_* macros.
60 "C4610" # struct 'argument' can never be instantiated - user defined
61 # constructor required.
David Benjamin9aaebef2016-04-22 15:02:23 -040062 "C4623" # default constructor was implicitly defined as deleted
Adam Langleye9ada862015-05-11 17:20:37 -070063 "C4625" # copy constructor could not be generated because a base class
64 # copy constructor is inaccessible or deleted
65 "C4626" # assignment operator could not be generated because a base class
66 # assignment operator is inaccessible or deleted
Adam Langleyd9e397b2015-01-22 14:27:53 -080067 "C4706" # assignment within conditional expression
68 "C4710" # 'function': function not inlined
69 "C4711" # function 'function' selected for inline expansion
70 "C4800" # 'int' : forcing value to bool 'true' or 'false'
71 # (performance warning)
72 "C4820" # 'bytes' bytes padding added after construct 'member_name'
73 "C4996" # 'read': The POSIX name for this item is deprecated. Instead,
74 # use the ISO C++ conformant name: _read.
David Benjamin9aaebef2016-04-22 15:02:23 -040075 "C5027" # move assignment operator was implicitly defined as deleted
76 )
77 set(MSVC_LEVEL4_WARNINGS_LIST
78 # See https://connect.microsoft.com/VisualStudio/feedback/details/1217660/warning-c4265-when-using-functional-header
79 "C4265" # class has virtual functions, but destructor is not virtual
80 )
Adam Langleyd9e397b2015-01-22 14:27:53 -080081 string(REPLACE "C" " -wd" MSVC_DISABLED_WARNINGS_STR
82 ${MSVC_DISABLED_WARNINGS_LIST})
David Benjamin9aaebef2016-04-22 15:02:23 -040083 string(REPLACE "C" " -w4" MSVC_LEVEL4_WARNINGS_STR
84 ${MSVC_LEVEL4_WARNINGS_LIST})
David Benjamin4969cc92016-04-22 15:02:23 -040085 set(CMAKE_C_FLAGS "-Wall -WX ${MSVC_DISABLED_WARNINGS_STR} ${MSVC_LEVEL4_WARNINGS_STR}")
86 set(CMAKE_CXX_FLAGS "-Wall -WX ${MSVC_DISABLED_WARNINGS_STR} ${MSVC_LEVEL4_WARNINGS_STR}")
Adam Langleyd9e397b2015-01-22 14:27:53 -080087 add_definitions(-D_HAS_EXCEPTIONS=0)
88 add_definitions(-DWIN32_LEAN_AND_MEAN)
Adam Langleye9ada862015-05-11 17:20:37 -070089 add_definitions(-DNOMINMAX)
Adam Langleyd9e397b2015-01-22 14:27:53 -080090endif()
91
Adam Langleye9ada862015-05-11 17:20:37 -070092if((CMAKE_COMPILER_IS_GNUCXX AND CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.7.99") OR
Adam Langleyd9e397b2015-01-22 14:27:53 -080093 CMAKE_CXX_COMPILER_ID MATCHES "Clang")
94 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wshadow")
95 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wshadow")
96endif()
97
Adam Langleyf4e42722015-06-04 17:45:09 -070098if((CMAKE_COMPILER_IS_GNUCXX AND CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.8.99") OR
99 CMAKE_CXX_COMPILER_ID MATCHES "Clang")
100 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c11 -D_XOPEN_SOURCE=700")
101endif()
102
Adam Langleyfad63272015-11-12 12:15:39 -0800103if(FUZZ)
104 if(!CMAKE_CXX_COMPILER_ID MATCHES "Clang")
105 message("You need to build with Clang for fuzzing to work")
106 endif()
107
David Benjamin4969cc92016-04-22 15:02:23 -0400108 add_definitions(-DBORINGSSL_UNSAFE_FUZZER_MODE)
109 set(RUNNER_ARGS "-fuzzer")
110
111 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -fsanitize-coverage=edge,indirect-calls,8bit-counters")
112 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fsanitize-coverage=edge,indirect-calls,8bit-counters")
Adam Langleyfad63272015-11-12 12:15:39 -0800113 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address")
114 link_directories(.)
115endif()
116
Adam Langleyd9e397b2015-01-22 14:27:53 -0800117add_definitions(-DBORINGSSL_IMPLEMENTATION)
118
119if (BUILD_SHARED_LIBS)
120 add_definitions(-DBORINGSSL_SHARED_LIBRARY)
121 # Enable position-independent code globally. This is needed because
122 # some library targets are OBJECT libraries.
123 set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
124endif()
125
126if (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86_64")
127 set(ARCH "x86_64")
128elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "amd64")
129 set(ARCH "x86_64")
130elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "AMD64")
131 # cmake reports AMD64 on Windows, but we might be building for 32-bit.
132 if (CMAKE_CL_64)
133 set(ARCH "x86_64")
134 else()
135 set(ARCH "x86")
136 endif()
137elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86")
138 set(ARCH "x86")
139elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "i386")
140 set(ARCH "x86")
141elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "i686")
142 set(ARCH "x86")
143elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "arm")
144 set(ARCH "arm")
Adam Langleyf4e42722015-06-04 17:45:09 -0700145elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "armv6")
146 set(ARCH "arm")
Adam Langleye9ada862015-05-11 17:20:37 -0700147elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "armv7-a")
148 set(ARCH "arm")
Adam Langleyd9e397b2015-01-22 14:27:53 -0800149elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "aarch64")
150 set(ARCH "aarch64")
151else()
152 message(FATAL_ERROR "Unknown processor:" ${CMAKE_SYSTEM_PROCESSOR})
153endif()
154
Adam Langleye9ada862015-05-11 17:20:37 -0700155if (ANDROID AND ${ARCH} STREQUAL "arm")
156 # The Android-NDK CMake files somehow fail to set the -march flag for
157 # assembly files. Without this flag, the compiler believes that it's
158 # building for ARMv5.
Adam Langleyf4e42722015-06-04 17:45:09 -0700159 set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -march=${CMAKE_SYSTEM_PROCESSOR}")
Adam Langleye9ada862015-05-11 17:20:37 -0700160endif()
161
Adam Langleyd9e397b2015-01-22 14:27:53 -0800162if (${ARCH} STREQUAL "x86" AND APPLE)
163 # With CMake 2.8.x, ${CMAKE_SYSTEM_PROCESSOR} evalutes to i386 on OS X,
164 # but clang defaults to 64-bit builds on OS X unless otherwise told.
165 # Set ARCH to x86_64 so clang and CMake agree. This is fixed in CMake 3.
166 set(ARCH "x86_64")
167endif()
168
Adam Langleye9ada862015-05-11 17:20:37 -0700169if (OPENSSL_NO_ASM)
170 add_definitions(-DOPENSSL_NO_ASM)
171 set(ARCH "generic")
172endif()
173
Kenny Roote99801b2015-11-06 15:31:15 -0800174# Declare a dummy target to build all unit tests. Test targets should inject
175# themselves as dependencies next to the target definition.
176add_custom_target(all_tests)
177
Adam Langleyd9e397b2015-01-22 14:27:53 -0800178add_subdirectory(crypto)
179add_subdirectory(ssl)
180add_subdirectory(ssl/test)
181add_subdirectory(tool)
Adam Langleye9ada862015-05-11 17:20:37 -0700182add_subdirectory(decrepit)
Kenny Roote99801b2015-11-06 15:31:15 -0800183
Adam Langleyfad63272015-11-12 12:15:39 -0800184if(FUZZ)
185 add_subdirectory(fuzz)
186endif()
187
Kenny Roote99801b2015-11-06 15:31:15 -0800188if (NOT ${CMAKE_VERSION} VERSION_LESS "3.2")
189 # USES_TERMINAL is only available in CMake 3.2 or later.
190 set(MAYBE_USES_TERMINAL USES_TERMINAL)
191endif()
192
193add_custom_target(
194 run_tests
195 COMMAND ${GO_EXECUTABLE} run util/all_tests.go -build-dir
196 ${CMAKE_BINARY_DIR}
197 COMMAND cd ssl/test/runner
198 COMMAND ${GO_EXECUTABLE} test -shim-path $<TARGET_FILE:bssl_shim>
David Benjamin4969cc92016-04-22 15:02:23 -0400199 ${RUNNER_ARGS}
Kenny Roote99801b2015-11-06 15:31:15 -0800200 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
201 DEPENDS all_tests bssl_shim
202 ${MAYBE_USES_TERMINAL})