Skip to content

Commit 9cf2d49

Browse files
committed
llgo:pretest embed clang
llvm build cache with mac cgo config with c++17 -Wl,-rpath with llvm remove CFLAG unset by goplus/llgo#1243 merged with llgo embed target build test DRUNTIMES_CMAKE_ARGS set RPATH clang prefix linux rpath -DLLVM_ENABLE_LIBEDIT=OFF refine rb struct llvm test off remove some llvm component as tinygo build only nessary executable -DLLVM_INSTALL_UTILS=OFF
1 parent 2b72034 commit 9cf2d49

File tree

1 file changed

+166
-24
lines changed

1 file changed

+166
-24
lines changed

Formula/l/llgo.rb

Lines changed: 166 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
class Llgo < Formula
22
desc "Go compiler based on LLVM integrate with the C ecosystem and Python"
3-
homepage "https://github.com/goplus/llgo"
4-
url "https://github.com/goplus/llgo/archive/refs/tags/v0.11.5.tar.gz"
5-
sha256 "e025993d12c1f5e49e5b8dcb31c0e8b349efe56970d1a23d6c089ebd10928c6b"
3+
homepage "https://github.com/luoliwoshang/llgo"
4+
url "https://github.com/luoliwoshang/llgo/archive/refs/tags/v0.12.13.tar.gz"
5+
sha256 "13106bf7d4812e58a427a565ab7413c6d086d12c3dbfaf058f47908cbe0bb40c"
66
license "Apache-2.0"
7-
head "https://github.com/goplus/llgo.git", branch: "main"
7+
head "https://github.com/luoliwoshang/llgo.git", branch: "main"
88

99
livecheck do
1010
url :stable
@@ -21,18 +21,21 @@ class Llgo < Formula
2121
sha256 x86_64_linux: "2486bc3ddd7c27fe03c9ae10df73553c09b9f21b3d4dc94b55f99da632dbbf5f"
2222
end
2323

24+
depends_on "cmake" => :build
25+
depends_on "ninja" => :build
26+
depends_on "python@3.13" => :build
2427
depends_on "bdw-gc"
2528
depends_on "go@1.24"
2629
depends_on "libffi"
2730
depends_on "libuv"
28-
depends_on "lld@19"
29-
depends_on "llvm@19"
3031
depends_on "openssl@3"
3132
depends_on "pkgconf"
3233
uses_from_macos "zlib"
3334

34-
on_linux do
35-
depends_on "libunwind"
35+
resource "espressif-llvm" do
36+
url "https://github.com/espressif/llvm-project.git",
37+
revision: "xtensa_release_19.1.2",
38+
shallow: true
3639
end
3740

3841
def find_dep(name)
@@ -41,37 +44,151 @@ def find_dep(name)
4144
end
4245

4346
def install
44-
llvm = find_dep("llvm")
47+
esp_clang_path = libexec/"crosscompile/clang"
48+
local_llvm_config = esp_clang_path/"bin/llvm-config"
49+
local_llvm_include = esp_clang_path/"include"
50+
local_llvm_lib = esp_clang_path/"lib"
51+
llvm_build_target_component = %w[clang llvm-config llvm-ar llvm-nm lld]
52+
53+
resource("espressif-llvm").stage do
54+
mkdir "build" do
55+
cmake_args = %w[
56+
-G Ninja
57+
-DCMAKE_BUILD_TYPE=Release
58+
-DLLVM_TARGETS_TO_BUILD=X86;ARM;AArch64;AVR;Mips;RISCV;WebAssembly
59+
-DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD=Xtensa
60+
-DLLVM_ENABLE_PROJECTS=clang;lld
61+
-DLLVM_ENABLE_RUNTIMES=compiler-rt;libcxx;libcxxabi;libunwind
62+
-DLLVM_POLLY_LINK_INTO_TOOLS=ON
63+
-DLLVM_BUILD_EXTERNAL_COMPILER_RT=ON
64+
-DLLVM_ENABLE_EH=ON
65+
-DLLVM_ENABLE_RTTI=ON
66+
-DLLVM_INCLUDE_DOCS=OFF
67+
-DLLVM_INCLUDE_EXAMPLES=OFF
68+
-DLLVM_INCLUDE_TESTS=OFF
69+
-DLLVM_INCLUDE_BENCHMARKS=OFF
70+
-DLLVM_BUILD_DOCS=OFF
71+
-DLLVM_ENABLE_DOXYGEN=OFF
72+
-DLLVM_INSTALL_UTILS=OFF
73+
-DLLVM_ENABLE_Z3_SOLVER=OFF
74+
-DLLVM_OPTIMIZED_TABLEGEN=ON
75+
-DLLVM_USE_RELATIVE_PATHS_IN_FILES=ON
76+
-DLLVM_SOURCE_PREFIX=.
77+
-DLIBCXX_INSTALL_MODULES=ON
78+
-DCLANG_FORCE_MATCHING_LIBCLANG_SOVERSION=OFF
79+
-DCOMPILER_RT_BUILD_SANITIZERS=OFF
80+
-DCOMPILER_RT_BUILD_XRAY=OFF
81+
-DCOMPILER_RT_BUILD_LIBFUZZER=OFF
82+
-DCOMPILER_RT_BUILD_PROFILE=OFF
83+
-DCOMPILER_RT_BUILD_MEMPROF=OFF
84+
-DCOMPILER_RT_BUILD_ORC=OFF
85+
-DCOMPILER_RT_BUILD_GWP_ASAN=OFF
86+
-DCOMPILER_RT_BUILD_CTX_PROFILE=OFF
87+
-DCMAKE_POSITION_INDEPENDENT_CODE=ON
88+
-DLLVM_ENABLE_PER_TARGET_RUNTIME_DIR=OFF
89+
-DCOMPILER_RT_DEFAULT_TARGET_ONLY=ON
90+
-DCOMPILER_RT_USE_BUILTINS_LIBRARY=ON
91+
-DLIBCXX_ENABLE_STATIC_ABI_LIBRARY=ON
92+
-DLIBCXX_STATICALLY_LINK_ABI_IN_SHARED_LIBRARY=OFF
93+
-DLIBCXX_STATICALLY_LINK_ABI_IN_STATIC_LIBRARY=ON
94+
-DLIBCXX_USE_COMPILER_RT=ON
95+
-DLIBCXX_HAS_ATOMIC_LIB=OFF
96+
-DLIBCXXABI_ENABLE_STATIC_UNWINDER=ON
97+
-DLIBCXXABI_STATICALLY_LINK_UNWINDER_IN_SHARED_LIBRARY=OFF
98+
-DLIBCXXABI_STATICALLY_LINK_UNWINDER_IN_STATIC_LIBRARY=ON
99+
-DLIBCXXABI_USE_COMPILER_RT=ON
100+
-DLIBCXXABI_USE_LLVM_UNWINDER=ON
101+
-DLIBUNWIND_USE_COMPILER_RT=ON
102+
-DSANITIZER_CXX_ABI=libc++
103+
-DSANITIZER_TEST_CXX=libc++
104+
-DLLVM_LINK_LLVM_DYLIB=ON
105+
-DCLANG_LINK_CLANG_DYLIB=ON
106+
-DCMAKE_STRIP=/usr/bin/strip
107+
]
108+
109+
# Add macOS-specific arguments
110+
cmake_args += if OS.mac?
111+
%W[
112+
-DLLVM_BUILD_LLVM_C_DYLIB=ON
113+
-DLLVM_ENABLE_LIBCXX=ON
114+
-DLIBCXX_PSTL_BACKEND=libdispatch
115+
-DCMAKE_OSX_SYSROOT=#{MacOS.sdk_path}
116+
-DCMAKE_OSX_ARCHITECTURES=#{Hardware::CPU.arch}
117+
-DLIBCXXABI_USE_SYSTEM_LIBS=ON
118+
]
119+
else
120+
%w[
121+
-DLLVM_ENABLE_LIBXML2=OFF
122+
-DLLVM_ENABLE_LIBCXX=OFF
123+
-DCLANG_DEFAULT_CXX_STDLIB=libstdc++
124+
-DLLVM_BUILD_LLVM_DYLIB=ON
125+
-DCOMPILER_RT_USE_LLVM_UNWINDER=ON
126+
]
127+
end
128+
129+
cmake_args += %w[
130+
-DLLVM_INCLUDE_TESTS=OFF
131+
-DLLVM_ENABLE_TERMINFO=OFF
132+
-DLLVM_ENABLE_ZSTD=OFF
133+
-DLLVM_ENABLE_LIBEDIT=OFF
134+
-DLLVM_ENABLE_Z3_SOLVER=OFF
135+
-DLLVM_ENABLE_OCAMLDOC=OFF
136+
-DLLVM_ENABLE_LIBXML2=OFF
137+
-DLLVM_TOOL_CLANG_TOOLS_EXTRA_BUILD=OFF
138+
-DCLANG_ENABLE_ARCMT=OFF
139+
]
140+
cmake_args << "-DCMAKE_INSTALL_PREFIX=#{esp_clang_path}"
141+
142+
# Fix rpath for libc++ libraries to pass Homebrew linkage tests
143+
# Without @loader_path, libc++.1.0.dylib and libc++abi.1.0.dylib fail
144+
# brew linkage --cached --test --strict with "Files with missing rpath" error
145+
cmake_args << "-DRUNTIMES_CMAKE_ARGS=-DCMAKE_INSTALL_RPATH=#{rpath}"
146+
147+
system "cmake", *cmake_args, "../llvm"
148+
149+
# Build with all available cores
150+
system "ninja", "-j#{ENV.make_jobs}", *llvm_build_target_component
151+
system "ninja", "install"
152+
end
153+
end
154+
45155
ldflags = %W[
46156
-s -w
47157
-X github.com/goplus/llgo/internal/env.buildVersion=v#{version}
48158
-X github.com/goplus/llgo/internal/env.buildTime=#{time.iso8601}
49-
-X github.com/goplus/llgo/xtool/env/llvm.ldLLVMConfigBin=#{llvm.opt_bin/"llvm-config"}
159+
-X github.com/goplus/llgo/xtool/env/llvm.ldLLVMConfigBin=#{local_llvm_config}
50160
]
51-
tags = nil
161+
162+
ENV.prepend "CGO_CXXFLAGS", "-std=c++17"
163+
ENV.prepend "CGO_LDFLAGS", "-L#{local_llvm_lib} -lLLVM -Wl,-rpath,#{local_llvm_lib}"
52164
if OS.linux?
53165
ENV.prepend "CGO_CPPFLAGS",
54-
"-I#{llvm.opt_include} " \
166+
"-I#{local_llvm_include} " \
55167
"-D_GNU_SOURCE " \
56168
"-D__STDC_CONSTANT_MACROS " \
57169
"-D__STDC_FORMAT_MACROS " \
58170
"-D__STDC_LIMIT_MACROS"
59-
ENV.prepend "CGO_LDFLAGS", "-L#{llvm.opt_lib} -lLLVM"
60-
tags = "byollvm"
171+
else
172+
ENV.prepend "CGO_CPPFLAGS", "-I#{local_llvm_include}"
61173
end
62174

63-
system "go", "build", *std_go_args(ldflags:, tags:), "-o", libexec/"bin/", "./cmd/llgo"
175+
ohai "CGO_CXXFLAGS: #{ENV["CGO_CXXFLAGS"]}"
176+
ohai "CGO_CPPFLAGS: #{ENV["CGO_CPPFLAGS"]}"
177+
ohai "CGO_LDFLAGS: #{ENV["CGO_LDFLAGS"]}"
178+
ohai "Start Building LLGO..."
179+
180+
# byollvm to use custom llvm
181+
system "go", "build", *std_go_args(ldflags: ldflags, tags: "byollvm"), "-o", libexec/"bin/", "./cmd/llgo"
64182

65-
libexec.install "LICENSE", "README.md", "go.mod", "go.sum", "runtime"
183+
libexec.install "LICENSE", "README.md", "go.mod", "go.sum", "runtime", "targets"
66184

67-
path_deps = %w[lld go pkgconf].map { |name| find_dep(name).opt_bin }
68-
path_deps << llvm.opt_bin
185+
path_deps = %w[go pkgconf].map { |name| find_dep(name).opt_bin }
186+
path_deps << (esp_clang_path/"bin")
69187
script_env = { PATH: "#{path_deps.join(":")}:$PATH" }
70188

71189
if OS.linux?
72-
libunwind = find_dep("libunwind")
73-
script_env[:CFLAGS] = "-I#{libunwind.opt_include} $CFLAGS"
74-
script_env[:LDFLAGS] = "-L#{libunwind.opt_lib} -rpath #{libunwind.opt_lib} $LDFLAGS"
190+
script_env[:CFLAGS] = "-I#{local_llvm_include} $CFLAGS"
191+
script_env[:LDFLAGS] = "-L#{local_llvm_lib} $LDFLAGS"
75192
end
76193

77194
(libexec/"bin").children.each do |f|
@@ -93,13 +210,18 @@ def install
93210
bdwgc = find_dep("bdw-gc")
94211
ENV.prepend_path "LD_LIBRARY_PATH", bdwgc.opt_lib
95212

213+
ohai "CCFLAGS: #{ENV["CCFLAGS"]}"
214+
ohai "CFLAGS: #{ENV["CFLAGS"]}"
215+
ohai "LDFLAGS: #{ENV["LDFLAGS"]}"
216+
96217
(testpath/"hello.go").write <<~GO
97218
package main
98219
99220
import (
100221
"fmt"
101222
102223
"github.com/goplus/lib/c"
224+
"github.com/goplus/lib/cpp/std"
103225
)
104226
105227
func Foo() string {
@@ -109,6 +231,7 @@ def install
109231
func main() {
110232
fmt.Println("Hello LLGo by fmt.Println")
111233
c.Printf(c.Str("Hello LLGo by c.Printf\\n"))
234+
c.Printf(std.Str("Hello LLGo by cpp/std.Str\\n").CStr())
112235
}
113236
GO
114237
(testpath/"hello_test.go").write <<~GO
@@ -130,14 +253,33 @@ module hello
130253
system go.opt_bin/"go", "get", "github.com/goplus/lib"
131254
# Test llgo run
132255
assert_equal "Hello LLGo by fmt.Println\n" \
133-
"Hello LLGo by c.Printf\n",
134-
shell_output("#{bin}/llgo run .")
256+
"Hello LLGo by c.Printf\n" \
257+
"Hello LLGo by cpp/std.Str\n",
258+
shell_output("#{bin}/llgo run -v .")
135259
# Test llgo build
136260
system bin/"llgo", "build", "-o", "hello", "."
137261
assert_equal "Hello LLGo by fmt.Println\n" \
138-
"Hello LLGo by c.Printf\n",
262+
"Hello LLGo by c.Printf\n" \
263+
"Hello LLGo by cpp/std.Str\n",
139264
shell_output("./hello")
140265
# Test llgo test
141266
assert_match "PASS", shell_output("#{bin}/llgo test .")
267+
268+
# Test embed targets
269+
# Homebrew sets LDFLAGS with macOS-specific flags (-F/opt/homebrew/Frameworks,
270+
# -Wl,-headerpad_max_install_names, -isysroot) that are incompatible with
271+
# ld.lld when cross-compiling for embedded targets. Clear them.
272+
with_env("LDFLAGS" => "") do
273+
(testpath/"emb"/"main.go").write <<~GO
274+
package main
275+
276+
func main() {
277+
}
278+
GO
279+
cd testpath/"emb" do
280+
system bin/"llgo", "build", "-v", "-target", "esp32-coreboard-v2", "-o", "demo.out", "."
281+
assert_path_exists testpath/"emb"/"demo.out.bin"
282+
end
283+
end
142284
end
143285
end

0 commit comments

Comments
 (0)