sord is a C++ library that wraps around the operating system API for reading directories. It is cross-platform and thread-safe.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

configure.pl 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. #! /usr/bin/perl -w
  2. ################################################################################
  3. # Copyright (C) 2001-2006 Peter J Jones (pjones@pmade.org)
  4. # All Rights Reserved
  5. #
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions
  8. # are met:
  9. #
  10. # 1. Redistributions of source code must retain the above copyright
  11. # notice, this list of conditions and the following disclaimer.
  12. # 2. Redistributions in binary form must reproduce the above copyright
  13. # notice, this list of conditions and the following disclaimer in
  14. # the documentation and/or other materials provided with the
  15. # distribution.
  16. # 3. Neither the name of the Author nor the names of its contributors
  17. # may be used to endorse or promote products derived from this software
  18. # without specific prior written permission.
  19. #
  20. # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
  21. # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  22. # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  23. # PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
  24. # OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  27. # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  28. # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  30. # OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. # SUCH DAMAGE.
  32. ################################################################################
  33. #
  34. # configure.pl (bootstrap the sord library)
  35. # Peter J Jones (pjones@pmade.org)
  36. #
  37. ################################################################################
  38. #
  39. # Includes
  40. #
  41. ################################################################################
  42. use strict;
  43. use Getopt::Long;
  44. use Cwd qw(chdir cwd);
  45. ################################################################################
  46. #
  47. # Constants
  48. #
  49. ################################################################################
  50. use constant DATE => 'Wed Oct 24 13:46:58 2001';
  51. use constant ID => '$Id: configure.pl,v 1.1.1.1 2003/08/04 04:50:42 pjones Exp $';
  52. ################################################################################
  53. #
  54. # Global Variables
  55. #
  56. ################################################################################
  57. my $lib_name = "sord";
  58. my $source_files;
  59. my $cwd = cwd();
  60. my $dirsep = '/';
  61. if ($^O =~ /mswin/i) {
  62. $dirsep = '\\';
  63. }
  64. my $mkmf = "'$cwd${dirsep}tools${dirsep}mkmf'";
  65. my $cxxflags = "'$cwd${dirsep}tools${dirsep}cxxflags'";
  66. my $genconfig = "'$cwd${dirsep}tools${dirsep}genconfig'";
  67. my $include_dir = "$cwd${dirsep}include";
  68. my $src_dir = "$cwd${dirsep}src";
  69. my $test_dir = "$cwd${dirsep}tests";
  70. my $build_test_dir = "$test_dir${dirsep}build";
  71. my $test_out = "$build_test_dir${dirsep}test.out";
  72. my $install_spec = "docs${dirsep}/install.spec";
  73. my $version_file = "docs${dirsep}/VERSION";
  74. my $config_script = "sord-config";
  75. my $build_test = "$^X build_test.pl";
  76. my %build_settings;
  77. my $config = "";
  78. my $using_readdir_r = 0;
  79. my $mkmf_flags = "--cxxflags $cxxflags --quiet";
  80. my $wrapper_flags = "";
  81. my $major_number = 1;
  82. my $version_number = 1;
  83. my %clo;
  84. my @common_files = qw(
  85. directory.cxx
  86. directory_iterator.cxx
  87. entry.cxx
  88. entry_common.cxx
  89. utility.cxx
  90. );
  91. my @unix_files = qw(
  92. directory_unix.cxx
  93. entry_unix.cxx
  94. );
  95. my @windows_files = qw(
  96. directory_win32.cxx
  97. entry_win32.cxx
  98. );
  99. my @test_directories = qw(
  100. entry
  101. directory
  102. );
  103. my @wrap_directories = qw(
  104. src
  105. );
  106. ################################################################################
  107. #
  108. # Code Start
  109. #
  110. ################################################################################
  111. $|++;
  112. GetOptions(
  113. \%clo,
  114. 'help|h!',
  115. 'developer!',
  116. 'force-readdir!',
  117. 'enable-tests!',
  118. 'contrib!',
  119. 'disable-shared!',
  120. 'prefix=s',
  121. 'libdir=s',
  122. 'bindir=s',
  123. 'incdir=s',
  124. ) || usage();
  125. $clo{'help'} && usage();
  126. sub usage {
  127. print "Usage: $0 [options]\n", <<EOT;
  128. --force-readdir Don't use readdir_r even if your system supports it
  129. --enable-tests Enable building of the sord test harness
  130. --developer Configure for use by a sord maintainer
  131. --contrib Configure for bundling
  132. --disable-shared Don't build a shared library
  133. --prefix path Set the installation prefix to path [default /usr/local]
  134. --incdir path Set the include install dir. [default prefix/include]
  135. --bindir path Set the bin install dir. [default prefix/bin]
  136. --libdir path Set the lib install dir. [default prefix/lib]
  137. EOT
  138. exit 1;
  139. }
  140. if (not exists $ENV{'CXX'}) {
  141. if ($^O =~ /mswin/i) {
  142. $ENV{'CXX'} = 'cl.exe';
  143. } else {
  144. $ENV{'CXX'} = "c++";
  145. }
  146. print "$0: Your CXX environment variable is not set.\n";
  147. print "$0: Using $ENV{CXX} to compile C++ files.\n\n";
  148. }
  149. $ENV{'CXXFLAGS'} ||= '';
  150. $clo{'prefix'} ||= "${dirsep}usr${dirsep}local";
  151. $clo{'bindir'} ||= "$clo{'prefix'}${dirsep}bin";
  152. $clo{'incdir'} ||= "$clo{'prefix'}${dirsep}include";
  153. $clo{'libdir'} ||= "$clo{'prefix'}${dirsep}lib";
  154. $clo{'disable-shared'} = 1 if $clo{'contrib'};
  155. if ($^O =~ /mswin/i) {
  156. $ENV{'CXXFLAGS'} .= " -DSORD_WIN32";
  157. $source_files = join(' ', @common_files, @windows_files);
  158. } else {
  159. $ENV{'CXXFLAGS'} .= " -DSORD_UNIX";
  160. $source_files = join(' ', @common_files, @unix_files);
  161. }
  162. unless (chdir($build_test_dir)) {
  163. print STDERR "$0: can't chdir into $build_test_dir: $!\n";
  164. exit 1;
  165. }
  166. if (system($build_test) != 0) {
  167. print STDERR "$0: build test failed. Please check it out\n";
  168. }
  169. chdir($cwd);
  170. if (open(CF, $test_out)) {
  171. while (<CF>) {
  172. clean($_);
  173. my ($key, $value) = split(/\s*=\s*/, $_, 2);
  174. $build_settings{$key} = $value;
  175. }
  176. close CF;
  177. } else {
  178. print STDERR "$0: error opening build test output: $!\n";
  179. }
  180. clean($ENV{CXX});
  181. clean($ENV{CXXFLAGS});
  182. $config .= "sord configuration:\n";
  183. $config .= " Compiler: $ENV{CXX}\n";
  184. $config .= " Compiler Flags: $ENV{CXXFLAGS}\n";
  185. if (exists $build_settings{'readdir_r'}
  186. && $build_settings{'readdir_r'} eq 'yes'
  187. && not $clo{'force-readdir'})
  188. {
  189. $config .= " Using readdir_r: Yes\n";
  190. $ENV{'CXXFLAGS'} .= " -DSORD_USE_READDIR_R";
  191. $using_readdir_r = 1;
  192. } else {
  193. $config .= " Using readdir_r: No\n";
  194. $using_readdir_r = 0;
  195. }
  196. if ($clo{'developer'}) {
  197. $mkmf_flags .= " --developer";
  198. $config .= " Developer Mode: On\n";
  199. $config .= " Source Files: $source_files\n";
  200. }
  201. $config .= " bin Install Dir: $clo{'bindir'}\n";
  202. $config .= " inc Install Dir: $clo{'incdir'}\n";
  203. $config .= " lib Install Dir: $clo{'libdir'}\n";
  204. if ($using_readdir_r) {
  205. $mkmf_flags .= " --mt";
  206. }
  207. parse_version_file();
  208. gen_install_spec();
  209. gen_config_script();
  210. if ($clo{'enable-tests'}) {
  211. push @wrap_directories, "tests";
  212. $wrapper_flags .= " --with-test tests";
  213. gen_test_makefiles();
  214. }
  215. unless (chdir($src_dir)) {
  216. print STDERR "$0: can't chdir into src: $!\n";
  217. exit 1;
  218. }
  219. my $src_flags = '';
  220. unless ($clo{'disable-shared'}) {
  221. $src_flags = "--shared-lib $lib_name --major $major_number";
  222. }
  223. print "Creating Makefile in src...\n";
  224. system("$^X $mkmf $mkmf_flags --include '$include_dir' $src_flags --static-lib $lib_name $source_files");
  225. chdir($cwd);
  226. print "Creating Makefile...\n";
  227. system("$^X $mkmf $mkmf_flags --install $install_spec $wrapper_flags --wrapper @wrap_directories");
  228. if (not $clo{'contrib'}) {
  229. print "\n$config\n";
  230. }
  231. ################################################################################
  232. sub gen_test_makefiles {
  233. if (!chdir($test_dir)) {
  234. print STDERR "$0: can't chdir to $test_dir: $!\n";
  235. exit 1;
  236. }
  237. print "Creating Makefile in tests...\n";
  238. my $test_dirs = join(',', @test_directories);
  239. system("$^X $mkmf $mkmf_flags --with-test '$test_dirs' --wrapper @test_directories");
  240. foreach my $dir (@test_directories) {
  241. if (!chdir("$test_dir${dirsep}$dir")) {
  242. print STDERR "$0: can't chdir to $test_dir${dirsep}$dir: $!\n";
  243. exit 1;
  244. }
  245. my @scripts = glob("*.pl");
  246. my $test_cmds = join(',', map{"$^X -I'$test_dir${dirsep}utility' $_"} @scripts);
  247. print "Creating Makefile in tests${dirsep}$dir...\n";
  248. system("$^X $mkmf $mkmf_flags --include '$include_dir' --many-exec --slinkwith '$src_dir,$lib_name' --test-cmds \"$test_cmds\" *.cxx");
  249. }
  250. chdir($cwd);
  251. }
  252. ################################################################################
  253. sub gen_install_spec {
  254. unless (open(IS, ">$install_spec")) {
  255. print STDERR "$0: can't create $install_spec: $!\n";
  256. exit 1;
  257. }
  258. print IS "BINDIR=$clo{'bindir'}\n";
  259. print IS "INCLUDEDIR=$clo{'incdir'}\n";
  260. print IS "LIBDIR=$clo{'libdir'}\n";
  261. print IS "include-dir include${dirsep}sord sord\n";
  262. print IS "static-lib src $lib_name\n";
  263. print IS "shared-lib src $lib_name $major_number\n" unless $clo{'disable-shared'};
  264. print IS "binary $config_script\n";
  265. close IS;
  266. }
  267. ################################################################################
  268. sub parse_version_file {
  269. unless (open(VF, $version_file)) {
  270. print STDERR "$0: can't open $version_file: $!\n";
  271. exit 1;
  272. }
  273. my $first_line = <VF>;
  274. close VF;
  275. if ($first_line =~ /^([\d.]+)\s+(\d+)/) {
  276. $version_number = $1;
  277. $major_number = $2;
  278. } else {
  279. print STDERR "$0: warning, $version_file is malformed\n";
  280. }
  281. }
  282. ################################################################################
  283. sub gen_config_script {
  284. my $libs = "-L$clo{'libdir'} -l$lib_name";
  285. my $flags = "-I$clo{'incdir'}";
  286. if ($clo{'contrib'}) {
  287. $libs = "$cwd/src/libsord.a";
  288. $flags = "-I$cwd/include";
  289. }
  290. s/\b-/^/g foreach ($libs, $flags);
  291. system("$^X $genconfig -o $config_script -v $version_number --libs '$libs' --cxxflags '$flags' --name $lib_name");
  292. }
  293. ################################################################################
  294. sub clean {
  295. chomp($_[0]);
  296. $_[0] =~ s/^\s+//;
  297. $_[0] =~ s/\s+$//;
  298. }
  299. ################################################################################