| 1 | // Build long LLVM and Mesa targets on one shared worker. |
| 2 | module main |
| 3 | |
| 4 | import os |
| 5 | import strconv |
| 6 | import time |
| 7 | |
| 8 | #include <errno.h> |
| 9 | #include <sys/file.h> |
| 10 | |
| 11 | fn C.flock(int, int) int |
| 12 | |
| 13 | fn lock(fd int, operation int) ! { |
| 14 | for C.flock(fd, operation) != 0 { |
| 15 | if C.errno != C.EINTR { |
| 16 | return os.last_error() |
| 17 | } |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | fn build(ninja string, directory string, targets []string) ! { |
| 22 | mut child := os.new_process(ninja) |
| 23 | mut args := ['-C', directory, '-j2'] |
| 24 | args << targets |
| 25 | child.set_args(args) |
| 26 | child.run() |
| 27 | child.wait() |
| 28 | defer { child.close() } |
| 29 | if child.code != 0 { |
| 30 | return error('Ninja exited with status ${child.code}') |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | fn wait_for_space(fd int, directory string, minimum_mib u64) ! { |
| 35 | if minimum_mib == 0 { |
| 36 | return |
| 37 | } |
| 38 | mut reported := false |
| 39 | for { |
| 40 | // The bootstrap V's os.disk_usage takes ownership of its argument. |
| 41 | disk := os.disk_usage(directory.clone())! |
| 42 | available_mib := disk.available / (1024 * 1024) |
| 43 | if available_mib >= minimum_mib { |
| 44 | return |
| 45 | } |
| 46 | if !reported { |
| 47 | eprintln('Waiting for ${minimum_mib} MiB of free space in ${directory}; ${available_mib} MiB available.') |
| 48 | reported = true |
| 49 | } |
| 50 | lock(fd, C.LOCK_UN)! |
| 51 | time.sleep(30 * time.second) |
| 52 | lock(fd, C.LOCK_EX)! |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | fn run(directory string, targets []string) ! { |
| 57 | ninja := os.find_abs_path_of_executable('ninja')! |
| 58 | shared := os.getenv('VINIX_BUILD_LOCK') |
| 59 | if shared == '' { |
| 60 | build(ninja, directory, targets)! |
| 61 | return |
| 62 | } |
| 63 | fd := strconv.atoi(os.getenv('VINIX_BUILD_LOCK_FD'))! |
| 64 | if fd < 3 || !os.is_abs_path(shared) { |
| 65 | return error('set VINIX_BUILD_LOCK to an absolute file and VINIX_BUILD_LOCK_FD to its inherited open descriptor') |
| 66 | } |
| 67 | want := os.stat(shared)! |
| 68 | have := os.stat('/proc/self/fd/${fd}')! |
| 69 | if want.dev != have.dev || want.inode != have.inode { |
| 70 | return error('inherited descriptor ${fd} does not refer to ${shared}') |
| 71 | } |
| 72 | seconds := strconv.atoi(os.getenv_opt('VINIX_BUILD_SLICE_SECONDS') or { '180' })! |
| 73 | if seconds < 1 { |
| 74 | return error('VINIX_BUILD_SLICE_SECONDS must be positive') |
| 75 | } |
| 76 | minimum_mib := strconv.parse_uint(os.getenv_opt('VINIX_BUILD_MIN_FREE_MIB') or { '0' }, 10, 64)! |
| 77 | lock(fd, C.LOCK_EX)! |
| 78 | mut query := [ninja, '-C', directory, '-t', 'inputs'] |
| 79 | query << targets |
| 80 | inputs := os.exec(query) |
| 81 | if inputs.exit_code != 0 { |
| 82 | return error('Ninja could not resolve the requested build graph: ${inputs.output}') |
| 83 | } |
| 84 | // Use Ninja's dependency graph. Each batch finishes all prerequisite |
| 85 | // generators and compiler invocations before another job gets the lock. |
| 86 | objects := inputs.output.split_into_lines().filter(it.ends_with('.o')) |
| 87 | mut since := time.sys_mono_now() |
| 88 | for first := 0; first < objects.len; first += 8 { |
| 89 | wait_for_space(fd, directory, minimum_mib)! |
| 90 | end := if first + 8 < objects.len { first + 8 } else { objects.len } |
| 91 | build(ninja, directory, objects[first..end])! |
| 92 | if end < objects.len && time.sys_mono_now() - since >= u64(seconds) * 1_000_000_000 { |
| 93 | println('Completed ${end}/${objects.len} object targets; yielding the worker lock.') |
| 94 | lock(fd, C.LOCK_UN)! |
| 95 | time.sleep(250 * time.millisecond) |
| 96 | lock(fd, C.LOCK_EX)! |
| 97 | since = time.sys_mono_now() |
| 98 | } |
| 99 | } |
| 100 | // Ninja rechecks every dependency and performs the requested final links. |
| 101 | // Its caller still owns the same locked file description for packaging. |
| 102 | wait_for_space(fd, directory, minimum_mib)! |
| 103 | build(ninja, directory, targets)! |
| 104 | } |
| 105 | |
| 106 | fn main() { |
| 107 | if os.args.len < 3 { |
| 108 | eprintln('usage: vinix-ninja BUILD_DIRECTORY TARGET ...') |
| 109 | exit(2) |
| 110 | } |
| 111 | run(os.args[1], os.args[2..]) or { |
| 112 | eprintln(err) |
| 113 | exit(1) |
| 114 | } |
| 115 | } |
| 116 | |