Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions .github/actions/setup-xcode/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: 'Setup Xcode Conditionally'
description: 'Check current Xcode version and setup if different from required version (Avoid unessary sudo/password input for self hosted runner)'

inputs:
xcode-version:
description: 'Required Xcode version'
required: true

outputs:
current-version:
description: 'Current Xcode version'
value: ${{ steps.current-xcode.outputs.version }}
setup-skipped:
description: 'Whether Xcode setup was skipped'
value: ${{ steps.xcode-check.outputs.skip-setup }}

runs:
using: 'composite'
steps:
- name: Get current Xcode version
id: current-xcode
shell: bash
run: |
CURRENT_VERSION=$(xcode-select -p | grep -o '[0-9]\+\.[0-9]\+' || echo "none")
echo "version=$CURRENT_VERSION" >> $GITHUB_OUTPUT
echo "Current Xcode version: $CURRENT_VERSION"

- name: Check if Xcode version matches
id: xcode-check
shell: bash
run: |
CURRENT="${{ steps.current-xcode.outputs.version }}"
REQUIRED="${{ inputs.xcode-version }}"
if [ "$CURRENT" = "$REQUIRED" ]; then
echo "skip-setup=true" >> $GITHUB_OUTPUT
echo "✅ Xcode version matches: $CURRENT = $REQUIRED, skipping setup"
else
echo "skip-setup=false" >> $GITHUB_OUTPUT
echo "❌ Xcode version mismatch: $CURRENT ≠ $REQUIRED, setup required"
fi

- name: Setup Xcode
if: steps.xcode-check.outputs.skip-setup == 'false'
uses: maxim-lobanov/setup-xcode@v1
with:
xcode-version: ${{ inputs.xcode-version }}
- name: Swift version
shell: bash
run: swift --version
28 changes: 28 additions & 0 deletions .github/workflows/CITest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,31 @@ jobs:
cmake --preset=${{ matrix.config.platform }}-x64${{ matrix.config.diet_build == true && '-diet' || '' }}
cmake --build --preset build-${{ matrix.config.platform }}${{ matrix.config.diet_build == true && '-diet' || '' }}-release
cmake --build --preset install-${{ matrix.config.platform }}${{ matrix.config.diet_build == true && '-diet' || '' }}-release

cmake --preset=${{ matrix.config.platform }}-x64
cmake --build --preset build-${{ matrix.config.platform }}-release
cmake --build --preset install-${{ matrix.config.platform }}-release
Comment on lines +230 to +232
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why those additions to the Windows build?

macOS:
name: Execute tests on macOS
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
name: Execute tests on macOS
name: Execute tests on macOS (Swift)

strategy:
fail-fast: false
matrix:
os: [macos-15]
xcode-version: ["16.4"]
runs-on: ${{ matrix.os }}
env:
GH_TOKEN: ${{ github.token }}
steps:
- uses: actions/checkout@v4
- name: Setup Xcode
uses: ./.github/actions/setup-xcode
with:
xcode-version: ${{ matrix.xcode-version }}
- name: Build and run tests in debug mode
run: |
swift test \
-c debug
Comment on lines +251 to +252
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
swift test \
-c debug
swift test -c debug

- name: Build and run tests in release mode
run: |
swift test \
-c release
Comment on lines +255 to +256
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
swift test \
-c release
swift test -c release

53 changes: 53 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// swift-tools-version: 6.1
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "capstone",
products: [
.library(
name: "Ccapstone",
targets: ["Ccapstone"]
Comment on lines +10 to +11
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the name Ccapstone is necessary because capstone exists already?
My naive search brought up nothing:
https://swiftpackageindex.com/search?query=capstone

Or is it to not conflict with @santalvarez bindings?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Swift convention is to name libraries imported from C as C + LibraryName for example, Swift's official example, and then have a pure Swift binding library that drops the C and starts with an uppercase letter.

),
],
targets: [
.target(
name: "Ccapstone",
path: "bindings/swift/Ccapstone",
cSettings: [
.headerSearchPath("../../../include"),
.define("CAPSTONE_USE_SYS_DYN_MEM"),
.define("CAPSTONE_HAS_ARM"),
.define("CAPSTONE_HAS_ARM64"),
.define("CAPSTONE_HAS_AARCH64"),
.define("CAPSTONE_HAS_MIPS"),
.define("CAPSTONE_HAS_X86"),
.define("CAPSTONE_HAS_POWERPC"),
.define("CAPSTONE_HAS_SPARC"),
.define("CAPSTONE_HAS_SYSTEMZ"),
.define("CAPSTONE_HAS_XCORE"),
.define("CAPSTONE_HAS_M68K"),
.define("CAPSTONE_HAS_TMS320C64X"),
.define("CAPSTONE_HAS_M680X"),
.define("CAPSTONE_HAS_EVM"),
.define("CAPSTONE_HAS_MOS65XX"),
.define("CAPSTONE_HAS_WASM"),
.define("CAPSTONE_HAS_BPF"),
.define("CAPSTONE_HAS_RISCV"),
.define("CAPSTONE_HAS_SH"),
.define("CAPSTONE_HAS_TRICORE"),
.define("CAPSTONE_HAS_ALPHA"),
.define("CAPSTONE_HAS_HPPA"),
.define("CAPSTONE_HAS_LOONGARCH"),
.define("CAPSTONE_HAS_XTENSA"),
.define("CAPSTONE_HAS_ARC"),
],
),
.testTarget(
name: "CcapstoneTests",
dependencies: ["Ccapstone"],
path: "bindings/swift/CcapstoneTests",
),
]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the Swift documentation (https://www.swift.org/documentation/articles/wrapping-c-cpp-library-in-swift.html) source files can be added with sources: [] and cSettings: []. Is there a reason you chose to copy the source and header files?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the capstone directory structure does not meet SwiftPM’s requirements, specifying only the sources or headers is not sufficient for SwiftPM, so a symbolic link-based approach is needed to create a directory structure that conforms to SwiftPM

)
1 change: 1 addition & 0 deletions bindings/swift/Ccapstone/LEB128.h
1 change: 1 addition & 0 deletions bindings/swift/Ccapstone/MCAsmInfo.h
1 change: 1 addition & 0 deletions bindings/swift/Ccapstone/MCDisassembler.h
1 change: 1 addition & 0 deletions bindings/swift/Ccapstone/MCFixedLenDisassembler.h
1 change: 1 addition & 0 deletions bindings/swift/Ccapstone/MCInst.c
1 change: 1 addition & 0 deletions bindings/swift/Ccapstone/MCInst.h
1 change: 1 addition & 0 deletions bindings/swift/Ccapstone/MCInstPrinter.c
1 change: 1 addition & 0 deletions bindings/swift/Ccapstone/MCInstPrinter.h
1 change: 1 addition & 0 deletions bindings/swift/Ccapstone/MCInstrDesc.c
1 change: 1 addition & 0 deletions bindings/swift/Ccapstone/MCInstrDesc.h
1 change: 1 addition & 0 deletions bindings/swift/Ccapstone/MCRegisterInfo.c
1 change: 1 addition & 0 deletions bindings/swift/Ccapstone/MCRegisterInfo.h
1 change: 1 addition & 0 deletions bindings/swift/Ccapstone/Mapping.c
1 change: 1 addition & 0 deletions bindings/swift/Ccapstone/Mapping.h
1 change: 1 addition & 0 deletions bindings/swift/Ccapstone/MathExtras.h
1 change: 1 addition & 0 deletions bindings/swift/Ccapstone/SStream.c
1 change: 1 addition & 0 deletions bindings/swift/Ccapstone/SStream.h
12 changes: 12 additions & 0 deletions bindings/swift/Ccapstone/UpdateSymlink.sh
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are these scripts necessary?
Should the swift package manager take care of installing the headers correctly?

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

target_dir="../../.."

find "$target_dir" -maxdepth 1 \( -name "*.h" -o -name "*.c" \) -print0 | while IFS= read -r -d $'\0' header_file; do

relative_path=$(echo "$header_file" | sed "s#^$PWD/##")

ln -s "$relative_path" .

echo "Created symlink: $(basename "$header_file") -> $relative_path"
done
1 change: 1 addition & 0 deletions bindings/swift/Ccapstone/arch
1 change: 1 addition & 0 deletions bindings/swift/Ccapstone/cs.c
1 change: 1 addition & 0 deletions bindings/swift/Ccapstone/cs_priv.h
1 change: 1 addition & 0 deletions bindings/swift/Ccapstone/cs_simple_types.h
11 changes: 11 additions & 0 deletions bindings/swift/Ccapstone/include/capstone/UpdateSymlink.sh
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also this one

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

target_dir="../../../../../include/capstone"

find "$target_dir" -name "*.h" -print0 | while IFS= read -r -d $'\0' header_file; do
relative_path=$(echo "$header_file" | sed "s#^$PWD/##")

ln -s "$relative_path" .

echo "Created symlink: $(basename "$header_file") -> $relative_path"
done
1 change: 1 addition & 0 deletions bindings/swift/Ccapstone/include/capstone/aarch64.h
1 change: 1 addition & 0 deletions bindings/swift/Ccapstone/include/capstone/alpha.h
1 change: 1 addition & 0 deletions bindings/swift/Ccapstone/include/capstone/arc.h
1 change: 1 addition & 0 deletions bindings/swift/Ccapstone/include/capstone/arm.h
1 change: 1 addition & 0 deletions bindings/swift/Ccapstone/include/capstone/arm64.h
1 change: 1 addition & 0 deletions bindings/swift/Ccapstone/include/capstone/bpf.h
1 change: 1 addition & 0 deletions bindings/swift/Ccapstone/include/capstone/capstone.h
1 change: 1 addition & 0 deletions bindings/swift/Ccapstone/include/capstone/cs_operand.h
1 change: 1 addition & 0 deletions bindings/swift/Ccapstone/include/capstone/evm.h
1 change: 1 addition & 0 deletions bindings/swift/Ccapstone/include/capstone/hppa.h
1 change: 1 addition & 0 deletions bindings/swift/Ccapstone/include/capstone/loongarch.h
1 change: 1 addition & 0 deletions bindings/swift/Ccapstone/include/capstone/m680x.h
1 change: 1 addition & 0 deletions bindings/swift/Ccapstone/include/capstone/m68k.h
1 change: 1 addition & 0 deletions bindings/swift/Ccapstone/include/capstone/mips.h
1 change: 1 addition & 0 deletions bindings/swift/Ccapstone/include/capstone/mos65xx.h
1 change: 1 addition & 0 deletions bindings/swift/Ccapstone/include/capstone/platform.h
1 change: 1 addition & 0 deletions bindings/swift/Ccapstone/include/capstone/ppc.h
1 change: 1 addition & 0 deletions bindings/swift/Ccapstone/include/capstone/riscv.h
1 change: 1 addition & 0 deletions bindings/swift/Ccapstone/include/capstone/sh.h
1 change: 1 addition & 0 deletions bindings/swift/Ccapstone/include/capstone/sparc.h
1 change: 1 addition & 0 deletions bindings/swift/Ccapstone/include/capstone/systemz.h
1 change: 1 addition & 0 deletions bindings/swift/Ccapstone/include/capstone/tms320c64x.h
1 change: 1 addition & 0 deletions bindings/swift/Ccapstone/include/capstone/tricore.h
1 change: 1 addition & 0 deletions bindings/swift/Ccapstone/include/capstone/wasm.h
1 change: 1 addition & 0 deletions bindings/swift/Ccapstone/include/capstone/x86.h
1 change: 1 addition & 0 deletions bindings/swift/Ccapstone/include/capstone/xcore.h
1 change: 1 addition & 0 deletions bindings/swift/Ccapstone/include/capstone/xtensa.h
1 change: 1 addition & 0 deletions bindings/swift/Ccapstone/platform.h
1 change: 1 addition & 0 deletions bindings/swift/Ccapstone/utils.c
1 change: 1 addition & 0 deletions bindings/swift/Ccapstone/utils.h
Loading
Loading