Hog v9.70.0
check_proj_ver.tcl
Go to the documentation of this file.
1 #!/usr/bin/env tclsh
2 # Copyright 2018-2025 The University of Birmingham
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 
16 set tcl_path [file normalize "[file dirname [info script]]/.."]
17 set repo_path [file normalize "$tcl_path/../.."]
18 
19 source $tcl_path/hog.tcl
20 
21 # Import tcllib for libero
22 if {[IsLibero]} {
23  if {[info exists env(HOG_TCLLIB_PATH)]} {
24  lappend auto_path $env(HOG_TCLLIB_PATH)
25  } else {
26  puts "ERROR: To run Hog with Microsemi Libero SoC, you need to define the HOG_TCLLIB_PATH variable."
27  return
28  }
29 }
30 
31 #parsing command options
32 if {[catch {package require cmdline} ERROR] || [catch {package require struct::matrix} ERROR]} {
33  puts "$ERROR\n Tcllib not found. If you are running this script on tclsh, you can fix this by installing 'tcllib'"
34  return
35 }
36 
37 
38 
39 set parameters {
40  {sim "If set, checks also the version of the simulation files."}
41  {ext_path.arg "" "Sets the absolute path for the external libraries."}
42 }
43 
44 set usage "- USAGE: $::argv0 \[OPTIONS\] <project> \n. Options:"
45 
46 if {$::argc eq 0} {
47  Msg Info [cmdline::usage $parameters $usage]
48  exit 1
49 } elseif {[IsQuartus] && [catch {array set options [cmdline::getoptions quartus(args) $parameters $usage]}] || $::argc eq 0} {
50  #Quartus
51  Msg Info [cmdline::usage $parameters $usage]
52  exit 1
53 } elseif {[catch {array set options [cmdline::getoptions ::argv $parameters $usage]}]} {
54  Msg Info [cmdline::usage $parameters $usage]
55  exit 1
56  ##nagelfar ignore Unknown variable
57 }
58 
59 set project [lindex $argv 0]
60 set sim 0
61 set ext_path ""
62 set project_dir $repo_path/Top/$project
63 
64 if {$options(sim) == 1} {
65  set sim 1
66  Msg Info "Will check also the version of the simulation files..."
67 }
68 
69 if {$options(ext_path) != ""} {
70  set ext_path $options(ext_path)
71  Msg Info "External path set to $ext_path"
72 }
73 
74 set ci_run 0
75 if {[info exists env(HOG_PUSH_TOKEN)] && [info exist env(CI_PROJECT_ID)] && [info exist env(CI_API_V4_URL)] } {
76  set token $env(HOG_PUSH_TOKEN)
77  set api_url $env(CI_API_V4_URL)
78  set project_id $env(CI_PROJECT_ID)
79  set ci_run 1
80 }
81 
82 cd $repo_path
83 
84 set ver [GetProjectVersion $project_dir $repo_path $ext_path $sim]
85 if {$ver == 0} {
86  Msg Info "$project was modified, continuing with the CI..."
87  if {$ci_run == 1 && ![IsQuartus] && ![IsISE]} {
88  Msg Info "Checking if the project has been already built in a previous CI run..."
89  lassign [GetRepoVersions $project_dir $repo_path] sha
90  Msg Info "Checking if project $project has been built in a previous CI run with sha $sha..."
91  set result [catch {package require json} JsonFound]
92  if {"$result" != "0"} {
93  Msg CriticalWarning "Cannot find JSON package equal or higher than 1.0.\n $JsonFound\n Exiting"
94  return
95  }
96  lassign [ExecuteRet curl --header "PRIVATE-TOKEN: $token" "$api_url/projects/$project_id/pipelines"] ret content
97  set pipeline_dict [json::json2dict $content]
98  if {[llength $pipeline_dict] > 0} {
99  foreach pip $pipeline_dict {
100  set pip_sha [DictGet $pip sha]
101  set source [DictGet $pip source]
102  if {$source == "merge_request_event" && [string first $sha $pip_sha] != -1} {
103  Msg Info "Found pipeline with sha $pip_sha for project $project"
104  set pipeline_id [DictGet $pip id]
105  # tclint-disable-next-line line-length
106  lassign [ExecuteRet curl --header "PRIVATE-TOKEN: $token" "$api_url/projects/${project_id}/pipelines/${pipeline_id}/jobs?pagination=keyset&per_page=100"] ret2 content2
107  set jobs_dict [json::json2dict $content2]
108  if {[llength $jobs_dict] > 0} {
109  foreach job $jobs_dict {
110  set job_name [DictGet $job name]
111  set job_id [DictGet $job id]
112  set artifacts [DictGet $job artifacts_file]
113  set status [DictGet $job status]
114  set current_job_name $env(CI_JOB_NAME)
115  if {$current_job_name == $job_name && $status == "success"} {
116  # tclint-disable-next-line line-length
117  lassign [ExecuteRet curl --location --output artifacts.zip --header "PRIVATE-TOKEN: $token" --url "$api_url/projects/$project_id/jobs/$job_id/artifacts"] ret3 content3
118  if {$ret3 != 0} {
119  Msg CriticalWarning "Cannot download artifacts for job $job_name with id $job_id"
120  return
121  } else {
122  lassign [ExecuteRet unzip -o $repo_path/artifacts.zip] ret_zip
123  if {$ret_zip != 0} {
124 
125  } else {
126  Msg Info "Artifacts for job $job_name with id $job_id downloaded and unzipped."
127  file mkdir $repo_path/Projects/$project
128  set fp [open "$repo_path/Projects/$project/skip.me" w+]
129  close $fp
130  exit 0
131  }
132  }
133  }
134  }
135  }
136  }
137  }
138  }
139  }
140 } elseif {$ver != -1} {
141  Msg Info "$project was not modified since version: $ver, disabling the CI..."
142  file mkdir $repo_path/Projects/$project
143  set fp [open "$repo_path/Projects/$project/skip.me" w+]
144  close $fp
145  exit 0
146 }