rustrunner.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/bin/env python3
  2. # Copyright 2014 The Meson development team
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. # Unless required by applicable law or agreed to in writing, software
  8. # distributed under the License is distributed on an "AS IS" BASIS,
  9. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. # See the License for the specific language governing permissions and
  11. # limitations under the License.
  12. """This is a wrapper script to run the Rust compiler. It is needed
  13. because:
  14. - output file name of Rust compilation is not knowable at command
  15. execution time (no, --crate-name can't be used)
  16. - need to delete old crates so nobody uses them by accident
  17. """
  18. import sys, os, subprocess, glob
  19. def delete_old_crates(target_name, target_type):
  20. if target_type == 'dylib':
  21. (base, suffix) = os.path.splitext(target_name)
  22. crates = glob.glob(base + '-*' + suffix)
  23. crates = [(os.stat(i).st_mtime, i) for i in crates]
  24. [os.unlink(c[1]) for c in sorted(crates)[:-1]]
  25. if target_type == 'lib':
  26. (base, suffix) = os.path.splitext(target_name)
  27. crates = glob.glob(base + '-*' + '.rlib') # Rust does not use .a
  28. crates = [(os.stat(i).st_mtime, i) for i in crates]
  29. [os.unlink(c[1]) for c in sorted(crates)[:-1]]
  30. def invoke_rust(rustc_command):
  31. return subprocess.call(rustc_command, shell=False)
  32. def touch_file(fname):
  33. try:
  34. os.unlink(fname)
  35. except FileNotFoundError:
  36. pass
  37. open(fname, 'w').close()
  38. if __name__ == '__main__':
  39. if len(sys.argv) < 3:
  40. print('This script is internal to Meson. Do not run it on its own.')
  41. print("%s <target name> <target type> <rustc invokation cmd line>")
  42. sys.exit(1)
  43. target_name = sys.argv[1]
  44. target_type = sys.argv[2]
  45. rustc_command = sys.argv[3:]
  46. retval = invoke_rust(rustc_command)
  47. if retval != 0:
  48. sys.exit(retval)
  49. if target_type != "bin":
  50. delete_old_crates(target_name, target_type)
  51. touch_file(target_name)