CMakeLists.txt 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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}/Config.xsp
  41. ${XSP_DIR}/ExPolygon.xsp
  42. ${XSP_DIR}/Geometry.xsp
  43. ${XSP_DIR}/Line.xsp
  44. ${XSP_DIR}/Model.xsp
  45. ${XSP_DIR}/Point.xsp
  46. ${XSP_DIR}/Polygon.xsp
  47. ${XSP_DIR}/Polyline.xsp
  48. ${XSP_DIR}/Print.xsp
  49. ${XSP_DIR}/TriangleMesh.xsp
  50. ${XSP_DIR}/XS.xsp
  51. )
  52. foreach (file ${XS_XSP_FILES})
  53. if (MSVC)
  54. # Visual Studio C compiler has issues with FILE pragmas containing quotes.
  55. set(INCLUDE_COMMANDS "${INCLUDE_COMMANDS}INCLUDE_COMMAND: $^X -MExtUtils::XSpp::Cmd -e xspp -- -t ${CMAKE_CURRENT_LIST_DIR}/xsp/typemap.xspt ${file}\n")
  56. else ()
  57. set(INCLUDE_COMMANDS "${INCLUDE_COMMANDS}INCLUDE_COMMAND: $^X -MExtUtils::XSpp::Cmd -e xspp -- -t \"${CMAKE_CURRENT_LIST_DIR}/xsp/typemap.xspt\" \"${file}\"\n")
  58. endif ()
  59. endforeach ()
  60. configure_file(main.xs.in ${XS_MAIN_XS} @ONLY) # Insert INCLUDE_COMMANDS into main.xs
  61. # Generate the Slic3r Perl module (XS) XS.cpp file.
  62. #FIXME add the dependency on main.xs and typemap.
  63. set(XS_MAIN_CPP ${CMAKE_CURRENT_BINARY_DIR}/XS.cpp)
  64. add_custom_command(
  65. OUTPUT ${XS_MAIN_CPP}
  66. DEPENDS ${MyTypemap} ${XS_XSP_FILES} ${CMAKE_CURRENT_LIST_DIR}/xsp/typemap.xspt
  67. COMMAND ${PERL5LIB_ENV_CMD} xsubpp -typemap typemap -output ${XS_MAIN_CPP} -hiertype ${XS_MAIN_XS}
  68. VERBATIM
  69. )
  70. # Define the Perl XS shared library.
  71. if(APPLE)
  72. set(XS_SHARED_LIBRARY_TYPE MODULE)
  73. else()
  74. set(XS_SHARED_LIBRARY_TYPE SHARED)
  75. endif()
  76. add_library(XS ${XS_SHARED_LIBRARY_TYPE}
  77. ${XS_MAIN_CPP}
  78. src/perlglue.cpp
  79. src/ppport.h
  80. src/xsinit.h
  81. xsp/my.map
  82. # mytype.map is empty. Is it required by Build.PL or the Perl xspp module?
  83. xsp/mytype.map
  84. # Used by Perl xsubpp to generate XS.cpp
  85. xsp/typemap.xspt
  86. )
  87. if(APPLE)
  88. set_target_properties(XS PROPERTIES BUNDLE TRUE)
  89. # Ignore undefined symbols of the perl interpreter, they will be found in the caller image.
  90. target_link_libraries(XS "-undefined dynamic_lookup")
  91. endif()
  92. target_link_libraries(XS libslic3r)
  93. target_include_directories(XS PRIVATE src ${LIBDIR}/libslic3r)
  94. target_compile_definitions(XS PRIVATE -DSLIC3RXS)
  95. set_target_properties(XS PROPERTIES PREFIX "") # Prevent cmake from generating libXS.so instead of XS.so
  96. if (APPLE)
  97. # -liconv: boost links to libiconv by default
  98. target_link_libraries(XS "-liconv -framework IOKit" "-framework CoreFoundation" -lc++)
  99. elseif (MSVC)
  100. target_link_libraries(XS )
  101. else ()
  102. target_link_libraries(XS -lstdc++)
  103. endif ()
  104. # Windows specific stuff
  105. if (WIN32)
  106. target_compile_definitions(XS PRIVATE -DNOGDI -DNOMINMAX -DHAS_BOOL)
  107. endif ()
  108. # SLIC3R_MSVC_PDB
  109. if (MSVC AND SLIC3R_MSVC_PDB AND "${CMAKE_BUILD_TYPE}" STREQUAL "Release")
  110. set_target_properties(XS PROPERTIES
  111. COMPILE_FLAGS "/Zi"
  112. LINK_FLAGS "/DEBUG /OPT:REF /OPT:ICF"
  113. )
  114. endif()
  115. if (CMAKE_BUILD_TYPE MATCHES DEBUG)
  116. target_compile_definitions(XS PRIVATE -DSLIC3R_DEBUG -DDEBUG -D_DEBUG)
  117. else ()
  118. target_compile_definitions(XS PRIVATE -DNDEBUG)
  119. endif ()
  120. target_include_directories(XS PRIVATE ${PERL_INCLUDE_PATH})
  121. target_compile_options(XS PRIVATE ${PerlEmbed_CCFLAGS})
  122. if (WIN32)
  123. target_link_libraries(XS ${PERL_LIBRARY})
  124. endif()
  125. set(PERL_LOCAL_LIB_ARCH_DIR "${PERL_LOCAL_LIB_DIR}/lib/perl5/${PerlEmbed_ARCHNAME}")
  126. add_custom_command(
  127. TARGET XS
  128. POST_BUILD
  129. COMMAND ${CMAKE_COMMAND} -E make_directory "${PERL_LOCAL_LIB_ARCH_DIR}/auto/Slic3r/XS/"
  130. COMMAND ${CMAKE_COMMAND} -E copy "$<TARGET_FILE:XS>" "${PERL_LOCAL_LIB_ARCH_DIR}/auto/Slic3r/XS/"
  131. COMMAND ${CMAKE_COMMAND} -E make_directory "${PERL_LOCAL_LIB_ARCH_DIR}/Slic3r/"
  132. COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_CURRENT_SOURCE_DIR}/lib/Slic3r/XS.pm" "${PERL_LOCAL_LIB_ARCH_DIR}/Slic3r/"
  133. COMMENT "Installing XS.pm and XS.{so,dll,bundle} into the local-lib directory ..."
  134. )
  135. if(APPLE)
  136. add_custom_command(
  137. TARGET XS
  138. POST_BUILD
  139. COMMAND ${CMAKE_COMMAND} -E rename "${PERL_LOCAL_LIB_ARCH_DIR}/auto/Slic3r/XS/XS" "${PERL_LOCAL_LIB_ARCH_DIR}/auto/Slic3r/XS/XS.bundle"
  140. )
  141. endif()
  142. if (MSVC)
  143. # Here we associate some additional properties with the MSVC project to enable compilation and debugging out of the box.
  144. get_filename_component(PROPS_PERL_BIN_PATH "${PERL_EXECUTABLE}" DIRECTORY)
  145. string(REPLACE "/" "\\" PROPS_PERL_BIN_PATH "${PROPS_PERL_BIN_PATH}")
  146. string(REPLACE "/" "\\" PROPS_PERL_EXECUTABLE "${PERL_EXECUTABLE}")
  147. string(REPLACE "/" "\\" PROPS_CMAKE_SOURCE_DIR "${CMAKE_SOURCE_DIR}")
  148. configure_file("../cmake/msvc/xs.wperl.props.in" "${CMAKE_BINARY_DIR}/xs.wperl.props" NEWLINE_STYLE CRLF)
  149. set_target_properties(XS PROPERTIES VS_USER_PROPS "${CMAKE_BINARY_DIR}/xs.wperl.props")
  150. if ("${CMAKE_SIZEOF_VOID_P}" STREQUAL "8")
  151. set(_bits 64)
  152. elseif ("${CMAKE_SIZEOF_VOID_P}" STREQUAL "4")
  153. set(_bits 32)
  154. endif ()
  155. add_custom_command(TARGET XS POST_BUILD
  156. 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/"
  157. COMMENT "Installing gmp runtime into the local-lib directory ..."
  158. VERBATIM)
  159. add_custom_command(TARGET XS POST_BUILD
  160. 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/"
  161. COMMENT "Installing mpfr runtime into the local-lib directory ..."
  162. VERBATIM)
  163. endif()
  164. # Installation
  165. install(TARGETS XS DESTINATION ${PERL_VENDORARCH}/auto/Slic3r/XS)
  166. install(FILES lib/Slic3r/XS.pm DESTINATION ${PERL_VENDORLIB}/Slic3r)
  167. # Unit / integration tests
  168. enable_testing()
  169. get_filename_component(PERL_BIN_PATH "${PERL_EXECUTABLE}" DIRECTORY)
  170. if (MSVC)
  171. set(PERL_PROVE "${PERL_BIN_PATH}/prove.bat")
  172. else ()
  173. set(PERL_PROVE "${PERL_BIN_PATH}/prove")
  174. endif ()
  175. set(PERL_ENV_VARS "")
  176. if (CMAKE_SYSTEM_NAME STREQUAL "Linux" AND NOT CMAKE_CROSSCOMPILING AND ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang"))
  177. if (SLIC3R_ASAN OR SLIC3R_UBSAN)
  178. set(PERL_ENV_VARS env)
  179. endif ()
  180. if (SLIC3R_ASAN)
  181. # Find the location of libasan.so for passing it into LD_PRELOAD. It works with GCC and Clang on Linux.
  182. # On Centos 7 calling "gcc -print-file-name=libasan.so" returns path to "ld script" instead of path to shared library.
  183. set(_asan_compiled_bin ${CMAKE_CURRENT_BINARY_DIR}/detect_libasan)
  184. set(_asan_source_file ${_asan_compiled_bin}.c)
  185. # Compile and link simple C application with enabled address sanitizer.
  186. file(WRITE ${_asan_source_file} "int main(){}")
  187. include(GetPrerequisites)
  188. execute_process(COMMAND ${CMAKE_C_COMPILER} ${_asan_source_file} -fsanitize=address -lasan -o ${_asan_compiled_bin})
  189. # Extract from the compiled application absolute path of libasan.
  190. get_prerequisites(${_asan_compiled_bin} _asan_shared_libraries_list 0 0 "" "")
  191. list(FILTER _asan_shared_libraries_list INCLUDE REGEX libasan)
  192. set(PERL_ENV_VARS ${PERL_ENV_VARS} "LD_PRELOAD=${_asan_shared_libraries_list}")
  193. # Suppressed memory leak reports that come from Perl.
  194. set(PERL_LEAK_SUPPRESSION_FILE ${CMAKE_CURRENT_BINARY_DIR}/leak_suppression.txt)
  195. file(WRITE ${PERL_LEAK_SUPPRESSION_FILE}
  196. "leak:Perl_safesysmalloc\n"
  197. "leak:Perl_safesyscalloc\n"
  198. "leak:Perl_safesysrealloc\n"
  199. "leak:__newlocale\n")
  200. # Suppress a few memory leak reports and disable informing about suppressions.
  201. # Print reports about memory leaks but exit with zero exit code when any memory leaks is found to make unit tests pass.
  202. set(PERL_ENV_VARS ${PERL_ENV_VARS} "LSAN_OPTIONS=suppressions=${PERL_LEAK_SUPPRESSION_FILE}:print_suppressions=0:exitcode=0")
  203. endif ()
  204. if (SLIC3R_UBSAN)
  205. # Do not show full stacktrace for reports from UndefinedBehaviorSanitizer in Perl tests.
  206. set(PERL_ENV_VARS ${PERL_ENV_VARS} "UBSAN_OPTIONS=print_stacktrace=0")
  207. endif ()
  208. endif ()
  209. add_test (NAME xs COMMAND ${PERL_ENV_VARS} "${PERL_EXECUTABLE}" ${PERL_PROVE} -I ${PERL_LOCAL_LIB_DIR}/lib/perl5 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR})
  210. add_test (NAME integration COMMAND ${PERL_ENV_VARS} "${PERL_EXECUTABLE}" ${PERL_PROVE} WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/..)