1@echo off
2
3REM Default build is 64-bit
4REM 32-bit compilation is available using "32" as a second parameter when you run msvc_build.bat
5REM How to use:
6REM BUILD 64-bit:
7REM    "msvc_build.bat debug" builds 64-bit Debug version of MuseScore without optimizations
8REM    "msvc_build.bat relwithdebinfo" builds optimized 64-bit version of MuseScore with almost all debug symbols
9REM    "msvc_build.bat release" builds fully optimized 64-bit version of MuseScore without command line output
10REM
11REM BUILD 32-bit:
12REM    "msvc_build.bat debug 32" builds 32-bit Debug version of MuseScore
13REM    "msvc_build.bat relwithdebinfo 32" builds 32-bit RelWithDebInfo version of MuseScore
14REM    "msvc_build.bat release 32" builds 32-bit Release version of MuseScore
15REM
16REM INSTALL 64-bit:
17REM    "msvc_build.bat install" put all required files of 64-bit Release build to install folder (msvc.install_x64)
18REM    "msvc_build.bat installdebug" put all required files of 64-bit Debug build to install folder (msvc.install_x64)
19REM    "msvc_build.bat installrelwithdebinfo" put all required files of 64-bit RelWithDebInfo build to install folder (msvc.install_x64)
20REM
21REM INSTALL 32-bit:
22REM    "msvc_build.bat install 32" put all required files of 32-bit Release build to install folder (msvc.install_x86)
23REM    "msvc_build.bat installdebug 32" put all required files of 32-bit Debug build to install folder (msvc.install_x86)
24REM    "msvc_build.bat installrelwithdebinfo 32" put all required files of 32-bit RelWithDebInfo build to install folder (msvc.install_x86)
25REM
26REM PACKAGE:
27REM    "msvc_build.bat package" pack the installer for already built and installed 64-bit Release build (msvc.build_x64/MuseScore-*.msi)
28REM    "msvc_build.bat package 32" pack the installer for already built and installed 32-bit Release build (msvc.build_x86/MuseScore-*.msi)
29REM
30REM CLEAN:
31REM    "msvc_build.bat clean" remove all files in msvc.* folders and the folders itself
32REM
33REM Windows Portable build is triggered by defining BUILD_WIN_PORTABLE environment variable to "ON" before launching this script, e.g.
34REM SET BUILD_WIN_PORTABLE=ON
35
36SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
37
38IF "%GENERATOR_NAME%"=="" (
39   CALL :FIND_GENERATOR
40)
41
42IF "%GENERATOR_NAME%"=="" (
43   ECHO "No supported version of Microsoft Visual Studio (2017 or 2019) found."
44   GOTO :END
45)
46
47REM BUILD_64 and BUILD_FOR_WINSTORE are used in CMakeLists.txt
48SET BUILD_FOR_WINSTORE=OFF
49SET "BUILD_FOLDER=msvc.build"
50SET "INSTALL_FOLDER=msvc.install"
51
52IF "%2"=="32" (
53    SET PLATFORM_NAME=Win32
54    SET "ARCH=x86"
55    SET BUILD_64=OFF
56) ELSE (
57    IF NOT "%2"=="" (
58        IF NOT "%2"=="64" (
59            echo Invalid second argument
60            GOTO :END
61        ) ELSE (
62            SET PLATFORM_NAME=x64
63            SET "ARCH=x64"
64            SET BUILD_64=ON
65        )
66    ) ELSE (
67        SET PLATFORM_NAME=x64
68        SET "ARCH=x64"
69        SET BUILD_64=ON
70    )
71)
72
73IF NOT "%3"=="" (
74   SET BUILD_NUMBER="%3"
75   SET BUILD_AUTOUPDATE="ON"
76)
77
78ECHO "BUILD_WIN_PORTABLE: %BUILD_WIN_PORTABLE%"
79IF "%BUILD_WIN_PORTABLE%"=="ON" (
80    SET "INSTALL_FOLDER=MuseScorePortable\App\MuseScore"
81    SET "BUILD_AUTOUPDATE=OFF"
82    SET "WIN_PORTABLE_OPT=-DBUILD_PORTABLEAPPS=ON"
83)
84
85ECHO "INSTALL_FOLDER: %INSTALL_FOLDER%"
86ECHO "WIN_PORTABLE_OPT: %WIN_PORTABLE_OPT%"
87
88IF /I "%1"=="release" (
89   SET CONFIGURATION_STR="release"
90   GOTO :BUILD
91)
92
93IF /I "%1"=="debug" (
94   SET CONFIGURATION_STR="debug"
95   GOTO :BUILD
96)
97
98IF /I "%1"=="relwithdebinfo" (
99   SET CONFIGURATION_STR="relwithdebinfo"
100   GOTO :BUILD
101)
102
103IF /I "%1"=="install" (
104   SET "BUILD_FOLDER=%BUILD_FOLDER%_%ARCH%"
105   SET CONFIGURATION_STR="release"
106   GOTO :INSTALL
107)
108
109IF /I "%1"=="installdebug" (
110   SET "BUILD_FOLDER=%BUILD_FOLDER%_%ARCH%"
111   SET CONFIGURATION_STR="debug"
112   GOTO :INSTALL
113)
114
115IF /I "%1"=="installrelwithdebinfo" (
116   SET "BUILD_FOLDER=%BUILD_FOLDER%_%ARCH%"
117   SET CONFIGURATION_STR="relwithdebinfo"
118   GOTO :INSTALL
119)
120
121IF /I "%1"=="package" (
122   cd "%BUILD_FOLDER%_%ARCH%"
123   cmake --build . --config RelWithDebInfo --target package
124   GOTO :END
125)
126
127IF /I "%1"=="revision" (
128   echo revisionStep
129   git rev-parse --short=7 HEAD > local_build_revision.env
130   GOTO :END
131)
132
133IF /I "%1"=="clean" (
134   for /d %%G in ("msvc.*") do rd /s /q "%%~G"
135   for /d %%G in ("MuseScorePortable") do rd /s /q "%%~G"
136   GOTO :END
137) ELSE (
138   echo No valid parameters are set
139   GOTO :END
140)
141
142:FIND_GENERATOR
143
144   REM Usage: CALL :FIND_GENERATOR
145   REM Detects the highest supported VS version installed and sets GENERATOR_NAME to the appropriate CMake generator name.
146
147   REM vswhere.exe is a helper utility that is automatically installed with VS2017 and later (and always at a fixed location).
148   SET VSWHERE="%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe"
149   IF NOT EXIST %VSWHERE% EXIT /B !ERRORLEVEL!
150
151   REM Try Visual Studio 2019 first.
152   CALL :FIND_GENERATOR_VERSION %VSWHERE% 16 2019
153
154   REM Fall back to Visual Studio 2017.
155   IF "%GENERATOR_NAME%"=="" CALL :FIND_GENERATOR_VERSION %VSWHERE% 15 2017
156
157   EXIT /B !ERRORLEVEL!
158
159:FIND_GENERATOR_VERSION
160
161   REM Usage: CALL :FIND_GENERATOR_VERSION "[path\]vswhere.exe" major_version_number year
162   REM Checks if the specified VS version is installed, and if so, sets GENERATOR_NAME to the appropriate CMake generator name.
163
164   FOR /F "usebackq delims=. tokens=1" %%I IN (`%1 -version [%2^,^) -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -property installationVersion -format value`) DO (
165      IF "%%I"=="%2" SET GENERATOR_NAME=Visual Studio %2 %3
166   )
167
168   EXIT /B !ERRORLEVEL!
169
170:BUILD
171   echo Generator is: %GENERATOR_NAME%
172   echo Platform is: %PLATFORM_NAME%
173   SET "BUILD_FOLDER=%BUILD_FOLDER%_%ARCH%"
174   echo Build folder is: %BUILD_FOLDER%
175   IF NOT "%BUILD_WIN_PORTABLE%"=="ON" (
176      SET "INSTALL_FOLDER=%INSTALL_FOLDER%_%ARCH%"
177   )
178   echo Install folder is: %INSTALL_FOLDER%
179   if not exist "%BUILD_FOLDER%\" mkdir "%BUILD_FOLDER%"
180   if not exist "%INSTALL_FOLDER%\" mkdir "%INSTALL_FOLDER%"
181
182IF NOT "%MSCORE_STABLE_BUILD%" == "" (
183    IF NOT "%CRASH_LOG_SERVER_URL%" == "" (
184        IF "%BUILD_FOR_WINSTORE%" == "OFF" (
185            SET CRASH_REPORT_URL_OPT=-DCRASH_REPORT_URL=%CRASH_LOG_SERVER_URL% -DBUILD_CRASH_REPORTER=ON
186        )
187    )
188
189    IF NOT "%TELEMETRY_TRACK_ID%" == "" (
190        SET TELEMETRY_TRACK_ID_OPT=-DTELEMETRY_TRACK_ID=%TELEMETRY_TRACK_ID%
191    )
192)
193
194IF "%MUSESCORE_BUILD_CONFIG%" == "" (
195    SET MUSESCORE_BUILD_CONFIG="dev"
196)
197
198SET "INSTALL_FOLDER=%INSTALL_FOLDER:\=/%"
199REM -DCMAKE_BUILD_NUMBER=%BUILD_NUMBER% -DCMAKE_BUILD_AUTOUPDATE=%BUILD_AUTOUPDATE% %CRASH_REPORT_URL_OPT% are used for CI only
200   cd "%BUILD_FOLDER%"
201   IF EXIST "CMakeCache.txt" (
202      echo Using existing CMake configuration to save time.
203      echo To force reconfiguration, delete CMakeCache.txt or run "msvc_build.bat clean".
204      echo You only need to do this if you want to use different build options to before.
205      REM Note: If a CMakeLists.txt file was edited then the build system will detect it
206      REM and run CMake again automatically with the same options as before.
207   ) ELSE (
208      echo Building CMake configuration...
209      cmake -G "%GENERATOR_NAME%" -A "%PLATFORM_NAME%" -DCMAKE_INSTALL_PREFIX=../%INSTALL_FOLDER% -DCMAKE_BUILD_TYPE=%CONFIGURATION_STR% -DMUSESCORE_BUILD_CONFIG=%MUSESCORE_BUILD_CONFIG% -DMUSESCORE_REVISION=%MUSESCORE_REVISION% -DBUILD_FOR_WINSTORE=%BUILD_FOR_WINSTORE% -DBUILD_64=%BUILD_64% -DCMAKE_BUILD_NUMBER=%BUILD_NUMBER% -DBUILD_AUTOUPDATE=%BUILD_AUTOUPDATE% %CRASH_REPORT_URL_OPT% %TELEMETRY_TRACK_ID_OPT% %WIN_PORTABLE_OPT% ..
210      IF !ERRORLEVEL! NEQ 0 (
211         set OLD_ERRORLEVEL=!ERRORLEVEL!
212         del /f "CMakeCache.txt"
213         exit /b !OLD_ERRORLEVEL!
214      )
215   )
216   echo Building MuseScore...
217   cmake --build . --config %CONFIGURATION_STR% --target mscore
218   GOTO :END
219
220:INSTALL
221   cd "%BUILD_FOLDER%"
222   echo Installing MuseScore files...
223   cmake --build . --config %CONFIGURATION_STR% --target install
224   GOTO :END
225
226:END
227exit /b !ERRORLEVEL!
228