Blob Blame History Raw
From 7c10e6e5debde713b52008e1f7476ffa036e2d48 Mon Sep 17 00:00:00 2001
From: Elliott Sales de Andrade <quantum.analyst@gmail.com>
Date: Sun, 2 Jan 2022 05:47:18 -0500
Subject: [PATCH 02/11] Use system mingw64 headers and crt

Signed-off-by: Elliott Sales de Andrade <quantum.analyst@gmail.com>
---
 builder/mingw-w64.go  | 68 ++++---------------------------------------
 compileopts/config.go |  7 ++---
 2 files changed, 7 insertions(+), 68 deletions(-)

diff --git a/builder/mingw-w64.go b/builder/mingw-w64.go
index 135cae94..d233068d 100644
--- a/builder/mingw-w64.go
+++ b/builder/mingw-w64.go
@@ -1,31 +1,11 @@
 package builder
 
 import (
-	"io"
-	"os"
-	"path/filepath"
-	"strings"
-
-	"github.com/tinygo-org/tinygo/goenv"
+	"fmt"
 )
 
 var MinGW = Library{
-	name: "mingw-w64",
-	makeHeaders: func(target, includeDir string) error {
-		// copy _mingw.h
-		srcDir := filepath.Join(goenv.Get("TINYGOROOT"), "lib", "mingw-w64")
-		outf, err := os.Create(includeDir + "/_mingw.h")
-		if err != nil {
-			return err
-		}
-		defer outf.Close()
-		inf, err := os.Open(srcDir + "/mingw-w64-headers/crt/_mingw.h.in")
-		if err != nil {
-			return err
-		}
-		_, err = io.Copy(outf, inf)
-		return err
-	},
+	name:      "mingw-w64",
 	sourceDir: func() string { return "" }, // unused
 	cflags: func(target, headerPath string) []string {
 		// No flags necessary because there are no files to compile.
@@ -45,47 +25,9 @@ var MinGW = Library{
 // compile these files.
 func makeMinGWExtraLibs(tmpdir string) []*compileJob {
 	var jobs []*compileJob
-	root := goenv.Get("TINYGOROOT")
-	// Normally all the api-ms-win-crt-*.def files are all compiled to a single
-	// .lib file. But to simplify things, we're going to leave them as separate
-	// files.
-	for _, name := range []string{
-		"kernel32.def.in",
-		"api-ms-win-crt-conio-l1-1-0.def",
-		"api-ms-win-crt-convert-l1-1-0.def",
-		"api-ms-win-crt-environment-l1-1-0.def",
-		"api-ms-win-crt-filesystem-l1-1-0.def",
-		"api-ms-win-crt-heap-l1-1-0.def",
-		"api-ms-win-crt-locale-l1-1-0.def",
-		"api-ms-win-crt-math-l1-1-0.def.in",
-		"api-ms-win-crt-multibyte-l1-1-0.def",
-		"api-ms-win-crt-private-l1-1-0.def.in",
-		"api-ms-win-crt-process-l1-1-0.def",
-		"api-ms-win-crt-runtime-l1-1-0.def.in",
-		"api-ms-win-crt-stdio-l1-1-0.def",
-		"api-ms-win-crt-string-l1-1-0.def",
-		"api-ms-win-crt-time-l1-1-0.def",
-		"api-ms-win-crt-utility-l1-1-0.def",
-	} {
-		outpath := filepath.Join(tmpdir, filepath.Base(name)+".lib")
-		inpath := filepath.Join(root, "lib/mingw-w64/mingw-w64-crt/lib-common/"+name)
-		job := &compileJob{
-			description: "create lib file " + inpath,
-			result:      outpath,
-			run: func(job *compileJob) error {
-				defpath := inpath
-				if strings.HasSuffix(inpath, ".in") {
-					// .in files need to be preprocessed by a preprocessor (-E)
-					// first.
-					defpath = outpath + ".def"
-					err := runCCompiler("-E", "-x", "c", "-Wp,-w", "-P", "-DDEF_X64", "-o", defpath, inpath, "-I"+goenv.Get("TINYGOROOT")+"/lib/mingw-w64/mingw-w64-crt/def-include/")
-					if err != nil {
-						return err
-					}
-				}
-				return link("ld.lld", "-m", "i386pep", "-o", outpath, defpath)
-			},
-		}
+	for _, name := range []string{"kernel32", "ucrt"} {
+		outpath := fmt.Sprintf("/usr/x86_64-w64-mingw32/sys-root/mingw/lib/lib%s.a", name)
+		job := dummyCompileJob(outpath)
 		jobs = append(jobs, job)
 	}
 	return jobs
diff --git a/compileopts/config.go b/compileopts/config.go
index a006b673..c81a547c 100644
--- a/compileopts/config.go
+++ b/compileopts/config.go
@@ -297,12 +297,9 @@ func (c *Config) CFlags() []string {
 		root := goenv.Get("TINYGOROOT")
 		cflags = append(cflags, "--sysroot="+root+"/lib/wasi-libc/sysroot")
 	case "mingw-w64":
-		root := goenv.Get("TINYGOROOT")
-		path, _ := c.LibcPath("mingw-w64")
 		cflags = append(cflags,
-			"--sysroot="+path,
-			"-isystem", filepath.Join(root, "lib", "mingw-w64", "mingw-w64-headers", "crt"),
-			"-isystem", filepath.Join(root, "lib", "mingw-w64", "mingw-w64-headers", "defaults", "include"),
+			"--sysroot=/usr/x86_64-w64-mingw32/sys-root",
+			"-isystem", "/usr/x86_64-w64-mingw32/sys-root/mingw/include",
 			"-D_UCRT",
 		)
 	case "":
-- 
2.36.1