blob: 5296f0209c59c00233f6c9d208ee777d3f4f4157 [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
39SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/wpilib/lib")
40
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
46LIST(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${CMAKE_INSTALL_PREFIX}/wpilib/lib" isSystemDir)
47IF("${isSystemDir}" STREQUAL "-1")
48 SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/wpilib/lib")
49ENDIF("${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)
Austin Schuh812d0d12021-11-04 20:16:48 -070055option(WITH_WPIMATH "Build wpimath" ON)
56option(WITH_WPILIB "Build hal, wpilibc/j, and myRobot (needs OpenCV)" ON)
57option(WITH_OLD_COMMANDS "Build old commands" OFF)
58option(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
67# Options for using a package manager (vcpkg) for certain dependencies.
Austin Schuh812d0d12021-11-04 20:16:48 -070068option(USE_VCPKG_FMTLIB "Use vcpkg fmtlib" OFF)
Brian Silverman8fce7482020-01-05 13:18:21 -080069option(USE_VCPKG_LIBUV "Use vcpkg libuv" OFF)
70option(USE_VCPKG_EIGEN "Use vcpkg 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
Austin Schuh812d0d12021-11-04 20:16:48 -0700121if (NOT WITH_WPIMATH AND WITH_WPILIB)
122 message(FATAL_ERROR "
123FATAL: Cannot build wpilib without wpimath.
124 Enable wpimath by setting WITH_WPIMATH=ON
125")
126endif()
127
Brian Silverman8fce7482020-01-05 13:18:21 -0800128set( wpilib_dest wpilib)
129set( include_dest wpilib/include )
130set( main_lib_dest wpilib/lib )
131set( java_lib_dest wpilib/java )
132set( jni_lib_dest wpilib/jni )
133
Austin Schuh1e69f942020-11-14 15:06:14 -0800134if (WITH_FLAT_INSTALL)
Brian Silverman8fce7482020-01-05 13:18:21 -0800135 set (wpilib_config_dir ${wpilib_dest})
136else()
137 set (wpilib_config_dir share/wpilib)
138endif()
139
140if (USE_VCPKG_LIBUV)
141set (LIBUV_VCPKG_REPLACE "find_package(unofficial-libuv CONFIG)")
142endif()
143
144if (USE_VCPKG_EIGEN)
145set (EIGEN_VCPKG_REPLACE "find_package(Eigen3 CONFIG)")
146endif()
147
Austin Schuh75263e32022-02-22 18:05:32 -0800148find_package(LIBSSH 0.7.1)
149
Austin Schuh1e69f942020-11-14 15:06:14 -0800150if (WITH_FLAT_INSTALL)
Brian Silverman8fce7482020-01-05 13:18:21 -0800151set(WPIUTIL_DEP_REPLACE "include($\{SELF_DIR\}/wpiutil-config.cmake)")
152set(NTCORE_DEP_REPLACE "include($\{SELF_DIR\}/ntcore-config.cmake)")
153set(CSCORE_DEP_REPLACE_IMPL "include(\${SELF_DIR}/cscore-config.cmake)")
154set(CAMERASERVER_DEP_REPLACE_IMPL "include(\${SELF_DIR}/cameraserver-config.cmake)")
155set(HAL_DEP_REPLACE_IMPL "include(\${SELF_DIR}/hal-config.cmake)")
Austin Schuh1e69f942020-11-14 15:06:14 -0800156set(WPIMATH_DEP_REPLACE "include($\{SELF_DIR\}/wpimath-config.cmake)")
Brian Silverman8fce7482020-01-05 13:18:21 -0800157set(WPILIBC_DEP_REPLACE_IMPL "include(\${SELF_DIR}/wpilibc-config.cmake)")
Austin Schuh812d0d12021-11-04 20:16:48 -0700158set(WPILIBNEWCOMMANDS_DEP_REPLACE "include(\${SELF_DIR}/wpilibNewcommands-config.cmake)")
159set(WPILIBOLDCOMMANDS_DEP_REPLACE "include(\${SELF_DIR}/wpilibOldcommands-config.cmake)")
Brian Silverman8fce7482020-01-05 13:18:21 -0800160else()
161set(WPIUTIL_DEP_REPLACE "find_dependency(wpiutil)")
162set(NTCORE_DEP_REPLACE "find_dependency(ntcore)")
163set(CSCORE_DEP_REPLACE_IMPL "find_dependency(cscore)")
164set(CAMERASERVER_DEP_REPLACE_IMPL "find_dependency(cameraserver)")
165set(HAL_DEP_REPLACE_IMPL "find_dependency(hal)")
Austin Schuh1e69f942020-11-14 15:06:14 -0800166set(WPIMATH_DEP_REPLACE "find_dependency(wpimath)")
Brian Silverman8fce7482020-01-05 13:18:21 -0800167set(WPILIBC_DEP_REPLACE_IMPL "find_dependency(wpilibc)")
Austin Schuh812d0d12021-11-04 20:16:48 -0700168set(WPILIBNEWCOMMANDS_DEP_REPLACE "find_dependency(wpilibNewCommands)")
169set(WPILIBOLDCOMMANDS_DEP_REPLACE "find_dependency(wpilibOldCommands)")
Brian Silverman8fce7482020-01-05 13:18:21 -0800170endif()
171
172set(FILENAME_DEP_REPLACE "get_filename_component(SELF_DIR \"$\{CMAKE_CURRENT_LIST_FILE\}\" PATH)")
173set(SELF_DIR "$\{SELF_DIR\}")
174
Austin Schuh812d0d12021-11-04 20:16:48 -0700175get_property(isMultiConfig GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
176
177if(isMultiConfig)
178 if(NOT "Asan" IN_LIST CMAKE_CONFIGURATION_TYPES)
179 list(APPEND CMAKE_CONFIGURATION_TYPES Asan)
180 endif()
181 if(NOT "Tsan" IN_LIST CMAKE_CONFIGURATION_TYPES)
182 list(APPEND CMAKE_CONFIGURATION_TYPES Tsan)
183 endif()
184 if(NOT "Ubsan" IN_LIST CMAKE_CONFIGURATION_TYPES)
185 list(APPEND CMAKE_CONFIGURATION_TYPES Ubsan)
186 endif()
187else()
188 set(allowedBuildTypes Asan Tsan Ubsan Debug Release RelWithDebInfo MinSizeRel)
189 set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "${allowedBuildTypes}")
190
191 if(CMAKE_BUILD_TYPE AND NOT CMAKE_BUILD_TYPE IN_LIST allowedBuildTypes)
192 message(FATAL_ERROR "Invalid build type: ${CMAKE_BUILD_TYPE}")
193 endif()
194endif()
195
196set(CMAKE_C_FLAGS_ASAN
197 "${CMAKE_C_FLAGS_DEBUG} -fsanitize=address -fno-omit-frame-pointer" CACHE STRING
198 "Flags used by the C compiler for Asan build type or configuration." FORCE)
199
200set(CMAKE_CXX_FLAGS_ASAN
201 "${CMAKE_CXX_FLAGS_DEBUG} -fsanitize=address -fno-omit-frame-pointer" CACHE STRING
202 "Flags used by the C++ compiler for Asan build type or configuration." FORCE)
203
204set(CMAKE_EXE_LINKER_FLAGS_ASAN
205 "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} -fsanitize=address" CACHE STRING
206 "Linker flags to be used to create executables for Asan build type." FORCE)
207
208set(CMAKE_SHARED_LINKER_FLAGS_ASAN
209 "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} -fsanitize=address" CACHE STRING
210 "Linker lags to be used to create shared libraries for Asan build type." FORCE)
211
212set(CMAKE_C_FLAGS_TSAN
213 "${CMAKE_C_FLAGS_DEBUG} -fsanitize=thread -fno-omit-frame-pointer" CACHE STRING
214 "Flags used by the C compiler for Tsan build type or configuration." FORCE)
215
216set(CMAKE_CXX_FLAGS_TSAN
217 "${CMAKE_CXX_FLAGS_DEBUG} -fsanitize=thread -fno-omit-frame-pointer" CACHE STRING
218 "Flags used by the C++ compiler for Tsan build type or configuration." FORCE)
219
220set(CMAKE_EXE_LINKER_FLAGS_TSAN
221 "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} -fsanitize=thread" CACHE STRING
222 "Linker flags to be used to create executables for Tsan build type." FORCE)
223
224set(CMAKE_SHARED_LINKER_FLAGS_TSAN
225 "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} -fsanitize=thread" CACHE STRING
226 "Linker lags to be used to create shared libraries for Tsan build type." FORCE)
227
228set(CMAKE_C_FLAGS_UBSAN
229 "${CMAKE_C_FLAGS_DEBUG} -fsanitize=undefined -fno-sanitize-recover=all -fno-omit-frame-pointer" CACHE STRING
230 "Flags used by the C compiler for Ubsan build type or configuration." FORCE)
231
232set(CMAKE_CXX_FLAGS_UBSAN
233 "${CMAKE_CXX_FLAGS_DEBUG} -fsanitize=undefined -fno-sanitize-recover=all -fno-omit-frame-pointer" CACHE STRING
234 "Flags used by the C++ compiler for Ubsan build type or configuration." FORCE)
235
236set(CMAKE_EXE_LINKER_FLAGS_UBSAN
237 "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} -fsanitize=undefined -fno-sanitize-recover=all" CACHE STRING
238 "Linker flags to be used to create executables for Ubsan build type." FORCE)
239
240set(CMAKE_SHARED_LINKER_FLAGS_UBSAN
241 "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} -fsanitize=undefined" CACHE STRING
242 "Linker lags to be used to create shared libraries for Ubsan build type." FORCE)
243
Brian Silverman8fce7482020-01-05 13:18:21 -0800244if (WITH_TESTS)
245 enable_testing()
246 add_subdirectory(googletest)
247 include(GoogleTest)
248endif()
249
250add_subdirectory(wpiutil)
251add_subdirectory(ntcore)
252
Austin Schuh812d0d12021-11-04 20:16:48 -0700253if (WITH_WPIMATH)
254 add_subdirectory(wpimath)
255endif()
256
Austin Schuh1e69f942020-11-14 15:06:14 -0800257if (WITH_GUI)
Austin Schuh75263e32022-02-22 18:05:32 -0800258 add_subdirectory(fieldImages)
Austin Schuh1e69f942020-11-14 15:06:14 -0800259 add_subdirectory(imgui)
260 add_subdirectory(wpigui)
Austin Schuh812d0d12021-11-04 20:16:48 -0700261 add_subdirectory(glass)
262 add_subdirectory(outlineviewer)
Austin Schuh75263e32022-02-22 18:05:32 -0800263 if (LIBSSH_FOUND)
264 add_subdirectory(roborioteamnumbersetter)
265 endif()
Austin Schuh812d0d12021-11-04 20:16:48 -0700266endif()
267
268if (WITH_WPILIB OR WITH_SIMULATION_MODULES)
269 set(HAL_DEP_REPLACE ${HAL_DEP_REPLACE_IMPL})
270 add_subdirectory(hal)
Austin Schuh1e69f942020-11-14 15:06:14 -0800271endif()
272
273if (WITH_CSCORE)
Brian Silverman8fce7482020-01-05 13:18:21 -0800274 set(CSCORE_DEP_REPLACE ${CSCORE_DEP_REPLACE_IMPL})
275 set(CAMERASERVER_DEP_REPLACE ${CAMERASERVER_DEP_REPLACE_IMPL})
276 add_subdirectory(cscore)
277 add_subdirectory(cameraserver)
Austin Schuh812d0d12021-11-04 20:16:48 -0700278endif()
279
280if (WITH_WPILIB)
281 set(WPILIBC_DEP_REPLACE ${WPILIBC_DEP_REPLACE_IMPL})
282 add_subdirectory(wpilibj)
283 add_subdirectory(wpilibc)
284 add_subdirectory(wpilibNewCommands)
285 if (WITH_OLD_COMMANDS)
286 add_subdirectory(wpilibOldCommands)
Brian Silverman8fce7482020-01-05 13:18:21 -0800287 endif()
Austin Schuh812d0d12021-11-04 20:16:48 -0700288 if (WITH_EXAMPLES)
289 add_subdirectory(wpilibcExamples)
290 endif()
291 add_subdirectory(myRobot)
Brian Silverman8fce7482020-01-05 13:18:21 -0800292endif()
293
Austin Schuh1e69f942020-11-14 15:06:14 -0800294if (WITH_SIMULATION_MODULES AND NOT WITH_EXTERNAL_HAL)
Brian Silverman8fce7482020-01-05 13:18:21 -0800295 add_subdirectory(simulation)
296endif()
297
Austin Schuh812d0d12021-11-04 20:16:48 -0700298configure_file(wpilib-config.cmake.in ${WPILIB_BINARY_DIR}/wpilib-config.cmake )
299install(FILES ${WPILIB_BINARY_DIR}/wpilib-config.cmake DESTINATION ${wpilib_config_dir})