build: separate compiler and libs

This commit is contained in:
Li Jie
2025-01-07 21:49:08 +08:00
parent b0123567cd
commit 1172e5bdce
559 changed files with 190 additions and 176 deletions

View File

@@ -0,0 +1,86 @@
/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package safesplit
import "strings"
// SplitPkgConfigFlags splits a pkg-config outputs string into parts.
// Each part starts with "-" followed by a single character flag.
// Spaces after the flag character are ignored.
// Content is read until the next space, unless escaped with "\".
func SplitPkgConfigFlags(s string) []string {
var result []string
var current strings.Builder
i := 0
// Skip leading whitespace
for i < len(s) && (s[i] == ' ' || s[i] == '\t') {
i++
}
for i < len(s) {
// Start a new part
if current.Len() > 0 {
result = append(result, strings.TrimSpace(current.String()))
current.Reset()
}
// Write "-" and the flag character
current.WriteByte('-')
i++
if i < len(s) {
current.WriteByte(s[i])
i++
}
// Skip spaces after flag character
for i < len(s) && (s[i] == ' ' || s[i] == '\t') {
i++
}
// Read content until next space
for i < len(s) {
if s[i] == '\\' && i+1 < len(s) && (s[i+1] == ' ' || s[i+1] == '\t') {
// Skip backslash and write the escaped space
i++
current.WriteByte(s[i])
i++
continue
}
if s[i] == ' ' || s[i] == '\t' {
// Skip consecutive spaces
j := i
for j < len(s) && (s[j] == ' ' || s[j] == '\t') {
j++
}
// If we've seen content, check for new flag
if j < len(s) && s[j] == '-' {
i = j
break
}
// Otherwise, include one space and continue
current.WriteByte(' ')
i = j
} else {
current.WriteByte(s[i])
i++
}
}
}
// Add the last part
if current.Len() > 0 {
result = append(result, strings.TrimSpace(current.String()))
}
return result
}

View File

@@ -0,0 +1,92 @@
/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package safesplit
import (
"strings"
"testing"
)
func TestSplitPkgConfigFlags(t *testing.T) {
ftest := func(s, want string) {
t.Helper() // for better error message
got := toString(SplitPkgConfigFlags(s))
if got != want {
t.Errorf("\nSplitPkgConfigFlags(%q) =\n got %v\nwant %v", s, got, want)
}
}
t.Run("basic", func(t *testing.T) {
ftest("-I/usr/include -L/usr/lib", `["-I/usr/include" "-L/usr/lib"]`)
ftest("-I /usr/include -L /usr/lib", `["-I/usr/include" "-L/usr/lib"]`)
ftest("-L/opt/homebrew/Cellar/bdw-gc/8.2.8/lib -lgc",
`["-L/opt/homebrew/Cellar/bdw-gc/8.2.8/lib" "-lgc"]`)
})
t.Run("spaces_in_path", func(t *testing.T) {
ftest("-I/usr/local/include directory -L/usr/local/lib path",
`["-I/usr/local/include directory" "-L/usr/local/lib path"]`)
})
t.Run("multiple_spaces", func(t *testing.T) {
ftest(" -I /usr/include -L /usr/lib ", `["-I/usr/include" "-L/usr/lib"]`)
})
t.Run("consecutive_flags", func(t *testing.T) {
ftest("-I -L", `["-I-L"]`)
ftest("-I -L /usr/lib", `["-I-L /usr/lib"]`)
})
t.Run("edge_cases", func(t *testing.T) {
ftest("", "[]")
ftest(" ", "[]")
ftest("-", `["-"]`)
ftest("-I", `["-I"]`)
ftest("-I -", `["-I-"]`)
})
t.Run("escaped_spaces", func(t *testing.T) {
ftest(`-I/path\ with\ spaces -L/lib`, `["-I/path with spaces" "-L/lib"]`)
ftest(`-I /first\ path -L /second\ long path`, `["-I/first path" "-L/second long path"]`)
})
t.Run("macro_flags", func(t *testing.T) {
ftest("-DMACRO -I/usr/include", `["-DMACRO" "-I/usr/include"]`)
ftest("-D MACRO -I/usr/include", `["-DMACRO" "-I/usr/include"]`)
ftest("-DMACRO=value -I/usr/include", `["-DMACRO=value" "-I/usr/include"]`)
ftest("-D MACRO=value -I/usr/include", `["-DMACRO=value" "-I/usr/include"]`)
ftest("-D_DEBUG -D_UNICODE -DWIN32", `["-D_DEBUG" "-D_UNICODE" "-DWIN32"]`)
ftest("-D _DEBUG -D _UNICODE -D WIN32", `["-D_DEBUG" "-D_UNICODE" "-DWIN32"]`)
ftest("-DVERSION=2.1 -DDEBUG=1", `["-DVERSION=2.1" "-DDEBUG=1"]`)
ftest("-D VERSION=2.1 -D DEBUG=1", `["-DVERSION=2.1" "-DDEBUG=1"]`)
})
}
func toString(ss []string) string {
if ss == nil {
return "[]"
}
s := "["
for i, v := range ss {
if i > 0 {
s += " "
}
v = strings.ReplaceAll(v, `"`, `\"`)
s += `"` + v + `"`
}
return s + "]"
}