Build.PL 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. #!/usr/bin/perl -w
  2. use strict;
  3. use warnings;
  4. use Config;
  5. use Devel::CheckLib;
  6. use ExtUtils::CppGuess;
  7. use Module::Build::WithXSpp;
  8. my $cpp_guess = ExtUtils::CppGuess->new;
  9. my $mswin = $^O eq 'MSWin32';
  10. my $linux = $^O eq 'linux';
  11. # prevent an annoying concatenation warning by Devel::CheckLib
  12. $ENV{LD_RUN_PATH} //= "";
  13. # _GLIBCXX_USE_C99 : to get the long long type for g++
  14. # HAS_BOOL : stops Perl/lib/CORE/handy.h from doing "# define bool char" for MSVC
  15. # NOGDI : prevents inclusion of wingdi.h which defines functions Polygon() and Polyline() in global namespace
  16. # BOOST_ASIO_DISABLE_KQUEUE : prevents a Boost ASIO bug on OS X: https://svn.boost.org/trac/boost/ticket/5339
  17. my @cflags = qw(-D_GLIBCXX_USE_C99 -DHAS_BOOL -DNOGDI -DSLIC3RXS -DBOOST_ASIO_DISABLE_KQUEUE -Dexprtk_disable_rtl_io_file -Dexprtk_disable_return_statement -Dexprtk_disable_rtl_vecops -Dexprtk_disable_string_capabilities -Dexprtk_disable_enhanced_features);
  18. if ($cpp_guess->is_gcc) {
  19. # GCC is pedantic with c++11 std, so undefine strict ansi to get M_PI back
  20. push @cflags, qw(-U__STRICT_ANSI__);
  21. }
  22. # std=c++11 Enforce usage of C++11 (required now). Minimum compiler supported: gcc 4.9, clang 3.3, MSVC 14.0
  23. push @cflags, qw(-std=c++11);
  24. my @ldflags = ();
  25. if ($linux && (defined $ENV{SLIC3R_STATIC} && $ENV{SLIC3R_STATIC})) {
  26. push @ldflags, qw(-static-libgcc -static-libstdc++);
  27. if ($ENV{TRAVIS}) {
  28. # On the build server, link to the actual static libraries to make sure we get them in the list.
  29. push @ldflags, qw(/usr/lib/gcc/x86_64-linux-gnu/4.9/libstdc++.a /usr/lib/gcc/x86_64-linux-gnu/4.9/libgcc.a);
  30. }
  31. # ExtUtils::CppGuess has a hard-coded -lstdc++, so we filter it out
  32. {
  33. no strict 'refs';
  34. no warnings 'redefine';
  35. my $func = "ExtUtils::CppGuess::_get_lflags";
  36. my $orig = *$func{CODE};
  37. *{$func} = sub {
  38. my $lflags = $orig->(@_);
  39. $lflags =~ s/\s*-lstdc\+\+//;
  40. return $lflags;
  41. };
  42. }
  43. }
  44. if ($^O eq 'darwin') {
  45. push @cflags, qw(-stdlib=libc++);
  46. push @ldflags, qw(-framework IOKit -framework CoreFoundation -lc++);
  47. # Due to a bug/misconfiguration/stupidity, boost 1.52 and libc++ don't like each
  48. # other much: a compilation error "Constexpr function never produces a constant
  49. # expression" pops up when trying to compile anything that uses
  50. # boost/chrono/duration.hpp (namely boost/thread for us). This is a workaround
  51. # that prevents this from happening, not needed with newer Boost versions.
  52. # See here for more details: https://svn.boost.org/trac/boost/ticket/7671
  53. push @cflags, qw(-DBOOST_THREAD_DONT_USE_CHRONO -DBOOST_NO_CXX11_RVALUE_REFERENCES -DBOOST_THREAD_USES_MOVE);
  54. # ExtUtils::CppGuess has a hard-coded -lstdc++, so we filter it out
  55. {
  56. no strict 'refs';
  57. no warnings 'redefine';
  58. my $func = "ExtUtils::CppGuess::_get_lflags";
  59. my $orig = *$func{CODE};
  60. *{$func} = sub {
  61. my $lflags = $orig->(@_);
  62. $lflags =~ s/\s*-lstdc\+\+//;
  63. return $lflags;
  64. };
  65. }
  66. }
  67. if ($mswin) {
  68. # In case windows.h is included, we don't want the min / max macros to be active.
  69. # If <math.h> is included, we want the #defines to be active (M_PI etc.)
  70. push @cflags, qw(-DNOMINMAX -D_USE_MATH_DEFINES);
  71. }
  72. my @INC = qw(-Isrc/libslic3r);
  73. my @LIBS = $cpp_guess->is_msvc ? qw(-LIBPATH:src/libslic3r) : qw(-Lsrc/libslic3r);
  74. # search for Boost in a number of places
  75. my @boost_include = ();
  76. if (defined $ENV{BOOST_INCLUDEDIR}) {
  77. push @boost_include, $ENV{BOOST_INCLUDEDIR}
  78. } elsif (defined $ENV{BOOST_DIR}) {
  79. # User might have provided a path relative to the Build.PL in the main directory
  80. foreach my $subdir ($ENV{BOOST_DIR}, "../$ENV{BOOST_DIR}", "$ENV{BOOST_DIR}/include", "../$ENV{BOOST_DIR}/include") {
  81. if (-d $subdir) {
  82. push @boost_include, $subdir;
  83. }
  84. }
  85. die "Invalid BOOST_DIR: no such directory\n" if !@boost_include;
  86. } else {
  87. # Boost library was not defined by the environment.
  88. # Try to guess at some default paths.
  89. if ($mswin) {
  90. for my $path (glob('C:\dev\boost*\include'), glob ('C:\boost*\include')) {
  91. push @boost_include, $path;
  92. }
  93. if (! @boost_include) {
  94. # No boost\include. Try to include the boost root.
  95. for my $path (glob('C:\dev\boost*'), glob ('C:\boost*')) {
  96. push @boost_include, $path;
  97. }
  98. }
  99. } else {
  100. push @boost_include, grep { -d $_ }
  101. qw(/opt/local/include /usr/local/include /opt/include /usr/include);
  102. }
  103. }
  104. my @boost_libs = ();
  105. if (defined $ENV{BOOST_LIBRARYPATH}) {
  106. push @boost_libs, $ENV{BOOST_LIBRARYPATH}
  107. } elsif (defined $ENV{BOOST_DIR}) {
  108. # User might have provided a path relative to the Build.PL in the main directory
  109. foreach my $subdir ("$ENV{BOOST_DIR}/stage/lib", "../$ENV{BOOST_DIR}/stage/lib") {
  110. if (-d $subdir) {
  111. push @boost_libs, $subdir;
  112. }
  113. }
  114. die "Invalid BOOST_DIR: no such directory\n" if !@boost_libs;
  115. } else {
  116. # Boost library was not defined by the environment.
  117. # Try to guess at some default paths.
  118. if ($mswin) {
  119. for my $path (
  120. glob('C:\dev\boost*\lib'), glob ('C:\boost*\lib'),
  121. glob('C:\dev\boost*\stage\lib'), glob ('C:\boost*\stage\lib')) {
  122. push @boost_libs, $path;
  123. }
  124. } else {
  125. push @boost_libs, grep { -d $_ }
  126. qw(/opt/local/lib /usr/local/lib /opt/lib /usr/lib /lib);
  127. }
  128. }
  129. # In order to generate the -l switches we need to know how Boost libraries are named
  130. my $have_boost = 0;
  131. my @boost_libraries = qw(system thread filesystem); # we need these
  132. # check without explicit lib path (works on Linux)
  133. if (! $mswin) {
  134. $have_boost = 1
  135. if check_lib(
  136. lib => [ map "boost_${_}", @boost_libraries ],
  137. );
  138. }
  139. if (!$ENV{SLIC3R_STATIC} && $have_boost) {
  140. # The boost library was detected by check_lib on Linux.
  141. push @LIBS, map "-lboost_${_}", @boost_libraries;
  142. } else {
  143. # Either static linking, or check_lib could not be used to find the boost libraries.
  144. my $lib_prefix = 'libboost_';
  145. my $lib_ext = $Config{lib_ext};
  146. PATH: foreach my $path (@boost_libs) {
  147. # Try to find the boost system library.
  148. my @files = glob "$path/${lib_prefix}system*$lib_ext";
  149. next if !@files;
  150. if ($files[0] =~ /${lib_prefix}system([^.]*)$lib_ext$/) {
  151. # Suffix contains the version number, the build type etc.
  152. my $suffix = $1;
  153. # Verify existence of all required boost libraries at $path.
  154. for my $lib (map "${lib_prefix}${_}${suffix}${lib_ext}", @boost_libraries) {
  155. # If the library file does not exist, try next library path.
  156. -f "$path/$lib" or next PATH;
  157. }
  158. if (! $cpp_guess->is_msvc) {
  159. # Test the correctness of boost libraries by linking them to a minimal C program.
  160. check_lib(
  161. lib => [ map "boost_${_}${suffix}", @boost_libraries ],
  162. INC => join(' ', map "-I$_", @INC, @boost_include),
  163. LIBS => "-L$path",
  164. ) or next;
  165. }
  166. push @INC, (map " -I$_", @boost_include); # TODO: only use the one related to the chosen lib path
  167. if ($ENV{SLIC3R_STATIC} || $cpp_guess->is_msvc) {
  168. push @LIBS, map "${path}/${lib_prefix}$_${suffix}${lib_ext}", @boost_libraries;
  169. } else {
  170. push @LIBS, " -L$path", (map " -lboost_$_$suffix", @boost_libraries);
  171. }
  172. $have_boost = 1;
  173. last;
  174. }
  175. }
  176. }
  177. push @cflags, '-DBOOST_LIBS' if $have_boost;
  178. die <<'EOF' if !$have_boost;
  179. Slic3r requires the Boost libraries. Please make sure they are installed.
  180. If they are installed, this script should be able to locate them in several
  181. standard locations. If this is not the case, you might want to supply their
  182. path through the BOOST_INCLUDEPATH and BOOST_LIBRARYPATH environment variables:
  183. BOOST_INCLUDEPATH=/usr/local/include BOOST_LIBRARYPATH=/usr/lib perl Build.PL
  184. If you just compiled Boost in its source directory without installing it in the
  185. system you can just provide the BOOST_DIR variable pointing to that directory.
  186. EOF
  187. if ($ENV{SLIC3R_DEBUG}) {
  188. # only on newer GCCs: -ftemplate-backtrace-limit=0
  189. push @cflags, '-DSLIC3R_DEBUG';
  190. push @cflags, $cpp_guess->is_msvc ? '-Gd' : '-g';
  191. } else {
  192. # Disable asserts in the release builds.
  193. push @cflags, '-DNDEBUG', '-O';
  194. }
  195. if ($cpp_guess->is_gcc) {
  196. # our templated XS bindings cause undefined-var-template warnings
  197. push @cflags, qw(-Wno-undefined-var-template);
  198. }
  199. my $build = Module::Build::WithXSpp->new(
  200. module_name => 'Slic3r::XS',
  201. dist_abstract => 'XS code for Slic3r',
  202. build_requires => {qw(
  203. ExtUtils::ParseXS 3.35
  204. ExtUtils::Typemaps 1.00
  205. ExtUtils::Typemaps::Default 1.05
  206. ExtUtils::XSpp 0.18
  207. Module::Build 0.3601
  208. Test::More 0
  209. )},
  210. configure_requires => {qw(
  211. ExtUtils::CppGuess 0.07
  212. Module::Build 0.38
  213. Module::Build::WithXSpp 0.13
  214. )},
  215. extra_compiler_flags => [ @INC, @cflags ],
  216. extra_linker_flags => [ @LIBS, @ldflags ],
  217. # Provides extra C typemaps that are auto-merged
  218. extra_typemap_modules => {
  219. 'ExtUtils::Typemaps::Basic' => '1.05',
  220. },
  221. # for MSVC builds
  222. early_includes => [qw(
  223. cstring
  224. cstdlib
  225. ostream
  226. sstream
  227. libslic3r/GCodeSender.hpp
  228. )]
  229. );
  230. $build->create_build_script;
  231. __END__