Skip to content

Commit ddd935c

Browse files
authored
Handle library paths with spaces
1 parent 99ccdb9 commit ddd935c

6 files changed

Lines changed: 36 additions & 2 deletions

File tree

pkg/maker/buildcontent.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ func (c *Cbuild) CMakeTargetLinkLibrariesGlobal() string {
389389

390390
// set content
391391
for _, library := range libraries {
392-
content += "\n " + library
392+
content += "\n " + quoteEntry(library)
393393
}
394394
content += "\n)"
395395
return content
@@ -398,12 +398,27 @@ func (c *Cbuild) CMakeTargetLinkLibrariesGlobal() string {
398398
func (c *Cbuild) CMakeTargetLinkLibraries(name string, scope string, libraries ...string) string {
399399
content := "\ntarget_link_libraries(" + name + " " + scope
400400
for _, library := range libraries {
401-
content += "\n " + library
401+
content += "\n " + quoteEntry(library)
402402
}
403403
content += "\n)"
404404
return content
405405
}
406406

407+
func quoteEntry(entry string) string {
408+
trimmed := strings.TrimLeft(entry, " ")
409+
if len(trimmed) == 0 {
410+
return entry
411+
}
412+
if !strings.Contains(trimmed, " ") {
413+
return entry
414+
}
415+
leadingSpaces := entry[:len(entry)-len(trimmed)]
416+
if strings.HasPrefix(trimmed, "\"") && strings.HasSuffix(trimmed, "\"") {
417+
return entry
418+
}
419+
return leadingSpaces + "\"" + trimmed + "\""
420+
}
421+
407422
func (c *Cbuild) CMakeTargetCompileOptions(name string, scope string, lto bool, misc Misc, preIncludes []string, parent string) string {
408423
content := "\ntarget_compile_options(" + name + " " + scope
409424
content += "\n $<TARGET_PROPERTY:" + parent + ",INTERFACE_COMPILE_OPTIONS>"

pkg/maker/buildcontent_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,20 @@ add_dependencies(project.debug+target-executes
455455
assert.Contains(linkerOptions, "--linkCPP-flag")
456456
})
457457

458+
t.Run("test quote link libraries with spaces", func(t *testing.T) {
459+
var cbuild maker.Cbuild
460+
content := cbuild.CMakeTargetLinkLibraries("TARGET", "PUBLIC",
461+
"libnospace.a",
462+
" libnospace2.a",
463+
" C:/Program Files/vendor/lib with spaces.a",
464+
" \"C:/Program Files/vendor/already quoted.a\"")
465+
466+
assert.Contains(content, "\n libnospace.a")
467+
assert.Contains(content, "\n libnospace2.a")
468+
assert.Contains(content, "\n \"C:/Program Files/vendor/lib with spaces.a\"")
469+
assert.Contains(content, "\n \"C:/Program Files/vendor/already quoted.a\"")
470+
})
471+
458472
t.Run("test linker options with pre-processing", func(t *testing.T) {
459473
var cbuild maker.Cbuild
460474
define := make([]interface{}, 1)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/* Dummy */

test/data/solutions/blanks/project/project X.AC6 X+ARMCM0 X.cbuild.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ build:
107107
files:
108108
- file: main.c
109109
category: sourceC
110+
- file: libr ary.a
111+
category: library
110112
constructed-files:
111113
- file: RTE/_AC6_X_ARMCM0_X/RTE_Components.h
112114
category: header

test/data/solutions/blanks/project/project X.cproject.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ project:
88
- group: Source
99
files:
1010
- file: ./main.c
11+
- file: ./libr ary.a

test/data/solutions/blanks/ref/project X.AC6 X+ARMCM0 X/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ target_link_libraries(${CONTEXT} PUBLIC
8383
Group_Source
8484
ARM_CMSIS_CORE_6_1_0
8585
ARM_Device_Startup_C_Startup_2_2_0
86+
"${SOLUTION_ROOT}/project/libr ary.a"
8687
)
8788

8889
# Linker options

0 commit comments

Comments
 (0)