Skip to content

Commit 9f3206b

Browse files
committed
feat(generator): add platform-aware compiler flags
- Add runtime OS detection to use appropriate include paths - Add feature test macros (_GNU_SOURCE, _DEFAULT_SOURCE, __STDC_CONSTANT_MACROS) to enable math constants on Linux - Avoid hardcoded paths that don't work on NixOS - Ensures consistent bindings generation across different platforms
1 parent 7005b2e commit 9f3206b

1 file changed

Lines changed: 30 additions & 5 deletions

File tree

internal/generator/parser.go

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"log"
66
"path"
77
"path/filepath"
8+
"runtime"
89
"slices"
910
"strings"
1011
"unicode"
@@ -165,6 +166,34 @@ type Parser struct {
165166
tu clang.TranslationUnit
166167
}
167168

169+
func getPlatformArgs() []string {
170+
args := []string{
171+
"-fparse-all-comments",
172+
fmt.Sprintf("-I%v", AVLibPath),
173+
"-D_GNU_SOURCE",
174+
"-D_DEFAULT_SOURCE",
175+
"-D__STDC_CONSTANT_MACROS",
176+
}
177+
178+
// Add platform-specific includes
179+
switch runtime.GOOS {
180+
case "darwin":
181+
args = append(args, "-I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/Kernel.framework/Headers/")
182+
case "linux":
183+
// For standard Linux distributions, add common include paths
184+
// These will be ignored if they don't exist, so it's safe to add them
185+
// NixOS will use its own paths through environment variables
186+
if runtime.GOARCH == "amd64" {
187+
args = append(args, "-I/usr/include/x86_64-linux-gnu")
188+
} else if runtime.GOARCH == "arm64" {
189+
args = append(args, "-I/usr/include/aarch64-linux-gnu")
190+
}
191+
args = append(args, "-I/usr/include")
192+
}
193+
194+
return args
195+
}
196+
168197
func (p *Parser) parseFile(indent string, path string) {
169198
p.path = path
170199

@@ -173,11 +202,7 @@ func (p *Parser) parseFile(indent string, path string) {
173202

174203
tu := idx.ParseTranslationUnit(
175204
path,
176-
[]string{
177-
"-fparse-all-comments",
178-
fmt.Sprintf("-I%v", AVLibPath),
179-
"-I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/Kernel.framework/Headers/",
180-
},
205+
getPlatformArgs(),
181206
nil,
182207
clang.TranslationUnit_IncludeBriefCommentsInCodeCompletion|clang.TranslationUnit_DetailedPreprocessingRecord,
183208
)

0 commit comments

Comments
 (0)