1@echo off
2rem Rime build script for msvc toolchain.
3rem Maintainer: Chen Gong <chen.sst@gmail.com>
4
5setlocal
6set BACK=%CD%
7
8if not exist env.bat copy env.bat.template env.bat
9
10if exist env.bat call env.bat
11
12rem for Windows XP compatibility (Visual Studio 2015+)
13set CL=/Zc:threadSafeInit-
14
15set OLD_PATH=%PATH%
16if defined DEVTOOLS_PATH set PATH=%OLD_PATH%;%DEVTOOLS_PATH%
17path
18echo.
19
20if not defined RIME_ROOT set RIME_ROOT=%CD%
21echo RIME_ROOT=%RIME_ROOT%
22echo.
23
24if defined BOOST_ROOT (
25  if exist "%BOOST_ROOT%\boost" goto boost_found
26)
27echo Error: Boost not found! Please set BOOST_ROOT in env.bat.
28exit /b 1
29:boost_found
30echo BOOST_ROOT=%BOOST_ROOT%
31echo.
32
33if not defined BJAM_TOOLSET (
34  rem the number actually means platform toolset, not %VisualStudioVersion%
35  set BJAM_TOOLSET=msvc-14.1
36)
37
38if not defined CMAKE_GENERATOR (
39  set CMAKE_GENERATOR="Visual Studio 15 2017"
40)
41
42if not defined PLATFORM_TOOLSET (
43  set PLATFORM_TOOLSET=v141_xp
44)
45
46set clean=0
47set build_dir_base=build
48set build_dir_suffix=
49set build_config=Release
50set build_boost=0
51set build_boost_x64=0
52set boost_build_variant=release
53set build_thirdparty=0
54set build_librime=0
55set build_shared=ON
56set build_test=OFF
57set enable_logging=ON
58
59:parse_cmdline_options
60if "%1" == "" goto end_parsing_cmdline_options
61if "%1" == "clean" set clean=1
62if "%1" == "boost" set build_boost=1
63if "%1" == "boost_x64" (
64  set build_boost=1
65  set build_boost_x64=1
66)
67if "%1" == "thirdparty" set build_thirdparty=1
68if "%1" == "librime" set build_librime=1
69if "%1" == "static" (
70  set build_dir_suffix=-static
71  set build_shared=OFF
72)
73if "%1" == "shared" (
74  set build_dir_suffix=
75  set build_shared=ON
76)
77if "%1" == "test" (
78  set build_librime=1
79  set build_test=ON
80)
81if "%1" == "debug" (
82  set build_dir_base=debug
83  set build_config=Debug
84  set boost_build_variant=debug
85)
86if "%1" == "release" (
87  set build_dir_base=build
88  set build_config=Release
89  set boost_build_variant=release
90
91)
92if "%1" == "logging" (
93  set enable_logging=ON
94)
95if "%1" == "nologging" (
96  set enable_logging=OFF
97)
98shift
99goto parse_cmdline_options
100:end_parsing_cmdline_options
101
102if %clean% == 0 (
103if %build_librime% == 0 (
104if %build_boost% == 0 (
105if %build_thirdparty% == 0 (
106  set build_librime=1
107))))
108
109if %clean% == 1 (
110  rmdir /s /q build
111  rmdir /s /q thirdparty\src\capnproto\build
112  rmdir /s /q thirdparty\src\glog\cmake-build
113  rmdir /s /q thirdparty\src\googletest\build
114  rmdir /s /q thirdparty\src\leveldb\build
115  rmdir /s /q thirdparty\src\marisa-trie\build
116  rmdir /s /q thirdparty\src\opencc\build
117  rmdir /s /q thirdparty\src\yaml-cpp\build
118)
119
120set build_dir=%build_dir_base%%build_dir_suffix%
121
122set DIST_DIR=%RIME_ROOT%\dist
123set THIRDPARTY=%RIME_ROOT%\thirdparty
124
125rem set CURL=%THIRDPARTY%\bin\curl.exe
126rem set DOWNLOAD="%CURL%" --remote-name-all
127
128set BOOST_COMPILED_LIBS=--with-date_time^
129 --with-filesystem^
130 --with-locale^
131 --with-regex^
132 --with-system^
133 --with-thread
134
135set BJAM_OPTIONS_COMMON=toolset=%BJAM_TOOLSET%^
136 variant=%boost_build_variant%^
137 link=static^
138 threading=multi^
139 runtime-link=static^
140 cxxflags="/Zc:threadSafeInit- "
141
142set BJAM_OPTIONS_X86=%BJAM_OPTIONS_COMMON%^
143 define=BOOST_USE_WINAPI_VERSION=0x0501
144
145set BJAM_OPTIONS_X64=%BJAM_OPTIONS_COMMON%^
146 define=BOOST_USE_WINAPI_VERSION=0x0502^
147 address-model=64^
148 --stagedir=stage_x64
149
150if %build_boost% == 1 (
151  cd /d %BOOST_ROOT%
152  if not exist bjam.exe call bootstrap.bat
153  if errorlevel 1 goto error
154
155  bjam %BJAM_OPTIONS_X86% stage %BOOST_COMPILED_LIBS%
156  if errorlevel 1 goto error
157
158  if %build_boost_x64% == 1 (
159    bjam %BJAM_OPTIONS_X64% stage %BOOST_COMPILED_LIBS%
160    if errorlevel 1 goto error
161  )
162)
163
164set THIRDPARTY_COMMON_CMAKE_FLAGS=-G%CMAKE_GENERATOR%^
165 -T%PLATFORM_TOOLSET%^
166 -DCMAKE_CONFIGURATION_TYPES:STRING="%build_config%"^
167 -DCMAKE_CXX_FLAGS_RELEASE:STRING="/MT /O2 /Ob2 /DNDEBUG"^
168 -DCMAKE_C_FLAGS_RELEASE:STRING="/MT /O2 /Ob2 /DNDEBUG"^
169 -DCMAKE_CXX_FLAGS_DEBUG:STRING="/MTd /Od"^
170 -DCMAKE_C_FLAGS_DEBUG:STRING="/MTd /Od"^
171 -DCMAKE_INSTALL_PREFIX:PATH="%THIRDPARTY%"
172
173if %build_thirdparty% == 1 (
174  cd /d %THIRDPARTY%
175
176  echo building capnproto.
177  cd %THIRDPARTY%\src\capnproto
178  cmake . -B%build_dir% %THIRDPARTY_COMMON_CMAKE_FLAGS%^
179	-DBUILD_SHARED_LIBS:BOOL=OFF^
180	-DBUILD_TESTING:BOOL=OFF
181  if errorlevel 1 goto error
182  cmake --build %build_dir% --config %build_config% --target INSTALL
183  if errorlevel 1 goto error
184
185  echo building glog.
186  cd %THIRDPARTY%\src\glog
187  cmake . -Bcmake-%build_dir% %THIRDPARTY_COMMON_CMAKE_FLAGS%^
188  -DBUILD_TESTING:BOOL=OFF^
189  -DWITH_GFLAGS:BOOL=OFF
190  if errorlevel 1 goto error
191  cmake --build cmake-%build_dir% --config %build_config% --target INSTALL
192  if errorlevel 1 goto error
193
194  echo building leveldb.
195  cd %THIRDPARTY%\src\leveldb
196  cmake . -B%build_dir% %THIRDPARTY_COMMON_CMAKE_FLAGS%^
197  -DLEVELDB_BUILD_BENCHMARKS:BOOL=OFF^
198  -DLEVELDB_BUILD_TESTS:BOOL=OFF
199  if errorlevel 1 goto error
200  cmake --build %build_dir% --config %build_config% --target INSTALL
201  if errorlevel 1 goto error
202
203  echo building yaml-cpp.
204  cd %THIRDPARTY%\src\yaml-cpp
205  cmake . -B%build_dir% %THIRDPARTY_COMMON_CMAKE_FLAGS%^
206  -DMSVC_SHARED_RT:BOOL=OFF^
207  -DYAML_CPP_BUILD_CONTRIB:BOOL=OFF^
208  -DYAML_CPP_BUILD_TESTS:BOOL=OFF^
209  -DYAML_CPP_BUILD_TOOLS:BOOL=OFF
210  if errorlevel 1 goto error
211  cmake --build %build_dir% --config %build_config% --target INSTALL
212  if errorlevel 1 goto error
213
214  echo building gtest.
215  cd %THIRDPARTY%\src\googletest
216  cmake . -B%build_dir% %THIRDPARTY_COMMON_CMAKE_FLAGS%^
217  -DBUILD_GMOCK:BOOL=OFF
218  if errorlevel 1 goto error
219  cmake --build %build_dir% --config %build_config% --target INSTALL
220  if errorlevel 1 goto error
221
222  echo building marisa.
223  cd %THIRDPARTY%\src\marisa-trie
224  cmake %THIRDPARTY%\src -B%build_dir% %THIRDPARTY_COMMON_CMAKE_FLAGS%
225  if errorlevel 1 goto error
226  cmake --build %build_dir% --config %build_config% --target INSTALL
227  if errorlevel 1 goto error
228
229  echo building opencc.
230  cd %THIRDPARTY%\src\opencc
231  cmake . -B%build_dir% %THIRDPARTY_COMMON_CMAKE_FLAGS%^
232  -DBUILD_SHARED_LIBS=OFF^
233  -DBUILD_TESTING=OFF
234  if errorlevel 1 goto error
235  cmake --build %build_dir% --config %build_config% --target INSTALL
236  if errorlevel 1 goto error
237)
238
239if %build_librime% == 0 goto exit
240
241set RIME_CMAKE_FLAGS=-G%CMAKE_GENERATOR%^
242 -T%PLATFORM_TOOLSET%^
243 -DBUILD_STATIC=ON^
244 -DBUILD_SHARED_LIBS=%build_shared%^
245 -DBUILD_TEST=%build_test%^
246 -DENABLE_LOGGING=%enable_logging%^
247 -DBOOST_USE_CXX11=ON^
248 -DCMAKE_CONFIGURATION_TYPES="%build_config%"^
249 -DCMAKE_INSTALL_PREFIX:PATH="%DIST_DIR%"
250
251cd /d %RIME_ROOT%
252echo cmake %RIME_ROOT% -B%build_dir% %RIME_CMAKE_FLAGS%
253call cmake %RIME_ROOT% -B%build_dir% %RIME_CMAKE_FLAGS%
254if errorlevel 1 goto error
255
256echo.
257echo building librime.
258cmake --build %build_dir% --config %build_config% --target INSTALL
259if errorlevel 1 goto error
260
261echo.
262echo ready.
263echo.
264goto exit
265
266:error
267set exitcode=%errorlevel%
268echo.
269echo error building la rime.
270echo.
271
272:exit
273set PATH=%OLD_PATH%
274cd /d %BACK%
275exit /b %exitcode%
276