xref: /reactos/configure.cmd (revision a9d9f0de)
1@echo off
2
3REM This is needed so as to avoid static expansion of environment variables
4REM inside if (...) conditionals.
5REM See http://stackoverflow.com/questions/305605/weird-scope-issue-in-bat-file
6REM for more explanation.
7REM Precisely needed for configuring Visual Studio Environment.
8setlocal enabledelayedexpansion
9
10REM Does the user need help?
11if /I "%1" == "help" goto help
12if /I "%1" == "/?" (
13:help
14    echo Help for configure script
15    echo Syntax: path\to\source\configure.cmd [script-options] [Cmake-options]
16    echo Available script-options: Codeblocks, Eclipse, Makefiles, clang, VSSolution
17    echo Cmake-options: -DVARIABLE:TYPE=VALUE
18    goto quit
19)
20
21REM Get the source root directory
22set REACTOS_SOURCE_DIR=%~dp0
23
24REM Ensure there's no spaces in the source path
25echo %REACTOS_SOURCE_DIR%| find " " > NUL
26if %ERRORLEVEL% == 0 (
27    echo. && echo   Your source path contains at least one space.
28    echo   This will cause problems with building.
29    echo   Please rename your folders so there are no spaces in the source path,
30    echo   or move your source to a different folder.
31    goto quit
32)
33
34REM Set default generator
35set CMAKE_GENERATOR="Ninja"
36set CMAKE_ARCH=
37
38REM Detect presence of cmake
39cmd /c cmake --version 2>&1 | find "cmake version" > NUL || goto cmake_notfound
40
41REM Detect build environment (MinGW, VS, WDK, ...)
42if defined ROS_ARCH (
43    echo Detected RosBE for %ROS_ARCH%
44    set BUILD_ENVIRONMENT=MinGW
45    set ARCH=%ROS_ARCH%
46    set MINGW_TOOCHAIN_FILE=toolchain-gcc.cmake
47
48) else if defined VCINSTALLDIR (
49    REM VS command prompt does not put this in environment vars
50    cl 2>&1 | find "x86" > NUL && set ARCH=i386
51    cl 2>&1 | find "x64" > NUL && set ARCH=amd64
52    cl 2>&1 | find "ARM" > NUL && set ARCH=arm
53    cl 2>&1 | find "ARM64" > NUL && set ARCH=arm64
54    cl 2>&1 | find "19.00." > NUL && set VS_VERSION=14
55    cl 2>&1 | findstr /R /c:"19\.1.\." > NUL && set VS_VERSION=15
56    cl 2>&1 | findstr /R /c:"19\.2.\." > NUL && set VS_VERSION=16
57    cl 2>&1 | findstr /R /c:"19\.3.\." > NUL && set VS_VERSION=17
58    cl 2>&1 | findstr /R /c:"19\.4.\." > NUL && set VS_VERSION=17
59    if not defined VS_VERSION (
60        echo Error: Visual Studio version too old ^(before 14 ^(2015^)^) or version detection failed.
61        goto quit
62    )
63    set BUILD_ENVIRONMENT=VS
64    set VS_SOLUTION=0
65    echo Detected Visual Studio Environment !BUILD_ENVIRONMENT!!VS_VERSION!-!ARCH!
66) else (
67    echo Error: Unable to detect build environment. Configure script failure.
68    goto quit
69)
70
71REM Checkpoint
72if not defined ARCH (
73    echo Unknown build architecture
74    goto quit
75)
76
77set USE_CLANG_CL=0
78
79REM Parse command line parameters
80set CMAKE_PARAMS=
81set REMAINING=%*
82:repeat
83
84    REM Extract a parameter without removing '='
85    for /f "tokens=1*" %%a in ("%REMAINING%") do (
86        set "PARAM=%%a"
87        set REMAINING=%%b
88    )
89
90    if "%BUILD_ENVIRONMENT%" == "MinGW" (
91        if /I "!PARAM!" == "Codeblocks" (
92            set CMAKE_GENERATOR="CodeBlocks - MinGW Makefiles"
93        ) else if /I "!PARAM!" == "Eclipse" (
94            set CMAKE_GENERATOR="Eclipse CDT4 - MinGW Makefiles"
95        ) else if /I "!PARAM!" == "Makefiles" (
96            set CMAKE_GENERATOR="MinGW Makefiles"
97        ) else if /I "!PARAM!" == "Ninja" (
98            set CMAKE_GENERATOR="Ninja"
99        ) else if /I "!PARAM!" == "VSSolution" (
100            echo. && echo Error: Creation of VS Solution files is not supported in a MinGW environment.
101            echo Please run this command in a [Developer] Command Prompt for Visual Studio.
102            goto quit
103        ) else if /I "!PARAM:~0,2!" == "-D" (
104            REM User is passing a switch to CMake
105            set "CMAKE_PARAMS=%CMAKE_PARAMS% !PARAM!"
106        ) else (
107            echo. && echo   Warning: Unrecognized switch "!PARAM!" && echo.
108        )
109    ) else (
110        if /I "!PARAM!" == "CodeBlocks" (
111            set CMAKE_GENERATOR="CodeBlocks - NMake Makefiles"
112        ) else if /I "!PARAM!" == "Eclipse" (
113            set CMAKE_GENERATOR="Eclipse CDT4 - NMake Makefiles"
114        ) else if /I "!PARAM!" == "Makefiles" (
115            set CMAKE_GENERATOR="NMake Makefiles"
116        ) else if /I "!PARAM!" == "Ninja" (
117            set CMAKE_GENERATOR="Ninja"
118        ) else if /I "!PARAM!" == "clang" (
119            set USE_CLANG_CL=1
120        ) else if /I "!PARAM!" == "VSSolution" (
121            set VS_SOLUTION=1
122            REM explicitly set VS version for project generator
123            for /f "tokens=1*" %%a in ("%REMAINING%") do (
124                set PARAM=%%a
125                set REMAINING2=%%b
126            )
127            if /I "!PARAM!" == "-VS_VER" (
128                for /f "tokens=1*" %%a in ("!REMAINING2!") do (
129                    set VS_VERSION=%%a
130                    set REMAINING=%%b
131                )
132                echo Visual Studio Environment set to !BUILD_ENVIRONMENT!!VS_VERSION!-!ARCH!
133            )
134            set CMAKE_GENERATOR="Visual Studio !VS_VERSION!"
135            if "!ARCH!" == "i386" (
136                set CMAKE_ARCH=-A Win32
137            ) else if "!ARCH!" == "amd64" (
138                set CMAKE_ARCH=-A x64
139            ) else if "!ARCH!" == "arm" (
140                set CMAKE_ARCH=-A ARM
141            ) else if "!ARCH!" == "arm64" (
142                set CMAKE_ARCH=-A ARM64
143            )
144        ) else if /I "!PARAM:~0,2!" == "-D" (
145            REM User is passing a switch to CMake
146            set "CMAKE_PARAMS=%CMAKE_PARAMS% !PARAM!"
147        ) else (
148            echo. && echo   Warning: Unrecognized switch "!PARAM!" && echo.
149        )
150    )
151
152    REM Go to next parameter
153    if defined REMAINING goto repeat
154
155REM Inform the user about the default build
156if "!CMAKE_GENERATOR!" == "Ninja" (
157    echo This script defaults to Ninja. Type "configure help" for alternative options.
158)
159
160REM Create directories
161set REACTOS_OUTPUT_PATH=output-%BUILD_ENVIRONMENT%-%ARCH%
162
163if "%VS_SOLUTION%" == "1" (
164    set REACTOS_OUTPUT_PATH=%REACTOS_OUTPUT_PATH%-sln
165)
166
167if "%REACTOS_SOURCE_DIR%" == "%CD%\" (
168    set CD_SAME_AS_SOURCE=1
169    echo Creating directories in %REACTOS_OUTPUT_PATH%
170
171    if not exist %REACTOS_OUTPUT_PATH% (
172        mkdir %REACTOS_OUTPUT_PATH%
173    )
174    cd %REACTOS_OUTPUT_PATH%
175)
176
177if "%VS_SOLUTION%" == "1" (
178
179    if exist build.ninja (
180        echo. && echo Error: This directory has already been configured for ninja.
181        echo An output folder configured for ninja can't be reconfigured for VSSolution.
182        echo Use an empty folder or delete the contents of this folder, then try again.
183        goto quit
184    )
185) else if exist REACTOS.sln (
186    echo. && echo Error: This directory has already been configured for Visual Studio.
187    echo An output folder configured for VSSolution can't be reconfigured for ninja.
188    echo Use an empty folder or delete the contents of this folder, then try again. && echo.
189    goto quit
190)
191
192echo Preparing reactos...
193
194if EXIST CMakeCache.txt (
195    del CMakeCache.txt /q
196)
197
198
199if "%BUILD_ENVIRONMENT%" == "MinGW" (
200    cmake -G %CMAKE_GENERATOR% -DENABLE_CCACHE:BOOL=0 -DCMAKE_TOOLCHAIN_FILE:FILEPATH=%MINGW_TOOCHAIN_FILE% -DARCH:STRING=%ARCH% %BUILD_TOOLS_FLAG% %CMAKE_PARAMS% "%REACTOS_SOURCE_DIR%"
201) else if %USE_CLANG_CL% == 1 (
202    cmake -G %CMAKE_GENERATOR% -DCMAKE_TOOLCHAIN_FILE:FILEPATH=toolchain-msvc.cmake -DARCH:STRING=%ARCH% %BUILD_TOOLS_FLAG% -DUSE_CLANG_CL:BOOL=1 %CMAKE_PARAMS% "%REACTOS_SOURCE_DIR%"
203) else (
204    cmake -G %CMAKE_GENERATOR% %CMAKE_ARCH% -DCMAKE_TOOLCHAIN_FILE:FILEPATH=toolchain-msvc.cmake -DARCH:STRING=%ARCH% %BUILD_TOOLS_FLAG% %CMAKE_PARAMS% "%REACTOS_SOURCE_DIR%"
205)
206
207if %ERRORLEVEL% NEQ 0 (
208    goto quit
209)
210
211if "%CD_SAME_AS_SOURCE%" == "1" (
212    set ENDV= from %REACTOS_OUTPUT_PATH%
213)
214
215if "%VS_SOLUTION%" == "1" (
216    set ENDV= You can now use msbuild or open REACTOS.sln%ENDV%.
217) else (
218    set ENDV= Execute appropriate build commands ^(ex: ninja, make, nmake, etc...^)%ENDV%
219)
220
221echo. && echo Configure script complete^^!%ENDV%
222
223goto quit
224
225:cmake_notfound
226echo Unable to find cmake, if it is installed, check your PATH variable.
227
228:quit
229endlocal
230exit /b
231