Skip to content

Commit b983bef

Browse files
committed
Try using the library
1 parent ce0f007 commit b983bef

File tree

6 files changed

+135
-11
lines changed

6 files changed

+135
-11
lines changed

contrib/juliac/juliac-buildscript.jl

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ const C_friendly_types = Base.IdSet{Any}([ # a few of these are redundant to
3434
RawFD,
3535
])
3636

37-
function is_c_friendly(@nospecialize(T::DataType))
38-
T <: Ptr && return is_c_friendly(T.parameters[1])
39-
return T in C_friendly_types
40-
end
41-
4237
function recursively_add_types!(types::Base.IdSet{DataType}, @nospecialize(T::DataType))
43-
if !is_c_friendly(T)
38+
if T C_friendly_types
39+
if T <: Ptr
40+
return recursively_add_types!(types, T.parameters[1])
41+
elseif T <: Array
42+
return recursively_add_types!(types, T.parameters[1])
43+
end
4444
T.name.module === Core && error("invalid type for juliac: ", T) # exclude internals (they may change)
4545
push!(types, T)
4646
end
@@ -49,6 +49,7 @@ function recursively_add_types!(types::Base.IdSet{DataType}, @nospecialize(T::Da
4949
recursively_add_types!(types, S)
5050
end
5151
end
52+
return types
5253
end
5354

5455
# Load user code

test/trimming/Makefile

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ JULIAC_BUILDSCRIPT := $(shell $(JULIA) -e 'print(joinpath(Sys.BINDIR, Base.DATAR
3333

3434
#=============================================================================
3535

36-
release: $(BIN)/hello$(EXE) $(BIN)/basic_jll$(EXE) $(BIN)/simplelib-o.a
36+
release: $(BIN)/hello$(EXE) $(BIN)/basic_jll$(EXE) $(BIN)/libsimple.$(SHLIB_EXT) $(BIN)/capplication$(EXE)
3737

3838
$(BIN)/hello-o.a: $(SRCDIR)/hello.jl $(JULIAC_BUILDSCRIPT)
3939
$(JULIA) -t 1 -J $(JULIA_LIBDIR)/julia/sys.$(SHLIB_EXT) --startup-file=no --history-file=no --output-o $@ --output-incremental=no --strip-ir --strip-metadata --experimental --trim $(JULIAC_BUILDSCRIPT) $< --output-exe true
@@ -42,20 +42,26 @@ $(BIN)/basic_jll-o.a: $(SRCDIR)/basic_jll.jl $(JULIAC_BUILDSCRIPT)
4242
$(JULIA) -t 1 -J $(JULIA_LIBDIR)/julia/sys.$(SHLIB_EXT) --startup-file=no --history-file=no --project=$(SRCDIR) -e "using Pkg; Pkg.instantiate()"
4343
$(JULIA) -t 1 -J $(JULIA_LIBDIR)/julia/sys.$(SHLIB_EXT) --startup-file=no --history-file=no --project=$(SRCDIR) --output-o $@ --output-incremental=no --strip-ir --strip-metadata --experimental --trim $(JULIAC_BUILDSCRIPT) $< --output-exe true
4444

45-
$(BIN)/simplelib-o.a: $(SRCDIR)/simplelib.jl $(JULIAC_BUILDSCRIPT)
46-
$(JULIA) -t 1 -J $(JULIA_LIBDIR)/julia/sys.$(SHLIB_EXT) --startup-file=no --history-file=no --output-o $@ --output-incremental=no --strip-ir --strip-metadata --experimental --trim $(JULIAC_BUILDSCRIPT) $< --output-lib true $(BIN)/bindinginfo_simplelib.log
45+
$(BIN)/libsimple-o.a: $(SRCDIR)/libsimple.jl $(JULIAC_BUILDSCRIPT)
46+
$(JULIA) -t 1 -J $(JULIA_LIBDIR)/julia/sys.$(SHLIB_EXT) --startup-file=no --history-file=no --output-o $@ --output-incremental=no --strip-ir --strip-metadata --experimental --trim $(JULIAC_BUILDSCRIPT) $< --output-lib true $(BIN)/bindinginfo_libsimple.log
4747

4848
$(BIN)/hello$(EXE): $(BIN)/hello-o.a
4949
$(CC) -o $@ $(WHOLE_ARCHIVE) $< $(NO_WHOLE_ARCHIVE) $(CPPFLAGS_ADD) $(CPPFLAGS) $(CFLAGS_ADD) $(CFLAGS) $(LDFLAGS_ADD) $(LDFLAGS)
5050

5151
$(BIN)/basic_jll$(EXE): $(BIN)/basic_jll-o.a
5252
$(CC) -o $@ $(WHOLE_ARCHIVE) $< $(NO_WHOLE_ARCHIVE) $(CPPFLAGS_ADD) $(CPPFLAGS) $(CFLAGS_ADD) $(CFLAGS) $(LDFLAGS_ADD) $(LDFLAGS)
5353

54-
check: $(BIN)/hello$(EXE) $(BIN)/basic_jll$(EXE) $(BIN)/simplelib-o.a
54+
$(BIN)/libsimple.$(SHLIB_EXT): $(BIN)/libsimple-o.a
55+
$(CC) -shared -o $(BIN)/libsimple.$(SHLIB_EXT) $(BIN)/libsimple-o.a $(LDFLAGS_ADD) $(LDFLAGS)
56+
57+
$(BIN)/capplication$(EXE): $(BIN)/capplication.c $(BIN)/libsimple.h $(BIN)/libsimple.$(SHLIB_EXT)
58+
$(CC) -I$(BIN) -I$(SRCDIR) -I$(JULIA_LIBDIR) -o $@ $< $(LDFLAGS_ADD) $(LDFLAGS) -Wl,-rpath=. -L. -lsimple $(CPPFLAGS_ADD) $(CPPFLAGS) $(CFLAGS_ADD) $(CFLAGS)
59+
60+
check: $(BIN)/hello$(EXE) $(BIN)/basic_jll$(EXE) $(BIN)/libsimple.$(SHLIB_EXT) $(BIN)/capplication$(EXE)
5561
$(JULIA) --depwarn=error $(SRCDIR)/trimming.jl $<
5662

5763
clean:
58-
-rm -f $(BIN)/hello$(EXE) $(BIN)/basic_jll$(EXE) $(BIN)/hello-o.a $(BIN)/basic_jll-o.a $(BIN)/simplelib-o.a $(BIN)/bindinginfo_simplelib.log
64+
-rm -f $(BIN)/hello$(EXE) $(BIN)/basic_jll$(EXE) $(BIN)/hello-o.a $(BIN)/basic_jll-o.a $(BIN)/libsimple-o.a $(BIN)/libsimple.$(SHLIB_EXT) $(BIN)/capplication$(EXE) $(BIN)/bindinginfo_libsimple.log
5965

6066
.PHONY: release clean check
6167

test/trimming/Manifest.toml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# This file is machine-generated - editing it directly is not advised
2+
3+
julia_version = "1.13.0-DEV"
4+
manifest_format = "2.0"
5+
project_hash = "86600fa55a0cab45eb9ff242236c1092e99e878b"
6+
7+
[[deps.Artifacts]]
8+
uuid = "56f22d72-fd6d-98f1-02f0-08ddc0907c33"
9+
version = "1.11.0"
10+
11+
[[deps.Dates]]
12+
deps = ["Printf"]
13+
uuid = "ade2ca70-3891-5945-98fb-dc099432e06a"
14+
version = "1.11.0"
15+
16+
[[deps.JLLWrappers]]
17+
deps = ["Artifacts", "Preferences"]
18+
git-tree-sha1 = "0533e564aae234aff59ab625543145446d8b6ec2"
19+
uuid = "692b3bcd-3c85-4b1f-b108-f13ce0eb3210"
20+
version = "1.7.1"
21+
22+
[[deps.Libdl]]
23+
uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
24+
version = "1.11.0"
25+
26+
[[deps.Preferences]]
27+
deps = ["TOML"]
28+
git-tree-sha1 = "9306f6085165d270f7e3db02af26a400d580f5c6"
29+
uuid = "21216c6a-2e73-6563-6e65-726566657250"
30+
version = "1.4.3"
31+
32+
[[deps.Printf]]
33+
deps = ["Unicode"]
34+
uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7"
35+
version = "1.11.0"
36+
37+
[[deps.TOML]]
38+
deps = ["Dates"]
39+
uuid = "fa267f1f-6049-4f14-aa54-33bafae1ed76"
40+
version = "1.0.3"
41+
42+
[[deps.Unicode]]
43+
uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5"
44+
version = "1.11.0"
45+
46+
[[deps.Zstd_jll]]
47+
deps = ["Libdl"]
48+
path = "Zstd_jll"
49+
uuid = "3161d3a3-bdf6-5164-811a-617609db77b4"
50+
version = "1.5.7+1"

test/trimming/capplication.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <stdio.h>
2+
#include "libsimple.h"
3+
4+
int main() {
5+
// Example usage of the functions defined in libsimple.h
6+
CVectorPair_Float32 vecPair;
7+
vecPair.from.length = 3;
8+
vecPair.from.data = (float[]){1.0f, 2.0f, 3.0f};
9+
vecPair.to.length = 3;
10+
vecPair.to.data = (float[]){4.0f, 5.0f, 6.0f};
11+
12+
float sum = copyto_and_sum(&vecPair);
13+
printf("Sum of copied values: %f\n", sum);
14+
15+
MyTwoVec list[] = {{1, 2}, {1, 2}, {3, 4}};
16+
int32_t count = countsame(list, 3);
17+
printf("Count of same vectors: %d\n", count);
18+
19+
return 0;
20+
}

test/trimming/libsimple.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#ifndef SIMPLELIB_H
2+
#define SIMPLELIB_H
3+
4+
#include <stdint.h>
5+
#include <stddef.h>
6+
7+
struct _MyTwoVec_ {
8+
int32_t x;
9+
int32_t y;
10+
};
11+
typedef struct _MyTwoVec_ MyTwoVec;
12+
13+
struct _CVector_Float32_ {
14+
int32_t length;
15+
float *data;
16+
};
17+
typedef struct _CVector_Float32_ CVector_Float32;
18+
19+
struct _CVectorPair_Float32_ {
20+
CVector_Float32 from;
21+
CVector_Float32 to;
22+
};
23+
typedef struct _CVectorPair_Float32_ CVectorPair_Float32;
24+
25+
float copyto_and_sum(CVectorPair_Float32 *fromto);
26+
int32_t countsame(MyTwoVec *list, int32_t length);
27+
28+
#endif // SIMPLELIB_H

test/trimming/simplelib.jl renamed to test/trimming/libsimple.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,31 @@ struct CVectorPair{T}
1111
to::CVector{T}
1212
end
1313

14+
struct MyTwoVec
15+
x::Int32
16+
y::Int32
17+
end
18+
1419
Base.@ccallable "copyto_and_sum" function badname(fromto::CVectorPair{Float32})::Float32
1520
from, to = unsafe_wrap(Array, fromto.from.data, fromto.from.length), unsafe_wrap(Array, fromto.to.data, fromto.to.length)
1621
copyto!(to, from)
1722
return sum(to)
1823
end
1924

25+
# Base.@ccallable function countsame(list::Ptr{MyTwoVec}, n::Int32)::Int32
26+
Base.@ccallable function countsame(list::Vector{MyTwoVec})::Int32
27+
count = 0
28+
# list = unsafe_wrap(Array, list, n)
29+
for v in list
30+
if v.x == v.y
31+
count += 1
32+
end
33+
end
34+
return count
35+
end
36+
37+
export countsame, copyto_and_sum
38+
2039
# FIXME? varargs
2140
# Base.@ccallable function printints(x::Cint...)::Nothing
2241
# for i in 1:length(x)

0 commit comments

Comments
 (0)