CMakeLists.txt 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. project(PrusaSlicer)
  2. cmake_minimum_required(VERSION 3.2)
  3. include("version.inc")
  4. include(GNUInstallDirs)
  5. set(SLIC3R_RESOURCES_DIR "${CMAKE_CURRENT_SOURCE_DIR}/resources")
  6. file(TO_NATIVE_PATH "${SLIC3R_RESOURCES_DIR}" SLIC3R_RESOURCES_DIR_WIN)
  7. if (NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
  8. message(STATUS "No build type selected, default to Release")
  9. set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Build type (default Release)" FORCE)
  10. endif()
  11. if(DEFINED ENV{SLIC3R_STATIC})
  12. set(SLIC3R_STATIC_INITIAL $ENV{SLIC3R_STATIC})
  13. else()
  14. if (MSVC OR MINGW OR APPLE)
  15. set(SLIC3R_STATIC_INITIAL 1)
  16. else()
  17. set(SLIC3R_STATIC_INITIAL 0)
  18. endif()
  19. endif()
  20. option(SLIC3R_STATIC "Compile PrusaSlicer with static libraries (Boost, TBB, glew)" ${SLIC3R_STATIC_INITIAL})
  21. option(SLIC3R_GUI "Compile PrusaSlicer with GUI components (OpenGL, wxWidgets)" 1)
  22. option(SLIC3R_FHS "Assume PrusaSlicer is to be installed in a FHS directory structure" 0)
  23. option(SLIC3R_WX_STABLE "Build against wxWidgets stable (3.0) as oppsed to dev (3.1) on Linux" 0)
  24. option(SLIC3R_PROFILE "Compile PrusaSlicer with an invasive Shiny profiler" 0)
  25. option(SLIC3R_PCH "Use precompiled headers" 1)
  26. option(SLIC3R_MSVC_COMPILE_PARALLEL "Compile on Visual Studio in parallel" 1)
  27. option(SLIC3R_MSVC_PDB "Generate PDB files on MSVC in Release mode" 1)
  28. option(SLIC3R_PERL_XS "Compile XS Perl module and enable Perl unit and integration tests" 0)
  29. option(SLIC3R_ASAN "Enable ASan on Clang and GCC" 0)
  30. option(SLIC3R_SYNTAXONLY "Only perform source code correctness checking, no binary output (UNIX only)" 0)
  31. set(SLIC3R_GTK "2" CACHE STRING "GTK version to use with wxWidgets on Linux")
  32. # Proposal for C++ unit tests and sandboxes
  33. option(SLIC3R_BUILD_SANDBOXES "Build development sandboxes" OFF)
  34. option(SLIC3R_BUILD_TESTS "Build unit tests" OFF)
  35. # Print out the SLIC3R_* cache options
  36. get_cmake_property(_cache_vars CACHE_VARIABLES)
  37. list (SORT _cache_vars)
  38. foreach (_cache_var ${_cache_vars})
  39. if("${_cache_var}" MATCHES "^SLIC3R_")
  40. message(STATUS "${_cache_var}: ${${_cache_var}}")
  41. endif ()
  42. endforeach()
  43. if (SLIC3R_GUI)
  44. add_definitions(-DSLIC3R_GUI)
  45. endif ()
  46. if (MSVC)
  47. if (SLIC3R_MSVC_COMPILE_PARALLEL)
  48. add_compile_options(/MP)
  49. endif ()
  50. # /bigobj (Increase Number of Sections in .Obj file)
  51. # error C3859: virtual memory range for PCH exceeded; please recompile with a command line option of '-Zm90' or greater
  52. # Generate symbols at every build target, even for the release.
  53. add_compile_options(-bigobj -Zm520 /Zi)
  54. endif ()
  55. # Display and check CMAKE_PREFIX_PATH
  56. message(STATUS "SLIC3R_STATIC: ${SLIC3R_STATIC}")
  57. if (NOT "${CMAKE_PREFIX_PATH}" STREQUAL "")
  58. message(STATUS "CMAKE_PREFIX_PATH: ${CMAKE_PREFIX_PATH} (from cache or command line)")
  59. set(PREFIX_PATH_CHECK ${CMAKE_PREFIX_PATH})
  60. elseif (NOT "$ENV{CMAKE_PREFIX_PATH}" STREQUAL "")
  61. message(STATUS "CMAKE_PREFIX_PATH: $ENV{CMAKE_PREFIX_PATH} (from environment)")
  62. set(PREFIX_PATH_CHECK $ENV{CMAKE_PREFIX_PATH})
  63. else ()
  64. message(STATUS "CMAKE_PREFIX_PATH: (default)")
  65. endif ()
  66. foreach (DIR ${PREFIX_PATH_CHECK})
  67. if (NOT EXISTS "${DIR}")
  68. message(WARNING "CMAKE_PREFIX_PATH element doesn't exist: ${DIR}")
  69. endif ()
  70. endforeach ()
  71. # Add our own cmake module path.
  72. list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/modules/)
  73. enable_testing ()
  74. # Enable C++11 language standard.
  75. set(CMAKE_CXX_STANDARD 11)
  76. set(CMAKE_CXX_STANDARD_REQUIRED ON)
  77. if(NOT WIN32)
  78. # Add DEBUG flags to debug builds.
  79. add_compile_options("$<$<CONFIG:DEBUG>:-DDEBUG>")
  80. endif()
  81. # To be able to link libslic3r with the Perl XS module.
  82. # Once we get rid of Perl and libslic3r is linked statically, we can get rid of -fPIC
  83. set(CMAKE_POSITION_INDEPENDENT_CODE ON)
  84. # WIN10SDK_PATH is used to point CMake to the WIN10 SDK installation directory.
  85. # We pick it from environment if it is not defined in another way
  86. if(WIN32)
  87. if(NOT DEFINED WIN10SDK_PATH)
  88. if(DEFINED ENV{WIN10SDK_PATH})
  89. set(WIN10SDK_PATH "$ENV{WIN10SDK_PATH}")
  90. endif()
  91. endif()
  92. if(DEFINED WIN10SDK_PATH AND NOT EXISTS "${WIN10SDK_PATH}/include/winrt/windows.graphics.printing3d.h")
  93. message("WIN10SDK_PATH is invalid: ${WIN10SDK_PATH}")
  94. message("${WIN10SDK_PATH}/include/winrt/windows.graphics.printing3d.h was not found")
  95. message("STL fixing by the Netfabb service will not be compiled")
  96. unset(WIN10SDK_PATH)
  97. endif()
  98. if(WIN10SDK_PATH)
  99. message("Building with Win10 Netfabb STL fixing service support")
  100. add_definitions(-DHAS_WIN10SDK)
  101. include_directories("${WIN10SDK_PATH}/Include")
  102. else()
  103. message("Building without Win10 Netfabb STL fixing service support")
  104. endif()
  105. endif()
  106. if (APPLE)
  107. message("OS X SDK Path: ${CMAKE_OSX_SYSROOT}")
  108. if (CMAKE_OSX_DEPLOYMENT_TARGET)
  109. message("OS X Deployment Target: ${CMAKE_OSX_DEPLOYMENT_TARGET}")
  110. else ()
  111. message("OS X Deployment Target: (default)")
  112. endif ()
  113. endif ()
  114. if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
  115. find_package(PkgConfig REQUIRED)
  116. if (CMAKE_VERSION VERSION_LESS "3.1")
  117. # Workaround for an old CMake, which does not understand CMAKE_CXX_STANDARD.
  118. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
  119. endif()
  120. # Boost on Raspberry-Pi does not link to pthreads.
  121. set(THREADS_PREFER_PTHREAD_FLAG ON)
  122. find_package(Threads REQUIRED)
  123. endif()
  124. if (CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUXX)
  125. # Adding -fext-numeric-literals to enable GCC extensions on definitions of quad float literals, which are required by Boost.
  126. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fext-numeric-literals" )
  127. if (SLIC3R_SYNTAXONLY)
  128. set(CMAKE_CXX_ARCHIVE_CREATE "true")
  129. set(CMAKE_C_ARCHIVE_CREATE "true")
  130. set(CMAKE_CXX_ARCHIVE_APPEND "true")
  131. set(CMAKE_C_ARCHIVE_APPEND "true")
  132. set(CMAKE_RANLIB "true")
  133. set(CMAKE_C_LINK_EXECUTABLE "true")
  134. set(CMAKE_CXX_LINK_EXECUTABLE "true")
  135. set(CMAKE_C_COMPILE_OBJECT "<CMAKE_C_COMPILER> -fsyntax-only <DEFINES> <INCLUDES> <FLAGS> -c <SOURCE> && touch <OBJECT>")
  136. set(CMAKE_CXX_COMPILE_OBJECT "<CMAKE_CXX_COMPILER> -fsyntax-only <DEFINES> <INCLUDES> <FLAGS> -c <SOURCE> && touch <OBJECT>")
  137. endif ()
  138. endif()
  139. if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
  140. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall" )
  141. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-reorder" )
  142. # On GCC and Clang, no return from a non-void function is a warning only. Here, we make it an error.
  143. add_compile_options(-Werror=return-type)
  144. #removes LOTS of extraneous Eigen warnings (GCC only supports it since 6.1)
  145. #if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" OR CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 6.1)
  146. # add_compile_options(-Wno-ignored-attributes) # Tamas: Eigen include dirs are marked as SYSTEM
  147. #endif()
  148. #GCC generates loads of -Wunknown-pragmas when compiling igl. The fix is not easy due to a bug in gcc, see
  149. # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66943 or
  150. # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53431
  151. # We will turn the warning of for GCC for now:
  152. if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
  153. add_compile_options(-Wno-unknown-pragmas)
  154. endif()
  155. if (SLIC3R_ASAN)
  156. add_compile_options(-fsanitize=address -fno-omit-frame-pointer)
  157. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=address")
  158. set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fsanitize=address")
  159. if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
  160. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lasan")
  161. endif ()
  162. endif ()
  163. endif()
  164. if (APPLE)
  165. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror=partial-availability -Werror=unguarded-availability -Werror=unguarded-availability-new")
  166. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror=partial-availability -Werror=unguarded-availability -Werror=unguarded-availability-new")
  167. endif ()
  168. # Where all the bundled libraries reside?
  169. set(LIBDIR ${CMAKE_CURRENT_SOURCE_DIR}/src)
  170. set(LIBDIR_BIN ${CMAKE_CURRENT_BINARY_DIR}/src)
  171. # For the bundled boost libraries (boost::nowide)
  172. include_directories(${LIBDIR})
  173. # For generated header files
  174. include_directories(${LIBDIR_BIN}/platform)
  175. # For libslic3r.h
  176. include_directories(${LIBDIR}/clipper ${LIBDIR}/polypartition)
  177. if(WIN32)
  178. # BOOST_ALL_NO_LIB: Avoid the automatic linking of Boost libraries on Windows. Rather rely on explicit linking.
  179. add_definitions(-D_USE_MATH_DEFINES -D_WIN32 -DBOOST_ALL_NO_LIB -DBOOST_USE_WINAPI_VERSION=0x601 -D_CRT_SECURE_NO_WARNINGS -D_SCL_SECURE_NO_WARNINGS)
  180. endif()
  181. add_definitions(-DwxUSE_UNICODE -D_UNICODE -DUNICODE -DWXINTL_NO_GETTEXT_MACRO)
  182. if (SLIC3R_PROFILE)
  183. message("PrusaSlicer will be built with a Shiny invasive profiler")
  184. add_definitions(-DSLIC3R_PROFILE)
  185. endif ()
  186. # Disable optimization even with debugging on.
  187. if (0)
  188. message(STATUS "Perl compiled without optimization. Disabling optimization for the PrusaSlicer build.")
  189. message("Old CMAKE_CXX_FLAGS_RELEASE: ${CMAKE_CXX_FLAGS_RELEASE}")
  190. message("Old CMAKE_CXX_FLAGS_RELWITHDEBINFO: ${CMAKE_CXX_FLAGS_RELEASE}")
  191. message("Old CMAKE_CXX_FLAGS: ${CMAKE_CXX_FLAGS_RELEASE}")
  192. set(CMAKE_CXX_FLAGS_RELEASE "/MD /Od /Zi /EHsc /DWIN32 /DTBB_USE_ASSERT")
  193. set(CMAKE_C_FLAGS_RELEASE "/MD /Od /Zi /DWIN32 /DTBB_USE_ASSERT")
  194. set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "/MD /Od /Zi /EHsc /DWIN32 /DTBB_USE_ASSERT")
  195. set(CMAKE_C_FLAGS_RELWITHDEBINFO "/MD /Od /Zi /DWIN32 /DTBB_USE_ASSERT")
  196. set(CMAKE_CXX_FLAGS "/MD /Od /Zi /EHsc /DWIN32 /DTBB_USE_ASSERT")
  197. set(CMAKE_C_FLAGS "/MD /Od /Zi /DWIN32 /DTBB_USE_ASSERT")
  198. endif()
  199. # Find and configure boost
  200. if(SLIC3R_STATIC)
  201. # Use static boost libraries.
  202. set(Boost_USE_STATIC_LIBS ON)
  203. # Use boost libraries linked statically to the C++ runtime.
  204. # set(Boost_USE_STATIC_RUNTIME ON)
  205. endif()
  206. #set(Boost_DEBUG ON)
  207. # set(Boost_COMPILER "-vc120")
  208. if(NOT WIN32)
  209. # boost::process was introduced first in version 1.64.0
  210. set(MINIMUM_BOOST_VERSION "1.64.0")
  211. endif()
  212. find_package(Boost ${MINIMUM_BOOST_VERSION} REQUIRED COMPONENTS system filesystem thread log locale regex)
  213. add_library(boost_libs INTERFACE)
  214. add_library(boost_headeronly INTERFACE)
  215. if (APPLE)
  216. # BOOST_ASIO_DISABLE_KQUEUE : prevents a Boost ASIO bug on OS X: https://svn.boost.org/trac/boost/ticket/5339
  217. target_compile_definitions(boost_headeronly INTERFACE BOOST_ASIO_DISABLE_KQUEUE)
  218. endif()
  219. if(NOT SLIC3R_STATIC)
  220. target_compile_definitions(boost_headeronly INTERFACE BOOST_LOG_DYN_LINK)
  221. endif()
  222. if(TARGET Boost::system)
  223. message(STATUS "Boost::boost exists")
  224. target_link_libraries(boost_headeronly INTERFACE Boost::boost)
  225. target_link_libraries(boost_libs INTERFACE
  226. boost_headeronly # includes the custom compile definitions as well
  227. Boost::system
  228. Boost::filesystem
  229. Boost::thread
  230. Boost::log
  231. Boost::locale
  232. Boost::regex
  233. )
  234. else()
  235. target_include_directories(boost_headeronly INTERFACE ${Boost_INCLUDE_DIRS})
  236. target_link_libraries(boost_libs INTERFACE boost_headeronly ${Boost_LIBRARIES})
  237. endif()
  238. # Find and configure intel-tbb
  239. if(SLIC3R_STATIC)
  240. set(TBB_STATIC 1)
  241. endif()
  242. set(TBB_DEBUG 1)
  243. find_package(TBB REQUIRED)
  244. include_directories(${TBB_INCLUDE_DIRS})
  245. add_definitions(${TBB_DEFINITIONS})
  246. if(MSVC)
  247. # Suppress implicit linking of the TBB libraries by the Visual Studio compiler.
  248. add_definitions(-D__TBB_NO_IMPLICIT_LINKAGE)
  249. endif()
  250. # The Intel TBB library will use the std::exception_ptr feature of C++11.
  251. add_definitions(-DTBB_USE_CAPTURED_EXCEPTION=0)
  252. find_package(CURL REQUIRED)
  253. include_directories(${CURL_INCLUDE_DIRS})
  254. if (SLIC3R_STATIC)
  255. if (NOT APPLE)
  256. # libcurl is always linked dynamically to the system libcurl on OSX.
  257. # On other systems, libcurl is linked statically if SLIC3R_STATIC is set.
  258. add_definitions(-DCURL_STATICLIB)
  259. endif()
  260. if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
  261. # As of now, our build system produces a statically linked libcurl,
  262. # which links the OpenSSL library dynamically.
  263. find_package(OpenSSL REQUIRED)
  264. message("OpenSSL include dir: ${OPENSSL_INCLUDE_DIR}")
  265. message("OpenSSL libraries: ${OPENSSL_LIBRARIES}")
  266. include_directories(${OPENSSL_INCLUDE_DIR})
  267. endif()
  268. endif()
  269. ## OPTIONAL packages
  270. # Find eigen3 or use bundled version
  271. if (NOT SLIC3R_STATIC)
  272. find_package(Eigen3 3.3)
  273. endif ()
  274. if (NOT EIGEN3_FOUND)
  275. set(EIGEN3_FOUND 1)
  276. set(EIGEN3_INCLUDE_DIR ${LIBDIR}/eigen/)
  277. endif ()
  278. include_directories(BEFORE SYSTEM ${EIGEN3_INCLUDE_DIR})
  279. # Find expat or use bundled version
  280. # Always use the system libexpat on Linux.
  281. if (NOT SLIC3R_STATIC OR CMAKE_SYSTEM_NAME STREQUAL "Linux")
  282. find_package(EXPAT)
  283. endif ()
  284. if (NOT EXPAT_FOUND)
  285. add_library(expat STATIC
  286. ${LIBDIR}/expat/xmlparse.c
  287. ${LIBDIR}/expat/xmlrole.c
  288. ${LIBDIR}/expat/xmltok.c
  289. )
  290. set(EXPAT_FOUND 1)
  291. set(EXPAT_INCLUDE_DIRS ${LIBDIR}/expat/)
  292. set(EXPAT_LIBRARIES expat)
  293. endif ()
  294. include_directories(${EXPAT_INCLUDE_DIRS})
  295. # Find glew or use bundled version
  296. if (NOT SLIC3R_STATIC)
  297. find_package(GLEW)
  298. endif ()
  299. if (NOT GLEW_FOUND)
  300. add_library(glew STATIC ${LIBDIR}/glew/src/glew.c)
  301. set(GLEW_FOUND 1)
  302. set(GLEW_INCLUDE_DIRS ${LIBDIR}/glew/include/)
  303. set(GLEW_LIBRARIES glew)
  304. add_definitions(-DGLEW_STATIC)
  305. endif ()
  306. include_directories(${GLEW_INCLUDE_DIRS})
  307. # Find the Cereal serialization library
  308. add_library(cereal INTERFACE)
  309. target_include_directories(cereal INTERFACE include)
  310. # l10n
  311. set(L10N_DIR "${SLIC3R_RESOURCES_DIR}/localization")
  312. add_custom_target(pot
  313. COMMAND xgettext --keyword=L --add-comments=TRN --from-code=UTF-8 --debug
  314. -f "${L10N_DIR}/list.txt"
  315. -o "${L10N_DIR}/PrusaSlicer.pot"
  316. WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
  317. COMMENT "Generate pot file from strings in the source tree"
  318. )
  319. # libslic3r, PrusaSlicer GUI and the PrusaSlicer executable.
  320. add_subdirectory(src)
  321. set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT PrusaSlicer_app_console)
  322. # Perl bindings, currently only used for the unit / integration tests of libslic3r.
  323. # Also runs the unit / integration tests.
  324. #FIXME Port the tests into C++ to finally get rid of the Perl!
  325. if (SLIC3R_PERL_XS)
  326. add_subdirectory(xs)
  327. endif ()
  328. if(SLIC3R_BUILD_SANDBOXES)
  329. add_subdirectory(sandboxes)
  330. endif()
  331. if(SLIC3R_BUILD_TESTS)
  332. add_subdirectory(tests)
  333. endif()
  334. # Resources install target, configure fhs.hpp on UNIX
  335. if (WIN32)
  336. install(DIRECTORY "${SLIC3R_RESOURCES_DIR}/" DESTINATION "${CMAKE_INSTALL_PREFIX}/resources")
  337. else ()
  338. set(SLIC3R_FHS_RESOURCES "${CMAKE_INSTALL_FULL_DATAROOTDIR}/PrusaSlicer")
  339. install(DIRECTORY "${SLIC3R_RESOURCES_DIR}/" DESTINATION "${SLIC3R_FHS_RESOURCES}")
  340. endif ()
  341. configure_file(${LIBDIR}/platform/unix/fhs.hpp.in ${LIBDIR_BIN}/platform/unix/fhs.hpp)