Skip to content

Commit 928d535

Browse files
authored
Merge pull request #305 from JuliaParallel/sb/build
Improve build process
2 parents b53f68e + d725c54 commit 928d535

File tree

4 files changed

+46
-36
lines changed

4 files changed

+46
-36
lines changed

deps/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
deps.jl
2+
consts.jl
23
build.log
34
gen_consts.c
45
gen_consts

deps/build.jl

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,34 @@ mpicc = get(ENV, "JULIA_MPICC") do
1515
end
1616
end
1717

18-
mpiexec = get(ENV, "JULIA_MPIEXEC") do
19-
if MPI_PATH !== nothing
18+
const mpiexec = get(ENV, "JULIA_MPIEXEC") do
19+
if MPI_PATH !== nothing && Sys.isexecutable(joinpath(MPI_PATH,"bin","mpiexec"))
2020
joinpath(MPI_PATH,"bin","mpiexec")
2121
else
22-
"mpiexec"
22+
Sys.which("mpiexec")
23+
end
24+
end
25+
26+
const libmpi = get(ENV, "JULIA_MPI_LIBRARY") do
27+
libmpi = find_library(["libmpi", "msmpi", "libmpich"],
28+
MPI_LIBRARY_PATH !== nothing ? [MPI_LIBRARY_PATH] : [])
29+
try
30+
# expand paths
31+
dlpath(libmpi)
32+
catch e
33+
error("No MPI library found.\nEnsure an MPI implementation is loaded, or set the `JULIA_MPI_PATH` variable.")
2334
end
2435
end
2536

37+
2638
if haskey(ENV, "JULIA_MPI_CFLAGS")
2739
CFLAGS = split(ENV["JULIA_MPI_CFLAGS"])
2840
else
29-
CFLAGS = ["-lmpi"]
41+
lname, = split(basename(libmpi),'.')
42+
if startswith(lname, "lib")
43+
lname = lname[4:end]
44+
end
45+
CFLAGS = ["-l$lname"]
3046
if MPI_LIBRARY_PATH !== nothing
3147
push!(CFLAGS, "-L$(MPI_LIBRARY_PATH)")
3248
end
@@ -35,19 +51,8 @@ else
3551
end
3652
end
3753

38-
const libmpi = find_library(Sys.iswindows() ? "msmpi.dll" : "libmpi",
39-
MPI_LIBRARY_PATH !== nothing ? [MPI_LIBRARY_PATH] : [])
40-
41-
if libmpi == ""
42-
error("No MPI library found.\nEnsure an MPI implementation is loaded, or set the `JULIA_MPI_PATH` variable.")
43-
end
44-
45-
# expand paths
46-
libmpi = dlpath(libmpi)
4754
libsize = filesize(libmpi)
4855

49-
mpiexec = Sys.which(mpiexec)
50-
5156
@info "Using MPI library $libmpi"
5257

5358
function Get_version()
@@ -74,18 +79,15 @@ open("deps.jl","w") do f
7479
println(f, :(const libmpi_size = $libsize))
7580
println(f, :(const MPI_VERSION = $MPI_VERSION))
7681
println(f, :(const mpiexec = $mpiexec))
77-
end
7882

79-
if Sys.iswindows()
80-
open("deps.jl","a") do f
83+
if Sys.iswindows()
8184
println(f, :(include("consts_msmpi.jl")))
82-
end
83-
else
84-
include("gen_consts.jl")
85+
else
86+
include("gen_consts.jl")
8587

86-
run(`$mpicc gen_consts.c -o gen_consts $CFLAGS`)
88+
run(`$mpicc gen_consts.c -o gen_consts $CFLAGS`)
89+
run(`$mpiexec -n 1 ./gen_consts`)
8790

88-
open("deps.jl","a") do f
89-
run(pipeline(`./gen_consts`, stdout = f))
91+
println(f, :(include("consts.jl")))
9092
end
9193
end

deps/gen_consts.jl

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -117,44 +117,50 @@ open("gen_consts.c","w") do f
117117
int main(int argc, char *argv[]) {
118118
MPI_Init(&argc, &argv);
119119
120+
FILE *fptr;
121+
fptr = fopen("consts.jl", "w");
122+
123+
fprintf(fptr, "# This file is automatically generated\\n");
124+
fprintf(fptr, "# Do not edit\\n");
120125
""")
121126

122-
println(f," printf(\"const MPI_Status_size = %d\\n\", (int)sizeof(MPI_Status));")
123-
println(f," printf(\"const MPI_Status_Source_offset = %d\\n\", (int)offsetof(MPI_Status, MPI_SOURCE));")
124-
println(f," printf(\"const MPI_Status_Tag_offset = %d\\n\", (int)offsetof(MPI_Status, MPI_TAG));")
125-
println(f," printf(\"const MPI_Status_Error_offset = %d\\n\", (int)offsetof(MPI_Status, MPI_ERROR));")
127+
println(f," fprintf(fptr, \"const MPI_Status_size = %d\\n\", (int)sizeof(MPI_Status));")
128+
println(f," fprintf(fptr, \"const MPI_Status_Source_offset = %d\\n\", (int)offsetof(MPI_Status, MPI_SOURCE));")
129+
println(f," fprintf(fptr, \"const MPI_Status_Tag_offset = %d\\n\", (int)offsetof(MPI_Status, MPI_TAG));")
130+
println(f," fprintf(fptr, \"const MPI_Status_Error_offset = %d\\n\", (int)offsetof(MPI_Status, MPI_ERROR));")
126131

127132
for (T,constants) in MPI_handle
128-
println(f," printf(\"primitive type $T %d end\\n\", (int)(sizeof($T) * 8));")
133+
println(f," fprintf(fptr, \"primitive type $T %d end\\n\", (int)(sizeof($T) * 8));")
129134

130135
T_f2c = T == :MPI_Datatype ? :MPI_Type_f2c : Symbol(T, :_f2c)
131136
T_c2f = T == :MPI_Datatype ? :MPI_Type_c2f : Symbol(T, :_c2f)
132137
if Libdl.dlsym_e(libptr, T_f2c) == C_NULL
133-
println(f," printf(\"$T(c::Cint) = reinterpret($T,c)\\n\");")
138+
println(f," fprintf(fptr, \"$T(c::Cint) = reinterpret($T,c)\\n\");")
134139
for constant in constants
135-
println(f," printf(\"const $constant = Cint(%i)\\n\", $constant);")
140+
println(f," fprintf(fptr, \"const $constant = Cint(%i)\\n\", $constant);")
136141
end
137142
else
138-
println(f," printf(\"$T(c::Cint) = ccall((:$T_f2c,libmpi),$T,(Cint,),c)\\n\");")
143+
println(f," fprintf(fptr, \"$T(c::Cint) = ccall((:$T_f2c,libmpi),$T,(Cint,),c)\\n\");")
139144
for constant in constants
140-
println(f," printf(\"const $constant = Cint(%i)\\n\", $T_c2f($constant));")
145+
println(f," fprintf(fptr, \"const $constant = Cint(%i)\\n\", $T_c2f($constant));")
141146
end
142147
end
143148
end
144149

145150
for constant in MPI_Cints
146-
println(f," printf(\"const $constant = Cint(%i)\\n\", (int)($constant));")
151+
println(f," fprintf(fptr, \"const $constant = Cint(%i)\\n\", (int)($constant));")
147152
end
148153

149154
for constant in MPI_pointers
150155
if Sys.WORD_SIZE == 32
151-
println(f," printf(\"const $constant = reinterpret(SentinelPtr, 0x%08\" PRIx32 \")\\n\", (uint32_t)($constant));")
156+
println(f," fprintf(fptr, \"const $constant = reinterpret(SentinelPtr, 0x%08\" PRIx32 \")\\n\", (uint32_t)($constant));")
152157
else
153-
println(f," printf(\"const $constant = reinterpret(SentinelPtr, 0x%016\" PRIx64 \")\\n\", (uint64_t)($constant));")
158+
println(f," fprintf(fptr, \"const $constant = reinterpret(SentinelPtr, 0x%016\" PRIx64 \")\\n\", (uint64_t)($constant));")
154159
end
155160
end
156161

157162
print(f,"""
163+
fclose(fptr);
158164
MPI_Finalize();
159165
return 0;
160166
}

docs/src/installation.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ The build script will attempt to find the shared library and constants: this can
2727
controlled with the optional environment variables:
2828

2929
- `JULIA_MPI_PATH`: the top-level installation directory of MPI.
30+
- `JULIA_MPI_LIBRARY`: the path of the MPI shared library.
3031
- `JULIA_MPI_LIBRARY_PATH`: the directory containing the MPI library files.
3132
- `JULIA_MPI_INCLUDE_PATH`: the directory containing the MPI header files.
3233
- `JULIA_MPI_CFLAGS`: C flags passed to the constant generation build (default: `-lmpi`)

0 commit comments

Comments
 (0)