Skip to content

Commit

Permalink
[cbuild2cmake] Handle define-asm at multiple levels
Browse files Browse the repository at this point in the history
  • Loading branch information
brondani authored May 15, 2024
1 parent 58c7d35 commit d20e4ab
Show file tree
Hide file tree
Showing 20 changed files with 115 additions and 39 deletions.
44 changes: 20 additions & 24 deletions pkg/maker/buildcontent.go
Original file line number Diff line number Diff line change
Expand Up @@ -619,8 +619,8 @@ func HasFileAbstractions(files []Files) bool {
}

func HasFileCustomOptions(file Files) bool {
if GetLanguage(file) != "ASM" &&
(len(file.AddPath) > 0 || len(file.Define) > 0 || len(file.DelPath) > 0 || len(file.Undefine) > 0) {
if len(file.AddPath) > 0 || len(file.DelPath) > 0 ||
(GetLanguage(file) != "ASM" && (len(file.Define) > 0 || len(file.Undefine) > 0)) {
return true
}
return false
Expand Down Expand Up @@ -655,46 +655,42 @@ func (c *Cbuild) CompilerAbstractions(abstractions CompilerAbstractions, languag
return content
}

func (c *Cbuild) CMakeSetFileProperties(file Files, abstractions CompilerAbstractions, parentMiscAsm []string) string {
func (c *Cbuild) CMakeSetFileProperties(file Files, abstractions CompilerAbstractions) string {
var content string
// file build options
language := GetLanguage(file)
hasIncludes := len(file.AddPath) > 0 && language == "ASM"
hasDefines := len(file.Define) > 0 && language == "ASM"
hasMisc := !IsCompileMiscEmpty(file.Misc)
// file compiler abstractions
hasAbstractions := !IsAbstractionEmpty(abstractions, language)
if hasAbstractions {
content += c.CompilerAbstractions(abstractions, language)
}
// handle specific asm defines
if hasDefines {
// set file properties
if hasMisc || hasAbstractions {
content += "\nset_source_files_properties(\"" + AddRootPrefix(c.ContextRoot, file.File) +
"\" PROPERTIES\n COMPILE_OPTIONS \"" + GetFileOptions(file, hasAbstractions, ";") + "\"\n)"
}
return content
}

func (c *Cbuild) SetFileAsmDefines(file Files, parentMiscAsm []string) string {
var content string
if len(file.DefineAsm) > 0 {
flags := utils.AppendUniquely(parentMiscAsm, file.Misc.ASM...)
if (c.Toolchain == "AC6" || c.Toolchain == "GCC") && path.Ext(file.File) != ".S" && !strings.Contains(utils.FindLast(flags, "-x"), "assembler-with-cpp") {
syntax := "AS_GNU"
masm := utils.FindLast(flags, "-masm")
if c.Toolchain == "AC6" && (strings.Contains(masm, "armasm") || strings.Contains(masm, "auto")) {
syntax = "AS_ARM"
}
content += "\nset(COMPILE_DEFINITIONS\n " + ListCompileDefinitions(file.Define, "\n ") + "\n)"
content += "\nset(COMPILE_DEFINITIONS\n " + ListCompileDefinitions(file.DefineAsm, "\n ") + "\n)"
content += "\ncbuild_set_defines(" + syntax + " COMPILE_DEFINITIONS)"
content += "\nset_source_files_properties(\"" + AddRootPrefix(c.ContextRoot, file.File) + "\" PROPERTIES\n COMPILE_FLAGS \"${COMPILE_DEFINITIONS}\"\n)"
hasDefines = false
}
}
// set file properties
if hasIncludes || hasDefines || hasMisc || hasAbstractions {
content += "\nset_source_files_properties(\"" + AddRootPrefix(c.ContextRoot, file.File) + "\" PROPERTIES"
if hasIncludes {
content += "\n INCLUDE_DIRECTORIES " + ListIncludeDirectories(AddRootPrefixes(c.ContextRoot, file.AddPath), ";")
}
if hasDefines {
content += "\n COMPILE_DEFINITIONS " + ListCompileDefinitions(file.Define, ";")
}
if hasMisc || hasAbstractions {
content += "\n COMPILE_OPTIONS " + GetFileOptions(file, hasAbstractions, ";")
content += "\nset_source_files_properties(\"" + AddRootPrefix(c.ContextRoot, file.File) +
"\" PROPERTIES\n COMPILE_FLAGS \"${COMPILE_DEFINITIONS}\"\n)"
} else {
content += "\nset_source_files_properties(\"" + AddRootPrefix(c.ContextRoot, file.File) +
"\" PROPERTIES\n COMPILE_DEFINITIONS \"" + ListCompileDefinitions(file.DefineAsm, ";") + "\"\n)"
}
content += "\n)"
}
return content
}
Expand Down
32 changes: 24 additions & 8 deletions pkg/maker/contextlists.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ include("` + toolchainConfig + `")
func (c *Cbuild) CMakeCreateGroups(contextDir string) error {
content := "# groups.cmake\n"
abstractions := CompilerAbstractions{c.BuildDescType.Debug, c.BuildDescType.Optimize, c.BuildDescType.Warnings, c.BuildDescType.LanguageC, c.BuildDescType.LanguageCpp}
content += c.CMakeCreateGroupRecursively("", c.BuildDescType.Groups, abstractions, c.BuildDescType.Misc.ASM)
content += c.CMakeCreateGroupRecursively("", c.BuildDescType.Groups, abstractions, c.BuildDescType.DefineAsm, c.BuildDescType.Misc.ASM)
filename := path.Join(contextDir, "groups.cmake")
err := utils.UpdateFile(filename, content)
if err != nil {
Expand All @@ -169,7 +169,7 @@ func (c *Cbuild) CMakeCreateGroups(contextDir string) error {
}

func (c *Cbuild) CMakeCreateGroupRecursively(parent string, groups []Groups,
parentAbstractions CompilerAbstractions, parentMiscAsm []string) string {
parentAbstractions CompilerAbstractions, parentDefineAsm []interface{}, parentMiscAsm []string) string {
var content string
for _, group := range groups {
miscAsm := utils.AppendUniquely(parentMiscAsm, group.Misc.ASM...)
Expand Down Expand Up @@ -201,6 +201,7 @@ func (c *Cbuild) CMakeCreateGroupRecursively(parent string, groups []Groups,
content += CMakeTargetIncludeDirectories(name, c.MergeIncludes(buildFiles.Include, scope, parentName, group.AddPath, group.DelPath))
// target_compile_definitions
content += CMakeTargetCompileDefinitions(name, parentName, scope, group.Define, group.Undefine)
group.DefineAsm = utils.AppendDefines(group.DefineAsm, parentDefineAsm)
// compiler abstractions
hasFileAbstractions := HasFileAbstractions(group.Files)
groupAbstractions := CompilerAbstractions{group.Debug, group.Optimize, group.Warnings, group.LanguageC, group.LanguageCpp}
Expand Down Expand Up @@ -241,24 +242,32 @@ func (c *Cbuild) CMakeCreateGroupRecursively(parent string, groups []Groups,
content += c.CMakeAddLibraryCustomFile(fileTargetName, file)
// target_include_directories
content += CMakeTargetIncludeDirectories(fileTargetName, c.MergeIncludes(ScopeMap{}, "PUBLIC", name, file.AddPath, file.DelPath))
// target_compile_definitions
content += CMakeTargetCompileDefinitions(fileTargetName, name, "PUBLIC", file.Define, file.Undefine)
// target_compile_definitions (except asm)
if GetLanguage(file) != "ASM" {
content += CMakeTargetCompileDefinitions(fileTargetName, name, "PUBLIC", file.Define, file.Undefine)
}
// target_compile_options
content += c.CMakeTargetCompileOptions(fileTargetName, "PUBLIC", Misc{}, []string{}, name)
}
// file properties
// asm defines are set in file properties
if GetLanguage(file) == "ASM" {
file.DefineAsm = utils.AppendDefines(file.DefineAsm, group.DefineAsm)
file.DefineAsm = utils.AppendDefines(file.Define, file.DefineAsm)
content += c.SetFileAsmDefines(file, miscAsm)
}
// file compile options and abstractions
fileAbstractions := CompilerAbstractions{file.Debug, file.Optimize, file.Warnings, file.LanguageC, file.LanguageCpp}
if hasFileAbstractions {
fileAbstractions = InheritCompilerAbstractions(abstractions, fileAbstractions)
}
content += c.CMakeSetFileProperties(file, fileAbstractions, miscAsm)
content += c.CMakeSetFileProperties(file, fileAbstractions)
}
}
content += "\n"

// create children groups recursively
if hasChildren {
content += c.CMakeCreateGroupRecursively(name, group.Groups, abstractions, miscAsm)
content += c.CMakeCreateGroupRecursively(name, group.Groups, abstractions, group.DefineAsm, miscAsm)
}
c.BuildGroups = append(c.BuildGroups, name)
}
Expand All @@ -285,6 +294,7 @@ func (c *Cbuild) CMakeCreateComponents(contextDir string) error {
content += CMakeTargetIncludeDirectories(name, c.MergeIncludes(buildFiles.Include, scope, "${CONTEXT}", component.AddPath, component.DelPath))
// target_compile_definitions
content += CMakeTargetCompileDefinitions(name, "${CONTEXT}", scope, component.Define, component.Undefine)
component.DefineAsm = utils.AppendDefines(component.DefineAsm, c.BuildDescType.DefineAsm)
// compiler abstractions
var libraries []string
componentAbstractions := CompilerAbstractions{component.Debug, component.Optimize, component.Warnings, component.LanguageC, component.LanguageCpp}
Expand All @@ -307,7 +317,13 @@ func (c *Cbuild) CMakeCreateComponents(contextDir string) error {
if len(libraries) > 0 {
content += c.CMakeTargetLinkLibraries(name, scope, libraries...)
}

// asm defines are set in file properties
for _, file := range component.Files {
if strings.Contains(file.Category, "source") && GetLanguage(file) == "ASM" {
file.DefineAsm = utils.AppendDefines(file.DefineAsm, component.DefineAsm)
content += c.SetFileAsmDefines(file, utils.AppendUniquely(c.BuildDescType.Misc.ASM, component.Misc.ASM...))
}
}
content += "\n"
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/maker/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type Cbuild struct {
LanguageCpp string `yaml:"language-CPP"`
Misc Misc `yaml:"misc"`
Define []interface{} `yaml:"define"`
DefineAsm []interface{} `yaml:"define-asm"`
AddPath []string `yaml:"add-path"`
OutputDirs OutputDirs `yaml:"output-dirs"`
Output []Output `yaml:"output"`
Expand Down Expand Up @@ -109,6 +110,7 @@ type Components struct {
LanguageC string `yaml:"language-C"`
LanguageCpp string `yaml:"language-CPP"`
Define []interface{} `yaml:"define"`
DefineAsm []interface{} `yaml:"define-asm"`
Undefine []string `yaml:"undefine"`
AddPath []string `yaml:"add-path"`
DelPath []string `yaml:"del-path"`
Expand Down Expand Up @@ -140,6 +142,7 @@ type Files struct {
LanguageC string `yaml:"language-C"`
LanguageCpp string `yaml:"language-CPP"`
Define []interface{} `yaml:"define"`
DefineAsm []interface{} `yaml:"define-asm"`
Undefine []string `yaml:"undefine"`
AddPath []string `yaml:"add-path"`
DelPath []string `yaml:"del-path"`
Expand All @@ -163,6 +166,7 @@ type Groups struct {
LanguageC string `yaml:"language-C"`
LanguageCpp string `yaml:"language-CPP"`
Define []interface{} `yaml:"define"`
DefineAsm []interface{} `yaml:"define-asm"`
Undefine []string `yaml:"undefine"`
AddPath []string `yaml:"add-path"`
DelPath []string `yaml:"del-path"`
Expand Down
4 changes: 4 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ func RemoveIncludes(includes []string, delpaths ...string) []string {
return includes
}

func AppendDefines(defines []interface{}, elements []interface{}) []interface{} {
return append(defines, elements...)
}

func RemoveDefines(defines []interface{}, undefines ...string) []interface{} {
for _, undefine := range undefines {
for index, define := range defines {
Expand Down
4 changes: 4 additions & 0 deletions test/data/solutions/build-asm/project/AC6/AsmArm.s
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@
INFO 1, "STRING_TEST failed!"
ENDIF

IF :LNOT::DEF:GROUP_ASM_AC6_DEF
INFO 1, "GROUP_ASM_AC6_DEF is not defined!"
ENDIF

END
4 changes: 4 additions & 0 deletions test/data/solutions/build-asm/project/AC6/Auto.s
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@
INFO 1, "AUTO_DEF is not defined!"
ENDIF

IF :LNOT::DEF:GROUP_ASM_AC6_DEF
INFO 1, "GROUP_ASM_AC6_DEF is not defined!"
ENDIF

END

4 changes: 4 additions & 0 deletions test/data/solutions/build-asm/project/AC6/GnuSyntax.s
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@
.error "GAS_DEF is not defined!"
.endif

.ifndef GROUP_ASM_AC6_DEF
.error "GROUP_ASM_AC6_DEF is not defined!"
.endif

.end

4 changes: 4 additions & 0 deletions test/data/solutions/build-asm/project/AC6/PreProcessed.S
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@
.error "PRE_PROCESSED_DEF is not defined!"
#endif

#ifndef GROUP_ASM_AC6_DEF
.error "GROUP_ASM_AC6_DEF is not defined!"
#endif

.end
4 changes: 4 additions & 0 deletions test/data/solutions/build-asm/project/GCC/GAS.s
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@
.error "GAS_DEF is not defined!"
.endif

.ifndef GROUP_ASM_GCC_CLANG_DEF
.error "GROUP_ASM_GCC_CLANG_DEF is not defined!"
.endif

.end
4 changes: 4 additions & 0 deletions test/data/solutions/build-asm/project/GCC/PreProcessed.S
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@
.error "PRE_PROCESSED_DEF is not defined!"
#endif

#ifndef GROUP_ASM_GCC_CLANG_DEF
.error "GROUP_ASM_GCC_CLANG_DEF is not defined!"
#endif

.end
4 changes: 4 additions & 0 deletions test/data/solutions/build-asm/project/IAR/Asm.s
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

#ifndef IAR_ASM_DEF
#error "IAR_ASM_DEF is not defined!"
#endif

#ifndef GROUP_ASM_IAR_DEF
#error "GROUP_ASM_IAR_DEF is not defined!"
#endif

EXTERN Reset_Handler_C
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ build:
- file: ./main.c
category: sourceC
- group: AC6
define-asm:
- GROUP_ASM_AC6_DEF
files:
- file: ./AC6/AsmArm.s
category: sourceAsm
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ build:
- file: ./main.c
category: sourceC
- group: GCC-CLANG
define-asm:
- GROUP_ASM_GCC_CLANG_DEF
files:
- file: ./GCC/PreProcessed.S
category: sourceAsm
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ build:
- file: ./main.c
category: sourceC
- group: GCC-CLANG
define-asm:
- GROUP_ASM_GCC_CLANG_DEF
files:
- file: ./GCC/GAS.s
category: sourceAsm
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ build:
- file: ./main.c
category: sourceC
- group: IAR
define-asm:
- GROUP_ASM_IAR_DEF
files:
- file: ./IAR/Asm.s
category: sourceAsm
Expand Down
6 changes: 6 additions & 0 deletions test/data/solutions/build-asm/project/project.cproject.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ project:

- group: GCC-CLANG
for-compiler: [GCC, CLANG]
define-asm:
- GROUP_ASM_GCC_CLANG_DEF
files:
- file: ./GCC/GAS.s
for-compiler: GCC
Expand All @@ -26,6 +28,8 @@ project:

- group: AC6
for-compiler: AC6
define-asm:
- GROUP_ASM_AC6_DEF
files:
- file: ./AC6/AsmArm.s
define:
Expand All @@ -47,6 +51,8 @@ project:

- group: IAR
for-compiler: IAR
define-asm:
- GROUP_ASM_IAR_DEF
files:
- file: ./IAR/Asm.s
define:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,29 @@ set(COMPILE_DEFINITIONS
HEXADECIMAL_TEST=11259375
DECIMAL_TEST=1234567890
STRING_TEST="String0"
GROUP_ASM_AC6_DEF
)
cbuild_set_defines(AS_ARM COMPILE_DEFINITIONS)
set_source_files_properties("${SOLUTION_ROOT}/project/AC6/AsmArm.s" PROPERTIES
COMPILE_FLAGS "${COMPILE_DEFINITIONS}"
)
set(COMPILE_DEFINITIONS
GAS_DEF
GROUP_ASM_AC6_DEF
)
cbuild_set_defines(AS_GNU COMPILE_DEFINITIONS)
set_source_files_properties("${SOLUTION_ROOT}/project/AC6/GnuSyntax.s" PROPERTIES
COMPILE_FLAGS "${COMPILE_DEFINITIONS}"
)
set_source_files_properties("${SOLUTION_ROOT}/project/AC6/GnuSyntax.s" PROPERTIES
COMPILE_OPTIONS -masm=gnu
COMPILE_OPTIONS "-masm=gnu"
)
set_source_files_properties("${SOLUTION_ROOT}/project/AC6/PreProcessed.S" PROPERTIES
COMPILE_DEFINITIONS PRE_PROCESSED_DEF
COMPILE_DEFINITIONS "PRE_PROCESSED_DEF;GROUP_ASM_AC6_DEF"
)
set(COMPILE_DEFINITIONS
AUTO_DEF
GROUP_ASM_AC6_DEF
)
cbuild_set_defines(AS_ARM COMPILE_DEFINITIONS)
set_source_files_properties("${SOLUTION_ROOT}/project/AC6/Auto.s" PROPERTIES
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ target_compile_options(Group_GCC-CLANG PUBLIC
$<TARGET_PROPERTY:${CONTEXT},INTERFACE_COMPILE_OPTIONS>
)
set_source_files_properties("${SOLUTION_ROOT}/project/GCC/PreProcessed.S" PROPERTIES
COMPILE_DEFINITIONS PRE_PROCESSED_DEF
COMPILE_DEFINITIONS "PRE_PROCESSED_DEF;GROUP_ASM_GCC_CLANG_DEF"
)
set_source_files_properties("${SOLUTION_ROOT}/project/GCC/NonPreProcessed.s" PROPERTIES
COMPILE_OPTIONS -DPRE_PROCESSED_DEF
COMPILE_DEFINITIONS "GROUP_ASM_GCC_CLANG_DEF"
)
set_source_files_properties("${SOLUTION_ROOT}/project/GCC/NonPreProcessed.s" PROPERTIES
COMPILE_OPTIONS "-DPRE_PROCESSED_DEF"
)
Loading

0 comments on commit d20e4ab

Please sign in to comment.