tor

The Tor anonymity network
git clone https://git.dasho.dev/tor.git
Log | Files | Refs | README | LICENSE

build-nix.php (3641B)


      1 <?php
      2 function echoln($str) {
      3 	echo $str;
      4 	echo "\n";
      5 }
      6 
      7 function usage($reason) {
      8 	echoln("Usage: php build-nix.php [flags]");
      9 	echoln("Flags in parentheses are optional");
     10 	echoln("");
     11 	echoln("  --bits=[32,64]");
     12 	echoln("  --function=[curve25519,ed25519]");
     13 	echoln(" (--compiler=[*gcc,clang,icc])        which compiler to use, gcc is default");
     14 	echoln(" (--with-openssl)                     use openssl for SHA512");
     15 	echoln(" (--with-sse2)                        additionally fuzz against SSE2");
     16 	echoln(" (--no-asm)                           don't use platform specific asm");
     17 	echoln("");
     18 	if ($reason)
     19 		echoln($reason);
     20 }
     21 
     22 function cleanup() {
     23 	system("rm -f *.o");
     24 }
     25 
     26 function runcmd($desc, $cmd) {
     27 	echoln($desc);
     28 
     29 	$ret = 0;
     30 	system($cmd, $ret);
     31 	if ($ret) {
     32 		cleanup();
     33 		exit;
     34 	}
     35 }
     36 
     37 class argument {
     38 	var $set, $value;
     39 }
     40 
     41 class multiargument extends argument {
     42 	function multiargument($flag, $legal_values) {
     43 		global $argc, $argv;
     44 
     45 		$this->set = false;
     46 
     47 		$map = array();
     48 		foreach($legal_values as $value)
     49 			$map[$value] = true;
     50 
     51 		for ($i = 1; $i < $argc; $i++) {
     52 			if (!preg_match("!--".$flag."=(.*)!", $argv[$i], $m))
     53 				continue;
     54 			if (isset($map[$m[1]])) {
     55 				$this->value = $m[1];
     56 				$this->set = true;
     57 				return;
     58 			} else {
     59 				usage("{$m[1]} is not a valid parameter to --{$flag}!");
     60 				exit(1);
     61 			}
     62 		}
     63 	}
     64 }
     65 
     66 class flag extends argument {
     67 	function flag($flag) {
     68 		global $argc, $argv;
     69 
     70 		$this->set = false;
     71 
     72 		$flag = "--{$flag}";
     73 		for ($i = 1; $i < $argc; $i++) {
     74 			if ($argv[$i] !== $flag)
     75 				continue;
     76 			$this->value = true;
     77 			$this->set = true;
     78 			return;
     79 		}
     80 	}
     81 }
     82 
     83 $bits = new multiargument("bits", array("32", "64"));
     84 $function = new multiargument("function", array("curve25519", "ed25519"));
     85 $compiler = new multiargument("compiler", array("gcc", "clang", "icc"));
     86 $with_sse2 = new flag("with-sse2");
     87 $with_openssl = new flag("with-openssl");
     88 $no_asm = new flag("no-asm");
     89 
     90 $err = "";
     91 if (!$bits->set)
     92 	$err .= "--bits not set\n";
     93 if (!$function->set)
     94 	$err .= "--function not set\n";
     95 
     96 if ($err !== "") {
     97 	usage($err);
     98 	exit;
     99 }
    100 
    101 $compile = ($compiler->set) ? $compiler->value : "gcc";
    102 $link = "";
    103 $flags = "-O3 -m{$bits->value}";
    104 $ret = 0;
    105 
    106 if ($with_openssl->set) $link .= " -lssl -lcrypto";
    107 if (!$with_openssl->set) $flags .= " -DED25519_REFHASH -DED25519_TEST";
    108 if ($no_asm->set) $flags .= " -DED25519_NO_INLINE_ASM";
    109 
    110 if ($function->value === "curve25519") {
    111 	runcmd("building ref10..", "{$compile} {$flags} curve25519-ref10.c -c -o curve25519-ref10.o");
    112 	runcmd("building ed25519..", "{$compile} {$flags} ed25519-donna.c -c -o ed25519.o");
    113 	if ($with_sse2->set) {
    114 		runcmd("building ed25519-sse2..", "{$compile} {$flags} ed25519-donna-sse2.c -c -o ed25519-sse2.o -msse2");
    115 		$flags .= " -DED25519_SSE2";
    116 		$link .= " ed25519-sse2.o";
    117 	}
    118 	runcmd("linking..", "{$compile} {$flags} {$link} fuzz-curve25519.c ed25519.o curve25519-ref10.o -o fuzz-curve25519");
    119 	echoln("fuzz-curve25519 built.");
    120 } else 	if ($function->value === "ed25519") {
    121 	runcmd("building ref10..", "{$compile} {$flags} ed25519-ref10.c -c -o ed25519-ref10.o");
    122 	runcmd("building ed25519..", "{$compile} {$flags} ed25519-donna.c -c -o ed25519.o");
    123 	if ($with_sse2->set) {
    124 		runcmd("building ed25519-sse2..", "{$compile} {$flags} ed25519-donna-sse2.c -c -o ed25519-sse2.o -msse2");
    125 		$flags .= " -DED25519_SSE2";
    126 		$link .= " ed25519-sse2.o";
    127 	}
    128 	runcmd("linking..", "{$compile} {$flags} {$link} fuzz-ed25519.c ed25519.o ed25519-ref10.o -o fuzz-ed25519");
    129 	echoln("fuzz-ed25519 built.");
    130 }
    131 
    132 
    133 cleanup();
    134 ?>