build.rs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. //! Build script for Rust modules in Tor.
  2. //!
  3. //! We need to use this because some of our Rust tests need to use some
  4. //! of our C modules, which need to link some external libraries.
  5. //!
  6. //! This script works by looking at a "config.rust" file generated by our
  7. //! configure script, and then building a set of options for cargo to pass to
  8. //! the compiler.
  9. use std::collections::HashMap;
  10. use std::env;
  11. use std::fs::File;
  12. use std::io::prelude::*;
  13. use std::io;
  14. use std::path::PathBuf;
  15. /// Wrapper around a key-value map.
  16. struct Config(
  17. HashMap<String,String>
  18. );
  19. /// Locate a config.rust file generated by autoconf, starting in the OUT_DIR
  20. /// location provided by cargo and recursing up the directory tree. Note that
  21. /// we need to look in the OUT_DIR, since autoconf will place generated files
  22. /// in the build directory.
  23. fn find_cfg() -> io::Result<String> {
  24. let mut path = PathBuf::from(env::var("OUT_DIR").unwrap());
  25. loop {
  26. path.push("config.rust");
  27. if path.exists() {
  28. return Ok(path.to_str().unwrap().to_owned());
  29. }
  30. path.pop(); // remove config.rust
  31. if ! path.pop() { // can't remove last part of directory
  32. return Err(io::Error::new(io::ErrorKind::NotFound,
  33. "No config.rust"));
  34. }
  35. }
  36. }
  37. impl Config {
  38. /// Find the config.rust file and try to parse it.
  39. ///
  40. /// The file format is a series of lines of the form KEY=VAL, with
  41. /// any blank lines and lines starting with # ignored.
  42. fn load() -> io::Result<Config> {
  43. let path = find_cfg()?;
  44. let f = File::open(&path)?;
  45. let reader = io::BufReader::new(f);
  46. let mut map = HashMap::new();
  47. for line in reader.lines() {
  48. let s = line?;
  49. if s.trim().starts_with("#") || s.trim() == "" {
  50. continue;
  51. }
  52. let idx = match s.find("=") {
  53. None => {
  54. return Err(io::Error::new(io::ErrorKind::InvalidData,
  55. "missing ="));
  56. },
  57. Some(x) => x
  58. };
  59. let (var,eq_val) = s.split_at(idx);
  60. let val = &eq_val[1..];
  61. map.insert(var.to_owned(), val.to_owned());
  62. }
  63. Ok(Config(map))
  64. }
  65. /// Return a reference to the value whose key is 'key'.
  66. ///
  67. /// Panics if 'key' is not found in the configuration.
  68. fn get(&self, key : &str) -> &str {
  69. self.0.get(key).unwrap()
  70. }
  71. /// Add a dependency on a static C library that is part of Tor, by name.
  72. fn component(&self, s : &str) {
  73. println!("cargo:rustc-link-lib=static={}", s);
  74. }
  75. /// Add a dependency on a native library that is not part of Tor, by name.
  76. fn dependency(&self, s : &str) {
  77. println!("cargo:rustc-link-lib={}", s);
  78. }
  79. /// Add a link path, relative to Tor's build directory.
  80. fn link_relpath(&self, s : &str) {
  81. let builddir = self.get("BUILDDIR");
  82. println!("cargo:rustc-link-search=native={}/{}", builddir, s);
  83. }
  84. /// Add an absolute link path.
  85. fn link_path(&self, s : &str) {
  86. println!("cargo:rustc-link-search=native={}", s);
  87. }
  88. /// Parse the CFLAGS in s, looking for -l and -L items, and adding
  89. /// rust configuration as appropriate.
  90. fn from_cflags(&self, s : &str) {
  91. let mut next_is_lib = false;
  92. let mut next_is_path = false;
  93. for ent in self.get(s).split_whitespace() {
  94. if next_is_lib {
  95. self.dependency(ent);
  96. next_is_lib = false;
  97. } else if next_is_path {
  98. self.link_path(ent);
  99. next_is_path = false;
  100. } else if ent == "-l" {
  101. next_is_lib = true;
  102. } else if ent == "-L" {
  103. next_is_path = true;
  104. } else if ent.starts_with("-L") {
  105. self.link_path(&ent[2..]);
  106. } else if ent.starts_with("-l") {
  107. self.dependency(&ent[2..]);
  108. }
  109. }
  110. }
  111. }
  112. pub fn main() {
  113. let cfg = Config::load().unwrap();
  114. let package = env::var("CARGO_PKG_NAME").unwrap();
  115. match package.as_ref() {
  116. "crypto" => {
  117. // Right now, I'm having a separate configuration for each Rust
  118. // package, since I'm hoping we can trim them down. Once we have a
  119. // second Rust package that needs to use this build script, let's
  120. // extract some of this stuff into a module.
  121. //
  122. // This is a ridiculous amount of code to be pulling in just
  123. // to test our crypto library: modularity would be our
  124. // friend here.
  125. cfg.from_cflags("TOR_LDFLAGS_zlib");
  126. cfg.from_cflags("TOR_LDFLAGS_openssl");
  127. cfg.from_cflags("TOR_LDFLAGS_libevent");
  128. cfg.link_relpath("src/lib");
  129. cfg.link_relpath("src/common");
  130. cfg.link_relpath("src/ext/keccak-tiny");
  131. cfg.link_relpath("src/ext/ed25519/ref10");
  132. cfg.link_relpath("src/ext/ed25519/donna");
  133. cfg.link_relpath("src/trunnel");
  134. // Note that we can't pull in "libtor-testing", or else we
  135. // will have dependencies on all the other rust packages that
  136. // tor uses. We must be careful with factoring and dependencies
  137. // moving forward!
  138. cfg.component("tor-crypt-ops-testing");
  139. cfg.component("or-testing");
  140. cfg.component("tor-sandbox");
  141. cfg.component("tor-encoding-testing");
  142. cfg.component("tor-net");
  143. cfg.component("tor-thread-testing");
  144. cfg.component("tor-memarea-testing");
  145. cfg.component("tor-log");
  146. cfg.component("tor-lock");
  147. cfg.component("tor-fdio");
  148. cfg.component("tor-container-testing");
  149. cfg.component("tor-smartlist-core-testing");
  150. cfg.component("tor-string-testing");
  151. cfg.component("tor-malloc");
  152. cfg.component("tor-wallclock");
  153. cfg.component("tor-err-testing");
  154. cfg.component("or-event-testing");
  155. cfg.component("tor-intmath-testing");
  156. cfg.component("tor-ctime-testing");
  157. cfg.component("curve25519_donna");
  158. cfg.component("keccak-tiny");
  159. cfg.component("ed25519_ref10");
  160. cfg.component("ed25519_donna");
  161. cfg.component("or-trunnel-testing");
  162. cfg.from_cflags("TOR_ZLIB_LIBS");
  163. cfg.from_cflags("TOR_LIB_MATH");
  164. cfg.from_cflags("TOR_OPENSSL_LIBS");
  165. cfg.from_cflags("TOR_LIBEVENT_LIBS");
  166. cfg.from_cflags("TOR_LIB_WS32");
  167. cfg.from_cflags("TOR_LIB_GDI");
  168. cfg.from_cflags("TOR_LIB_USERENV");
  169. cfg.from_cflags("CURVE25519_LIBS");
  170. cfg.from_cflags("TOR_LZMA_LIBS");
  171. cfg.from_cflags("TOR_ZSTD_LIBS");
  172. cfg.from_cflags("LIBS");
  173. },
  174. _ => {
  175. panic!("No configuration in build.rs for package {}", package);
  176. }
  177. }
  178. }