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 71add_definitions( 72 -D__REACTOS__ 73 # swprintf without count argument is used in most of the codebase 74 -D_CRT_NON_CONFORMING_SWPRINTFS 75) 76 77# There doesn't seem to be a standard for __FILE__ being relative or absolute, so detect it at runtime. 78file(RELATIVE_PATH _PATH_PREFIX ${REACTOS_BINARY_DIR} ${REACTOS_SOURCE_DIR}) 79if (NOT MSVC AND (CMAKE_C_COMPILER_ID STREQUAL "GNU" OR CMAKE_C_COMPILER_ID STREQUAL "Clang")) 80 # Thankfully, GCC has this 81 add_compile_options(-ffile-prefix-map=${REACTOS_SOURCE_DIR}=) 82 add_compile_options(-ffile-prefix-map=${_PATH_PREFIX}=) 83else() 84 string(LENGTH ${_PATH_PREFIX} _PATH_PREFIX_LENGTH) 85 string(LENGTH ${REACTOS_SOURCE_DIR} REACTOS_SOURCE_DIR_LENGTH) 86 math(EXPR REACTOS_SOURCE_DIR_LENGTH "${REACTOS_SOURCE_DIR_LENGTH} + 1") 87 add_compile_definitions("$<$<COMPILE_LANGUAGE:C,CXX>:__RELFILE__=&__FILE__[__FILE__[0] == '.' ? ${_PATH_PREFIX_LENGTH} : ${REACTOS_SOURCE_DIR_LENGTH}]>") 88endif() 89 90if(MSVC_IDE) 91 add_compile_options("/MP") 92endif() 93 94# Bison and Flex support 95find_package(BISON REQUIRED) 96find_package(FLEX REQUIRED) 97 98if(MSVC_IDE) 99 # Bison needs M4 and BISON_PKGDATADIR set at build time, 100 # but visual studio is hardly ever opened from the configure-time environment. 101 # Since cmake does not support setting env variables for a custom command, 102 # we have to write a wrapper that sets the variables and then executes bison. 103 # Idea taken from https://stackoverflow.com/a/35032051/4928207 104 if(DEFINED ENV{M4}) 105 # Store this environment variable for configure re-runs from withing visual studio. 106 SET(ROS_SAVED_M4 "$ENV{M4}" CACHE INTERNAL "") 107 endif() 108 if(DEFINED ENV{BISON_PKGDATADIR}) 109 SET(ROS_SAVED_BISON_PKGDATADIR "$ENV{BISON_PKGDATADIR}" CACHE INTERNAL "") 110 endif() 111 112 # Tell the user about a misconfigured environment 113 if("x${ROS_SAVED_M4}x" STREQUAL "xx" OR "x${ROS_SAVED_BISON_PKGDATADIR}x" STREQUAL "xx") 114 message(FATAL_ERROR "\nM4 or BISON_PKGDATADIR environment variables not set, cannot continue!\n" 115 "See https://reactos.org/wiki/Visual_Studio for more information!") 116 endif() 117 118 file(WRITE "${CMAKE_BINARY_DIR}/bison_wrapper.cmd" 119 "@ECHO OFF\n" 120 "set M4=${ROS_SAVED_M4}\n" 121 "set BISON_PKGDATADIR=${ROS_SAVED_BISON_PKGDATADIR}\n" 122 "${BISON_EXECUTABLE} %*\n") 123 set(BISON_EXECUTABLE "${CMAKE_BINARY_DIR}/bison_wrapper.cmd") 124 # And the same hacks for FLEX 125 file(WRITE "${CMAKE_BINARY_DIR}/flex_wrapper.cmd" 126 "@ECHO OFF\n" 127 "set M4=${ROS_SAVED_M4}\n" 128 "set BISON_PKGDATADIR=${ROS_SAVED_BISON_PKGDATADIR}\n" 129 "${FLEX_EXECUTABLE} %*\n") 130 set(FLEX_EXECUTABLE "${CMAKE_BINARY_DIR}/flex_wrapper.cmd") 131endif() 132 133if(NOT CMAKE_CROSSCOMPILING) 134 set(TOOLS_FOLDER ${CMAKE_CURRENT_BINARY_DIR}) 135 add_definitions(-DTARGET_${ARCH}) 136 137 if(MSVC) 138 if(ARCH STREQUAL "i386") 139 add_definitions(/D_X86_ /D__i386__ /DWIN32 /D_WINDOWS) 140 elseif(ARCH STREQUAL "amd64") 141 add_definitions(-D_AMD64_ -D__x86_64__ /DWIN32 -D_WINDOWS) 142 endif() 143 if(MSVC_VERSION GREATER 1699) 144 add_definitions(/D_ALLOW_KEYWORD_MACROS) 145 endif() 146 endif() 147 add_subdirectory(sdk/include/host) 148 149 if(NOT MSVC) 150 add_subdirectory(dll/win32/dbghelp) 151 endif() 152 add_subdirectory(sdk/tools) 153 add_subdirectory(sdk/lib) 154 155 set(NATIVE_TARGETS bin2c widl gendib cabman fatten hpp isohybrid mkhive mkisofs obj2bin spec2def geninc mkshelllink utf16le xml2sdb) 156 if(NOT MSVC) 157 list(APPEND NATIVE_TARGETS rsym pefixup) 158 endif() 159 160 install(TARGETS ${NATIVE_TARGETS}) 161else() 162 # Add host tools target 163 include(sdk/cmake/host-tools.cmake) 164 setup_host_tools() 165 166 # We don't need CMake importlib handling. 167 unset(CMAKE_IMPORT_LIBRARY_SUFFIX) 168 169 # Print build type(s) 170 if(CMAKE_CONFIGURATION_TYPES) 171 # Multi-config generators, like Visual Studio (MSBuild). 172 message("-- Configuration types: ${CMAKE_CONFIGURATION_TYPES}") 173 else() 174 # Single-configuration generators, like Ninja. 175 message("-- Build type: ${CMAKE_BUILD_TYPE}") 176 endif() 177 178 # Always add /MT in VS CMAKE_GENERATOR and define _SBCS otherwise VS thinks it's a multi-byte or whatever project 179 if (MSVC_IDE) 180 add_compile_options("/MT") 181 add_compile_definitions(_SBCS) 182 endif() 183 184 185 # adjust the default behaviour of the FIND_XXX() commands: 186 # search headers and libraries in the target environment, search 187 # programs in the host environment 188 set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) 189 set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY NEVER) 190 set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE NEVER) 191 192 # Add our own target properties 193 # General module definitions 194 define_property(TARGET PROPERTY REACTOS_MODULE_TYPE 195 BRIEF_DOCS "The type of this module" 196 FULL_DOCS [[ 197The type of this module. 198One of "nativecui", "nativedll", "kernelmodedriver", "wdmdriver", "kerneldll", "win32cui", "win32gui", "win32dll", "win32ocx", "cpl" or "module"]]) 199 200 # C++ 201 define_property(TARGET PROPERTY WITH_CXX_EXCEPTIONS 202 BRIEF_DOCS "Enable C++ exceptions on this target" 203 FULL_DOCS [[ 204Enables C++ exception handling. 205Enable 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).]]) 206 define_property(TARGET PROPERTY WITH_CXX_RTTI 207 BRIEF_DOCS "Enable C++ RTTI on this target" 208 FULL_DOCS [[ 209Enables run-time type information. 210Enable 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.]]) 211 212 213 if(DBG) 214 add_definitions(-DDBG=1 -D_SEH_ENABLE_TRACE) 215 else() 216 add_definitions(-DDBG=0) 217 endif() 218 219 if(KDBG) 220 add_definitions(-DKDBG) 221 endif() 222 223 if(_WINKD_) 224 add_definitions(-D_WINKD_) 225 endif() 226 227 if(ENABLE_CCACHE) 228 message(WARNING "-- Disabling precompiled headers support (ccache).") 229 option(PCH "Whether to use precompiled headers" OFF) 230 set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache) 231 elseif(CMAKE_C_COMPILER_ID STREQUAL "GNU") 232 message(WARNING "-- Disabling precompiled headers on GCC by default CORE-17108.") 233 option(PCH "Whether to use precompiled headers" OFF) 234 else() 235 option(PCH "Whether to use precompiled headers" ON) 236 endif() 237 238 # Version Options 239 add_definitions(-DWINVER=0x502 240 -D_WIN32_IE=0x600 241 -D_WIN32_WINNT=0x502 242 -D_WIN32_WINDOWS=0x502 243 -D_SETUPAPI_VER=0x502 244 -DMINGW_HAS_SECURE_API=1) 245 246 # Arch Options 247 if(ARCH STREQUAL "i386") 248 # clang-cl defines this one for itself 249 if(NOT (MSVC AND CMAKE_C_COMPILER_ID STREQUAL "Clang")) 250 add_definitions(-D_M_IX86) 251 endif() 252 add_definitions(-D_X86_ -D__i386__ -Di386) 253 if(SARCH STREQUAL "xbox") 254 add_definitions(-DSARCH_XBOX) 255 elseif(SARCH STREQUAL "pc98") 256 add_definitions(-DSARCH_PC98) 257 endif() 258 elseif(ARCH STREQUAL "amd64") 259 # clang-cl defines this one for itself 260 if (NOT (MSVC AND CMAKE_C_COMPILER_ID STREQUAL "Clang")) 261 add_compile_definitions(_M_AMD64) 262 endif() 263 add_definitions(-D_AMD64_ -D__x86_64__ -D_WIN64) 264 elseif(ARCH STREQUAL "arm") 265 # _M_ARM is already defined by toolchain 266 add_definitions(-D_ARM_ -D__arm__ -DWIN32) 267 if(SARCH STREQUAL "omap3-zoom2") 268 add_definitions(-D_ZOOM2_) 269 endif() 270 elseif(ARCH STREQUAL "arm64") 271 # GNU tools refer to arm64 as aarch64 272 add_definitions(-D_ARM64_ -D__arm64__ -D__aarch64__ -D_WIN64) 273 endif() 274 275 # Other 276 add_definitions(-D_NEW_DELETE_OPERATORS_) 277 if(ARCH STREQUAL "i386") 278 add_definitions(-DUSE_COMPILER_EXCEPTIONS -D_USE_32BIT_TIME_T) 279 elseif(ARCH STREQUAL "amd64") 280 add_compile_definitions(USE_COMPILER_EXCEPTIONS) 281 elseif(ARCH STREQUAL "arm") 282 add_compile_definitions(USE_COMPILER_EXCEPTIONS) 283 elseif(ARCH STREQUAL "arm64") 284 add_compile_definitions(USE_COMPILER_EXCEPTIONS) 285 endif() 286 287 # Activate support for assembly source files 288 if (MSVC) 289 enable_language(ASM_MASM) 290 else() 291 enable_language(ASM) 292 endif() 293 294 # Activate language support for resource files 295 enable_language(RC) 296 297 # Localization definitions 298 include(sdk/cmake/localization.cmake) 299 set(I18N_DEFS "") 300 # This will set I18N_DEFS for later use 301 set_i18n_language(${I18N_LANG}) 302 303 # Compiler specific definitions and macros 304 if(MSVC) 305 include(sdk/cmake/msvc.cmake) 306 else() 307 include(sdk/cmake/gcc.cmake) 308 endif() 309 310 # Generic macros 311 include(sdk/cmake/CMakeMacros.cmake) 312 313 # IDL macros for widl/midl 314 # We're using widl now for both MSVC and GCC builds 315 include(sdk/cmake/widl-support.cmake) 316 317 include_directories( 318 sdk/include 319 sdk/include/psdk 320 sdk/include/dxsdk 321 ${REACTOS_BINARY_DIR}/sdk/include 322 ${REACTOS_BINARY_DIR}/sdk/include/psdk 323 ${REACTOS_BINARY_DIR}/sdk/include/dxsdk 324 ${REACTOS_BINARY_DIR}/sdk/include/ddk 325 ${REACTOS_BINARY_DIR}/sdk/include/reactos 326 ${REACTOS_BINARY_DIR}/sdk/include/reactos/mc 327 sdk/include/crt 328 sdk/include/ddk 329 sdk/include/ndk 330 sdk/include/reactos 331 sdk/include/reactos/libs) 332 333 if(ARCH STREQUAL "arm") 334 include_directories(${REACTOS_SOURCE_DIR}/sdk/include/reactos/arm) 335 endif() 336 337 add_dependency_header() 338 339 add_subdirectory(sdk/include/ndk/tests) 340 add_subdirectory(sdk/include/xdk) 341 add_subdirectory(sdk/include/psdk) 342 add_subdirectory(sdk/include/dxsdk) 343 add_subdirectory(sdk/include/reactos/wine) 344 add_subdirectory(sdk/include/reactos/mc) 345 add_subdirectory(sdk/include/asm) 346 347 if(NO_ROSSYM) 348 include(sdk/cmake/baseaddress_dwarf.cmake) 349 elseif(MSVC) 350 if (ARCH STREQUAL "amd64") 351 include(sdk/cmake/baseaddress_msvc_x64.cmake) 352 else() 353 include(sdk/cmake/baseaddress_msvc.cmake) 354 endif() 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() 390