Skip to content

Commit

Permalink
add cxxwrap2
Browse files Browse the repository at this point in the history
  • Loading branch information
terasakisatoshi committed May 6, 2024
1 parent df357a0 commit ad5c027
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 0 deletions.
1 change: 1 addition & 0 deletions cxxwrap2/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
libhello*
15 changes: 15 additions & 0 deletions cxxwrap2/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
cmake_minimum_required(VERSION 3.15)
project(cxxwrap2)
# https://github.com/JuliaInterop/libcxxwrap-julia/blob/main/CMakeLists.txt
# https://github.com/JuliaInterop/libcxxwrap-julia/blob/main/testlib-builder/src/testlib/CMakeLists.txt
find_package(JlCxx)
get_target_property(JlCxx_location JlCxx::cxxwrap_julia LOCATION)
get_filename_component(JlCxx_location ${JlCxx_location} DIRECTORY)

add_library(hello SHARED hello.cpp)

message("-- AAA" ${Julia_LIBRARY})
message(STATUS "Found JlCxx at ${JlCxx_location}")
message("-- BBB" ${JLCXX_TARGET} JlCxx::cxxwrap_julia)

target_link_libraries(hello JlCxx::cxxwrap_julia ${Julia_LIBRARY})
6 changes: 6 additions & 0 deletions cxxwrap2/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM julia:1.10.3

RUN apt-get update && apt-get install -y --no-install-recommends build-essential cmake \
&& apt-get clean && rm -rf /var/lib/apt/lists/*

RUN julia -e 'using Pkg; Pkg.add("CxxWrap"); Pkg.precompile()'
86 changes: 86 additions & 0 deletions cxxwrap2/Manifest.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# This file is machine-generated - editing it directly is not advised

julia_version = "1.10.3"
manifest_format = "2.0"
project_hash = "3cef41461bc070f5890a35a6516e3e4c0caa014a"

[[deps.Artifacts]]
uuid = "56f22d72-fd6d-98f1-02f0-08ddc0907c33"

[[deps.Base64]]
uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"

[[deps.CxxWrap]]
deps = ["Libdl", "MacroTools", "libcxxwrap_julia_jll"]
git-tree-sha1 = "ba1a559afd562447710513525030d6ad83e5868a"
uuid = "1f15a43c-97ca-5a2a-ae31-89f07a497df4"
version = "0.15.1"

[[deps.Dates]]
deps = ["Printf"]
uuid = "ade2ca70-3891-5945-98fb-dc099432e06a"

[[deps.InteractiveUtils]]
deps = ["Markdown"]
uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240"

[[deps.JLLWrappers]]
deps = ["Artifacts", "Preferences"]
git-tree-sha1 = "7e5d6779a1e09a36db2a7b6cff50942a0a7d0fca"
uuid = "692b3bcd-3c85-4b1f-b108-f13ce0eb3210"
version = "1.5.0"

[[deps.Libdl]]
uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb"

[[deps.Logging]]
uuid = "56ddb016-857b-54e1-b83d-db4d58db5568"

[[deps.MacroTools]]
deps = ["Markdown", "Random"]
git-tree-sha1 = "2fa9ee3e63fd3a4f7a9a4f4744a52f4856de82df"
uuid = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
version = "0.5.13"

[[deps.Markdown]]
deps = ["Base64"]
uuid = "d6f4376e-aef5-505a-96c1-9c027394607a"

[[deps.Preferences]]
deps = ["TOML"]
git-tree-sha1 = "9306f6085165d270f7e3db02af26a400d580f5c6"
uuid = "21216c6a-2e73-6563-6e65-726566657250"
version = "1.4.3"

[[deps.Printf]]
deps = ["Unicode"]
uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7"

[[deps.Random]]
deps = ["SHA"]
uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

[[deps.SHA]]
uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce"
version = "0.7.0"

[[deps.Serialization]]
uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b"

[[deps.TOML]]
deps = ["Dates"]
uuid = "fa267f1f-6049-4f14-aa54-33bafae1ed76"
version = "1.0.3"

[[deps.Test]]
deps = ["InteractiveUtils", "Logging", "Random", "Serialization"]
uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[[deps.Unicode]]
uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5"

[[deps.libcxxwrap_julia_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl"]
git-tree-sha1 = "0f10092db771939e29fd13a67967f5c63212fdfe"
uuid = "3eaa8342-bff7-56a5-9981-c04077f7cee7"
version = "0.12.3+0"
4 changes: 4 additions & 0 deletions cxxwrap2/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[deps]
CxxWrap = "1f15a43c-97ca-5a2a-ae31-89f07a497df4"
Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
10 changes: 10 additions & 0 deletions cxxwrap2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Usage

```
$ docker build -t cxxwrap2 .
$ docker run --rm -it -v $PWD:/work -w /work cxxwrap2 bash -c 'bash build.sh && julia callcxx.jl'
```

# Reference

[my gist](https://gist.github.com/terasakisatoshi/b6a7121cd570f6739992345095b07d62)
26 changes: 26 additions & 0 deletions cxxwrap2/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Detect operating system
OS_TYPE=$(uname -s)

# Set shared library extension based on OS
if [[ $OS_TYPE == "Linux" ]]; then
SHARED_LIB_EXT=".so"
elif [[ $OS_TYPE == "Darwin" ]]; then
SHARED_LIB_EXT=".dylib"
else
echo "Unsupported operating system: $OS_TYPE"
exit 1
fi

# Get Julia installation paths
JULIA_PREFIX=`julia --project -e 'joinpath(Sys.BINDIR, "..") |> abspath |> print'`
CXXWRAP_PREFIX=`julia --project -e 'using CxxWrap; CxxWrap.prefix_path() |> print'`
cmake -S . -B ./build -DCMAKE_BUILD_TYPE=Release -DJulia_PREFIX=$JULIA_PREFIX -DCMAKE_PREFIX_PATH=$CXXWRAP_PREFIX
cmake --build ./build --config Release -j `nproc`

# Build shared library with appropriate extension
#g++ -fPIC -shared -std=c++17 \
# -I${CXXWRAP_PREFIX}/include/ \
# -L${CXXWRAP_PREFIX}/lib/ \
# -I${JULIA_PREFIX}/include/julia \
# -L${JULIA_PREFIX}/lib \
# -ljulia -lcxxwrap_julia hello.cpp -o libhello${SHARED_LIB_EXT}
18 changes: 18 additions & 0 deletions cxxwrap2/callcxx.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Test

# Load the module and generate the functions
module CppHello
using Libdl: dlext

using CxxWrap
@wrapmodule(() -> joinpath("./build", "libhello.$(dlext)"))

function __init__()
@initcxx
end
end

@testset "greet" begin
# Call greet and show the result
@test CppHello.greet("Hello World") == "Hello World"
end
13 changes: 13 additions & 0 deletions cxxwrap2/hello.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <string>

#include "jlcxx/jlcxx.hpp"

std::string greet(std::string msg)
{
return msg;
}

JLCXX_MODULE define_julia_module(jlcxx::Module& mod)
{
mod.method("greet", &greet);
}

0 comments on commit ad5c027

Please sign in to comment.