CMakeLists.txt 9.3 KB

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