optional use esp clang for non-cross-compilation
This commit is contained in:
@@ -159,7 +159,8 @@ func Do(args []string, conf *Config) ([]Package, error) {
|
|||||||
conf.Goarch = runtime.GOARCH
|
conf.Goarch = runtime.GOARCH
|
||||||
}
|
}
|
||||||
// Handle crosscompile configuration first to set correct GOOS/GOARCH
|
// Handle crosscompile configuration first to set correct GOOS/GOARCH
|
||||||
export, err := crosscompile.Use(conf.Goos, conf.Goarch, IsWasiThreadsEnabled(), conf.Target)
|
forceEspClang := conf.Target != ""
|
||||||
|
export, err := crosscompile.Use(conf.Goos, conf.Goarch, conf.Target, IsWasiThreadsEnabled(), forceEspClang)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to setup crosscompile: %w", err)
|
return nil, fmt.Errorf("failed to setup crosscompile: %w", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -156,7 +156,7 @@ func getMacOSSysroot() (string, error) {
|
|||||||
|
|
||||||
// getESPClangRoot returns the ESP Clang root directory, checking LLGoROOT first,
|
// getESPClangRoot returns the ESP Clang root directory, checking LLGoROOT first,
|
||||||
// then downloading if needed and platform is supported
|
// then downloading if needed and platform is supported
|
||||||
func getESPClangRoot() (clangRoot string, err error) {
|
func getESPClangRoot(forceEspClang bool) (clangRoot string, err error) {
|
||||||
llgoRoot := env.LLGoROOT()
|
llgoRoot := env.LLGoROOT()
|
||||||
|
|
||||||
// First check if clang exists in LLGoROOT
|
// First check if clang exists in LLGoROOT
|
||||||
@@ -166,6 +166,10 @@ func getESPClangRoot() (clangRoot string, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !forceEspClang {
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
|
||||||
// Try to download ESP Clang if platform is supported
|
// Try to download ESP Clang if platform is supported
|
||||||
platformSuffix := getESPClangPlatform(runtime.GOOS, runtime.GOARCH)
|
platformSuffix := getESPClangPlatform(runtime.GOOS, runtime.GOARCH)
|
||||||
if platformSuffix != "" {
|
if platformSuffix != "" {
|
||||||
@@ -215,52 +219,57 @@ func getESPClangPlatform(goos, goarch string) string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func use(goos, goarch string, wasiThreads bool) (export Export, err error) {
|
func use(goos, goarch string, wasiThreads, forceEspClang bool) (export Export, err error) {
|
||||||
targetTriple := llvm.GetTargetTriple(goos, goarch)
|
targetTriple := llvm.GetTargetTriple(goos, goarch)
|
||||||
llgoRoot := env.LLGoROOT()
|
llgoRoot := env.LLGoROOT()
|
||||||
|
|
||||||
// Check for ESP Clang support for target-based builds
|
// Check for ESP Clang support for target-based builds
|
||||||
clangRoot, err := getESPClangRoot()
|
clangRoot, err := getESPClangRoot(forceEspClang)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set ClangRoot and CC if clang is available
|
// Set ClangRoot and CC if clang is available
|
||||||
export.ClangRoot = clangRoot
|
export.ClangRoot = clangRoot
|
||||||
export.CC = filepath.Join(clangRoot, "bin", "clang++")
|
if clangRoot != "" {
|
||||||
|
export.CC = filepath.Join(clangRoot, "bin", "clang++")
|
||||||
|
} else {
|
||||||
|
export.CC = "clang++"
|
||||||
|
}
|
||||||
|
|
||||||
if runtime.GOOS == goos && runtime.GOARCH == goarch {
|
if runtime.GOOS == goos && runtime.GOARCH == goarch {
|
||||||
clangLib := filepath.Join(clangRoot, "lib")
|
|
||||||
clangInc := filepath.Join(clangRoot, "include")
|
|
||||||
// not cross compile
|
// not cross compile
|
||||||
// Set up basic flags for non-cross-compile
|
// Set up basic flags for non-cross-compile
|
||||||
export.LDFLAGS = []string{
|
export.LDFLAGS = []string{
|
||||||
"-L" + clangLib,
|
|
||||||
"-target", targetTriple,
|
"-target", targetTriple,
|
||||||
"-Qunused-arguments",
|
"-Qunused-arguments",
|
||||||
"-Wno-unused-command-line-argument",
|
"-Wno-unused-command-line-argument",
|
||||||
"-Wl,--error-limit=0",
|
"-Wl,--error-limit=0",
|
||||||
"-fuse-ld=lld",
|
"-fuse-ld=lld",
|
||||||
}
|
}
|
||||||
export.CFLAGS = append(export.CFLAGS, "-I"+clangInc)
|
if clangRoot != "" {
|
||||||
|
clangLib := filepath.Join(clangRoot, "lib")
|
||||||
|
clangInc := filepath.Join(clangRoot, "include")
|
||||||
|
export.CFLAGS = append(export.CFLAGS, "-I"+clangInc)
|
||||||
|
export.LDFLAGS = append(export.LDFLAGS, "-L"+clangLib)
|
||||||
|
// Add platform-specific rpath flags
|
||||||
|
switch goos {
|
||||||
|
case "darwin":
|
||||||
|
export.LDFLAGS = append(export.LDFLAGS, "-Wl,-rpath,"+clangLib)
|
||||||
|
case "linux":
|
||||||
|
export.LDFLAGS = append(export.LDFLAGS, "-Wl,-rpath,"+clangLib)
|
||||||
|
case "windows":
|
||||||
|
// Windows doesn't support rpath, DLLs should be in PATH or same directory
|
||||||
|
default:
|
||||||
|
// For other Unix-like systems, try the generic rpath
|
||||||
|
export.LDFLAGS = append(export.LDFLAGS, "-Wl,-rpath,"+clangLib)
|
||||||
|
}
|
||||||
|
}
|
||||||
export.CCFLAGS = []string{
|
export.CCFLAGS = []string{
|
||||||
"-Qunused-arguments",
|
"-Qunused-arguments",
|
||||||
"-Wno-unused-command-line-argument",
|
"-Wno-unused-command-line-argument",
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add platform-specific rpath flags
|
|
||||||
switch goos {
|
|
||||||
case "darwin":
|
|
||||||
export.LDFLAGS = append(export.LDFLAGS, "-Wl,-rpath,"+clangLib)
|
|
||||||
case "linux":
|
|
||||||
export.LDFLAGS = append(export.LDFLAGS, "-Wl,-rpath,"+clangLib)
|
|
||||||
case "windows":
|
|
||||||
// Windows doesn't support rpath, DLLs should be in PATH or same directory
|
|
||||||
default:
|
|
||||||
// For other Unix-like systems, try the generic rpath
|
|
||||||
export.LDFLAGS = append(export.LDFLAGS, "-Wl,-rpath,"+clangLib)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add sysroot for macOS only
|
// Add sysroot for macOS only
|
||||||
if goos == "darwin" {
|
if goos == "darwin" {
|
||||||
sysrootPath, sysrootErr := getMacOSSysroot()
|
sysrootPath, sysrootErr := getMacOSSysroot()
|
||||||
@@ -452,7 +461,7 @@ func useTarget(targetName string) (export Export, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Check for ESP Clang support for target-based builds
|
// Check for ESP Clang support for target-based builds
|
||||||
clangRoot, err := getESPClangRoot()
|
clangRoot, err := getESPClangRoot(true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -585,9 +594,9 @@ func useTarget(targetName string) (export Export, err error) {
|
|||||||
|
|
||||||
// Use extends the original Use function to support target-based configuration
|
// Use extends the original Use function to support target-based configuration
|
||||||
// If targetName is provided, it takes precedence over goos/goarch
|
// If targetName is provided, it takes precedence over goos/goarch
|
||||||
func Use(goos, goarch string, wasiThreads bool, targetName string) (export Export, err error) {
|
func Use(goos, goarch, targetName string, wasiThreads, forceEspClang bool) (export Export, err error) {
|
||||||
if targetName != "" {
|
if targetName != "" {
|
||||||
return useTarget(targetName)
|
return useTarget(targetName)
|
||||||
}
|
}
|
||||||
return use(goos, goarch, wasiThreads)
|
return use(goos, goarch, wasiThreads, forceEspClang)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,10 +37,10 @@ func TestUseCrossCompileSDK(t *testing.T) {
|
|||||||
name: "Same Platform",
|
name: "Same Platform",
|
||||||
goos: runtime.GOOS,
|
goos: runtime.GOOS,
|
||||||
goarch: runtime.GOARCH,
|
goarch: runtime.GOARCH,
|
||||||
expectSDK: true, // Changed: now we expect flags even for same platform
|
expectSDK: true, // Changed: now we expect flags even for same platform
|
||||||
expectCCFlags: true, // Changed: CCFLAGS will contain sysroot
|
expectCCFlags: true, // Changed: CCFLAGS will contain sysroot
|
||||||
expectCFlags: true, // Changed: CFLAGS will contain include paths
|
expectCFlags: false, // Changed: CFLAGS will not contain include paths
|
||||||
expectLDFlags: true, // Changed: LDFLAGS will contain library paths
|
expectLDFlags: false, // Changed: LDFLAGS will not contain library paths
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "WASM Target",
|
name: "WASM Target",
|
||||||
@@ -76,7 +76,7 @@ func TestUseCrossCompileSDK(t *testing.T) {
|
|||||||
|
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
export, err := use(tc.goos, tc.goarch, false)
|
export, err := use(tc.goos, tc.goarch, false, false)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Unexpected error: %v", err)
|
t.Fatalf("Unexpected error: %v", err)
|
||||||
@@ -279,7 +279,7 @@ func TestUseTarget(t *testing.T) {
|
|||||||
|
|
||||||
func TestUseWithTarget(t *testing.T) {
|
func TestUseWithTarget(t *testing.T) {
|
||||||
// Test target-based configuration takes precedence
|
// Test target-based configuration takes precedence
|
||||||
export, err := Use("linux", "amd64", false, "wasi")
|
export, err := Use("linux", "amd64", "wasi", false, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Unexpected error: %v", err)
|
t.Fatalf("Unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
@@ -291,7 +291,7 @@ func TestUseWithTarget(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Test fallback to goos/goarch when no target specified
|
// Test fallback to goos/goarch when no target specified
|
||||||
export, err = Use(runtime.GOOS, runtime.GOARCH, false, "")
|
export, err = Use(runtime.GOOS, runtime.GOARCH, "", false, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Unexpected error: %v", err)
|
t.Fatalf("Unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user