|
1 | 1 | package builder |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "io/ioutil" |
5 | | - "os" |
6 | | - "path/filepath" |
7 | 4 | "strings" |
8 | | - |
9 | | - "github.com/tinygo-org/tinygo/goenv" |
10 | 5 | ) |
11 | 6 |
|
12 | 7 | // These are the GENERIC_SOURCES according to CMakeList.txt. |
@@ -156,105 +151,20 @@ var aeabiBuiltins = []string{ |
156 | 151 | "arm/aeabi_uldivmod.S", |
157 | 152 | } |
158 | 153 |
|
159 | | -func builtinFiles(target string) []string { |
160 | | - builtins := append([]string{}, genericBuiltins...) // copy genericBuiltins |
161 | | - if strings.HasPrefix(target, "arm") { |
162 | | - builtins = append(builtins, aeabiBuiltins...) |
163 | | - } |
164 | | - return builtins |
165 | | -} |
166 | | - |
167 | | -// builtinsDir returns the directory where the sources for compiler-rt are kept. |
168 | | -func builtinsDir() string { |
169 | | - return filepath.Join(goenv.Get("TINYGOROOT"), "lib", "compiler-rt", "lib", "builtins") |
170 | | -} |
171 | | - |
172 | | -// Get the builtins archive, possibly generating it as needed. |
173 | | -func loadBuiltins(target string) (path string, err error) { |
174 | | - // Try to load a precompiled compiler-rt library. |
175 | | - precompiledPath := filepath.Join(goenv.Get("TINYGOROOT"), "pkg", target, "compiler-rt.a") |
176 | | - if _, err := os.Stat(precompiledPath); err == nil { |
177 | | - // Found a precompiled compiler-rt for this OS/architecture. Return the |
178 | | - // path directly. |
179 | | - return precompiledPath, nil |
180 | | - } |
181 | | - |
182 | | - outfile := "librt-" + target + ".a" |
183 | | - builtinsDir := builtinsDir() |
184 | | - |
185 | | - builtins := builtinFiles(target) |
186 | | - srcs := make([]string, len(builtins)) |
187 | | - for i, name := range builtins { |
188 | | - srcs[i] = filepath.Join(builtinsDir, name) |
189 | | - } |
190 | | - |
191 | | - if path, err := cacheLoad(outfile, commands["clang"][0], srcs); path != "" || err != nil { |
192 | | - return path, err |
193 | | - } |
194 | | - |
195 | | - var cachepath string |
196 | | - err = CompileBuiltins(target, func(path string) error { |
197 | | - path, err := cacheStore(path, outfile, commands["clang"][0], srcs) |
198 | | - cachepath = path |
199 | | - return err |
200 | | - }) |
201 | | - return cachepath, err |
202 | | -} |
203 | | - |
204 | | -// CompileBuiltins compiles builtins from compiler-rt into a static library. |
205 | | -// When it succeeds, it will call the callback with the resulting path. The path |
206 | | -// will be removed after callback returns. If callback returns an error, this is |
207 | | -// passed through to the return value of this function. |
208 | | -func CompileBuiltins(target string, callback func(path string) error) error { |
209 | | - builtinsDir := builtinsDir() |
210 | | - |
211 | | - builtins := builtinFiles(target) |
212 | | - srcs := make([]string, len(builtins)) |
213 | | - for i, name := range builtins { |
214 | | - srcs[i] = filepath.Join(builtinsDir, name) |
215 | | - } |
216 | | - |
217 | | - dirPrefix := "tinygo-builtins" |
218 | | - remapDir := filepath.Join(os.TempDir(), dirPrefix) |
219 | | - dir, err := ioutil.TempDir(os.TempDir(), dirPrefix) |
220 | | - if err != nil { |
221 | | - return err |
222 | | - } |
223 | | - defer os.RemoveAll(dir) |
224 | | - |
225 | | - // Compile all builtins. |
226 | | - // TODO: use builtins optimized for a given target if available. |
227 | | - objs := make([]string, 0, len(builtins)) |
228 | | - for _, name := range builtins { |
229 | | - objname := name |
230 | | - if strings.LastIndexByte(objname, '/') >= 0 { |
231 | | - objname = objname[strings.LastIndexByte(objname, '/'):] |
| 154 | +// CompilerRT is a library with symbols required by programs compiled with LLVM. |
| 155 | +// These symbols are for operations that cannot be emitted with a single |
| 156 | +// instruction or a short sequence of instructions for that target. |
| 157 | +// |
| 158 | +// For more information, see: https://compiler-rt.llvm.org/ |
| 159 | +var CompilerRT = Library{ |
| 160 | + name: "compiler-rt", |
| 161 | + cflags: func() []string { return []string{"-Werror", "-Wall", "-std=c11", "-fshort-enums", "-nostdlibinc"} }, |
| 162 | + sourceDir: "lib/compiler-rt/lib/builtins", |
| 163 | + sources: func(target string) []string { |
| 164 | + builtins := append([]string{}, genericBuiltins...) // copy genericBuiltins |
| 165 | + if strings.HasPrefix(target, "arm") { |
| 166 | + builtins = append(builtins, aeabiBuiltins...) |
232 | 167 | } |
233 | | - objpath := filepath.Join(dir, objname+".o") |
234 | | - objs = append(objs, objpath) |
235 | | - srcpath := filepath.Join(builtinsDir, name) |
236 | | - // Note: -fdebug-prefix-map is necessary to make the output archive |
237 | | - // reproducible. Otherwise the temporary directory is stored in the |
238 | | - // archive itself, which varies each run. |
239 | | - args := []string{"-c", "-Oz", "-g", "-Werror", "-Wall", "-std=c11", "-fshort-enums", "-nostdlibinc", "-ffunction-sections", "-fdata-sections", "-Wno-macro-redefined", "--target=" + target, "-fdebug-prefix-map=" + dir + "=" + remapDir} |
240 | | - if strings.HasPrefix(target, "riscv32-") { |
241 | | - args = append(args, "-march=rv32imac", "-mabi=ilp32", "-fforce-enable-int128") |
242 | | - } |
243 | | - err := runCCompiler("clang", append(args, "-o", objpath, srcpath)...) |
244 | | - if err != nil { |
245 | | - return &commandError{"failed to build", srcpath, err} |
246 | | - } |
247 | | - } |
248 | | - |
249 | | - // Put all the object files in a single archive. This archive file will be |
250 | | - // used to statically link compiler-rt. |
251 | | - arpath := filepath.Join(dir, "librt.a") |
252 | | - err = makeArchive(arpath, objs) |
253 | | - if err != nil { |
254 | | - return err |
255 | | - } |
256 | | - |
257 | | - // Give the caller the resulting file. The callback must copy the file, |
258 | | - // because after it returns the temporary directory will be removed. |
259 | | - return callback(arpath) |
| 168 | + return builtins |
| 169 | + }, |
260 | 170 | } |
0 commit comments