1
1
class Llgo < Formula
2
2
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 "
6
6
license "Apache-2.0"
7
- head "https://github.com/goplus /llgo.git" , branch : "main"
7
+ head "https://github.com/luoliwoshang /llgo.git" , branch : "main"
8
8
9
9
livecheck do
10
10
url :stable
@@ -21,18 +21,21 @@ class Llgo < Formula
21
21
sha256 x86_64_linux : "2486bc3ddd7c27fe03c9ae10df73553c09b9f21b3d4dc94b55f99da632dbbf5f"
22
22
end
23
23
24
+ depends_on "cmake" => :build
25
+ depends_on "ninja" => :build
26
+ depends_on "python@3.13" => :build
24
27
depends_on "bdw-gc"
25
28
depends_on "go@1.24"
26
29
depends_on "libffi"
27
30
depends_on "libuv"
28
- depends_on "lld@19"
29
- depends_on "llvm@19"
30
31
depends_on "openssl@3"
31
32
depends_on "pkgconf"
32
33
uses_from_macos "zlib"
33
34
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
36
39
end
37
40
38
41
def find_dep ( name )
@@ -41,37 +44,151 @@ def find_dep(name)
41
44
end
42
45
43
46
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
+
45
155
ldflags = %W[
46
156
-s -w
47
157
-X github.com/goplus/llgo/internal/env.buildVersion=v#{ version }
48
158
-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 }
50
160
]
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 } "
52
164
if OS . linux?
53
165
ENV . prepend "CGO_CPPFLAGS" ,
54
- "-I#{ llvm . opt_include } " \
166
+ "-I#{ local_llvm_include } " \
55
167
"-D_GNU_SOURCE " \
56
168
"-D__STDC_CONSTANT_MACROS " \
57
169
"-D__STDC_FORMAT_MACROS " \
58
170
"-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 } "
61
173
end
62
174
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"
64
182
65
- libexec . install "LICENSE" , "README.md" , "go.mod" , "go.sum" , "runtime"
183
+ libexec . install "LICENSE" , "README.md" , "go.mod" , "go.sum" , "runtime" , "targets"
66
184
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" )
69
187
script_env = { PATH : "#{ path_deps . join ( ":" ) } :$PATH" }
70
188
71
189
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"
75
192
end
76
193
77
194
( libexec /"bin" ) . children . each do |f |
@@ -93,13 +210,18 @@ def install
93
210
bdwgc = find_dep ( "bdw-gc" )
94
211
ENV . prepend_path "LD_LIBRARY_PATH" , bdwgc . opt_lib
95
212
213
+ ohai "CCFLAGS: #{ ENV [ "CCFLAGS" ] } "
214
+ ohai "CFLAGS: #{ ENV [ "CFLAGS" ] } "
215
+ ohai "LDFLAGS: #{ ENV [ "LDFLAGS" ] } "
216
+
96
217
( testpath /"hello.go" ) . write <<~GO
97
218
package main
98
219
99
220
import (
100
221
"fmt"
101
222
102
223
"github.com/goplus/lib/c"
224
+ "github.com/goplus/lib/cpp/std"
103
225
)
104
226
105
227
func Foo() string {
@@ -109,6 +231,7 @@ def install
109
231
func main() {
110
232
fmt.Println("Hello LLGo by fmt.Println")
111
233
c.Printf(c.Str("Hello LLGo by c.Printf\\ n"))
234
+ c.Printf(std.Str("Hello LLGo by cpp/std.Str\\ n").CStr())
112
235
}
113
236
GO
114
237
( testpath /"hello_test.go" ) . write <<~GO
@@ -130,14 +253,33 @@ module hello
130
253
system go . opt_bin /"go" , "get" , "github.com/goplus/lib"
131
254
# Test llgo run
132
255
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 ." )
135
259
# Test llgo build
136
260
system bin /"llgo" , "build" , "-o" , "hello" , "."
137
261
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 " ,
139
264
shell_output ( "./hello" )
140
265
# Test llgo test
141
266
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
142
284
end
143
285
end
0 commit comments