Build.PL 9.9 KB

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