2020-04-26 08:32:05 +02:00
|
|
|
import os
|
|
|
|
import flag
|
|
|
|
import scripting
|
|
|
|
import vgit
|
2019-12-08 18:21:17 +01:00
|
|
|
|
|
|
|
const (
|
2020-10-14 23:39:09 +02:00
|
|
|
tool_version = '0.0.3'
|
2020-01-08 21:45:47 +01:00
|
|
|
tool_description = ' Checkout an old V and compile it as it was on specific commit.
|
2020-10-21 14:24:45 +02:00
|
|
|
| This tool is useful, when you want to discover when something broke.
|
|
|
|
| It is also useful, when you just want to experiment with an older historic V.
|
|
|
|
|
|
|
|
|
| The VCOMMIT argument can be a git commitish like HEAD or master and so on.
|
|
|
|
| When oldv is used with git bisect, you probably want to give HEAD. For example:
|
|
|
|
| git bisect start
|
|
|
|
| git bisect bad
|
|
|
|
| git checkout known_good_commit
|
|
|
|
| git bisect good
|
|
|
|
| ## Now git will automatically checkout a middle commit between the bad and the good
|
2022-02-17 15:34:05 +01:00
|
|
|
| cmd/tools/oldv --bisect --command="run commands in oldv folder, to verify if the commit is good or bad"
|
2020-10-21 14:24:45 +02:00
|
|
|
| ## See what the result is, and either do: ...
|
|
|
|
| git bisect good
|
|
|
|
| ## ... or do:
|
|
|
|
| git bisect bad
|
|
|
|
| ## Now you just repeat the above steps, each time running oldv with the same command, then mark the result as good or bad,
|
|
|
|
| ## until you find the commit, where the problem first occurred.
|
|
|
|
| ## When you finish, do not forget to do:
|
|
|
|
| git bisect reset'.strip_margin()
|
2019-12-08 18:21:17 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
struct Context {
|
|
|
|
mut:
|
2021-04-07 11:22:51 +02:00
|
|
|
vgo vgit.VGitOptions
|
|
|
|
vgcontext vgit.VGitContext
|
|
|
|
commit_v string = 'master' // the commit from which you want to produce a working v compiler (this may be a commit-ish too)
|
2020-01-08 21:45:47 +01:00
|
|
|
commit_v_hash string // this will be filled from the commit-ish commit_v using rev-list. It IS a commit hash.
|
|
|
|
path_v string // the full path to the v folder inside workdir.
|
|
|
|
path_vc string // the full path to the vc folder inside workdir.
|
|
|
|
cmd_to_run string // the command that you want to run *in* the oldv repo
|
2021-04-07 11:22:51 +02:00
|
|
|
cc string = 'cc' // the C compiler to use for bootstrapping.
|
|
|
|
cleanup bool // should the tool run a cleanup first
|
|
|
|
use_cache bool // use local cached copies for --vrepo and --vcrepo in
|
2021-05-31 13:21:06 +02:00
|
|
|
fresh_tcc bool // do use `make fresh_tcc`
|
2022-02-17 14:21:57 +01:00
|
|
|
is_bisect bool // bisect mode; usage: `cmd/tools/oldv -b -c './v run bug.v'`
|
2019-12-08 18:21:17 +01:00
|
|
|
}
|
|
|
|
|
2020-05-17 13:51:18 +02:00
|
|
|
fn (mut c Context) compile_oldv_if_needed() {
|
2021-04-07 11:22:51 +02:00
|
|
|
c.vgcontext = vgit.VGitContext{
|
2020-10-14 23:39:09 +02:00
|
|
|
workdir: c.vgo.workdir
|
|
|
|
v_repo_url: c.vgo.v_repo_url
|
2020-04-20 17:39:25 +02:00
|
|
|
vc_repo_url: c.vgo.vc_repo_url
|
2020-10-14 23:39:09 +02:00
|
|
|
cc: c.cc
|
|
|
|
commit_v: c.commit_v
|
|
|
|
path_v: c.path_v
|
|
|
|
path_vc: c.path_vc
|
2021-05-31 13:21:06 +02:00
|
|
|
make_fresh_tcc: c.fresh_tcc
|
2019-12-08 18:21:17 +01:00
|
|
|
}
|
2021-04-07 11:22:51 +02:00
|
|
|
c.vgcontext.compile_oldv_if_needed()
|
|
|
|
c.commit_v_hash = c.vgcontext.commit_v__hash
|
|
|
|
if !os.exists(c.vgcontext.vexepath) && c.cmd_to_run.len > 0 {
|
2022-03-06 18:01:22 +01:00
|
|
|
// Note: 125 is a special code, that git bisect understands as 'skip this commit'.
|
2019-12-08 18:21:17 +01:00
|
|
|
// it is used to inform git bisect that the current commit leads to a build failure.
|
2020-01-08 21:45:47 +01:00
|
|
|
exit(125)
|
2019-12-08 18:21:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-07 11:22:51 +02:00
|
|
|
const cache_oldv_folder = os.join_path(os.cache_dir(), 'oldv')
|
|
|
|
|
|
|
|
const cache_oldv_folder_v = os.join_path(cache_oldv_folder, 'v')
|
|
|
|
|
|
|
|
const cache_oldv_folder_vc = os.join_path(cache_oldv_folder, 'vc')
|
|
|
|
|
|
|
|
fn sync_cache() {
|
|
|
|
scripting.verbose_trace(@FN, 'start')
|
|
|
|
if !os.exists(cache_oldv_folder) {
|
|
|
|
scripting.verbose_trace(@FN, 'creating $cache_oldv_folder')
|
|
|
|
scripting.mkdir_all(cache_oldv_folder) or {
|
|
|
|
scripting.verbose_trace(@FN, '## failed.')
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
scripting.chdir(cache_oldv_folder)
|
|
|
|
for reponame in ['v', 'vc'] {
|
|
|
|
repofolder := os.join_path(cache_oldv_folder, reponame)
|
|
|
|
if !os.exists(repofolder) {
|
|
|
|
scripting.verbose_trace(@FN, 'cloning to $repofolder')
|
|
|
|
scripting.exec('git clone --quiet https://github.com/vlang/$reponame $repofolder') or {
|
|
|
|
scripting.verbose_trace(@FN, '## error during clone: $err')
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
scripting.chdir(repofolder)
|
|
|
|
scripting.exec('git pull --quiet') or {
|
|
|
|
scripting.verbose_trace(@FN, 'pulling to $repofolder')
|
|
|
|
scripting.verbose_trace(@FN, '## error during pull: $err')
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
scripting.verbose_trace(@FN, 'done')
|
|
|
|
}
|
|
|
|
|
2020-01-08 21:45:47 +01:00
|
|
|
fn main() {
|
|
|
|
scripting.used_tools_must_exist(['git', 'cc'])
|
2021-04-07 11:22:51 +02:00
|
|
|
//
|
|
|
|
// Resetting VEXE here allows for `v run cmd/tools/oldv.v'.
|
|
|
|
// the parent V would have set VEXE, which later will
|
|
|
|
// affect the V's run from the tool itself.
|
|
|
|
os.setenv('VEXE', '', true)
|
|
|
|
//
|
2019-12-08 18:21:17 +01:00
|
|
|
mut context := Context{}
|
2021-04-07 11:22:51 +02:00
|
|
|
context.vgo.workdir = cache_oldv_folder
|
2019-12-08 18:21:17 +01:00
|
|
|
mut fp := flag.new_flag_parser(os.args)
|
2020-03-19 15:49:07 +01:00
|
|
|
fp.application(os.file_name(os.executable()))
|
2020-01-08 21:45:47 +01:00
|
|
|
fp.version(tool_version)
|
|
|
|
fp.description(tool_description)
|
2019-12-08 18:21:17 +01:00
|
|
|
fp.arguments_description('VCOMMIT')
|
|
|
|
fp.skip_executable()
|
2021-04-07 11:22:51 +02:00
|
|
|
context.use_cache = fp.bool('cache', `u`, true, 'Use a cache of local repositories for --vrepo and --vcrepo in \$HOME/.cache/oldv/')
|
|
|
|
if context.use_cache {
|
|
|
|
context.vgo.v_repo_url = cache_oldv_folder_v
|
|
|
|
context.vgo.vc_repo_url = cache_oldv_folder_vc
|
|
|
|
} else {
|
|
|
|
context.vgo.v_repo_url = 'https://github.com/vlang/v'
|
|
|
|
context.vgo.vc_repo_url = 'https://github.com/vlang/vc'
|
|
|
|
}
|
|
|
|
should_sync := fp.bool('cache-sync', `s`, false, 'Update the local cache')
|
2022-02-17 14:21:57 +01:00
|
|
|
context.is_bisect = fp.bool('bisect', `b`, false, 'Bisect mode. Use the current commit in the repo where oldv is.')
|
|
|
|
if !should_sync && !context.is_bisect {
|
2021-09-16 18:25:05 +02:00
|
|
|
fp.limit_free_args(1, 1) ?
|
2021-04-07 11:22:51 +02:00
|
|
|
}
|
|
|
|
////
|
|
|
|
context.cleanup = fp.bool('clean', 0, false, 'Clean before running (slower).')
|
2021-05-31 13:21:06 +02:00
|
|
|
context.fresh_tcc = fp.bool('fresh_tcc', 0, true, 'Do `make fresh_tcc` when preparing a V compiler.')
|
2020-03-19 07:06:37 +01:00
|
|
|
context.cmd_to_run = fp.string('command', `c`, '', 'Command to run in the old V repo.\n')
|
2020-04-20 17:39:25 +02:00
|
|
|
commits := vgit.add_common_tool_options(mut context.vgo, mut fp)
|
2021-04-07 11:22:51 +02:00
|
|
|
if should_sync {
|
|
|
|
sync_cache()
|
|
|
|
exit(0)
|
|
|
|
}
|
|
|
|
if context.use_cache {
|
|
|
|
if !os.is_dir(cache_oldv_folder_v) || !os.is_dir(cache_oldv_folder_vc) {
|
|
|
|
sync_cache()
|
|
|
|
}
|
|
|
|
}
|
2019-12-08 18:21:17 +01:00
|
|
|
if commits.len > 0 {
|
|
|
|
context.commit_v = commits[0]
|
2022-02-17 14:21:57 +01:00
|
|
|
if context.is_bisect {
|
|
|
|
eprintln('In bisect mode, you should not pass any commits, since oldv will use the current one.')
|
|
|
|
exit(2)
|
|
|
|
}
|
2020-01-08 21:45:47 +01:00
|
|
|
} else {
|
2019-12-08 18:21:17 +01:00
|
|
|
context.commit_v = scripting.run('git rev-list -n1 HEAD')
|
|
|
|
}
|
2020-08-04 10:22:37 +02:00
|
|
|
scripting.cprintln('################# context.commit_v: $context.commit_v #####################')
|
2020-04-20 17:39:25 +02:00
|
|
|
context.path_v = vgit.normalized_workpath_for_commit(context.vgo.workdir, context.commit_v)
|
|
|
|
context.path_vc = vgit.normalized_workpath_for_commit(context.vgo.workdir, 'vc')
|
|
|
|
if !os.is_dir(context.vgo.workdir) {
|
2020-10-14 23:39:09 +02:00
|
|
|
eprintln('Work folder: $context.vgo.workdir , does not exist.')
|
2019-12-08 18:21:17 +01:00
|
|
|
exit(2)
|
|
|
|
}
|
|
|
|
ecc := os.getenv('CC')
|
2020-01-08 21:45:47 +01:00
|
|
|
if ecc != '' {
|
|
|
|
context.cc = ecc
|
|
|
|
}
|
2019-12-08 18:21:17 +01:00
|
|
|
if context.cleanup {
|
2020-01-27 16:56:32 +01:00
|
|
|
scripting.rmrf(context.path_v)
|
|
|
|
scripting.rmrf(context.path_vc)
|
2019-12-08 18:21:17 +01:00
|
|
|
}
|
|
|
|
context.compile_oldv_if_needed()
|
2020-01-08 21:45:47 +01:00
|
|
|
scripting.chdir(context.path_v)
|
2021-04-06 10:43:46 +02:00
|
|
|
shorter_hash := context.commit_v_hash[0..10]
|
|
|
|
scripting.cprintln('# v commit hash: $shorter_hash | folder: $context.path_v')
|
2019-12-08 18:21:17 +01:00
|
|
|
if context.cmd_to_run.len > 0 {
|
2021-04-06 10:43:46 +02:00
|
|
|
scripting.cprintln_strong('# command: ${context.cmd_to_run:-34s}')
|
2021-07-20 13:04:35 +02:00
|
|
|
cmdres := os.execute_or_exit(context.cmd_to_run)
|
2021-04-07 11:22:51 +02:00
|
|
|
if cmdres.exit_code != 0 {
|
|
|
|
scripting.cprintln_strong('# exit code: ${cmdres.exit_code:-4d}')
|
|
|
|
}
|
2021-04-06 10:43:46 +02:00
|
|
|
scripting.cprint_strong('# result: ')
|
|
|
|
print(cmdres.output)
|
2021-10-02 13:13:17 +02:00
|
|
|
if !cmdres.output.ends_with('\n') {
|
|
|
|
println('')
|
|
|
|
}
|
2020-01-08 21:45:47 +01:00
|
|
|
exit(cmdres.exit_code)
|
2019-12-08 18:21:17 +01:00
|
|
|
}
|
|
|
|
}
|