Skip to content
Merged
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
39 changes: 30 additions & 9 deletions CalcPi.jl/deps/build.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,37 @@ if isdir(DEV_DIR)
# Always copy to libcalcpi_rs.<ext> in deps/ for consistency
cp(libcalcpi_path, joinpath(@__DIR__, "libcalcpi_rs.$(dlext)"); force=true)

cd(joinpath(dirname(@__DIR__), "utils")) do
run(`$(Base.julia_cmd()) --project generate_C_API.jl`)
# Generate C_API.jl in a temporary directory first, then copy to the appropriate location
# This handles the case where the package is installed and src/ is read-only
package_dir = dirname(@__DIR__)
src_dir = joinpath(package_dir, "src")
temp_output_dir = mktempdir()

cd(joinpath(package_dir, "utils")) do
run(`$(Base.julia_cmd()) --project generate_C_API.jl --output-dir $temp_output_dir`)
end

# Remove CEnum import if not needed (post-processing)
c_api_path = joinpath(dirname(@__DIR__), "src", "C_API.jl")
if isfile(c_api_path)
content = read(c_api_path, String)
# Remove CEnum import if present
content = replace(content, r"using CEnum: CEnum, @cenum\n\n" => "")
write(c_api_path, content)
# Copy generated file to src/ if writable
temp_c_api_path = joinpath(temp_output_dir, "C_API.jl")
if isfile(temp_c_api_path)
c_api_path = joinpath(src_dir, "C_API.jl")
# Check if src/ is writable by testing write access
try
content = read(temp_c_api_path, String)
# Remove CEnum import if not needed (post-processing)
content = replace(content, r"using CEnum: CEnum, @cenum\n\n" => "")
# Try to write to src/
open(c_api_path, "w") do f
write(f, content)
end
println("Generated C_API.jl in src/")
catch e
# If src/ is read-only (e.g., installed package), skip writing
# The existing C_API.jl in the package should be used
println("Warning: Cannot write to src/ (package may be installed). Using existing C_API.jl.")
end
rm(temp_output_dir; recursive=true)
else
error("Failed to generate C_API.jl")
end
end
30 changes: 29 additions & 1 deletion CalcPi.jl/utils/generate_C_API.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,46 @@ function print_help()
println()
println("Options:")
println(" --calcpi-rs-dir PATH Specify the calcpi-rs directory path")
println(" --output-dir PATH Specify the output directory for C_API.jl")
println(" --help, -h Show this help message")
println()
println("Examples:")
println(" julia generate_C_API.jl --calcpi-rs-dir /path/to/calcpi-rs")
println(" julia generate_C_API.jl --output-dir /path/to/output")
println()
println("Default: Uses ../deps/calcpi-rs relative to this script")
println(" Output goes to ../src/C_API.jl relative to this script")
end

# Parse command line arguments
calcpi_rs_dir = nothing
for (i, arg) in enumerate(ARGS)
output_dir = nothing
i = 1
while i <= length(ARGS)
arg = ARGS[i]
if arg == "--help" || arg == "-h"
print_help()
exit(0)
elseif arg == "--calcpi-rs-dir"
if i + 1 <= length(ARGS)
global calcpi_rs_dir
calcpi_rs_dir = ARGS[i + 1]
i += 2
else
println("Error: --calcpi-rs-dir requires a path argument")
exit(1)
end
elseif arg == "--output-dir"
if i + 1 <= length(ARGS)
global output_dir
output_dir = ARGS[i + 1]
i += 2
else
println("Error: --output-dir requires a path argument")
exit(1)
end
else
i += 1
end
end

Expand Down Expand Up @@ -74,6 +92,16 @@ else
options = Dict{String,Any}()
end

# Override output path if specified
if output_dir !== nothing
output_dir = normpath(abspath(output_dir))
if !isdir(output_dir)
mkpath(output_dir)
end
options["output_file_path"] = joinpath(output_dir, "C_API.jl")
println("Output directory: $output_dir")
end

# add compiler flags, e.g. "-DXXXXXXXXX"
args = get_default_args()
push!(args, "-I$include_dir")
Expand Down
1 change: 1 addition & 0 deletions run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
julia -e 'using Pkg; Pkg.activate(temp=true); Pkg.add(url="git@github.com:AtelierArith/RustToolChainExamples.jl.git", subdir="CalcPi.jl"); Pkg.test("CalcPi")'
Loading