CMakeLists.txt 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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}/MotionPlanner.xsp
  60. ${XSP_DIR}/PerimeterGenerator.xsp
  61. ${XSP_DIR}/PlaceholderParser.xsp
  62. ${XSP_DIR}/Point.xsp
  63. ${XSP_DIR}/Polygon.xsp
  64. ${XSP_DIR}/Polyline.xsp
  65. ${XSP_DIR}/PolylineCollection.xsp
  66. ${XSP_DIR}/Print.xsp
  67. ${XSP_DIR}/Surface.xsp
  68. ${XSP_DIR}/SurfaceCollection.xsp
  69. ${XSP_DIR}/TriangleMesh.xsp
  70. ${XSP_DIR}/XS.xsp
  71. )
  72. foreach (file ${XS_XSP_FILES})
  73. if (MSVC)
  74. # Visual Studio C compiler has issues with FILE pragmas containing quotes.
  75. set(INCLUDE_COMMANDS "${INCLUDE_COMMANDS}INCLUDE_COMMAND: $^X -MExtUtils::XSpp::Cmd -e xspp -- -t ${CMAKE_CURRENT_LIST_DIR}/xsp/typemap.xspt ${file}\n")
  76. else ()
  77. set(INCLUDE_COMMANDS "${INCLUDE_COMMANDS}INCLUDE_COMMAND: $^X -MExtUtils::XSpp::Cmd -e xspp -- -t \"${CMAKE_CURRENT_LIST_DIR}/xsp/typemap.xspt\" \"${file}\"\n")
  78. endif ()
  79. endforeach ()
  80. configure_file(main.xs.in ${XS_MAIN_XS} @ONLY) # Insert INCLUDE_COMMANDS into main.xs
  81. # Generate the Slic3r Perl module (XS) XS.cpp file.
  82. #FIXME add the dependency on main.xs and typemap.
  83. set(XS_MAIN_CPP ${CMAKE_CURRENT_BINARY_DIR}/XS.cpp)
  84. add_custom_command(
  85. OUTPUT ${XS_MAIN_CPP}
  86. DEPENDS ${MyTypemap} ${XS_XSP_FILES} ${CMAKE_CURRENT_LIST_DIR}/xsp/typemap.xspt
  87. COMMAND ${PERL5LIB_ENV_CMD} xsubpp -typemap typemap -output ${XS_MAIN_CPP} -hiertype ${XS_MAIN_XS}
  88. VERBATIM
  89. )
  90. # Define the Perl XS shared library.
  91. if(APPLE)
  92. set(XS_SHARED_LIBRARY_TYPE MODULE)
  93. else()
  94. set(XS_SHARED_LIBRARY_TYPE SHARED)
  95. endif()
  96. add_library(XS ${XS_SHARED_LIBRARY_TYPE}
  97. ${XS_MAIN_CPP}
  98. src/perlglue.cpp
  99. src/ppport.h
  100. src/xsinit.h
  101. xsp/my.map
  102. # mytype.map is empty. Is it required by Build.PL or the Perl xspp module?
  103. xsp/mytype.map
  104. # Used by Perl xsubpp to generate XS.cpp
  105. xsp/typemap.xspt
  106. )
  107. if(APPLE)
  108. set_target_properties(XS PROPERTIES BUNDLE TRUE)
  109. # Ignore undefined symbols of the perl interpreter, they will be found in the caller image.
  110. target_link_libraries(XS "-undefined dynamic_lookup")
  111. endif()
  112. target_link_libraries(XS libslic3r)
  113. target_include_directories(XS PRIVATE src ${LIBDIR}/libslic3r)
  114. target_compile_definitions(XS PRIVATE -DSLIC3RXS)
  115. set_target_properties(XS PROPERTIES PREFIX "") # Prevent cmake from generating libXS.so instead of XS.so
  116. if (APPLE)
  117. # -liconv: boost links to libiconv by default
  118. target_link_libraries(XS "-liconv -framework IOKit" "-framework CoreFoundation" -lc++)
  119. elseif (MSVC)
  120. target_link_libraries(XS )
  121. else ()
  122. target_link_libraries(XS -lstdc++)
  123. endif ()
  124. # Windows specific stuff
  125. if (WIN32)
  126. target_compile_definitions(XS PRIVATE -DNOGDI -DNOMINMAX -DHAS_BOOL)
  127. endif ()
  128. # SLIC3R_MSVC_PDB
  129. if (MSVC AND SLIC3R_MSVC_PDB AND "${CMAKE_BUILD_TYPE}" STREQUAL "Release")
  130. set_target_properties(XS PROPERTIES
  131. COMPILE_FLAGS "/Zi"
  132. LINK_FLAGS "/DEBUG /OPT:REF /OPT:ICF"
  133. )
  134. endif()
  135. if (CMAKE_BUILD_TYPE MATCHES DEBUG)
  136. target_compile_definitions(XS PRIVATE -DSLIC3R_DEBUG -DDEBUG -D_DEBUG)
  137. else ()
  138. target_compile_definitions(XS PRIVATE -DNDEBUG)
  139. endif ()
  140. target_include_directories(XS PRIVATE ${PERL_INCLUDE_PATH})
  141. target_compile_options(XS PRIVATE ${PerlEmbed_CCFLAGS})
  142. if (WIN32)
  143. target_link_libraries(XS ${PERL_LIBRARY})
  144. endif()
  145. set(PERL_LOCAL_LIB_ARCH_DIR "${PERL_LOCAL_LIB_DIR}/lib/perl5/${PerlEmbed_ARCHNAME}")
  146. add_custom_command(
  147. TARGET XS
  148. POST_BUILD
  149. COMMAND ${CMAKE_COMMAND} -E make_directory "${PERL_LOCAL_LIB_ARCH_DIR}/auto/Slic3r/XS/"
  150. COMMAND ${CMAKE_COMMAND} -E copy "$<TARGET_FILE:XS>" "${PERL_LOCAL_LIB_ARCH_DIR}/auto/Slic3r/XS/"
  151. COMMAND ${CMAKE_COMMAND} -E make_directory "${PERL_LOCAL_LIB_ARCH_DIR}/Slic3r/"
  152. COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_CURRENT_SOURCE_DIR}/lib/Slic3r/XS.pm" "${PERL_LOCAL_LIB_ARCH_DIR}/Slic3r/"
  153. COMMENT "Installing XS.pm and XS.{so,dll,bundle} into the local-lib directory ..."
  154. )
  155. if(APPLE)
  156. add_custom_command(
  157. TARGET XS
  158. POST_BUILD
  159. COMMAND ${CMAKE_COMMAND} -E rename "${PERL_LOCAL_LIB_ARCH_DIR}/auto/Slic3r/XS/XS" "${PERL_LOCAL_LIB_ARCH_DIR}/auto/Slic3r/XS/XS.bundle"
  160. )
  161. endif()
  162. if (MSVC)
  163. # Here we associate some additional properties with the MSVC project to enable compilation and debugging out of the box.
  164. get_filename_component(PROPS_PERL_BIN_PATH "${PERL_EXECUTABLE}" DIRECTORY)
  165. string(REPLACE "/" "\\" PROPS_PERL_BIN_PATH "${PROPS_PERL_BIN_PATH}")
  166. string(REPLACE "/" "\\" PROPS_PERL_EXECUTABLE "${PERL_EXECUTABLE}")
  167. string(REPLACE "/" "\\" PROPS_CMAKE_SOURCE_DIR "${CMAKE_SOURCE_DIR}")
  168. configure_file("../cmake/msvc/xs.wperl.props.in" "${CMAKE_BINARY_DIR}/xs.wperl.props" NEWLINE_STYLE CRLF)
  169. set_target_properties(XS PROPERTIES VS_USER_PROPS "${CMAKE_BINARY_DIR}/xs.wperl.props")
  170. endif()
  171. # Installation
  172. install(TARGETS XS DESTINATION ${PERL_VENDORARCH}/auto/Slic3r/XS)
  173. install(FILES lib/Slic3r/XS.pm DESTINATION ${PERL_VENDORLIB}/Slic3r)
  174. # Unit / integration tests
  175. enable_testing()
  176. get_filename_component(PERL_BIN_PATH "${PERL_EXECUTABLE}" DIRECTORY)
  177. if (MSVC)
  178. set(PERL_PROVE "${PERL_BIN_PATH}/prove.bat")
  179. else ()
  180. set(PERL_PROVE "${PERL_BIN_PATH}/prove")
  181. endif ()
  182. add_test (NAME xs COMMAND "${PERL_EXECUTABLE}" ${PERL_PROVE} -I ${PERL_LOCAL_LIB_DIR}/lib/perl5 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR})
  183. add_test (NAME integration COMMAND "${PERL_EXECUTABLE}" ${PERL_PROVE} WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/..)