Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix groups compiler abstractions inheritance #234

Merged
merged 1 commit into from
Nov 25, 2024
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
14 changes: 14 additions & 0 deletions cmd/cbuild2cmake/commands/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,4 +310,18 @@ set(OUTPUTS_1
assert.Nil(err)
assert.False(mismatch)
})

t.Run("test abstractions", func(t *testing.T) {
cmd := commands.NewRootCmd()
testCaseRoot := testRoot + "/run/solutions/abstractions"
cbuildIdxFile := testCaseRoot + "/solution.cbuild-idx.yml"
cmd.SetArgs([]string{cbuildIdxFile, "--debug"})
err := cmd.Execute()
assert.Nil(err)

// check golden references
err, mismatch := inittest.CompareFiles(testCaseRoot+"/ref", testCaseRoot+"/tmp")
assert.Nil(err)
assert.False(mismatch)
})
}
23 changes: 9 additions & 14 deletions pkg/maker/buildcontent.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,14 +392,14 @@ func (c *Cbuild) CMakeTargetCompileOptionsAbstractions(name string, abstractions
if language == "C" {
prefix = "CC"
}
if !IsAbstractionEmpty(abstractions, language) {
if !AreAbstractionsEmpty(abstractions, []string{language}) {
content += "\ncbuild_set_options_flags(" + prefix
content += c.SetOptionsFlags(abstractions, language)
content += " " + prefix + "_OPTIONS_FLAGS_" + name + ")"
options += c.LanguageSpecificCompileOptions(language, "${"+prefix+"_OPTIONS_FLAGS_"+name+"}")
}
}
if len(content) > 0 {
if len(options) > 0 {
content += "\ntarget_compile_options(" + name + "_ABSTRACTIONS INTERFACE" + options + "\n)"
}
return content
Expand Down Expand Up @@ -434,23 +434,18 @@ func IsCompileMiscEmpty(misc Misc) bool {
}

func AreAbstractionsEmpty(abstractions CompilerAbstractions, languages []string) bool {
if len(abstractions.Debug) > 0 || len(abstractions.Optimize) > 0 || len(abstractions.Warnings) > 0 {
return false
}
for _, language := range languages {
if !IsAbstractionEmpty(abstractions, language) {
if (language == "C" && len(abstractions.LanguageC) > 0) ||
(language == "CXX" && len(abstractions.LanguageCpp) > 0) {
return false
}
}
return true
}

func IsAbstractionEmpty(abstractions CompilerAbstractions, language string) bool {
if len(abstractions.Debug) > 0 || len(abstractions.Optimize) > 0 || len(abstractions.Warnings) > 0 ||
(language == "C" && len(abstractions.LanguageC) > 0) ||
(language == "CXX" && len(abstractions.LanguageCpp) > 0) {
return false
}
return true
}

func GetFileOptions(file Files, hasAbstractions bool, delimiter string) string {
var options []string
language := GetLanguage(file)
Expand Down Expand Up @@ -712,7 +707,7 @@ func HasFileAbstractions(files []Files) bool {
for _, file := range files {
if strings.Contains(file.Category, "source") {
fileAbstractions := CompilerAbstractions{file.Debug, file.Optimize, file.Warnings, file.LanguageC, file.LanguageCpp}
hasFileAbstractions = !IsAbstractionEmpty(fileAbstractions, GetLanguage(file))
hasFileAbstractions = !AreAbstractionsEmpty(fileAbstractions, []string{GetLanguage(file)})
if hasFileAbstractions {
break
}
Expand Down Expand Up @@ -764,7 +759,7 @@ func (c *Cbuild) CMakeSetFileProperties(file Files, abstractions CompilerAbstrac
language := GetLanguage(file)
hasMisc := !IsCompileMiscEmpty(file.Misc)
// file compiler abstractions
hasAbstractions := !IsAbstractionEmpty(abstractions, language)
hasAbstractions := !AreAbstractionsEmpty(abstractions, []string{language})
if hasAbstractions {
content += c.CompilerAbstractions(abstractions, language)
}
Expand Down
18 changes: 8 additions & 10 deletions pkg/maker/contextlists.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,22 +224,20 @@ func (c *Cbuild) CMakeCreateGroupRecursively(parent string, groups []Groups,
content += CMakeTargetCompileDefinitions(name, parentName, scope, group.Define, group.Undefine)
group.DefineAsm = utils.AppendDefines(group.DefineAsm, parentDefineAsm)
// compiler abstractions
var libraries []string
hasFileAbstractions := HasFileAbstractions(group.Files)
groupAbstractions := CompilerAbstractions{group.Debug, group.Optimize, group.Warnings, group.LanguageC, group.LanguageCpp}
languages := utils.AppendUniquely(maps.Keys(buildFiles.Source), maps.Keys(buildFiles.Custom)...)
var abstractions CompilerAbstractions
if !AreAbstractionsEmpty(groupAbstractions, c.Languages) {
abstractions = InheritCompilerAbstractions(parentAbstractions, groupAbstractions)
if !hasFileAbstractions {
abstractions := InheritCompilerAbstractions(parentAbstractions, groupAbstractions)
if !AreAbstractionsEmpty(abstractions, c.Languages) && (!hasFileAbstractions || hasChildren) {
if AreAbstractionsEmpty(groupAbstractions, c.Languages) {
content += c.CMakeTargetCompileOptionsAbstractions(name, CompilerAbstractions{}, []string{})
content += c.CMakeTargetLinkLibraries(name+"_ABSTRACTIONS", "INTERFACE", parentName+"_ABSTRACTIONS")
} else {
content += c.CMakeTargetCompileOptionsAbstractions(name, abstractions, languages)
}
}
var libraries []string
if !buildFiles.Interface && !hasFileAbstractions {
if !AreAbstractionsEmpty(groupAbstractions, languages) {
if !buildFiles.Interface && !hasFileAbstractions {
libraries = append(libraries, name+"_ABSTRACTIONS")
} else if !AreAbstractionsEmpty(parentAbstractions, languages) {
libraries = append(libraries, parentName+"_ABSTRACTIONS")
}
}
// target_compile_options
Expand Down
5 changes: 2 additions & 3 deletions pkg/maker/superlists.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (m *Maker) CreateSuperCMakeLists() error {
dirs = dirs + " \"${CMAKE_CURRENT_SOURCE_DIR}/" + cbuild.BuildDescType.Context + "\"\n"

var contextOutputsName = "OUTPUTS_" + strconv.Itoa(i+1)
contextOutputs += "set(" + contextOutputsName + "\n"
contextOutputs += "\nset(" + contextOutputsName + "\n"

var outputFile string
for _, output := range cbuild.BuildDescType.Output {
Expand All @@ -40,7 +40,7 @@ func (m *Maker) CreateSuperCMakeLists() error {
contextOutputs += " \"" + output + "\"\n"
}

contextOutputs += ")\n"
contextOutputs += ")"
}

solutionRoot, _ := filepath.EvalSymlinks(m.SolutionRoot)
Expand Down Expand Up @@ -78,7 +78,6 @@ math(EXPR CONTEXTS_LENGTH "${CONTEXTS_LENGTH}-1")

set(DIRS
` + dirs + `)

` + contextOutputs + `

set(ARGS
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// expect optimize: size

#ifdef __OPTIMIZE__
#error "__OPTIMIZE__ was defined"
#endif

#ifdef __OPTIMIZE_SIZE__
#error "__OPTIMIZE_SIZE__ was defined"
#endif
13 changes: 13 additions & 0 deletions test/data/solutions/abstractions/project/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// expect optimize: size

#ifndef __OPTIMIZE__
#error "__OPTIMIZE__ was not defined"
#endif

#ifndef __OPTIMIZE_SIZE__
#error "__OPTIMIZE_SIZE__ was not defined"
#endif

int main(void) {
return 0;
}
9 changes: 9 additions & 0 deletions test/data/solutions/abstractions/project/optimize_size.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// expect optimize: size

#ifndef __OPTIMIZE__
#error "__OPTIMIZE__ was not defined"
#endif

#ifndef __OPTIMIZE_SIZE__
#error "__OPTIMIZE_SIZE__ was not defined"
#endif
9 changes: 9 additions & 0 deletions test/data/solutions/abstractions/project/optimize_speed.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// expect optimize: speed

#ifndef __OPTIMIZE__
#error "__OPTIMIZE__ was not defined"
#endif

#ifdef __OPTIMIZE_SIZE__
#error "__OPTIMIZE_SIZE__ was defined"
#endif
147 changes: 147 additions & 0 deletions test/data/solutions/abstractions/project/project.GCC+ARMCM0.cbuild.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
build:
generated-by: csolution version 2.6.0
solution: ../solution.csolution.yml
project: project.cproject.yml
context: project.GCC+ARMCM0
compiler: GCC
device: ARMCM0
device-pack: ARM::[email protected]
processor:
fpu: off
core: Cortex-M0
packs:
- pack: ARM::[email protected]
path: ${CMSIS_PACK_ROOT}/ARM/CMSIS/6.1.0
- pack: ARM::[email protected]
path: ${CMSIS_PACK_ROOT}/ARM/Cortex_DFP/1.1.0
optimize: size
debug: off
misc:
C:
- -std=gnu11
- -masm-syntax-unified
- -fomit-frame-pointer
- -ffunction-sections
- -fdata-sections
CPP:
- -masm-syntax-unified
- -fomit-frame-pointer
- -ffunction-sections
- -fdata-sections
Link:
- --specs=nano.specs
- --specs=nosys.specs
- -Wl,--gc-sections
- -Wl,--no-warn-rwx-segments
define:
- ARMCM0
- _RTE_
define-asm:
- ARMCM0
- _RTE_
add-path:
- RTE/_GCC_ARMCM0
- ${CMSIS_PACK_ROOT}/ARM/CMSIS/6.1.0/CMSIS/Core/Include
- ${CMSIS_PACK_ROOT}/ARM/Cortex_DFP/1.1.0/Device/ARMCM0/Include
add-path-asm:
- RTE/_GCC_ARMCM0
- ${CMSIS_PACK_ROOT}/ARM/CMSIS/6.1.0/CMSIS/Core/Include
- ${CMSIS_PACK_ROOT}/ARM/Cortex_DFP/1.1.0/Device/ARMCM0/Include
output-dirs:
intdir: ../tmp
outdir: ../out/project/ARMCM0/GCC
rtedir: RTE
output:
- type: elf
file: project.elf
components:
- component: ARM::CMSIS:[email protected]
condition: ARMv6_7_8-M Device
from-pack: ARM::[email protected]
selected-by: ARM::CMSIS:CORE
files:
- file: ${CMSIS_PACK_ROOT}/ARM/CMSIS/6.1.0/CMSIS/Core/Include
category: include
version: 6.1.0
- file: ${CMSIS_PACK_ROOT}/ARM/CMSIS/6.1.0/CMSIS/Core/Include/tz_context.h
category: header
version: 6.1.0
- file: ${CMSIS_PACK_ROOT}/ARM/CMSIS/6.1.0/CMSIS/Core/Template/ARMv8-M/main_s.c
category: sourceC
attr: template
version: 1.1.1
select: Secure mode 'main' module for ARMv8-M
- file: ${CMSIS_PACK_ROOT}/ARM/CMSIS/6.1.0/CMSIS/Core/Template/ARMv8-M/tz_context.c
category: sourceC
attr: template
version: 1.1.1
select: RTOS Context Management (TrustZone for ARMv8-M)
- file: ${CMSIS_PACK_ROOT}/ARM/CMSIS/6.1.0/CMSIS/Documentation/html/Core/index.html
category: doc
version: 6.1.0
- component: ARM::Device:Startup&C [email protected]
condition: ARMCM0 CMSIS
from-pack: ARM::[email protected]
selected-by: ARM::Device:Startup&C Startup
optimize: none
files:
- file: ${CMSIS_PACK_ROOT}/ARM/Cortex_DFP/1.1.0/Device/ARMCM0/Include/ARMCM0.h
category: header
version: 2.2.0
- file: RTE/Device/ARMCM0/ARMCM0_gcc.ld
category: linkerScript
attr: config
version: 2.2.0
- file: RTE/Device/ARMCM0/startup_ARMCM0.c
category: sourceC
attr: config
version: 2.0.3
- file: RTE/Device/ARMCM0/system_ARMCM0.c
category: sourceC
attr: config
version: 1.0.0
linker:
script: RTE/Device/ARMCM0/ARMCM0_gcc.ld
groups:
- group: Group1
files:
- file: main.c
category: sourceC
- group: Group2
optimize: none
files:
- file: optimize_none1.c
category: sourceC
- file: optimize_speed1.c
category: sourceC
optimize: speed
groups:
- group: SubGroup
files:
- file: optimize_none2.c
category: sourceC
groups:
- group: SubGroup2
optimize: speed
files:
- file: optimize_speed2.c
category: sourceC
- group: EmptyParent
groups:
- group: NestedChild
files:
- file: optimize_size1.c
category: sourceC
- file: optimize_size2.c
category: sourceC
constructed-files:
- file: RTE/_GCC_ARMCM0/RTE_Components.h
category: header
licenses:
- license: Apache-2.0
packs:
- pack: ARM::[email protected]
- pack: ARM::[email protected]
components:
- component: ARM::CMSIS:[email protected]
- component: ARM::Device:Startup&C [email protected]
36 changes: 36 additions & 0 deletions test/data/solutions/abstractions/project/project.cproject.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# yaml-language-server: $schema=https://raw.githubusercontent.com/Open-CMSIS-Pack/devtools/main/tools/projmgr/schemas/cproject.schema.json

project:

components:
- component: ARM::CMSIS:CORE
- component: ARM::Device:Startup&C Startup
optimize: none

#inherited from build-type: optimize: size

groups:
- group: Group1
files:
- file: ./main.c
- group: Group2
optimize: none
files:
- file: ./optimize_none1.c
- file: ./optimize_speed1.c
optimize: speed
groups:
- group: SubGroup
files:
- file: ./optimize_none2.c
groups:
- group: SubGroup2
optimize: speed
files:
- file: ./optimize_speed2.c
- group: EmptyParent
groups:
- group: NestedChild
files:
- file: ./optimize_size1.c
- file: ./optimize_size2.c
Loading
Loading