Skip to content

Refactor C dependencies: Use vcpkg, auto VS setup, add libtcc #543

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
4 changes: 2 additions & 2 deletions .github/workflows/macos-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,15 @@ jobs:
- name: Set up the environment
run: sh ./tools/metacall-environment.sh $METACALL_INSTALL_OPTIONS
env:
METACALL_INSTALL_OPTIONS: base python nodejs typescript java ruby wasm rpc file cobol go backtrace #netcore5 c rust rapidjson funchook swig pack # clangformat v8rep51 coverage
METACALL_INSTALL_OPTIONS: base python nodejs typescript java ruby c wasm rpc file cobol go backtrace #netcore5 rust rapidjson funchook swig pack # clangformat v8rep51 coverage

- name: Configure
run: |
cd build
. .env
bash ../tools/metacall-configure.sh $METACALL_CONFIGURE_OPTIONS
env:
METACALL_CONFIGURE_OPTIONS: ${{ matrix.options.build }} ${{ matrix.options.sanitizer }} scripts ports tests python nodejs typescript java ruby wasm rpc file cobol go benchmarks install # netcore5 c rust examples pack # v8 coverage
METACALL_CONFIGURE_OPTIONS: ${{ matrix.options.build }} ${{ matrix.options.sanitizer }} scripts ports tests python nodejs typescript java ruby c wasm rpc file cobol go benchmarks install # netcore5 rust examples pack # v8 coverage

- name: Build
working-directory: ./build
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/windows-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jobs:
- name: Set up the environment
run: cmd.exe /c "powershell .\tools\metacall-environment.ps1 $Env:METACALL_INSTALL_OPTIONS"
env:
METACALL_INSTALL_OPTIONS: python nodejs java ruby typescript wasm rpc file # netcore5 java c cobol rust rapidjson funchook swig pack # clangformat v8rep51 coverage
METACALL_INSTALL_OPTIONS: c python nodejs java ruby typescript wasm rpc file # netcore5 java cobol rust rapidjson funchook swig pack # clangformat v8rep51 coverage

- name: Configure
run: |
Expand All @@ -57,7 +57,7 @@ jobs:
cd "$METACALL_PATH\build"
cmd.exe /c "powershell ..\tools\metacall-configure.ps1 $Env:METACALL_BUILD_OPTIONS"
env:
METACALL_BUILD_OPTIONS: ${{ matrix.options.build }} ${{ matrix.options.sanitizer }} scripts ports tests python nodejs java ruby typescript wasm rpc file # netcore5 java c cobol rust examples install pack benchmarks # v8 coverage
METACALL_BUILD_OPTIONS: ${{ matrix.options.build }} ${{ matrix.options.sanitizer }} scripts ports tests c python nodejs java ruby typescript wasm rpc file # netcore5 java cobol rust examples install pack benchmarks # v8 coverage

- name: Build
working-directory: ./build
Expand Down
2 changes: 1 addition & 1 deletion cmake/InstallLibTCC.cmake
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,4 @@ set(LIBTCC_FOUND TRUE)

mark_as_advanced(LIBTCC_INCLUDE_DIR LIBTCC_LIBRARY)

message(STATUS "Installing LibTCC ${LIBTCC_COMMIT_SHA}")
message(STATUS "Installing LibTCC ${LIBTCC_COMMIT_SHA}")
4 changes: 2 additions & 2 deletions cmake/Warnings.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ if(WARNINGS_ENABLED)
string(REPLACE "/W1" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
string(REPLACE "/W2" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
string(REPLACE "/W3" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W4 /Wall")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W2 /Wall")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /D _CTR_NONSTDC_NO_WARNINGS=1")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /D _CTR_SECURE_NO_WARNINGS=1")
set(WARNINGS_C_AVAILABLE 1)
Expand All @@ -105,7 +105,7 @@ if(WARNINGS_ENABLED)
string(REPLACE "/W1" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
string(REPLACE "/W2" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
string(REPLACE "/W3" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4 /Wall")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W2 /Wall")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /D _CTR_SECURE_CPP_OVERLOAD_STANDARD_NAMES=1")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /D _CTR_SECURE_CPP_OVERLOAD_STANDARD_NAMES_COUNT=1")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /D _CTR_NONSTDC_NO_WARNINGS=1")
Expand Down
4 changes: 3 additions & 1 deletion source/loaders/c_loader/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ endif()
find_package(LibTCC)

if(NOT LIBTCC_FOUND)
message(Status "###########checking in install libtcc###########")
include(InstallLibTCC)

message(Status "###########Done checking###########")
if(NOT LIBTCC_FOUND)
message(SEND_ERROR "TCC library not found")
return()
endif()
message(Status "###########Lib tcc found###########")
endif()

find_package(LibClang)
Expand Down
2 changes: 1 addition & 1 deletion source/loaders/c_loader/source/c_loader_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ namespace fs = std::experimental::filesystem;
#include <libtcc.h>

/* LibClang */
#include <clang-c/CXString.h>
#include <clang-c/Index.h>
#include <clang-c/CXString.h>

typedef struct loader_impl_c_type
{
Expand Down
137 changes: 137 additions & 0 deletions tools/metacall-environment.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,127 @@ function Set-Ruby {
Write-Output "-DRuby_LIBRARY_NAME=""$RubyDir/bin/x64-vcruntime140-ruby310.dll""" >> $EnvOpts
}


function Set-C {
$ErrorActionPreference = "Stop"
Write-Output "Installing C dependencies..."

# Ensure Chocolatey is set up
Set-Choco

# Set directories
Set-Location $ROOT_DIR
$DepsDir = Join-Path $ROOT_DIR 'dependencies'
$vcpkgDir = Join-Path $DepsDir 'vcpkg'

# Install vcpkg if missing
if (-not (Test-Path (Join-Path $vcpkgDir 'vcpkg.exe'))) {
Write-Output "Cloning vcpkg into $vcpkgDir..."
git clone --depth=1 https://github.com/microsoft/vcpkg.git $vcpkgDir
& (Join-Path $vcpkgDir 'bootstrap-vcpkg.bat')
& (Join-Path $vcpkgDir 'vcpkg.exe') integrate install
} else {
Write-Output "vcpkg already installed."
}

# Install libffi using vcpkg
Write-Output "Installing libffi using vcpkg..."
& (Join-Path $vcpkgDir 'vcpkg.exe') install libffi


# Define LLVM version and download URL
$llvmVersion = "19.1.7"
$llvmArchiveUrl = "https://github.com/llvm/llvm-project/releases/download/llvmorg-$llvmVersion/clang+llvm-$llvmVersion-x86_64-pc-windows-msvc.tar.xz"
$archivePath = "$env:TEMP\clang+llvm-$llvmVersion-x86_64-pc-windows-msvc.tar.xz"
$extractPath = "$env:TEMP\clang+llvm-$llvmVersion"
$finalPath = Join-Path $DepsDir 'llvm'

# Ensure dependencies directory exists
if (!(Test-Path $DepsDir)) {
New-Item -ItemType Directory -Path $DepsDir -Force | Out-Null
}

Download LLVM archive
Write-Output "Downloading LLVM $llvmVersion archive..."
Invoke-WebRequest -Uri $llvmArchiveUrl -OutFile $archivePath

# Extract the .xz file
Write-Output "Extracting .xz archive..."
tar -xf $archivePath -C $env:TEMP
$tarFile = $archivePath -replace '\.xz$', '' # Remove .xz extension

# Extract the .tar file
Write-Output "Extracting .tar archive..."
tar -xf $tarFile -C $env:TEMP

# Find the extracted folder
$extractedFolder = Get-ChildItem -Path $env:TEMP -Directory | Where-Object { $_.Name -match "clang\+llvm-$llvmVersion-x86_64-pc-windows-msvc" } | Select-Object -First 1

if ($extractedFolder) {
Write-Output "Moving extracted LLVM to $finalPath..."
Move-Item -Path $extractedFolder.FullName -Destination $finalPath -Force
} else {
Write-Output "Extracted LLVM folder not found!"
}

# Clean up
Write-Output "Cleaning up downloaded files..."
Remove-Item -Path $archivePath, $tarFile -Force

Write-Output "LLVM $llvmVersion successfully installed at $finalPath!"

# Write environment options for CMake configuration
$Env_Opts = "$ROOT_DIR\build\CMakeConfig.txt"
$vcpkgLibDir = "$DepsDir\vcpkg\installed\x64-windows\lib"
$vcpkgIncludeDir = "$DepsDir\vcpkg\installed\x64-windows\include"

$cmakeOptions = @(
"set(OPTION_BUILD_LOADERS_C ON CACHE BOOL `"Build C loaders`")"

"set(LIBFFI_LIBRARY `"$vcpkgLibDir\ffi.lib`" CACHE STRING `"Path to libffi library`")"
"set(LIBFFI_INCLUDE_DIR `"$vcpkgIncludeDir`" CACHE STRING `"Path to libffi include directory`")"

"set(LibClang_INCLUDE_DIR `"$finalPath\include`" CACHE STRING `"Path to libclang include directory`")"
"set(LIBCLANG_LIBRARY `"$finalPath\lib\libclang.lib`" CACHE STRING `"Path to libclang library`")"

"set(CMAKE_TOOLCHAIN_FILE `"$vcpkgDir\scripts\buildsystems\vcpkg.cmake`" CACHE STRING `"Path to vcpkg toolchain file`")"
)

$cmakeOptions | Out-File -Append -FilePath $Env_Opts

Write-Output "All dependencies installed and configured successfully."
}

function Clone-GitRepository {
param (
[string]$repositoryUrl,
[string]$destinationPath
)

# Check if Git is installed
if (-not (Get-Command git -ErrorAction SilentlyContinue)) {
Write-Error "Git is not installed. Please install Git and try again."
return
}

# Check if the destination path already exists
if (Test-Path $destinationPath) {
Write-Error "Destination path already exists. Please provide a different path."
return
}

# Clone the repository using Git
& git clone $repositoryUrl $destinationPath

# Check if the cloning was successful
if ($LASTEXITCODE -ne 0) {
Write-Error "Failed to clone the repository."
return
}

Write-Output "Repository cloned successfully."
}

function Set-TypeScript {
Write-Output "Install TypeScript"
npm i react@latest -g
Expand Down Expand Up @@ -279,6 +400,21 @@ function Set-Base {
}
}

function Set-Choco {
# Set directory for installation - Chocolatey does not lock
# down the directory if not the default
$InstallDir='C:\ProgramData\chocoportable'
$env:ChocolateyInstall="$InstallDir"

# If your PowerShell Execution policy is restrictive, you may
# not be able to get around that. Try setting your session to
# Bypass.
Set-ExecutionPolicy Bypass -Scope Process -Force;

# All install options - offline, proxy, etc at
# https://chocolatey.org/install
iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
}
# Configure
function Configure {
# Create option variables file
Expand Down Expand Up @@ -352,6 +488,7 @@ function Configure {
}
if ("$var" -eq 'c') {
Write-Output "c selected"
Set-C
}
if ("$var" -eq 'cobol') {
Write-Output "cobol selected"
Expand Down
3 changes: 1 addition & 2 deletions tools/metacall-environment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -650,9 +650,8 @@ sub_java(){
# C
sub_c(){
echo "configure c"

LLVM_VERSION_STRING=14
if [ "${OPERATIVE_SYSTEM}" = "Linux" ]; then
LLVM_VERSION_STRING=14

if [ "${LINUX_DISTRO}" = "debian" ]; then
UBUNTU_CODENAME=""
Expand Down
Loading