blob: e1cafeab7677082f9dc63b573ffa00bcf7eb4c73 [file] [log] [blame]
Brian Silverman8fce7482020-01-05 13:18:21 -08001# Disable in-source builds to prevent source tree corruption.
Austin Schuh812d0d12021-11-04 20:16:48 -07002if(" ${CMAKE_CURRENT_SOURCE_DIR}" STREQUAL " ${CMAKE_CURRENT_BINARY_DIR}")
Brian Silverman8fce7482020-01-05 13:18:21 -08003 message(FATAL_ERROR "
4FATAL: In-source builds are not allowed.
5 You should create a separate directory for build files.
6")
7endif()
8
9
10project(allwpilib)
11cmake_minimum_required(VERSION 3.3.0)
Austin Schuh812d0d12021-11-04 20:16:48 -070012set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules")
13
14set(WPILIB_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
Brian Silverman8fce7482020-01-05 13:18:21 -080015
16INCLUDE(CPack)
17
18set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS TRUE)
19set_property(GLOBAL PROPERTY USE_FOLDERS ON)
20
Austin Schuh812d0d12021-11-04 20:16:48 -070021set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${WPILIB_BINARY_DIR}/lib)
22set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${WPILIB_BINARY_DIR}/lib)
23set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${WPILIB_BINARY_DIR}/bin)
24set(CMAKE_JAVA_TARGET_OUTPUT_DIR ${WPILIB_BINARY_DIR}/jar)
Brian Silverman8fce7482020-01-05 13:18:21 -080025
26# use, i.e. don't skip the full RPATH for the build tree
27SET(CMAKE_SKIP_BUILD_RPATH FALSE)
28
29# when building, don't use the install RPATH already
30# (but later on when installing)
31SET(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
32
33SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/wpilib/lib")
34
35# add the automatically determined parts of the RPATH
36# which point to directories outside the build tree to the install RPATH
37SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
38
39# the RPATH to be used when installing, but only if it's not a system directory
40LIST(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${CMAKE_INSTALL_PREFIX}/wpilib/lib" isSystemDir)
41IF("${isSystemDir}" STREQUAL "-1")
42 SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/wpilib/lib")
43ENDIF("${isSystemDir}" STREQUAL "-1")
44
Austin Schuh1e69f942020-11-14 15:06:14 -080045# Options for building certain parts of the repo. Everything is built by default.
46option(BUILD_SHARED_LIBS "Build with shared libs (needed for JNI)" ON)
47option(WITH_JAVA "Include java and JNI in the build" ON)
48option(WITH_CSCORE "Build cscore (needs OpenCV)" ON)
Austin Schuh812d0d12021-11-04 20:16:48 -070049option(WITH_WPIMATH "Build wpimath" ON)
50option(WITH_WPILIB "Build hal, wpilibc/j, and myRobot (needs OpenCV)" ON)
51option(WITH_OLD_COMMANDS "Build old commands" OFF)
52option(WITH_EXAMPLES "Build examples" OFF)
Austin Schuh1e69f942020-11-14 15:06:14 -080053option(WITH_TESTS "Build unit tests (requires internet connection)" ON)
54option(WITH_GUI "Build GUI items" ON)
55option(WITH_SIMULATION_MODULES "Build simulation modules" ON)
56
57# Options for external HAL.
58option(WITH_EXTERNAL_HAL "Use a separately built HAL" OFF)
Brian Silverman8fce7482020-01-05 13:18:21 -080059set(EXTERNAL_HAL_FILE "" CACHE FILEPATH "Location to look for an external HAL CMake File")
Austin Schuh1e69f942020-11-14 15:06:14 -080060
61# Options for using a package manager (vcpkg) for certain dependencies.
Austin Schuh812d0d12021-11-04 20:16:48 -070062option(USE_VCPKG_FMTLIB "Use vcpkg fmtlib" OFF)
Brian Silverman8fce7482020-01-05 13:18:21 -080063option(USE_VCPKG_LIBUV "Use vcpkg libuv" OFF)
64option(USE_VCPKG_EIGEN "Use vcpkg eigen" OFF)
Brian Silverman8fce7482020-01-05 13:18:21 -080065
Austin Schuh1e69f942020-11-14 15:06:14 -080066# Options for installation.
67option(WITH_FLAT_INSTALL "Use a flat install directory" OFF)
68
69# Options for location of OpenCV Java.
70set(OPENCV_JAVA_INSTALL_DIR "" CACHE PATH "Location to search for the OpenCV jar file")
71
72# Set default build type to release with debug info (i.e. release mode optimizations
73# are performed, but debug info still exists).
74if (NOT CMAKE_BUILD_TYPE)
75 set (CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE STRING "" FORCE)
76endif()
77
78# We always want flat install with MSVC.
79if (MSVC)
80 set(WITH_FLAT_INSTALL ON)
81endif()
82
83if (WITH_JAVA AND NOT BUILD_SHARED_LIBS)
Brian Silverman8fce7482020-01-05 13:18:21 -080084 message(FATAL_ERROR "
85FATAL: Cannot build static libs with Java enabled.
86 Static libs requires both BUILD_SHARED_LIBS=OFF and
Austin Schuh1e69f942020-11-14 15:06:14 -080087 WITH_JAVA=OFF
88")
89endif()
90
91if (WITH_SIMULATION_MODULES AND NOT BUILD_SHARED_LIBS)
92 message(FATAL_ERROR "
93FATAL: Cannot build static libs with simulation modules enabled.
94 Static libs requires both BUILD_SHARED_LIBS=OFF and
95 WITH_SIMULATION_MODULES=OFF
96")
97endif()
98
99if (NOT WITH_JAVA OR NOT WITH_CSCORE)
100 if(NOT "${OPENCV_JAVA_INSTALL_DIR}" STREQUAL "")
101 message(WARNING "
102WARNING: OpenCV Java dir set but java is not enabled!
103It will be ignored.
104")
105 endif()
106endif()
107
108if (NOT WITH_WPILIB AND WITH_SIMULATION_MODULES)
109 message(FATAL_ERROR "
110FATAL: Cannot build simulation modules with wpilib disabled.
111 Enable wpilib by setting WITH_WPILIB=ON
Brian Silverman8fce7482020-01-05 13:18:21 -0800112")
113endif()
114
Austin Schuh812d0d12021-11-04 20:16:48 -0700115if (NOT WITH_WPIMATH AND WITH_WPILIB)
116 message(FATAL_ERROR "
117FATAL: Cannot build wpilib without wpimath.
118 Enable wpimath by setting WITH_WPIMATH=ON
119")
120endif()
121
Brian Silverman8fce7482020-01-05 13:18:21 -0800122set( wpilib_dest wpilib)
123set( include_dest wpilib/include )
124set( main_lib_dest wpilib/lib )
125set( java_lib_dest wpilib/java )
126set( jni_lib_dest wpilib/jni )
127
Austin Schuh1e69f942020-11-14 15:06:14 -0800128if (WITH_FLAT_INSTALL)
Brian Silverman8fce7482020-01-05 13:18:21 -0800129 set (wpilib_config_dir ${wpilib_dest})
130else()
131 set (wpilib_config_dir share/wpilib)
132endif()
133
134if (USE_VCPKG_LIBUV)
135set (LIBUV_VCPKG_REPLACE "find_package(unofficial-libuv CONFIG)")
136endif()
137
138if (USE_VCPKG_EIGEN)
139set (EIGEN_VCPKG_REPLACE "find_package(Eigen3 CONFIG)")
140endif()
141
Austin Schuh1e69f942020-11-14 15:06:14 -0800142if (WITH_FLAT_INSTALL)
Brian Silverman8fce7482020-01-05 13:18:21 -0800143set(WPIUTIL_DEP_REPLACE "include($\{SELF_DIR\}/wpiutil-config.cmake)")
144set(NTCORE_DEP_REPLACE "include($\{SELF_DIR\}/ntcore-config.cmake)")
145set(CSCORE_DEP_REPLACE_IMPL "include(\${SELF_DIR}/cscore-config.cmake)")
146set(CAMERASERVER_DEP_REPLACE_IMPL "include(\${SELF_DIR}/cameraserver-config.cmake)")
147set(HAL_DEP_REPLACE_IMPL "include(\${SELF_DIR}/hal-config.cmake)")
Austin Schuh1e69f942020-11-14 15:06:14 -0800148set(WPIMATH_DEP_REPLACE "include($\{SELF_DIR\}/wpimath-config.cmake)")
Brian Silverman8fce7482020-01-05 13:18:21 -0800149set(WPILIBC_DEP_REPLACE_IMPL "include(\${SELF_DIR}/wpilibc-config.cmake)")
Austin Schuh812d0d12021-11-04 20:16:48 -0700150set(WPILIBNEWCOMMANDS_DEP_REPLACE "include(\${SELF_DIR}/wpilibNewcommands-config.cmake)")
151set(WPILIBOLDCOMMANDS_DEP_REPLACE "include(\${SELF_DIR}/wpilibOldcommands-config.cmake)")
Brian Silverman8fce7482020-01-05 13:18:21 -0800152else()
153set(WPIUTIL_DEP_REPLACE "find_dependency(wpiutil)")
154set(NTCORE_DEP_REPLACE "find_dependency(ntcore)")
155set(CSCORE_DEP_REPLACE_IMPL "find_dependency(cscore)")
156set(CAMERASERVER_DEP_REPLACE_IMPL "find_dependency(cameraserver)")
157set(HAL_DEP_REPLACE_IMPL "find_dependency(hal)")
Austin Schuh1e69f942020-11-14 15:06:14 -0800158set(WPIMATH_DEP_REPLACE "find_dependency(wpimath)")
Brian Silverman8fce7482020-01-05 13:18:21 -0800159set(WPILIBC_DEP_REPLACE_IMPL "find_dependency(wpilibc)")
Austin Schuh812d0d12021-11-04 20:16:48 -0700160set(WPILIBNEWCOMMANDS_DEP_REPLACE "find_dependency(wpilibNewCommands)")
161set(WPILIBOLDCOMMANDS_DEP_REPLACE "find_dependency(wpilibOldCommands)")
Brian Silverman8fce7482020-01-05 13:18:21 -0800162endif()
163
164set(FILENAME_DEP_REPLACE "get_filename_component(SELF_DIR \"$\{CMAKE_CURRENT_LIST_FILE\}\" PATH)")
165set(SELF_DIR "$\{SELF_DIR\}")
166
Austin Schuh812d0d12021-11-04 20:16:48 -0700167get_property(isMultiConfig GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
168
169if(isMultiConfig)
170 if(NOT "Asan" IN_LIST CMAKE_CONFIGURATION_TYPES)
171 list(APPEND CMAKE_CONFIGURATION_TYPES Asan)
172 endif()
173 if(NOT "Tsan" IN_LIST CMAKE_CONFIGURATION_TYPES)
174 list(APPEND CMAKE_CONFIGURATION_TYPES Tsan)
175 endif()
176 if(NOT "Ubsan" IN_LIST CMAKE_CONFIGURATION_TYPES)
177 list(APPEND CMAKE_CONFIGURATION_TYPES Ubsan)
178 endif()
179else()
180 set(allowedBuildTypes Asan Tsan Ubsan Debug Release RelWithDebInfo MinSizeRel)
181 set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "${allowedBuildTypes}")
182
183 if(CMAKE_BUILD_TYPE AND NOT CMAKE_BUILD_TYPE IN_LIST allowedBuildTypes)
184 message(FATAL_ERROR "Invalid build type: ${CMAKE_BUILD_TYPE}")
185 endif()
186endif()
187
188set(CMAKE_C_FLAGS_ASAN
189 "${CMAKE_C_FLAGS_DEBUG} -fsanitize=address -fno-omit-frame-pointer" CACHE STRING
190 "Flags used by the C compiler for Asan build type or configuration." FORCE)
191
192set(CMAKE_CXX_FLAGS_ASAN
193 "${CMAKE_CXX_FLAGS_DEBUG} -fsanitize=address -fno-omit-frame-pointer" CACHE STRING
194 "Flags used by the C++ compiler for Asan build type or configuration." FORCE)
195
196set(CMAKE_EXE_LINKER_FLAGS_ASAN
197 "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} -fsanitize=address" CACHE STRING
198 "Linker flags to be used to create executables for Asan build type." FORCE)
199
200set(CMAKE_SHARED_LINKER_FLAGS_ASAN
201 "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} -fsanitize=address" CACHE STRING
202 "Linker lags to be used to create shared libraries for Asan build type." FORCE)
203
204set(CMAKE_C_FLAGS_TSAN
205 "${CMAKE_C_FLAGS_DEBUG} -fsanitize=thread -fno-omit-frame-pointer" CACHE STRING
206 "Flags used by the C compiler for Tsan build type or configuration." FORCE)
207
208set(CMAKE_CXX_FLAGS_TSAN
209 "${CMAKE_CXX_FLAGS_DEBUG} -fsanitize=thread -fno-omit-frame-pointer" CACHE STRING
210 "Flags used by the C++ compiler for Tsan build type or configuration." FORCE)
211
212set(CMAKE_EXE_LINKER_FLAGS_TSAN
213 "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} -fsanitize=thread" CACHE STRING
214 "Linker flags to be used to create executables for Tsan build type." FORCE)
215
216set(CMAKE_SHARED_LINKER_FLAGS_TSAN
217 "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} -fsanitize=thread" CACHE STRING
218 "Linker lags to be used to create shared libraries for Tsan build type." FORCE)
219
220set(CMAKE_C_FLAGS_UBSAN
221 "${CMAKE_C_FLAGS_DEBUG} -fsanitize=undefined -fno-sanitize-recover=all -fno-omit-frame-pointer" CACHE STRING
222 "Flags used by the C compiler for Ubsan build type or configuration." FORCE)
223
224set(CMAKE_CXX_FLAGS_UBSAN
225 "${CMAKE_CXX_FLAGS_DEBUG} -fsanitize=undefined -fno-sanitize-recover=all -fno-omit-frame-pointer" CACHE STRING
226 "Flags used by the C++ compiler for Ubsan build type or configuration." FORCE)
227
228set(CMAKE_EXE_LINKER_FLAGS_UBSAN
229 "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} -fsanitize=undefined -fno-sanitize-recover=all" CACHE STRING
230 "Linker flags to be used to create executables for Ubsan build type." FORCE)
231
232set(CMAKE_SHARED_LINKER_FLAGS_UBSAN
233 "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} -fsanitize=undefined" CACHE STRING
234 "Linker lags to be used to create shared libraries for Ubsan build type." FORCE)
235
Brian Silverman8fce7482020-01-05 13:18:21 -0800236if (WITH_TESTS)
237 enable_testing()
238 add_subdirectory(googletest)
239 include(GoogleTest)
240endif()
241
242add_subdirectory(wpiutil)
243add_subdirectory(ntcore)
244
Austin Schuh812d0d12021-11-04 20:16:48 -0700245if (WITH_WPIMATH)
246 add_subdirectory(wpimath)
247endif()
248
Austin Schuh1e69f942020-11-14 15:06:14 -0800249if (WITH_GUI)
250 add_subdirectory(imgui)
251 add_subdirectory(wpigui)
Austin Schuh812d0d12021-11-04 20:16:48 -0700252 add_subdirectory(glass)
253 add_subdirectory(outlineviewer)
254endif()
255
256if (WITH_WPILIB OR WITH_SIMULATION_MODULES)
257 set(HAL_DEP_REPLACE ${HAL_DEP_REPLACE_IMPL})
258 add_subdirectory(hal)
Austin Schuh1e69f942020-11-14 15:06:14 -0800259endif()
260
261if (WITH_CSCORE)
Brian Silverman8fce7482020-01-05 13:18:21 -0800262 set(CSCORE_DEP_REPLACE ${CSCORE_DEP_REPLACE_IMPL})
263 set(CAMERASERVER_DEP_REPLACE ${CAMERASERVER_DEP_REPLACE_IMPL})
264 add_subdirectory(cscore)
265 add_subdirectory(cameraserver)
Austin Schuh812d0d12021-11-04 20:16:48 -0700266endif()
267
268if (WITH_WPILIB)
269 set(WPILIBC_DEP_REPLACE ${WPILIBC_DEP_REPLACE_IMPL})
270 add_subdirectory(wpilibj)
271 add_subdirectory(wpilibc)
272 add_subdirectory(wpilibNewCommands)
273 if (WITH_OLD_COMMANDS)
274 add_subdirectory(wpilibOldCommands)
Brian Silverman8fce7482020-01-05 13:18:21 -0800275 endif()
Austin Schuh812d0d12021-11-04 20:16:48 -0700276 if (WITH_EXAMPLES)
277 add_subdirectory(wpilibcExamples)
278 endif()
279 add_subdirectory(myRobot)
Brian Silverman8fce7482020-01-05 13:18:21 -0800280endif()
281
Austin Schuh1e69f942020-11-14 15:06:14 -0800282if (WITH_SIMULATION_MODULES AND NOT WITH_EXTERNAL_HAL)
Brian Silverman8fce7482020-01-05 13:18:21 -0800283 add_subdirectory(simulation)
284endif()
285
Austin Schuh812d0d12021-11-04 20:16:48 -0700286configure_file(wpilib-config.cmake.in ${WPILIB_BINARY_DIR}/wpilib-config.cmake )
287install(FILES ${WPILIB_BINARY_DIR}/wpilib-config.cmake DESTINATION ${wpilib_config_dir})