CMakeLists.txt 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. set(DEP_DOWNLOAD_DIR ${CMAKE_CURRENT_BINARY_DIR} CACHE PATH "Path for downloaded source packages.")
  32. option(DEP_DEBUG "Build debug variants (only applicable on Windows)" ON)
  33. if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
  34. option(DEP_WX_GTK3 "Build wxWidgets against GTK3" OFF)
  35. endif()
  36. set(IS_CROSS_COMPILE FALSE)
  37. if (APPLE)
  38. set(CMAKE_FIND_FRAMEWORK LAST)
  39. set(CMAKE_FIND_APPBUNDLE LAST)
  40. list(FIND CMAKE_OSX_ARCHITECTURES ${CMAKE_SYSTEM_PROCESSOR} _arch_idx)
  41. message(STATUS "prusaslicer_add_cmake_project for Apple")
  42. if (CMAKE_OSX_ARCHITECTURES AND _arch_idx LESS 0)
  43. message(STATUS "prusaslicer_add_cmake_project for Apple crosscompiling")
  44. set(IS_CROSS_COMPILE TRUE)
  45. set(CMAKE_CXX_COMPILER_ID "Clang")
  46. string(REPLACE ";" "$<SEMICOLON>" CMAKE_OSX_ARCHS "${CMAKE_OSX_ARCHITECTURES}")
  47. set(_cmake_osx_arch -DCMAKE_OSX_ARCHITECTURES:STRING=${CMAKE_OSX_ARCHS})
  48. set(_cmake_args_osx_arch CMAKE_ARGS -DCMAKE_OSX_ARCHITECTURES:STRING=${CMAKE_OSX_ARCHS})
  49. message(STATUS "Detect Cross-compilation. Will build for target ${CMAKE_OSX_ARCHS}" )
  50. endif ()
  51. endif ()
  52. # On developer machines, it can be enabled to speed up compilation and suppress warnings coming from IGL.
  53. # FIXME:
  54. # Enabling this option is not safe. IGL will compile itself with its own version of Eigen while
  55. # Slic3r compiles with a different version which will cause runtime errors.
  56. # option(DEP_BUILD_IGL_STATIC "Build IGL as a static library. Might cause link errors and increase binary size." OFF)
  57. message(STATUS "Slic3r deps DESTDIR: ${DESTDIR}")
  58. message(STATUS "Slic3r dowload dir for source packages: ${DEP_DOWNLOAD_DIR}")
  59. message(STATUS "Slic3r deps debug build: ${DEP_DEBUG}")
  60. find_package(Git REQUIRED)
  61. # The default command line for patching. Only works for newer
  62. set(PATCH_CMD ${GIT_EXECUTABLE} apply --verbose --ignore-space-change --whitespace=fix)
  63. get_property(_is_multi GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
  64. if (NOT _is_multi AND NOT CMAKE_BUILD_TYPE)
  65. set(CMAKE_BUILD_TYPE Release)
  66. message(STATUS "Forcing CMAKE_BUILD_TYPE to Release as it was not specified.")
  67. endif ()
  68. function(prusaslicer_add_cmake_project projectname)
  69. cmake_parse_arguments(P_ARGS "" "INSTALL_DIR;BUILD_COMMAND;INSTALL_COMMAND" "CMAKE_ARGS" ${ARGN})
  70. set(_configs_line -DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE})
  71. if (_is_multi OR MSVC)
  72. set(_configs_line "")
  73. endif ()
  74. set(_gen "")
  75. set(_build_j "-j${NPROC}")
  76. if (MSVC)
  77. set(_gen CMAKE_GENERATOR "${DEP_MSVC_GEN}" CMAKE_GENERATOR_PLATFORM "${DEP_PLATFORM}")
  78. set(_build_j "/m")
  79. if (${projectname} STREQUAL "OCCT")
  80. set(_build_j "/m:1")
  81. endif ()
  82. endif ()
  83. if (NOT IS_CROSS_COMPILE OR NOT APPLE)
  84. ExternalProject_Add(
  85. dep_${projectname}
  86. EXCLUDE_FROM_ALL ON
  87. INSTALL_DIR ${DESTDIR}/usr/local
  88. DOWNLOAD_DIR ${DEP_DOWNLOAD_DIR}/${projectname}
  89. ${_gen}
  90. CMAKE_ARGS
  91. -DCMAKE_INSTALL_PREFIX:STRING=${DESTDIR}/usr/local
  92. -DCMAKE_MODULE_PATH:STRING=${PROJECT_SOURCE_DIR}/../cmake/modules
  93. -DCMAKE_PREFIX_PATH:STRING=${DESTDIR}/usr/local
  94. -DCMAKE_DEBUG_POSTFIX:STRING=d
  95. -DCMAKE_C_COMPILER:STRING=${CMAKE_C_COMPILER}
  96. -DCMAKE_CXX_COMPILER:STRING=${CMAKE_CXX_COMPILER}
  97. -DCMAKE_TOOLCHAIN_FILE:STRING=${CMAKE_TOOLCHAIN_FILE}
  98. -DBUILD_SHARED_LIBS:BOOL=OFF
  99. ${_cmake_osx_arch}
  100. "${_configs_line}"
  101. ${DEP_CMAKE_OPTS}
  102. ${P_ARGS_CMAKE_ARGS}
  103. ${P_ARGS_UNPARSED_ARGUMENTS}
  104. BUILD_COMMAND ${CMAKE_COMMAND} --build . --config Release -- ${_build_j}
  105. INSTALL_COMMAND ${CMAKE_COMMAND} --build . --target install --config Release
  106. )
  107. else()
  108. ExternalProject_Add(
  109. dep_${projectname}
  110. EXCLUDE_FROM_ALL ON
  111. INSTALL_DIR ${DESTDIR}/usr/local
  112. DOWNLOAD_DIR ${DEP_DOWNLOAD_DIR}/${projectname}
  113. ${_gen}
  114. CMAKE_ARGS
  115. -DCMAKE_INSTALL_PREFIX:STRING=${DESTDIR}/usr/local
  116. -DBUILD_SHARED_LIBS:BOOL=OFF
  117. ${_cmake_osx_arch}
  118. "${_configs_line}"
  119. ${DEP_CMAKE_OPTS}
  120. ${P_ARGS_CMAKE_ARGS}
  121. ${P_ARGS_UNPARSED_ARGUMENTS}
  122. BUILD_COMMAND ${CMAKE_COMMAND} --build . --config Release -- ${_build_j}
  123. INSTALL_COMMAND ${CMAKE_COMMAND} --build . --target install --config Release
  124. )
  125. endif()
  126. endfunction(prusaslicer_add_cmake_project)
  127. if (MSVC)
  128. if ("${CMAKE_SIZEOF_VOID_P}" STREQUAL "8")
  129. message(STATUS "\nDetected 64-bit compiler => building 64-bit deps bundle\n")
  130. set(DEPS_BITS 64)
  131. include("deps-windows.cmake")
  132. elseif ("${CMAKE_SIZEOF_VOID_P}" STREQUAL "4")
  133. message(STATUS "\nDetected 32-bit compiler => building 32-bit deps bundle\n")
  134. set(DEPS_BITS 32)
  135. include("deps-windows.cmake")
  136. else ()
  137. message(FATAL_ERROR "Unable to detect architecture")
  138. endif ()
  139. elseif (APPLE)
  140. message("OS X SDK Path: ${CMAKE_OSX_SYSROOT}")
  141. if (CMAKE_OSX_DEPLOYMENT_TARGET)
  142. set(DEP_OSX_TARGET "${CMAKE_OSX_DEPLOYMENT_TARGET}")
  143. message("OS X Deployment Target: ${DEP_OSX_TARGET}")
  144. else ()
  145. # Attempt to infer the SDK version from the CMAKE_OSX_SYSROOT,
  146. # this is done because wxWidgets need the min version explicitly set
  147. string(REGEX MATCH "[0-9]+[.][0-9]+[.]sdk$" DEP_OSX_TARGET "${CMAKE_OSX_SYSROOT}")
  148. string(REGEX MATCH "^[0-9]+[.][0-9]+" DEP_OSX_TARGET "${DEP_OSX_TARGET}")
  149. if (NOT DEP_OSX_TARGET)
  150. message(FATAL_ERROR "Could not determine OS X SDK version. Please use -DCMAKE_OSX_DEPLOYMENT_TARGET=<version>")
  151. endif ()
  152. message("OS X Deployment Target (inferred from SDK): ${DEP_OSX_TARGET}")
  153. endif ()
  154. include("deps-macos.cmake")
  155. elseif (MINGW)
  156. message(STATUS "Building for MinGW...")
  157. include("deps-mingw.cmake")
  158. else()
  159. include("deps-linux.cmake")
  160. endif()
  161. set(ZLIB_PKG "")
  162. if (NOT ZLIB_FOUND)
  163. include(ZLIB/ZLIB.cmake)
  164. set(ZLIB_PKG dep_ZLIB)
  165. endif ()
  166. set(PNG_PKG "")
  167. if (NOT PNG_FOUND)
  168. include(PNG/PNG.cmake)
  169. set(PNG_PKG dep_PNG)
  170. endif ()
  171. set(EXPAT_PKG "")
  172. if (NOT EXPAT_FOUND)
  173. include(EXPAT/EXPAT.cmake)
  174. set(EXPAT_PKG dep_EXPAT)
  175. endif ()
  176. set(DEP_Boost_COMPONENTS system iostreams filesystem thread log locale regex date_time)
  177. include(Boost/Boost.cmake)
  178. # The order of includes respects the dependencies between libraries
  179. include(Cereal/Cereal.cmake)
  180. include(Qhull/Qhull.cmake)
  181. include(GLEW/GLEW.cmake)
  182. include(OpenCSG/OpenCSG.cmake)
  183. include(TBB/TBB.cmake)
  184. include(Blosc/Blosc.cmake)
  185. include(OpenEXR/OpenEXR.cmake)
  186. include(OpenVDB/OpenVDB.cmake)
  187. include(GMP/GMP.cmake)
  188. include(MPFR/MPFR.cmake)
  189. include(CGAL/CGAL.cmake)
  190. include(NLopt/NLopt.cmake)
  191. include(OpenSSL/OpenSSL.cmake)
  192. set(CURL_PKG "")
  193. if (NOT CURL_FOUND)
  194. include(CURL/CURL.cmake)
  195. set(CURL_PKG dep_CURL)
  196. endif ()
  197. include(JPEG/JPEG.cmake)
  198. include(TIFF/TIFF.cmake)
  199. include(wxWidgets/wxWidgets.cmake)
  200. include(OCCT/OCCT.cmake)
  201. set(_dep_list
  202. dep_Boost
  203. dep_TBB
  204. ${CURL_PKG}
  205. dep_wxWidgets
  206. dep_Cereal
  207. dep_NLopt
  208. dep_OpenVDB
  209. dep_OpenCSG
  210. dep_CGAL
  211. dep_OCCT
  212. ${PNG_PKG}
  213. ${ZLIB_PKG}
  214. ${EXPAT_PKG}
  215. )
  216. if (MSVC)
  217. # Experimental
  218. #list(APPEND _dep_list "dep_qhull")
  219. else()
  220. list(APPEND _dep_list "dep_Qhull")
  221. # Not working, static build has different Eigen
  222. #list(APPEND _dep_list "dep_libigl")
  223. endif()
  224. add_custom_target(deps ALL DEPENDS ${_dep_list})
  225. # Note: I'm not using any of the LOG_xxx options in ExternalProject_Add() commands
  226. # because they seem to generate bogus build files (possibly a bug in ExternalProject).