Skip to content

Commit d20e4ab

Browse files
authored
[cbuild2cmake] Handle define-asm at multiple levels
1 parent 58c7d35 commit d20e4ab

File tree

20 files changed

+115
-39
lines changed

20 files changed

+115
-39
lines changed

pkg/maker/buildcontent.go

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -619,8 +619,8 @@ func HasFileAbstractions(files []Files) bool {
619619
}
620620

621621
func HasFileCustomOptions(file Files) bool {
622-
if GetLanguage(file) != "ASM" &&
623-
(len(file.AddPath) > 0 || len(file.Define) > 0 || len(file.DelPath) > 0 || len(file.Undefine) > 0) {
622+
if len(file.AddPath) > 0 || len(file.DelPath) > 0 ||
623+
(GetLanguage(file) != "ASM" && (len(file.Define) > 0 || len(file.Undefine) > 0)) {
624624
return true
625625
}
626626
return false
@@ -655,46 +655,42 @@ func (c *Cbuild) CompilerAbstractions(abstractions CompilerAbstractions, languag
655655
return content
656656
}
657657

658-
func (c *Cbuild) CMakeSetFileProperties(file Files, abstractions CompilerAbstractions, parentMiscAsm []string) string {
658+
func (c *Cbuild) CMakeSetFileProperties(file Files, abstractions CompilerAbstractions) string {
659659
var content string
660660
// file build options
661661
language := GetLanguage(file)
662-
hasIncludes := len(file.AddPath) > 0 && language == "ASM"
663-
hasDefines := len(file.Define) > 0 && language == "ASM"
664662
hasMisc := !IsCompileMiscEmpty(file.Misc)
665663
// file compiler abstractions
666664
hasAbstractions := !IsAbstractionEmpty(abstractions, language)
667665
if hasAbstractions {
668666
content += c.CompilerAbstractions(abstractions, language)
669667
}
670-
// handle specific asm defines
671-
if hasDefines {
668+
// set file properties
669+
if hasMisc || hasAbstractions {
670+
content += "\nset_source_files_properties(\"" + AddRootPrefix(c.ContextRoot, file.File) +
671+
"\" PROPERTIES\n COMPILE_OPTIONS \"" + GetFileOptions(file, hasAbstractions, ";") + "\"\n)"
672+
}
673+
return content
674+
}
675+
676+
func (c *Cbuild) SetFileAsmDefines(file Files, parentMiscAsm []string) string {
677+
var content string
678+
if len(file.DefineAsm) > 0 {
672679
flags := utils.AppendUniquely(parentMiscAsm, file.Misc.ASM...)
673680
if (c.Toolchain == "AC6" || c.Toolchain == "GCC") && path.Ext(file.File) != ".S" && !strings.Contains(utils.FindLast(flags, "-x"), "assembler-with-cpp") {
674681
syntax := "AS_GNU"
675682
masm := utils.FindLast(flags, "-masm")
676683
if c.Toolchain == "AC6" && (strings.Contains(masm, "armasm") || strings.Contains(masm, "auto")) {
677684
syntax = "AS_ARM"
678685
}
679-
content += "\nset(COMPILE_DEFINITIONS\n " + ListCompileDefinitions(file.Define, "\n ") + "\n)"
686+
content += "\nset(COMPILE_DEFINITIONS\n " + ListCompileDefinitions(file.DefineAsm, "\n ") + "\n)"
680687
content += "\ncbuild_set_defines(" + syntax + " COMPILE_DEFINITIONS)"
681-
content += "\nset_source_files_properties(\"" + AddRootPrefix(c.ContextRoot, file.File) + "\" PROPERTIES\n COMPILE_FLAGS \"${COMPILE_DEFINITIONS}\"\n)"
682-
hasDefines = false
683-
}
684-
}
685-
// set file properties
686-
if hasIncludes || hasDefines || hasMisc || hasAbstractions {
687-
content += "\nset_source_files_properties(\"" + AddRootPrefix(c.ContextRoot, file.File) + "\" PROPERTIES"
688-
if hasIncludes {
689-
content += "\n INCLUDE_DIRECTORIES " + ListIncludeDirectories(AddRootPrefixes(c.ContextRoot, file.AddPath), ";")
690-
}
691-
if hasDefines {
692-
content += "\n COMPILE_DEFINITIONS " + ListCompileDefinitions(file.Define, ";")
693-
}
694-
if hasMisc || hasAbstractions {
695-
content += "\n COMPILE_OPTIONS " + GetFileOptions(file, hasAbstractions, ";")
688+
content += "\nset_source_files_properties(\"" + AddRootPrefix(c.ContextRoot, file.File) +
689+
"\" PROPERTIES\n COMPILE_FLAGS \"${COMPILE_DEFINITIONS}\"\n)"
690+
} else {
691+
content += "\nset_source_files_properties(\"" + AddRootPrefix(c.ContextRoot, file.File) +
692+
"\" PROPERTIES\n COMPILE_DEFINITIONS \"" + ListCompileDefinitions(file.DefineAsm, ";") + "\"\n)"
696693
}
697-
content += "\n)"
698694
}
699695
return content
700696
}

pkg/maker/contextlists.go

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ include("` + toolchainConfig + `")
158158
func (c *Cbuild) CMakeCreateGroups(contextDir string) error {
159159
content := "# groups.cmake\n"
160160
abstractions := CompilerAbstractions{c.BuildDescType.Debug, c.BuildDescType.Optimize, c.BuildDescType.Warnings, c.BuildDescType.LanguageC, c.BuildDescType.LanguageCpp}
161-
content += c.CMakeCreateGroupRecursively("", c.BuildDescType.Groups, abstractions, c.BuildDescType.Misc.ASM)
161+
content += c.CMakeCreateGroupRecursively("", c.BuildDescType.Groups, abstractions, c.BuildDescType.DefineAsm, c.BuildDescType.Misc.ASM)
162162
filename := path.Join(contextDir, "groups.cmake")
163163
err := utils.UpdateFile(filename, content)
164164
if err != nil {
@@ -169,7 +169,7 @@ func (c *Cbuild) CMakeCreateGroups(contextDir string) error {
169169
}
170170

171171
func (c *Cbuild) CMakeCreateGroupRecursively(parent string, groups []Groups,
172-
parentAbstractions CompilerAbstractions, parentMiscAsm []string) string {
172+
parentAbstractions CompilerAbstractions, parentDefineAsm []interface{}, parentMiscAsm []string) string {
173173
var content string
174174
for _, group := range groups {
175175
miscAsm := utils.AppendUniquely(parentMiscAsm, group.Misc.ASM...)
@@ -201,6 +201,7 @@ func (c *Cbuild) CMakeCreateGroupRecursively(parent string, groups []Groups,
201201
content += CMakeTargetIncludeDirectories(name, c.MergeIncludes(buildFiles.Include, scope, parentName, group.AddPath, group.DelPath))
202202
// target_compile_definitions
203203
content += CMakeTargetCompileDefinitions(name, parentName, scope, group.Define, group.Undefine)
204+
group.DefineAsm = utils.AppendDefines(group.DefineAsm, parentDefineAsm)
204205
// compiler abstractions
205206
hasFileAbstractions := HasFileAbstractions(group.Files)
206207
groupAbstractions := CompilerAbstractions{group.Debug, group.Optimize, group.Warnings, group.LanguageC, group.LanguageCpp}
@@ -241,24 +242,32 @@ func (c *Cbuild) CMakeCreateGroupRecursively(parent string, groups []Groups,
241242
content += c.CMakeAddLibraryCustomFile(fileTargetName, file)
242243
// target_include_directories
243244
content += CMakeTargetIncludeDirectories(fileTargetName, c.MergeIncludes(ScopeMap{}, "PUBLIC", name, file.AddPath, file.DelPath))
244-
// target_compile_definitions
245-
content += CMakeTargetCompileDefinitions(fileTargetName, name, "PUBLIC", file.Define, file.Undefine)
245+
// target_compile_definitions (except asm)
246+
if GetLanguage(file) != "ASM" {
247+
content += CMakeTargetCompileDefinitions(fileTargetName, name, "PUBLIC", file.Define, file.Undefine)
248+
}
246249
// target_compile_options
247250
content += c.CMakeTargetCompileOptions(fileTargetName, "PUBLIC", Misc{}, []string{}, name)
248251
}
249-
// file properties
252+
// asm defines are set in file properties
253+
if GetLanguage(file) == "ASM" {
254+
file.DefineAsm = utils.AppendDefines(file.DefineAsm, group.DefineAsm)
255+
file.DefineAsm = utils.AppendDefines(file.Define, file.DefineAsm)
256+
content += c.SetFileAsmDefines(file, miscAsm)
257+
}
258+
// file compile options and abstractions
250259
fileAbstractions := CompilerAbstractions{file.Debug, file.Optimize, file.Warnings, file.LanguageC, file.LanguageCpp}
251260
if hasFileAbstractions {
252261
fileAbstractions = InheritCompilerAbstractions(abstractions, fileAbstractions)
253262
}
254-
content += c.CMakeSetFileProperties(file, fileAbstractions, miscAsm)
263+
content += c.CMakeSetFileProperties(file, fileAbstractions)
255264
}
256265
}
257266
content += "\n"
258267

259268
// create children groups recursively
260269
if hasChildren {
261-
content += c.CMakeCreateGroupRecursively(name, group.Groups, abstractions, miscAsm)
270+
content += c.CMakeCreateGroupRecursively(name, group.Groups, abstractions, group.DefineAsm, miscAsm)
262271
}
263272
c.BuildGroups = append(c.BuildGroups, name)
264273
}
@@ -285,6 +294,7 @@ func (c *Cbuild) CMakeCreateComponents(contextDir string) error {
285294
content += CMakeTargetIncludeDirectories(name, c.MergeIncludes(buildFiles.Include, scope, "${CONTEXT}", component.AddPath, component.DelPath))
286295
// target_compile_definitions
287296
content += CMakeTargetCompileDefinitions(name, "${CONTEXT}", scope, component.Define, component.Undefine)
297+
component.DefineAsm = utils.AppendDefines(component.DefineAsm, c.BuildDescType.DefineAsm)
288298
// compiler abstractions
289299
var libraries []string
290300
componentAbstractions := CompilerAbstractions{component.Debug, component.Optimize, component.Warnings, component.LanguageC, component.LanguageCpp}
@@ -307,7 +317,13 @@ func (c *Cbuild) CMakeCreateComponents(contextDir string) error {
307317
if len(libraries) > 0 {
308318
content += c.CMakeTargetLinkLibraries(name, scope, libraries...)
309319
}
310-
320+
// asm defines are set in file properties
321+
for _, file := range component.Files {
322+
if strings.Contains(file.Category, "source") && GetLanguage(file) == "ASM" {
323+
file.DefineAsm = utils.AppendDefines(file.DefineAsm, component.DefineAsm)
324+
content += c.SetFileAsmDefines(file, utils.AppendUniquely(c.BuildDescType.Misc.ASM, component.Misc.ASM...))
325+
}
326+
}
311327
content += "\n"
312328
}
313329

pkg/maker/parser.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ type Cbuild struct {
5858
LanguageCpp string `yaml:"language-CPP"`
5959
Misc Misc `yaml:"misc"`
6060
Define []interface{} `yaml:"define"`
61+
DefineAsm []interface{} `yaml:"define-asm"`
6162
AddPath []string `yaml:"add-path"`
6263
OutputDirs OutputDirs `yaml:"output-dirs"`
6364
Output []Output `yaml:"output"`
@@ -109,6 +110,7 @@ type Components struct {
109110
LanguageC string `yaml:"language-C"`
110111
LanguageCpp string `yaml:"language-CPP"`
111112
Define []interface{} `yaml:"define"`
113+
DefineAsm []interface{} `yaml:"define-asm"`
112114
Undefine []string `yaml:"undefine"`
113115
AddPath []string `yaml:"add-path"`
114116
DelPath []string `yaml:"del-path"`
@@ -140,6 +142,7 @@ type Files struct {
140142
LanguageC string `yaml:"language-C"`
141143
LanguageCpp string `yaml:"language-CPP"`
142144
Define []interface{} `yaml:"define"`
145+
DefineAsm []interface{} `yaml:"define-asm"`
143146
Undefine []string `yaml:"undefine"`
144147
AddPath []string `yaml:"add-path"`
145148
DelPath []string `yaml:"del-path"`
@@ -163,6 +166,7 @@ type Groups struct {
163166
LanguageC string `yaml:"language-C"`
164167
LanguageCpp string `yaml:"language-CPP"`
165168
Define []interface{} `yaml:"define"`
169+
DefineAsm []interface{} `yaml:"define-asm"`
166170
Undefine []string `yaml:"undefine"`
167171
AddPath []string `yaml:"add-path"`
168172
DelPath []string `yaml:"del-path"`

pkg/utils/utils.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ func RemoveIncludes(includes []string, delpaths ...string) []string {
4242
return includes
4343
}
4444

45+
func AppendDefines(defines []interface{}, elements []interface{}) []interface{} {
46+
return append(defines, elements...)
47+
}
48+
4549
func RemoveDefines(defines []interface{}, undefines ...string) []interface{} {
4650
for _, undefine := range undefines {
4751
for index, define := range defines {

test/data/solutions/build-asm/project/AC6/AsmArm.s

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,8 @@
1313
INFO 1, "STRING_TEST failed!"
1414
ENDIF
1515

16+
IF :LNOT::DEF:GROUP_ASM_AC6_DEF
17+
INFO 1, "GROUP_ASM_AC6_DEF is not defined!"
18+
ENDIF
19+
1620
END

test/data/solutions/build-asm/project/AC6/Auto.s

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,9 @@
55
INFO 1, "AUTO_DEF is not defined!"
66
ENDIF
77

8+
IF :LNOT::DEF:GROUP_ASM_AC6_DEF
9+
INFO 1, "GROUP_ASM_AC6_DEF is not defined!"
10+
ENDIF
11+
812
END
913

test/data/solutions/build-asm/project/AC6/GnuSyntax.s

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,9 @@
66
.error "GAS_DEF is not defined!"
77
.endif
88

9+
.ifndef GROUP_ASM_AC6_DEF
10+
.error "GROUP_ASM_AC6_DEF is not defined!"
11+
.endif
12+
913
.end
1014

test/data/solutions/build-asm/project/AC6/PreProcessed.S

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@
66
.error "PRE_PROCESSED_DEF is not defined!"
77
#endif
88

9+
#ifndef GROUP_ASM_AC6_DEF
10+
.error "GROUP_ASM_AC6_DEF is not defined!"
11+
#endif
12+
913
.end

test/data/solutions/build-asm/project/GCC/GAS.s

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@
66
.error "GAS_DEF is not defined!"
77
.endif
88

9+
.ifndef GROUP_ASM_GCC_CLANG_DEF
10+
.error "GROUP_ASM_GCC_CLANG_DEF is not defined!"
11+
.endif
12+
913
.end

test/data/solutions/build-asm/project/GCC/PreProcessed.S

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@
66
.error "PRE_PROCESSED_DEF is not defined!"
77
#endif
88

9+
#ifndef GROUP_ASM_GCC_CLANG_DEF
10+
.error "GROUP_ASM_GCC_CLANG_DEF is not defined!"
11+
#endif
12+
913
.end

0 commit comments

Comments
 (0)