@@ -345,7 +345,7 @@ func (c *Cbuild) CMakeTargetCompileOptionsGlobal(name string, scope string) stri
345345 }
346346 }
347347 // add global misc options
348- c .GetCompileOptionsLanguageMap (c .BuildDescType .Misc , & optionsMap )
348+ c .GetCompileOptionsLanguageMap (( c . BuildDescType . Lto ), c .BuildDescType .Misc , & optionsMap )
349349
350350 // pre-includes global
351351 for _ , preInclude := range c .PreIncludeGlobal {
@@ -370,11 +370,11 @@ func (c *Cbuild) CMakeTargetLinkLibraries(name string, scope string, libraries .
370370 return content
371371}
372372
373- func (c * Cbuild ) CMakeTargetCompileOptions (name string , scope string , misc Misc , preIncludes []string , parent string ) string {
373+ func (c * Cbuild ) CMakeTargetCompileOptions (name string , scope string , lto bool , misc Misc , preIncludes []string , parent string ) string {
374374 content := "\n target_compile_options(" + name + " " + scope
375375 content += "\n $<TARGET_PROPERTY:" + parent + ",INTERFACE_COMPILE_OPTIONS>"
376376 optionsMap := make (map [string ][]string )
377- c .GetCompileOptionsLanguageMap (misc , & optionsMap )
377+ c .GetCompileOptionsLanguageMap (lto , misc , & optionsMap )
378378 for _ , preInclude := range preIncludes {
379379 optionsMap ["C,CXX" ] = append (optionsMap ["C,CXX" ], "${_PI}\" " + preInclude + "\" " )
380380 }
@@ -406,19 +406,31 @@ func (c *Cbuild) CMakeTargetCompileOptionsAbstractions(name string, abstractions
406406 return content
407407}
408408
409- func (c * Cbuild ) GetCompileOptionsLanguageMap (misc Misc , optionsMap * map [string ][]string ) {
409+ func (c * Cbuild ) GetCompileOptionsLanguageMap (lto bool , misc Misc , optionsMap * map [string ][]string ) {
410410 for _ , language := range c .Languages {
411411 switch language {
412412 case "ASM" :
413413 if len (misc .ASM ) > 0 {
414414 (* optionsMap )[language ] = append ((* optionsMap )[language ], misc .ASM ... )
415415 }
416416 case "C" , "CXX" :
417- if language == "C" && len (misc .C ) > 0 {
418- (* optionsMap )[language ] = append ((* optionsMap )[language ], misc .C ... )
417+ if language == "C" {
418+ if lto {
419+ c .LinkerLto = true
420+ (* optionsMap )[language ] = append ((* optionsMap )[language ], "${CC_LTO}" )
421+ }
422+ if len (misc .C ) > 0 {
423+ (* optionsMap )[language ] = append ((* optionsMap )[language ], misc .C ... )
424+ }
419425 }
420- if language == "CXX" && len (misc .CPP ) > 0 {
421- (* optionsMap )[language ] = append ((* optionsMap )[language ], misc .CPP ... )
426+ if language == "CXX" {
427+ if lto {
428+ c .LinkerLto = true
429+ (* optionsMap )[language ] = append ((* optionsMap )[language ], "${CXX_LTO}" )
430+ }
431+ if len (misc .CPP ) > 0 {
432+ (* optionsMap )[language ] = append ((* optionsMap )[language ], misc .CPP ... )
433+ }
422434 }
423435 if len (misc .CCPP ) > 0 {
424436 (* optionsMap )[language ] = append ((* optionsMap )[language ], misc .CCPP ... )
@@ -460,6 +472,9 @@ func GetFileOptions(file Files, hasAbstractions bool, delimiter string) string {
460472 case "CXX" :
461473 options = append (file .Misc .CPP , file .Misc .CCPP ... )
462474 }
475+ if file .Lto {
476+ options = append (options , "${" + prefix + "_LTO}" )
477+ }
463478 if hasAbstractions {
464479 options = append (options , "${" + prefix + "_OPTIONS_FLAGS}" )
465480 }
@@ -759,6 +774,7 @@ func (c *Cbuild) CMakeSetFileProperties(file Files, abstractions CompilerAbstrac
759774 // file build options
760775 language := GetLanguage (file )
761776 hasMisc := ! IsCompileMiscEmpty (file .Misc )
777+ c .LinkerLto = c .LinkerLto || file .Lto
762778 // file compiler abstractions
763779 hasAbstractions := ! AreAbstractionsEmpty (abstractions , []string {language })
764780 if hasAbstractions {
@@ -768,9 +784,9 @@ func (c *Cbuild) CMakeSetFileProperties(file Files, abstractions CompilerAbstrac
768784 filename := c .AddRootPrefix (c .ContextRoot , file .File )
769785 isGenerated := slices .Contains (c .GeneratedFiles , filename )
770786 // set file properties
771- if hasMisc || hasAbstractions || isGenerated {
787+ if hasMisc || file . Lto || hasAbstractions || isGenerated {
772788 content += "\n set_source_files_properties(\" " + filename + "\" PROPERTIES"
773- if hasMisc || hasAbstractions {
789+ if hasMisc || file . Lto || hasAbstractions {
774790 content += "\n COMPILE_OPTIONS \" " + GetFileOptions (file , hasAbstractions , ";" ) + "\" "
775791 }
776792 if isGenerated {
@@ -834,6 +850,9 @@ func (c *Cbuild) LinkerOptions() (linkerVars string, linkerOptions string) {
834850 if len (c .BuildDescType .Processor .Trustzone ) > 0 {
835851 linkerOptions += "\n " + AddShellPrefix ("${LD_SECURE}" )
836852 }
853+ if c .LinkerLto {
854+ linkerOptions += "\n " + AddShellPrefix ("${LD_LTO}" )
855+ }
837856 options := c .BuildDescType .Misc .Link
838857 for _ , language := range c .Languages {
839858 if language == "C" {
0 commit comments