CMakeLists.txt 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. project(Slic3r)
  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 Slic3r with static libraries (Boost, TBB, glew)" ${SLIC3R_STATIC_INITIAL})
  21. option(SLIC3R_GUI "Compile Slic3r with GUI components (OpenGL, wxWidgets)" 1)
  22. option(SLIC3R_FHS "Assume Slic3r 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 Slic3r 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. # Proposal for C++ unit tests and sandboxes
  32. option(SLIC3R_BUILD_SANDBOXES "Build development sandboxes" OFF)
  33. option(SLIC3R_BUILD_TESTS "Build unit tests" OFF)
  34. # Print out the SLIC3R_* cache options
  35. get_cmake_property(_cache_vars CACHE_VARIABLES)
  36. list (SORT _cache_vars)
  37. foreach (_cache_var ${_cache_vars})
  38. if("${_cache_var}" MATCHES "^SLIC3R_")
  39. message(STATUS "${_cache_var}: ${${_cache_var}}")
  40. endif ()
  41. endforeach()
  42. if (MSVC)
  43. if (SLIC3R_MSVC_COMPILE_PARALLEL)
  44. add_compile_options(/MP)
  45. endif ()
  46. # /bigobj (Increase Number of Sections in .Obj file)
  47. # error C3859: virtual memory range for PCH exceeded; please recompile with a command line option of '-Zm90' or greater
  48. add_compile_options(-bigobj -Zm316)
  49. endif ()
  50. # Display and check CMAKE_PREFIX_PATH
  51. message(STATUS "SLIC3R_STATIC: ${SLIC3R_STATIC}")
  52. if (NOT "${CMAKE_PREFIX_PATH}" STREQUAL "")
  53. message(STATUS "CMAKE_PREFIX_PATH: ${CMAKE_PREFIX_PATH} (from cache or command line)")
  54. set(PREFIX_PATH_CHECK ${CMAKE_PREFIX_PATH})
  55. elseif (NOT "$ENV{CMAKE_PREFIX_PATH}" STREQUAL "")
  56. message(STATUS "CMAKE_PREFIX_PATH: $ENV{CMAKE_PREFIX_PATH} (from environment)")
  57. set(PREFIX_PATH_CHECK $ENV{CMAKE_PREFIX_PATH})
  58. else ()
  59. message(STATUS "CMAKE_PREFIX_PATH: (default)")
  60. endif ()
  61. foreach (DIR ${PREFIX_PATH_CHECK})
  62. if (NOT EXISTS "${DIR}")
  63. message(WARNING "CMAKE_PREFIX_PATH element doesn't exist: ${DIR}")
  64. endif ()
  65. endforeach ()
  66. # Add our own cmake module path.
  67. list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/modules/)
  68. enable_testing ()
  69. # Enable C++11 language standard.
  70. set(CMAKE_CXX_STANDARD 11)
  71. set(CMAKE_CXX_STANDARD_REQUIRED ON)
  72. set(CMAKE_C_STANDARD 11)
  73. set(CMAKE_C_STANDARD_REQUIRED ON)
  74. if(NOT WIN32)
  75. # Add DEBUG flags to debug builds.
  76. add_compile_options("$<$<CONFIG:DEBUG>:-DDEBUG>")
  77. endif()
  78. # To be able to link libslic3r with the Perl XS module.
  79. # Once we get rid of Perl and libslic3r is linked statically, we can get rid of -fPIC
  80. set(CMAKE_POSITION_INDEPENDENT_CODE ON)
  81. # WIN10SDK_PATH is used to point CMake to the WIN10 SDK installation directory.
  82. # We pick it from environment if it is not defined in another way
  83. if(WIN32)
  84. if(NOT DEFINED WIN10SDK_PATH)
  85. if(DEFINED ENV{WIN10SDK_PATH})
  86. set(WIN10SDK_PATH "$ENV{WIN10SDK_PATH}")
  87. endif()
  88. endif()
  89. if(DEFINED WIN10SDK_PATH AND NOT EXISTS "${WIN10SDK_PATH}/include/winrt/windows.graphics.printing3d.h")
  90. message("WIN10SDK_PATH is invalid: ${WIN10SDK_PATH}")
  91. message("${WIN10SDK_PATH}/include/winrt/windows.graphics.printing3d.h was not found")
  92. message("STL fixing by the Netfabb service will not be compiled")
  93. unset(WIN10SDK_PATH)
  94. endif()
  95. if(WIN10SDK_PATH)
  96. message("Building with Win10 Netfabb STL fixing service support")
  97. add_definitions(-DHAS_WIN10SDK)
  98. include_directories("${WIN10SDK_PATH}/Include")
  99. else()
  100. message("Building without Win10 Netfabb STL fixing service support")
  101. endif()
  102. endif()
  103. if (APPLE)
  104. message("OS X SDK Path: ${CMAKE_OSX_SYSROOT}")
  105. if (CMAKE_OSX_DEPLOYMENT_TARGET)
  106. message("OS X Deployment Target: ${CMAKE_OSX_DEPLOYMENT_TARGET}")
  107. else ()
  108. message("OS X Deployment Target: (default)")
  109. endif ()
  110. endif ()
  111. if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
  112. find_package(PkgConfig REQUIRED)
  113. if (CMAKE_VERSION VERSION_LESS "3.1")
  114. # Workaround for an old CMake, which does not understand CMAKE_CXX_STANDARD.
  115. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
  116. endif()
  117. endif()
  118. if (CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUXX)
  119. # Adding -fext-numeric-literals to enable GCC extensions on definitions of quad float literals, which are required by Boost.
  120. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fext-numeric-literals" )
  121. if (SLIC3R_SYNTAXONLY)
  122. set(CMAKE_CXX_ARCHIVE_CREATE "true")
  123. set(CMAKE_C_ARCHIVE_CREATE "true")
  124. set(CMAKE_CXX_ARCHIVE_APPEND "true")
  125. set(CMAKE_C_ARCHIVE_APPEND "true")
  126. set(CMAKE_RANLIB "true")
  127. set(CMAKE_C_LINK_EXECUTABLE "true")
  128. set(CMAKE_CXX_LINK_EXECUTABLE "true")
  129. set(CMAKE_C_COMPILE_OBJECT "<CMAKE_C_COMPILER> -fsyntax-only <DEFINES> <INCLUDES> <FLAGS> -c <SOURCE> && touch <OBJECT>")
  130. set(CMAKE_CXX_COMPILE_OBJECT "<CMAKE_CXX_COMPILER> -fsyntax-only <DEFINES> <INCLUDES> <FLAGS> -c <SOURCE> && touch <OBJECT>")
  131. endif ()
  132. endif()
  133. if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
  134. add_compile_options(-Wall)
  135. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-reorder" )
  136. # On GCC and Clang, no return from a non-void function is a warning only. Here, we make it an error.
  137. add_compile_options(-Werror=return-type)
  138. #removes LOTS of extraneous Eigen warnings
  139. add_compile_options(-Wno-ignored-attributes)
  140. if (SLIC3R_ASAN)
  141. add_compile_options(-fsanitize=address -fno-omit-frame-pointer)
  142. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=address")
  143. set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fsanitize=address")
  144. if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
  145. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lasan")
  146. endif ()
  147. endif ()
  148. endif()
  149. if (APPLE)
  150. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror=partial-availability -Werror=unguarded-availability -Werror=unguarded-availability-new")
  151. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror=partial-availability -Werror=unguarded-availability -Werror=unguarded-availability-new")
  152. endif ()
  153. # Where all the bundled libraries reside?
  154. set(LIBDIR ${CMAKE_CURRENT_SOURCE_DIR}/src)
  155. set(LIBDIR_BIN ${CMAKE_CURRENT_BINARY_DIR}/src)
  156. # For the bundled boost libraries (boost::nowide)
  157. include_directories(${LIBDIR})
  158. # For generated header files
  159. include_directories(${LIBDIR_BIN}/platform)
  160. # For libslic3r.h
  161. include_directories(${LIBDIR}/clipper ${LIBDIR}/polypartition)
  162. if(WIN32)
  163. # BOOST_ALL_NO_LIB: Avoid the automatic linking of Boost libraries on Windows. Rather rely on explicit linking.
  164. 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)
  165. endif()
  166. add_definitions(-DwxUSE_UNICODE -D_UNICODE -DUNICODE -DWXINTL_NO_GETTEXT_MACRO)
  167. if (SLIC3R_PROFILE)
  168. message("Slic3r will be built with a Shiny invasive profiler")
  169. add_definitions(-DSLIC3R_PROFILE)
  170. endif ()
  171. # Disable optimization even with debugging on.
  172. if (0)
  173. message(STATUS "Perl compiled without optimization. Disabling optimization for the Slic3r build.")
  174. message("Old CMAKE_CXX_FLAGS_RELEASE: ${CMAKE_CXX_FLAGS_RELEASE}")
  175. message("Old CMAKE_CXX_FLAGS_RELWITHDEBINFO: ${CMAKE_CXX_FLAGS_RELEASE}")
  176. message("Old CMAKE_CXX_FLAGS: ${CMAKE_CXX_FLAGS_RELEASE}")
  177. set(CMAKE_CXX_FLAGS_RELEASE "/MD /Od /Zi /EHsc /DWIN32 /DTBB_USE_ASSERT")
  178. set(CMAKE_C_FLAGS_RELEASE "/MD /Od /Zi /DWIN32 /DTBB_USE_ASSERT")
  179. set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "/MD /Od /Zi /EHsc /DWIN32 /DTBB_USE_ASSERT")
  180. set(CMAKE_C_FLAGS_RELWITHDEBINFO "/MD /Od /Zi /DWIN32 /DTBB_USE_ASSERT")
  181. set(CMAKE_CXX_FLAGS "/MD /Od /Zi /EHsc /DWIN32 /DTBB_USE_ASSERT")
  182. set(CMAKE_C_FLAGS "/MD /Od /Zi /DWIN32 /DTBB_USE_ASSERT")
  183. endif()
  184. # Find and configure boost
  185. if(SLIC3R_STATIC)
  186. # Use static boost libraries.
  187. set(Boost_USE_STATIC_LIBS ON)
  188. # Use boost libraries linked statically to the C++ runtime.
  189. # set(Boost_USE_STATIC_RUNTIME ON)
  190. endif()
  191. #set(Boost_DEBUG ON)
  192. # set(Boost_COMPILER "-vc120")
  193. find_package(Boost REQUIRED COMPONENTS system filesystem thread log locale regex)
  194. if(Boost_FOUND)
  195. include_directories(${Boost_INCLUDE_DIRS})
  196. if (APPLE)
  197. # BOOST_ASIO_DISABLE_KQUEUE : prevents a Boost ASIO bug on OS X: https://svn.boost.org/trac/boost/ticket/5339
  198. add_definitions(-DBOOST_ASIO_DISABLE_KQUEUE)
  199. endif()
  200. if(NOT SLIC3R_STATIC)
  201. add_definitions(-DBOOST_LOG_DYN_LINK)
  202. endif()
  203. endif()
  204. # Find and configure intel-tbb
  205. if(SLIC3R_STATIC)
  206. set(TBB_STATIC 1)
  207. endif()
  208. set(TBB_DEBUG 1)
  209. find_package(TBB REQUIRED)
  210. include_directories(${TBB_INCLUDE_DIRS})
  211. add_definitions(${TBB_DEFINITIONS})
  212. if(MSVC)
  213. # Suppress implicit linking of the TBB libraries by the Visual Studio compiler.
  214. add_definitions(-D__TBB_NO_IMPLICIT_LINKAGE)
  215. endif()
  216. # The Intel TBB library will use the std::exception_ptr feature of C++11.
  217. add_definitions(-DTBB_USE_CAPTURED_EXCEPTION=0)
  218. find_package(CURL REQUIRED)
  219. include_directories(${CURL_INCLUDE_DIRS})
  220. if (SLIC3R_STATIC)
  221. if (NOT APPLE)
  222. # libcurl is always linked dynamically to the system libcurl on OSX.
  223. # On other systems, libcurl is linked statically if SLIC3R_STATIC is set.
  224. add_definitions(-DCURL_STATICLIB)
  225. endif()
  226. if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
  227. # As of now, our build system produces a statically linked libcurl,
  228. # which links the OpenSSL library dynamically.
  229. find_package(OpenSSL REQUIRED)
  230. message("OpenSSL include dir: ${OPENSSL_INCLUDE_DIR}")
  231. message("OpenSSL libraries: ${OPENSSL_LIBRARIES}")
  232. include_directories(${OPENSSL_INCLUDE_DIR})
  233. endif()
  234. endif()
  235. ## OPTIONAL packages
  236. # Find eigen3 or use bundled version
  237. if (NOT SLIC3R_STATIC)
  238. find_package(Eigen3 3)
  239. endif ()
  240. if (NOT Eigen3_FOUND)
  241. set(Eigen3_FOUND 1)
  242. set(EIGEN3_INCLUDE_DIR ${LIBDIR}/eigen/)
  243. endif ()
  244. include_directories(${EIGEN3_INCLUDE_DIR})
  245. # Find expat or use bundled version
  246. # Always use the system libexpat on Linux.
  247. if (NOT SLIC3R_STATIC OR CMAKE_SYSTEM_NAME STREQUAL "Linux")
  248. find_package(EXPAT)
  249. endif ()
  250. if (NOT EXPAT_FOUND)
  251. add_library(expat STATIC
  252. ${LIBDIR}/expat/xmlparse.c
  253. ${LIBDIR}/expat/xmlrole.c
  254. ${LIBDIR}/expat/xmltok.c
  255. )
  256. set(EXPAT_FOUND 1)
  257. set(EXPAT_INCLUDE_DIRS ${LIBDIR}/expat/)
  258. set(EXPAT_LIBRARIES expat)
  259. endif ()
  260. include_directories(${EXPAT_INCLUDE_DIRS})
  261. # Find glew or use bundled version
  262. if (NOT SLIC3R_STATIC)
  263. find_package(GLEW)
  264. endif ()
  265. if (NOT GLEW_FOUND)
  266. add_library(glew STATIC ${LIBDIR}/glew/src/glew.c)
  267. set(GLEW_FOUND 1)
  268. set(GLEW_INCLUDE_DIRS ${LIBDIR}/glew/include/)
  269. set(GLEW_LIBRARIES glew)
  270. add_definitions(-DGLEW_STATIC)
  271. endif ()
  272. include_directories(${GLEW_INCLUDE_DIRS})
  273. # l10n
  274. set(L10N_DIR "${SLIC3R_RESOURCES_DIR}/localization")
  275. add_custom_target(pot
  276. COMMAND xgettext --keyword=L --from-code=UTF-8 --debug
  277. -f "${L10N_DIR}/list.txt"
  278. -o "${L10N_DIR}/Slic3rPE.pot"
  279. WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
  280. COMMENT "Generate pot file from strings in the source tree"
  281. )
  282. # libslic3r, Slic3r GUI and the slic3r executable.
  283. add_subdirectory(src)
  284. set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT slic3r_app_console)
  285. # Perl bindings, currently only used for the unit / integration tests of libslic3r.
  286. # Also runs the unit / integration tests.
  287. #FIXME Port the tests into C++ to finally get rid of the Perl!
  288. if (SLIC3R_PERL_XS)
  289. add_subdirectory(xs)
  290. endif ()
  291. if(SLIC3R_BUILD_SANDBOXES)
  292. add_subdirectory(sandboxes)
  293. endif()
  294. if(SLIC3R_BUILD_TESTS)
  295. add_subdirectory(tests)
  296. endif()
  297. # Resources install target, configure fhs.hpp on UNIX
  298. if (WIN32)
  299. install(DIRECTORY "${SLIC3R_RESOURCES_DIR}/" DESTINATION "${CMAKE_INSTALL_PREFIX}/resources")
  300. else ()
  301. set(SLIC3R_FHS_RESOURCES "${CMAKE_INSTALL_FULL_DATAROOTDIR}/slic3r-prusa3d")
  302. install(DIRECTORY "${SLIC3R_RESOURCES_DIR}/" DESTINATION "${SLIC3R_FHS_RESOURCES}")
  303. endif ()
  304. configure_file(${LIBDIR}/platform/unix/fhs.hpp.in ${LIBDIR_BIN}/platform/unix/fhs.hpp)