build_environment: support for msvc2017 and newer cmake.
[blender.git] / build_files / build_environment / cmake / options.cmake
1 # ***** BEGIN GPL LICENSE BLOCK *****
2 #
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software Foundation,
15 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 #
17 # ***** END GPL LICENSE BLOCK *****
18
19 if(WIN32)
20         option(ENABLE_MINGW64 "Enable building of ffmpeg/iconv/libsndfile/lapack/fftw3 by installing mingw64" ON)
21 endif()
22 option(WITH_WEBP "Enable building of oiio with webp support" OFF)
23 set(MAKE_THREADS 1 CACHE STRING "Number of threads to run make with")
24
25 if(NOT BUILD_MODE)
26         set(BUILD_MODE "Release")
27         message(STATUS "Build type not specified: defaulting to a release build.")
28 endif()
29 message("BuildMode = ${BUILD_MODE}")
30
31 if(BUILD_MODE STREQUAL "Debug")
32         set(LIBDIR ${CMAKE_CURRENT_BINARY_DIR}/Debug)
33 else(BUILD_MODE STREQUAL "Debug")
34         set(LIBDIR ${CMAKE_CURRENT_BINARY_DIR}/Release)
35 endif()
36
37 option(DOWNLOAD_DIR "Path for downloaded files" ${CMAKE_CURRENT_SOURCE_DIR}/downloads)
38 file(TO_CMAKE_PATH ${DOWNLOAD_DIR} DOWNLOAD_DIR)
39 set(PATCH_DIR ${CMAKE_CURRENT_SOURCE_DIR}/patches)
40 set(BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR}/build)
41
42 message("LIBDIR = ${LIBDIR}")
43 message("DOWNLOAD_DIR = ${DOWNLOAD_DIR}")
44 message("PATCH_DIR = ${PATCH_DIR}")
45 message("BUILD_DIR = ${BUILD_DIR}")
46
47 if(WIN32)
48         if("${CMAKE_SIZEOF_VOID_P}" EQUAL "8")
49                 set(PATCH_CMD ${DOWNLOAD_DIR}/mingw/mingw64/msys/1.0/bin/patch.exe)
50         else()
51                 set(PATCH_CMD ${DOWNLOAD_DIR}/mingw/mingw32/msys/1.0/bin/patch.exe)
52         endif()
53         set(LIBEXT ".lib")
54         set(LIBPREFIX "")
55
56         # For OIIO and OSL
57         set(COMMON_DEFINES /DPSAPI_VERSION=1 /DOIIO_STATIC_BUILD /DTINYFORMAT_ALLOW_WCHAR_STRINGS)
58
59         if(MSVC_VERSION GREATER 1909)
60                 set(COMMON_MSVC_FLAGS "/Wv:18") #some deps with warnings as error aren't quite ready for dealing with the new 2017 warnings. 
61         endif()
62         
63         if(WITH_OPTIMIZED_DEBUG)
64                 set(BLENDER_CMAKE_C_FLAGS_DEBUG "/MTd ${COMMON_MSVC_FLAGS} /O2 /Ob2 /DNDEBUG /DPSAPI_VERSION=1 /DOIIO_STATIC_BUILD /DTINYFORMAT_ALLOW_WCHAR_STRINGS")
65         else()
66                 set(BLENDER_CMAKE_C_FLAGS_DEBUG "/MTd ${COMMON_MSVC_FLAGS} /Zi /Ob0 /Od /RTC1 /D_DEBUG /DPSAPI_VERSION=1 /DOIIO_STATIC_BUILD /DTINYFORMAT_ALLOW_WCHAR_STRINGS")
67         endif()
68         set(BLENDER_CMAKE_C_FLAGS_MINSIZEREL "/MT ${COMMON_MSVC_FLAGS} /O1 /Ob1 /D NDEBUG /DPSAPI_VERSION=1 /DOIIO_STATIC_BUILD /DTINYFORMAT_ALLOW_WCHAR_STRINGS")
69         set(BLENDER_CMAKE_C_FLAGS_RELEASE "/MT ${COMMON_MSVC_FLAGS} /O2 /Ob2 /DNDEBUG /DPSAPI_VERSION=1 /DOIIO_STATIC_BUILD /DTINYFORMAT_ALLOW_WCHAR_STRINGS")
70         set(BLENDER_CMAKE_C_FLAGS_RELWITHDEBINFO "/MT ${COMMON_MSVC_FLAGS} /Zi /O2 /Ob1 /D NDEBUG /DPSAPI_VERSION=1 /DOIIO_STATIC_BUILD /DTINYFORMAT_ALLOW_WCHAR_STRINGS")
71
72         if(WITH_OPTIMIZED_DEBUG)
73                 set(BLENDER_CMAKE_CXX_FLAGS_DEBUG "/MTd ${COMMON_MSVC_FLAGS} /O2 /Ob2 /D NDEBUG /D PLATFORM_WINDOWS /DPSAPI_VERSION=1 /DOIIO_STATIC_BUILD /DTINYFORMAT_ALLOW_WCHAR_STRINGS")
74         else()
75                 set(BLENDER_CMAKE_CXX_FLAGS_DEBUG "/D_DEBUG /D PLATFORM_WINDOWS /MTd  ${COMMON_MSVC_FLAGS} /Zi /Ob0 /Od /RTC1 /DPSAPI_VERSION=1 /DOIIO_STATIC_BUILD /DTINYFORMAT_ALLOW_WCHAR_STRINGS")
76         endif()
77         set(BLENDER_CMAKE_CXX_FLAGS_MINSIZEREL "/MT /${COMMON_MSVC_FLAGS} /O1 /Ob1 /D NDEBUG  /D PLATFORM_WINDOWS /DPSAPI_VERSION=1 /DOIIO_STATIC_BUILD /DTINYFORMAT_ALLOW_WCHAR_STRINGS")
78         set(BLENDER_CMAKE_CXX_FLAGS_RELEASE "/MT ${COMMON_MSVC_FLAGS} /O2 /Ob2 /D NDEBUG /D PLATFORM_WINDOWS /DPSAPI_VERSION=1 /DOIIO_STATIC_BUILD /DTINYFORMAT_ALLOW_WCHAR_STRINGS")
79         set(BLENDER_CMAKE_CXX_FLAGS_RELWITHDEBINFO "/MT ${COMMON_MSVC_FLAGS} /Zi /O2 /Ob1 /D NDEBUG /D PLATFORM_WINDOWS /DPSAPI_VERSION=1 /DOIIO_STATIC_BUILD /DTINYFORMAT_ALLOW_WCHAR_STRINGS")
80
81         set(PLATFORM_FLAGS)
82         set(PLATFORM_CXX_FLAGS)
83         set(PLATFORM_CMAKE_FLAGS)
84
85         if("${CMAKE_SIZEOF_VOID_P}" EQUAL "8")
86                 set(MINGW_PATH ${DOWNLOAD_DIR}/mingw/mingw64)
87                 set(MINGW_SHELL ming64sh.cmd)
88                 set(PERL_SHELL ${DOWNLOAD_DIR}/perl/portableshell.bat)
89                 set(MINGW_HOST x86_64-w64-mingw32)
90         else()
91                 set(MINGW_PATH ${DOWNLOAD_DIR}/mingw/mingw32)
92                 set(MINGW_SHELL ming32sh.cmd)
93                 set(PERL_SHELL ${DOWNLOAD_DIR}/perl32/portableshell.bat)
94                 set(MINGW_HOST i686-w64-mingw32)
95         endif()
96
97         set(CONFIGURE_ENV
98                 cd ${MINGW_PATH} &&
99                 call ${MINGW_SHELL} &&
100                 call ${PERL_SHELL} &&
101                 set path &&
102                 set CFLAGS=-g &&
103                 set LDFLAGS=-Wl,--as-needed -static-libgcc
104         )
105
106         set(CONFIGURE_ENV_NO_PERL
107                 cd ${MINGW_PATH} &&
108                 call ${MINGW_SHELL} &&
109                 set path &&
110                 set CFLAGS=-g &&
111                 set LDFLAGS=-Wl,--as-needed -static-libgcc
112         )
113
114         set(CONFIGURE_COMMAND sh ./configure)
115         set(CONFIGURE_COMMAND_NO_TARGET ${CONFIGURE_COMMAND})
116 else()
117         set(PATCH_CMD patch)
118         set(LIBEXT ".a")
119         set(LIBPREFIX "lib")
120
121         if(APPLE)
122                 # Let's get the current Xcode dir, to support xcode-select
123                 execute_process(
124                         COMMAND xcode-select --print-path
125                         OUTPUT_VARIABLE XCODE_DEV_PATH OUTPUT_STRIP_TRAILING_WHITESPACE
126                 )
127                 set(OSX_ARCHITECTURES x86_64)
128                 set(OSX_DEPLOYMENT_TARGET 10.9)
129                 set(OSX_SDK_VERSION 10.12)
130                 set(OSX_SYSROOT ${XCODE_DEV_PATH}/Platforms/MacOSX.platform/Developer/SDKs/MacOSX${OSX_SDK_VERSION}.sdk)
131
132                 set(PLATFORM_CFLAGS "-isysroot ${OSX_SYSROOT} -mmacosx-version-min=${OSX_DEPLOYMENT_TARGET}")
133                 set(PLATFORM_CXXFLAGS "-isysroot ${OSX_SYSROOT} -mmacosx-version-min=${OSX_DEPLOYMENT_TARGET} -std=c++11 -stdlib=libc++")
134                 set(PLATFORM_LDFLAGS "-isysroot ${OSX_SYSROOT} -mmacosx-version-min=${OSX_DEPLOYMENT_TARGET}")
135                 set(PLATFORM_BUILD_TARGET --build=x86_64-apple-darwin13.0.0) # OS X 10.9
136                 set(PLATFORM_CMAKE_FLAGS
137                         -DCMAKE_OSX_ARCHITECTURES:STRING=${OSX_ARCHITECTURES}
138                         -DCMAKE_OSX_DEPLOYMENT_TARGET:STRING=${OSX_DEPLOYMENT_TARGET}
139                         -DCMAKE_OSX_SYSROOT:PATH=${OSX_SYSROOT}
140                 )
141         else()
142                 set(PLATFORM_CFLAGS "-fPIC")
143                 set(PLATFORM_CXXFLAGS "-std=c++11 -fPIC")
144                 set(PLATFORM_LDFLAGS)
145                 set(PLATFORM_BUILD_TARGET)
146                 set(PLATFORM_CMAKE_FLAGS)
147         endif()
148
149         if(WITH_OPTIMIZED_DEBUG)
150                 set(BLENDER_CMAKE_C_FLAGS_DEBUG "-O2 -DNDEBUG")
151         else()
152                 set(BLENDER_CMAKE_C_FLAGS_DEBUG "-g")
153         endif()
154         set(BLENDER_CMAKE_C_FLAGS_MINSIZEREL "-Os -DNDEBUG")
155         set(BLENDER_CMAKE_C_FLAGS_RELEASE "-O2 -DNDEBUG")
156         set(BLENDER_CMAKE_C_FLAGS_RELWITHDEBINFO "-O2 -g -DNDEBUG")
157
158         if(WITH_OPTIMIZED_DEBUG)
159                 set(BLENDER_CMAKE_CXX_FLAGS_DEBUG "-O2 -DNDEBUG ${PLATFORM_CXXFLAGS}")
160         else()
161                 set(BLENDER_CMAKE_CXX_FLAGS_DEBUG "-g ${PLATFORM_CXXFLAGS}")
162         endif()
163
164         set(BLENDER_CMAKE_CXX_FLAGS_MINSIZEREL "-Os -DNDEBUG ${PLATFORM_CXXFLAGS}")
165         set(BLENDER_CMAKE_CXX_FLAGS_RELEASE "-O2 -DNDEBUG ${PLATFORM_CXXFLAGS}")
166         set(BLENDER_CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O2 -g -DNDEBUG ${PLATFORM_CXXFLAGS}")
167
168         set(CONFIGURE_ENV
169                 export MACOSX_DEPLOYMENT_TARGET=${OSX_DEPLOYMENT_TARGET} &&
170                 export CFLAGS=${PLATFORM_CFLAGS} &&
171                 export CXXFLAGS=${PLATFORM_CXXFLAGS} &&
172                 export LDFLAGS=${PLATFORM_LDFLAGS}
173         )
174         set(CONFIGURE_ENV_NO_PERL ${CONFIGURE_ENV})
175         set(CONFIGURE_COMMAND ./configure ${PLATFORM_BUILD_TARGET})
176         set(CONFIGURE_COMMAND_NO_TARGET ./configure)
177 endif()
178
179 set(DEFAULT_CMAKE_FLAGS
180         -DCMAKE_BUILD_TYPE=${BUILD_MODE}
181         -DCMAKE_C_FLAGS_DEBUG=${BLENDER_CMAKE_C_FLAGS_DEBUG}
182         -DCMAKE_C_FLAGS_MINSIZEREL=${BLENDER_CMAKE_C_FLAGS_MINSIZEREL}
183         -DCMAKE_C_FLAGS_RELEASE=${BLENDER_CMAKE_C_FLAGS_RELEASE}
184         -DCMAKE_C_FLAGS_RELWITHDEBINFO=${BLENDER_CMAKE_C_FLAGS_RELWITHDEBINFO}
185         -DCMAKE_CXX_FLAGS_DEBUG=${BLENDER_CMAKE_CXX_FLAGS_DEBUG}
186         -DCMAKE_CXX_FLAGS_MINSIZEREL=${BLENDER_CMAKE_CXX_FLAGS_MINSIZEREL}
187         -DCMAKE_CXX_FLAGS_RELEASE=${BLENDER_CMAKE_CXX_FLAGS_RELEASE}
188         -DCMAKE_CXX_FLAGS_RELWITHDEBINFO=${CMAKE_CXX_FLAGS_RELWITHDEBINFO}
189         ${PLATFORM_CMAKE_FLAGS}
190 )
191
192 if(WIN32)
193         #we need both flavors to build the thumbnail dlls
194         if(MSVC12)
195                 set(GENERATOR_32 "Visual Studio 12 2013")
196                 set(GENERATOR_64 "Visual Studio 12 2013 Win64")
197         elseif(MSVC14)
198                 set(GENERATOR_32 "Visual Studio 14 2015")
199                 set(GENERATOR_64 "Visual Studio 14 2015 Win64")
200         endif()
201 endif()
202
203
204 if(WIN32)
205         set(ZLIB_LIBRARY zlibstatic${LIBEXT})
206 else()
207         set(ZLIB_LIBRARY libz${LIBEXT})
208 endif()
209
210 if(MSVC)
211         set_property(GLOBAL PROPERTY USE_FOLDERS ON)
212 endif()
213
214 set(CMAKE_INSTALL_MESSAGE LAZY)