Skip to content

Commit b60b725

Browse files
authored
Fix groups compiler abstractions inheritance
1 parent 8e45fa1 commit b60b725

File tree

18 files changed

+567
-30
lines changed

18 files changed

+567
-30
lines changed

cmd/cbuild2cmake/commands/root_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,4 +310,18 @@ set(OUTPUTS_1
310310
assert.Nil(err)
311311
assert.False(mismatch)
312312
})
313+
314+
t.Run("test abstractions", func(t *testing.T) {
315+
cmd := commands.NewRootCmd()
316+
testCaseRoot := testRoot + "/run/solutions/abstractions"
317+
cbuildIdxFile := testCaseRoot + "/solution.cbuild-idx.yml"
318+
cmd.SetArgs([]string{cbuildIdxFile, "--debug"})
319+
err := cmd.Execute()
320+
assert.Nil(err)
321+
322+
// check golden references
323+
err, mismatch := inittest.CompareFiles(testCaseRoot+"/ref", testCaseRoot+"/tmp")
324+
assert.Nil(err)
325+
assert.False(mismatch)
326+
})
313327
}

pkg/maker/buildcontent.go

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -392,14 +392,14 @@ func (c *Cbuild) CMakeTargetCompileOptionsAbstractions(name string, abstractions
392392
if language == "C" {
393393
prefix = "CC"
394394
}
395-
if !IsAbstractionEmpty(abstractions, language) {
395+
if !AreAbstractionsEmpty(abstractions, []string{language}) {
396396
content += "\ncbuild_set_options_flags(" + prefix
397397
content += c.SetOptionsFlags(abstractions, language)
398398
content += " " + prefix + "_OPTIONS_FLAGS_" + name + ")"
399399
options += c.LanguageSpecificCompileOptions(language, "${"+prefix+"_OPTIONS_FLAGS_"+name+"}")
400400
}
401401
}
402-
if len(content) > 0 {
402+
if len(options) > 0 {
403403
content += "\ntarget_compile_options(" + name + "_ABSTRACTIONS INTERFACE" + options + "\n)"
404404
}
405405
return content
@@ -434,23 +434,18 @@ func IsCompileMiscEmpty(misc Misc) bool {
434434
}
435435

436436
func AreAbstractionsEmpty(abstractions CompilerAbstractions, languages []string) bool {
437+
if len(abstractions.Debug) > 0 || len(abstractions.Optimize) > 0 || len(abstractions.Warnings) > 0 {
438+
return false
439+
}
437440
for _, language := range languages {
438-
if !IsAbstractionEmpty(abstractions, language) {
441+
if (language == "C" && len(abstractions.LanguageC) > 0) ||
442+
(language == "CXX" && len(abstractions.LanguageCpp) > 0) {
439443
return false
440444
}
441445
}
442446
return true
443447
}
444448

445-
func IsAbstractionEmpty(abstractions CompilerAbstractions, language string) bool {
446-
if len(abstractions.Debug) > 0 || len(abstractions.Optimize) > 0 || len(abstractions.Warnings) > 0 ||
447-
(language == "C" && len(abstractions.LanguageC) > 0) ||
448-
(language == "CXX" && len(abstractions.LanguageCpp) > 0) {
449-
return false
450-
}
451-
return true
452-
}
453-
454449
func GetFileOptions(file Files, hasAbstractions bool, delimiter string) string {
455450
var options []string
456451
language := GetLanguage(file)
@@ -712,7 +707,7 @@ func HasFileAbstractions(files []Files) bool {
712707
for _, file := range files {
713708
if strings.Contains(file.Category, "source") {
714709
fileAbstractions := CompilerAbstractions{file.Debug, file.Optimize, file.Warnings, file.LanguageC, file.LanguageCpp}
715-
hasFileAbstractions = !IsAbstractionEmpty(fileAbstractions, GetLanguage(file))
710+
hasFileAbstractions = !AreAbstractionsEmpty(fileAbstractions, []string{GetLanguage(file)})
716711
if hasFileAbstractions {
717712
break
718713
}
@@ -764,7 +759,7 @@ func (c *Cbuild) CMakeSetFileProperties(file Files, abstractions CompilerAbstrac
764759
language := GetLanguage(file)
765760
hasMisc := !IsCompileMiscEmpty(file.Misc)
766761
// file compiler abstractions
767-
hasAbstractions := !IsAbstractionEmpty(abstractions, language)
762+
hasAbstractions := !AreAbstractionsEmpty(abstractions, []string{language})
768763
if hasAbstractions {
769764
content += c.CompilerAbstractions(abstractions, language)
770765
}

pkg/maker/contextlists.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -224,22 +224,20 @@ func (c *Cbuild) CMakeCreateGroupRecursively(parent string, groups []Groups,
224224
content += CMakeTargetCompileDefinitions(name, parentName, scope, group.Define, group.Undefine)
225225
group.DefineAsm = utils.AppendDefines(group.DefineAsm, parentDefineAsm)
226226
// compiler abstractions
227+
var libraries []string
227228
hasFileAbstractions := HasFileAbstractions(group.Files)
228229
groupAbstractions := CompilerAbstractions{group.Debug, group.Optimize, group.Warnings, group.LanguageC, group.LanguageCpp}
229230
languages := utils.AppendUniquely(maps.Keys(buildFiles.Source), maps.Keys(buildFiles.Custom)...)
230-
var abstractions CompilerAbstractions
231-
if !AreAbstractionsEmpty(groupAbstractions, c.Languages) {
232-
abstractions = InheritCompilerAbstractions(parentAbstractions, groupAbstractions)
233-
if !hasFileAbstractions {
231+
abstractions := InheritCompilerAbstractions(parentAbstractions, groupAbstractions)
232+
if !AreAbstractionsEmpty(abstractions, c.Languages) && (!hasFileAbstractions || hasChildren) {
233+
if AreAbstractionsEmpty(groupAbstractions, c.Languages) {
234+
content += c.CMakeTargetCompileOptionsAbstractions(name, CompilerAbstractions{}, []string{})
235+
content += c.CMakeTargetLinkLibraries(name+"_ABSTRACTIONS", "INTERFACE", parentName+"_ABSTRACTIONS")
236+
} else {
234237
content += c.CMakeTargetCompileOptionsAbstractions(name, abstractions, languages)
235238
}
236-
}
237-
var libraries []string
238-
if !buildFiles.Interface && !hasFileAbstractions {
239-
if !AreAbstractionsEmpty(groupAbstractions, languages) {
239+
if !buildFiles.Interface && !hasFileAbstractions {
240240
libraries = append(libraries, name+"_ABSTRACTIONS")
241-
} else if !AreAbstractionsEmpty(parentAbstractions, languages) {
242-
libraries = append(libraries, parentName+"_ABSTRACTIONS")
243241
}
244242
}
245243
// target_compile_options

pkg/maker/superlists.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func (m *Maker) CreateSuperCMakeLists() error {
2828
dirs = dirs + " \"${CMAKE_CURRENT_SOURCE_DIR}/" + cbuild.BuildDescType.Context + "\"\n"
2929

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

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

43-
contextOutputs += ")\n"
43+
contextOutputs += ")"
4444
}
4545

4646
solutionRoot, _ := filepath.EvalSymlinks(m.SolutionRoot)
@@ -78,7 +78,6 @@ math(EXPR CONTEXTS_LENGTH "${CONTEXTS_LENGTH}-1")
7878
7979
set(DIRS
8080
` + dirs + `)
81-
8281
` + contextOutputs + `
8382
8483
set(ARGS
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// expect optimize: size
2+
3+
#ifdef __OPTIMIZE__
4+
#error "__OPTIMIZE__ was defined"
5+
#endif
6+
7+
#ifdef __OPTIMIZE_SIZE__
8+
#error "__OPTIMIZE_SIZE__ was defined"
9+
#endif
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// expect optimize: size
2+
3+
#ifndef __OPTIMIZE__
4+
#error "__OPTIMIZE__ was not defined"
5+
#endif
6+
7+
#ifndef __OPTIMIZE_SIZE__
8+
#error "__OPTIMIZE_SIZE__ was not defined"
9+
#endif
10+
11+
int main(void) {
12+
return 0;
13+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// expect optimize: size
2+
3+
#ifndef __OPTIMIZE__
4+
#error "__OPTIMIZE__ was not defined"
5+
#endif
6+
7+
#ifndef __OPTIMIZE_SIZE__
8+
#error "__OPTIMIZE_SIZE__ was not defined"
9+
#endif
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// expect optimize: speed
2+
3+
#ifndef __OPTIMIZE__
4+
#error "__OPTIMIZE__ was not defined"
5+
#endif
6+
7+
#ifdef __OPTIMIZE_SIZE__
8+
#error "__OPTIMIZE_SIZE__ was defined"
9+
#endif
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
build:
2+
generated-by: csolution version 2.6.0
3+
solution: ../solution.csolution.yml
4+
project: project.cproject.yml
5+
context: project.GCC+ARMCM0
6+
compiler: GCC
7+
device: ARMCM0
8+
device-pack: ARM::[email protected]
9+
processor:
10+
fpu: off
11+
core: Cortex-M0
12+
packs:
13+
- pack: ARM::[email protected]
14+
path: ${CMSIS_PACK_ROOT}/ARM/CMSIS/6.1.0
15+
- pack: ARM::[email protected]
16+
path: ${CMSIS_PACK_ROOT}/ARM/Cortex_DFP/1.1.0
17+
optimize: size
18+
debug: off
19+
misc:
20+
C:
21+
- -std=gnu11
22+
- -masm-syntax-unified
23+
- -fomit-frame-pointer
24+
- -ffunction-sections
25+
- -fdata-sections
26+
CPP:
27+
- -masm-syntax-unified
28+
- -fomit-frame-pointer
29+
- -ffunction-sections
30+
- -fdata-sections
31+
Link:
32+
- --specs=nano.specs
33+
- --specs=nosys.specs
34+
- -Wl,--gc-sections
35+
- -Wl,--no-warn-rwx-segments
36+
define:
37+
- ARMCM0
38+
- _RTE_
39+
define-asm:
40+
- ARMCM0
41+
- _RTE_
42+
add-path:
43+
- RTE/_GCC_ARMCM0
44+
- ${CMSIS_PACK_ROOT}/ARM/CMSIS/6.1.0/CMSIS/Core/Include
45+
- ${CMSIS_PACK_ROOT}/ARM/Cortex_DFP/1.1.0/Device/ARMCM0/Include
46+
add-path-asm:
47+
- RTE/_GCC_ARMCM0
48+
- ${CMSIS_PACK_ROOT}/ARM/CMSIS/6.1.0/CMSIS/Core/Include
49+
- ${CMSIS_PACK_ROOT}/ARM/Cortex_DFP/1.1.0/Device/ARMCM0/Include
50+
output-dirs:
51+
intdir: ../tmp
52+
outdir: ../out/project/ARMCM0/GCC
53+
rtedir: RTE
54+
output:
55+
- type: elf
56+
file: project.elf
57+
components:
58+
- component: ARM::CMSIS:[email protected]
59+
condition: ARMv6_7_8-M Device
60+
from-pack: ARM::[email protected]
61+
selected-by: ARM::CMSIS:CORE
62+
files:
63+
- file: ${CMSIS_PACK_ROOT}/ARM/CMSIS/6.1.0/CMSIS/Core/Include
64+
category: include
65+
version: 6.1.0
66+
- file: ${CMSIS_PACK_ROOT}/ARM/CMSIS/6.1.0/CMSIS/Core/Include/tz_context.h
67+
category: header
68+
version: 6.1.0
69+
- file: ${CMSIS_PACK_ROOT}/ARM/CMSIS/6.1.0/CMSIS/Core/Template/ARMv8-M/main_s.c
70+
category: sourceC
71+
attr: template
72+
version: 1.1.1
73+
select: Secure mode 'main' module for ARMv8-M
74+
- file: ${CMSIS_PACK_ROOT}/ARM/CMSIS/6.1.0/CMSIS/Core/Template/ARMv8-M/tz_context.c
75+
category: sourceC
76+
attr: template
77+
version: 1.1.1
78+
select: RTOS Context Management (TrustZone for ARMv8-M)
79+
- file: ${CMSIS_PACK_ROOT}/ARM/CMSIS/6.1.0/CMSIS/Documentation/html/Core/index.html
80+
category: doc
81+
version: 6.1.0
82+
- component: ARM::Device:Startup&C [email protected]
83+
condition: ARMCM0 CMSIS
84+
from-pack: ARM::[email protected]
85+
selected-by: ARM::Device:Startup&C Startup
86+
optimize: none
87+
files:
88+
- file: ${CMSIS_PACK_ROOT}/ARM/Cortex_DFP/1.1.0/Device/ARMCM0/Include/ARMCM0.h
89+
category: header
90+
version: 2.2.0
91+
- file: RTE/Device/ARMCM0/ARMCM0_gcc.ld
92+
category: linkerScript
93+
attr: config
94+
version: 2.2.0
95+
- file: RTE/Device/ARMCM0/startup_ARMCM0.c
96+
category: sourceC
97+
attr: config
98+
version: 2.0.3
99+
- file: RTE/Device/ARMCM0/system_ARMCM0.c
100+
category: sourceC
101+
attr: config
102+
version: 1.0.0
103+
linker:
104+
script: RTE/Device/ARMCM0/ARMCM0_gcc.ld
105+
groups:
106+
- group: Group1
107+
files:
108+
- file: main.c
109+
category: sourceC
110+
- group: Group2
111+
optimize: none
112+
files:
113+
- file: optimize_none1.c
114+
category: sourceC
115+
- file: optimize_speed1.c
116+
category: sourceC
117+
optimize: speed
118+
groups:
119+
- group: SubGroup
120+
files:
121+
- file: optimize_none2.c
122+
category: sourceC
123+
groups:
124+
- group: SubGroup2
125+
optimize: speed
126+
files:
127+
- file: optimize_speed2.c
128+
category: sourceC
129+
- group: EmptyParent
130+
groups:
131+
- group: NestedChild
132+
files:
133+
- file: optimize_size1.c
134+
category: sourceC
135+
- file: optimize_size2.c
136+
category: sourceC
137+
constructed-files:
138+
- file: RTE/_GCC_ARMCM0/RTE_Components.h
139+
category: header
140+
licenses:
141+
- license: Apache-2.0
142+
packs:
143+
- pack: ARM::[email protected]
144+
- pack: ARM::[email protected]
145+
components:
146+
- component: ARM::CMSIS:[email protected]
147+
- component: ARM::Device:Startup&C [email protected]
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# yaml-language-server: $schema=https://raw.githubusercontent.com/Open-CMSIS-Pack/devtools/main/tools/projmgr/schemas/cproject.schema.json
2+
3+
project:
4+
5+
components:
6+
- component: ARM::CMSIS:CORE
7+
- component: ARM::Device:Startup&C Startup
8+
optimize: none
9+
10+
#inherited from build-type: optimize: size
11+
12+
groups:
13+
- group: Group1
14+
files:
15+
- file: ./main.c
16+
- group: Group2
17+
optimize: none
18+
files:
19+
- file: ./optimize_none1.c
20+
- file: ./optimize_speed1.c
21+
optimize: speed
22+
groups:
23+
- group: SubGroup
24+
files:
25+
- file: ./optimize_none2.c
26+
groups:
27+
- group: SubGroup2
28+
optimize: speed
29+
files:
30+
- file: ./optimize_speed2.c
31+
- group: EmptyParent
32+
groups:
33+
- group: NestedChild
34+
files:
35+
- file: ./optimize_size1.c
36+
- file: ./optimize_size2.c

0 commit comments

Comments
 (0)