Skip to content

Commit 12c61e2

Browse files
committed
Add macOS build script for extractors
Introduces build-macos.sh to support building and cleaning CIL and x86 extractors on macOS ARM64. The script handles argument parsing, directory validation, and build artifact management. x86 extractor build and initialization are not yet implemented.
1 parent b2e0d59 commit 12c61e2

1 file changed

Lines changed: 194 additions & 0 deletions

File tree

binary/build-macos.sh

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
#!/bin/bash
2+
3+
set -eu
4+
5+
# Build script for macOS (ARM64)
6+
# Usage:
7+
# ./build-macos.sh -cil -cliFolder /path/to/cli # Build CIL extractor
8+
# ./build-macos.sh -cil -clean # Clean CIL build artifacts
9+
# ./build-macos.sh -cil -init -cliFolder /path/to/cli # Initialize and build CIL
10+
#
11+
# Future: x86 extractor support will be added
12+
13+
# Defensive script directory detection
14+
if [[ -n "${BASH_SOURCE[0]:-}" ]]; then
15+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
16+
elif [[ -n "${0:-}" ]]; then
17+
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
18+
else
19+
echo "Error: Unable to determine script directory"
20+
exit 1
21+
fi
22+
23+
if [[ -z "${SCRIPT_DIR}" || "${SCRIPT_DIR}" != /* ]]; then
24+
echo "Error: Failed to determine absolute script directory"
25+
exit 1
26+
fi
27+
28+
# Verify we're in the expected directory by checking for a known file
29+
if [[ ! -f "${SCRIPT_DIR}/build-win64.ps1" ]]; then
30+
echo "Error: Script directory validation failed - expected files not found"
31+
echo "SCRIPT_DIR: ${SCRIPT_DIR}"
32+
exit 1
33+
fi
34+
35+
# Parse arguments
36+
BUILD_CIL=false
37+
BUILD_X86=false
38+
CLEAN=false
39+
INIT=false
40+
CLI_FOLDER=""
41+
42+
while [[ $# -gt 0 ]]; do
43+
case $1 in
44+
-cil)
45+
BUILD_CIL=true
46+
shift
47+
;;
48+
-x86)
49+
BUILD_X86=true
50+
shift
51+
;;
52+
-clean)
53+
CLEAN=true
54+
shift
55+
;;
56+
-init)
57+
INIT=true
58+
shift
59+
;;
60+
-cliFolder)
61+
CLI_FOLDER="$2"
62+
shift 2
63+
;;
64+
*)
65+
echo "Unknown option: $1"
66+
echo "Usage: $0 [-cil|-x86] [-clean|-init] [-cliFolder <path>]"
67+
exit 1
68+
;;
69+
esac
70+
done
71+
72+
# If no extractor specified, show usage
73+
if [[ "$BUILD_CIL" == false && "$BUILD_X86" == false ]]; then
74+
echo "Usage: $0 [-cil|-x86] [-clean|-init] [-cliFolder <path>]"
75+
echo ""
76+
echo "Options:"
77+
echo " -cil Build the CIL (C# IL) extractor"
78+
echo " -x86 Build the x86 extractor (not yet implemented)"
79+
echo " -clean Clean build artifacts"
80+
echo " -init Initialize dependencies (x86 only)"
81+
echo " -cliFolder Path to the CodeQL CLI folder (required for build)"
82+
exit 1
83+
fi
84+
85+
# Validate arguments
86+
if [[ "$CLEAN" == false && -z "$CLI_FOLDER" ]]; then
87+
echo "Error: -cliFolder is required unless -clean is specified"
88+
exit 1
89+
fi
90+
91+
build_cil() {
92+
local tools_folder="${CLI_FOLDER}/cil/tools/osx-arm64"
93+
local cil_folder="${CLI_FOLDER}/cil"
94+
95+
pushd "${SCRIPT_DIR}/extractor/cil" > /dev/null
96+
97+
dotnet build Semmle.Extraction.CSharp.IL -o "${tools_folder}" -c Release --self-contained
98+
if [[ $? -ne 0 ]]; then
99+
echo "Build failed"
100+
popd > /dev/null
101+
exit 1
102+
fi
103+
104+
popd > /dev/null
105+
106+
# Create directories
107+
mkdir -p "${tools_folder}"
108+
mkdir -p "${cil_folder}"
109+
110+
# Copy extractor configuration
111+
cp "${SCRIPT_DIR}/extractor/cil/codeql-extractor.yml" "${cil_folder}/"
112+
113+
# Copy downgrades if they exist
114+
if [[ -d "${SCRIPT_DIR}/downgrades" ]]; then
115+
cp -r "${SCRIPT_DIR}/downgrades" "${cil_folder}/"
116+
fi
117+
118+
# Copy dbscheme files
119+
local ql_lib_folder="${SCRIPT_DIR}/ql/lib"
120+
cp "${ql_lib_folder}/semmlecode.binary.dbscheme" "${cil_folder}/"
121+
if [[ -f "${ql_lib_folder}/semmlecode.binary.dbscheme.stats" ]]; then
122+
cp "${ql_lib_folder}/semmlecode.binary.dbscheme.stats" "${cil_folder}/"
123+
fi
124+
125+
# Copy tool scripts
126+
mkdir -p "${cil_folder}/tools"
127+
cp "${SCRIPT_DIR}/tools/cil/"* "${cil_folder}/tools/"
128+
129+
echo "CIL extractor built successfully to ${cil_folder}"
130+
}
131+
132+
clean_cil() {
133+
echo "Cleaning CIL build artifacts..."
134+
135+
local bin_dir="${SCRIPT_DIR}/extractor/cil/Semmle.Extraction.CSharp.IL/bin"
136+
local obj_dir="${SCRIPT_DIR}/extractor/cil/Semmle.Extraction.CSharp.IL/obj"
137+
138+
[[ -d "${bin_dir}" ]] && rm -rf "${bin_dir}"
139+
[[ -d "${obj_dir}" ]] && rm -rf "${obj_dir}"
140+
141+
echo "CIL clean complete"
142+
}
143+
144+
build_x86() {
145+
echo "x86 extractor build for macOS is not yet implemented"
146+
echo "This will require:"
147+
echo " - LIEF library (build with cmake/make)"
148+
echo " - Zydis library (build with cmake/make)"
149+
echo " - fmt library"
150+
echo " - Boost headers"
151+
echo " - args library"
152+
echo " - clang++ compiler"
153+
exit 1
154+
}
155+
156+
clean_x86() {
157+
echo "Cleaning x86 build artifacts..."
158+
159+
local x86_dir="${SCRIPT_DIR}/extractor/x86"
160+
161+
[[ -d "${x86_dir}/args" ]] && rm -rf "${x86_dir}/args"
162+
[[ -d "${x86_dir}/boost-minimal" ]] && rm -rf "${x86_dir}/boost-minimal"
163+
[[ -d "${x86_dir}/fmt" ]] && rm -rf "${x86_dir}/fmt"
164+
[[ -d "${x86_dir}/LIEF" ]] && rm -rf "${x86_dir}/LIEF"
165+
[[ -d "${x86_dir}/zydis" ]] && rm -rf "${x86_dir}/zydis"
166+
[[ -f "${x86_dir}/extractor" ]] && rm -f "${x86_dir}/extractor"
167+
[[ -f "${x86_dir}/main.o" ]] && rm -f "${x86_dir}/main.o"
168+
169+
echo "x86 clean complete"
170+
}
171+
172+
init_x86() {
173+
echo "x86 extractor initialization for macOS is not yet implemented"
174+
exit 1
175+
}
176+
177+
# Execute requested builds
178+
if [[ "$BUILD_CIL" == true ]]; then
179+
if [[ "$CLEAN" == true ]]; then
180+
clean_cil
181+
else
182+
build_cil
183+
fi
184+
fi
185+
186+
if [[ "$BUILD_X86" == true ]]; then
187+
if [[ "$CLEAN" == true ]]; then
188+
clean_x86
189+
elif [[ "$INIT" == true ]]; then
190+
init_x86
191+
else
192+
build_x86
193+
fi
194+
fi

0 commit comments

Comments
 (0)