build-nix.php 3.6 KB

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