@@ -12,17 +12,32 @@ LOG_DIR=${LOG_DIR:-_logs/logs}
1212SUMMARY_FILE=${SUMMARY_FILE:- _logs/ build_summary.md}
1313KNOWN_GOOD_FILE=" "
1414
15+ # Codeql
16+
17+ CODEQL_WORK_DIR=" ./codeql_analysis_results"
18+ CODEQL_DATABASES_DIR=" ${CODEQL_WORK_DIR} /databases"
19+ CODEQL_SARIF_DIR=" ${CODEQL_WORK_DIR} /sarif"
20+ CODEQL_LANGUAGE=" cpp"
21+ CODEQL_QUERY_PACKS=" codeql/cpp-queries,codeql/misra-cpp-coding-standards" # Add more packs as needed
22+ CODEQL_CLI_VERSION=" v2.23.6" # Use the latest stable version
23+ CODEQL_PLATFORM=" linux64" # e.g., linux64, macos, win64
24+ CODEQL_BUNDLE=" codeql-${CODEQL_PLATFORM} .zip"
25+ CODEQL_URL=" https://github.com/github/codeql-cli-binaries/releases/download/${CODEQL_CLI_VERSION} /${CODEQL_BUNDLE} "
26+ # https://github.com/github/codeql-cli-binaries/releases/download/v2.23.6/codeql-linux64.zip
27+
1528# maybe move this to known_good.json or a config file later
1629declare -A BUILD_TARGET_GROUPS=(
1730 [score_baselibs]=" @score_baselibs//score/..."
1831 [score_communication]=" @score_communication//score/mw/com:com"
1932 [score_persistency]=" @score_persistency//src/cpp/src/... @score_persistency//src/rust/..."
20- # [score_logging]="@score_logging//src/..."
33+ [score_logging]=" @score_logging//src/..."
2134 [score_orchestrator]=" @score_orchestrator//src/..."
2235 [score_test_scenarios]=" @score_test_scenarios//..."
2336 [score_feo]=" @score_feo//..."
2437)
2538
39+
40+
2641# Parse command line arguments
2742while [[ $# -gt 0 ]]; do
2843 case $1 in
@@ -107,19 +122,71 @@ overall_depr_total=0
107122
108123# Track if any build group failed
109124any_failed=0
125+ binary_path=" ${CODEQL_WORK_DIR} /codeql-cli/codeql/codeql"
126+
127+ if [ -x " ${binary_path} " ]; then
128+ echo " Local CodeQL CLI found at ${binary_path} . Adding to PATH."
129+ export PATH=" $( pwd) /${CODEQL_WORK_DIR} /codeql-cli/codeql:${PATH} "
130+ else
131+ echo " CodeQL CLI not found. Downloading..."
132+ mkdir -p " ${CODEQL_WORK_DIR} /codeql-cli"
133+ curl -L " ${CODEQL_URL} " -o " ${CODEQL_WORK_DIR} /${CODEQL_BUNDLE} "
134+ unzip " ${CODEQL_WORK_DIR} /${CODEQL_BUNDLE} " -d " ${CODEQL_WORK_DIR} /codeql-cli"
135+ export PATH=" $( pwd) /${CODEQL_WORK_DIR} /codeql-cli/codeql:${PATH} "
136+ echo " CodeQL CLI downloaded and added to PATH."
137+ fi
138+
139+ # Verify CodeQL CLI is now available
140+ if ! command -v codeql & > /dev/null; then
141+ echo " Error: CodeQL CLI could not be set up. Exiting."
142+ exit 1
143+ else
144+ echo " codeql found in path"
145+ fi
146+
147+
148+ mkdir -p " ${CODEQL_DATABASES_DIR} "
149+ mkdir -p " ${CODEQL_SARIF_DIR} "
110150
111151for group in " ${! BUILD_TARGET_GROUPS[@]} " ; do
112152 targets=" ${BUILD_TARGET_GROUPS[$group]} "
113153 log_file=" ${LOG_DIR} /${group} .log"
114-
154+
155+ db_path=" ${CODEQL_DATABASES_DIR} /${group} _db"
156+ sarif_output=" ${CODEQL_SARIF_DIR} /${group} .sarif"
157+ current_bazel_output_base=" /tmp/codeql_bazel_output_${group} _$( date +%s%N) " # Add timestamp for extra uniqueness
158+
159+
160+ # 1. Clean Bazel to ensure a fresh build for CodeQL tracing
161+ echo " Running 'bazel clean --expunge' and 'bazel shutdown'..."
162+ bazel --output_base=" ${current_bazel_output_base} " clean --expunge || { echo " Bazel clean failed for ${group} " ; exit 1; }
163+ bazel --output_base=" ${current_bazel_output_base} " shutdown || { echo " Bazel shutdown failed for ${group} " ; exit 1; }
164+
115165 # Log build group banner only to stdout/stderr (not into summary table file)
116166 echo " --- Building group: ${group} ---"
117167 start_ts=$( date +%s)
118168 echo " bazel build --config " ${CONFIG} " ${targets} --verbose_failures"
119169 # GitHub Actions log grouping start
120170 echo " ::group::Bazel build (${group} )"
121171 set +e
122- bazel build --config " ${CONFIG} " ${targets} --verbose_failures 2>&1 | tee " $log_file "
172+
173+ build_command=" bazel --output_base=\\\" ${current_bazel_output_base} \\\" build \
174+ ${targets} \
175+ --verbose_failures \
176+ --spawn_strategy=standalone \
177+ --nouse_action_cache \
178+ --noremote_accept_cached \
179+ --noremote_upload_local_results \
180+ --disk_cache= ${targets} "
181+
182+ codeql database create " ${db_path} " \
183+ --language=" ${CODEQL_LANGUAGE} " \
184+ --build-mode=none \
185+ # --command="${build_command}" \
186+ --overwrite \
187+ || { echo " CodeQL database creation failed for ${group} " ; exit 1; }
188+
189+
123190 build_status=${PIPESTATUS[0]}
124191 # Track if any build group failed
125192 if [[ ${build_status} -ne 0 ]]; then
@@ -133,6 +200,24 @@ for group in "${!BUILD_TARGET_GROUPS[@]}"; do
133200 d_count=$( depr_count " $log_file " )
134201 overall_warn_total=$(( overall_warn_total + w_count ))
135202 overall_depr_total=$(( overall_depr_total + d_count ))
203+
204+ # Shutdown Bazel again after the traced build
205+ echo " Running 'bazel shutdown' after CodeQL database creation..."
206+ bazel shutdown || { echo " Bazel shutdown failed after tracing for ${group} " ; exit 1; }
207+
208+ # 4. Analyze the created database
209+ echo " Analyzing CodeQL database for ${group} ..."
210+ codeql database analyze " ${DB_PATH} " \
211+ --format=sarifv2.1.0 \
212+ --output=" ${SARIF_OUTPUT} " \
213+ --sarif-category=" ${group} -${CODEQL_LANGUAGE} " \
214+ --packs " ${CODEQL_QUERY_PACKS} " \
215+ || { echo " CodeQL analysis failed for ${group} " ; exit 1; }
216+
217+ echo " CodeQL analysis for ${group} complete. Results saved to: ${SARIF_OUTPUT} "
218+ echo " "
219+
220+
136221 # Append as a markdown table row (duration without trailing 's')
137222 if [[ ${build_status} -eq 0 ]]; then
138223 status_symbol=" ✅"
0 commit comments