CMakeLists.txt 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #
  2. # This CMake project downloads, configures and builds Slic3r++ dependencies on Unix and Windows.
  3. #
  4. # When using this script, it's recommended to perform an out-of-source build using CMake.
  5. #
  6. # All the dependencies are installed in a `destdir` directory in the root of the build directory,
  7. # in a traditional Unix-style prefix structure. The destdir can be used directly by CMake
  8. # when building Slic3r - to do this, set the CMAKE_PREFIX_PATH to ${destdir}/usr/local.
  9. # Warning: On UNIX/Linux, you also need to set -DSlic3r_STATIC=1 when building Slic3r.
  10. #
  11. # For better clarity of console output, it's recommended to _not_ use a parallelized build
  12. # for the top-level command, ie. use `make -j 1` or `ninja -j 1` to force single-threaded top-level
  13. # build. This doesn't degrade performance as individual dependencies are built in parallel fashion
  14. # if supported by the dependency.
  15. #
  16. # On Windows, architecture (64 vs 32 bits) is judged based on the compiler variant.
  17. # To build dependencies for either 64 or 32 bit OS, use the respective compiler command line.
  18. #
  19. # WARNING: On UNIX platforms wxWidgets hardcode the destdir path into its `wx-conffig` utility,
  20. # therefore, unfortunatelly, the installation cannot be copied/moved elsewhere without re-installing wxWidgets.
  21. #
  22. project(Slic3r-deps)
  23. cmake_minimum_required(VERSION 3.2)
  24. include(ExternalProject)
  25. include(ProcessorCount)
  26. ProcessorCount(NPROC)
  27. if (NPROC EQUAL 0)
  28. set(NPROC 1)
  29. endif ()
  30. set(DESTDIR "${CMAKE_CURRENT_BINARY_DIR}/destdir" CACHE PATH "Destination directory")
  31. option(DEP_DEBUG "Build debug variants (only applicable on Windows)" ON)
  32. if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
  33. option(DEP_WX_GTK3 "Build wxWidgets against GTK3" OFF)
  34. endif()
  35. set(IS_CROSS_COMPILE FALSE)
  36. if (APPLE)
  37. set(CMAKE_FIND_FRAMEWORK LAST)
  38. set(CMAKE_FIND_APPBUNDLE LAST)
  39. list(FIND CMAKE_OSX_ARCHITECTURES ${CMAKE_SYSTEM_PROCESSOR} _arch_idx)
  40. if (CMAKE_OSX_ARCHITECTURES AND _arch_idx LESS 0)
  41. set(IS_CROSS_COMPILE TRUE)
  42. string(REPLACE ";" "$<SEMICOLON>" CMAKE_OSX_ARCHS "${CMAKE_OSX_ARCHITECTURES}")
  43. set(_cmake_osx_arch -DCMAKE_OSX_ARCHITECTURES:STRING=${CMAKE_OSX_ARCHS})
  44. set(_cmake_args_osx_arch CMAKE_ARGS -DCMAKE_OSX_ARCHITECTURES:STRING=${CMAKE_OSX_ARCHS})
  45. message("Detect Cross-compilation. Will build for target ${CMAKE_OSX_ARCHS}" )
  46. endif ()
  47. endif ()
  48. # On developer machines, it can be enabled to speed up compilation and suppress warnings coming from IGL.
  49. # FIXME:
  50. # Enabling this option is not safe. IGL will compile itself with its own version of Eigen while
  51. # Slic3r compiles with a different version which will cause runtime errors.
  52. # option(DEP_BUILD_IGL_STATIC "Build IGL as a static library. Might cause link errors and increase binary size." OFF)
  53. message(STATUS "Slic3r deps DESTDIR: ${DESTDIR}")
  54. message(STATUS "Slic3r deps debug build: ${DEP_DEBUG}")
  55. find_package(Git REQUIRED)
  56. get_property(_is_multi GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
  57. function(prusaslicer_add_cmake_project projectname)
  58. cmake_parse_arguments(P_ARGS "" "INSTALL_DIR;BUILD_COMMAND;INSTALL_COMMAND" "CMAKE_ARGS" ${ARGN})
  59. set(_configs_line -DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE})
  60. if (_is_multi OR MSVC)
  61. set(_configs_line "")
  62. endif ()
  63. set(_gen "")
  64. set(_build_j "-j${NPROC}")
  65. if (MSVC)
  66. set(_gen CMAKE_GENERATOR "${DEP_MSVC_GEN}" CMAKE_GENERATOR_PLATFORM "${DEP_PLATFORM}")
  67. set(_build_j "/m")
  68. endif ()
  69. ExternalProject_Add(
  70. dep_${projectname}
  71. EXCLUDE_FROM_ALL ON
  72. INSTALL_DIR ${DESTDIR}/usr/local
  73. ${_gen}
  74. CMAKE_ARGS
  75. -DCMAKE_INSTALL_PREFIX:STRING=${DESTDIR}/usr/local
  76. -DCMAKE_MODULE_PATH:STRING=${PROJECT_SOURCE_DIR}/../cmake/modules
  77. -DCMAKE_PREFIX_PATH:STRING=${DESTDIR}/usr/local
  78. -DCMAKE_DEBUG_POSTFIX:STRING=d
  79. -DCMAKE_C_COMPILER:STRING=${CMAKE_C_COMPILER}
  80. -DCMAKE_CXX_COMPILER:STRING=${CMAKE_CXX_COMPILER}
  81. -DBUILD_SHARED_LIBS:BOOL=OFF
  82. ${_cmake_osx_arch}
  83. "${_configs_line}"
  84. ${DEP_CMAKE_OPTS}
  85. ${P_ARGS_CMAKE_ARGS}
  86. ${P_ARGS_UNPARSED_ARGUMENTS}
  87. BUILD_COMMAND ${CMAKE_COMMAND} --build . --config Release -- ${_build_j}
  88. INSTALL_COMMAND ${CMAKE_COMMAND} --build . --target install --config Release
  89. )
  90. endfunction(prusaslicer_add_cmake_project)
  91. if (MSVC)
  92. if ("${CMAKE_SIZEOF_VOID_P}" STREQUAL "8")
  93. message(STATUS "\nDetected 64-bit compiler => building 64-bit deps bundle\n")
  94. set(DEPS_BITS 64)
  95. include("deps-windows.cmake")
  96. elseif ("${CMAKE_SIZEOF_VOID_P}" STREQUAL "4")
  97. message(STATUS "\nDetected 32-bit compiler => building 32-bit deps bundle\n")
  98. set(DEPS_BITS 32)
  99. include("deps-windows.cmake")
  100. else ()
  101. message(FATAL_ERROR "Unable to detect architecture")
  102. endif ()
  103. elseif (APPLE)
  104. message("OS X SDK Path: ${CMAKE_OSX_SYSROOT}")
  105. if (CMAKE_OSX_DEPLOYMENT_TARGET)
  106. set(DEP_OSX_TARGET "${CMAKE_OSX_DEPLOYMENT_TARGET}")
  107. message("OS X Deployment Target: ${DEP_OSX_TARGET}")
  108. else ()
  109. # Attempt to infer the SDK version from the CMAKE_OSX_SYSROOT,
  110. # this is done because wxWidgets need the min version explicitly set
  111. string(REGEX MATCH "[0-9]+[.][0-9]+[.]sdk$" DEP_OSX_TARGET "${CMAKE_OSX_SYSROOT}")
  112. string(REGEX MATCH "^[0-9]+[.][0-9]+" DEP_OSX_TARGET "${DEP_OSX_TARGET}")
  113. if (NOT DEP_OSX_TARGET)
  114. message(FATAL_ERROR "Could not determine OS X SDK version. Please use -DCMAKE_OSX_DEPLOYMENT_TARGET=<version>")
  115. endif ()
  116. message("OS X Deployment Target (inferred from SDK): ${DEP_OSX_TARGET}")
  117. endif ()
  118. include("deps-macos.cmake")
  119. elseif (MINGW)
  120. message(STATUS "Building for MinGW...")
  121. include("deps-mingw.cmake")
  122. else()
  123. include("deps-linux.cmake")
  124. endif()
  125. set(ZLIB_PKG "")
  126. if (NOT ZLIB_FOUND)
  127. include(ZLIB/ZLIB.cmake)
  128. set(ZLIB_PKG dep_ZLIB)
  129. endif ()
  130. set(PNG_PKG "")
  131. if (NOT PNG_FOUND)
  132. include(PNG/PNG.cmake)
  133. set(PNG_PKG dep_PNG)
  134. endif ()
  135. set(EXPAT_PKG "")
  136. if (NOT EXPAT_FOUND)
  137. include(EXPAT/EXPAT.cmake)
  138. set(EXPAT_PKG dep_EXPAT)
  139. endif ()
  140. include(GLEW/GLEW.cmake)
  141. include(OpenCSG/OpenCSG.cmake)
  142. include(GMP/GMP.cmake)
  143. include(MPFR/MPFR.cmake)
  144. include(CGAL/CGAL.cmake)
  145. include(wxWidgets/wxWidgets.cmake)
  146. if (NOT "${ZLIB_PKG}" STREQUAL "")
  147. add_dependencies(dep_blosc ${ZLIB_PKG})
  148. add_dependencies(dep_openexr ${ZLIB_PKG})
  149. endif ()
  150. set(_dep_list
  151. dep_boost
  152. dep_tbb
  153. dep_libcurl
  154. dep_wxWidgets
  155. dep_gtest
  156. dep_cereal
  157. dep_nlopt
  158. dep_openvdb
  159. dep_OpenCSG
  160. dep_CGAL
  161. ${PNG_PKG}
  162. ${ZLIB_PKG}
  163. ${EXPAT_PKG}
  164. )
  165. if ("${CMAKE_SIZEOF_VOID_P}" STREQUAL "8")
  166. # Patch the boost::polygon library with a custom one.
  167. ExternalProject_Add(dep_boost_polygon
  168. EXCLUDE_FROM_ALL ON
  169. GIT_REPOSITORY "https://github.com/prusa3d/polygon"
  170. GIT_TAG prusaslicer_gmp
  171. DEPENDS dep_boost
  172. CONFIGURE_COMMAND ""
  173. BUILD_COMMAND ""
  174. ${_cmake_args_osx_arch}
  175. INSTALL_COMMAND ${CMAKE_COMMAND} -E copy_directory
  176. "${CMAKE_CURRENT_BINARY_DIR}/dep_boost_polygon-prefix/src/dep_boost_polygon/include/boost/polygon"
  177. "${DESTDIR}/usr/local/include/boost/polygon"
  178. )
  179. # Only override boost::Polygon Voronoi implementation with Vojtech's GMP hacks on 64bit platforms.
  180. list(APPEND _dep_list "dep_boost_polygon")
  181. endif ()
  182. if (MSVC)
  183. # Experimental
  184. #list(APPEND _dep_list "dep_qhull")
  185. else()
  186. list(APPEND _dep_list "dep_qhull")
  187. # Not working, static build has different Eigen
  188. #list(APPEND _dep_list "dep_libigl")
  189. endif()
  190. add_custom_target(deps ALL DEPENDS ${_dep_list})
  191. # Note: I'm not using any of the LOG_xxx options in ExternalProject_Add() commands
  192. # because they seem to generate bogus build files (possibly a bug in ExternalProject).