Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 29 additions & 10 deletions pkg/maker/buildcontent.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ func (c *Cbuild) CMakeTargetCompileOptionsGlobal(name string, scope string) stri
}
}
// add global misc options
c.GetCompileOptionsLanguageMap(c.BuildDescType.Misc, &optionsMap)
c.GetCompileOptionsLanguageMap((c.BuildDescType.Lto), c.BuildDescType.Misc, &optionsMap)

// pre-includes global
for _, preInclude := range c.PreIncludeGlobal {
Expand All @@ -370,11 +370,11 @@ func (c *Cbuild) CMakeTargetLinkLibraries(name string, scope string, libraries .
return content
}

func (c *Cbuild) CMakeTargetCompileOptions(name string, scope string, misc Misc, preIncludes []string, parent string) string {
func (c *Cbuild) CMakeTargetCompileOptions(name string, scope string, lto bool, misc Misc, preIncludes []string, parent string) string {
content := "\ntarget_compile_options(" + name + " " + scope
content += "\n $<TARGET_PROPERTY:" + parent + ",INTERFACE_COMPILE_OPTIONS>"
optionsMap := make(map[string][]string)
c.GetCompileOptionsLanguageMap(misc, &optionsMap)
c.GetCompileOptionsLanguageMap(lto, misc, &optionsMap)
for _, preInclude := range preIncludes {
optionsMap["C,CXX"] = append(optionsMap["C,CXX"], "${_PI}\""+preInclude+"\"")
}
Expand Down Expand Up @@ -406,19 +406,31 @@ func (c *Cbuild) CMakeTargetCompileOptionsAbstractions(name string, abstractions
return content
}

func (c *Cbuild) GetCompileOptionsLanguageMap(misc Misc, optionsMap *map[string][]string) {
func (c *Cbuild) GetCompileOptionsLanguageMap(lto bool, misc Misc, optionsMap *map[string][]string) {
for _, language := range c.Languages {
switch language {
case "ASM":
if len(misc.ASM) > 0 {
(*optionsMap)[language] = append((*optionsMap)[language], misc.ASM...)
}
case "C", "CXX":
if language == "C" && len(misc.C) > 0 {
(*optionsMap)[language] = append((*optionsMap)[language], misc.C...)
if language == "C" {
if lto {
c.LinkerLto = true
(*optionsMap)[language] = append((*optionsMap)[language], "${CC_LTO}")
}
if len(misc.C) > 0 {
(*optionsMap)[language] = append((*optionsMap)[language], misc.C...)
}
}
if language == "CXX" && len(misc.CPP) > 0 {
(*optionsMap)[language] = append((*optionsMap)[language], misc.CPP...)
if language == "CXX" {
if lto {
c.LinkerLto = true
(*optionsMap)[language] = append((*optionsMap)[language], "${CXX_LTO}")
}
if len(misc.CPP) > 0 {
(*optionsMap)[language] = append((*optionsMap)[language], misc.CPP...)
}
}
if len(misc.CCPP) > 0 {
(*optionsMap)[language] = append((*optionsMap)[language], misc.CCPP...)
Expand Down Expand Up @@ -460,6 +472,9 @@ func GetFileOptions(file Files, hasAbstractions bool, delimiter string) string {
case "CXX":
options = append(file.Misc.CPP, file.Misc.CCPP...)
}
if file.Lto {
options = append(options, "${"+prefix+"_LTO}")
}
if hasAbstractions {
options = append(options, "${"+prefix+"_OPTIONS_FLAGS}")
}
Expand Down Expand Up @@ -759,6 +774,7 @@ func (c *Cbuild) CMakeSetFileProperties(file Files, abstractions CompilerAbstrac
// file build options
language := GetLanguage(file)
hasMisc := !IsCompileMiscEmpty(file.Misc)
c.LinkerLto = c.LinkerLto || file.Lto
// file compiler abstractions
hasAbstractions := !AreAbstractionsEmpty(abstractions, []string{language})
if hasAbstractions {
Expand All @@ -768,9 +784,9 @@ func (c *Cbuild) CMakeSetFileProperties(file Files, abstractions CompilerAbstrac
filename := c.AddRootPrefix(c.ContextRoot, file.File)
isGenerated := slices.Contains(c.GeneratedFiles, filename)
// set file properties
if hasMisc || hasAbstractions || isGenerated {
if hasMisc || file.Lto || hasAbstractions || isGenerated {
content += "\nset_source_files_properties(\"" + filename + "\" PROPERTIES"
if hasMisc || hasAbstractions {
if hasMisc || file.Lto || hasAbstractions {
content += "\n COMPILE_OPTIONS \"" + GetFileOptions(file, hasAbstractions, ";") + "\""
}
if isGenerated {
Expand Down Expand Up @@ -834,6 +850,9 @@ func (c *Cbuild) LinkerOptions() (linkerVars string, linkerOptions string) {
if len(c.BuildDescType.Processor.Trustzone) > 0 {
linkerOptions += "\n " + AddShellPrefix("${LD_SECURE}")
}
if c.LinkerLto {
linkerOptions += "\n " + AddShellPrefix("${LD_LTO}")
}
options := c.BuildDescType.Misc.Link
for _, language := range c.Languages {
if language == "C" {
Expand Down
7 changes: 4 additions & 3 deletions pkg/maker/buildcontent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,14 @@ func TestBuildContent(t *testing.T) {
CPP: []string{"-cpp-flag"},
CCPP: []string{"-c-cpp-flag"},
}
lto := true
var cbuild maker.Cbuild
cbuild.Languages = []string{"ASM", "C", "CXX"}
preIncludes := []string{"${SOLUTION_ROOT}/project/RTE/class/pre-include.h"}
content := cbuild.CMakeTargetCompileOptions("TARGET", "PUBLIC", misc, preIncludes, "${CONTEXT}")
content := cbuild.CMakeTargetCompileOptions("TARGET", "PUBLIC", lto, misc, preIncludes, "${CONTEXT}")
assert.Contains(content, "$<$<COMPILE_LANGUAGE:ASM>:\n \"SHELL:-asm-flag\"")
assert.Contains(content, "$<$<COMPILE_LANGUAGE:C>:\n \"SHELL:-c-flag\"\n \"SHELL:-c-cpp-flag\"")
assert.Contains(content, "$<$<COMPILE_LANGUAGE:CXX>:\n \"SHELL:-cpp-flag\"\n \"SHELL:-c-cpp-flag\"")
assert.Contains(content, "$<$<COMPILE_LANGUAGE:C>:\n \"SHELL:${CC_LTO}\"\n \"SHELL:-c-flag\"\n \"SHELL:-c-cpp-flag\"")
assert.Contains(content, "$<$<COMPILE_LANGUAGE:CXX>:\n \"SHELL:${CXX_LTO}\"\n \"SHELL:-cpp-flag\"\n \"SHELL:-c-cpp-flag\"")
assert.Contains(content, "\"SHELL:${_PI}\\\"${SOLUTION_ROOT}/project/RTE/class/pre-include.h\\\"\"")
})

Expand Down
6 changes: 3 additions & 3 deletions pkg/maker/contextlists.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ func (c *Cbuild) CMakeCreateGroupRecursively(parent string, groups []Groups,
}
// target_compile_options
if hasChildren || len(buildFiles.Source) > 0 || len(buildFiles.Custom) > 0 {
content += c.CMakeTargetCompileOptions(name, scope, group.Misc, buildFiles.PreIncludeLocal, parentName)
content += c.CMakeTargetCompileOptions(name, scope, group.Lto, group.Misc, buildFiles.PreIncludeLocal, parentName)
}
// target_link_libraries
libraries = append(libraries, buildFiles.Library...)
Expand All @@ -269,7 +269,7 @@ func (c *Cbuild) CMakeCreateGroupRecursively(parent string, groups []Groups,
content += CMakeTargetCompileDefinitions(fileTargetName, name, "PUBLIC", file.Define, file.Undefine)
}
// target_compile_options
content += c.CMakeTargetCompileOptions(fileTargetName, "PUBLIC", Misc{}, []string{}, name)
content += c.CMakeTargetCompileOptions(fileTargetName, "PUBLIC", false, Misc{}, []string{}, name)
}
// asm defines are set in file properties
if GetLanguage(file) == "ASM" {
Expand Down Expand Up @@ -331,7 +331,7 @@ func (c *Cbuild) CMakeCreateComponents(contextDir string) error {
}
// target_compile_options
if len(buildFiles.Source) > 0 || len(buildFiles.Custom) > 0 {
content += c.CMakeTargetCompileOptions(name, scope, component.Misc, buildFiles.PreIncludeLocal, "${CONTEXT}")
content += c.CMakeTargetCompileOptions(name, scope, component.Lto, component.Misc, buildFiles.PreIncludeLocal, "${CONTEXT}")
}
// target_link_libraries
libraries = append(libraries, buildFiles.Library...)
Expand Down
5 changes: 5 additions & 0 deletions pkg/maker/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type Cbuild struct {
Warnings string `yaml:"warnings"`
LanguageC string `yaml:"language-C"`
LanguageCpp string `yaml:"language-CPP"`
Lto bool `yaml:"link-time-optimize"`
Misc Misc `yaml:"misc"`
Define []interface{} `yaml:"define"`
DefineAsm []interface{} `yaml:"define-asm"`
Expand All @@ -83,6 +84,7 @@ type Cbuild struct {
BuildGroups []string
Toolchain string
GeneratedFiles []string
LinkerLto bool
}

type Cbuilds struct {
Expand Down Expand Up @@ -122,6 +124,7 @@ type Components struct {
Warnings string `yaml:"warnings"`
LanguageC string `yaml:"language-C"`
LanguageCpp string `yaml:"language-CPP"`
Lto bool `yaml:"link-time-optimize"`
Define []interface{} `yaml:"define"`
DefineAsm []interface{} `yaml:"define-asm"`
Undefine []string `yaml:"undefine"`
Expand Down Expand Up @@ -155,6 +158,7 @@ type Files struct {
Warnings string `yaml:"warnings"`
LanguageC string `yaml:"language-C"`
LanguageCpp string `yaml:"language-CPP"`
Lto bool `yaml:"link-time-optimize"`
Define []interface{} `yaml:"define"`
DefineAsm []interface{} `yaml:"define-asm"`
Undefine []string `yaml:"undefine"`
Expand All @@ -180,6 +184,7 @@ type Groups struct {
Warnings string `yaml:"warnings"`
LanguageC string `yaml:"language-C"`
LanguageCpp string `yaml:"language-CPP"`
Lto bool `yaml:"link-time-optimize"`
Define []interface{} `yaml:"define"`
DefineAsm []interface{} `yaml:"define-asm"`
Undefine []string `yaml:"undefine"`
Expand Down
3 changes: 3 additions & 0 deletions pkg/maker/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func TestParser(t *testing.T) {
assert.Equal("bti-signret", data.BuildDescType.Processor.BranchProtection)
assert.Equal("non-secure", data.BuildDescType.Processor.Trustzone)
assert.Equal("Cortex-M0", data.BuildDescType.Processor.Core)
assert.True(data.BuildDescType.Lto)

assert.Equal("vendorName::DFP@8.8.8", data.BuildDescType.Packs[0].Pack)
assert.Equal("${CMSIS_PACK_ROOT}/vendorName/DFP/8.8.8", data.BuildDescType.Packs[0].Path)
Expand Down Expand Up @@ -83,13 +84,15 @@ func TestParser(t *testing.T) {
assert.Equal("vendorName::DFP@8.8.8", data.BuildDescType.Components[0].FromPack)
assert.Equal("DFP:CORE@1.1.1", data.BuildDescType.Components[0].Implements)
assert.Equal("CORE", data.BuildDescType.Components[0].SelectedBy)
assert.True(data.BuildDescType.Components[0].Lto)

assert.Equal("DFP:CORE@1.1.1", data.BuildDescType.Apis[0].API)
assert.Equal("vendorName::DFP@8.8.8", data.BuildDescType.Apis[0].FromPack)

assert.Equal("Source", data.BuildDescType.Groups[0].Group)
assert.Equal("./TestSource.c", data.BuildDescType.Groups[0].Files[0].File)
assert.Equal("sourceC", data.BuildDescType.Groups[0].Files[0].Category)
assert.True(data.BuildDescType.Groups[0].Files[0].Lto)

assert.Equal("Subgroup", data.BuildDescType.Groups[0].Groups[0].Group)
assert.Equal("./TestSubgroup.c", data.BuildDescType.Groups[0].Groups[0].Files[0].File)
Expand Down
3 changes: 3 additions & 0 deletions test/data/generic/contextName0.cbuild.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ build:
from-pack: vendorName::DFP@8.8.8
implements: DFP:CORE@1.1.1
selected-by: CORE
link-time-optimize: true
apis:
- api: DFP:CORE@1.1.1
from-pack: vendorName::DFP@8.8.8
Expand All @@ -44,6 +45,7 @@ build:
regions: regions_deviceName.h
define:
- LD_PP_DEF0
link-time-optimize: true
groups:
- group: Source
files:
Expand All @@ -56,6 +58,7 @@ build:
warnings: all
language-C: c90
language-CPP: c++20
link-time-optimize: true
define:
- DEF_FILE
undefine:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
build:
generated-by: csolution version 2.6.0
generated-by: csolution version 0.0.0+gd100d305
solution: ../solution.csolution.yml
project: project.cproject.yml
context: project.GCC+ARMCM0
compiler: GCC
device: ARMCM0
device: ARM::ARMCM0
device-pack: ARM::Cortex_DFP@1.1.0
device-books:
- name: https://developer.arm.com/documentation/dui0497
title: Cortex-M0 Processor Devices Generic Users Guide
processor:
fpu: off
core: Cortex-M0
Expand Down Expand Up @@ -84,6 +87,7 @@ build:
from-pack: ARM::Cortex_DFP@1.1.0
selected-by: ARM::Device:Startup&C Startup
optimize: none
link-time-optimize: true
files:
- file: ${CMSIS_PACK_ROOT}/ARM/Cortex_DFP/1.1.0/Device/ARMCM0/Include/ARMCM0.h
category: header
Expand All @@ -104,6 +108,7 @@ build:
script: RTE/Device/ARMCM0/ARMCM0_gcc.ld
groups:
- group: Group1
link-time-optimize: true
files:
- file: main.c
category: sourceC
Expand Down Expand Up @@ -134,6 +139,7 @@ build:
category: sourceC
- file: optimize_size2.c
category: sourceC
link-time-optimize: true
constructed-files:
- file: RTE/_GCC_ARMCM0/RTE_Components.h
category: header
Expand Down
3 changes: 3 additions & 0 deletions test/data/solutions/abstractions/project/project.cproject.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ project:
- component: ARM::CMSIS:CORE
- component: ARM::Device:Startup&C Startup
optimize: none
link-time-optimize:

#inherited from build-type: optimize: size

groups:
- group: Group1
link-time-optimize:
files:
- file: ./main.c
- group: Group2
Expand All @@ -34,3 +36,4 @@ project:
files:
- file: ./optimize_size1.c
- file: ./optimize_size2.c
link-time-optimize:
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ target_link_libraries(${CONTEXT} PUBLIC
target_link_options(${CONTEXT} PUBLIC
"SHELL:${LD_CPU}"
"SHELL:${_LS}\"${LD_SCRIPT_PP}\""
"SHELL:${LD_LTO}"
"SHELL:--specs=nano.specs"
"SHELL:--specs=nosys.specs"
"SHELL:-Wl,--gc-sections"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ target_compile_options(ARM_Device_Startup_C_Startup_2_2_0_ABSTRACTIONS INTERFACE
)
target_compile_options(ARM_Device_Startup_C_Startup_2_2_0 PUBLIC
$<TARGET_PROPERTY:${CONTEXT},INTERFACE_COMPILE_OPTIONS>
$<$<COMPILE_LANGUAGE:C>:
"SHELL:${CC_LTO}"
>
)
target_link_libraries(ARM_Device_Startup_C_Startup_2_2_0 PUBLIC
ARM_Device_Startup_C_Startup_2_2_0_ABSTRACTIONS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ target_link_libraries(Group_Group1_ABSTRACTIONS INTERFACE
)
target_compile_options(Group_Group1 PUBLIC
$<TARGET_PROPERTY:${CONTEXT},INTERFACE_COMPILE_OPTIONS>
$<$<COMPILE_LANGUAGE:C>:
"SHELL:${CC_LTO}"
>
)
target_link_libraries(Group_Group1 PUBLIC
Group_Group1_ABSTRACTIONS
Expand Down Expand Up @@ -137,3 +140,6 @@ target_compile_options(Group_EmptyParent_NestedChild PUBLIC
target_link_libraries(Group_EmptyParent_NestedChild PUBLIC
Group_EmptyParent_NestedChild_ABSTRACTIONS
)
set_source_files_properties("${SOLUTION_ROOT}/project/optimize_size2.c" PROPERTIES
COMPILE_OPTIONS "${CC_LTO}"
)
Loading