2020-04-02 13:31:02 +02:00
|
|
|
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
|
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
module util
|
|
|
|
|
2020-04-26 07:35:59 +02:00
|
|
|
import os
|
|
|
|
import v.pref
|
2020-04-02 13:31:02 +02:00
|
|
|
|
|
|
|
pub const (
|
2020-05-06 02:26:43 +02:00
|
|
|
v_version = '0.1.27'
|
2020-04-02 13:31:02 +02:00
|
|
|
)
|
|
|
|
|
2020-04-26 07:35:59 +02:00
|
|
|
// math.bits is needed by strconv.ftoa
|
2020-04-16 11:29:36 +02:00
|
|
|
pub const (
|
2020-04-26 07:35:59 +02:00
|
|
|
builtin_module_parts = ['math.bits', 'strconv', 'strconv.ftoa', 'hash.wyhash', 'strings']
|
2020-04-16 11:29:36 +02:00
|
|
|
)
|
|
|
|
|
2020-04-02 13:31:02 +02:00
|
|
|
// vhash() returns the build string C.V_COMMIT_HASH . See cmd/tools/gen_vc.v .
|
|
|
|
pub fn vhash() string {
|
|
|
|
mut buf := [50]byte
|
|
|
|
buf[0] = 0
|
|
|
|
C.snprintf(charptr(buf), 50, '%s', C.V_COMMIT_HASH)
|
|
|
|
return tos_clone(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn full_hash() string {
|
|
|
|
build_hash := vhash()
|
|
|
|
current_hash := githash(false)
|
2020-04-05 13:47:01 +02:00
|
|
|
if build_hash == current_hash {
|
2020-04-08 22:12:42 +02:00
|
|
|
return build_hash
|
2020-04-05 13:47:01 +02:00
|
|
|
}
|
2020-04-02 15:51:48 +02:00
|
|
|
return '${build_hash}.${current_hash}'
|
2020-04-02 13:31:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// full_v_version() returns the full version of the V compiler
|
2020-05-16 23:52:03 +02:00
|
|
|
pub fn full_v_version(is_verbose bool) string {
|
|
|
|
if is_verbose {
|
|
|
|
return 'V ${v_version} ${full_hash()}'
|
|
|
|
}
|
|
|
|
hash := githash(false)
|
|
|
|
return 'V ${v_version} $hash'
|
2020-04-02 13:31:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// githash(x) returns the current git commit hash.
|
|
|
|
// When x is false, it is very fast - it just returns a predefined C constant.
|
|
|
|
// When x is true, it tries to get the current commit hash, by parsing the
|
|
|
|
// relevant files in the .git/ folder, or if that is not possible
|
|
|
|
// for example when using a V from a V binary release, that does not have .git/
|
|
|
|
// defaults to getting the predefined C constant again.
|
|
|
|
// NB: githash(true) must be called only when v detects that it builds itself.
|
|
|
|
// For all other programs, githash(false) should be used.
|
|
|
|
pub fn githash(should_get_from_filesystem bool) string {
|
2020-04-26 07:35:59 +02:00
|
|
|
for {
|
2020-04-02 13:31:02 +02:00
|
|
|
// The `for` construct here is used as a goto substitute.
|
|
|
|
// The code in this function will break out of the `for`
|
|
|
|
// if it detects an error and can not continue.
|
|
|
|
if should_get_from_filesystem {
|
|
|
|
vexe := os.getenv('VEXE')
|
|
|
|
vroot := os.dir(vexe)
|
|
|
|
// .git/HEAD
|
|
|
|
git_head_file := os.join_path(vroot, '.git', 'HEAD')
|
2020-04-08 22:12:42 +02:00
|
|
|
if !os.exists(git_head_file) {
|
2020-04-02 13:31:02 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
// 'ref: refs/heads/master' ... the current branch name
|
|
|
|
head_content := os.read_file(git_head_file) or {
|
|
|
|
break
|
|
|
|
}
|
2020-05-10 17:56:19 +02:00
|
|
|
mut current_branch_hash := head_content
|
|
|
|
if head_content.starts_with('ref: ') {
|
|
|
|
gcbranch_rel_path := head_content.replace('ref: ', '').trim_space()
|
|
|
|
gcbranch_file := os.join_path(vroot, '.git', gcbranch_rel_path)
|
|
|
|
// .git/refs/heads/master
|
|
|
|
if !os.exists(gcbranch_file) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// get the full commit hash contained in the ref heads file
|
|
|
|
branch_hash := os.read_file(gcbranch_file) or {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
current_branch_hash = branch_hash
|
2020-04-02 13:31:02 +02:00
|
|
|
}
|
|
|
|
desired_hash_length := 7
|
|
|
|
if current_branch_hash.len > desired_hash_length {
|
|
|
|
return current_branch_hash[0..desired_hash_length]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
2020-04-02 15:51:48 +02:00
|
|
|
mut buf := [50]byte
|
|
|
|
buf[0] = 0
|
|
|
|
C.snprintf(charptr(buf), 50, '%s', C.V_CURRENT_COMMIT_HASH)
|
|
|
|
return tos_clone(buf)
|
2020-04-02 13:31:02 +02:00
|
|
|
}
|
2020-04-03 17:38:41 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
fn set_vroot_folder(vroot_path string) {
|
|
|
|
// Preparation for the compiler module:
|
|
|
|
// VEXE env variable is needed so that compiler.vexe_path()
|
|
|
|
// can return it later to whoever needs it:
|
|
|
|
vname := if os.user_os() == 'windows' { 'v.exe' } else { 'v' }
|
2020-04-08 22:12:42 +02:00
|
|
|
os.setenv('VEXE', os.real_path(os.join_path(vroot_path, vname)), true)
|
2020-04-03 17:38:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn launch_tool(is_verbose bool, tool_name string) {
|
|
|
|
vexe := pref.vexe_path()
|
|
|
|
vroot := os.dir(vexe)
|
|
|
|
set_vroot_folder(vroot)
|
2020-05-14 08:07:14 +02:00
|
|
|
tool_args := args_quote_paths_with_spaces(os.args[1..])
|
2020-04-03 17:38:41 +02:00
|
|
|
tool_exe := path_of_executable(os.real_path('$vroot/cmd/tools/$tool_name'))
|
|
|
|
tool_source := os.real_path('$vroot/cmd/tools/${tool_name}.v')
|
|
|
|
tool_command := '"$tool_exe" $tool_args'
|
|
|
|
if is_verbose {
|
2020-04-05 14:31:01 +02:00
|
|
|
println('launch_tool vexe : $vroot')
|
|
|
|
println('launch_tool vroot : $vroot')
|
|
|
|
println('launch_tool tool_args : $tool_args')
|
|
|
|
println('launch_tool tool_command: $tool_command')
|
2020-04-03 17:38:41 +02:00
|
|
|
}
|
|
|
|
// TODO Caching should be done on the `vlib/v` level.
|
|
|
|
mut should_compile := false
|
|
|
|
if !os.exists(tool_exe) {
|
|
|
|
should_compile = true
|
2020-05-10 17:56:19 +02:00
|
|
|
} else {
|
2020-04-03 17:38:41 +02:00
|
|
|
if os.file_last_mod_unix(tool_exe) <= os.file_last_mod_unix(vexe) {
|
|
|
|
// v was recompiled, maybe after v up ...
|
|
|
|
// rebuild the tool too just in case
|
|
|
|
should_compile = true
|
|
|
|
if tool_name == 'vself' || tool_name == 'vup' {
|
|
|
|
// The purpose of vself/up is to update and recompile v itself.
|
|
|
|
// After the first 'v self' execution, v will be modified, so
|
|
|
|
// then a second 'v self' will detect, that v is newer than the
|
|
|
|
// vself executable, and try to recompile vself/up again, which
|
|
|
|
// will slow down the next v recompilation needlessly.
|
|
|
|
should_compile = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if os.file_last_mod_unix(tool_exe) <= os.file_last_mod_unix(tool_source) {
|
|
|
|
// the user changed the source code of the tool, or git updated it:
|
|
|
|
should_compile = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if is_verbose {
|
2020-04-05 14:31:01 +02:00
|
|
|
println('launch_tool should_compile: $should_compile')
|
2020-04-03 17:38:41 +02:00
|
|
|
}
|
|
|
|
if should_compile {
|
|
|
|
mut compilation_command := '"$vexe" '
|
|
|
|
compilation_command += '"$tool_source"'
|
|
|
|
if is_verbose {
|
2020-04-05 14:31:01 +02:00
|
|
|
println('Compiling $tool_name with: "$compilation_command"')
|
2020-04-03 17:38:41 +02:00
|
|
|
}
|
2020-04-08 22:12:42 +02:00
|
|
|
tool_compilation := os.exec(compilation_command) or {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-04-03 17:38:41 +02:00
|
|
|
if tool_compilation.exit_code != 0 {
|
2020-04-07 16:56:33 +02:00
|
|
|
mut err := 'Permission denied'
|
|
|
|
if !tool_compilation.output.contains('Permission denied') {
|
|
|
|
err = '\n$tool_compilation.output'
|
|
|
|
}
|
2020-05-06 08:10:40 +02:00
|
|
|
eprintln('cannot compile `$tool_source`: $err')
|
2020-04-07 16:56:33 +02:00
|
|
|
exit(1)
|
2020-04-03 17:38:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if is_verbose {
|
2020-04-05 14:31:01 +02:00
|
|
|
println('launch_tool running tool command: $tool_command ...')
|
2020-04-03 17:38:41 +02:00
|
|
|
}
|
|
|
|
exit(os.system(tool_command))
|
|
|
|
}
|
|
|
|
|
2020-05-14 08:07:14 +02:00
|
|
|
pub fn quote_path_with_spaces(s string) string {
|
|
|
|
if s.contains(' ') {
|
|
|
|
return '"${s}"'
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn args_quote_paths_with_spaces(args []string) string {
|
|
|
|
mut res := []string{}
|
|
|
|
for a in args {
|
2020-05-16 23:52:03 +02:00
|
|
|
res << quote_path_with_spaces(a)
|
2020-05-14 08:07:14 +02:00
|
|
|
}
|
|
|
|
return res.join(' ')
|
|
|
|
}
|
|
|
|
|
2020-04-03 17:38:41 +02:00
|
|
|
pub fn path_of_executable(path string) string {
|
|
|
|
$if windows {
|
|
|
|
return path + '.exe'
|
|
|
|
}
|
|
|
|
return path
|
|
|
|
}
|
2020-04-06 18:39:58 +02:00
|
|
|
|
|
|
|
pub fn read_file(file_path string) ?string {
|
|
|
|
mut raw_text := os.read_file(file_path) or {
|
|
|
|
return error('failed to open $file_path')
|
|
|
|
}
|
|
|
|
// BOM check
|
|
|
|
if raw_text.len >= 3 {
|
|
|
|
c_text := raw_text.str
|
|
|
|
if c_text[0] == 0xEF && c_text[1] == 0xBB && c_text[2] == 0xBF {
|
|
|
|
// skip three BOM bytes
|
|
|
|
offset_from_begin := 3
|
|
|
|
raw_text = tos(c_text[offset_from_begin], vstrlen(c_text) - offset_from_begin)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return raw_text
|
|
|
|
}
|
2020-04-06 19:35:24 +02:00
|
|
|
|
|
|
|
[inline]
|
|
|
|
fn imin(a, b int) int {
|
2020-04-08 22:12:42 +02:00
|
|
|
return if a < b {
|
|
|
|
a
|
|
|
|
} else {
|
|
|
|
b
|
|
|
|
}
|
2020-04-06 19:35:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
|
|
|
fn imax(a, b int) int {
|
2020-04-08 22:12:42 +02:00
|
|
|
return if a > b {
|
|
|
|
a
|
|
|
|
} else {
|
|
|
|
b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn replace_op(s string) string {
|
|
|
|
last_char := s[s.len - 1]
|
|
|
|
suffix := match last_char {
|
2020-04-26 07:35:59 +02:00
|
|
|
`+` { '_plus' }
|
|
|
|
`-` { '_minus' }
|
|
|
|
`*` { '_mult' }
|
|
|
|
`/` { '_div' }
|
|
|
|
`%` { '_mod' }
|
|
|
|
else { '' }
|
2020-04-08 22:12:42 +02:00
|
|
|
}
|
|
|
|
return s[..s.len - 1] + suffix
|
2020-04-06 19:35:24 +02:00
|
|
|
}
|
2020-04-11 09:18:59 +02:00
|
|
|
|
|
|
|
pub fn join_env_vflags_and_os_args() []string {
|
|
|
|
vosargs := os.getenv('VOSARGS')
|
|
|
|
if vosargs != '' {
|
|
|
|
return non_empty(vosargs.split(' '))
|
|
|
|
}
|
2020-04-26 09:17:13 +02:00
|
|
|
mut args := []string{}
|
2020-04-11 09:18:59 +02:00
|
|
|
vflags := os.getenv('VFLAGS')
|
|
|
|
if vflags != '' {
|
|
|
|
args << os.args[0]
|
|
|
|
args << vflags.split(' ')
|
|
|
|
if os.args.len > 1 {
|
|
|
|
args << os.args[1..]
|
|
|
|
}
|
|
|
|
return non_empty(args)
|
|
|
|
}
|
|
|
|
return non_empty(os.args)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn non_empty(arg []string) []string {
|
|
|
|
return arg.filter(it != '')
|
|
|
|
}
|