Reactos
1
2cmake_minimum_required(VERSION 3.17.0)
3
4if(NOT CMAKE_VERSION MATCHES "ReactOS")
5 message(WARNING "Building with \"${CMAKE_COMMAND}\", which is not the custom CMake included in RosBE, might cause build issues...")
6endif()
7
8include(CMakeDependentOption)
9
10# CMAKE_CROSSCOMPILING and MSVC_IDE are not set until project() is called, so let's test this instead
11if ((DEFINED CMAKE_TOOLCHAIN_FILE) AND (CMAKE_GENERATOR MATCHES "Visual Studio.*"))
12# Do not use MSVC_RUNTIME_LIBRARY target property. We use our own flags instead
13message(WARNING "Setting policy CMP0091 to OLD behaviour")
14cmake_policy(SET CMP0091 OLD)
15endif()
16
17project(REACTOS)
18
19set(CMAKE_INCLUDE_CURRENT_DIR ON)
20set(CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE ON)
21set(CMAKE_SHARED_LIBRARY_PREFIX "")
22set(CMAKE_SHARED_MODULE_PREFIX "")
23set(CMAKE_SKIP_PREPROCESSED_SOURCE_RULES TRUE)
24set(CMAKE_SKIP_ASSEMBLY_SOURCE_RULES TRUE)
25set(CMAKE_COLOR_MAKEFILE OFF)
26set(CMAKE_POSITION_INDEPENDENT_CODE OFF)
27set(CMAKE_C_STANDARD 99)
28set(CMAKE_CXX_STANDARD 11)
29#set_property(GLOBAL PROPERTY RULE_MESSAGES OFF)
30
31# check that the ARCH (target architecture) variable is defined
32if(NOT ARCH)
33 message(FATAL_ERROR "Target architecture (ARCH) is not defined. Please, choose one of: i386, amd64, arm, arm64")
34endif()
35# Now the ARCH variable will be in lowercase.
36# It is needed because STREQUAL comparison
37# is case-sensitive.
38# See http://cmake.3232098.n2.nabble.com/Case-insensitive-string-compare-td7580269.html
39# for more information.
40string(TOLOWER ${ARCH} ARCH)
41
42# set possible values for cmake GUI
43set_property(CACHE ARCH PROPERTY STRINGS "i386" "amd64" "arm" "arm64")
44
45# Alternative WinNT-compatible architecture string
46if(ARCH STREQUAL "i386")
47 set(WINARCH "x86")
48else()
49 set(WINARCH ${ARCH})
50endif()
51
52# set CMAKE_BUILD_TYPE if not set
53if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
54 message(STATUS "Setting build type to Debug as none was specified.")
55 set(CMAKE_BUILD_TYPE "Debug" CACHE
56 STRING "Choose the type of build." FORCE)
57 # Set the possible values of build type for cmake-gui
58 set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS
59 "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
60endif()
61
62# Versioning
63include(sdk/include/reactos/version.cmake)
64
65# Compile options
66include(sdk/cmake/config.cmake)
67
68# Compiler flags handling
69include(sdk/cmake/compilerflags.cmake)
70
71# set_wine_module function
72include(sdk/cmake/set_wine_module.cmake)
73
74add_definitions(
75 -D__REACTOS__
76 # swprintf without count argument is used in most of the codebase
77 -D_CRT_NON_CONFORMING_SWPRINTFS
78)
79
80# There doesn't seem to be a standard for __FILE__ being relative or absolute, so detect it at runtime.
81file(RELATIVE_PATH _PATH_PREFIX ${REACTOS_BINARY_DIR} ${REACTOS_SOURCE_DIR})
82if (NOT MSVC AND ((CMAKE_C_COMPILER_ID STREQUAL "GNU") AND (CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL "8.0.0")
83 OR (CMAKE_C_COMPILER_ID STREQUAL "Clang") AND (CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL "10.0.0")))
84 # Thankfully, GCC has this
85 add_compile_options(-ffile-prefix-map=${REACTOS_SOURCE_DIR}=)
86 add_compile_options(-ffile-prefix-map=${_PATH_PREFIX}=)
87else()
88 string(LENGTH ${_PATH_PREFIX} _PATH_PREFIX_LENGTH)
89 string(LENGTH ${REACTOS_SOURCE_DIR} REACTOS_SOURCE_DIR_LENGTH)
90 math(EXPR REACTOS_SOURCE_DIR_LENGTH "${REACTOS_SOURCE_DIR_LENGTH} + 1")
91 add_compile_definitions("$<$<COMPILE_LANGUAGE:C,CXX>:__RELFILE__=&__FILE__[__FILE__[0] == '.' ? ${_PATH_PREFIX_LENGTH} : ${REACTOS_SOURCE_DIR_LENGTH}]>")
92endif()
93
94if(MSVC_IDE)
95 add_compile_options("/MP")
96endif()
97
98# Bison and Flex support
99find_package(BISON REQUIRED)
100find_package(FLEX REQUIRED)
101
102if(MSVC_IDE)
103 # Bison needs M4 and BISON_PKGDATADIR set at build time,
104 # but visual studio is hardly ever opened from the configure-time environment.
105 # Since cmake does not support setting env variables for a custom command,
106 # we have to write a wrapper that sets the variables and then executes bison.
107 # Idea taken from https://stackoverflow.com/a/35032051/4928207
108 if(DEFINED ENV{M4})
109 # Store this environment variable for configure re-runs from withing visual studio.
110 SET(ROS_SAVED_M4 "$ENV{M4}" CACHE INTERNAL "")
111 endif()
112 if(DEFINED ENV{BISON_PKGDATADIR})
113 SET(ROS_SAVED_BISON_PKGDATADIR "$ENV{BISON_PKGDATADIR}" CACHE INTERNAL "")
114 endif()
115
116 # Tell the user about a misconfigured environment
117 if("x${ROS_SAVED_M4}x" STREQUAL "xx" OR "x${ROS_SAVED_BISON_PKGDATADIR}x" STREQUAL "xx")
118 message(FATAL_ERROR "\nM4 or BISON_PKGDATADIR environment variables not set, cannot continue!\n"
119 "See https://reactos.org/wiki/Visual_Studio for more information!")
120 endif()
121
122 file(WRITE "${CMAKE_BINARY_DIR}/bison_wrapper.cmd"
123 "@ECHO OFF\n"
124 "set M4=${ROS_SAVED_M4}\n"
125 "set BISON_PKGDATADIR=${ROS_SAVED_BISON_PKGDATADIR}\n"
126 "${BISON_EXECUTABLE} %*\n")
127 set(BISON_EXECUTABLE "${CMAKE_BINARY_DIR}/bison_wrapper.cmd")
128 # And the same hacks for FLEX
129 file(WRITE "${CMAKE_BINARY_DIR}/flex_wrapper.cmd"
130 "@ECHO OFF\n"
131 "set M4=${ROS_SAVED_M4}\n"
132 "set BISON_PKGDATADIR=${ROS_SAVED_BISON_PKGDATADIR}\n"
133 "${FLEX_EXECUTABLE} %*\n")
134 set(FLEX_EXECUTABLE "${CMAKE_BINARY_DIR}/flex_wrapper.cmd")
135endif()
136
137if(NOT CMAKE_CROSSCOMPILING)
138 set(TOOLS_FOLDER ${CMAKE_CURRENT_BINARY_DIR})
139 add_definitions(-DTARGET_${ARCH})
140
141 if(MSVC)
142 if(ARCH STREQUAL "i386")
143 add_definitions(/D_X86_ /D__i386__ /DWIN32 /D_WINDOWS)
144 elseif(ARCH STREQUAL "amd64")
145 add_definitions(-D_AMD64_ -D__x86_64__ /DWIN32 -D_WINDOWS)
146 elseif(ARCH STREQUAL "arm")
147 add_definitions(-D__arm__)
148 elseif(ARCH STREQUAL "arm64")
149 add_definitions(-D__aarch64__)
150 endif()
151 if(MSVC_VERSION GREATER 1699)
152 add_definitions(/D_ALLOW_KEYWORD_MACROS)
153 endif()
154 endif()
155 add_subdirectory(sdk/include/host)
156
157 add_subdirectory(dll/win32/dbghelp)
158 add_subdirectory(sdk/tools)
159 add_subdirectory(sdk/lib)
160
161 set(NATIVE_TARGETS asmpp bin2c widl gendib cabman fatten hpp isohybrid mkhive mkisofs obj2bin spec2def geninc mkshelllink utf16le xml2sdb)
162 if(NOT MSVC)
163 list(APPEND NATIVE_TARGETS pefixup)
164 if (ARCH STREQUAL "i386")
165 list(APPEND NATIVE_TARGETS rsym)
166 endif()
167 endif()
168
169 install(TARGETS ${NATIVE_TARGETS})
170else()
171 # Add host tools target
172 include(sdk/cmake/host-tools.cmake)
173 setup_host_tools()
174
175 # We don't need CMake importlib handling.
176 unset(CMAKE_IMPORT_LIBRARY_SUFFIX)
177
178 # Print build type(s)
179 if(CMAKE_CONFIGURATION_TYPES)
180 # Multi-config generators, like Visual Studio (MSBuild).
181 message("-- Configuration types: ${CMAKE_CONFIGURATION_TYPES}")
182 else()
183 # Single-configuration generators, like Ninja.
184 message("-- Build type: ${CMAKE_BUILD_TYPE}")
185 endif()
186
187 # Always add /MT in VS CMAKE_GENERATOR and define _SBCS otherwise VS thinks it's a multi-byte or whatever project
188 if (MSVC_IDE)
189 add_compile_options("/MT")
190 add_compile_definitions(_SBCS)
191 endif()
192
193
194 # adjust the default behaviour of the FIND_XXX() commands:
195 # search headers and libraries in the target environment, search
196 # programs in the host environment
197 set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
198 set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY NEVER)
199 set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE NEVER)
200
201 # Add our own target properties
202 # General module definitions
203 define_property(TARGET PROPERTY REACTOS_MODULE_TYPE
204 BRIEF_DOCS "The type of this module"
205 FULL_DOCS [[
206The type of this module.
207One of "nativecui", "nativedll", "kernelmodedriver", "wdmdriver", "kerneldll", "win32cui", "win32gui", "win32dll", "win32ocx", "cpl" or "module"]])
208
209 # C++
210 define_property(TARGET PROPERTY WITH_CXX_EXCEPTIONS
211 BRIEF_DOCS "Enable C++ exceptions on this target"
212 FULL_DOCS [[
213Enables C++ exception handling.
214Enable this if the module uses try/catch or throw. You might also need this if you use a standard operator new (the one without nothrow).]])
215 define_property(TARGET PROPERTY WITH_CXX_RTTI
216 BRIEF_DOCS "Enable C++ RTTI on this target"
217 FULL_DOCS [[
218Enables run-time type information.
219Enable this if the module uses typeid or dynamic_cast. You will probably need to link yith cpprt as well, if you are not already using STL.]])
220
221
222 if(DBG)
223 add_definitions(-DDBG=1 -D_SEH_ENABLE_TRACE)
224 else()
225 add_definitions(-DDBG=0)
226 endif()
227
228 if(ENABLE_CCACHE)
229 message(WARNING "-- Disabling precompiled headers support (ccache).")
230 option(PCH "Whether to use precompiled headers" OFF)
231 set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache)
232 elseif(CMAKE_C_COMPILER_ID STREQUAL "GNU")
233 message(WARNING "-- Disabling precompiled headers on GCC by default CORE-17108.")
234 option(PCH "Whether to use precompiled headers" OFF)
235 else()
236 option(PCH "Whether to use precompiled headers" ON)
237 endif()
238
239 # Version Options
240 add_definitions(-DWINVER=0x502
241 -D_WIN32_IE=0x603
242 -D_WIN32_WINNT=0x502
243 -D_WIN32_WINDOWS=0x502
244 -D_SETUPAPI_VER=0x502
245 -DMINGW_HAS_SECURE_API=1
246 -DD3D_UMD_INTERFACE_VERSION=0x000C # Vista
247 -DDXGKDDI_INTERFACE_VERSION=0x1052 # Vista
248 -DDLL_EXPORT_VERSION=${DLL_EXPORT_VERSION})
249
250 # Arch Options
251 if(ARCH STREQUAL "i386")
252 # clang-cl defines this one for itself
253 if(NOT (MSVC AND CMAKE_C_COMPILER_ID STREQUAL "Clang"))
254 add_definitions(-D_M_IX86)
255 endif()
256 add_definitions(-D_X86_ -D__i386__ -Di386)
257 if(SARCH STREQUAL "xbox")
258 add_definitions(-DSARCH_XBOX)
259 elseif(SARCH STREQUAL "pc98")
260 add_definitions(-DSARCH_PC98)
261 endif()
262 elseif(ARCH STREQUAL "amd64")
263 # clang-cl defines this one for itself
264 if (NOT (MSVC AND CMAKE_C_COMPILER_ID STREQUAL "Clang"))
265 add_compile_definitions(_M_AMD64 _M_X64)
266 endif()
267 add_definitions(-D_AMD64_ -D__x86_64__ -D_WIN64)
268 elseif(ARCH STREQUAL "arm")
269 # _M_ARM is already defined by toolchain
270 add_definitions(-D_ARM_ -D__arm__ -DWIN32)
271 if(SARCH STREQUAL "omap3-zoom2")
272 add_definitions(-D_ZOOM2_)
273 endif()
274 elseif(ARCH STREQUAL "arm64")
275 # GNU tools refer to arm64 as aarch64
276 add_definitions(-D_ARM64_ -D__arm64__ -D__aarch64__ -D_WIN64)
277 endif()
278
279 # Other
280 add_definitions(-D_NEW_DELETE_OPERATORS_)
281 if(ARCH STREQUAL "i386")
282 add_definitions(-DUSE_COMPILER_EXCEPTIONS -D_USE_32BIT_TIME_T)
283 elseif(ARCH STREQUAL "amd64")
284 add_compile_definitions(USE_COMPILER_EXCEPTIONS)
285 elseif(ARCH STREQUAL "arm")
286 add_compile_definitions(USE_COMPILER_EXCEPTIONS)
287 elseif(ARCH STREQUAL "arm64")
288 add_compile_definitions(USE_COMPILER_EXCEPTIONS)
289 endif()
290
291 # Activate support for assembly source files
292 if (MSVC)
293 enable_language(ASM_MASM)
294 else()
295 enable_language(ASM)
296 endif()
297
298 # Activate language support for resource files
299 enable_language(RC)
300
301 # Localization definitions
302 include(sdk/cmake/localization.cmake)
303 set(I18N_DEFS "")
304 # This will set I18N_DEFS for later use
305 set_i18n_language(${I18N_LANG})
306
307 # Compiler specific definitions and macros
308 if(MSVC)
309 include(sdk/cmake/msvc.cmake)
310 else()
311 include(sdk/cmake/gcc.cmake)
312 endif()
313
314 # Generic macros
315 include(sdk/cmake/CMakeMacros.cmake)
316
317 # IDL macros for widl/midl
318 # We're using widl now for both MSVC and GCC builds
319 include(sdk/cmake/widl-support.cmake)
320
321 include_directories(
322 sdk/include
323 sdk/include/crt
324 sdk/include/ddk
325 sdk/include/ndk
326 sdk/include/psdk
327 sdk/include/reactos
328 sdk/include/reactos/libs
329 sdk/include/vcruntime
330 sdk/include/winrt
331 ${REACTOS_BINARY_DIR}/sdk/include
332 ${REACTOS_BINARY_DIR}/sdk/include/psdk
333 ${REACTOS_BINARY_DIR}/sdk/include/ddk
334 ${REACTOS_BINARY_DIR}/sdk/include/dxsdk
335 ${REACTOS_BINARY_DIR}/sdk/include/reactos
336 ${REACTOS_BINARY_DIR}/sdk/include/reactos/mc
337 sdk/include/dxsdk
338 sdk/lib/pseh/include
339 )
340
341 if(ARCH STREQUAL "arm")
342 include_directories(${REACTOS_SOURCE_DIR}/sdk/include/reactos/arm)
343 endif()
344
345 add_dependency_header()
346
347 add_subdirectory(sdk/include)
348
349 if(ARCH MATCHES "64$")
350 include(sdk/cmake/baseaddress64.cmake)
351 elseif(NO_ROSSYM)
352 include(sdk/cmake/baseaddress_dwarf.cmake)
353 elseif(MSVC)
354 include(sdk/cmake/baseaddress_msvc.cmake)
355 else()
356 include(sdk/cmake/baseaddress.cmake)
357 endif()
358
359 # For MSVC builds, this puts all debug symbols file in the same directory.
360 if(MSVC)
361 set(CMAKE_PDB_OUTPUT_DIRECTORY "${REACTOS_BINARY_DIR}/msvc_pdb")
362 elseif(SEPARATE_DBG)
363 set(CMAKE_PDB_OUTPUT_DIRECTORY "${REACTOS_BINARY_DIR}/symbols")
364 endif()
365
366 #begin with boot so reactos_cab target is defined before all other modules
367 add_subdirectory(boot)
368 add_subdirectory(base)
369 add_subdirectory(dll)
370 add_subdirectory(drivers)
371 add_subdirectory(hal)
372 add_subdirectory(sdk/lib)
373 add_subdirectory(media)
374 add_subdirectory(modules)
375 add_subdirectory(ntoskrnl)
376 add_subdirectory(subsystems)
377 add_subdirectory(sdk/tools/wpp)
378 add_subdirectory(win32ss)
379
380 # Create the registry hives
381 create_registry_hives()
382
383 # Create {bootcd, livecd, bootcdregtest}.lst
384 create_iso_lists()
385
386 file(MAKE_DIRECTORY ${REACTOS_BINARY_DIR}/sdk/include/reactos)
387
388 add_dependency_footer()
389endif()