From d44eabe24733992d7ccdb4de593cdf28245d5650 Mon Sep 17 00:00:00 2001 From: spaceface777 Date: Tue, 22 Sep 2020 16:41:32 +0200 Subject: [PATCH] doctor: detect if running in a VM / WSL / chroot (#6447) --- .github/workflows/ci.yml | 9 ++++++++ cmd/tools/vdoctor.v | 47 +++++++++++++++++++++++++++++++++++----- 2 files changed, 50 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e36f98d483..462a9630bd 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -49,6 +49,9 @@ jobs: run: ./v -o v2 cmd/v && ./v2 -o v3 cmd/v && ./v3 -o v4 cmd/v - name: Test building v tools run: ./v build-tools + - name: v doctor + run: | + ./v doctor - name: v vet run: | ./v vet vlib/sqlite @@ -127,6 +130,9 @@ jobs: run: VJOBS=1 ./v test-fixed - name: Build examples run: ./v build-examples + - name: v doctor + run: | + ./v doctor - name: v vet run: | ./v vet vlib/sqlite @@ -342,6 +348,9 @@ jobs: run: | ./v -cg cmd\tools\vtest-fixed.v ./v test-fixed + - name: v doctor + run: | + ./v doctor # - name: Test # run: | # .\v.exe -silent test-compiler diff --git a/cmd/tools/vdoctor.v b/cmd/tools/vdoctor.v index a44c723306..a738b3c7a4 100644 --- a/cmd/tools/vdoctor.v +++ b/cmd/tools/vdoctor.v @@ -6,7 +6,8 @@ import runtime struct App { mut: - report_lines []string + report_lines []string + cached_cpuinfo map[string]string } fn (mut a App) println(s string) { @@ -33,12 +34,11 @@ fn (mut a App) collect_info() { arch_details << a.cmd(command:'sysctl -n machdep.cpu.brand_string') } if os_kind == 'linux' { - mname := a.cmd(command:'grep "model name" /proc/cpuinfo | sed "s/.*: //gm"') - if !mname.starts_with('Error:') { - arch_details << mname + info := a.cpu_info() + if info['model name'] != '' { + arch_details << info['model name'] } else { - hinfo := a.cmd(command:'grep "Hardware" /proc/cpuinfo | sed "s/.*: //gm"') - arch_details << hinfo + arch_details << info['hardware'] } } if os_kind == 'windows' { @@ -48,6 +48,18 @@ fn (mut a App) collect_info() { mut os_details := '' if os_kind == 'linux' { os_details = a.get_linux_os_name() + info := a.cpu_info() + if 'hypervisor' in info['flags'] { + if 'microsoft' in a.cmd(command: 'cat /proc/sys/kernel/osrelease') { + os_details += ' (WSL)' + } else { + os_details += ' (VM)' + } + } + // From https://unix.stackexchange.com/a/14346 + if a.cmd(command: '[ "$(awk \'\$5=="/" {print \$1}\' 0 { + return a.cached_cpuinfo + } + + info := os.exec('cat /proc/cpuinfo') or { return a.cached_cpuinfo } + mut vals := map[string]string + for line in info.output.split_into_lines() { + sline := line.trim(' ') + if sline.len < 1 || sline[0] == `#`{ + continue + } + x := sline.split(':') + if x.len < 2 { + continue + } + vals[x[0].trim_space().to_lower()] = x[1].trim_space().trim('"') + } + + a.cached_cpuinfo = vals + return vals +} + fn (mut a App) report_tcc_version(tccfolder string) { if !os.is_file(os.join_path(tccfolder, '.git', 'config')) { a.line(tccfolder, 'N/A')