Skip to content

Commit 2144f87

Browse files
committed
Improve target detection for building i2c_dev
It turns out that people sometimes have MIX_TARGET set when they're not compiling for Nerves. On MacOS which doesn't support i2c_dev, this causes the following error: ``` ==> circuits_i2c "**** CIRCUITS_I2C_BACKEND set to [normal] ****" Makefile:44: *** Circuits.I2C Linux i2c-dev backend is not supported on non-Linux platforms. Review circuits_i2c backend configuration or report an issue if improperly detected.. Stop. could not compile dependency :circuits_i2c, "mix compile" failed. Errors may have been logged above. You can recompile this dependency with "mix deps.compile circuits_i2c --force", update it with "mix deps.update circuits_i2c" or clean it with "mix deps.clean circuits_i2c" ``` This is pretty confusing. This change makes the Nerves detection smarter by first checking the MIX_TARGET and then checking if crosscompilation environment variables are actually set. If not, the host compiler will be used, so use the default for host builds.
1 parent 77f8e1a commit 2144f87

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

mix.exs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,16 @@ defmodule Circuits.I2C.MixProject do
9090
end
9191
end
9292

93-
# Assume Nerves for a default
94-
defp default_backend(_env, _not_host), do: Circuits.I2C.I2CDev
93+
# MIX_TARGET set to something besides host
94+
defp default_backend(env, _not_host) do
95+
# If CROSSCOMPILE is set, then the Makefile will use the crosscompiler and
96+
# assume a Linux/Nerves build If not, then the NIF will be build for the
97+
# host, so use the default host backend
98+
case System.fetch_env("CROSSCOMPILE") do
99+
{:ok, _} -> Circuits.I2C.I2CDev
100+
:error -> default_backend(env, :host)
101+
end
102+
end
95103

96104
defp set_make_env(_args) do
97105
# Since user configuration hasn't been loaded into the application

0 commit comments

Comments
 (0)