|
| 1 | +module MPIABI |
| 2 | + |
| 3 | +using Preferences |
| 4 | + |
| 5 | +function known_abis() |
| 6 | + return (:MicrosoftMPI, :MPICH, :OpenMPI, :MPItrampoline) |
| 7 | +end |
| 8 | + |
| 9 | +# const abi = begin |
| 10 | +# _abi = @load_preference("abi", nothing) |
| 11 | +# if isnothing(_abi) |
| 12 | +# _abi = find_abi() |
| 13 | +# @set_preference!("abi" => _abi) |
| 14 | +# end |
| 15 | +# _abi |
| 16 | +# end |
| 17 | + |
| 18 | +const abi = @load_preference("abi", Sys.iswindows() ? :MicrosoftMPI : :MPICH) |
| 19 | + |
| 20 | +function __init__() |
| 21 | + if Sys.iswindows() |
| 22 | + if abi != :MicrosoftMPI |
| 23 | + @error """ |
| 24 | + The MPI ABI is not compatible with the current platform. |
| 25 | + Please set the MPI ABI to :MicrosoftMPI. |
| 26 | + """ |
| 27 | + end |
| 28 | + end |
| 29 | +end |
| 30 | + |
| 31 | +function set_abi(abi) |
| 32 | + if abi ∉ known_abis() |
| 33 | + error(""" |
| 34 | + The MPI ABI $abi is not supported. |
| 35 | + Please set the MPI ABI to one of the following: |
| 36 | + $(known_abis()) |
| 37 | + """) |
| 38 | + end |
| 39 | + @set_preferences!("abi" => abi) |
| 40 | + @warn "The MPI abi has changed, you will need to restart Julia for the change to take effect" abi |
| 41 | + |
| 42 | + if VERSION <= v"1.6.5" || VERSION == v"1.7.0" |
| 43 | + @warn """ |
| 44 | + Due to a bug in Julia (until 1.6.5 and 1.7.1), setting preferences in transitive dependencies |
| 45 | + is broken (https://github.com/JuliaPackaging/Preferences.jl/issues/24). As a workaround, we |
| 46 | + will now add MPIABI as a direct dependency in your current project. |
| 47 | + """ |
| 48 | + |
| 49 | + uuid = Preferences.get_uuid(@__MODULE__) |
| 50 | + project_toml, pkg_name = Preferences.find_first_project_with_uuid(uuid) |
| 51 | + |
| 52 | + if project_toml === nothing && pkg_name === nothing |
| 53 | + # If we couldn't find one, we're going to use `active_project()` |
| 54 | + project_toml = Base.active_project() |
| 55 | + |
| 56 | + # And we're going to need to add this UUID as an "extras" dependency: |
| 57 | + # We're going to assume you want to name this this dependency in the |
| 58 | + # same way as it's been loaded: |
| 59 | + pkg_uuid_matches = filter(d -> d.uuid == uuid, keys(Base.loaded_modules)) |
| 60 | + if isempty(pkg_uuid_matches) |
| 61 | + error("Cannot set preferences of an unknown package that is not loaded!") |
| 62 | + end |
| 63 | + pkg_name = first(pkg_uuid_matches).name |
| 64 | + |
| 65 | + # Read in the project, add the deps, write it back out! |
| 66 | + project = Base.parsed_toml(project_toml) |
| 67 | + if !haskey(project, "deps") |
| 68 | + project["deps"] = Dict{String,Any}() |
| 69 | + end |
| 70 | + project["deps"][pkg_name] = string(uuid) |
| 71 | + open(project_toml, "w") do io |
| 72 | + TOML.print(io, project; sorted=true) |
| 73 | + end |
| 74 | + end |
| 75 | + end |
| 76 | + return abi |
| 77 | +end |
| 78 | + |
| 79 | +end # module |
0 commit comments