Introduce C library for Pkl (#1238)

This uses native-image to generate a C library for Pkl.
This generated library from native-image is wrapped with our own library,
in `pkl.h`.

This produces a static and a dynamic library for each os/arch variant
that Pkl currently supports.

Co-authored-by: Kushal Pisavadia <kushal.p@apple.com>
Co-authored-by: Jen Basch <jbasch94@gmail.com>
Co-authored-by: Islon Scherer <i_desouzascherer@apple.com>
This commit is contained in:
Daniel Chao
2026-07-25 04:15:26 +00:00
committed by GitHub
co-authored by Kushal Pisavadia Jen Basch Islon Scherer
parent 175e2b6273
commit 67df676359
51 changed files with 4982 additions and 349 deletions
+85
View File
@@ -0,0 +1,85 @@
::===----------------------------------------------------------------------===//
:: Copyright © 2026 Apple Inc. and the Pkl project authors. All rights reserved.
::
:: Licensed under the Apache License, Version 2.0 (the "License");
:: you may not use this file except in compliance with the License.
:: You may obtain a copy of the License at
::
:: https://www.apache.org/licenses/LICENSE-2.0
::
:: Unless required by applicable law or agreed to in writing, software
:: distributed under the License is distributed on an "AS IS" BASIS,
:: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
:: See the License for the specific language governing permissions and
:: limitations under the License.
::===----------------------------------------------------------------------===//
@echo off
setlocal EnableDelayedExpansion
REM Adapted from https://github.com/oracle/graal/issues/3053#issuecomment-1866735057
REM This script intercepts compiler arguments generated by GraalVM's native-image for
REM creating a shared library, and additionally generates a self-contained static library.
REM
REM Use with --native-compiler-path=${pathToThisScript}.bat
REM Determine the library name and output path based on the .dll argument. MSVC's output-naming
REM flag is passed as `/Fe<path>` with no space between the flag and the path, so strip that
REM prefix before parsing the path - otherwise ~dp/~n misparse the combined token.
set LIB_NAME=
set OUTPUT_PATH=
for %%P in (%*) do (
echo %%P | findstr /C:".dll" 1>nul
if !errorlevel!==0 (
set "ARG=%%~P"
if "!ARG:~0,3!"=="/Fe" set "ARG=!ARG:~3!"
for %%F in ("!ARG!") do (
set LIB_NAME=%%~nF
set OUTPUT_PATH=%%~dpF
)
)
)
REM Do a simple forward for any calls that are used to compile individual C files
IF "%LIB_NAME%"=="" (
cmd /c cl %*
exit /b
)
REM Setup log path and log file
set LOG_PATH=%OUTPUT_PATH%logs
set LOG_FILE=%LOG_PATH%\compiler_commands.txt
if not exist %LOG_PATH% mkdir %LOG_PATH%
echo Working directory: %CD% > %LOG_FILE%
echo Output path: %OUTPUT_PATH% >> %LOG_FILE%
echo Library name: %LIB_NAME% >> %LOG_FILE%
echo ===================================================== >> %LOG_FILE%
echo SHARED LIBRARY >> %LOG_FILE%
echo ===================================================== >> %LOG_FILE%
set CL_ARGS=%*
echo cl.exe %CL_ARGS% >> %LOG_FILE%
cmd /c cl.exe %CL_ARGS%
echo ===================================================== >> %LOG_FILE%
echo STATIC LIBRARY >> %LOG_FILE%
echo ===================================================== >> %LOG_FILE%
REM lib.exe can combine .obj and .lib inputs directly into a single output archive, so unlike
REM build_unix.sh we don't need to manually extract members from input archives first - we just
REM collect every .obj/.lib argument that was passed to the shared-library link and re-archive
REM them together into one self-contained static library with no runtime DLL dependency.
REM We don't want to overwrite the import library needed to link against the DLL, so we append
REM "_s" to indicate that it is the static variant.
set STATIC_INPUTS=
for %%P in (%*) do (
set ARG=%%P
if /I "!ARG:~-4!"==".obj" set STATIC_INPUTS=!STATIC_INPUTS! %%P
if /I "!ARG:~-4!"==".lib" set STATIC_INPUTS=!STATIC_INPUTS! %%P
)
echo ======= Static inputs >> %LOG_FILE%
echo %STATIC_INPUTS% >> %LOG_FILE%
set LIB_ARGS=/OUT:%OUTPUT_PATH%%LIB_NAME%_s.lib %STATIC_INPUTS%
echo lib.exe %LIB_ARGS% >> %LOG_FILE%
cmd /c lib.exe %LIB_ARGS%