blob: 076eca223223ce93d4fd1a96430dad17a031e6bd [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
Austin Schuh75263e32022-02-22 18:05:32 -08009if ("${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Windows")
10 set(CMAKE_SYSTEM_VERSION 10.0.18362.0 CACHE STRING INTERNAL FORCE)
11 set(CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION 10.0.18362.0 CACHE STRING INTERNAL FORCE)
12endif()
Brian Silverman8fce7482020-01-05 13:18:21 -080013
14project(allwpilib)
15cmake_minimum_required(VERSION 3.3.0)
Austin Schuh812d0d12021-11-04 20:16:48 -070016set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules")
17
Austin Schuh75263e32022-02-22 18:05:32 -080018message(STATUS "Platform version: ${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION}")
19
Austin Schuh812d0d12021-11-04 20:16:48 -070020set(WPILIB_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
Brian Silverman8fce7482020-01-05 13:18:21 -080021
22INCLUDE(CPack)
23
24set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS TRUE)
25set_property(GLOBAL PROPERTY USE_FOLDERS ON)
26
Austin Schuh812d0d12021-11-04 20:16:48 -070027set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${WPILIB_BINARY_DIR}/lib)
28set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${WPILIB_BINARY_DIR}/lib)
29set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${WPILIB_BINARY_DIR}/bin)
30set(CMAKE_JAVA_TARGET_OUTPUT_DIR ${WPILIB_BINARY_DIR}/jar)
Brian Silverman8fce7482020-01-05 13:18:21 -080031
32# use, i.e. don't skip the full RPATH for the build tree
33SET(CMAKE_SKIP_BUILD_RPATH FALSE)
34
35# when building, don't use the install RPATH already
36# (but later on when installing)
37SET(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
38
James Kuszmaulcf324122023-01-14 14:07:17 -080039SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
Brian Silverman8fce7482020-01-05 13:18:21 -080040
41# add the automatically determined parts of the RPATH
42# which point to directories outside the build tree to the install RPATH
43SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
44
45# the RPATH to be used when installing, but only if it's not a system directory
James Kuszmaulcf324122023-01-14 14:07:17 -080046LIST(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${CMAKE_INSTALL_PREFIX}/lib" isSystemDir)
Brian Silverman8fce7482020-01-05 13:18:21 -080047IF("${isSystemDir}" STREQUAL "-1")
James Kuszmaulcf324122023-01-14 14:07:17 -080048 SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
Brian Silverman8fce7482020-01-05 13:18:21 -080049ENDIF("${isSystemDir}" STREQUAL "-1")
50
Austin Schuh1e69f942020-11-14 15:06:14 -080051# Options for building certain parts of the repo. Everything is built by default.
52option(BUILD_SHARED_LIBS "Build with shared libs (needed for JNI)" ON)
53option(WITH_JAVA "Include java and JNI in the build" ON)
54option(WITH_CSCORE "Build cscore (needs OpenCV)" ON)
James Kuszmaulcf324122023-01-14 14:07:17 -080055option(WITH_NTCORE "Build ntcore" ON)
Austin Schuh812d0d12021-11-04 20:16:48 -070056option(WITH_WPIMATH "Build wpimath" ON)
57option(WITH_WPILIB "Build hal, wpilibc/j, and myRobot (needs OpenCV)" ON)
Austin Schuh812d0d12021-11-04 20:16:48 -070058option(WITH_EXAMPLES "Build examples" OFF)
Austin Schuh1e69f942020-11-14 15:06:14 -080059option(WITH_TESTS "Build unit tests (requires internet connection)" ON)
60option(WITH_GUI "Build GUI items" ON)
61option(WITH_SIMULATION_MODULES "Build simulation modules" ON)
62
63# Options for external HAL.
64option(WITH_EXTERNAL_HAL "Use a separately built HAL" OFF)
Brian Silverman8fce7482020-01-05 13:18:21 -080065set(EXTERNAL_HAL_FILE "" CACHE FILEPATH "Location to look for an external HAL CMake File")
Austin Schuh1e69f942020-11-14 15:06:14 -080066
James Kuszmaulcf324122023-01-14 14:07:17 -080067# Options for using a package manager (e.g., vcpkg) for certain dependencies.
68option(USE_SYSTEM_FMTLIB "Use system fmtlib" OFF)
69option(USE_SYSTEM_LIBUV "Use system libuv" OFF)
70option(USE_SYSTEM_EIGEN "Use system eigen" OFF)
Brian Silverman8fce7482020-01-05 13:18:21 -080071
Austin Schuh1e69f942020-11-14 15:06:14 -080072# Options for installation.
73option(WITH_FLAT_INSTALL "Use a flat install directory" OFF)
74
75# Options for location of OpenCV Java.
76set(OPENCV_JAVA_INSTALL_DIR "" CACHE PATH "Location to search for the OpenCV jar file")
77
78# Set default build type to release with debug info (i.e. release mode optimizations
79# are performed, but debug info still exists).
80if (NOT CMAKE_BUILD_TYPE)
81 set (CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE STRING "" FORCE)
82endif()
83
84# We always want flat install with MSVC.
85if (MSVC)
86 set(WITH_FLAT_INSTALL ON)
87endif()
88
89if (WITH_JAVA AND NOT BUILD_SHARED_LIBS)
Brian Silverman8fce7482020-01-05 13:18:21 -080090 message(FATAL_ERROR "
91FATAL: Cannot build static libs with Java enabled.
92 Static libs requires both BUILD_SHARED_LIBS=OFF and
Austin Schuh1e69f942020-11-14 15:06:14 -080093 WITH_JAVA=OFF
94")
95endif()
96
97if (WITH_SIMULATION_MODULES AND NOT BUILD_SHARED_LIBS)
98 message(FATAL_ERROR "
99FATAL: Cannot build static libs with simulation modules enabled.
100 Static libs requires both BUILD_SHARED_LIBS=OFF and
101 WITH_SIMULATION_MODULES=OFF
102")
103endif()
104
105if (NOT WITH_JAVA OR NOT WITH_CSCORE)
106 if(NOT "${OPENCV_JAVA_INSTALL_DIR}" STREQUAL "")
107 message(WARNING "
108WARNING: OpenCV Java dir set but java is not enabled!
109It will be ignored.
110")
111 endif()
112endif()
113
114if (NOT WITH_WPILIB AND WITH_SIMULATION_MODULES)
115 message(FATAL_ERROR "
116FATAL: Cannot build simulation modules with wpilib disabled.
117 Enable wpilib by setting WITH_WPILIB=ON
Brian Silverman8fce7482020-01-05 13:18:21 -0800118")
119endif()
120
James Kuszmaulcf324122023-01-14 14:07:17 -0800121if (NOT WITH_NTCORE AND WITH_CSCORE)
122 message(FATAL_ERROR "
123FATAL: Cannot build cameraserver without ntcore.
124 Enable ntcore by setting WITH_NTCORE=ON
125")
126endif()
127
128if (NOT WITH_NTCORE AND WITH_GUI)
129 message(FATAL_ERROR "
130FATAL: Cannot build GUI modules without ntcore.
131 Enable ntcore by setting WITH_NTCORE=ON
132")
133endif()
134
135if (NOT WITH_NTCORE AND WITH_SIMULATION_MODULES)
136 message(FATAL_ERROR "
137FATAL: Cannot build simulation modules without ntcore.
138 Enable ntcore by setting WITH_NTCORE=ON
139")
140endif()
141
142if (NOT WITH_NTCORE AND WITH_WPILIB)
143 message(FATAL_ERROR "
144FATAL: Cannot build wpilib without ntcore.
145 Enable ntcore by setting WITH_NTCORE=ON
146")
147endif()
148
Austin Schuh812d0d12021-11-04 20:16:48 -0700149if (NOT WITH_WPIMATH AND WITH_WPILIB)
150 message(FATAL_ERROR "
151FATAL: Cannot build wpilib without wpimath.
152 Enable wpimath by setting WITH_WPIMATH=ON
153")
154endif()
155
James Kuszmaulcf324122023-01-14 14:07:17 -0800156set( wpilib_dest "")
157set( include_dest include )
158set( main_lib_dest lib )
159set( java_lib_dest java )
160set( jni_lib_dest jni )
Brian Silverman8fce7482020-01-05 13:18:21 -0800161
Austin Schuh1e69f942020-11-14 15:06:14 -0800162if (WITH_FLAT_INSTALL)
Brian Silverman8fce7482020-01-05 13:18:21 -0800163 set (wpilib_config_dir ${wpilib_dest})
164else()
165 set (wpilib_config_dir share/wpilib)
166endif()
167
James Kuszmaulcf324122023-01-14 14:07:17 -0800168if (USE_SYSTEM_LIBUV)
169set (LIBUV_SYSTEM_REPLACE "
170find_package(PkgConfig REQUIRED)
171pkg_check_modules(libuv REQUIRED IMPORTED_TARGET libuv)
172")
Brian Silverman8fce7482020-01-05 13:18:21 -0800173endif()
174
James Kuszmaulcf324122023-01-14 14:07:17 -0800175if (USE_SYSTEM_EIGEN)
176set (EIGEN_SYSTEM_REPLACE "find_package(Eigen3 CONFIG)")
Brian Silverman8fce7482020-01-05 13:18:21 -0800177endif()
178
Austin Schuh75263e32022-02-22 18:05:32 -0800179find_package(LIBSSH 0.7.1)
180
Austin Schuh1e69f942020-11-14 15:06:14 -0800181if (WITH_FLAT_INSTALL)
Brian Silverman8fce7482020-01-05 13:18:21 -0800182set(WPIUTIL_DEP_REPLACE "include($\{SELF_DIR\}/wpiutil-config.cmake)")
James Kuszmaulcf324122023-01-14 14:07:17 -0800183set(WPINET_DEP_REPLACE "include($\{SELF_DIR\}/wpinet-config.cmake)")
Brian Silverman8fce7482020-01-05 13:18:21 -0800184set(NTCORE_DEP_REPLACE "include($\{SELF_DIR\}/ntcore-config.cmake)")
185set(CSCORE_DEP_REPLACE_IMPL "include(\${SELF_DIR}/cscore-config.cmake)")
186set(CAMERASERVER_DEP_REPLACE_IMPL "include(\${SELF_DIR}/cameraserver-config.cmake)")
187set(HAL_DEP_REPLACE_IMPL "include(\${SELF_DIR}/hal-config.cmake)")
Austin Schuh1e69f942020-11-14 15:06:14 -0800188set(WPIMATH_DEP_REPLACE "include($\{SELF_DIR\}/wpimath-config.cmake)")
Brian Silverman8fce7482020-01-05 13:18:21 -0800189set(WPILIBC_DEP_REPLACE_IMPL "include(\${SELF_DIR}/wpilibc-config.cmake)")
Austin Schuh812d0d12021-11-04 20:16:48 -0700190set(WPILIBNEWCOMMANDS_DEP_REPLACE "include(\${SELF_DIR}/wpilibNewcommands-config.cmake)")
Brian Silverman8fce7482020-01-05 13:18:21 -0800191else()
192set(WPIUTIL_DEP_REPLACE "find_dependency(wpiutil)")
James Kuszmaulcf324122023-01-14 14:07:17 -0800193set(WPINET_DEP_REPLACE "find_dependency(wpinet)")
Brian Silverman8fce7482020-01-05 13:18:21 -0800194set(NTCORE_DEP_REPLACE "find_dependency(ntcore)")
195set(CSCORE_DEP_REPLACE_IMPL "find_dependency(cscore)")
196set(CAMERASERVER_DEP_REPLACE_IMPL "find_dependency(cameraserver)")
197set(HAL_DEP_REPLACE_IMPL "find_dependency(hal)")
Austin Schuh1e69f942020-11-14 15:06:14 -0800198set(WPIMATH_DEP_REPLACE "find_dependency(wpimath)")
Brian Silverman8fce7482020-01-05 13:18:21 -0800199set(WPILIBC_DEP_REPLACE_IMPL "find_dependency(wpilibc)")
Austin Schuh812d0d12021-11-04 20:16:48 -0700200set(WPILIBNEWCOMMANDS_DEP_REPLACE "find_dependency(wpilibNewCommands)")
Brian Silverman8fce7482020-01-05 13:18:21 -0800201endif()
202
203set(FILENAME_DEP_REPLACE "get_filename_component(SELF_DIR \"$\{CMAKE_CURRENT_LIST_FILE\}\" PATH)")
204set(SELF_DIR "$\{SELF_DIR\}")
205
Austin Schuh812d0d12021-11-04 20:16:48 -0700206get_property(isMultiConfig GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
207
208if(isMultiConfig)
209 if(NOT "Asan" IN_LIST CMAKE_CONFIGURATION_TYPES)
210 list(APPEND CMAKE_CONFIGURATION_TYPES Asan)
211 endif()
212 if(NOT "Tsan" IN_LIST CMAKE_CONFIGURATION_TYPES)
213 list(APPEND CMAKE_CONFIGURATION_TYPES Tsan)
214 endif()
215 if(NOT "Ubsan" IN_LIST CMAKE_CONFIGURATION_TYPES)
216 list(APPEND CMAKE_CONFIGURATION_TYPES Ubsan)
217 endif()
218else()
219 set(allowedBuildTypes Asan Tsan Ubsan Debug Release RelWithDebInfo MinSizeRel)
220 set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "${allowedBuildTypes}")
221
222 if(CMAKE_BUILD_TYPE AND NOT CMAKE_BUILD_TYPE IN_LIST allowedBuildTypes)
223 message(FATAL_ERROR "Invalid build type: ${CMAKE_BUILD_TYPE}")
224 endif()
225endif()
226
227set(CMAKE_C_FLAGS_ASAN
228 "${CMAKE_C_FLAGS_DEBUG} -fsanitize=address -fno-omit-frame-pointer" CACHE STRING
229 "Flags used by the C compiler for Asan build type or configuration." FORCE)
230
231set(CMAKE_CXX_FLAGS_ASAN
232 "${CMAKE_CXX_FLAGS_DEBUG} -fsanitize=address -fno-omit-frame-pointer" CACHE STRING
233 "Flags used by the C++ compiler for Asan build type or configuration." FORCE)
234
235set(CMAKE_EXE_LINKER_FLAGS_ASAN
236 "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} -fsanitize=address" CACHE STRING
237 "Linker flags to be used to create executables for Asan build type." FORCE)
238
239set(CMAKE_SHARED_LINKER_FLAGS_ASAN
240 "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} -fsanitize=address" CACHE STRING
241 "Linker lags to be used to create shared libraries for Asan build type." FORCE)
242
243set(CMAKE_C_FLAGS_TSAN
244 "${CMAKE_C_FLAGS_DEBUG} -fsanitize=thread -fno-omit-frame-pointer" CACHE STRING
245 "Flags used by the C compiler for Tsan build type or configuration." FORCE)
246
247set(CMAKE_CXX_FLAGS_TSAN
248 "${CMAKE_CXX_FLAGS_DEBUG} -fsanitize=thread -fno-omit-frame-pointer" CACHE STRING
249 "Flags used by the C++ compiler for Tsan build type or configuration." FORCE)
250
251set(CMAKE_EXE_LINKER_FLAGS_TSAN
252 "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} -fsanitize=thread" CACHE STRING
253 "Linker flags to be used to create executables for Tsan build type." FORCE)
254
255set(CMAKE_SHARED_LINKER_FLAGS_TSAN
256 "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} -fsanitize=thread" CACHE STRING
257 "Linker lags to be used to create shared libraries for Tsan build type." FORCE)
258
259set(CMAKE_C_FLAGS_UBSAN
260 "${CMAKE_C_FLAGS_DEBUG} -fsanitize=undefined -fno-sanitize-recover=all -fno-omit-frame-pointer" CACHE STRING
261 "Flags used by the C compiler for Ubsan build type or configuration." FORCE)
262
263set(CMAKE_CXX_FLAGS_UBSAN
264 "${CMAKE_CXX_FLAGS_DEBUG} -fsanitize=undefined -fno-sanitize-recover=all -fno-omit-frame-pointer" CACHE STRING
265 "Flags used by the C++ compiler for Ubsan build type or configuration." FORCE)
266
267set(CMAKE_EXE_LINKER_FLAGS_UBSAN
268 "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} -fsanitize=undefined -fno-sanitize-recover=all" CACHE STRING
269 "Linker flags to be used to create executables for Ubsan build type." FORCE)
270
271set(CMAKE_SHARED_LINKER_FLAGS_UBSAN
272 "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} -fsanitize=undefined" CACHE STRING
273 "Linker lags to be used to create shared libraries for Ubsan build type." FORCE)
274
Brian Silverman8fce7482020-01-05 13:18:21 -0800275if (WITH_TESTS)
276 enable_testing()
277 add_subdirectory(googletest)
278 include(GoogleTest)
279endif()
280
281add_subdirectory(wpiutil)
James Kuszmaulcf324122023-01-14 14:07:17 -0800282
283if (WITH_NTCORE)
284 add_subdirectory(wpinet)
285 add_subdirectory(ntcore)
286endif()
Brian Silverman8fce7482020-01-05 13:18:21 -0800287
Austin Schuh812d0d12021-11-04 20:16:48 -0700288if (WITH_WPIMATH)
289 add_subdirectory(wpimath)
290endif()
291
Austin Schuh1e69f942020-11-14 15:06:14 -0800292if (WITH_GUI)
Austin Schuh75263e32022-02-22 18:05:32 -0800293 add_subdirectory(fieldImages)
Austin Schuh1e69f942020-11-14 15:06:14 -0800294 add_subdirectory(imgui)
295 add_subdirectory(wpigui)
Austin Schuh812d0d12021-11-04 20:16:48 -0700296 add_subdirectory(glass)
297 add_subdirectory(outlineviewer)
Austin Schuh75263e32022-02-22 18:05:32 -0800298 if (LIBSSH_FOUND)
299 add_subdirectory(roborioteamnumbersetter)
James Kuszmaulcf324122023-01-14 14:07:17 -0800300 add_subdirectory(datalogtool)
Austin Schuh75263e32022-02-22 18:05:32 -0800301 endif()
Austin Schuh812d0d12021-11-04 20:16:48 -0700302endif()
303
304if (WITH_WPILIB OR WITH_SIMULATION_MODULES)
305 set(HAL_DEP_REPLACE ${HAL_DEP_REPLACE_IMPL})
306 add_subdirectory(hal)
Austin Schuh1e69f942020-11-14 15:06:14 -0800307endif()
308
309if (WITH_CSCORE)
Brian Silverman8fce7482020-01-05 13:18:21 -0800310 set(CSCORE_DEP_REPLACE ${CSCORE_DEP_REPLACE_IMPL})
311 set(CAMERASERVER_DEP_REPLACE ${CAMERASERVER_DEP_REPLACE_IMPL})
312 add_subdirectory(cscore)
313 add_subdirectory(cameraserver)
Austin Schuh812d0d12021-11-04 20:16:48 -0700314endif()
315
316if (WITH_WPILIB)
317 set(WPILIBC_DEP_REPLACE ${WPILIBC_DEP_REPLACE_IMPL})
James Kuszmaulcf324122023-01-14 14:07:17 -0800318 add_subdirectory(apriltag)
Austin Schuh812d0d12021-11-04 20:16:48 -0700319 add_subdirectory(wpilibj)
320 add_subdirectory(wpilibc)
321 add_subdirectory(wpilibNewCommands)
Austin Schuh812d0d12021-11-04 20:16:48 -0700322 if (WITH_EXAMPLES)
323 add_subdirectory(wpilibcExamples)
324 endif()
325 add_subdirectory(myRobot)
Brian Silverman8fce7482020-01-05 13:18:21 -0800326endif()
327
Austin Schuh1e69f942020-11-14 15:06:14 -0800328if (WITH_SIMULATION_MODULES AND NOT WITH_EXTERNAL_HAL)
Brian Silverman8fce7482020-01-05 13:18:21 -0800329 add_subdirectory(simulation)
330endif()
331
Austin Schuh812d0d12021-11-04 20:16:48 -0700332configure_file(wpilib-config.cmake.in ${WPILIB_BINARY_DIR}/wpilib-config.cmake )
333install(FILES ${WPILIB_BINARY_DIR}/wpilib-config.cmake DESTINATION ${wpilib_config_dir})