11#!/bin/ env v
22
33import time
4+ import runtime
5+ import flag
6+ import os
7+
8+ struct Config {
9+ jobs int @[long: cpus; short: 'j']
10+ }
411
512const ignore_dirs = {
613 'windows': [
@@ -41,12 +48,10 @@ const ignore_dirs = {
4148
4249unbuffer_stdout()
4350
44- dump(user_os())
45- dump(ignore_dirs)
46-
47- args : = arguments()
48- vargs : = if args.len > 1 { args[1 ..] } else { [] }
49- dump(vargs)
51+ config, remaining : = flag.to_struct[Config](os.args, skip: 1 )!
52+ mut jobs : = if config.jobs > 0 { config.jobs } else { runtime.nr_cpus() }
53+ mut vargs : = remaining.clone()
54+ println('Using ${jobs} parallel jobs')
5055
5156curdir : = getwd()
5257chdir('src')!
@@ -60,6 +65,8 @@ if !exists('${curdir}/bin') {
6065sw_total : = time.new_stopwatch()
6166mut compiled : = 0
6267mut already_compiled : = 0
68+ mut dirs_to_compile : = []string{}
69+
6370for dir in dirs {
6471 if dir in ignore_dirs {
6572 continue
@@ -85,16 +92,48 @@ for dir in dirs {
8592 }
8693 }
8794
88- mut final_args : = '- Wimpure- v'
89- for arg in vargs {
90- final_args += ' ' + arg
95+ dirs_to_compile << dir
96+ }
97+
98+ // Now compile in parallel
99+ ch : = chan bool {cap: jobs}
100+ results_ch : = chan bool {cap: dirs_to_compile.len}
101+ print_ch : = chan string{cap: 100 }
102+
103+ // Start printer thread, avoiding garbled text when building
104+ spawn fn [print_ch] () {
105+ for {
106+ msg : = <-print_ch or { break }
107+ print(msg)
91108 }
92- print('compiling ${dir: - 20s}...')
93- cmd : = @VEXE + ' ${final_args} - o ${curdir}/ bin/ ${dir} ./${dir}'
94- sw : = time.new_stopwatch()
95- execute_or_panic(cmd)
96- println(' took ${sw.elapsed().milliseconds()}ms .')
97- compiled++
109+ }()
110+
111+ for dir in dirs_to_compile {
112+ // Acquire the current slot
113+ ch <- true
114+ spawn fn [ch, results_ch, dir, curdir, vargs, print_ch] () {
115+ defer {
116+ results_ch <- true
117+ // Released
118+ _ : = <-ch
119+ }
120+ mut final_args : = '- Wimpure- v'
121+ for arg in vargs {
122+ final_args += ' ' + arg
123+ }
124+ cmd : = @VEXE + ' ${final_args} - o "${curdir}/ bin/ ${dir}" "./${dir}"'
125+ sw : = time.new_stopwatch()
126+ execute_or_panic(cmd)
127+ print_ch <- 'compiling ${dir: - 20s}... took ${sw.elapsed().milliseconds()}ms .\n'
128+ }()
98129}
130+
131+ // Wait for all compilations to finish
132+ for _ in 0 .. dirs_to_compile.len {
133+ _ : = <-results_ch
134+ }
135+
136+ print_ch.close()
137+ compiled = dirs_to_compile.len
99138println('> Compiled: ${compiled: 3 } tools in ${sw_total.elapsed().milliseconds()}ms. Already compiled and skipped: ${already_compiled} . All folders: ${dirs.len} .')
100139chdir(curdir)!
0 commit comments