@@ -65,19 +65,20 @@ type Pluto struct {
6565
6666 PtCache string // Root cache directory (PTCACHE)
6767 CacheDir string // Project-specific cache directory (<PTCACHE>/<modulePath>)
68+ Config buildConfig
6869
6970 Ctx llvm.Context // LLVM context and code‐compiler for "code" files
7071}
7172
72- // sanitizeVersion returns a filesystem-safe version string .
73- // Returns error for path traversal attempts or empty versions .
74- func sanitizeVersion (v string ) (string , error ) {
73+ // sanitizeCacheComponent returns a filesystem-safe cache path component .
74+ // Returns error for path traversal attempts or empty values .
75+ func sanitizeCacheComponent (v string ) (string , error ) {
7576 if v == "" {
76- return "" , fmt .Errorf ("invalid version : empty string" )
77+ return "" , fmt .Errorf ("invalid cache component : empty string" )
7778 }
7879 escaped := url .PathEscape (v )
7980 if escaped == "." || escaped == ".." {
80- return "" , fmt .Errorf ("invalid version : %q" , v )
81+ return "" , fmt .Errorf ("invalid cache component : %q" , v )
8182 }
8283 return escaped , nil
8384}
@@ -330,19 +331,13 @@ func (p *Pluto) GenBinary(scriptLL, bin string, rtObjs []string) error {
330331 }
331332
332333 // 1) Optimize IR
333- if out , err := exec .Command (OPT_BIN , OPT_LEVEL , "-S" , scriptLL , "-o" , optFile ).CombinedOutput (); err != nil {
334+ if out , err := exec .Command (OPT_BIN , optCommandArgs ( p . Config , scriptLL , optFile ) ... ).CombinedOutput (); err != nil {
334335 fmt .Printf ("optimization failed: %v\n %s\n " , err , out )
335336 return err
336337 }
337338
338339 // 2) Lower to object
339- llcArgs := []string {FILETYPE_OBJ }
340- // PIC is ELF/Mach-O specific; avoid on Windows COFF
341- if runtime .GOOS != OS_WINDOWS {
342- llcArgs = append (llcArgs , RELOC_PIC )
343- }
344- llcArgs = append (llcArgs , optFile , "-o" , objFile )
345- if out , err := exec .Command (LLC_BIN , llcArgs ... ).CombinedOutput (); err != nil {
340+ if out , err := exec .Command (LLC_BIN , llcCommandArgs (p .Config , optFile , objFile )... ).CombinedOutput (); err != nil {
346341 fmt .Printf ("llc compilation failed: %v\n %s\n " , err , out )
347342 return err
348343 }
@@ -377,6 +372,24 @@ func (p *Pluto) GenBinary(scriptLL, bin string, rtObjs []string) error {
377372 return nil
378373}
379374
375+ func optCommandArgs (cfg buildConfig , scriptLL , optFile string ) []string {
376+ args := []string {OPT_LEVEL }
377+ args = append (args , cfg .llvmCodegenFlags ()... )
378+ args = append (args , "-S" , scriptLL , "-o" , optFile )
379+ return args
380+ }
381+
382+ func llcCommandArgs (cfg buildConfig , optFile , objFile string ) []string {
383+ args := []string {FILETYPE_OBJ }
384+ args = append (args , cfg .llvmCodegenFlags ()... )
385+ // PIC is ELF/Mach-O specific; avoid on Windows COFF
386+ if runtime .GOOS != OS_WINDOWS {
387+ args = append (args , RELOC_PIC )
388+ }
389+ args = append (args , optFile , "-o" , objFile )
390+ return args
391+ }
392+
380393func (p * Pluto ) ScanPlutoFiles (specificScript string ) ([]string , []string ) {
381394 dirEntries , err := os .ReadDir (p .Cwd )
382395 if err != nil {
@@ -411,8 +424,9 @@ func New(cwd string) *Pluto {
411424 fmt .Println ("Current working directory is" , cwd )
412425
413426 ptcache := defaultPTCache ()
427+ cfg := currentBuildConfig ()
414428 // Include version in cache path to isolate different compiler versions
415- safeVersion , err := sanitizeVersion (Version )
429+ safeVersion , err := sanitizeCacheComponent (Version )
416430 if err != nil {
417431 fmt .Printf ("Error: %v\n " , err )
418432 os .Exit (1 )
@@ -427,6 +441,7 @@ func New(cwd string) *Pluto {
427441 p := & Pluto {
428442 Cwd : cwd ,
429443 PtCache : versionedCache ,
444+ Config : cfg ,
430445 Ctx : llvm .NewContext (),
431446 }
432447
@@ -436,7 +451,15 @@ func New(cwd string) *Pluto {
436451 }
437452
438453 // Use module path (slashes) as unique cache key
454+ targetSegment , err := p .Config .targetCPUCacheSegment ()
455+ if err != nil {
456+ fmt .Printf ("Error: %v\n " , err )
457+ os .Exit (1 )
458+ }
439459 p .CacheDir = filepath .Join (p .PtCache , filepath .FromSlash (p .ModPath ))
460+ if targetSegment != "" {
461+ p .CacheDir = filepath .Join (p .PtCache , targetSegment , filepath .FromSlash (p .ModPath ))
462+ }
440463 fmt .Printf ("Cache dir is %s\n " , p .CacheDir )
441464 fmt .Println ()
442465
@@ -474,7 +497,7 @@ func main() {
474497// runClean removes the cache directory for the current version.
475498func runClean () {
476499 ptcache := defaultPTCache ()
477- safeVersion , err := sanitizeVersion (Version )
500+ safeVersion , err := sanitizeCacheComponent (Version )
478501 if err != nil {
479502 fmt .Printf ("Error: %v\n " , err )
480503 os .Exit (1 )
@@ -524,7 +547,7 @@ func runCompile() {
524547 p := New (cwd )
525548
526549 // Prepare runtime once (in PtCache root, shared across all projects)
527- rtObjs , err := prepareRuntime (p .PtCache )
550+ rtObjs , err := prepareRuntime (p .PtCache , p . Config )
528551 if err != nil {
529552 fmt .Printf ("Error preparing runtime: %v\n " , err )
530553 os .Exit (1 )
0 commit comments