unix.pl 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. #!/usr/local/bin/perl
  2. #
  3. # unix.pl - the standard unix makefile stuff.
  4. #
  5. $o='/';
  6. $cp='/bin/cp';
  7. $rm='/bin/rm -f';
  8. # C compiler stuff
  9. if ($gcc)
  10. {
  11. $cc='gcc';
  12. if ($debug)
  13. { $cflags="-g2 -ggdb"; }
  14. else
  15. { $cflags="-O3 -fomit-frame-pointer"; }
  16. }
  17. else
  18. {
  19. $cc='cc';
  20. if ($debug)
  21. { $cflags="-g"; }
  22. else
  23. { $cflags="-O"; }
  24. }
  25. $obj='.o';
  26. $asm_suffix='.s';
  27. $ofile='-o ';
  28. # EXE linking stuff
  29. $link='${CC}';
  30. $lflags='${CFLAG}';
  31. $efile='-o ';
  32. $exep='';
  33. $ex_libs="";
  34. # static library stuff
  35. $mklib='ar r';
  36. $mlflags='';
  37. $ranlib=&which("ranlib") or $ranlib="true";
  38. $plib='lib';
  39. $libp=".a";
  40. $shlibp=".a";
  41. $lfile='';
  42. $asm='as';
  43. $afile='-o ';
  44. $bn_asm_obj="";
  45. $bn_asm_src="";
  46. $des_enc_obj="";
  47. $des_enc_src="";
  48. $bf_enc_obj="";
  49. $bf_enc_src="";
  50. %perl1 = (
  51. 'md5-x86_64' => 'crypto/md5',
  52. 'x86_64-mont' => 'crypto/bn',
  53. 'x86_64-mont5' => 'crypto/bn',
  54. 'x86_64-gf2m' => 'crypto/bn',
  55. 'aes-x86_64' => 'crypto/aes',
  56. 'vpaes-x86_64' => 'crypto/aes',
  57. 'bsaes-x86_64' => 'crypto/aes',
  58. 'aesni-x86_64' => 'crypto/aes',
  59. 'aesni-sha1-x86_64' => 'crypto/aes',
  60. 'sha1-x86_64' => 'crypto/sha',
  61. 'e_padlock-x86_64' => 'engines',
  62. 'rc4-x86_64' => 'crypto/rc4',
  63. 'rc4-md5-x86_64' => 'crypto/rc4',
  64. 'ghash-x86_64' => 'crypto/modes',
  65. 'aesni-gcm-x86_64' => 'crypto/modes',
  66. 'aesni-sha256-x86_64' => 'crypto/aes',
  67. 'rsaz-x86_64' => 'crypto/bn',
  68. 'rsaz-avx2' => 'crypto/bn',
  69. 'aesni-mb-x86_64' => 'crypto/aes',
  70. 'sha1-mb-x86_64' => 'crypto/sha',
  71. 'sha256-mb-x86_64' => 'crypto/sha',
  72. 'ecp_nistz256-x86_64' => 'crypto/ec',
  73. );
  74. # If I were feeling more clever, these could probably be extracted
  75. # from makefiles.
  76. sub platform_perlasm_compile_target
  77. {
  78. local($target, $source, $bname) = @_;
  79. for $p (keys %perl1)
  80. {
  81. if ($target eq "\$(OBJ_D)/$p.o")
  82. {
  83. return << "EOF";
  84. \$(TMP_D)/$p.s: $perl1{$p}/asm/$p.pl
  85. \$(PERL) $perl1{$p}/asm/$p.pl \$(PERLASM_SCHEME) > \$@
  86. EOF
  87. }
  88. }
  89. if ($target eq '$(OBJ_D)/x86_64cpuid.o')
  90. {
  91. return << 'EOF';
  92. $(TMP_D)/x86_64cpuid.s: crypto/x86_64cpuid.pl
  93. $(PERL) crypto/x86_64cpuid.pl $(PERLASM_SCHEME) > $@
  94. EOF
  95. }
  96. elsif ($target eq '$(OBJ_D)/sha256-x86_64.o')
  97. {
  98. return << 'EOF';
  99. $(TMP_D)/sha256-x86_64.s: crypto/sha/asm/sha512-x86_64.pl
  100. $(PERL) crypto/sha/asm/sha512-x86_64.pl $(PERLASM_SCHEME) $@
  101. EOF
  102. }
  103. elsif ($target eq '$(OBJ_D)/sha512-x86_64.o')
  104. {
  105. return << 'EOF';
  106. $(TMP_D)/sha512-x86_64.s: crypto/sha/asm/sha512-x86_64.pl
  107. $(PERL) crypto/sha/asm/sha512-x86_64.pl $(PERLASM_SCHEME) $@
  108. EOF
  109. }
  110. elsif ($target eq '$(OBJ_D)/sha512-x86_64.o')
  111. {
  112. return << 'EOF';
  113. $(TMP_D)/sha512-x86_64.s: crypto/sha/asm/sha512-x86_64.pl
  114. $(PERL) crypto/sha/asm/sha512-x86_64.pl $(PERLASM_SCHEME) $@
  115. EOF
  116. }
  117. die $target;
  118. }
  119. sub special_compile_target
  120. {
  121. local($target) = @_;
  122. if ($target eq 'crypto/bn/x86_64-gcc')
  123. {
  124. return << "EOF";
  125. \$(TMP_D)/x86_64-gcc.o: crypto/bn/asm/x86_64-gcc.c
  126. \$(CC) \$(CFLAGS) -c -o \$@ crypto/bn/asm/x86_64-gcc.c
  127. EOF
  128. }
  129. return undef;
  130. }
  131. sub do_lib_rule
  132. {
  133. local($obj,$target,$name,$shlib)=@_;
  134. local($ret,$_,$Name);
  135. $target =~ s/\//$o/g if $o ne '/';
  136. $target="$target";
  137. ($Name=$name) =~ tr/a-z/A-Z/;
  138. $ret.="$target: \$(${Name}OBJ)\n";
  139. $ret.="\t\$(RM) $target\n";
  140. $ret.="\t\$(MKLIB) $target \$(${Name}OBJ)\n";
  141. $ret.="\t\$(RANLIB) $target\n\n";
  142. }
  143. sub do_link_rule
  144. {
  145. local($target,$files,$dep_libs,$libs)=@_;
  146. local($ret,$_);
  147. $file =~ s/\//$o/g if $o ne '/';
  148. $n=&bname($target);
  149. $ret.="$target: $files $dep_libs\n";
  150. $ret.="\t\$(LINK_CMD) ${efile}$target \$(LFLAGS) $files $libs\n\n";
  151. return($ret);
  152. }
  153. sub which
  154. {
  155. my ($name)=@_;
  156. my $path;
  157. foreach $path (split /:/, $ENV{PATH})
  158. {
  159. if (-x "$path/$name")
  160. {
  161. return "$path/$name";
  162. }
  163. }
  164. }
  165. sub fixtests
  166. {
  167. my ($str, $tests) = @_;
  168. foreach my $t (keys %$tests)
  169. {
  170. $str =~ s/(\.\/)?\$\($t\)/\$(TEST_D)\/$tests->{$t}/g;
  171. }
  172. return $str;
  173. }
  174. sub fixdeps
  175. {
  176. my ($str, $fakes) = @_;
  177. my @t = split(/\s+/, $str);
  178. $str = '';
  179. foreach my $t (@t)
  180. {
  181. $str .= ' ' if $str ne '';
  182. if (exists($fakes->{$t}))
  183. {
  184. $str .= $fakes->{$t};
  185. next;
  186. }
  187. if ($t =~ /^[^\/]+$/)
  188. {
  189. $str .= '$(TEST_D)/' . $t;
  190. }
  191. else
  192. {
  193. $str .= $t;
  194. }
  195. }
  196. return $str;
  197. }
  198. sub fixrules
  199. {
  200. my ($str) = @_;
  201. # Compatible with -j...
  202. $str =~ s/^(\s+@?)/$1cd \$(TEST_D) && /;
  203. return $str;
  204. # Compatible with not -j.
  205. my @t = split("\n", $str);
  206. $str = '';
  207. my $prev;
  208. foreach my $t (@t)
  209. {
  210. $t =~ s/^\s+//;
  211. if (!$prev)
  212. {
  213. if ($t =~ /^@/)
  214. {
  215. $t =~ s/^@/\@cd \$(TEST_D) && /;
  216. }
  217. elsif ($t !~ /^\s*#/)
  218. {
  219. $t = 'cd $(TEST_D) && ' . $t;
  220. }
  221. }
  222. $str .= "\t$t\n";
  223. $prev = $t =~/\\$/;
  224. }
  225. return $str;
  226. }
  227. sub copy_scripts
  228. {
  229. my ($sed, $src, @targets) = @_;
  230. my $s = '';
  231. foreach my $t (@targets)
  232. {
  233. # Copy first so we get file modes...
  234. $s .= "\$(TEST_D)/$t: \$(SRC_D)/$src/$t\n\tcp \$(SRC_D)/$src/$t \$(TEST_D)/$t\n";
  235. $s .= "\tsed -e 's/\\.\\.\\/apps/..\\/\$(OUT_D)/' -e 's/\\.\\.\\/util/..\\/\$(TEST_D)/' < \$(SRC_D)/$src/$t > \$(TEST_D)/$t\n" if $sed;
  236. $s .= "\n";
  237. }
  238. return $s;
  239. }
  240. sub get_tests
  241. {
  242. my ($makefile) = @_;
  243. open(M, $makefile) || die "Can't open $makefile: $!";
  244. my %targets;
  245. my %deps;
  246. my %tests;
  247. my %alltests;
  248. my %fakes;
  249. while (my $line = <M>)
  250. {
  251. chomp $line;
  252. while ($line =~ /^(.*)\\$/)
  253. {
  254. $line = $1 . <M>;
  255. }
  256. if ($line =~ /^alltests:(.*)$/)
  257. {
  258. my @t = split(/\s+/, $1);
  259. foreach my $t (@t)
  260. {
  261. $targets{$t} = '';
  262. $alltests{$t} = undef;
  263. }
  264. }
  265. if (($line =~ /^(?<t>\S+):(?<d>.*)$/ && exists $targets{$1})
  266. || $line =~ /^(?<t>test_(ss|gen) .*):(?<d>.*)/)
  267. {
  268. my $t = $+{t};
  269. my $d = $+{d};
  270. # If there are multiple targets stupid FreeBSD make runs the
  271. # rules once for each dependency that matches one of the
  272. # targets. Running the same rule twice concurrently causes
  273. # breakage, so replace with a fake target.
  274. if ($t =~ /\s/)
  275. {
  276. ++$fake;
  277. my @targets = split /\s+/, $t;
  278. $t = "_fake$fake";
  279. foreach my $f (@targets)
  280. {
  281. $fakes{$f} = $t;
  282. }
  283. }
  284. $deps{$t} = $d;
  285. $deps{$t} =~ s/#.*$//;
  286. for (;;)
  287. {
  288. $line = <M>;
  289. chomp $line;
  290. last if $line eq '';
  291. $targets{$t} .= "$line\n";
  292. }
  293. next;
  294. }
  295. if ($line =~ /^(\S+TEST)=\s*(\S+)$/)
  296. {
  297. $tests{$1} = $2;
  298. next;
  299. }
  300. }
  301. delete $alltests{test_jpake} if $no_jpake;
  302. delete $targets{test_ige} if $no_ige;
  303. delete $alltests{test_md2} if $no_md2;
  304. delete $alltests{test_rc5} if $no_rc5;
  305. my $tests;
  306. foreach my $t (keys %tests)
  307. {
  308. $tests .= "$t = $tests{$t}\n";
  309. }
  310. my $each;
  311. foreach my $t (keys %targets)
  312. {
  313. next if $t eq '';
  314. my $d = $deps{$t};
  315. $d =~ s/\.\.\/apps/\$(BIN_D)/g;
  316. $d =~ s/\.\.\/util/\$(TEST_D)/g;
  317. $d = fixtests($d, \%tests);
  318. $d = fixdeps($d, \%fakes);
  319. my $r = $targets{$t};
  320. $r =~ s/\.\.\/apps/..\/\$(BIN_D)/g;
  321. $r =~ s/\.\.\/util/..\/\$(TEST_D)/g;
  322. $r =~ s/\.\.\/(\S+)/\$(SRC_D)\/$1/g;
  323. $r = fixrules($r);
  324. next if $r eq '';
  325. $t =~ s/\s+/ \$(TEST_D)\//g;
  326. $each .= "$t: test_scripts $d\n\t\@echo '$t test started'\n$r\t\@echo '$t test done'\n\n";
  327. }
  328. # FIXME: Might be a clever way to figure out what needs copying
  329. my @copies = ( 'bctest',
  330. 'testgen',
  331. 'cms-test.pl',
  332. 'tx509',
  333. 'test.cnf',
  334. 'testenc',
  335. 'tocsp',
  336. 'testca',
  337. 'CAss.cnf',
  338. 'testtsa',
  339. 'CAtsa.cnf',
  340. 'Uss.cnf',
  341. 'P1ss.cnf',
  342. 'P2ss.cnf',
  343. 'tcrl',
  344. 'tsid',
  345. 'treq',
  346. 'tpkcs7',
  347. 'tpkcs7d',
  348. 'testcrl.pem',
  349. 'testx509.pem',
  350. 'v3-cert1.pem',
  351. 'v3-cert2.pem',
  352. 'testreq2.pem',
  353. 'testp7.pem',
  354. 'pkcs7-1.pem',
  355. 'trsa',
  356. 'testrsa.pem',
  357. 'testsid.pem',
  358. 'testss',
  359. 'testssl',
  360. 'testsslproxy',
  361. 'serverinfo.pem',
  362. );
  363. my $copies = copy_scripts(1, 'test', @copies);
  364. $copies .= copy_scripts(0, 'test', ('smcont.txt'));
  365. my @utils = ( 'shlib_wrap.sh',
  366. 'opensslwrap.sh',
  367. );
  368. $copies .= copy_scripts(1, 'util', @utils);
  369. my @apps = ( 'CA.sh',
  370. 'openssl.cnf',
  371. 'server2.pem',
  372. );
  373. $copies .= copy_scripts(1, 'apps', @apps);
  374. $copies .= copy_scripts(1, 'crypto/evp', ('evptests.txt'));
  375. $scripts = "test_scripts: \$(TEST_D)/CA.sh \$(TEST_D)/opensslwrap.sh \$(TEST_D)/openssl.cnf \$(TEST_D)/shlib_wrap.sh ocsp smime\n";
  376. $scripts .= "\nocsp:\n\tcp -R test/ocsp-tests \$(TEST_D)\n";
  377. $scripts .= "\smime:\n\tcp -R test/smime-certs \$(TEST_D)\n";
  378. my $all = 'test:';
  379. foreach my $t (keys %alltests)
  380. {
  381. if (exists($fakes{$t}))
  382. {
  383. $all .= " $fakes{$t}";
  384. }
  385. else
  386. {
  387. $all .= " $t";
  388. }
  389. }
  390. return "$scripts\n$copies\n$tests\n$all\n\n$each";
  391. }
  392. 1;