Skip to content

Commit e5cf665

Browse files
authored
Modify CodeQL build script to use testinfo.yml (#4372)
Also, fix C# CodeQL analysis
1 parent ab4b0b8 commit e5cf665

File tree

3 files changed

+55
-33
lines changed

3 files changed

+55
-33
lines changed

.github/workflows/codeql-analysis.yml

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,27 @@ jobs:
112112
distribution: 'oracle'
113113
java-version: '23'
114114

115+
- name: Install C# compiler
116+
if: ${{ matrix.language == 'c#' }}
117+
run: sudo apt-get update && sudo apt-get install -y mono-mcs
118+
115119
- name: Remove ignored paths
116120
if: ${{ github.event_name == 'pull_request' }}
117121
run: rm -f ${{ matrix.paths-ignore }}
118122

123+
- name: Set up Python
124+
uses: actions/setup-python@v5
125+
with:
126+
python-version: "3.11"
127+
128+
- name: Run Poetry Image
129+
uses: abatilo/actions-poetry@v3
130+
with:
131+
poetry-version: "1.8.5"
132+
133+
- name: Install Dependencies
134+
run: poetry install
135+
119136
# Initializes the CodeQL tools for scanning.
120137
- name: Initialize CodeQL
121138
uses: github/codeql-action/init@v3
@@ -138,7 +155,7 @@ jobs:
138155

139156
- name: Manual build
140157
if: ${{ matrix.build-mode == 'manual' }}
141-
run: python scripts/build_codeql_language.py ${{ matrix.language }} ${{ matrix.paths }}
158+
run: poetry run python scripts/build_codeql_language.py ${{ matrix.language }} ${{ matrix.paths }}
142159

143160
- name: Perform CodeQL Analysis
144161
uses: github/codeql-action/analyze@v3

scripts/build_codeql_language.py

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,58 @@
11
import argparse
2+
import shlex
23
import subprocess
4+
from dataclasses import dataclass
35
from pathlib import Path
4-
from typing import Callable, Dict, List
6+
from typing import Dict, List
7+
8+
from glotter.source import Source
9+
10+
TEST_INFO_DIR: Dict[str, str] = {
11+
"c": "c",
12+
"cpp": "c-plus-plus",
13+
"c#": "c-sharp",
14+
"java": "java",
15+
"kotlin": "kotlin",
16+
"swift": "swift",
17+
}
18+
19+
20+
@dataclass
21+
class TestInfoStruct:
22+
path: Path
23+
language: str
24+
test_info_str: str
525

626

727
def main():
828
parser = argparse.ArgumentParser()
929
parser.add_argument("language", help="language to build")
1030
parser.add_argument("files_changed", nargs="*", help="files that have changed")
1131
parsed_args = parser.parse_args()
12-
command_func = COMMANDS[parsed_args.language]
32+
testinfo_struct = get_test_info_struct(parsed_args.language)
1333
for changed_file in parsed_args.files_changed:
1434
path = Path(changed_file)
1535
print(f"Building {path}")
16-
command = command_func(path)
36+
command = get_build_command(testinfo_struct, path)
1737
subprocess.run(command, cwd=path.parent, check=True)
1838

1939

20-
def build_c(path: Path) -> List[str]:
21-
return ["gcc", "-o", path.stem, path.name, "-lm"]
22-
23-
24-
def build_cpp(path: Path) -> List[str]:
25-
return ["g++", "-o", path.stem, path.name]
26-
40+
def get_test_info_struct(language: str) -> TestInfoStruct:
41+
path = Path("archive") / language[0] / TEST_INFO_DIR[language] / "testinfo.yml"
42+
testinfo_str = path.read_text(encoding="utf-8")
43+
return TestInfoStruct(path=path, language=language, test_info_str=testinfo_str)
2744

28-
def build_c_sharp(path: Path) -> List[str]:
29-
return ["mcs", "-reference:System.Numerics", path.name]
3045

46+
def get_build_command(testinfo_struct: TestInfoStruct, path: Path) -> List[str]:
47+
source = Source(
48+
name=path.name,
49+
language=testinfo_struct.language,
50+
path=str(path),
51+
test_info_string=testinfo_struct.test_info_str,
52+
)
53+
build: str = source.test_info.container_info.build
54+
return shlex.split(build)
3155

32-
def build_java(path: Path) -> List[str]:
33-
return ["javac", path.name]
34-
35-
36-
def build_kotlin(path: Path) -> List[str]:
37-
return ["kotlinc", path.name, "-include-runtime", "-d", f"{path.stem}.jar"]
38-
39-
40-
def build_swift(path: Path) -> List[str]:
41-
return ["swiftc", "-o", path.stem, path.name]
42-
43-
44-
COMMANDS: Dict[str, Callable[[Path], List[str]]] = {
45-
"c": build_c,
46-
"cpp": build_cpp,
47-
"c#": build_c_sharp,
48-
"java": build_java,
49-
"kotlin": build_kotlin,
50-
"swift": build_swift,
51-
}
5256

5357
if __name__ == "__main__":
5458
main()

scripts/get_codeql_languages.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
LINUX = "ubuntu-latest"
1010
MACOS = "macos-latest"
11+
WINDOWS = "windows-latest"
1112

1213

1314
@dataclass(frozen=True)

0 commit comments

Comments
 (0)