d29f086069aff863f3aa57ac7bca2c43c93d8526
[blender-staging.git] / build_files / cmake / macros.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 # The Original Code is Copyright (C) 2006, Blender Foundation
18 # All rights reserved.
19 #
20 # The Original Code is: all of this file.
21 #
22 # Contributor(s): Jacques Beaurain.
23 #
24 # ***** END GPL LICENSE BLOCK *****
25
26 macro(list_insert_after
27         list_id item_check item_add
28         )
29         set(_index)
30         list(FIND "${list_id}" "${item_check}" _index)
31         if("${_index}" MATCHES "-1")
32                 message(FATAL_ERROR "'${list_id}' doesn't contain '${item_check}'")
33         endif()
34         math(EXPR _index "${_index} + 1")
35         list(INSERT ${list_id} "${_index}" ${item_add})
36         unset(_index)
37 endmacro()
38
39 macro(list_insert_before
40         list_id item_check item_add
41         )
42         set(_index)
43         list(FIND "${list_id}" "${item_check}" _index)
44         if("${_index}" MATCHES "-1")
45                 message(FATAL_ERROR "'${list_id}' doesn't contain '${item_check}'")
46         endif()
47         list(INSERT ${list_id} "${_index}" ${item_add})
48         unset(_index)
49 endmacro()
50
51 function(list_assert_duplicates
52         list_id
53         )
54         
55         # message(STATUS "list data: ${list_id}")
56
57         list(LENGTH list_id _len_before)
58         list(REMOVE_DUPLICATES list_id)
59         list(LENGTH list_id _len_after)
60         # message(STATUS "list size ${_len_before} -> ${_len_after}")
61         if(NOT _len_before EQUAL _len_after)
62                 message(FATAL_ERROR "duplicate found in list which should not contain duplicates: ${list_id}")
63         endif()
64         unset(_len_before)
65         unset(_len_after)
66 endfunction()
67
68
69 # foo_bar.spam --> foo_barMySuffix.spam
70 macro(file_suffix
71         file_name_new file_name file_suffix
72         )
73
74         get_filename_component(_file_name_PATH ${file_name} PATH)
75         get_filename_component(_file_name_NAME_WE ${file_name} NAME_WE)
76         get_filename_component(_file_name_EXT ${file_name} EXT)
77         set(${file_name_new} "${_file_name_PATH}/${_file_name_NAME_WE}${file_suffix}${_file_name_EXT}")
78
79         unset(_file_name_PATH)
80         unset(_file_name_NAME_WE)
81         unset(_file_name_EXT)
82 endmacro()
83
84 # useful for adding debug suffix to library lists:
85 # /somepath/foo.lib --> /somepath/foo_d.lib
86 macro(file_list_suffix
87         fp_list_new fp_list fn_suffix
88         )
89
90         # incase of empty list
91         set(_fp)
92         set(_fp_suffixed)
93
94         set(fp_list_new)
95
96         foreach(_fp ${fp_list})
97                 file_suffix(_fp_suffixed "${_fp}" "${fn_suffix}")
98                 list(APPEND "${fp_list_new}" "${_fp_suffixed}")
99         endforeach()
100
101         unset(_fp)
102         unset(_fp_suffixed)
103
104 endmacro()
105
106 if(UNIX AND NOT APPLE)
107         macro(find_package_static)
108                 set(_cmake_find_library_suffixes_back ${CMAKE_FIND_LIBRARY_SUFFIXES})
109                 set(CMAKE_FIND_LIBRARY_SUFFIXES .a ${CMAKE_FIND_LIBRARY_SUFFIXES})
110                 find_package(${ARGV})
111                 set(CMAKE_FIND_LIBRARY_SUFFIXES ${_cmake_find_library_suffixes_back})
112                 unset(_cmake_find_library_suffixes_back)
113         endmacro()
114
115         macro(find_library_static)
116                 set(_cmake_find_library_suffixes_back ${CMAKE_FIND_LIBRARY_SUFFIXES})
117                 set(CMAKE_FIND_LIBRARY_SUFFIXES .a ${CMAKE_FIND_LIBRARY_SUFFIXES})
118                 find_library(${ARGV})
119                 set(CMAKE_FIND_LIBRARY_SUFFIXES ${_cmake_find_library_suffixes_back})
120                 unset(_cmake_find_library_suffixes_back)
121         endmacro()
122 endif()
123
124 function(target_link_libraries_optimized
125         TARGET
126         LIBS
127         )
128
129         foreach(_LIB ${LIBS})
130                 target_link_libraries(${TARGET} optimized "${_LIB}")
131         endforeach()
132 endfunction()
133
134 function(target_link_libraries_debug
135         TARGET
136         LIBS
137         )
138
139         foreach(_LIB ${LIBS})
140                 target_link_libraries(${TARGET} debug "${_LIB}")
141         endforeach()
142 endfunction()
143
144 # Nicer makefiles with -I/1/foo/ instead of -I/1/2/3/../../foo/
145 # use it instead of include_directories()
146 function(blender_include_dirs
147         includes
148         )
149
150         set(_ALL_INCS "")
151         foreach(_INC ${ARGV})
152                 get_filename_component(_ABS_INC ${_INC} ABSOLUTE)
153                 list(APPEND _ALL_INCS ${_ABS_INC})
154                 # for checking for invalid includes, disable for regular use
155                 ##if(NOT EXISTS "${_ABS_INC}/")
156                 ##      message(FATAL_ERROR "Include not found: ${_ABS_INC}/")
157                 ##endif()
158         endforeach()
159         include_directories(${_ALL_INCS})
160 endfunction()
161
162 function(blender_include_dirs_sys
163         includes
164         )
165
166         set(_ALL_INCS "")
167         foreach(_INC ${ARGV})
168                 get_filename_component(_ABS_INC ${_INC} ABSOLUTE)
169                 list(APPEND _ALL_INCS ${_ABS_INC})
170                 ##if(NOT EXISTS "${_ABS_INC}/")
171                 ##      message(FATAL_ERROR "Include not found: ${_ABS_INC}/")
172                 ##endif()
173         endforeach()
174         include_directories(SYSTEM ${_ALL_INCS})
175 endfunction()
176
177 function(blender_source_group
178         sources
179         )
180
181         # Group by location on disk
182         source_group("Source Files" FILES CMakeLists.txt)
183
184         foreach(_SRC ${sources})
185                 get_filename_component(_SRC_EXT ${_SRC} EXT)
186                 if((${_SRC_EXT} MATCHES ".h") OR
187                    (${_SRC_EXT} MATCHES ".hpp") OR
188                    (${_SRC_EXT} MATCHES ".hh"))
189
190                         set(GROUP_ID "Header Files")
191                 else()
192                         set(GROUP_ID "Source Files")
193                 endif()
194                 source_group("${GROUP_ID}" FILES ${_SRC})
195         endforeach()
196 endfunction()
197
198
199 # only MSVC uses SOURCE_GROUP
200 function(blender_add_lib_nolist
201         name
202         sources
203         includes
204         includes_sys
205         )
206
207         # message(STATUS "Configuring library ${name}")
208
209         # include_directories(${includes})
210         # include_directories(SYSTEM ${includes_sys})
211         blender_include_dirs("${includes}")
212         blender_include_dirs_sys("${includes_sys}")
213
214         add_library(${name} ${sources})
215
216         # works fine without having the includes
217         # listed is helpful for IDE's (QtCreator/MSVC)
218         blender_source_group("${sources}")
219
220         list_assert_duplicates("${sources}")
221         list_assert_duplicates("${includes}")
222         # Not for system includes because they can resolve to the same path
223         # list_assert_duplicates("${includes_sys}")
224
225 endfunction()
226
227
228 function(blender_add_lib
229         name
230         sources
231         includes
232         includes_sys
233         )
234
235         blender_add_lib_nolist(${name} "${sources}" "${includes}" "${includes_sys}")
236
237         set_property(GLOBAL APPEND PROPERTY BLENDER_LINK_LIBS ${name})
238 endfunction()
239
240
241 function(SETUP_LIBDIRS)
242
243         # NOTE: For all new libraries, use absolute library paths.
244         # This should eventually be phased out.
245
246         link_directories(${JPEG_LIBPATH} ${PNG_LIBPATH} ${ZLIB_LIBPATH} ${FREETYPE_LIBPATH})
247
248         if(WITH_PYTHON)  #  AND NOT WITH_PYTHON_MODULE  # WIN32 needs
249                 link_directories(${PYTHON_LIBPATH})
250         endif()
251         if(WITH_SDL AND NOT WITH_SDL_DYNLOAD)
252                 link_directories(${SDL_LIBPATH})
253         endif()
254         if(WITH_CODEC_FFMPEG)
255                 link_directories(${FFMPEG_LIBPATH})
256         endif()
257         if(WITH_IMAGE_OPENEXR)
258                 link_directories(${OPENEXR_LIBPATH})
259         endif()
260         if(WITH_IMAGE_TIFF)
261                 link_directories(${TIFF_LIBPATH})
262         endif()
263         if(WITH_BOOST)
264                 link_directories(${BOOST_LIBPATH})
265         endif()
266         if(WITH_OPENIMAGEIO)
267                 link_directories(${OPENIMAGEIO_LIBPATH})
268         endif()
269         if(WITH_OPENCOLORIO)
270                 link_directories(${OPENCOLORIO_LIBPATH})
271         endif()
272         if(WITH_OPENVDB)
273                 link_directories(${OPENVDB_LIBPATH})
274         endif()
275         if(WITH_OPENAL)
276                 link_directories(${OPENAL_LIBPATH})
277         endif()
278         if(WITH_JACK AND NOT WITH_JACK_DYNLOAD)
279                 link_directories(${JACK_LIBPATH})
280         endif()
281         if(WITH_CODEC_SNDFILE)
282                 link_directories(${SNDFILE_LIBPATH})
283         endif()
284         if(WITH_FFTW3)
285                 link_directories(${FFTW3_LIBPATH})
286         endif()
287         if(WITH_OPENCOLLADA)
288                 link_directories(${OPENCOLLADA_LIBPATH})
289                 ## Never set
290                 # link_directories(${PCRE_LIBPATH})
291                 # link_directories(${EXPAT_LIBPATH})
292         endif()
293         if(WITH_LLVM)
294                 link_directories(${LLVM_LIBPATH})
295         endif()
296
297         if(WIN32 AND NOT UNIX)
298                 link_directories(${PTHREADS_LIBPATH})
299         endif()
300 endfunction()
301
302 function(setup_liblinks
303         target
304         )
305
306         set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${PLATFORM_LINKFLAGS}" PARENT_SCOPE)
307         set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} ${PLATFORM_LINKFLAGS_DEBUG}" PARENT_SCOPE)
308
309         set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${PLATFORM_LINKFLAGS}" PARENT_SCOPE)
310         set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} ${PLATFORM_LINKFLAGS_DEBUG}" PARENT_SCOPE)
311
312         set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${PLATFORM_LINKFLAGS}" PARENT_SCOPE)
313         set(CMAKE_MODULE_LINKER_FLAGS_DEBUG "${CMAKE_MODULE_LINKER_FLAGS_DEBUG} ${PLATFORM_LINKFLAGS_DEBUG}" PARENT_SCOPE)
314
315         target_link_libraries(
316                 ${target}
317                 ${PNG_LIBRARIES}
318                 ${ZLIB_LIBRARIES}
319                 ${FREETYPE_LIBRARY}
320         )
321
322         # since we are using the local libs for python when compiling msvc projects, we need to add _d when compiling debug versions
323         if(WITH_PYTHON)  # AND NOT WITH_PYTHON_MODULE  # WIN32 needs
324                 target_link_libraries(${target} ${PYTHON_LINKFLAGS})
325
326                 if(WIN32 AND NOT UNIX)
327                         file_list_suffix(PYTHON_LIBRARIES_DEBUG "${PYTHON_LIBRARIES}" "_d")
328                         target_link_libraries_debug(${target} "${PYTHON_LIBRARIES_DEBUG}")
329                         target_link_libraries_optimized(${target} "${PYTHON_LIBRARIES}")
330                         unset(PYTHON_LIBRARIES_DEBUG)
331                 else()
332                         target_link_libraries(${target} ${PYTHON_LIBRARIES})
333                 endif()
334         endif()
335
336         if(WITH_LZO AND WITH_SYSTEM_LZO)
337                 target_link_libraries(${target} ${LZO_LIBRARIES})
338         endif()
339         if(WITH_SYSTEM_GLEW)
340                 target_link_libraries(${target} ${BLENDER_GLEW_LIBRARIES})
341         endif()
342         if(WITH_BULLET AND WITH_SYSTEM_BULLET)
343                 target_link_libraries(${target} ${BULLET_LIBRARIES})
344         endif()
345         if(WITH_AUDASPACE AND WITH_SYSTEM_AUDASPACE)
346                 target_link_libraries(${target} ${AUDASPACE_C_LIBRARIES} ${AUDASPACE_PY_LIBRARIES})
347         endif()
348         if(WITH_OPENAL)
349                 target_link_libraries(${target} ${OPENAL_LIBRARY})
350         endif()
351         if(WITH_FFTW3)
352                 target_link_libraries(${target} ${FFTW3_LIBRARIES})
353         endif()
354         if(WITH_JACK AND NOT WITH_JACK_DYNLOAD)
355                 target_link_libraries(${target} ${JACK_LIBRARIES})
356         endif()
357         if(WITH_CODEC_SNDFILE)
358                 target_link_libraries(${target} ${SNDFILE_LIBRARIES})
359         endif()
360         if(WITH_SDL AND NOT WITH_SDL_DYNLOAD)
361                 target_link_libraries(${target} ${SDL_LIBRARY})
362         endif()
363         if(WITH_CODEC_QUICKTIME)
364                 target_link_libraries(${target} ${QUICKTIME_LIBRARIES})
365         endif()
366         if(WITH_IMAGE_TIFF)
367                 target_link_libraries(${target} ${TIFF_LIBRARY})
368         endif()
369         if(WITH_OPENIMAGEIO)
370                 target_link_libraries(${target} ${OPENIMAGEIO_LIBRARIES})
371         endif()
372         if(WITH_OPENCOLORIO)
373                 target_link_libraries(${target} ${OPENCOLORIO_LIBRARIES})
374         endif()
375         if(WITH_OPENSUBDIV)
376                 if(WIN32 AND NOT UNIX)
377                         file_list_suffix(OPENSUBDIV_LIBRARIES_DEBUG "${OPENSUBDIV_LIBRARIES}" "_d")
378                         target_link_libraries_debug(${target} "${OPENSUBDIV_LIBRARIES_DEBUG}")
379                         target_link_libraries_optimized(${target} "${OPENSUBDIV_LIBRARIES}")
380                         unset(OPENSUBDIV_LIBRARIES_DEBUG)
381                 else()
382                         target_link_libraries(${target} ${OPENSUBDIV_LIBRARIES})
383                 endif()
384         endif()
385         if(WITH_OPENVDB)
386                 target_link_libraries(${target} ${OPENVDB_LIBRARIES} ${TBB_LIBRARIES})
387         endif()
388         if(WITH_CYCLES_OSL)
389                 target_link_libraries(${target} ${OSL_LIBRARIES})
390         endif()
391         if(WITH_BOOST)
392                 target_link_libraries(${target} ${BOOST_LIBRARIES})
393                 if(Boost_USE_STATIC_LIBS AND Boost_USE_ICU)
394                         target_link_libraries(${target} ${ICU_LIBRARIES})
395                 endif()
396         endif()
397         target_link_libraries(${target} ${JPEG_LIBRARIES})
398         if(WITH_IMAGE_OPENEXR)
399                 target_link_libraries(${target} ${OPENEXR_LIBRARIES})
400         endif()
401         if(WITH_IMAGE_OPENJPEG AND WITH_SYSTEM_OPENJPEG)
402                 target_link_libraries(${target} ${OPENJPEG_LIBRARIES})
403         endif()
404         if(WITH_CODEC_FFMPEG)
405                 target_link_libraries(${target} ${FFMPEG_LIBRARIES})
406         endif()
407         if(WITH_OPENCOLLADA)
408                 if(WIN32 AND NOT UNIX)
409                         file_list_suffix(OPENCOLLADA_LIBRARIES_DEBUG "${OPENCOLLADA_LIBRARIES}" "_d")
410                         target_link_libraries_debug(${target} "${OPENCOLLADA_LIBRARIES_DEBUG}")
411                         target_link_libraries_optimized(${target} "${OPENCOLLADA_LIBRARIES}")
412                         unset(OPENCOLLADA_LIBRARIES_DEBUG)
413
414                         file_list_suffix(PCRE_LIBRARIES_DEBUG "${PCRE_LIBRARIES}" "_d")
415                         target_link_libraries_debug(${target} "${PCRE_LIBRARIES_DEBUG}")
416                         target_link_libraries_optimized(${target} "${PCRE_LIBRARIES}")
417                         unset(PCRE_LIBRARIES_DEBUG)
418
419                         if(EXPAT_LIB)
420                                 file_list_suffix(EXPAT_LIB_DEBUG "${EXPAT_LIB}" "_d")
421                                 target_link_libraries_debug(${target} "${EXPAT_LIB_DEBUG}")
422                                 target_link_libraries_optimized(${target} "${EXPAT_LIB}")
423                                 unset(EXPAT_LIB_DEBUG)
424                         endif()
425                 else()
426                         target_link_libraries(
427                                 ${target}
428                                 ${OPENCOLLADA_LIBRARIES}
429                                 ${PCRE_LIBRARIES}
430                                 ${XML2_LIBRARIES}
431                                 ${EXPAT_LIB}
432                         )
433                 endif()
434         endif()
435         if(WITH_MEM_JEMALLOC)
436                 target_link_libraries(${target} ${JEMALLOC_LIBRARIES})
437         endif()
438         if(WITH_MOD_CLOTH_ELTOPO)
439                 target_link_libraries(${target} ${LAPACK_LIBRARIES})
440         endif()
441         if(WITH_LLVM)
442                 target_link_libraries(${target} ${LLVM_LIBRARY})
443         endif()
444         if(WIN32 AND NOT UNIX)
445                 target_link_libraries(${target} ${PTHREADS_LIBRARIES})
446         endif()
447         if(UNIX AND NOT APPLE)
448                 if(WITH_OPENMP_STATIC)
449                         target_link_libraries(${target} ${OpenMP_LIBRARIES})
450                 endif()
451                 if(WITH_INPUT_NDOF)
452                         target_link_libraries(${target} ${NDOF_LIBRARIES})
453                 endif()
454         endif()
455
456         # We put CLEW and CUEW here because OPENSUBDIV_LIBRARIES dpeends on them..
457         if(WITH_CYCLES OR WITH_COMPOSITOR OR WITH_OPENSUBDIV)
458                 target_link_libraries(${target} "extern_clew")
459                 if(WITH_CUDA_DYNLOAD)
460                         target_link_libraries(${target} "extern_cuew")
461                 else()
462                         target_link_libraries(${target} ${CUDA_CUDA_LIBRARY})
463                 endif()
464         endif()
465
466         #system libraries with no dependencies such as platform link libs or opengl should go last
467         target_link_libraries(${target}
468                         ${BLENDER_GL_LIBRARIES})
469
470         target_link_libraries(${target} ${PLATFORM_LINKLIBS} ${CMAKE_DL_LIBS})
471 endfunction()
472
473
474 function(SETUP_BLENDER_SORTED_LIBS)
475
476         get_property(BLENDER_LINK_LIBS GLOBAL PROPERTY BLENDER_LINK_LIBS)
477
478         list(APPEND BLENDER_LINK_LIBS
479                 bf_windowmanager
480                 bf_render
481         )
482
483         if(WITH_MOD_FLUID)
484                 list(APPEND BLENDER_LINK_LIBS bf_intern_elbeem)
485         endif()
486
487         if(WITH_CYCLES)
488                 list(APPEND BLENDER_LINK_LIBS
489                         cycles_render
490                         cycles_graph
491                         cycles_bvh
492                         cycles_device
493                         cycles_kernel
494                         cycles_util
495                         cycles_subd)
496                 if(WITH_CYCLES_OSL)
497                         list(APPEND BLENDER_LINK_LIBS cycles_kernel_osl)
498                 endif()
499         endif()
500
501         # Sort libraries
502         set(BLENDER_SORTED_LIBS
503                 bf_windowmanager
504
505                 bf_editor_space_api
506                 bf_editor_space_action
507                 bf_editor_space_buttons
508                 bf_editor_space_console
509                 bf_editor_space_file
510                 bf_editor_space_graph
511                 bf_editor_space_image
512                 bf_editor_space_info
513                 bf_editor_space_logic
514                 bf_editor_space_nla
515                 bf_editor_space_node
516                 bf_editor_space_outliner
517                 bf_editor_space_script
518                 bf_editor_space_sequencer
519                 bf_editor_space_text
520                 bf_editor_space_time
521                 bf_editor_space_userpref
522                 bf_editor_space_view3d
523                 bf_editor_space_clip
524
525                 bf_editor_transform
526                 bf_editor_util
527                 bf_editor_uvedit
528                 bf_editor_curve
529                 bf_editor_gpencil
530                 bf_editor_interface
531                 bf_editor_mesh
532                 bf_editor_metaball
533                 bf_editor_object
534                 bf_editor_armature
535                 bf_editor_physics
536                 bf_editor_render
537                 bf_editor_screen
538                 bf_editor_sculpt_paint
539                 bf_editor_sound
540                 bf_editor_animation
541                 bf_editor_datafiles
542                 bf_editor_mask
543                 bf_editor_io
544
545                 bf_render
546                 bf_python
547                 bf_python_ext
548                 bf_python_mathutils
549                 bf_python_bmesh
550                 bf_freestyle
551                 bf_ikplugin
552                 bf_modifiers
553                 bf_bmesh
554                 bf_gpu
555                 bf_blenloader
556                 bf_blenkernel
557                 bf_physics
558                 bf_nodes
559                 bf_rna
560                 bf_imbuf
561                 bf_blenlib
562                 bf_depsgraph
563                 bf_intern_ghost
564                 bf_intern_string
565                 bf_avi
566                 bf_imbuf_cineon
567                 bf_imbuf_openexr
568                 bf_imbuf_openimageio
569                 bf_imbuf_dds
570                 bf_collada
571                 bf_intern_elbeem
572                 bf_intern_memutil
573                 bf_intern_guardedalloc
574                 bf_intern_ctr
575                 bf_intern_utfconv
576                 ge_blen_routines
577                 ge_converter
578                 ge_phys_dummy
579                 ge_phys_bullet
580                 bf_intern_smoke
581                 extern_lzma
582                 extern_curve_fit_nd
583                 ge_logic_ketsji
584                 extern_recastnavigation
585                 ge_logic
586                 ge_rasterizer
587                 ge_oglrasterizer
588                 ge_logic_expressions
589                 ge_scenegraph
590                 ge_logic_network
591                 ge_logic_ngnetwork
592                 ge_logic_loopbacknetwork
593                 bf_intern_moto
594                 extern_openjpeg
595                 ge_videotex
596                 bf_dna
597                 bf_blenfont
598                 bf_blentranslation
599                 bf_intern_audaspace
600                 bf_intern_mikktspace
601                 bf_intern_dualcon
602                 bf_intern_cycles
603                 cycles_render
604                 cycles_graph
605                 cycles_bvh
606                 cycles_device
607                 cycles_kernel
608                 cycles_util
609                 cycles_subd
610                 bf_intern_opencolorio
611                 bf_intern_eigen
612                 extern_rangetree
613                 extern_wcwidth
614                 bf_intern_libmv
615                 extern_glog
616                 extern_gflags
617                 extern_sdlew
618
619                 bf_intern_glew_mx
620         )
621
622         if(WITH_COMPOSITOR)
623                 # added for opencl compositor
624                 list_insert_before(BLENDER_SORTED_LIBS "bf_blenkernel" "bf_compositor")
625                 list_insert_after(BLENDER_SORTED_LIBS "bf_compositor" "bf_intern_opencl")
626         endif()
627
628         if(WITH_LIBMV)
629                 list(APPEND BLENDER_SORTED_LIBS extern_ceres)
630         endif()
631
632         if(WITH_MOD_CLOTH_ELTOPO)
633                 list(APPEND BLENDER_SORTED_LIBS extern_eltopo)
634         endif()
635
636         if(NOT WITH_SYSTEM_LZO)
637                 list(APPEND BLENDER_SORTED_LIBS extern_minilzo)
638         endif()
639
640         if(NOT WITH_SYSTEM_GLEW)
641                 list(APPEND BLENDER_SORTED_LIBS ${BLENDER_GLEW_LIBRARIES})
642         endif()
643
644         if(WITH_BINRELOC)
645                 list(APPEND BLENDER_SORTED_LIBS extern_binreloc)
646         endif()
647
648         if(WITH_CXX_GUARDEDALLOC)
649                 list(APPEND BLENDER_SORTED_LIBS bf_intern_guardedalloc_cpp)
650         endif()
651
652         if(WITH_IK_SOLVER)
653                 list_insert_after(BLENDER_SORTED_LIBS "bf_intern_elbeem" "bf_intern_iksolver")
654         endif()
655
656         if(WITH_IK_ITASC)
657                 list(APPEND BLENDER_SORTED_LIBS bf_intern_itasc)
658         endif()
659
660         if(WITH_CODEC_QUICKTIME)
661                 list(APPEND BLENDER_SORTED_LIBS bf_quicktime)
662         endif()
663
664         if(WITH_MOD_BOOLEAN)
665                 list(APPEND BLENDER_SORTED_LIBS extern_carve)
666         endif()
667
668         if(WITH_GHOST_XDND)
669                 list(APPEND BLENDER_SORTED_LIBS extern_xdnd)
670         endif()
671
672         if(WITH_CYCLES_OSL)
673                 list_insert_after(BLENDER_SORTED_LIBS "cycles_kernel" "cycles_kernel_osl")
674         endif()
675
676         if(WITH_INTERNATIONAL)
677                 list(APPEND BLENDER_SORTED_LIBS bf_intern_locale)
678         endif()
679
680         if(WITH_BULLET)
681                 list_insert_after(BLENDER_SORTED_LIBS "bf_blenkernel" "bf_intern_rigidbody")
682         endif()
683
684         if(WITH_BULLET AND NOT WITH_SYSTEM_BULLET)
685                 list_insert_after(BLENDER_SORTED_LIBS "ge_logic_ngnetwork" "extern_bullet")
686         endif()
687
688         if(WITH_GAMEENGINE_DECKLINK)
689                 list(APPEND BLENDER_SORTED_LIBS bf_intern_decklink)
690         endif()
691
692         if(WIN32)
693                 list(APPEND BLENDER_SORTED_LIBS bf_intern_gpudirect)
694         endif()
695
696         if(WITH_OPENSUBDIV)
697                 list(APPEND BLENDER_SORTED_LIBS bf_intern_opensubdiv)
698         endif()
699
700         if(WITH_OPENVDB)
701                 list(APPEND BLENDER_SORTED_LIBS bf_intern_openvdb)
702         endif()
703
704         foreach(SORTLIB ${BLENDER_SORTED_LIBS})
705                 set(REMLIB ${SORTLIB})
706                 foreach(SEARCHLIB ${BLENDER_LINK_LIBS})
707                         if(${SEARCHLIB} STREQUAL ${SORTLIB})
708                                 set(REMLIB "")
709                         endif()
710                 endforeach()
711                 if(REMLIB)
712                         # message(STATUS "Removing library ${REMLIB} from blender linking because: not configured")
713                         list(APPEND REM_MSG ${REMLIB})
714                         list(REMOVE_ITEM BLENDER_SORTED_LIBS ${REMLIB})
715                 endif()
716         endforeach()
717         if(REM_MSG)
718                 list(SORT REM_MSG)
719                 message(STATUS "Blender Skipping: (${REM_MSG})")
720         endif()
721
722
723         set(BLENDER_SORTED_LIBS ${BLENDER_SORTED_LIBS} PARENT_SCOPE)
724
725         # for top-level tests
726         set_property(GLOBAL PROPERTY BLENDER_SORTED_LIBS_PROP ${BLENDER_SORTED_LIBS})
727 endfunction()
728
729 macro(TEST_SSE_SUPPORT
730         _sse_flags
731         _sse2_flags)
732
733         include(CheckCSourceRuns)
734
735         # message(STATUS "Detecting SSE support")
736         if(CMAKE_COMPILER_IS_GNUCC OR (CMAKE_C_COMPILER_ID MATCHES "Clang"))
737                 set(${_sse_flags} "-msse")
738                 set(${_sse2_flags} "-msse2")
739         elseif(MSVC)
740                 # x86_64 has this auto enabled
741                 if("${CMAKE_SIZEOF_VOID_P}" EQUAL "8")
742                         set(${_sse_flags} "")
743                         set(${_sse2_flags} "")
744                 else()
745                         set(${_sse_flags} "/arch:SSE")
746                         set(${_sse2_flags} "/arch:SSE2")
747                 endif()
748         elseif(CMAKE_C_COMPILER_ID MATCHES "Intel")
749                 set(${_sse_flags} "")  # icc defaults to -msse
750                 set(${_sse2_flags} "-msse2")
751         else()
752                 message(WARNING "SSE flags for this compiler: '${CMAKE_C_COMPILER_ID}' not known")
753                 set(${_sse_flags})
754                 set(${_sse2_flags})
755         endif()
756
757         set(CMAKE_REQUIRED_FLAGS "${${_sse_flags}} ${${_sse2_flags}}")
758
759         if(NOT DEFINED SUPPORT_SSE_BUILD)
760                 # result cached
761                 check_c_source_runs("
762                         #include <xmmintrin.h>
763                         int main(void) { __m128 v = _mm_setzero_ps(); return 0; }"
764                 SUPPORT_SSE_BUILD)
765
766                 if(SUPPORT_SSE_BUILD)
767                         message(STATUS "SSE Support: detected.")
768                 else()
769                         message(STATUS "SSE Support: missing.")
770                 endif()
771         endif()
772
773         if(NOT DEFINED SUPPORT_SSE2_BUILD)
774                 # result cached
775                 check_c_source_runs("
776                         #include <emmintrin.h>
777                         int main(void) { __m128d v = _mm_setzero_pd(); return 0; }"
778                 SUPPORT_SSE2_BUILD)
779
780                 if(SUPPORT_SSE2_BUILD)
781                         message(STATUS "SSE2 Support: detected.")
782                 else()
783                         message(STATUS "SSE2 Support: missing.")
784                 endif()
785         endif()
786
787         unset(CMAKE_REQUIRED_FLAGS)
788 endmacro()
789
790 # Only print message if running CMake first time
791 macro(message_first_run)
792         if(FIRST_RUN)
793                 message(${ARGV})
794         endif()
795 endmacro()
796
797 macro(TEST_UNORDERED_MAP_SUPPORT)
798         # - Detect unordered_map availability
799         # Test if a valid implementation of unordered_map exists
800         # and define the include path
801         # This module defines
802         #  HAVE_UNORDERED_MAP, whether unordered_map implementation was found
803         #
804         #  HAVE_STD_UNORDERED_MAP_HEADER, <unordered_map.h> was found
805         #  HAVE_UNORDERED_MAP_IN_STD_NAMESPACE, unordered_map is in namespace std
806         #  HAVE_UNORDERED_MAP_IN_TR1_NAMESPACE, unordered_map is in namespace std::tr1
807         #
808         #  UNORDERED_MAP_INCLUDE_PREFIX, include path prefix for unordered_map, if found
809         #  UNORDERED_MAP_NAMESPACE, namespace for unordered_map, if found
810
811         include(CheckIncludeFileCXX)
812
813         # Workaround for newer GCC (6.x+) where C++11 was enabled by default, which lead us
814         # to a situation when there is <unordered_map> include but which can't be used uless
815         # C++11 is enabled.
816         if(CMAKE_COMPILER_IS_GNUCC AND (NOT "${CMAKE_C_COMPILER_VERSION}" VERSION_LESS "6.0") AND (NOT WITH_CXX11))
817                 set(HAVE_STD_UNORDERED_MAP_HEADER False)
818         else()
819                 CHECK_INCLUDE_FILE_CXX("unordered_map" HAVE_STD_UNORDERED_MAP_HEADER)
820         endif()
821         if(HAVE_STD_UNORDERED_MAP_HEADER)
822                 # Even so we've found unordered_map header file it doesn't
823                 # mean unordered_map and unordered_set will be declared in
824                 # std namespace.
825                 #
826                 # Namely, MSVC 2008 have unordered_map header which declares
827                 # unordered_map class in std::tr1 namespace. In order to support
828                 # this, we do extra check to see which exactly namespace is
829                 # to be used.
830
831                 include(CheckCXXSourceCompiles)
832                 CHECK_CXX_SOURCE_COMPILES("#include <unordered_map>
833                                           int main() {
834                                             std::unordered_map<int, int> map;
835                                             return 0;
836                                           }"
837                                           HAVE_UNORDERED_MAP_IN_STD_NAMESPACE)
838                 if(HAVE_UNORDERED_MAP_IN_STD_NAMESPACE)
839                         message_first_run(STATUS "Found unordered_map/set in std namespace.")
840
841                         set(HAVE_UNORDERED_MAP "TRUE")
842                         set(UNORDERED_MAP_INCLUDE_PREFIX "")
843                         set(UNORDERED_MAP_NAMESPACE "std")
844                 else()
845                         CHECK_CXX_SOURCE_COMPILES("#include <unordered_map>
846                                                   int main() {
847                                                     std::tr1::unordered_map<int, int> map;
848                                                     return 0;
849                                                   }"
850                                                   HAVE_UNORDERED_MAP_IN_TR1_NAMESPACE)
851                         if(HAVE_UNORDERED_MAP_IN_TR1_NAMESPACE)
852                                 message_first_run(STATUS "Found unordered_map/set in std::tr1 namespace.")
853
854                                 set(HAVE_UNORDERED_MAP "TRUE")
855                                 set(UNORDERED_MAP_INCLUDE_PREFIX "")
856                                 set(UNORDERED_MAP_NAMESPACE "std::tr1")
857                         else()
858                                 message_first_run(STATUS "Found <unordered_map> but cannot find either std::unordered_map "
859                                                   "or std::tr1::unordered_map.")
860                         endif()
861                 endif()
862         else()
863                 CHECK_INCLUDE_FILE_CXX("tr1/unordered_map" HAVE_UNORDERED_MAP_IN_TR1_NAMESPACE)
864                 if(HAVE_UNORDERED_MAP_IN_TR1_NAMESPACE)
865                         message_first_run(STATUS "Found unordered_map/set in std::tr1 namespace.")
866
867                         set(HAVE_UNORDERED_MAP "TRUE")
868                         set(UNORDERED_MAP_INCLUDE_PREFIX "tr1")
869                         set(UNORDERED_MAP_NAMESPACE "std::tr1")
870                 else()
871                         message_first_run(STATUS "Unable to find <unordered_map> or <tr1/unordered_map>. ")
872                 endif()
873         endif()
874 endmacro()
875
876 macro(TEST_SHARED_PTR_SUPPORT)
877         # This check are coming from Ceres library.
878         #
879         # Find shared pointer header and namespace.
880         #
881         # This module defines the following variables:
882         #
883         # SHARED_PTR_FOUND: TRUE if shared_ptr found.
884         # SHARED_PTR_TR1_MEMORY_HEADER: True if <tr1/memory> header is to be used
885         # for the shared_ptr object, otherwise use <memory>.
886         # SHARED_PTR_TR1_NAMESPACE: TRUE if shared_ptr is defined in std::tr1 namespace,
887         # otherwise it's assumed to be defined in std namespace.
888
889         include(CheckIncludeFileCXX)
890         include(CheckCXXSourceCompiles)
891         set(SHARED_PTR_FOUND FALSE)
892         # Workaround for newer GCC (6.x+) where C++11 was enabled by default, which lead us
893         # to a situation when there is <unordered_map> include but which can't be used uless
894         # C++11 is enabled.
895         if(CMAKE_COMPILER_IS_GNUCC AND (NOT "${CMAKE_C_COMPILER_VERSION}" VERSION_LESS "6.0") AND (NOT WITH_CXX11))
896                 set(HAVE_STD_MEMORY_HEADER False)
897         else()
898                 CHECK_INCLUDE_FILE_CXX(memory HAVE_STD_MEMORY_HEADER)
899         endif()
900         if(HAVE_STD_MEMORY_HEADER)
901                 # Finding the memory header doesn't mean that shared_ptr is in std
902                 # namespace.
903                 #
904                 # In particular, MSVC 2008 has shared_ptr declared in std::tr1.  In
905                 # order to support this, we do an extra check to see which namespace
906                 # should be used.
907                 CHECK_CXX_SOURCE_COMPILES("#include <memory>
908                                            int main() {
909                                              std::shared_ptr<int> int_ptr;
910                                              return 0;
911                                            }"
912                                           HAVE_SHARED_PTR_IN_STD_NAMESPACE)
913
914                 if(HAVE_SHARED_PTR_IN_STD_NAMESPACE)
915                         message_first_run("-- Found shared_ptr in std namespace using <memory> header.")
916                         set(SHARED_PTR_FOUND TRUE)
917                 else()
918                         CHECK_CXX_SOURCE_COMPILES("#include <memory>
919                                                    int main() {
920                                                    std::tr1::shared_ptr<int> int_ptr;
921                                                    return 0;
922                                                    }"
923                                                   HAVE_SHARED_PTR_IN_TR1_NAMESPACE)
924                         if(HAVE_SHARED_PTR_IN_TR1_NAMESPACE)
925                                 message_first_run("-- Found shared_ptr in std::tr1 namespace using <memory> header.")
926                                 set(SHARED_PTR_TR1_NAMESPACE TRUE)
927                                 set(SHARED_PTR_FOUND TRUE)
928                         endif()
929                 endif()
930         endif()
931
932         if(NOT SHARED_PTR_FOUND)
933                 # Further, gcc defines shared_ptr in std::tr1 namespace and
934                 # <tr1/memory> is to be included for this. And what makes things
935                 # even more tricky is that gcc does have <memory> header, so
936                 # all the checks above wouldn't find shared_ptr.
937                 CHECK_INCLUDE_FILE_CXX("tr1/memory" HAVE_TR1_MEMORY_HEADER)
938                 if(HAVE_TR1_MEMORY_HEADER)
939                         CHECK_CXX_SOURCE_COMPILES("#include <tr1/memory>
940                                                    int main() {
941                                                    std::tr1::shared_ptr<int> int_ptr;
942                                                    return 0;
943                                                    }"
944                                                    HAVE_SHARED_PTR_IN_TR1_NAMESPACE_FROM_TR1_MEMORY_HEADER)
945                         if(HAVE_SHARED_PTR_IN_TR1_NAMESPACE_FROM_TR1_MEMORY_HEADER)
946                                 message_first_run("-- Found shared_ptr in std::tr1 namespace using <tr1/memory> header.")
947                                 set(SHARED_PTR_TR1_MEMORY_HEADER TRUE)
948                                 set(SHARED_PTR_TR1_NAMESPACE TRUE)
949                                 set(SHARED_PTR_FOUND TRUE)
950                         endif()
951                 endif()
952         endif()
953 endmacro()
954
955 # when we have warnings as errors applied globally this
956 # needs to be removed for some external libs which we dont maintain.
957
958 # utility macro
959 macro(remove_cc_flag
960         _flag)
961
962         foreach(flag ${ARGV})
963                 string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
964                 string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}")
965                 string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
966                 string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL}")
967                 string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO}")
968
969                 string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
970                 string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
971                 string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
972                 string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL}")
973                 string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
974         endforeach()
975         unset(flag)
976
977 endmacro()
978
979 macro(add_cc_flag
980         flag)
981
982         set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}")
983         set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}")
984 endmacro()
985
986 macro(remove_strict_flags)
987
988         if(CMAKE_COMPILER_IS_GNUCC)
989                 remove_cc_flag(
990                         "-Wstrict-prototypes"
991                         "-Wmissing-prototypes"
992                         "-Wmissing-declarations"
993                         "-Wmissing-format-attribute"
994                         "-Wunused-local-typedefs"
995                         "-Wunused-macros"
996                         "-Wunused-parameter"
997                         "-Wwrite-strings"
998                         "-Wredundant-decls"
999                         "-Wundef"
1000                         "-Wshadow"
1001                         "-Wdouble-promotion"
1002                         "-Wold-style-definition"
1003                         "-Werror=[^ ]+"
1004                         "-Werror"
1005                 )
1006
1007                 # negate flags implied by '-Wall'
1008                 add_cc_flag("${CC_REMOVE_STRICT_FLAGS}")
1009         endif()
1010
1011         if(CMAKE_C_COMPILER_ID MATCHES "Clang")
1012                 remove_cc_flag(
1013                         "-Wunused-parameter"
1014                         "-Wunused-variable"
1015                         "-Werror=[^ ]+"
1016                         "-Werror"
1017                 )
1018
1019                 # negate flags implied by '-Wall'
1020                 add_cc_flag("${CC_REMOVE_STRICT_FLAGS}")
1021         endif()
1022
1023         if(MSVC)
1024                 # TODO
1025         endif()
1026
1027 endmacro()
1028
1029 macro(remove_extra_strict_flags)
1030         if(CMAKE_COMPILER_IS_GNUCC)
1031                 remove_cc_flag(
1032                         "-Wunused-parameter"
1033                 )
1034         endif()
1035
1036         if(CMAKE_C_COMPILER_ID MATCHES "Clang")
1037                 remove_cc_flag(
1038                         "-Wunused-parameter"
1039                 )
1040         endif()
1041
1042         if(MSVC)
1043                 # TODO
1044         endif()
1045 endmacro()
1046
1047 # note, we can only append flags on a single file so we need to negate the options.
1048 # at the moment we cant shut up ffmpeg deprecations, so use this, but will
1049 # probably add more removals here.
1050 macro(remove_strict_flags_file
1051         filenames)
1052
1053         foreach(_SOURCE ${ARGV})
1054
1055                 if(CMAKE_COMPILER_IS_GNUCC OR
1056                   (CMAKE_C_COMPILER_ID MATCHES "Clang"))
1057
1058                         set_source_files_properties(${_SOURCE}
1059                                 PROPERTIES
1060                                         COMPILE_FLAGS "${CC_REMOVE_STRICT_FLAGS}"
1061                         )
1062                 endif()
1063
1064                 if(MSVC)
1065                         # TODO
1066                 endif()
1067
1068         endforeach()
1069
1070         unset(_SOURCE)
1071
1072 endmacro()
1073
1074 # External libs may need 'signed char' to be default.
1075 macro(remove_cc_flag_unsigned_char)
1076         if(CMAKE_C_COMPILER_ID MATCHES "^(GNU|Clang|Intel)$")
1077                 remove_cc_flag("-funsigned-char")
1078         elseif(MSVC)
1079                 remove_cc_flag("/J")
1080         else()
1081                 message(WARNING
1082                         "Compiler '${CMAKE_C_COMPILER_ID}' failed to disable 'unsigned char' flag."
1083                         "Build files need updating."
1084                 )
1085         endif()
1086 endmacro()
1087
1088 function(ADD_CHECK_C_COMPILER_FLAG
1089         _CFLAGS
1090         _CACHE_VAR
1091         _FLAG
1092         )
1093
1094         include(CheckCCompilerFlag)
1095
1096         CHECK_C_COMPILER_FLAG("${_FLAG}" "${_CACHE_VAR}")
1097         if(${_CACHE_VAR})
1098                 # message(STATUS "Using CFLAG: ${_FLAG}")
1099                 set(${_CFLAGS} "${${_CFLAGS}} ${_FLAG}" PARENT_SCOPE)
1100         else()
1101                 message(STATUS "Unsupported CFLAG: ${_FLAG}")
1102         endif()
1103 endfunction()
1104
1105 function(ADD_CHECK_CXX_COMPILER_FLAG
1106         _CXXFLAGS
1107         _CACHE_VAR
1108         _FLAG
1109         )
1110
1111         include(CheckCXXCompilerFlag)
1112
1113         CHECK_CXX_COMPILER_FLAG("${_FLAG}" "${_CACHE_VAR}")
1114         if(${_CACHE_VAR})
1115                 # message(STATUS "Using CXXFLAG: ${_FLAG}")
1116                 set(${_CXXFLAGS} "${${_CXXFLAGS}} ${_FLAG}" PARENT_SCOPE)
1117         else()
1118                 message(STATUS "Unsupported CXXFLAG: ${_FLAG}")
1119         endif()
1120 endfunction()
1121
1122 function(get_blender_version)
1123         # extracts header vars and defines them in the parent scope:
1124         #
1125         # - BLENDER_VERSION (major.minor)
1126         # - BLENDER_VERSION_MAJOR
1127         # - BLENDER_VERSION_MINOR
1128         # - BLENDER_SUBVERSION (used for internal versioning mainly)
1129         # - BLENDER_VERSION_CHAR (a, b, c, ...or empty string)
1130         # - BLENDER_VERSION_CYCLE (alpha, beta, rc, release)
1131
1132         # So cmake depends on BKE_blender.h, beware of inf-loops!
1133         CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/source/blender/blenkernel/BKE_blender_version.h
1134                        ${CMAKE_BINARY_DIR}/source/blender/blenkernel/BKE_blender_version.h.done)
1135
1136         file(STRINGS ${CMAKE_SOURCE_DIR}/source/blender/blenkernel/BKE_blender_version.h _contents REGEX "^#define[ \t]+BLENDER_.*$")
1137
1138         string(REGEX REPLACE ".*#define[ \t]+BLENDER_VERSION[ \t]+([0-9]+).*" "\\1" _out_version "${_contents}")
1139         string(REGEX REPLACE ".*#define[ \t]+BLENDER_SUBVERSION[ \t]+([0-9]+).*" "\\1" _out_subversion "${_contents}")
1140         string(REGEX REPLACE ".*#define[ \t]+BLENDER_VERSION_CHAR[ \t]+([a-z]+).*" "\\1" _out_version_char "${_contents}")
1141         string(REGEX REPLACE ".*#define[ \t]+BLENDER_VERSION_CYCLE[ \t]+([a-z]+).*" "\\1" _out_version_cycle "${_contents}")
1142
1143         if(NOT ${_out_version} MATCHES "[0-9]+")
1144                 message(FATAL_ERROR "Version parsing failed for BLENDER_VERSION")
1145         endif()
1146
1147         if(NOT ${_out_subversion} MATCHES "[0-9]+")
1148                 message(FATAL_ERROR "Version parsing failed for BLENDER_SUBVERSION")
1149         endif()
1150
1151         # clumsy regex, only single char are ok but it could be unset
1152
1153         string(LENGTH "${_out_version_char}" _out_version_char_len)
1154         if(NOT _out_version_char_len EQUAL 1)
1155                 set(_out_version_char "")
1156         elseif(NOT ${_out_version_char} MATCHES "[a-z]+")
1157                 message(FATAL_ERROR "Version parsing failed for BLENDER_VERSION_CHAR")
1158         endif()
1159
1160         if(NOT ${_out_version_cycle} MATCHES "[a-z]+")
1161                 message(FATAL_ERROR "Version parsing failed for BLENDER_VERSION_CYCLE")
1162         endif()
1163
1164         math(EXPR _out_version_major "${_out_version} / 100")
1165         math(EXPR _out_version_minor "${_out_version} % 100")
1166
1167         # for packaging, alpha to numbers
1168         string(COMPARE EQUAL "${_out_version_char}" "" _out_version_char_empty)
1169         if(${_out_version_char_empty})
1170                 set(_out_version_char_index "0")
1171         else()
1172                 set(_char_ls a b c d e f g h i j k l m n o p q r s t u v w x y z)
1173                 list(FIND _char_ls ${_out_version_char} _out_version_char_index)
1174                 math(EXPR _out_version_char_index "${_out_version_char_index} + 1")
1175         endif()
1176
1177         # output vars
1178         set(BLENDER_VERSION "${_out_version_major}.${_out_version_minor}" PARENT_SCOPE)
1179         set(BLENDER_VERSION_MAJOR "${_out_version_major}" PARENT_SCOPE)
1180         set(BLENDER_VERSION_MINOR "${_out_version_minor}" PARENT_SCOPE)
1181         set(BLENDER_SUBVERSION "${_out_subversion}" PARENT_SCOPE)
1182         set(BLENDER_VERSION_CHAR "${_out_version_char}" PARENT_SCOPE)
1183         set(BLENDER_VERSION_CHAR_INDEX "${_out_version_char_index}" PARENT_SCOPE)
1184         set(BLENDER_VERSION_CYCLE "${_out_version_cycle}" PARENT_SCOPE)
1185
1186 endfunction()
1187
1188
1189 # hacks to override initial project settings
1190 # these macros must be called directly before/after project(Blender)
1191 macro(blender_project_hack_pre)
1192         # ----------------
1193         # MINGW HACK START
1194         # ignore system set flag, use our own
1195         # must be before project(...)
1196         # if the user wants to add their own its ok after first run.
1197         if(DEFINED CMAKE_C_STANDARD_LIBRARIES)
1198                 set(_reset_standard_libraries OFF)
1199         else()
1200                 set(_reset_standard_libraries ON)
1201         endif()
1202
1203         # ------------------
1204         # GCC -O3 HACK START
1205         # needed because O3 can cause problems but
1206         # allow the builder to set O3 manually after.
1207         if(DEFINED CMAKE_C_FLAGS_RELEASE)
1208                 set(_reset_standard_cflags_rel OFF)
1209         else()
1210                 set(_reset_standard_cflags_rel ON)
1211         endif()
1212         if(DEFINED CMAKE_CXX_FLAGS_RELEASE)
1213                 set(_reset_standard_cxxflags_rel OFF)
1214         else()
1215                 set(_reset_standard_cxxflags_rel ON)
1216         endif()
1217 endmacro()
1218
1219
1220 macro(blender_project_hack_post)
1221         # --------------
1222         # MINGW HACK END
1223         if(_reset_standard_libraries)
1224                 # Must come after projecINCt(...)
1225                 #
1226                 # MINGW workaround for -ladvapi32 being included which surprisingly causes
1227                 # string formatting of floats, eg: printf("%.*f", 3, value). to crash blender
1228                 # with a meaningless stack trace. by overriding this flag we ensure we only
1229                 # have libs we define.
1230                 set(CMAKE_C_STANDARD_LIBRARIES "" CACHE STRING "" FORCE)
1231                 set(CMAKE_CXX_STANDARD_LIBRARIES "" CACHE STRING "" FORCE)
1232                 mark_as_advanced(
1233                         CMAKE_C_STANDARD_LIBRARIES
1234                         CMAKE_CXX_STANDARD_LIBRARIES
1235                 )
1236         endif()
1237         unset(_reset_standard_libraries)
1238
1239
1240         # ----------------
1241         # GCC -O3 HACK END
1242         if(_reset_standard_cflags_rel)
1243                 string(REGEX REPLACE "-O3" "-O2" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
1244                 set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}" CACHE STRING "" FORCE)
1245                 mark_as_advanced(CMAKE_C_FLAGS_RELEASE)
1246         endif()
1247
1248         if(_reset_standard_cxxflags_rel)
1249                 string(REGEX REPLACE "-O3" "-O2" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
1250                 set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}" CACHE STRING "" FORCE)
1251                 mark_as_advanced(CMAKE_CXX_FLAGS_RELEASE)
1252         endif()
1253
1254         unset(_reset_standard_cflags_rel)
1255         unset(_reset_standard_cxxflags_rel)
1256
1257         # ------------------------------------------------------------------
1258         # workaround for omission in cmake 2.8.4's GNU.cmake, fixed in 2.8.5
1259         if(CMAKE_COMPILER_IS_GNUCC)
1260                 if(NOT DARWIN)
1261                         set(CMAKE_INCLUDE_SYSTEM_FLAG_C "-isystem ")
1262                 endif()
1263         endif()
1264
1265 endmacro()
1266
1267 # pair of macros to allow libraries to be specify files to install, but to
1268 # only install them at the end so the directories don't get cleared with
1269 # the files in them. used by cycles to install addon.
1270 function(delayed_install
1271         base
1272         files
1273         destination)
1274
1275         foreach(f ${files})
1276                 if(IS_ABSOLUTE ${f})
1277                         set_property(GLOBAL APPEND PROPERTY DELAYED_INSTALL_FILES ${f})
1278                 else()
1279                         set_property(GLOBAL APPEND PROPERTY DELAYED_INSTALL_FILES ${base}/${f})
1280                 endif()
1281                 set_property(GLOBAL APPEND PROPERTY DELAYED_INSTALL_DESTINATIONS ${destination})
1282         endforeach()
1283 endfunction()
1284
1285 # note this is a function instead of a macro so that ${BUILD_TYPE} in targetdir
1286 # does not get expanded in calling but is preserved
1287 function(delayed_do_install
1288         targetdir)
1289
1290         get_property(files GLOBAL PROPERTY DELAYED_INSTALL_FILES)
1291         get_property(destinations GLOBAL PROPERTY DELAYED_INSTALL_DESTINATIONS)
1292
1293         if(files)
1294                 list(LENGTH files n)
1295                 math(EXPR n "${n}-1")
1296
1297                 foreach(i RANGE ${n})
1298                         list(GET files ${i} f)
1299                         list(GET destinations ${i} d)
1300                         install(FILES ${f} DESTINATION ${targetdir}/${d})
1301                 endforeach()
1302         endif()
1303 endfunction()
1304
1305
1306 function(data_to_c
1307         file_from file_to
1308         list_to_add
1309         )
1310
1311         list(APPEND ${list_to_add} ${file_to})
1312         set(${list_to_add} ${${list_to_add}} PARENT_SCOPE)
1313
1314         get_filename_component(_file_to_path ${file_to} PATH)
1315
1316         add_custom_command(
1317                 OUTPUT ${file_to}
1318                 COMMAND ${CMAKE_COMMAND} -E make_directory ${_file_to_path}
1319                 COMMAND "$<TARGET_FILE:datatoc>" ${file_from} ${file_to}
1320                 DEPENDS ${file_from} datatoc)
1321
1322         set_source_files_properties(${file_to} PROPERTIES GENERATED TRUE)
1323 endfunction()
1324
1325
1326 # same as above but generates the var name and output automatic.
1327 function(data_to_c_simple
1328         file_from
1329         list_to_add
1330         )
1331
1332         # remove ../'s
1333         get_filename_component(_file_from ${CMAKE_CURRENT_SOURCE_DIR}/${file_from}   REALPATH)
1334         get_filename_component(_file_to   ${CMAKE_CURRENT_BINARY_DIR}/${file_from}.c REALPATH)
1335
1336         list(APPEND ${list_to_add} ${_file_to})
1337         set(${list_to_add} ${${list_to_add}} PARENT_SCOPE)
1338
1339         get_filename_component(_file_to_path ${_file_to} PATH)
1340
1341         add_custom_command(
1342                 OUTPUT  ${_file_to}
1343                 COMMAND ${CMAKE_COMMAND} -E make_directory ${_file_to_path}
1344                 COMMAND "$<TARGET_FILE:datatoc>" ${_file_from} ${_file_to}
1345                 DEPENDS ${_file_from} datatoc)
1346
1347         set_source_files_properties(${_file_to} PROPERTIES GENERATED TRUE)
1348 endfunction()
1349
1350 # macro for converting pixmap directory to a png and then a c file
1351 function(data_to_c_simple_icons
1352         path_from
1353         list_to_add
1354         )
1355
1356         # Conversion steps
1357         #  path_from  ->  _file_from  ->  _file_to
1358         #  foo/*.dat  ->  foo.png     ->  foo.png.c
1359
1360         get_filename_component(_path_from_abs ${path_from} ABSOLUTE)
1361         # remove ../'s
1362         get_filename_component(_file_from ${CMAKE_CURRENT_BINARY_DIR}/${path_from}.png   REALPATH)
1363         get_filename_component(_file_to   ${CMAKE_CURRENT_BINARY_DIR}/${path_from}.png.c REALPATH)
1364
1365         list(APPEND ${list_to_add} ${_file_to})
1366         set(${list_to_add} ${${list_to_add}} PARENT_SCOPE)
1367
1368         get_filename_component(_file_to_path ${_file_to} PATH)
1369
1370         # ideally we wouldn't glob, but storing all names for all pixmaps is a bit heavy
1371         file(GLOB _icon_files "${path_from}/*.dat")
1372
1373         add_custom_command(
1374                 OUTPUT  ${_file_from} ${_file_to}
1375                 COMMAND ${CMAKE_COMMAND} -E make_directory ${_file_to_path}
1376                 #COMMAND python3 ${CMAKE_SOURCE_DIR}/source/blender/datatoc/datatoc_icon.py ${_path_from_abs} ${_file_from}
1377                 COMMAND "$<TARGET_FILE:datatoc_icon>" ${_path_from_abs} ${_file_from}
1378                 COMMAND "$<TARGET_FILE:datatoc>" ${_file_from} ${_file_to}
1379                 DEPENDS
1380                         ${_icon_files}
1381                         datatoc_icon
1382                         datatoc
1383                         # could be an arg but for now we only create icons depending on UI_icons.h
1384                         ${CMAKE_SOURCE_DIR}/source/blender/editors/include/UI_icons.h
1385                 )
1386
1387         set_source_files_properties(${_file_from} ${_file_to} PROPERTIES GENERATED TRUE)
1388 endfunction()
1389
1390 # XXX Not used for now...
1391 function(svg_to_png
1392         file_from
1393         file_to
1394         dpi
1395         list_to_add
1396         )
1397
1398         # remove ../'s
1399         get_filename_component(_file_from ${CMAKE_CURRENT_SOURCE_DIR}/${file_from} REALPATH)
1400         get_filename_component(_file_to   ${CMAKE_CURRENT_SOURCE_DIR}/${file_to}   REALPATH)
1401
1402         list(APPEND ${list_to_add} ${_file_to})
1403         set(${list_to_add} ${${list_to_add}} PARENT_SCOPE)
1404
1405         find_program(INKSCAPE_EXE inkscape)
1406         mark_as_advanced(INKSCAPE_EXE)
1407
1408         if(INKSCAPE_EXE)
1409                 if(APPLE)
1410                         # in OS X app bundle, the binary is a shim that doesn't take any
1411                         # command line arguments, replace it with the actual binary
1412                         string(REPLACE "MacOS/Inkscape" "Resources/bin/inkscape" INKSCAPE_REAL_EXE ${INKSCAPE_EXE})
1413                         if(EXISTS "${INKSCAPE_REAL_EXE}")
1414                                 set(INKSCAPE_EXE ${INKSCAPE_REAL_EXE})
1415                         endif()
1416                 endif()
1417
1418                 add_custom_command(
1419                         OUTPUT  ${_file_to}
1420                         COMMAND ${INKSCAPE_EXE} ${_file_from} --export-dpi=${dpi}  --without-gui --export-png=${_file_to}
1421                         DEPENDS ${_file_from} ${INKSCAPE_EXE}
1422                 )
1423         else()
1424                 message(WARNING "Inkscape not found, could not re-generate ${_file_to} from ${_file_from}!")
1425         endif()
1426 endfunction()
1427
1428 function(msgfmt_simple
1429         file_from
1430         list_to_add
1431         )
1432
1433         # remove ../'s
1434         get_filename_component(_file_from_we ${file_from} NAME_WE)
1435
1436         get_filename_component(_file_from ${file_from} REALPATH)
1437         get_filename_component(_file_to ${CMAKE_CURRENT_BINARY_DIR}/${_file_from_we}.mo REALPATH)
1438
1439         list(APPEND ${list_to_add} ${_file_to})
1440         set(${list_to_add} ${${list_to_add}} PARENT_SCOPE)
1441
1442         get_filename_component(_file_to_path ${_file_to} PATH)
1443
1444         add_custom_command(
1445                 OUTPUT  ${_file_to}
1446                 COMMAND ${CMAKE_COMMAND} -E make_directory ${_file_to_path}
1447                 COMMAND "$<TARGET_FILE:msgfmt>" ${_file_from} ${_file_to}
1448                 DEPENDS msgfmt ${_file_from})
1449
1450         set_source_files_properties(${_file_to} PROPERTIES GENERATED TRUE)
1451 endfunction()
1452
1453 function(find_python_package
1454         package
1455         )
1456
1457         string(TOUPPER ${package} _upper_package)
1458
1459         # set but invalid
1460         if((NOT ${PYTHON_${_upper_package}_PATH} STREQUAL "") AND
1461            (NOT ${PYTHON_${_upper_package}_PATH} MATCHES NOTFOUND))
1462 #               if(NOT EXISTS "${PYTHON_${_upper_package}_PATH}/${package}")
1463 #                       message(WARNING "PYTHON_${_upper_package}_PATH is invalid, ${package} not found in '${PYTHON_${_upper_package}_PATH}' "
1464 #                                       "WITH_PYTHON_INSTALL_${_upper_package} option will be ignored when installing python")
1465 #                       set(WITH_PYTHON_INSTALL${_upper_package} OFF)
1466 #               endif()
1467         # not set, so initialize
1468         else()
1469                 string(REPLACE "." ";" _PY_VER_SPLIT "${PYTHON_VERSION}")
1470                 list(GET _PY_VER_SPLIT 0 _PY_VER_MAJOR)
1471
1472                 # re-cache
1473                 unset(PYTHON_${_upper_package}_PATH CACHE)
1474                 find_path(PYTHON_${_upper_package}_PATH
1475                   NAMES
1476                     ${package}
1477                   HINTS
1478                     "${PYTHON_LIBPATH}/python${PYTHON_VERSION}/"
1479                     "${PYTHON_LIBPATH}/python${_PY_VER_MAJOR}/"
1480                   PATH_SUFFIXES
1481                     site-packages
1482                     dist-packages
1483                    NO_DEFAULT_PATH
1484                 )
1485
1486                  if(NOT EXISTS "${PYTHON_${_upper_package}_PATH}")
1487                         message(WARNING
1488                                 "Python package '${package}' path could not be found in:\n"
1489                                 "'${PYTHON_LIBPATH}/python${PYTHON_VERSION}/site-packages/${package}', "
1490                                 "'${PYTHON_LIBPATH}/python${_PY_VER_MAJOR}/site-packages/${package}', "
1491                                 "'${PYTHON_LIBPATH}/python${PYTHON_VERSION}/dist-packages/${package}', "
1492                                 "'${PYTHON_LIBPATH}/python${_PY_VER_MAJOR}/dist-packages/${package}', "
1493                                 "\n"
1494                                 "The 'WITH_PYTHON_INSTALL_${_upper_package}' option will be ignored when installing Python.\n"
1495                                 "The build will be usable, only add-ons that depend on this package won't be functional."
1496                         )
1497                         set(WITH_PYTHON_INSTALL_${_upper_package} OFF PARENT_SCOPE)
1498                 else()
1499                         message(STATUS "${package} found at '${PYTHON_${_upper_package}_PATH}'")
1500                 endif()
1501         endif()
1502 endfunction()
1503
1504 # like Python's 'print(dir())'
1505 function(print_all_vars)
1506         get_cmake_property(_vars VARIABLES)
1507         foreach(_var ${_vars})
1508                 message("${_var}=${${_var}}")
1509         endforeach()
1510 endfunction()