Skip to content

Commit

Permalink
Merge pull request #118 from zeam-vm/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
zacky1972 authored May 1, 2020
2 parents 38e9015 + b0642d4 commit a16d7c9
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Add `pelemay` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:pelemay, "~> 0.0.8"},
{:pelemay, "~> 0.0.9"},
]
end
```
Expand Down
5 changes: 5 additions & 0 deletions lib/pelemay.ex
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ defmodule Pelemay do
end

def eval_compile_time_info() do
unless File.exists?(Application.app_dir(:pelemay, "priv/compile_time_info")) do
Application.app_dir(:pelemay, "priv/compile_time_info")
|> File.write!("compile_time_info = #{CpuInfo.all_profile() |> inspect()}")
end

Code.eval_file(Application.app_dir(:pelemay, "priv/compile_time_info"))
end
end
70 changes: 55 additions & 15 deletions lib/pelemay/generator/builder.ex
Original file line number Diff line number Diff line change
@@ -1,45 +1,85 @@
defmodule Pelemay.Generator.Builder do
alias Pelemay.Generator

@clang "clang"
@gcc "gcc"
@cflags ["-Ofast", "-g", "-ansi", "-pedantic"]
@cflags_includes ["-I/usr/local/include", "-I/usr/include"]
@cflags_after ["-std=c11", "-Wno-unused-function"]
@ldflags ["-L/usr/local/lib", "-L/usr/lib"]
@cflags_non_windows ["-fPIC"]
@ldflags_non_windows ["-dynamiclib", "-undefined", "dynamic_lookup"]

def generate(module) do
cc = System.get_env("CC")
def parse_info(compile_time_info) do
Map.get(compile_time_info, :compiler) |> parse_compiler()
end

cc =
if is_nil(cc) or is_nil(System.find_executable(cc)) do
@clang
defp parse_compiler(compiler) do
parse_compiler_sub(Map.get(compiler, :cc_env), compiler)
end

def apple_clang(compiler) do
Map.get(compiler, :apple_clang)
end

def clang(compiler) do
Map.get(compiler, :clang)
|> Enum.filter(&(Map.get(&1, :type) == :clang))
end

def gcc(compiler) do
Map.get(compiler, :gcc)
|> Enum.filter(&(Map.get(&1, :type) == :gcc))
end

defp parse_compiler_sub([], compiler) do
case {apple_clang(compiler), clang(compiler), gcc(compiler)} do
{nil, [], []} -> []
{nil, [], gcc} -> gcc
{nil, clang, _} -> clang
{apple_clang, _, _} -> apple_clang
end
end

defp parse_compiler_sub(cc, _compiler) do
cc
end

def select_latest(cc) do
Enum.reduce(cc, fn x, acc ->
if Map.get(x, :version) >= Map.get(acc, :version) do
x
else
cc
acc
end
end)
end

def generate(module) do
cpu_info =
Pelemay.eval_compile_time_info()
|> elem(0)

cc =
if is_nil(System.find_executable(cc)) do
@gcc
else
cc
end
cpu_info
|> parse_info()
|> select_latest()
|> Map.get(:bin)

if is_nil(System.find_executable(cc)) do
raise CompileError, message: "#{cc} is not installed."
end

cflags = cpu_info |> Map.get(:compiler) |> Map.get(:cflags_env) |> String.split()

{cflags_t, ldflags_t} =
if is_nil(System.get_env("CROSSCOMPILE")) do
{
@cflags ++ ["-I#{erlang_include_path()}"] ++ @cflags_includes ++ @cflags_after,
cflags ++
@cflags ++ ["-I#{erlang_include_path()}"] ++ @cflags_includes ++ @cflags_after,
@ldflags
}
else
{
String.split(System.get_env("CFLAGS")) ++ String.split(System.get_env("ERL_CFLAGS")),
cflags ++ String.split(System.get_env("ERL_CFLAGS")),
@ldflags ++ String.split(System.get_env("ERL_LDFLAGS"))
}
end
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Pelemay.MixProject do
def project do
[
app: :pelemay,
version: "0.0.8",
version: "0.0.9",
elixir: "~> 1.9",
start_permanent: Mix.env() == :prod,
description: description(),
Expand Down

0 comments on commit a16d7c9

Please sign in to comment.