CMakeLists.txt 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. project(XS)
  2. # Find the Perl interpreter, add local-lib to PATH and PERL5LIB environment variables,
  3. # so the locally installed modules (mainly the Alien::wxPerl) will be reached.
  4. if (WIN32)
  5. set(ENV_PATH_SEPARATOR ";")
  6. else()
  7. set(ENV_PATH_SEPARATOR ":")
  8. endif()
  9. # Install the XS.pm and XS.{so,dll,bundle} into the local-lib directory.
  10. set(PERL_LOCAL_LIB_DIR ${PROJECT_SOURCE_DIR}/../local-lib)
  11. set(ENV{PATH} "${PERL_LOCAL_LIB_DIR}/bin${ENV_PATH_SEPARATOR}$ENV{PATH}")
  12. set(PERL_INCLUDE "${PERL_LOCAL_LIB_DIR}/lib/perl5${ENV_PATH_SEPARATOR}$ENV{PERL5LIB}")
  13. message("PATH: $ENV{PATH}")
  14. message("PERL_INCLUDE: ${PERL_INCLUDE}")
  15. find_package(Perl REQUIRED)
  16. if (WIN32)
  17. # On Windows passing the PERL5LIB variable causes various problems (such as with MAX_PATH and others),
  18. # basically I've found no good way to do it on Windows.
  19. set(PERL5LIB_ENV_CMD "")
  20. else()
  21. set(PERL5LIB_ENV_CMD ${CMAKE_COMMAND} -E env PERL5LIB=${PERL_INCLUDE})
  22. endif()
  23. # Perl specific stuff
  24. find_package(PerlLibs REQUIRED)
  25. set(PerlEmbed_DEBUG 1)
  26. find_package(PerlEmbed REQUIRED)
  27. # Generate the Slic3r Perl module (XS) typemap file.
  28. set(MyTypemap ${CMAKE_CURRENT_BINARY_DIR}/typemap)
  29. add_custom_command(
  30. OUTPUT ${MyTypemap}
  31. DEPENDS ${CMAKE_CURRENT_LIST_DIR}/xsp/my.map
  32. COMMAND ${PERL5LIB_ENV_CMD} ${PERL_EXECUTABLE} -MExtUtils::Typemaps -MExtUtils::Typemaps::Basic -e "$typemap = ExtUtils::Typemaps->new(file => \"${CMAKE_CURRENT_LIST_DIR}/xsp/my.map\"); $typemap->merge(typemap => ExtUtils::Typemaps::Basic->new); $typemap->write(file => \"${MyTypemap}\")"
  33. VERBATIM
  34. )
  35. # Generate the Slic3r Perl module (XS) main.xs file.
  36. set(XS_MAIN_XS ${CMAKE_CURRENT_BINARY_DIR}/main.xs)
  37. set(XSP_DIR ${CMAKE_CURRENT_SOURCE_DIR}/xsp)
  38. #FIXME list the dependecies explicitely, add dependency on the typemap.
  39. set(XS_XSP_FILES
  40. ${XSP_DIR}/BoundingBox.xsp
  41. ${XSP_DIR}/BridgeDetector.xsp
  42. ${XSP_DIR}/Clipper.xsp
  43. ${XSP_DIR}/Config.xsp
  44. ${XSP_DIR}/ExPolygon.xsp
  45. ${XSP_DIR}/ExPolygonCollection.xsp
  46. ${XSP_DIR}/ExtrusionEntityCollection.xsp
  47. ${XSP_DIR}/ExtrusionLoop.xsp
  48. ${XSP_DIR}/ExtrusionMultiPath.xsp
  49. ${XSP_DIR}/ExtrusionPath.xsp
  50. ${XSP_DIR}/ExtrusionSimulator.xsp
  51. ${XSP_DIR}/Filler.xsp
  52. ${XSP_DIR}/Flow.xsp
  53. ${XSP_DIR}/GCode.xsp
  54. # ${XSP_DIR}/GCodeSender.xsp
  55. ${XSP_DIR}/Geometry.xsp
  56. ${XSP_DIR}/Layer.xsp
  57. ${XSP_DIR}/Line.xsp
  58. ${XSP_DIR}/Model.xsp
  59. ${XSP_DIR}/PerimeterGenerator.xsp
  60. ${XSP_DIR}/PlaceholderParser.xsp
  61. ${XSP_DIR}/Point.xsp
  62. ${XSP_DIR}/Polygon.xsp
  63. ${XSP_DIR}/Polyline.xsp
  64. ${XSP_DIR}/PolylineCollection.xsp
  65. ${XSP_DIR}/Print.xsp
  66. ${XSP_DIR}/Surface.xsp
  67. ${XSP_DIR}/SurfaceCollection.xsp
  68. ${XSP_DIR}/TriangleMesh.xsp
  69. ${XSP_DIR}/XS.xsp
  70. )
  71. foreach (file ${XS_XSP_FILES})
  72. if (MSVC)
  73. # Visual Studio C compiler has issues with FILE pragmas containing quotes.
  74. set(INCLUDE_COMMANDS "${INCLUDE_COMMANDS}INCLUDE_COMMAND: $^X -MExtUtils::XSpp::Cmd -e xspp -- -t ${CMAKE_CURRENT_LIST_DIR}/xsp/typemap.xspt ${file}\n")
  75. else ()
  76. set(INCLUDE_COMMANDS "${INCLUDE_COMMANDS}INCLUDE_COMMAND: $^X -MExtUtils::XSpp::Cmd -e xspp -- -t \"${CMAKE_CURRENT_LIST_DIR}/xsp/typemap.xspt\" \"${file}\"\n")
  77. endif ()
  78. endforeach ()
  79. configure_file(main.xs.in ${XS_MAIN_XS} @ONLY) # Insert INCLUDE_COMMANDS into main.xs
  80. # Generate the Slic3r Perl module (XS) XS.cpp file.
  81. #FIXME add the dependency on main.xs and typemap.
  82. set(XS_MAIN_CPP ${CMAKE_CURRENT_BINARY_DIR}/XS.cpp)
  83. add_custom_command(
  84. OUTPUT ${XS_MAIN_CPP}
  85. DEPENDS ${MyTypemap} ${XS_XSP_FILES} ${CMAKE_CURRENT_LIST_DIR}/xsp/typemap.xspt
  86. COMMAND ${PERL5LIB_ENV_CMD} xsubpp -typemap typemap -output ${XS_MAIN_CPP} -hiertype ${XS_MAIN_XS}
  87. VERBATIM
  88. )
  89. # Define the Perl XS shared library.
  90. if(APPLE)
  91. set(XS_SHARED_LIBRARY_TYPE MODULE)
  92. else()
  93. set(XS_SHARED_LIBRARY_TYPE SHARED)
  94. endif()
  95. add_library(XS ${XS_SHARED_LIBRARY_TYPE}
  96. ${XS_MAIN_CPP}
  97. src/perlglue.cpp
  98. src/ppport.h
  99. src/xsinit.h
  100. xsp/my.map
  101. # mytype.map is empty. Is it required by Build.PL or the Perl xspp module?
  102. xsp/mytype.map
  103. # Used by Perl xsubpp to generate XS.cpp
  104. xsp/typemap.xspt
  105. )
  106. if(APPLE)
  107. set_target_properties(XS PROPERTIES BUNDLE TRUE)
  108. # Ignore undefined symbols of the perl interpreter, they will be found in the caller image.
  109. target_link_libraries(XS "-undefined dynamic_lookup")
  110. endif()
  111. target_link_libraries(XS libslic3r)
  112. target_include_directories(XS PRIVATE src ${LIBDIR}/libslic3r)
  113. target_compile_definitions(XS PRIVATE -DSLIC3RXS)
  114. set_target_properties(XS PROPERTIES PREFIX "") # Prevent cmake from generating libXS.so instead of XS.so
  115. if (APPLE)
  116. # -liconv: boost links to libiconv by default
  117. target_link_libraries(XS "-liconv -framework IOKit" "-framework CoreFoundation" -lc++)
  118. elseif (MSVC)
  119. target_link_libraries(XS )
  120. else ()
  121. target_link_libraries(XS -lstdc++)
  122. endif ()
  123. # Windows specific stuff
  124. if (WIN32)
  125. target_compile_definitions(XS PRIVATE -DNOGDI -DNOMINMAX -DHAS_BOOL)
  126. endif ()
  127. # SLIC3R_MSVC_PDB
  128. if (MSVC AND SLIC3R_MSVC_PDB AND "${CMAKE_BUILD_TYPE}" STREQUAL "Release")
  129. set_target_properties(XS PROPERTIES
  130. COMPILE_FLAGS "/Zi"
  131. LINK_FLAGS "/DEBUG /OPT:REF /OPT:ICF"
  132. )
  133. endif()
  134. if (CMAKE_BUILD_TYPE MATCHES DEBUG)
  135. target_compile_definitions(XS PRIVATE -DSLIC3R_DEBUG -DDEBUG -D_DEBUG)
  136. else ()
  137. target_compile_definitions(XS PRIVATE -DNDEBUG)
  138. endif ()
  139. target_include_directories(XS PRIVATE ${PERL_INCLUDE_PATH})
  140. target_compile_options(XS PRIVATE ${PerlEmbed_CCFLAGS})
  141. if (WIN32)
  142. target_link_libraries(XS ${PERL_LIBRARY})
  143. endif()
  144. set(PERL_LOCAL_LIB_ARCH_DIR "${PERL_LOCAL_LIB_DIR}/lib/perl5/${PerlEmbed_ARCHNAME}")
  145. add_custom_command(
  146. TARGET XS
  147. POST_BUILD
  148. COMMAND ${CMAKE_COMMAND} -E make_directory "${PERL_LOCAL_LIB_ARCH_DIR}/auto/Slic3r/XS/"
  149. COMMAND ${CMAKE_COMMAND} -E copy "$<TARGET_FILE:XS>" "${PERL_LOCAL_LIB_ARCH_DIR}/auto/Slic3r/XS/"
  150. COMMAND ${CMAKE_COMMAND} -E make_directory "${PERL_LOCAL_LIB_ARCH_DIR}/Slic3r/"
  151. COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_CURRENT_SOURCE_DIR}/lib/Slic3r/XS.pm" "${PERL_LOCAL_LIB_ARCH_DIR}/Slic3r/"
  152. COMMENT "Installing XS.pm and XS.{so,dll,bundle} into the local-lib directory ..."
  153. )
  154. if(APPLE)
  155. add_custom_command(
  156. TARGET XS
  157. POST_BUILD
  158. COMMAND ${CMAKE_COMMAND} -E rename "${PERL_LOCAL_LIB_ARCH_DIR}/auto/Slic3r/XS/XS" "${PERL_LOCAL_LIB_ARCH_DIR}/auto/Slic3r/XS/XS.bundle"
  159. )
  160. endif()
  161. if (MSVC)
  162. # Here we associate some additional properties with the MSVC project to enable compilation and debugging out of the box.
  163. get_filename_component(PROPS_PERL_BIN_PATH "${PERL_EXECUTABLE}" DIRECTORY)
  164. string(REPLACE "/" "\\" PROPS_PERL_BIN_PATH "${PROPS_PERL_BIN_PATH}")
  165. string(REPLACE "/" "\\" PROPS_PERL_EXECUTABLE "${PERL_EXECUTABLE}")
  166. string(REPLACE "/" "\\" PROPS_CMAKE_SOURCE_DIR "${CMAKE_SOURCE_DIR}")
  167. configure_file("../cmake/msvc/xs.wperl.props.in" "${CMAKE_BINARY_DIR}/xs.wperl.props" NEWLINE_STYLE CRLF)
  168. set_target_properties(XS PROPERTIES VS_USER_PROPS "${CMAKE_BINARY_DIR}/xs.wperl.props")
  169. if ("${CMAKE_SIZEOF_VOID_P}" STREQUAL "8")
  170. set(_bits 64)
  171. elseif ("${CMAKE_SIZEOF_VOID_P}" STREQUAL "4")
  172. set(_bits 32)
  173. endif ()
  174. add_custom_command(TARGET XS POST_BUILD
  175. COMMAND ${CMAKE_COMMAND} -E copy ${TOP_LEVEL_PROJECT_DIR}/deps/GMP/gmp/lib/win${_bits}/libgmp-10.dll "${PERL_LOCAL_LIB_ARCH_DIR}/auto/Slic3r/XS/"
  176. COMMENT "Installing gmp runtime into the local-lib directory ..."
  177. VERBATIM)
  178. add_custom_command(TARGET XS POST_BUILD
  179. COMMAND ${CMAKE_COMMAND} -E copy ${TOP_LEVEL_PROJECT_DIR}/deps/MPFR/mpfr/lib/win${_bits}/libmpfr-4.dll "${PERL_LOCAL_LIB_ARCH_DIR}/auto/Slic3r/XS/"
  180. COMMENT "Installing mpfr runtime into the local-lib directory ..."
  181. VERBATIM)
  182. endif()
  183. # Installation
  184. install(TARGETS XS DESTINATION ${PERL_VENDORARCH}/auto/Slic3r/XS)
  185. install(FILES lib/Slic3r/XS.pm DESTINATION ${PERL_VENDORLIB}/Slic3r)
  186. # Unit / integration tests
  187. enable_testing()
  188. get_filename_component(PERL_BIN_PATH "${PERL_EXECUTABLE}" DIRECTORY)
  189. if (MSVC)
  190. set(PERL_PROVE "${PERL_BIN_PATH}/prove.bat")
  191. else ()
  192. set(PERL_PROVE "${PERL_BIN_PATH}/prove")
  193. endif ()
  194. add_test (NAME xs COMMAND "${PERL_EXECUTABLE}" ${PERL_PROVE} -I ${PERL_LOCAL_LIB_DIR}/lib/perl5 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR})
  195. add_test (NAME integration COMMAND "${PERL_EXECUTABLE}" ${PERL_PROVE} WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/..)